Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
| 4 | * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 25 | #include <import/ebmbtree.h> |
| 26 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | #include <haproxy/buf-t.h> |
| 28 | #include <haproxy/compat.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/debug.h> |
| 31 | #include <haproxy/tools.h> |
| 32 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 33 | |
| 34 | #include <haproxy/connection.h> |
| 35 | #include <haproxy/fd.h> |
| 36 | #include <haproxy/freq_ctr.h> |
| 37 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 39 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 41 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 47 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 48 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 49 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/ssl_sock.h> |
| 51 | #include <haproxy/stream_interface.h> |
| 52 | #include <haproxy/task.h> |
| 53 | #include <haproxy/trace.h> |
| 54 | #include <haproxy/xprt_quic.h> |
| 55 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 56 | /* list of supported QUIC versions by this implementation */ |
| 57 | static int quic_supported_version[] = { |
| 58 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 59 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 60 | |
| 61 | /* placeholder, do not add entry after this */ |
| 62 | 0x0 |
| 63 | }; |
| 64 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 65 | /* This is the values of some QUIC transport parameters when absent. |
| 66 | * Should be used to initialize any transport parameters (local or remote) |
| 67 | * before updating them with customized values. |
| 68 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 69 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 70 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 71 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 72 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 73 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* trace source and events */ |
| 77 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 78 | const struct trace_source *src, |
| 79 | const struct ist where, const struct ist func, |
| 80 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 81 | |
| 82 | static const struct trace_event quic_trace_events[] = { |
| 83 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 84 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 85 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 86 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 89 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 90 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 91 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 92 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 95 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 96 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 100 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 102 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 103 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 105 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 106 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 107 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 108 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 109 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 110 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 111 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 112 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 113 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 114 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 115 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 116 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 117 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 118 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 119 | { /* end */ } |
| 120 | }; |
| 121 | |
| 122 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 123 | /* arg1 */ { /* already used by the connection */ }, |
| 124 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 125 | /* arg3 */ { }, |
| 126 | /* arg4 */ { } |
| 127 | }; |
| 128 | |
| 129 | static const struct name_desc quic_trace_decoding[] = { |
| 130 | #define QUIC_VERB_CLEAN 1 |
| 131 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 132 | { /* end */ } |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | struct trace_source trace_quic = { |
| 137 | .name = IST("quic"), |
| 138 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 139 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 140 | .default_cb = quic_trace, |
| 141 | .known_events = quic_trace_events, |
| 142 | .lockon_args = quic_trace_lockon_args, |
| 143 | .decoding = quic_trace_decoding, |
| 144 | .report_events = ~0, /* report everything by default */ |
| 145 | }; |
| 146 | |
| 147 | #define TRACE_SOURCE &trace_quic |
| 148 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 149 | |
| 150 | static BIO_METHOD *ha_quic_meth; |
| 151 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 152 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 153 | DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 154 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 155 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 156 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 157 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 158 | DECLARE_POOL(pool_head_quic_connection_id, |
| 159 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 161 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 163 | DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 165 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 166 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 167 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 168 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 169 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 170 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 171 | int padding, int ack, int nb_pto_dgrams, int cc, int *err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 172 | |
| 173 | /* Add traces to <buf> depending on <frm> TX frame type. */ |
| 174 | static inline void chunk_tx_frm_appendf(struct buffer *buf, |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 175 | const struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 176 | { |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 177 | chunk_appendf(buf, " %s", quic_frame_type_string(frm->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 178 | switch (frm->type) { |
| 179 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 180 | { |
| 181 | const struct quic_crypto *cf = &frm->crypto; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 182 | chunk_appendf(buf, " cfoff=%llu cflen=%llu", |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 183 | (ull)cf->offset, (ull)cf->len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 184 | break; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 185 | } |
| 186 | case QUIC_FT_STOP_SENDING: |
| 187 | { |
| 188 | const struct quic_stop_sending *s = &frm->stop_sending; |
| 189 | chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu", |
| 190 | (ull)s->id, (ull)s->app_error_code); |
| 191 | break; |
| 192 | } |
| 193 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 194 | { |
| 195 | const struct quic_stream *s = &frm->stream; |
| 196 | chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu", |
| 197 | !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT), |
| 198 | !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT), |
| 199 | (ull)s->id, (ull)s->offset.key, (ull)s->len); |
| 200 | break; |
| 201 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 205 | /* Only for debug purpose */ |
| 206 | struct enc_debug_info { |
| 207 | unsigned char *payload; |
| 208 | size_t payload_len; |
| 209 | unsigned char *aad; |
| 210 | size_t aad_len; |
| 211 | uint64_t pn; |
| 212 | }; |
| 213 | |
| 214 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 215 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 216 | unsigned char *payload, size_t payload_len, |
| 217 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 218 | { |
| 219 | edi->payload = payload; |
| 220 | edi->payload_len = payload_len; |
| 221 | edi->aad = aad; |
| 222 | edi->aad_len = aad_len; |
| 223 | edi->pn = pn; |
| 224 | } |
| 225 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 226 | /* Trace callback for QUIC. |
| 227 | * These traces always expect that arg1, if non-null, is of type connection. |
| 228 | */ |
| 229 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 230 | const struct ist where, const struct ist func, |
| 231 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 232 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 233 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 234 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 235 | if (qc) { |
| 236 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 237 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 238 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 239 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 240 | chunk_appendf(&trace_buf, "\n odcid"); |
| 241 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 242 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 243 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 244 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 245 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 249 | const enum ssl_encryption_level_t *level = a2; |
| 250 | const size_t *len = a3; |
| 251 | |
| 252 | if (level) { |
| 253 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 254 | |
| 255 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 256 | } |
| 257 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 258 | 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] | 259 | } |
| 260 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 261 | /* Initial read & write secrets. */ |
| 262 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 263 | const unsigned char *rx_sec = a2; |
| 264 | const unsigned char *tx_sec = a3; |
| 265 | |
| 266 | secs = &qc->els[level].tls_ctx.rx; |
| 267 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 268 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 269 | if (rx_sec) |
| 270 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 271 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 272 | } |
| 273 | secs = &qc->els[level].tls_ctx.tx; |
| 274 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 275 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 276 | if (tx_sec) |
| 277 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 278 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 279 | } |
| 280 | } |
| 281 | if (mask & (QUIC_EV_CONN_RSEC|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 RX 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.rx; |
| 293 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 294 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 299 | const enum ssl_encryption_level_t *level = a2; |
| 300 | const unsigned char *secret = a3; |
| 301 | const size_t *secret_len = a4; |
| 302 | |
| 303 | if (level) { |
| 304 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 305 | |
| 306 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 307 | if (secret && secret_len) |
| 308 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 309 | secs = &qc->els[lvl].tls_ctx.tx; |
| 310 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 311 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 312 | } |
| 313 | |
| 314 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 315 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 316 | 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] | 317 | const struct quic_tx_packet *pkt = a2; |
| 318 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 319 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 320 | |
| 321 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 322 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 323 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 324 | "if=%llu pp=%u pdg=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 325 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 326 | (unsigned long long)qc->path->cwnd, |
| 327 | (unsigned long long)qc->path->prep_in_flight, |
| 328 | (unsigned long long)qc->path->in_flight, |
| 329 | (unsigned long long)pktns->tx.in_flight, |
| 330 | pktns->tx.pto_probe, qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 331 | } |
| 332 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 333 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 334 | if (pkt->pn_node.key != (uint64_t)-1) |
| 335 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 336 | list_for_each_entry(frm, &pkt->frms, list) |
| 337 | chunk_tx_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 338 | chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes); |
| 339 | } |
| 340 | |
| 341 | if (room) { |
| 342 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 343 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 344 | (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] | 345 | } |
| 346 | } |
| 347 | |
| 348 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 349 | const enum quic_handshake_state *state = a2; |
| 350 | const int *err = a3; |
| 351 | |
| 352 | if (state) |
| 353 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 354 | if (err) |
| 355 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 356 | } |
| 357 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 358 | 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] | 359 | const struct quic_rx_packet *pkt = a2; |
| 360 | const unsigned long *pktlen = a3; |
| 361 | const SSL *ssl = a4; |
| 362 | |
| 363 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 364 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 365 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 366 | if (pkt->pnl) |
| 367 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 368 | (unsigned long long)pkt->pn); |
| 369 | if (pkt->token_len) |
| 370 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 371 | (unsigned long long)pkt->token_len); |
| 372 | if (pkt->aad_len) |
| 373 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 374 | (unsigned long long)pkt->aad_len); |
| 375 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 376 | pkt->flags, (unsigned long long)pkt->len); |
| 377 | } |
| 378 | if (pktlen) |
| 379 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 380 | if (ssl) { |
| 381 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 382 | chunk_appendf(&trace_buf, " el=%c", |
| 383 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 388 | const struct quic_rx_packet *pkt = a2; |
| 389 | const struct quic_rx_crypto_frm *cf = a3; |
| 390 | const SSL *ssl = a4; |
| 391 | |
| 392 | if (pkt) |
| 393 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 394 | quic_packet_type_enc_level_char(pkt->type), |
| 395 | (unsigned long long)pkt->pn); |
| 396 | if (cf) |
| 397 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 398 | (unsigned long long)cf->offset_node.key, |
| 399 | (unsigned long long)cf->len); |
| 400 | if (ssl) { |
| 401 | 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] | 402 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 403 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 404 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 405 | |
| 406 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 407 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 411 | const struct quic_frame *frm = a2; |
| 412 | |
| 413 | if (frm) |
| 414 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 415 | } |
| 416 | |
| 417 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 418 | const struct quic_enc_level *qel = a2; |
| 419 | |
| 420 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 421 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 422 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 423 | " 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] | 424 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 425 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 426 | !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 427 | (unsigned long long)qc->path->cwnd, |
| 428 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 429 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 430 | (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe, |
| 431 | (unsigned long long)qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 435 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 436 | const struct enc_debug_info *edi = a2; |
| 437 | |
| 438 | if (edi) |
| 439 | chunk_appendf(&trace_buf, |
| 440 | " payload=@%p payload_len=%llu" |
| 441 | " aad=@%p aad_len=%llu pn=%llu", |
| 442 | edi->payload, (unsigned long long)edi->payload_len, |
| 443 | edi->aad, (unsigned long long)edi->aad_len, |
| 444 | (unsigned long long)edi->pn); |
| 445 | } |
| 446 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 447 | if (mask & QUIC_EV_CONN_RMHP) { |
| 448 | const struct quic_rx_packet *pkt = a2; |
| 449 | |
| 450 | if (pkt) { |
| 451 | const int *ret = a3; |
| 452 | |
| 453 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 454 | if (ret && *ret) |
| 455 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 456 | pkt->pnl, (unsigned long long)pkt->pn); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 461 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 462 | const unsigned long *val1 = a3; |
| 463 | const unsigned long *val2 = a4; |
| 464 | |
| 465 | if (frm) |
| 466 | chunk_tx_frm_appendf(&trace_buf, frm); |
| 467 | if (val1) |
| 468 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 469 | if (val2) |
| 470 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 471 | } |
| 472 | |
| 473 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 474 | const unsigned int *rtt_sample = a2; |
| 475 | const unsigned int *ack_delay = a3; |
| 476 | const struct quic_loss *ql = a4; |
| 477 | |
| 478 | if (rtt_sample) |
| 479 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 480 | if (ack_delay) |
| 481 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 482 | if (ql) |
| 483 | chunk_appendf(&trace_buf, |
| 484 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 485 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 486 | } |
| 487 | if (mask & QUIC_EV_CONN_CC) { |
| 488 | const struct quic_cc_event *ev = a2; |
| 489 | const struct quic_cc *cc = a3; |
| 490 | |
| 491 | if (a2) |
| 492 | quic_cc_event_trace(&trace_buf, ev); |
| 493 | if (a3) |
| 494 | quic_cc_state_trace(&trace_buf, cc); |
| 495 | } |
| 496 | |
| 497 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 498 | const struct quic_pktns *pktns = a2; |
| 499 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 500 | |
| 501 | if (pktns) { |
| 502 | chunk_appendf(&trace_buf, " pktns=%s", |
| 503 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 504 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 505 | if (pktns->tx.loss_time) |
| 506 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 507 | 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] | 508 | } |
| 509 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 510 | struct quic_tx_packet *pkt; |
| 511 | |
| 512 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 513 | list_for_each_entry(pkt, lost_pkts, list) |
| 514 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | const struct quic_pktns *pktns = a2; |
| 520 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 521 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 522 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 523 | if (ifae_pkts) |
| 524 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 525 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 526 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 527 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 528 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 529 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 530 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 531 | if (mask & QUIC_EV_CONN_STIMER) { |
| 532 | if (pktns->tx.loss_time) |
| 533 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 534 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 535 | } |
| 536 | if (mask & QUIC_EV_CONN_SPTO) { |
| 537 | if (pktns->tx.time_of_last_eliciting) |
| 538 | chunk_appendf(&trace_buf, " tole=%dms", |
| 539 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 540 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 541 | 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] | 542 | } |
| 543 | } |
| 544 | |
| 545 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 546 | chunk_appendf(&trace_buf, |
| 547 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 552 | const struct quic_tx_packet *pkt = a2; |
| 553 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 554 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 555 | (unsigned long long)qc->path->cwnd, |
| 556 | (unsigned long long)qc->path->prep_in_flight, |
| 557 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 558 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 559 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 560 | (unsigned long)pkt->pn_node.key, |
| 561 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 562 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 563 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 566 | |
| 567 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 568 | const uint8_t *alert = a2; |
| 569 | const enum ssl_encryption_level_t *level = a3; |
| 570 | |
| 571 | if (alert) |
| 572 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 573 | if (level) |
| 574 | chunk_appendf(&trace_buf, " el=%c", |
| 575 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 576 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 577 | |
| 578 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 579 | const size_t *sz1 = a2; |
| 580 | const size_t *sz2 = a3; |
| 581 | const size_t *sz3 = a4; |
| 582 | |
| 583 | if (sz1) |
| 584 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 585 | if (sz2) |
| 586 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 587 | if (sz3) |
| 588 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 589 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 590 | |
| 591 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 592 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 593 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 594 | if (frm) |
| 595 | chunk_tx_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 596 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 597 | } |
| 598 | if (mask & QUIC_EV_CONN_LPKT) { |
| 599 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 600 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 601 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 602 | if (pkt) { |
| 603 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 604 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 605 | if (pkt->pn_node.key != (uint64_t)-1) |
| 606 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 607 | } |
| 608 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 609 | if (len) |
| 610 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | } |
| 614 | |
| 615 | /* 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] | 616 | 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] | 617 | { |
| 618 | struct quic_conn *qc; |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 619 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 620 | |
| 621 | qc = ctx->conn->qc; |
| 622 | if (objt_server(qc->conn->target)) |
| 623 | return 1; |
| 624 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 625 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 626 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 627 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 628 | (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] | 629 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 630 | return 1; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 636 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 637 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 638 | 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] | 639 | { |
| 640 | struct quic_conn *qc; |
| 641 | struct quic_pktns *pktns; |
| 642 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 643 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 644 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 645 | qc = ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 646 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
| 647 | NULL, NULL, &ctx->conn->qc->path->ifae_pkts); |
| 648 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 649 | pktns = quic_loss_pktns(qc); |
| 650 | if (tick_isset(pktns->tx.loss_time)) { |
| 651 | qc->timer = pktns->tx.loss_time; |
| 652 | goto out; |
| 653 | } |
| 654 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 655 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 656 | * cancelled for a server which reached the anti-amplification limit. |
| 657 | */ |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 658 | if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 659 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 660 | qc->timer = TICK_ETERNITY; |
| 661 | goto out; |
| 662 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 663 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 664 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 665 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 666 | /* Timer cancellation. */ |
| 667 | qc->timer = TICK_ETERNITY; |
| 668 | goto out; |
| 669 | } |
| 670 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 671 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 672 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 673 | if (tick_isset(pto)) |
| 674 | qc->timer = pto; |
| 675 | out: |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 676 | if (qc->timer != TICK_ETERNITY) |
| 677 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 678 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 681 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 682 | * connection. |
| 683 | * Return 1 if succeeded, 0 if not. |
| 684 | */ |
| 685 | static int quic_tls_key_update(struct quic_conn *qc) |
| 686 | { |
| 687 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 688 | struct quic_tls_secrets *rx, *tx; |
| 689 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 690 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 691 | |
| 692 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 693 | rx = &tls_ctx->rx; |
| 694 | tx = &tls_ctx->tx; |
| 695 | nxt_rx = &qc->ku.nxt_rx; |
| 696 | nxt_tx = &qc->ku.nxt_tx; |
| 697 | |
| 698 | /* Prepare new RX secrets */ |
| 699 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 700 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 701 | TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 706 | nxt_rx->key, nxt_rx->keylen, |
| 707 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 708 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 709 | TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | /* Prepare new TX secrets */ |
| 714 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 715 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 716 | TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 721 | nxt_tx->key, nxt_tx->keylen, |
| 722 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 723 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 724 | TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | return 1; |
| 729 | } |
| 730 | |
| 731 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 732 | * Must be used after having updated them. |
| 733 | * Always succeeds. |
| 734 | */ |
| 735 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 736 | { |
| 737 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 738 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 739 | |
| 740 | /* Rotate the RX secrets */ |
| 741 | curr_secret = tls_ctx->rx.secret; |
| 742 | curr_iv = tls_ctx->rx.iv; |
| 743 | curr_key = tls_ctx->rx.key; |
| 744 | |
| 745 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 746 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 747 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 748 | |
| 749 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 750 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 751 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 752 | |
| 753 | qc->ku.prv_rx.secret = curr_secret; |
| 754 | qc->ku.prv_rx.iv = curr_iv; |
| 755 | qc->ku.prv_rx.key = curr_key; |
| 756 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 757 | |
| 758 | /* Update the TX secrets */ |
| 759 | curr_secret = tls_ctx->tx.secret; |
| 760 | curr_iv = tls_ctx->tx.iv; |
| 761 | curr_key = tls_ctx->tx.key; |
| 762 | |
| 763 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 764 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 765 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 766 | |
| 767 | qc->ku.nxt_tx.secret = curr_secret; |
| 768 | qc->ku.nxt_tx.iv = curr_iv; |
| 769 | qc->ku.nxt_tx.key = curr_key; |
| 770 | } |
| 771 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 772 | #ifndef OPENSSL_IS_BORINGSSL |
| 773 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 774 | const uint8_t *read_secret, |
| 775 | const uint8_t *write_secret, size_t secret_len) |
| 776 | { |
| 777 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 778 | struct quic_tls_ctx *tls_ctx = |
| 779 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 780 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 781 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 782 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 783 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 784 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 785 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 786 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 787 | goto out; |
| 788 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 789 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 790 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 791 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 792 | goto err; |
| 793 | } |
| 794 | |
| 795 | rx = &tls_ctx->rx; |
| 796 | tx = &tls_ctx->tx; |
| 797 | |
| 798 | rx->aead = tx->aead = tls_aead(cipher); |
| 799 | rx->md = tx->md = tls_md(cipher); |
| 800 | rx->hp = tx->hp = tls_hp(cipher); |
| 801 | |
| 802 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 803 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 804 | read_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 805 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 806 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 809 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 810 | |
| 811 | if (!write_secret) |
| 812 | goto tp; |
| 813 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 814 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 815 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 816 | write_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 817 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 818 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 821 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 822 | tp: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 823 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 824 | const unsigned char *buf; |
| 825 | size_t buflen; |
| 826 | |
| 827 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 828 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 829 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 830 | |
| 831 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 832 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 833 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 834 | |
| 835 | if (level == ssl_encryption_application) { |
| 836 | struct quic_conn *qc = conn->qc; |
| 837 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 838 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 839 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 840 | |
| 841 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 842 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 843 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 844 | goto err; |
| 845 | } |
| 846 | |
| 847 | memcpy(rx->secret, read_secret, secret_len); |
| 848 | rx->secretlen = secret_len; |
| 849 | memcpy(tx->secret, write_secret, secret_len); |
| 850 | tx->secretlen = secret_len; |
| 851 | /* Initialize all the secret keys lengths */ |
| 852 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 853 | /* Prepare the next key update */ |
| 854 | if (!quic_tls_key_update(qc)) |
| 855 | goto err; |
| 856 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 857 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 858 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 859 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 860 | |
| 861 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 862 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 863 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 864 | } |
| 865 | #else |
| 866 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 867 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 868 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 869 | */ |
| 870 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 871 | const SSL_CIPHER *cipher, |
| 872 | const uint8_t *secret, size_t secret_len) |
| 873 | { |
| 874 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 875 | struct quic_tls_ctx *tls_ctx = |
| 876 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 877 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 878 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 879 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 880 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 881 | goto out; |
| 882 | } |
| 883 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 884 | tls_ctx->rx.aead = tls_aead(cipher); |
| 885 | tls_ctx->rx.md = tls_md(cipher); |
| 886 | tls_ctx->rx.hp = tls_hp(cipher); |
| 887 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 888 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 889 | goto err; |
| 890 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 891 | if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 892 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 893 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 894 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 895 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 896 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 897 | goto err; |
| 898 | } |
| 899 | |
| 900 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 901 | const unsigned char *buf; |
| 902 | size_t buflen; |
| 903 | |
| 904 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 905 | if (!buflen) |
| 906 | goto err; |
| 907 | |
| 908 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 909 | goto err; |
| 910 | } |
| 911 | |
| 912 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 913 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 914 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 915 | |
| 916 | return 1; |
| 917 | |
| 918 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 919 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 924 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 925 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 926 | */ |
| 927 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 928 | const SSL_CIPHER *cipher, |
| 929 | const uint8_t *secret, size_t secret_len) |
| 930 | { |
| 931 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 932 | struct quic_tls_ctx *tls_ctx = |
| 933 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 934 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 935 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 936 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 937 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 938 | goto out; |
| 939 | } |
| 940 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 941 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 942 | goto err; |
| 943 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 944 | tls_ctx->tx.aead = tls_aead(cipher); |
| 945 | tls_ctx->tx.md = tls_md(cipher); |
| 946 | tls_ctx->tx.hp = tls_hp(cipher); |
| 947 | |
| 948 | if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 949 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 950 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 951 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 952 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 953 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 954 | goto err; |
| 955 | } |
| 956 | |
| 957 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 958 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 959 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 960 | return 1; |
| 961 | |
| 962 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 963 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | #endif |
| 967 | |
| 968 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 969 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 970 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 971 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 972 | * store all the data. |
| 973 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 974 | */ |
| 975 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 976 | const unsigned char *data, size_t len) |
| 977 | { |
| 978 | struct quic_crypto_buf **qcb; |
| 979 | /* The remaining byte to store in CRYPTO buffers. */ |
| 980 | size_t cf_offset, cf_len, *nb_buf; |
| 981 | unsigned char *pos; |
| 982 | |
| 983 | nb_buf = &qel->tx.crypto.nb_buf; |
| 984 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 985 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 986 | cf_len = len; |
| 987 | |
| 988 | while (len) { |
| 989 | size_t to_copy, room; |
| 990 | |
| 991 | pos = (*qcb)->data + (*qcb)->sz; |
| 992 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 993 | to_copy = len > room ? room : len; |
| 994 | if (to_copy) { |
| 995 | memcpy(pos, data, to_copy); |
| 996 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 997 | qel->tx.crypto.sz += to_copy; |
| 998 | (*qcb)->sz += to_copy; |
| 999 | pos += to_copy; |
| 1000 | len -= to_copy; |
| 1001 | data += to_copy; |
| 1002 | } |
| 1003 | else { |
| 1004 | struct quic_crypto_buf **tmp; |
| 1005 | |
| 1006 | tmp = realloc(qel->tx.crypto.bufs, |
| 1007 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1008 | if (tmp) { |
| 1009 | qel->tx.crypto.bufs = tmp; |
| 1010 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1011 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1012 | if (!*qcb) |
| 1013 | return 0; |
| 1014 | |
| 1015 | (*qcb)->sz = 0; |
| 1016 | ++*nb_buf; |
| 1017 | } |
| 1018 | else { |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1025 | * have been buffered. |
| 1026 | */ |
| 1027 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1028 | struct quic_frame *frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1029 | |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1030 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1031 | if (!frm) |
| 1032 | return 0; |
| 1033 | |
| 1034 | frm->type = QUIC_FT_CRYPTO; |
| 1035 | frm->crypto.offset = cf_offset; |
| 1036 | frm->crypto.len = cf_len; |
Frédéric Lécaille | f252adb | 2021-08-03 17:01:25 +0200 | [diff] [blame] | 1037 | frm->crypto.qel = qel; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1038 | 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] | 1039 | } |
| 1040 | |
| 1041 | return len == 0; |
| 1042 | } |
| 1043 | |
| 1044 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1045 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1046 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1047 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1048 | HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1049 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1050 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1053 | /* Set the application for <qc> QUIC connection. |
| 1054 | * Return 1 if succeeded, 0 if not. |
| 1055 | */ |
| 1056 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1057 | { |
| 1058 | const struct qcc_app_ops *app_ops; |
| 1059 | |
| 1060 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) { |
| 1061 | app_ops = qc->qcc->app_ops = &h3_ops; |
| 1062 | } |
| 1063 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) { |
| 1064 | app_ops = qc->qcc->app_ops = &hq_interop_ops; |
| 1065 | } |
| 1066 | else |
| 1067 | return 0; |
| 1068 | |
| 1069 | if (app_ops->init && !app_ops->init(qc->qcc)) |
| 1070 | return 0; |
| 1071 | |
| 1072 | if (app_ops->finalize) |
| 1073 | app_ops->finalize(qc->qcc->ctx); |
| 1074 | |
| 1075 | return 1; |
| 1076 | } |
| 1077 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1078 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1079 | * wants to provide the QUIC layer with CRYPTO data. |
| 1080 | * Returns 1 if succeeded, 0 if not. |
| 1081 | */ |
| 1082 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1083 | const uint8_t *data, size_t len) |
| 1084 | { |
| 1085 | struct connection *conn; |
| 1086 | enum quic_tls_enc_level tel; |
| 1087 | struct quic_enc_level *qel; |
| 1088 | |
| 1089 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1090 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1091 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1092 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1093 | goto out; |
| 1094 | } |
| 1095 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1096 | tel = ssl_to_quic_enc_level(level); |
| 1097 | qel = &conn->qc->els[tel]; |
| 1098 | |
| 1099 | if (tel == -1) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1100 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1101 | goto err; |
| 1102 | } |
| 1103 | |
| 1104 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1105 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1106 | goto err; |
| 1107 | } |
| 1108 | |
| 1109 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1110 | conn->qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1111 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1112 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1113 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1114 | return 1; |
| 1115 | |
| 1116 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1117 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | int ha_quic_flush_flight(SSL *ssl) |
| 1122 | { |
| 1123 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1124 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1125 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc); |
| 1126 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1127 | |
| 1128 | return 1; |
| 1129 | } |
| 1130 | |
| 1131 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1132 | { |
| 1133 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1134 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1135 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1136 | quic_set_tls_alert(conn->qc, alert); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1137 | HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1138 | return 1; |
| 1139 | } |
| 1140 | |
| 1141 | /* QUIC TLS methods */ |
| 1142 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1143 | #ifdef OPENSSL_IS_BORINGSSL |
| 1144 | .set_read_secret = ha_set_rsec, |
| 1145 | .set_write_secret = ha_set_wsec, |
| 1146 | #else |
| 1147 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1148 | #endif |
| 1149 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1150 | .flush_flight = ha_quic_flush_flight, |
| 1151 | .send_alert = ha_quic_send_alert, |
| 1152 | }; |
| 1153 | |
| 1154 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1155 | * Returns an error count. |
| 1156 | */ |
| 1157 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1158 | { |
| 1159 | struct proxy *curproxy = bind_conf->frontend; |
| 1160 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1161 | int cfgerr = 0; |
| 1162 | |
| 1163 | #if 0 |
| 1164 | /* XXX Did not manage to use this. */ |
| 1165 | const char *ciphers = |
| 1166 | "TLS_AES_128_GCM_SHA256:" |
| 1167 | "TLS_AES_256_GCM_SHA384:" |
| 1168 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1169 | "TLS_AES_128_CCM_SHA256"; |
| 1170 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1171 | 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] | 1172 | long options = |
| 1173 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1174 | SSL_OP_SINGLE_ECDH_USE | |
| 1175 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1176 | SSL_CTX *ctx; |
| 1177 | |
| 1178 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1179 | bind_conf->initial_ctx = ctx; |
| 1180 | |
| 1181 | SSL_CTX_set_options(ctx, options); |
| 1182 | #if 0 |
| 1183 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1184 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1185 | "for bind '%s' at [%s:%d].\n", |
| 1186 | curproxy->id, ciphers, |
| 1187 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1188 | cfgerr++; |
| 1189 | } |
| 1190 | #endif |
| 1191 | |
| 1192 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1193 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1194 | "for bind '%s' at [%s:%d].\n", |
| 1195 | curproxy->id, groups, |
| 1196 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1197 | cfgerr++; |
| 1198 | } |
| 1199 | |
| 1200 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1201 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1202 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1203 | SSL_CTX_set_default_verify_paths(ctx); |
| 1204 | |
| 1205 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1206 | #ifdef OPENSSL_IS_BORINGSSL |
| 1207 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1208 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1209 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1210 | if (bind_conf->ssl_conf.early_data) { |
| 1211 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 1212 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1213 | } |
| 1214 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1215 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1216 | #else |
| 1217 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1218 | #endif |
| 1219 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1220 | #endif |
| 1221 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1222 | |
| 1223 | return cfgerr; |
| 1224 | } |
| 1225 | |
| 1226 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1227 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1228 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1229 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1230 | */ |
| 1231 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1232 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1233 | { |
| 1234 | uint64_t expected_pn = largest_pn + 1; |
| 1235 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1236 | uint64_t pn_hwin = pn_win / 2; |
| 1237 | uint64_t pn_mask = pn_win - 1; |
| 1238 | uint64_t candidate_pn; |
| 1239 | |
| 1240 | |
| 1241 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1242 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1243 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1244 | candidate_pn + pn_hwin <= expected_pn) |
| 1245 | return candidate_pn + pn_win; |
| 1246 | |
| 1247 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1248 | return candidate_pn - pn_win; |
| 1249 | |
| 1250 | return candidate_pn; |
| 1251 | } |
| 1252 | |
| 1253 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1254 | * cryptographic context. |
| 1255 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1256 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1257 | * <end> points to one byte past the end of this packet. |
| 1258 | * Returns 1 if succeeded, 0 if not. |
| 1259 | */ |
| 1260 | static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1261 | int64_t largest_pn, unsigned char *pn, |
| 1262 | unsigned char *byte0, const unsigned char *end, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1263 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1264 | { |
| 1265 | int ret, outlen, i, pnlen; |
| 1266 | uint64_t packet_number; |
| 1267 | uint32_t truncated_pn = 0; |
| 1268 | unsigned char mask[5] = {0}; |
| 1269 | unsigned char *sample; |
| 1270 | EVP_CIPHER_CTX *cctx; |
| 1271 | unsigned char *hp_key; |
| 1272 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1273 | /* Check there is enough data in this packet. */ |
| 1274 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1275 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | cctx = EVP_CIPHER_CTX_new(); |
| 1280 | if (!cctx) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1281 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | ret = 0; |
| 1286 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1287 | |
| 1288 | hp_key = tls_ctx->rx.hp_key; |
| 1289 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1290 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1291 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1292 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1293 | goto out; |
| 1294 | } |
| 1295 | |
| 1296 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1297 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1298 | for (i = 0; i < pnlen; i++) { |
| 1299 | pn[i] ^= mask[i + 1]; |
| 1300 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1301 | } |
| 1302 | |
| 1303 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1304 | /* Store remaining information for this unprotected header */ |
| 1305 | pkt->pn = packet_number; |
| 1306 | pkt->pnl = pnlen; |
| 1307 | |
| 1308 | ret = 1; |
| 1309 | |
| 1310 | out: |
| 1311 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1312 | |
| 1313 | return ret; |
| 1314 | } |
| 1315 | |
| 1316 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1317 | * address, with <payload_len> as payload length, <aad> as address of |
| 1318 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1319 | * context. |
| 1320 | * Returns 1 if succeeded, 0 if not. |
| 1321 | */ |
| 1322 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1323 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1324 | struct quic_tls_ctx *tls_ctx, struct connection *conn) |
| 1325 | { |
| 1326 | unsigned char iv[12]; |
| 1327 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1328 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1329 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1330 | |
| 1331 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1332 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn->qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1333 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1337 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1338 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn->qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1339 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1343 | |
| 1344 | err: |
| 1345 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1346 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn->qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1347 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1351 | * Returns 1 if succeeded, 0 if not. |
| 1352 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1353 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1354 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1355 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1356 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1357 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1358 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1359 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1360 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1361 | |
| 1362 | kp_changed = 0; |
| 1363 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1364 | /* The two tested bits are not at the same position, |
| 1365 | * this is why they are first both inversed. |
| 1366 | */ |
| 1367 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1368 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1369 | /* The lowest packet number of a previous key phase |
| 1370 | * cannot be null if it really stores previous key phase |
| 1371 | * secrets. |
| 1372 | */ |
| 1373 | if (!pkt->qc->ku.prv_rx.pn) |
| 1374 | return 0; |
| 1375 | |
| 1376 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1377 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1378 | } |
| 1379 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1380 | /* Next key phase */ |
| 1381 | kp_changed = 1; |
| 1382 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1383 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1384 | } |
| 1385 | } |
| 1386 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1387 | |
| 1388 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1389 | return 0; |
| 1390 | |
| 1391 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1392 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1393 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1394 | if (!ret) |
| 1395 | return 0; |
| 1396 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1397 | /* Update the keys only if the packet decryption succeeded. */ |
| 1398 | if (kp_changed) { |
| 1399 | quic_tls_rotate_keys(pkt->qc); |
| 1400 | /* Toggle the Key Phase bit */ |
| 1401 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1402 | /* Store the lowest packet number received for the current key phase */ |
| 1403 | tls_ctx->rx.pn = pkt->pn; |
| 1404 | /* Prepare the next key update */ |
| 1405 | if (!quic_tls_key_update(pkt->qc)) |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1409 | /* Update the packet length (required to parse the frames). */ |
| 1410 | pkt->len = pkt->aad_len + ret; |
| 1411 | |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1415 | /* Remove from <qcs> stream the acknowledged frames. |
| 1416 | * Never fails. |
| 1417 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1418 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1419 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1420 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1421 | struct eb64_node *frm_node; |
| 1422 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1423 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1424 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1425 | while (frm_node) { |
| 1426 | struct quic_stream *strm; |
| 1427 | |
| 1428 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1429 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1430 | break; |
| 1431 | |
| 1432 | b_del(strm->buf, strm->len); |
| 1433 | qcs->tx.ack_offset += strm->len; |
| 1434 | frm_node = eb64_next(frm_node); |
| 1435 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1436 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1437 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1438 | |
| 1439 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1442 | /* 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] | 1443 | 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] | 1444 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1445 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1446 | int stream_acked; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1447 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1448 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1449 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1450 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1451 | switch (frm->type) { |
| 1452 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1453 | { |
| 1454 | struct qcs *qcs = frm->stream.qcs; |
| 1455 | struct quic_stream *strm = &frm->stream; |
| 1456 | |
| 1457 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1458 | b_del(strm->buf, strm->len); |
| 1459 | qcs->tx.ack_offset += strm->len; |
| 1460 | LIST_DELETE(&frm->list); |
| 1461 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1462 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1463 | } |
| 1464 | else { |
| 1465 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1466 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1467 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1468 | } |
| 1469 | break; |
| 1470 | default: |
| 1471 | LIST_DELETE(&frm->list); |
| 1472 | pool_free(pool_head_quic_frame, frm); |
| 1473 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1474 | |
| 1475 | if (stream_acked) { |
| 1476 | struct qcc *qcc = qc->qcc; |
| 1477 | |
| 1478 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1479 | tasklet_wakeup(qcc->subs->tasklet); |
| 1480 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1481 | if (!qcc->subs->events) |
| 1482 | qcc->subs = NULL; |
| 1483 | } |
| 1484 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1488 | * deallocating them, and their TX frames. |
| 1489 | * Returns the last node reached to be used for the next range. |
| 1490 | * May be NULL if <largest> node could not be found. |
| 1491 | */ |
| 1492 | static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags, |
| 1493 | struct list *newly_acked_pkts, |
| 1494 | struct eb64_node *largest_node, |
| 1495 | uint64_t largest, uint64_t smallest, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1496 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1497 | { |
| 1498 | struct eb64_node *node; |
| 1499 | struct quic_tx_packet *pkt; |
| 1500 | |
| 1501 | if (largest_node) |
| 1502 | node = largest_node; |
| 1503 | else { |
| 1504 | node = eb64_lookup(pkts, largest); |
| 1505 | while (!node && largest > smallest) { |
| 1506 | node = eb64_lookup(pkts, --largest); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1511 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1512 | |
| 1513 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1514 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1515 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1516 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->qc, &pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1517 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1518 | qc_treat_acked_tx_frm(frm, ctx); |
| 1519 | node = eb64_prev(node); |
| 1520 | eb64_delete(&pkt->pn_node); |
| 1521 | } |
| 1522 | |
| 1523 | return node; |
| 1524 | } |
| 1525 | |
| 1526 | /* Treat <frm> frame whose packet it is attached to has just been detected as non |
| 1527 | * acknowledged. |
| 1528 | */ |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1529 | 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] | 1530 | struct quic_pktns *pktns, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1531 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1532 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1533 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->qc, frm); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1534 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1535 | MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | /* Free the TX packets of <pkts> list */ |
| 1540 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1541 | { |
| 1542 | struct quic_tx_packet *pkt, *tmp; |
| 1543 | |
| 1544 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1545 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1546 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1547 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /* Send a packet loss event nofification to the congestion controller |
| 1552 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1553 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1554 | * at <now_us> current time. |
| 1555 | * Always succeeds. |
| 1556 | */ |
| 1557 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1558 | unsigned int lost_bytes, |
| 1559 | unsigned int newest_time_sent, |
| 1560 | unsigned int period, |
| 1561 | unsigned int now_us) |
| 1562 | { |
| 1563 | struct quic_cc_event ev = { |
| 1564 | .type = QUIC_CC_EVT_LOSS, |
| 1565 | .loss.now_ms = now_ms, |
| 1566 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1567 | .loss.lost_bytes = lost_bytes, |
| 1568 | .loss.newest_time_sent = newest_time_sent, |
| 1569 | .loss.period = period, |
| 1570 | }; |
| 1571 | |
| 1572 | quic_cc_event(&qc->path->cc, &ev); |
| 1573 | } |
| 1574 | |
| 1575 | /* Send a packet ack event nofication for each newly acked packet of |
| 1576 | * <newly_acked_pkts> list and free them. |
| 1577 | * Always succeeds. |
| 1578 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1579 | 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] | 1580 | struct list *newly_acked_pkts) |
| 1581 | { |
| 1582 | struct quic_conn *qc = ctx->conn->qc; |
| 1583 | struct quic_tx_packet *pkt, *tmp; |
| 1584 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1585 | |
| 1586 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1587 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1588 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1589 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1590 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1591 | ev.ack.acked = pkt->in_flight_len; |
| 1592 | ev.ack.time_sent = pkt->time_sent; |
| 1593 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1594 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1595 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1596 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | } |
| 1600 | |
| 1601 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1602 | * their TX frames. |
| 1603 | * Send a packet loss event to the congestion controller if |
| 1604 | * in flight packet have been lost. |
| 1605 | * Also frees the packet in <pkts> list. |
| 1606 | * Never fails. |
| 1607 | */ |
| 1608 | 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] | 1609 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1610 | struct list *pkts, |
| 1611 | uint64_t now_us) |
| 1612 | { |
| 1613 | struct quic_conn *qc = ctx->conn->qc; |
| 1614 | 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] | 1615 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1616 | uint64_t lost_bytes; |
| 1617 | |
| 1618 | lost_bytes = 0; |
| 1619 | oldest_lost = newest_lost = NULL; |
| 1620 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1621 | lost_bytes += pkt->in_flight_len; |
| 1622 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1623 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1624 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1625 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1626 | /* Treat the frames of this lost packet. */ |
| 1627 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1628 | qc_treat_nacked_tx_frm(frm, pktns, ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1629 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1630 | if (!oldest_lost) { |
| 1631 | oldest_lost = newest_lost = pkt; |
| 1632 | } |
| 1633 | else { |
| 1634 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1635 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1636 | newest_lost = pkt; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | if (lost_bytes) { |
| 1641 | /* Sent a packet loss event to the congestion controller. */ |
| 1642 | qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent, |
| 1643 | 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] | 1644 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1645 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1646 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1651 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1652 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1653 | * space if any not deemed lost. |
| 1654 | * Should be called after having received an ACK frame with newly acknowledged |
| 1655 | * packets or when the the loss detection timer has expired. |
| 1656 | * Always succeeds. |
| 1657 | */ |
| 1658 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1659 | struct quic_conn *qc, |
| 1660 | struct list *lost_pkts) |
| 1661 | { |
| 1662 | struct eb_root *pkts; |
| 1663 | struct eb64_node *node; |
| 1664 | struct quic_loss *ql; |
| 1665 | unsigned int loss_delay; |
| 1666 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1667 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1668 | pkts = &pktns->tx.pkts; |
| 1669 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1670 | if (eb_is_empty(pkts)) |
| 1671 | goto out; |
| 1672 | |
| 1673 | ql = &qc->path->loss; |
| 1674 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1675 | loss_delay += loss_delay >> 3; |
| 1676 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1677 | |
| 1678 | node = eb64_first(pkts); |
| 1679 | while (node) { |
| 1680 | struct quic_tx_packet *pkt; |
| 1681 | int64_t largest_acked_pn; |
| 1682 | unsigned int loss_time_limit, time_sent; |
| 1683 | |
| 1684 | 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] | 1685 | 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] | 1686 | node = eb64_next(node); |
| 1687 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1688 | break; |
| 1689 | |
| 1690 | time_sent = pkt->time_sent; |
| 1691 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1692 | if (tick_is_le(time_sent, now_ms) || |
| 1693 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1694 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1695 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1696 | } |
| 1697 | else { |
| 1698 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1703 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1707 | * 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] | 1708 | * 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] | 1709 | * acked ack-eliciting packet. |
| 1710 | * Return 1, if succeeded, 0 if not. |
| 1711 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1712 | 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] | 1713 | struct quic_enc_level *qel, |
| 1714 | unsigned int *rtt_sample, |
| 1715 | const unsigned char **pos, const unsigned char *end) |
| 1716 | { |
| 1717 | struct quic_ack *ack = &frm->ack; |
| 1718 | uint64_t smallest, largest; |
| 1719 | struct eb_root *pkts; |
| 1720 | struct eb64_node *largest_node; |
| 1721 | unsigned int time_sent, pkt_flags; |
| 1722 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1723 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1724 | |
| 1725 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1726 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1727 | ctx->qc, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1728 | goto err; |
| 1729 | } |
| 1730 | |
| 1731 | if (ack->first_ack_range > ack->largest_ack) { |
| 1732 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1733 | ctx->qc, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1734 | goto err; |
| 1735 | } |
| 1736 | |
| 1737 | largest = ack->largest_ack; |
| 1738 | smallest = largest - ack->first_ack_range; |
| 1739 | pkts = &qel->pktns->tx.pkts; |
| 1740 | pkt_flags = 0; |
| 1741 | largest_node = NULL; |
| 1742 | time_sent = 0; |
| 1743 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1744 | 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] | 1745 | largest_node = eb64_lookup(pkts, largest); |
| 1746 | if (!largest_node) { |
| 1747 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1748 | QUIC_EV_CONN_PRSAFRM, ctx->qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1749 | } |
| 1750 | else { |
| 1751 | time_sent = eb64_entry(&largest_node->node, |
| 1752 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1753 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1757 | ctx->qc, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1758 | do { |
| 1759 | uint64_t gap, ack_range; |
| 1760 | |
| 1761 | qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts, |
| 1762 | largest_node, largest, smallest, ctx); |
| 1763 | if (!ack->ack_range_num--) |
| 1764 | break; |
| 1765 | |
| 1766 | if (!quic_dec_int(&gap, pos, end)) |
| 1767 | goto err; |
| 1768 | |
| 1769 | if (smallest < gap + 2) { |
| 1770 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1771 | ctx->qc, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1772 | goto err; |
| 1773 | } |
| 1774 | |
| 1775 | largest = smallest - gap - 2; |
| 1776 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1777 | goto err; |
| 1778 | |
| 1779 | if (largest < ack_range) { |
| 1780 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1781 | ctx->qc, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1782 | goto err; |
| 1783 | } |
| 1784 | |
| 1785 | /* Do not use this node anymore. */ |
| 1786 | largest_node = NULL; |
| 1787 | /* Next range */ |
| 1788 | smallest = largest - ack_range; |
| 1789 | |
| 1790 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1791 | ctx->qc, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1792 | } while (1); |
| 1793 | |
| 1794 | /* 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] | 1795 | 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] | 1796 | |
| 1797 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1798 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1799 | 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] | 1800 | } |
| 1801 | |
| 1802 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1803 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 1804 | qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts); |
| 1805 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 1806 | qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms); |
| 1807 | } |
| 1808 | qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts); |
| 1809 | if (quic_peer_validated_addr(ctx)) |
| 1810 | ctx->conn->qc->path->loss.pto_count = 0; |
| 1811 | qc_set_timer(ctx); |
| 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | return 1; |
| 1816 | |
| 1817 | err: |
| 1818 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1819 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1820 | return 0; |
| 1821 | } |
| 1822 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1823 | /* This function gives the detail of the SSL error. It is used only |
| 1824 | * if the debug mode and the verbose mode are activated. It dump all |
| 1825 | * the SSL error until the stack was empty. |
| 1826 | */ |
| 1827 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1828 | { |
| 1829 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1830 | while (1) { |
| 1831 | unsigned long ret; |
| 1832 | |
| 1833 | ret = ERR_get_error(); |
| 1834 | if (!ret) |
| 1835 | return; |
| 1836 | |
| 1837 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1838 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1843 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1844 | const char **str, int *len); |
| 1845 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1846 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1847 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1848 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1849 | * Return 1 if succeeded, 0 if not. |
| 1850 | */ |
| 1851 | 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] | 1852 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1853 | const unsigned char *data, size_t len, |
| 1854 | struct quic_rx_packet *pkt, |
| 1855 | struct quic_rx_crypto_frm *cf) |
| 1856 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1857 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1858 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1859 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1860 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1861 | qc = ctx->qc; |
| 1862 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1863 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1864 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1865 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1866 | TRACE_PROTO("SSL_provide_quic_data() error", |
| 1867 | QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl); |
| 1868 | goto err; |
| 1869 | } |
| 1870 | |
| 1871 | el->rx.crypto.offset += len; |
| 1872 | TRACE_PROTO("in order CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1873 | QUIC_EV_CONN_SSLDATA, qc, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1874 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1875 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1876 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1877 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1878 | if (ssl_err != 1) { |
| 1879 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1880 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1881 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1882 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1883 | goto out; |
| 1884 | } |
| 1885 | |
| 1886 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1887 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1888 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1889 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1890 | goto err; |
| 1891 | } |
| 1892 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1893 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1894 | if (objt_listener(ctx->conn->target)) |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1895 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1896 | else |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1897 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1898 | } else { |
| 1899 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1900 | if (ssl_err != 1) { |
| 1901 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1902 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1903 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1904 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1905 | goto out; |
| 1906 | } |
| 1907 | |
| 1908 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1909 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1910 | goto err; |
| 1911 | } |
| 1912 | |
| 1913 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1914 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1915 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1916 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1917 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1918 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1919 | return 1; |
| 1920 | |
| 1921 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 1922 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1923 | return 0; |
| 1924 | } |
| 1925 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1926 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1927 | * <pkt> RX packet. |
| 1928 | * Return it if succeeded, NULL if not. |
| 1929 | */ |
| 1930 | static inline |
| 1931 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1932 | struct quic_rx_packet *pkt) |
| 1933 | { |
| 1934 | struct quic_rx_strm_frm *frm; |
| 1935 | |
| 1936 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1937 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1938 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1939 | frm->len = stream_frm->len; |
| 1940 | frm->data = stream_frm->data; |
| 1941 | frm->pkt = pkt; |
| 1942 | } |
| 1943 | |
| 1944 | return frm; |
| 1945 | } |
| 1946 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1947 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
Frédéric Lécaille | 3fe7df8 | 2021-12-15 15:32:55 +0100 | [diff] [blame] | 1948 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1949 | */ |
| 1950 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1951 | { |
| 1952 | size_t ret; |
| 1953 | |
| 1954 | ret = 0; |
| 1955 | while (strm_frm->len) { |
| 1956 | size_t try; |
| 1957 | |
| 1958 | try = b_contig_space(buf); |
| 1959 | if (!try) |
| 1960 | break; |
| 1961 | |
| 1962 | if (try > strm_frm->len) |
| 1963 | try = strm_frm->len; |
| 1964 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1965 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1966 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1967 | b_add(buf, try); |
| 1968 | ret += try; |
| 1969 | } |
| 1970 | |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1974 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1975 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1976 | */ |
| 1977 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1978 | struct quic_rx_strm_frm *strm_frm) |
| 1979 | { |
| 1980 | size_t ret; |
| 1981 | |
| 1982 | ret = 0; |
| 1983 | while (strm_frm->len) { |
| 1984 | size_t try; |
| 1985 | |
| 1986 | try = b_contig_space(buf); |
| 1987 | if (!try) |
| 1988 | break; |
| 1989 | |
| 1990 | if (try > strm_frm->len) |
| 1991 | try = strm_frm->len; |
| 1992 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1993 | strm_frm->len -= try; |
| 1994 | strm_frm->offset_node.key += try; |
| 1995 | b_add(buf, try); |
| 1996 | ret += try; |
| 1997 | } |
| 1998 | |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
| 2002 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 2003 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 2004 | { |
| 2005 | int total; |
| 2006 | struct eb64_node *frm_node; |
| 2007 | |
| 2008 | total = 0; |
| 2009 | frm_node = eb64_first(&qcs->rx.frms); |
| 2010 | while (frm_node) { |
| 2011 | int ret; |
| 2012 | struct quic_rx_strm_frm *frm; |
| 2013 | |
| 2014 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 2015 | if (frm->offset_node.key != qcs->rx.offset) |
| 2016 | break; |
| 2017 | |
| 2018 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 2019 | qcs->rx.offset += ret; |
| 2020 | total += ret; |
| 2021 | if (frm->len) { |
| 2022 | /* If there is remaining data in this frame |
| 2023 | * this is because the destination buffer is full. |
| 2024 | */ |
| 2025 | break; |
| 2026 | } |
| 2027 | |
| 2028 | frm_node = eb64_next(frm_node); |
| 2029 | quic_rx_packet_refdec(frm->pkt); |
| 2030 | eb64_delete(&frm->offset_node); |
| 2031 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2032 | } |
| 2033 | |
| 2034 | return total; |
| 2035 | } |
| 2036 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2037 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2038 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2039 | * If not, the STREAM frame is stored to be treated again later. |
| 2040 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2041 | * Return 1 if succeeded, 0 if not. |
| 2042 | */ |
| 2043 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2044 | struct quic_stream *strm_frm, |
| 2045 | struct quic_conn *qc) |
| 2046 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2047 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2048 | struct qcs *strm; |
| 2049 | struct eb64_node *strm_node, *frm_node; |
| 2050 | struct quic_rx_strm_frm *frm; |
| 2051 | |
| 2052 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2053 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2054 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2059 | 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] | 2060 | /* FIXME: handle the case where this frame overlap others */ |
| 2061 | if (frm_node) { |
| 2062 | TRACE_PROTO("Already existing stream data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2063 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2064 | goto out; |
| 2065 | } |
| 2066 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2067 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2068 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2069 | int ret; |
| 2070 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2071 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2072 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2073 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2074 | |
| 2075 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2076 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2077 | strm->rx.offset += ret; |
| 2078 | } |
| 2079 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2080 | total += qc_treat_rx_strm_frms(strm); |
| 2081 | if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) { |
| 2082 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2086 | if (!strm_frm->len) |
| 2087 | goto out; |
| 2088 | |
| 2089 | store_frm: |
| 2090 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2091 | if (!frm) { |
| 2092 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2093 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2094 | return 0; |
| 2095 | } |
| 2096 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2097 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2098 | quic_rx_packet_refinc(pkt); |
| 2099 | |
| 2100 | out: |
| 2101 | return 1; |
| 2102 | } |
| 2103 | |
| 2104 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2105 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2106 | * If not, the STREAM frame is stored to be treated again later. |
| 2107 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2108 | * Return 1 if succeeded, 0 if not. |
| 2109 | */ |
| 2110 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2111 | struct quic_stream *strm_frm, |
| 2112 | struct quic_conn *qc) |
| 2113 | { |
| 2114 | struct qcs *strm; |
| 2115 | struct eb64_node *strm_node, *frm_node; |
| 2116 | struct quic_rx_strm_frm *frm; |
| 2117 | size_t strm_frm_len; |
| 2118 | |
| 2119 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2120 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2121 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
| 2124 | |
| 2125 | 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] | 2126 | 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] | 2127 | /* FIXME: handle the case where this frame overlap others */ |
| 2128 | if (frm_node) { |
| 2129 | TRACE_PROTO("Already existing stream data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2130 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2131 | goto out; |
| 2132 | } |
| 2133 | |
| 2134 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2135 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2136 | int ret; |
| 2137 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2138 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2139 | goto store_frm; |
| 2140 | |
| 2141 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2142 | * of bytes copied. |
| 2143 | */ |
| 2144 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2145 | /* Inform the application of the arrival of this new stream */ |
| 2146 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2147 | TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
| 2150 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2151 | if (ret) |
| 2152 | qcs_notify_recv(strm); |
| 2153 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2154 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2155 | } |
| 2156 | /* Take this frame into an account for the stream flow control */ |
| 2157 | strm->rx.offset += strm_frm_len; |
| 2158 | /* 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] | 2159 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2160 | */ |
| 2161 | if (!strm_frm->len) |
| 2162 | goto out; |
| 2163 | |
| 2164 | store_frm: |
| 2165 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2166 | if (!frm) { |
| 2167 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2168 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2172 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2173 | quic_rx_packet_refinc(pkt); |
| 2174 | |
| 2175 | out: |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
| 2179 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2180 | struct quic_stream *strm_frm, |
| 2181 | struct quic_conn *qc) |
| 2182 | { |
| 2183 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2184 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2185 | else |
| 2186 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2187 | } |
| 2188 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2189 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2190 | * as I/O handler context and <qel> as encryption level. |
| 2191 | * Returns 1 if succeeded, 0 if failed. |
| 2192 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2193 | 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] | 2194 | struct quic_enc_level *qel) |
| 2195 | { |
| 2196 | struct quic_frame frm; |
| 2197 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2198 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2199 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2200 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2201 | /* Skip the AAD */ |
| 2202 | pos = pkt->data + pkt->aad_len; |
| 2203 | end = pkt->data + pkt->len; |
| 2204 | |
| 2205 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2206 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2207 | goto err; |
| 2208 | |
| 2209 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2210 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2211 | break; |
| 2212 | case QUIC_FT_PING: |
| 2213 | break; |
| 2214 | case QUIC_FT_ACK: |
| 2215 | { |
| 2216 | unsigned int rtt_sample; |
| 2217 | |
| 2218 | rtt_sample = 0; |
| 2219 | if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end)) |
| 2220 | goto err; |
| 2221 | |
| 2222 | if (rtt_sample) { |
| 2223 | unsigned int ack_delay; |
| 2224 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2225 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2226 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)); |
| 2227 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2228 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2229 | break; |
| 2230 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2231 | case QUIC_FT_STOP_SENDING: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2232 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2233 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2234 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2235 | { |
| 2236 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2237 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2238 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2239 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2240 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2241 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2242 | |
| 2243 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2244 | cfdebug.len = frm.crypto.len; |
| 2245 | /* Nothing to do */ |
| 2246 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2247 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2248 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2249 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2250 | else { |
| 2251 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2252 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2253 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2254 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2255 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2256 | cfdebug.len = frm.crypto.len; |
| 2257 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2258 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2259 | frm.crypto.len -= diff; |
| 2260 | frm.crypto.data += diff; |
| 2261 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2262 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2263 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2264 | |
| 2265 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2266 | /* XXX TO DO: <cf> is used only for the traces. */ |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2267 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2268 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2269 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2270 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2271 | if (!qc_provide_cdata(qel, ctx, |
| 2272 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2273 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2274 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2275 | |
| 2276 | break; |
| 2277 | } |
| 2278 | |
| 2279 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2280 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2281 | if (!cf) { |
| 2282 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2283 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2284 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2285 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2286 | |
| 2287 | cf->offset_node.key = frm.crypto.offset; |
| 2288 | cf->len = frm.crypto.len; |
| 2289 | cf->data = frm.crypto.data; |
| 2290 | cf->pkt = pkt; |
| 2291 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2292 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2293 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2294 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2295 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2296 | { |
| 2297 | struct quic_stream *stream = &frm.stream; |
| 2298 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2299 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2300 | if (objt_listener(ctx->conn->target)) { |
| 2301 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2302 | goto err; |
| 2303 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2304 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2305 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2306 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2307 | goto err; |
| 2308 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2309 | break; |
| 2310 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2311 | case QUIC_FT_MAX_DATA: |
| 2312 | case QUIC_FT_MAX_STREAM_DATA: |
| 2313 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2314 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2315 | case QUIC_FT_DATA_BLOCKED: |
| 2316 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2317 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2318 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2319 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2320 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2321 | break; |
| 2322 | case QUIC_FT_CONNECTION_CLOSE: |
| 2323 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2324 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2325 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2326 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2327 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2328 | case QUIC_FT_HANDSHAKE_DONE: |
| 2329 | if (objt_listener(ctx->conn->target)) |
| 2330 | goto err; |
| 2331 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2332 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2333 | break; |
| 2334 | default: |
| 2335 | goto err; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2340 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2341 | * be discarded. |
| 2342 | */ |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2343 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2344 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2345 | |
| 2346 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2347 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2348 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2349 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2350 | qc_set_timer(ctx); |
| 2351 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2352 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2353 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2356 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2357 | return 1; |
| 2358 | |
| 2359 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2360 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2361 | return 0; |
| 2362 | } |
| 2363 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2364 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2365 | * 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] | 2366 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2367 | * This function must be called only after having built a correct datagram. |
| 2368 | * Always succeeds. |
| 2369 | */ |
| 2370 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2371 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2372 | { |
| 2373 | write_u16(cb_wr(cbuf), dglen); |
| 2374 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2375 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2376 | } |
| 2377 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2378 | /* Prepare as much as possible packets into <qr> ring buffer for |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2379 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2380 | * several packets in the same datagram. A header made of two fields is added |
| 2381 | * to each datagram: the datagram length followed by the address of the first |
| 2382 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2383 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2384 | */ |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2385 | static int qc_prep_pkts(struct qring *qr, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2386 | { |
| 2387 | struct quic_conn *qc; |
| 2388 | enum quic_tls_enc_level tel, next_tel; |
| 2389 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2390 | struct cbuf *cbuf; |
| 2391 | unsigned char *end_buf, *end, *pos, *spos; |
| 2392 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2393 | /* length of datagrams */ |
| 2394 | uint16_t dglen; |
| 2395 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2396 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2397 | /* Each datagram is prepended with its length followed by the |
| 2398 | * address of the first packet in the datagram. |
| 2399 | */ |
| 2400 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2401 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2402 | qc = ctx->qc; |
| 2403 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2404 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2405 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 2406 | if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2407 | TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2408 | goto err; |
| 2409 | } |
| 2410 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2411 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2412 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2413 | total = 0; |
| 2414 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2415 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2416 | cbuf = qr->cbuf; |
| 2417 | spos = pos = cb_wr(cbuf); |
| 2418 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2419 | * to ensure there is enough room to mark the end of prepared |
| 2420 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2421 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2422 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2423 | first_pkt = prv_pkt = NULL; |
| 2424 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2425 | int err, nb_ptos, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2426 | enum quic_pkt_type pkt_type; |
| 2427 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2428 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2429 | nb_ptos = ack = 0; |
| 2430 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2431 | if (!cc) { |
| 2432 | if (!prv_pkt) { |
| 2433 | /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */ |
| 2434 | do { |
| 2435 | nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams); |
| 2436 | } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1)); |
| 2437 | } |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2438 | ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2439 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2440 | /* Do not build any more packet if the TX secrets are not available or |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2441 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2442 | * 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] | 2443 | * and if there is no more CRYPTO data available or in flight |
| 2444 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2445 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2446 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2447 | (!cc && !ack && !nb_ptos && |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2448 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2449 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2450 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2451 | /* Set the current datagram as prepared into <cbuf> if |
| 2452 | * the was already a correct packet which was previously written. |
| 2453 | */ |
| 2454 | if (prv_pkt) |
| 2455 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2456 | break; |
| 2457 | } |
| 2458 | |
| 2459 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2460 | if (!prv_pkt) { |
| 2461 | /* Leave room for the datagram header */ |
| 2462 | pos += dg_headlen; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2463 | if (!quic_peer_validated_addr(ctx) && objt_listener(ctx->conn->target)) { |
| 2464 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 2465 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 2466 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2467 | } |
| 2468 | else { |
| 2469 | end = pos + qc->path->mtu; |
| 2470 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2471 | } |
| 2472 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2473 | cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2474 | pkt_type, ack, nb_ptos, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2475 | /* 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] | 2476 | if (err < 0) { |
| 2477 | if (!prv_pkt && nb_ptos) |
| 2478 | HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2479 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2480 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2481 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2482 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2483 | case -2: |
| 2484 | goto err; |
| 2485 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2486 | /* If there was already a correct packet present, set the |
| 2487 | * current datagram as prepared into <cbuf>. |
| 2488 | */ |
| 2489 | if (prv_pkt) { |
| 2490 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2491 | goto stop_build; |
| 2492 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2493 | goto out; |
| 2494 | default: |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2495 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2496 | if (!cur_pkt) |
| 2497 | goto err; |
| 2498 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2499 | total += cur_pkt->len; |
| 2500 | /* keep trace of the first packet in the datagram */ |
| 2501 | if (!first_pkt) |
| 2502 | first_pkt = cur_pkt; |
| 2503 | /* Attach the current one to the previous one */ |
| 2504 | if (prv_pkt) |
| 2505 | prv_pkt->next = cur_pkt; |
| 2506 | /* Let's say we have to build a new dgram */ |
| 2507 | prv_pkt = NULL; |
| 2508 | dglen += cur_pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2509 | /* Discard the Initial encryption keys as soon as |
| 2510 | * a handshake packet could be built. |
| 2511 | */ |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2512 | 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] | 2513 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2514 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2515 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2516 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2517 | qc_set_timer(ctx); |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2518 | 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] | 2519 | } |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2520 | /* If the data for the current encryption level have all been sent, |
| 2521 | * select the next level. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2522 | */ |
Frédéric Lécaille | d067088 | 2021-08-19 07:53:27 +0200 | [diff] [blame] | 2523 | 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] | 2524 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2525 | (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] | 2526 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2527 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2528 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2529 | tel = next_tel; |
| 2530 | qel = &qc->els[tel]; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 2531 | if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2532 | /* If there is data for the next level, do not |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2533 | * consume a datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2534 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2535 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2536 | } |
| 2537 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | /* If we have to build a new datagram, set the current datagram as |
| 2541 | * prepared into <cbuf>. |
| 2542 | */ |
| 2543 | if (!prv_pkt) { |
| 2544 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2545 | first_pkt = NULL; |
| 2546 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2547 | padding = 0; |
| 2548 | } |
| 2549 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 2550 | (objt_server(qc->conn->target) || |
| 2551 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2552 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | stop_build: |
| 2557 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2558 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2559 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2560 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2561 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2562 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2563 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2564 | /* Mark the end of contiguous data for the reader */ |
| 2565 | write_u16(cb_wr(cbuf), 0); |
| 2566 | cb_add(cbuf, sizeof(uint16_t)); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (rd && rd <= cbuf->wr) { |
| 2571 | cb_wr_reset(cbuf); |
| 2572 | if (pos == spos) { |
| 2573 | /* Reuse the same buffer if nothing was built. */ |
| 2574 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2580 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2581 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2582 | |
| 2583 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2584 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2585 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /* 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] | 2589 | * 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] | 2590 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2591 | 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] | 2592 | { |
| 2593 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2594 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2595 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2596 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2597 | cbuf = qr->cbuf; |
| 2598 | while (cb_contig_data(cbuf)) { |
| 2599 | unsigned char *pos; |
| 2600 | struct buffer tmpbuf = { }; |
| 2601 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2602 | uint16_t dglen; |
| 2603 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2604 | unsigned int time_sent; |
| 2605 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2606 | pos = cb_rd(cbuf); |
| 2607 | dglen = read_u16(pos); |
| 2608 | /* End of prepared datagrams. |
| 2609 | * Reset the reader index only if in front of the writer index. |
| 2610 | */ |
| 2611 | if (!dglen) { |
| 2612 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2613 | |
| 2614 | if (wr && wr < cbuf->rd) { |
| 2615 | cb_rd_reset(cbuf); |
| 2616 | continue; |
| 2617 | } |
| 2618 | break; |
| 2619 | } |
| 2620 | |
| 2621 | pos += sizeof dglen; |
| 2622 | first_pkt = read_ptr(pos); |
| 2623 | pos += sizeof first_pkt; |
| 2624 | tmpbuf.area = (char *)pos; |
| 2625 | tmpbuf.size = tmpbuf.data = dglen; |
| 2626 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2627 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2628 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2629 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2630 | 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] | 2631 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
| 2632 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2633 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2634 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2635 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2636 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2637 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2638 | qc->tx.bytes += tmpbuf.data; |
| 2639 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2640 | |
| 2641 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2642 | pkt->time_sent = time_sent; |
| 2643 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2644 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2645 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2646 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2647 | qc->path->in_flight += pkt->in_flight_len; |
| 2648 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2649 | if (pkt->in_flight_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2650 | qc_set_timer(ctx); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2651 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2652 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2653 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2654 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2655 | } |
| 2656 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2657 | |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2662 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2663 | * a HANDSHAKE_DONE frame. |
| 2664 | * Return 1 if succeeded, 0 if not. |
| 2665 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2666 | 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] | 2667 | { |
| 2668 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2669 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2670 | struct quic_frame *frm; |
| 2671 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2672 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2673 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2674 | if (!objt_server(qc->conn->target)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2675 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2676 | if (!frm) |
| 2677 | return 0; |
| 2678 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2679 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2680 | 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] | 2681 | } |
| 2682 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2683 | 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] | 2684 | struct quic_connection_id *cid; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2685 | struct listener *l = __objt_listener(qc->conn->target); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2686 | |
| 2687 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2688 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2689 | goto err; |
| 2690 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2691 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2692 | if (!cid) |
| 2693 | goto err; |
| 2694 | |
| 2695 | /* insert the allocated CID in the receiver tree */ |
| 2696 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2697 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2698 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2699 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2700 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2701 | 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] | 2702 | } |
| 2703 | |
| 2704 | return 1; |
| 2705 | |
| 2706 | err: |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2707 | free_quic_conn_cids(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
| 2710 | |
| 2711 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2712 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2713 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2714 | struct eb64_node *n; |
| 2715 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2716 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2717 | n = eb64_first(&arngs->root); |
| 2718 | while (n) { |
| 2719 | struct eb64_node *next; |
| 2720 | |
| 2721 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2722 | next = eb64_next(n); |
| 2723 | eb64_delete(n); |
| 2724 | free(ar); |
| 2725 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2729 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2730 | * descending order. |
| 2731 | */ |
| 2732 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2733 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2734 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2735 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2740 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2741 | * 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] | 2742 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2743 | 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] | 2744 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2745 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2746 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2747 | last = eb64_last(&arngs->root); |
| 2748 | while (last && arngs->enc_sz > limit) { |
| 2749 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2750 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2751 | prev = eb64_prev(last); |
| 2752 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2753 | return 0; |
| 2754 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2755 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2756 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2757 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2758 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2759 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2760 | --arngs->sz; |
| 2761 | eb64_delete(last); |
| 2762 | pool_free(pool_head_quic_arng, last); |
| 2763 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | return 1; |
| 2767 | } |
| 2768 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2769 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2770 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2771 | { |
| 2772 | struct eb64_node *node, *next; |
| 2773 | struct quic_arng_node *ar, *ar_next; |
| 2774 | |
| 2775 | node = eb64_last(&arngs->root); |
| 2776 | if (!node) |
| 2777 | return; |
| 2778 | |
| 2779 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2780 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2781 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2782 | |
| 2783 | while ((next = eb64_prev(node))) { |
| 2784 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2785 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2786 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2787 | node = next; |
| 2788 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2789 | } |
| 2790 | } |
| 2791 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2792 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2793 | * 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] | 2794 | */ |
| 2795 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2796 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2797 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2798 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2799 | struct quic_arng_node *new_ar; |
| 2800 | |
| 2801 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2802 | if (new_ar) { |
| 2803 | new_ar->first.key = ar->first; |
| 2804 | new_ar->last = ar->last; |
| 2805 | eb64_insert(&arngs->root, &new_ar->first); |
| 2806 | arngs->sz++; |
| 2807 | } |
| 2808 | |
| 2809 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | /* 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] | 2813 | * 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] | 2814 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2815 | * |
| 2816 | * Descending order |
| 2817 | * -------------> |
| 2818 | * range1 range2 |
| 2819 | * ..........|--------|..............|--------| |
| 2820 | * ^ ^ ^ ^ |
| 2821 | * | | | | |
| 2822 | * last1 first1 last2 first2 |
| 2823 | * ..........+--------+--------------+--------+...... |
| 2824 | * diff1 gap12 diff2 |
| 2825 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2826 | * To encode the previous list of ranges we must encode integers as follows in |
| 2827 | * descending order: |
| 2828 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2829 | * with diff1 = last1 - first1 |
| 2830 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2831 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2832 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2833 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2834 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2835 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2836 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2837 | struct eb64_node *le; |
| 2838 | struct quic_arng_node *new_node; |
| 2839 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2840 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2841 | new = NULL; |
| 2842 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2843 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2844 | if (!new_node) |
| 2845 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2846 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2847 | goto out; |
| 2848 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2849 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2850 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2851 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2852 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2853 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2854 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2855 | |
| 2856 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2857 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2858 | else { |
| 2859 | struct quic_arng_node *le_ar = |
| 2860 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2861 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2862 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2863 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2864 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2865 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2866 | if (le_ar->last + 1 >= ar->first) { |
| 2867 | le_ar->last = ar->last; |
| 2868 | new = le; |
| 2869 | new_node = le_ar; |
| 2870 | } |
| 2871 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2872 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2873 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2874 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2875 | |
| 2876 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2877 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2878 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2879 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2880 | /* Verify that the new inserted node does not overlap the nodes |
| 2881 | * which follow it. |
| 2882 | */ |
| 2883 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2884 | struct eb64_node *next; |
| 2885 | struct quic_arng_node *next_node; |
| 2886 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2887 | while ((next = eb64_next(new))) { |
| 2888 | next_node = |
| 2889 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 2890 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2891 | break; |
| 2892 | |
| 2893 | if (next_node->last > new_node->last) |
| 2894 | new_node->last = next_node->last; |
| 2895 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 2896 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2897 | /* Decrement the size of these ranges. */ |
| 2898 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2899 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 2902 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2903 | quic_arngs_set_enc_sz(arngs); |
| 2904 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2905 | return 1; |
| 2906 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2907 | /* Remove the header protection of packets at <el> encryption level. |
| 2908 | * Always succeeds. |
| 2909 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2910 | 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] | 2911 | { |
| 2912 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2913 | struct quic_rx_packet *pqpkt; |
| 2914 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2915 | struct quic_enc_level *app_qel; |
| 2916 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2917 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2918 | app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2919 | /* 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] | 2920 | if (el == app_qel && objt_listener(ctx->conn->target) && |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2921 | 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] | 2922 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2923 | QUIC_EV_CONN_ELRMHP, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2924 | goto out; |
| 2925 | } |
| 2926 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2927 | 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] | 2928 | if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 2929 | pqpkt->data + pqpkt->pn_offset, |
| 2930 | pqpkt->data, pqpkt->data + pqpkt->len, ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2931 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | /* XXX TO DO XXX */ |
| 2933 | } |
| 2934 | else { |
| 2935 | /* The AAD includes the packet number field */ |
| 2936 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 2937 | /* Store the packet into the tree of packets to decrypt. */ |
| 2938 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2939 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 2940 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 2941 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2942 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2943 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->qc, pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2944 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2945 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 2946 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2950 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | /* Process all the CRYPTO frame at <el> encryption level. |
| 2954 | * Return 1 if succeeded, 0 if not. |
| 2955 | */ |
| 2956 | 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] | 2957 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2958 | { |
| 2959 | struct eb64_node *node; |
| 2960 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2961 | node = eb64_first(&el->rx.crypto.frms); |
| 2962 | while (node) { |
| 2963 | struct quic_rx_crypto_frm *cf; |
| 2964 | |
| 2965 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 2966 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 2967 | break; |
| 2968 | |
| 2969 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 2970 | goto err; |
| 2971 | |
| 2972 | node = eb64_next(node); |
| 2973 | quic_rx_packet_refdec(cf->pkt); |
| 2974 | eb64_delete(&cf->offset_node); |
| 2975 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 2976 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2977 | return 1; |
| 2978 | |
| 2979 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2980 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2981 | return 0; |
| 2982 | } |
| 2983 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2984 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2985 | * 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] | 2986 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2987 | * Return 1 if succeeded, 0 if not. |
| 2988 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2989 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 2990 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2991 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2992 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2993 | int64_t largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2994 | struct quic_conn *qc = ctx->conn->qc; |
| 2995 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2996 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 2997 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2998 | qel = cur_el; |
| 2999 | next_tel: |
| 3000 | if (!qel) |
| 3001 | goto out; |
| 3002 | |
| 3003 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3004 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3005 | while (node) { |
| 3006 | struct quic_rx_packet *pkt; |
| 3007 | |
| 3008 | 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] | 3009 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3010 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3011 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3012 | /* Drop the packet */ |
| 3013 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3014 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3015 | } |
| 3016 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3017 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3018 | /* Drop the packet */ |
| 3019 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3020 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3021 | } |
| 3022 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3023 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3024 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3025 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3026 | (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 3027 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3028 | if (pkt->pn > largest_pn) |
| 3029 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3030 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3031 | 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] | 3032 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3033 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3037 | eb64_delete(&pkt->pn_node); |
| 3038 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3039 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3040 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3041 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3042 | /* Update the largest packet number. */ |
| 3043 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3044 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3045 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3046 | goto err; |
| 3047 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3048 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3049 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3050 | qel = next_el; |
| 3051 | goto next_tel; |
| 3052 | } |
| 3053 | |
| 3054 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3055 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3056 | return 1; |
| 3057 | |
| 3058 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3059 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3060 | return 0; |
| 3061 | } |
| 3062 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3063 | /* QUIC connection packet handler task. */ |
| 3064 | 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] | 3065 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3066 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3067 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3068 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3069 | enum quic_tls_enc_level tel, next_tel; |
| 3070 | struct quic_enc_level *qel, *next_qel; |
| 3071 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3072 | struct qring *qr; // Tx ring |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3073 | int prev_st, st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3074 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3075 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3076 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3077 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3078 | st = HA_ATOMIC_LOAD(&qc->state); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3079 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3080 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3081 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3082 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3083 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA])); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3084 | start: |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3085 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3086 | goto err; |
| 3087 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3088 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3089 | 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] | 3090 | |
| 3091 | next_level: |
| 3092 | tls_ctx = &qel->tls_ctx; |
| 3093 | |
| 3094 | /* If the header protection key for this level has been derived, |
| 3095 | * remove the packet header protections. |
| 3096 | */ |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3097 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3098 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 3099 | qc_rm_hp_pkts(qel, ctx); |
| 3100 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3101 | prev_st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3102 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3103 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3104 | 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] | 3105 | goto err; |
| 3106 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3107 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3108 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3109 | qel = next_qel; |
| 3110 | next_qel = NULL; |
| 3111 | goto next_level; |
| 3112 | } |
| 3113 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3114 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 754f99e | 2021-08-19 15:35:59 +0200 | [diff] [blame] | 3115 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3116 | (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] | 3117 | /* Discard the Handshake keys. */ |
| 3118 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3119 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3120 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3121 | qc_set_timer(ctx); |
| 3122 | if (!quic_build_post_handshake_frames(qc)) |
| 3123 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3124 | |
| 3125 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3126 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3127 | goto start; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3130 | if (!qr) |
| 3131 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 3132 | ret = qc_prep_pkts(qr, ctx); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3133 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3134 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3135 | else if (ret == 0) |
| 3136 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3137 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3138 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3139 | goto err; |
| 3140 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3141 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3142 | /* Check if there is something to do for the next level. |
| 3143 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3144 | if (next_qel && next_qel != qel && |
| 3145 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 3146 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3148 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | goto next_level; |
| 3150 | } |
| 3151 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3152 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3153 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3154 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3155 | |
| 3156 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3157 | if (qr) |
| 3158 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3159 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3160 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3161 | } |
| 3162 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3163 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3164 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3165 | { |
| 3166 | int i; |
| 3167 | |
| 3168 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3169 | if (qel->tx.crypto.bufs[i]) { |
| 3170 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3171 | qel->tx.crypto.bufs[i] = NULL; |
| 3172 | } |
| 3173 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3174 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3178 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3179 | * Returns 1 if succeeded, 0 if not. |
| 3180 | */ |
| 3181 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3182 | enum quic_tls_enc_level level) |
| 3183 | { |
| 3184 | struct quic_enc_level *qel; |
| 3185 | |
| 3186 | qel = &qc->els[level]; |
| 3187 | qel->level = quic_to_ssl_enc_level(level); |
| 3188 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3189 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3190 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3191 | qel->tls_ctx.rx.flags = 0; |
| 3192 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3193 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3194 | |
| 3195 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3196 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3197 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3198 | qel->rx.crypto.offset = 0; |
| 3199 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3200 | |
| 3201 | /* Allocate only one buffer. */ |
| 3202 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3203 | if (!qel->tx.crypto.bufs) |
| 3204 | goto err; |
| 3205 | |
| 3206 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3207 | if (!qel->tx.crypto.bufs[0]) |
| 3208 | goto err; |
| 3209 | |
| 3210 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3211 | qel->tx.crypto.nb_buf = 1; |
| 3212 | |
| 3213 | qel->tx.crypto.sz = 0; |
| 3214 | qel->tx.crypto.offset = 0; |
| 3215 | |
| 3216 | return 1; |
| 3217 | |
| 3218 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3219 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3220 | return 0; |
| 3221 | } |
| 3222 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3223 | /* Release all the memory allocated for <conn> QUIC connection. */ |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3224 | static void quic_conn_free(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3225 | { |
| 3226 | int i; |
| 3227 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3228 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3229 | return; |
| 3230 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3231 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3232 | |
| 3233 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3234 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3235 | ebmb_delete(&qc->odcid_node); |
| 3236 | ebmb_delete(&qc->scid_node); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3237 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3238 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3239 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3240 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3241 | quic_conn_enc_level_uninit(&qc->els[i]); |
| 3242 | if (qc->timer_task) |
| 3243 | task_destroy(qc->timer_task); |
| 3244 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3245 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3246 | } |
| 3247 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3248 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3249 | { |
| 3250 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 3251 | struct quic_conn *qc = conn_ctx->conn->qc; |
| 3252 | quic_conn_free(qc); |
| 3253 | } |
| 3254 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3255 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3256 | 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] | 3257 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3258 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3259 | struct quic_conn *qc; |
| 3260 | struct quic_pktns *pktns; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3261 | int st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3262 | |
| 3263 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3264 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3265 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3266 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3267 | task->expire = TICK_ETERNITY; |
| 3268 | pktns = quic_loss_pktns(qc); |
| 3269 | if (tick_isset(pktns->tx.loss_time)) { |
| 3270 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3271 | |
| 3272 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3273 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 3274 | qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms); |
| 3275 | qc_set_timer(conn_ctx); |
| 3276 | goto out; |
| 3277 | } |
| 3278 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3279 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3280 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3281 | 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] | 3282 | pktns->tx.pto_probe = 1; |
| 3283 | } |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3284 | 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] | 3285 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3286 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3287 | |
| 3288 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3289 | hel->pktns->tx.pto_probe = 1; |
| 3290 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3291 | iel->pktns->tx.pto_probe = 1; |
| 3292 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 3293 | 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] | 3294 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3295 | qc->path->loss.pto_count++; |
| 3296 | |
| 3297 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3298 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3299 | |
| 3300 | return task; |
| 3301 | } |
| 3302 | |
| 3303 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3304 | * connections used to identify the first Initial packets of client connecting |
| 3305 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3306 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3307 | * <scid> is the source connection ID with <scid_len> as length. |
| 3308 | * Returns 1 if succeeded, 0 if not. |
| 3309 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3310 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3311 | unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3312 | 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] | 3313 | { |
| 3314 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3315 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | /* Initial CID. */ |
| 3317 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3318 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3319 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3320 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3321 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3322 | qc = pool_zalloc(pool_head_quic_conn); |
| 3323 | if (!qc) { |
| 3324 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3325 | goto err; |
| 3326 | } |
| 3327 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3328 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3329 | if (!buf_area) { |
| 3330 | TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT); |
| 3331 | goto err; |
| 3332 | } |
| 3333 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3334 | qc->cids = EB_ROOT; |
| 3335 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3336 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3337 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3338 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3339 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3340 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3341 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3342 | qc->odcid.addrlen = dcid_addr_len; |
| 3343 | memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3344 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3345 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3346 | if (scid_len) |
| 3347 | memcpy(qc->dcid.data, scid, scid_len); |
| 3348 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3349 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3350 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3351 | } |
| 3352 | /* QUIC Client (outgoing connection to servers) */ |
| 3353 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3354 | 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] | 3355 | if (dcid_len) |
| 3356 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3357 | qc->dcid.len = dcid_len; |
| 3358 | } |
| 3359 | |
| 3360 | /* Initialize the output buffer */ |
| 3361 | qc->obuf.pos = qc->obuf.data; |
| 3362 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3363 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3364 | if (!icid) { |
| 3365 | TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT); |
| 3366 | goto err; |
| 3367 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3368 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3369 | /* insert the allocated CID in the receiver tree */ |
| 3370 | if (server) { |
| 3371 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3372 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3373 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3374 | } |
| 3375 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3376 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3377 | qc->scid = icid->cid; |
| 3378 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3379 | /* Packet number spaces initialization. */ |
| 3380 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3381 | quic_pktns_init(&qc->pktns[i]); |
| 3382 | /* QUIC encryption level context initialization. */ |
| 3383 | 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] | 3384 | if (!quic_conn_enc_level_init(qc, i)) { |
| 3385 | 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] | 3386 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3387 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3388 | /* Initialize the packet number space. */ |
| 3389 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3390 | } |
| 3391 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3392 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3393 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3394 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3395 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3396 | /* TX part. */ |
| 3397 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3398 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3399 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3400 | qc->tx.bytes = 0; |
| 3401 | qc->tx.nb_pto_dgrams = 0; |
| 3402 | /* RX part. */ |
| 3403 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3404 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3405 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3406 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3407 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3408 | if (!quic_tls_ku_init(qc)) { |
| 3409 | TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT); |
| 3410 | goto err; |
| 3411 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3412 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3413 | /* XXX TO DO: Only one path at this time. */ |
| 3414 | qc->path = &qc->paths[0]; |
| 3415 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3416 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3417 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3418 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3419 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3420 | |
| 3421 | err: |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3422 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3423 | quic_conn_free(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3424 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | /* Initialize the timer task of <qc> QUIC connection. |
| 3428 | * Returns 1 if succeeded, 0 if not. |
| 3429 | */ |
| 3430 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3431 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3432 | /* Attach this task to the same thread ID used for the connection */ |
| 3433 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3434 | if (!qc->timer_task) |
| 3435 | return 0; |
| 3436 | |
| 3437 | qc->timer = TICK_ETERNITY; |
| 3438 | qc->timer_task->process = process_timer; |
| 3439 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3440 | |
| 3441 | return 1; |
| 3442 | } |
| 3443 | |
| 3444 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3445 | * past one byte of this buffer. |
| 3446 | */ |
| 3447 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3448 | struct quic_rx_packet *pkt) |
| 3449 | { |
| 3450 | unsigned char dcid_len, scid_len; |
| 3451 | |
| 3452 | /* Version */ |
| 3453 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3454 | return 0; |
| 3455 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3456 | /* Destination Connection ID Length */ |
| 3457 | dcid_len = *(*buf)++; |
| 3458 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3459 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3460 | /* XXX MUST BE DROPPED */ |
| 3461 | return 0; |
| 3462 | |
| 3463 | if (dcid_len) { |
| 3464 | /* Check that the length of this received DCID matches the CID lengths |
| 3465 | * of our implementation for non Initials packets only. |
| 3466 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3467 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3468 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3469 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | return 0; |
| 3471 | |
| 3472 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3473 | } |
| 3474 | |
| 3475 | pkt->dcid.len = dcid_len; |
| 3476 | *buf += dcid_len; |
| 3477 | |
| 3478 | /* Source Connection ID Length */ |
| 3479 | scid_len = *(*buf)++; |
| 3480 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3481 | /* XXX MUST BE DROPPED */ |
| 3482 | return 0; |
| 3483 | |
| 3484 | if (scid_len) |
| 3485 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3486 | pkt->scid.len = scid_len; |
| 3487 | *buf += scid_len; |
| 3488 | |
| 3489 | return 1; |
| 3490 | } |
| 3491 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3492 | /* If the header protection of <pkt> packet attached to <qc> connection with <ctx> |
| 3493 | * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated |
| 3494 | * encryption level matching with the packet type. <*qel> may be null if not found. |
| 3495 | * Note that <ctx> may be null (for Initial packets). |
| 3496 | */ |
| 3497 | 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] | 3498 | struct quic_conn *qc, struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3499 | struct quic_enc_level **qel) |
| 3500 | { |
| 3501 | enum quic_tls_enc_level tel; |
| 3502 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3503 | /* Special case without connection context (first Initial packets) */ |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3504 | if (!ctx) { |
| 3505 | *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3506 | return 1; |
| 3507 | } |
| 3508 | |
| 3509 | tel = quic_packet_type_enc_level(pkt->type); |
| 3510 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 3511 | *qel = NULL; |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
| 3515 | *qel = &qc->els[tel]; |
| 3516 | if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3517 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3518 | return 0; |
| 3519 | } |
| 3520 | |
| 3521 | 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] | 3522 | (tel != QUIC_TLS_ENC_LEVEL_APP || |
| 3523 | 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] | 3524 | return 1; |
| 3525 | |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3529 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3530 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3531 | { |
| 3532 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3533 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3534 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3535 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3536 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | /* 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] | 3540 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3541 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3542 | * the packet first byte. |
| 3543 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3544 | * Returns 1 if succeeded, 0 if not. |
| 3545 | */ |
| 3546 | static inline int qc_try_rm_hp(struct quic_rx_packet *pkt, |
| 3547 | unsigned char **buf, unsigned char *beg, |
| 3548 | const unsigned char *end, |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3549 | struct quic_conn *qc, struct quic_enc_level **el, |
| 3550 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3551 | { |
| 3552 | unsigned char *pn = NULL; /* Packet number field */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3553 | struct quic_enc_level *qel; |
| 3554 | /* Only for traces. */ |
| 3555 | struct quic_rx_packet *qpkt_trace; |
| 3556 | |
| 3557 | qpkt_trace = NULL; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3558 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3559 | /* The packet number is here. This is also the start minus |
| 3560 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3561 | * protection. |
| 3562 | */ |
| 3563 | pn = *buf; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3564 | 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] | 3565 | /* Note that the following function enables us to unprotect the packet |
| 3566 | * number and its length subsequently used to decrypt the entire |
| 3567 | * packets. |
| 3568 | */ |
| 3569 | if (!qc_do_rm_hp(pkt, &qel->tls_ctx, |
| 3570 | qel->pktns->rx.largest_pn, pn, beg, end, ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3571 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3572 | goto err; |
| 3573 | } |
| 3574 | |
| 3575 | /* The AAD includes the packet number field found at <pn>. */ |
| 3576 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3577 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3578 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3579 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3580 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3581 | /* If the packet number space has been discarded, this packet |
| 3582 | * will be not parsed. |
| 3583 | */ |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3584 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3585 | goto out; |
| 3586 | } |
| 3587 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3588 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3589 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3590 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3591 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3592 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3593 | else { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3594 | TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3595 | goto err; |
| 3596 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3597 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3598 | *el = qel; |
| 3599 | /* No reference counter incrementation here!!! */ |
| 3600 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3601 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3602 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3603 | b_add(&qc->rx.buf, pkt->len); |
| 3604 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3605 | /* Updtate the offset of <*buf> for the next QUIC packet. */ |
| 3606 | *buf = beg + pkt->len; |
| 3607 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3608 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3609 | return 1; |
| 3610 | |
| 3611 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3612 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3613 | return 0; |
| 3614 | } |
| 3615 | |
| 3616 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3617 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3618 | */ |
| 3619 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3620 | unsigned char byte0, int *long_header) |
| 3621 | { |
| 3622 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3623 | pkt->type = |
| 3624 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3625 | *long_header = 1; |
| 3626 | } |
| 3627 | else { |
| 3628 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3629 | *long_header = 0; |
| 3630 | } |
| 3631 | } |
| 3632 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3633 | /* |
| 3634 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3635 | */ |
| 3636 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3637 | { |
| 3638 | int j = 0, version; |
| 3639 | |
| 3640 | do { |
| 3641 | version = quic_supported_version[j]; |
| 3642 | if (version == pkt->version) |
| 3643 | return 1; |
| 3644 | |
| 3645 | version = quic_supported_version[++j]; |
| 3646 | } while(version); |
| 3647 | |
| 3648 | return 0; |
| 3649 | } |
| 3650 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3651 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3652 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3653 | struct quic_rx_packet *pkt, |
| 3654 | struct quic_dgram_ctx *dgram_ctx, |
| 3655 | struct sockaddr_storage *saddr) |
| 3656 | { |
| 3657 | unsigned char *beg; |
| 3658 | uint64_t len; |
| 3659 | struct quic_conn *qc; |
| 3660 | struct eb_root *cids; |
| 3661 | struct ebmb_node *node; |
| 3662 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3663 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3664 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3665 | size_t b_cspace; |
| 3666 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3667 | |
| 3668 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3669 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3670 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3671 | if (end <= *buf) |
| 3672 | goto err; |
| 3673 | |
| 3674 | /* Fixed bit */ |
| 3675 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3676 | /* XXX TO BE DISCARDED */ |
| 3677 | goto err; |
| 3678 | |
| 3679 | srv_conn = dgram_ctx->owner; |
| 3680 | beg = *buf; |
| 3681 | /* Header form */ |
| 3682 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3683 | if (long_header) { |
| 3684 | size_t cid_lookup_len; |
| 3685 | |
| 3686 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3687 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3688 | |
| 3689 | /* unsupported QUIC version */ |
| 3690 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3691 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3692 | goto err; |
| 3693 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3694 | |
| 3695 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3696 | * there is no Initial connection IDs storage. |
| 3697 | */ |
| 3698 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3699 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3700 | cid_lookup_len = pkt->dcid.len; |
| 3701 | } |
| 3702 | else { |
| 3703 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3704 | cid_lookup_len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3708 | if (!node) |
| 3709 | goto err; |
| 3710 | |
| 3711 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3712 | |
| 3713 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3714 | qc->dcid.len = pkt->scid.len; |
| 3715 | if (pkt->scid.len) |
| 3716 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3717 | } |
| 3718 | |
| 3719 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3720 | uint64_t token_len; |
| 3721 | |
| 3722 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3723 | goto err; |
| 3724 | |
| 3725 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3726 | * A server which sends an Initial packet must not set the token. |
| 3727 | * So, a client which receives an Initial packet with a token |
| 3728 | * MUST discard the packet or generate a connection error with |
| 3729 | * PROTOCOL_VIOLATION as type. |
| 3730 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3731 | */ |
| 3732 | pkt->token_len = token_len; |
| 3733 | } |
| 3734 | } |
| 3735 | else { |
| 3736 | /* XXX TO DO: Short header XXX */ |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3737 | if (end - *buf < QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3738 | goto err; |
| 3739 | |
| 3740 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3741 | node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3742 | if (!node) |
| 3743 | goto err; |
| 3744 | |
| 3745 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3746 | *buf += QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3747 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3748 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3749 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3750 | * have a length field. |
| 3751 | */ |
| 3752 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3753 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3754 | goto err; |
| 3755 | |
| 3756 | pkt->len = len; |
| 3757 | } |
| 3758 | else if (!long_header) { |
| 3759 | /* A short packet is the last one of an UDP datagram. */ |
| 3760 | pkt->len = end - *buf; |
| 3761 | } |
| 3762 | |
| 3763 | conn_ctx = qc->conn->xprt_ctx; |
| 3764 | |
| 3765 | /* Increase the total length of this packet by the header length. */ |
| 3766 | pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3767 | |
| 3768 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 3769 | * they must have the same DCID. |
| 3770 | * |
| 3771 | * This check must be done after the final update to pkt.len to |
| 3772 | * properly drop the packet on failure. |
| 3773 | */ |
| 3774 | if (!dgram_ctx->dcid.len) { |
| 3775 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 3776 | } |
| 3777 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3778 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3779 | goto err; |
| 3780 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3781 | dgram_ctx->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3782 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3783 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3784 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3785 | if (b_cspace < pkt->len) { |
| 3786 | /* Let us consume the remaining contiguous space. */ |
| 3787 | b_add(&qc->rx.buf, b_cspace); |
| 3788 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3789 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3790 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3791 | goto err; |
| 3792 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3793 | } |
| 3794 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3795 | if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) { |
| 3796 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3797 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3798 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3799 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3800 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3801 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3802 | if (pkt->aad_len) |
| 3803 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3804 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 3805 | if (conn_ctx) |
| 3806 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3807 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3808 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3809 | |
| 3810 | return pkt->len; |
| 3811 | |
| 3812 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 3813 | TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3814 | return -1; |
| 3815 | } |
| 3816 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3817 | /* |
| 3818 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3819 | * address <addr>. |
| 3820 | * Implementation of RFC9000 6. Version Negotiation |
| 3821 | * |
| 3822 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3823 | * |
| 3824 | * Returns 0 on success else non-zero |
| 3825 | */ |
| 3826 | static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3827 | struct quic_rx_packet *pkt) |
| 3828 | { |
| 3829 | char buf[256]; |
| 3830 | int i = 0, j, version; |
| 3831 | const socklen_t addrlen = get_addr_len(addr); |
| 3832 | |
| 3833 | /* |
| 3834 | * header form |
| 3835 | * long header, fixed bit to 0 for Version Negotiation |
| 3836 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3837 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3838 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3839 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3840 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3841 | /* null version for Version Negotiation */ |
| 3842 | buf[i++] = '\x00'; |
| 3843 | buf[i++] = '\x00'; |
| 3844 | buf[i++] = '\x00'; |
| 3845 | buf[i++] = '\x00'; |
| 3846 | |
| 3847 | /* source connection id */ |
| 3848 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3849 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3850 | i += pkt->scid.len; |
| 3851 | |
| 3852 | /* destination connection id */ |
| 3853 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3854 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3855 | i += pkt->dcid.len; |
| 3856 | |
| 3857 | /* supported version */ |
| 3858 | j = 0; |
| 3859 | do { |
| 3860 | version = htonl(quic_supported_version[j]); |
| 3861 | memcpy(&buf[i], &version, sizeof(version)); |
| 3862 | i += sizeof(version); |
| 3863 | |
| 3864 | version = quic_supported_version[++j]; |
| 3865 | } while (version); |
| 3866 | |
| 3867 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3868 | return 1; |
| 3869 | |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3873 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 3874 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 3875 | * concatenated to the <pkt> DCID field. |
| 3876 | * |
| 3877 | * Returns the instance or NULL if not found. |
| 3878 | */ |
| 3879 | static struct quic_conn *qc_retrieve_conn_from_cid(struct quic_rx_packet *pkt, |
| 3880 | struct listener *l, |
| 3881 | struct sockaddr_storage *saddr) |
| 3882 | { |
| 3883 | struct quic_conn *qc = NULL; |
| 3884 | struct ebmb_node *node; |
| 3885 | struct quic_connection_id *id; |
| 3886 | |
| 3887 | HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3888 | |
| 3889 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 3890 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 3891 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 3892 | /* DCIDs of first packets coming from multiple clients may have |
| 3893 | * the same values. Let's distinguish them by concatenating the |
| 3894 | * socket addresses. |
| 3895 | */ |
| 3896 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 3897 | node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data, |
| 3898 | pkt->dcid.len + pkt->dcid.addrlen); |
| 3899 | if (node) { |
| 3900 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 3901 | goto end; |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 3906 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 3907 | * used. |
| 3908 | */ |
| 3909 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len); |
| 3910 | if (!node) |
| 3911 | goto end; |
| 3912 | |
| 3913 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 3914 | qc = id->qc; |
| 3915 | |
| 3916 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 3917 | * If already done, this is a noop. |
| 3918 | */ |
Amaury Denoyelle | dbef985 | 2021-12-16 16:15:18 +0100 | [diff] [blame] | 3919 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3920 | |
| 3921 | end: |
| 3922 | HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3923 | |
| 3924 | return qc; |
| 3925 | } |
| 3926 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3927 | static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3928 | struct quic_rx_packet *pkt, |
| 3929 | struct quic_dgram_ctx *dgram_ctx, |
| 3930 | struct sockaddr_storage *saddr) |
| 3931 | { |
| 3932 | unsigned char *beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3933 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3934 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3935 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3936 | int long_header = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3937 | size_t b_cspace; |
| 3938 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3939 | |
| 3940 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 3941 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3942 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 3943 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 3944 | /* This ist only to please to traces and distinguish the |
| 3945 | * packet with parsed packet number from others. |
| 3946 | */ |
| 3947 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3948 | if (end <= *buf) |
| 3949 | goto err; |
| 3950 | |
| 3951 | /* Fixed bit */ |
| 3952 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) { |
| 3953 | /* XXX TO BE DISCARDED */ |
| 3954 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3955 | goto err; |
| 3956 | } |
| 3957 | |
| 3958 | l = dgram_ctx->owner; |
| 3959 | beg = *buf; |
| 3960 | /* Header form */ |
| 3961 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3962 | if (long_header) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3963 | if (!quic_packet_read_long_header(buf, end, pkt)) { |
| 3964 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3965 | goto err; |
| 3966 | } |
| 3967 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3968 | /* RFC9000 6. Version Negotiation */ |
| 3969 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3970 | /* do not send Version Negotiation in response to a |
| 3971 | * Version Negotiation packet. |
| 3972 | */ |
| 3973 | if (!pkt->version) { |
| 3974 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3975 | goto err; |
| 3976 | } |
| 3977 | |
| 3978 | /* unsupported version, send Negotiation packet */ |
| 3979 | if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) { |
| 3980 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 3981 | goto err; |
| 3982 | } |
| 3983 | |
| 3984 | TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT); |
| 3985 | goto out; |
| 3986 | } |
| 3987 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3988 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3989 | * there is no Initial connection IDs storage. |
| 3990 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3991 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3992 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3993 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3994 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || |
| 3995 | end - *buf < token_len) { |
| 3996 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3997 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3998 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3999 | |
| 4000 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 4001 | * A server which sends an Initial packet must not set the token. |
| 4002 | * So, a client which receives an Initial packet with a token |
| 4003 | * MUST discard the packet or generate a connection error with |
| 4004 | * PROTOCOL_VIOLATION as type. |
| 4005 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 4006 | */ |
| 4007 | pkt->token_len = token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4008 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4009 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4010 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4011 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4012 | goto err; |
| 4013 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4016 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 4017 | * have a length field. |
| 4018 | */ |
| 4019 | if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 4020 | uint64_t len; |
| 4021 | |
| 4022 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || |
| 4023 | end - *buf < len) { |
| 4024 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4025 | goto err; |
| 4026 | } |
| 4027 | |
| 4028 | pkt->len = len; |
| 4029 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4030 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4031 | qc = qc_retrieve_conn_from_cid(pkt, l, saddr); |
| 4032 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4033 | int ipv4; |
| 4034 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4035 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4036 | const unsigned char *salt = initial_salt_v1; |
| 4037 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4038 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4039 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4040 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4041 | goto err; |
| 4042 | } |
| 4043 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4044 | pkt->saddr = *saddr; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4045 | ipv4 = saddr->ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4046 | qc = qc_new_conn(pkt->version, ipv4, |
| 4047 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4048 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4049 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4050 | goto err; |
| 4051 | |
| 4052 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4053 | /* Copy the transport parameters. */ |
| 4054 | qc->rx.params = l->bind_conf->quic_params; |
| 4055 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4056 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4057 | odcid->len = pkt->dcid.len; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4058 | /* Copy the initial source connection ID. */ |
| 4059 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4060 | qc->enc_params_len = |
| 4061 | quic_transport_params_encode(qc->enc_params, |
| 4062 | qc->enc_params + sizeof qc->enc_params, |
| 4063 | &qc->rx.params, 1); |
| 4064 | if (!qc->enc_params_len) |
| 4065 | goto err; |
| 4066 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4067 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4068 | * chosen by the client for Initial packets. |
| 4069 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4070 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4071 | salt = initial_salt_draft_29; |
| 4072 | salt_len = sizeof initial_salt_draft_29; |
| 4073 | } |
| 4074 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4075 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4076 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4077 | goto err; |
| 4078 | } |
| 4079 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4080 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4081 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4082 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, |
| 4083 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4084 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4085 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4086 | /* If the insertion failed, it means that another |
| 4087 | * thread has already allocated a QUIC connection for |
| 4088 | * the same CID. Liberate our allocated connection. |
| 4089 | */ |
| 4090 | if (unlikely(n != &qc->odcid_node)) { |
| 4091 | quic_conn_free(qc); |
| 4092 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4093 | pkt->qc = qc; |
| 4094 | } |
| 4095 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4096 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4097 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4098 | MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list); |
| 4099 | /* Try to accept a new connection. */ |
| 4100 | listener_accept(l); |
| 4101 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4102 | } |
| 4103 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4104 | pkt->qc = qc; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 4105 | if (HA_ATOMIC_LOAD(&qc->conn)) |
| 4106 | conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | else { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4110 | if (end - *buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4111 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4112 | goto err; |
| 4113 | } |
| 4114 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4115 | memcpy(pkt->dcid.data, *buf, QUIC_HAP_CID_LEN); |
| 4116 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 4117 | *buf += QUIC_HAP_CID_LEN; |
| 4118 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4119 | qc = qc_retrieve_conn_from_cid(pkt, l, saddr); |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 4120 | if (HA_ATOMIC_LOAD(&qc->conn)) |
| 4121 | conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4122 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4123 | pkt->qc = qc; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4124 | /* A short packet is the last one of an UDP datagram. */ |
| 4125 | pkt->len = end - *buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4126 | } |
| 4127 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4128 | /* 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] | 4129 | pkt->raw_len = pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4132 | * they must have the same DCID. |
| 4133 | * |
| 4134 | * This check must be done after the final update to pkt.len to |
| 4135 | * properly drop the packet on failure. |
| 4136 | */ |
| 4137 | if (!dgram_ctx->dcid.len) { |
| 4138 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 4139 | } |
| 4140 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4141 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4142 | goto err; |
| 4143 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4144 | dgram_ctx->qc = qc; |
| 4145 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4146 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4147 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4148 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4149 | goto out; |
| 4150 | } |
| 4151 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4152 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4153 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4154 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4155 | if (b_cspace < pkt->len) { |
| 4156 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4157 | if (b_cspace) { |
| 4158 | b_putchr(&qc->rx.buf, 0x00); |
| 4159 | b_cspace--; |
| 4160 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4161 | b_add(&qc->rx.buf, b_cspace); |
| 4162 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4163 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4164 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 91ac6c3 | 2021-12-17 16:11:54 +0100 | [diff] [blame] | 4165 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4166 | goto err; |
| 4167 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4170 | if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) { |
| 4171 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4172 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4173 | goto err; |
| 4174 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4175 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4176 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4177 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4178 | if (pkt->aad_len) |
| 4179 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4180 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4181 | /* Wake up the connection packet handler task from here only if all |
| 4182 | * the contexts have been initialized, especially the mux context |
| 4183 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4184 | * will start it if these contexts for the connection are not already |
| 4185 | * initialized. |
| 4186 | */ |
| 4187 | 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] | 4188 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4189 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4190 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4191 | |
| 4192 | return pkt->len; |
| 4193 | |
| 4194 | err: |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4195 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4196 | qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4197 | return -1; |
| 4198 | } |
| 4199 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4200 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4201 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4202 | */ |
| 4203 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4204 | int type, size_t pn_len, struct quic_conn *conn) |
| 4205 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4206 | if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4207 | return 0; |
| 4208 | |
| 4209 | /* #0 byte flags */ |
| 4210 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4211 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4212 | /* Version */ |
| 4213 | quic_write_uint32(buf, end, conn->version); |
| 4214 | *(*buf)++ = conn->dcid.len; |
| 4215 | /* Destination connection ID */ |
| 4216 | if (conn->dcid.len) { |
| 4217 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4218 | *buf += conn->dcid.len; |
| 4219 | } |
| 4220 | /* Source connection ID */ |
| 4221 | *(*buf)++ = conn->scid.len; |
| 4222 | if (conn->scid.len) { |
| 4223 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4224 | *buf += conn->scid.len; |
| 4225 | } |
| 4226 | |
| 4227 | return 1; |
| 4228 | } |
| 4229 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4230 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4231 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4232 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4233 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4234 | size_t pn_len, struct quic_conn *conn, |
| 4235 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4236 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4237 | if (end - *buf < 1 + conn->dcid.len) |
| 4238 | return 0; |
| 4239 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4240 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4241 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4242 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4243 | /* Destination connection ID */ |
| 4244 | if (conn->dcid.len) { |
| 4245 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4246 | *buf += conn->dcid.len; |
| 4247 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4248 | |
| 4249 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4253 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4254 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4255 | * Returns 1 if succeeded or 0 if failed. |
| 4256 | */ |
| 4257 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4258 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4259 | { |
| 4260 | int i, ret, outlen; |
| 4261 | EVP_CIPHER_CTX *ctx; |
| 4262 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4263 | * and at most 4 bytes for the packet number |
| 4264 | */ |
| 4265 | unsigned char mask[5] = {0}; |
| 4266 | |
| 4267 | ret = 0; |
| 4268 | ctx = EVP_CIPHER_CTX_new(); |
| 4269 | if (!ctx) |
| 4270 | return 0; |
| 4271 | |
| 4272 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4273 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4274 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4275 | goto out; |
| 4276 | |
| 4277 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4278 | for (i = 0; i < pnlen; i++) |
| 4279 | pn[i] ^= mask[i + 1]; |
| 4280 | |
| 4281 | ret = 1; |
| 4282 | |
| 4283 | out: |
| 4284 | EVP_CIPHER_CTX_free(ctx); |
| 4285 | |
| 4286 | return ret; |
| 4287 | } |
| 4288 | |
| 4289 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4290 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4291 | * Return 1 if succeeded, 0 if not. |
| 4292 | */ |
| 4293 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4294 | { |
| 4295 | size_t room, ack_delay_sz; |
| 4296 | |
| 4297 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4298 | /* A frame is made of 1 byte for the frame type. */ |
| 4299 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4300 | 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] | 4301 | return 0; |
| 4302 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4303 | 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] | 4304 | } |
| 4305 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4306 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4307 | * 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] | 4308 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4309 | * 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] | 4310 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4311 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4312 | * Update consequently <*len> to reflect the size of these frames built |
| 4313 | * 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] | 4314 | * Return 1 if succeeded, 0 if not. |
| 4315 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4316 | static inline int qc_build_frms(struct quic_tx_packet *pkt, |
| 4317 | size_t room, size_t *len, size_t headlen, |
| 4318 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4319 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4320 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4321 | int ret; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4322 | struct quic_frame *cf; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4323 | struct mt_list *tmp1, tmp2; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4324 | size_t remain = quic_path_prep_data(qc->path); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4325 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4326 | ret = 0; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4327 | if (*len > room || headlen > remain) |
| 4328 | return 0; |
| 4329 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4330 | /* If we are not probing we must take into an account the congestion |
| 4331 | * control window. |
| 4332 | */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4333 | if (!qc->tx.nb_pto_dgrams) |
| 4334 | room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4335 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4336 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4337 | 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] | 4338 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4339 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4340 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4341 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4342 | break; |
| 4343 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4344 | switch (cf->type) { |
| 4345 | case QUIC_FT_CRYPTO: |
| 4346 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4347 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4348 | /* Compute the length of this CRYPTO frame header */ |
| 4349 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4350 | /* Compute the data length of this CRyPTO frame. */ |
| 4351 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4352 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4353 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4354 | if (!dlen) |
| 4355 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4356 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4357 | /* CRYPTO frame length. */ |
| 4358 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4359 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4360 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4361 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4362 | * length and the length of this length. |
| 4363 | */ |
| 4364 | *len += flen; |
| 4365 | room -= flen; |
| 4366 | if (dlen == cf->crypto.len) { |
| 4367 | /* <cf> CRYPTO data have been consumed. */ |
| 4368 | MT_LIST_DELETE_SAFE(tmp1); |
| 4369 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4370 | } |
| 4371 | else { |
| 4372 | struct quic_frame *new_cf; |
| 4373 | |
| 4374 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4375 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4376 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4377 | return 0; |
| 4378 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4379 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4380 | new_cf->type = QUIC_FT_CRYPTO; |
| 4381 | new_cf->crypto.len = dlen; |
| 4382 | new_cf->crypto.offset = cf->crypto.offset; |
| 4383 | new_cf->crypto.qel = qel; |
| 4384 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4385 | /* Consume <dlen> bytes of the current frame. */ |
| 4386 | cf->crypto.len -= dlen; |
| 4387 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4388 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4389 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4390 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4391 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4392 | /* Note that these frames are accepted in short packets only without |
| 4393 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4394 | * sum of the lengths of the already built frames for this packet. |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4395 | * |
| 4396 | * Compute the length of this STREAM frame "header" made a all the field |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4397 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4398 | */ |
| 4399 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4400 | ((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] | 4401 | /* Compute the data length of this STREAM frame. */ |
| 4402 | avail_room = room - hlen - *len; |
| 4403 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4404 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4405 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4406 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4407 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4408 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4409 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4410 | if (dlen > cf->stream.len) { |
| 4411 | dlen = cf->stream.len; |
| 4412 | } |
| 4413 | dlen_sz = quic_int_getsize(dlen); |
| 4414 | flen = hlen + dlen_sz + dlen; |
| 4415 | } |
| 4416 | else { |
| 4417 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4418 | flen = hlen + dlen; |
| 4419 | } |
| 4420 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4421 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4422 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4423 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4424 | /* Add the STREAM data length and its encoded length to the packet |
| 4425 | * length and the length of this length. |
| 4426 | */ |
| 4427 | *len += flen; |
| 4428 | room -= flen; |
| 4429 | if (dlen == cf->stream.len) { |
| 4430 | /* <cf> STREAM data have been consumed. */ |
| 4431 | MT_LIST_DELETE_SAFE(tmp1); |
| 4432 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4433 | } |
| 4434 | else { |
| 4435 | struct quic_frame *new_cf; |
| 4436 | |
| 4437 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4438 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4439 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4440 | return 0; |
| 4441 | } |
| 4442 | |
| 4443 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4444 | new_cf->stream.qcs = cf->stream.qcs; |
| 4445 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4446 | new_cf->stream.id = cf->stream.id; |
| 4447 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4448 | new_cf->stream.offset = cf->stream.offset; |
| 4449 | new_cf->stream.len = dlen; |
| 4450 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4451 | /* FIN bit reset */ |
| 4452 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4453 | new_cf->stream.data = cf->stream.data; |
| 4454 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4455 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4456 | /* Consume <dlen> bytes of the current frame. */ |
| 4457 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4458 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4459 | cf->stream.data += dlen; |
| 4460 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4461 | break; |
| 4462 | |
| 4463 | default: |
| 4464 | flen = qc_frm_len(cf); |
| 4465 | BUG_ON(!flen); |
| 4466 | if (flen > room) |
| 4467 | continue; |
| 4468 | |
| 4469 | *len += flen; |
| 4470 | room -= flen; |
| 4471 | MT_LIST_DELETE_SAFE(tmp1); |
| 4472 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4473 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4474 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4475 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4478 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4479 | } |
| 4480 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4481 | /* 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] | 4482 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4483 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4484 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4485 | * 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] | 4486 | * 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] | 4487 | * having returned from this function. |
| 4488 | * 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] | 4489 | * number field in this packet. <pn_len> will also have the packet number |
| 4490 | * length as value. |
| 4491 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4492 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4493 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4494 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4495 | size_t dglen, struct quic_tx_packet *pkt, |
| 4496 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 4497 | int ack, int padding, int cc, int nb_pto_dgrams, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4498 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4499 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4500 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4501 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4502 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4503 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4504 | struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4505 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4506 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4507 | int add_ping_frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4508 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4509 | /* Length field value with CRYPTO frames if present. */ |
| 4510 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4511 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4512 | /* When not probing and not acking, and no immediate close is required, |
| 4513 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4514 | * the congestion controller window. So, we do not limit the size of this |
| 4515 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4516 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4517 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4518 | */ |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4519 | if (!nb_pto_dgrams && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4520 | size_t path_room; |
| 4521 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4522 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4523 | if (end - beg > path_room) |
| 4524 | end = beg + path_room; |
| 4525 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4526 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4527 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4528 | * length field if any. |
| 4529 | */ |
| 4530 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4531 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4532 | goto no_room; |
| 4533 | |
| 4534 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4535 | 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] | 4536 | /* packet number length */ |
| 4537 | *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] | 4538 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4539 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4540 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4541 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4542 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc))) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4543 | goto no_room; |
| 4544 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4545 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4546 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4547 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4548 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4549 | /* Build an ACK frame if required. */ |
| 4550 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4551 | if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4552 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4553 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4554 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4555 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4556 | * that from here, we do not know if we will have to send a PING frame. |
| 4557 | * This will be decided after having computed the ack-eliciting frames |
| 4558 | * to be added to this packet. |
| 4559 | */ |
| 4560 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4561 | if (!ack_frm_len) |
| 4562 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4563 | } |
| 4564 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4565 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4566 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4567 | len_frms = 0; |
| 4568 | if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4569 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4570 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4571 | /* Initialize the length of the frames built below to <len>. |
| 4572 | * If any frame could be successfully built by qc_build_frms(), |
| 4573 | * we will have len_frms > len. |
| 4574 | */ |
| 4575 | len_frms = len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4576 | if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4577 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4578 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4579 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4580 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4581 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4582 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4583 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4584 | * for the encryption tag. It must be taken into an account for the length |
| 4585 | * of this packet. |
| 4586 | */ |
| 4587 | if (len_frms) |
| 4588 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4589 | else |
| 4590 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4591 | /* CONNECTION_CLOSE frame */ |
| 4592 | if (cc) { |
| 4593 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4594 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4595 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4596 | len += qc_frm_len(&cc_frm); |
| 4597 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4598 | add_ping_frm = 0; |
| 4599 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4600 | len_sz = quic_int_getsize(len); |
| 4601 | /* Add this packet size to <dglen> */ |
| 4602 | dglen += head_len + len_sz + len; |
| 4603 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4604 | /* This is a maximum padding size */ |
| 4605 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4606 | /* The length field value is of this packet is <len> + <padding_len> |
| 4607 | * the size of which may be greater than the initial computed size |
| 4608 | * <len_sz>. So, let's deduce the difference betwen these to packet |
| 4609 | * sizes from <padding_len>. |
| 4610 | */ |
| 4611 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4612 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4613 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4614 | else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4615 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4616 | /* 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] | 4617 | add_ping_frm = 1; |
| 4618 | len += 1; |
| 4619 | } |
| 4620 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4621 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4622 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4623 | } |
| 4624 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4625 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 4626 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4627 | |
| 4628 | /* Packet number field address. */ |
| 4629 | *buf_pn = pos; |
| 4630 | |
| 4631 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4632 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 4633 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4634 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4635 | if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4636 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4637 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4638 | /* Ack-eliciting frames */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4639 | if (!LIST_ISEMPTY(&pkt->frms)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4640 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4641 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4642 | TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4643 | list_for_each_entry(cf, &pkt->frms, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4644 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4645 | ssize_t room = end - pos; |
| 4646 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4647 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4648 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4649 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | /* Build a PING frame if needed. */ |
| 4654 | if (add_ping_frm) { |
| 4655 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4656 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4657 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4660 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4661 | if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4662 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4663 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4664 | /* Build a PADDING frame if needed. */ |
| 4665 | if (padding_len) { |
| 4666 | frm.type = QUIC_FT_PADDING; |
| 4667 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4668 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4669 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4670 | } |
| 4671 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4672 | /* Always reset this variable as this function has no idea |
| 4673 | * if it was set. It is handle by the loss detection timer. |
| 4674 | */ |
| 4675 | qel->pktns->tx.pto_probe = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4676 | pkt->len = pos - beg; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4677 | |
| 4678 | return 1; |
| 4679 | |
| 4680 | no_room: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4681 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4682 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4683 | } |
| 4684 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4685 | 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] | 4686 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4687 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4688 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4689 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 4690 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4691 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4692 | pkt->next = NULL; |
| 4693 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4694 | } |
| 4695 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4696 | /* 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] | 4697 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 4698 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4699 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4700 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4701 | if (!pkt) |
| 4702 | return; |
| 4703 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4704 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4705 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4706 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4707 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4708 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4709 | } |
| 4710 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4711 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 4712 | * type for <qc> QUIC connection from <qel> encryption level. |
| 4713 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 4714 | * -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] | 4715 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4716 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 4717 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4718 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4719 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4720 | int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4721 | { |
| 4722 | /* The pointer to the packet number field. */ |
| 4723 | unsigned char *buf_pn; |
| 4724 | unsigned char *beg, *end, *payload; |
| 4725 | int64_t pn; |
| 4726 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4727 | struct quic_tls_ctx *tls_ctx; |
| 4728 | struct quic_tx_packet *pkt; |
| 4729 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4730 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4731 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4732 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 4733 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4734 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4735 | *err = -2; |
| 4736 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4737 | } |
| 4738 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4739 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4740 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4741 | pn_len = 0; |
| 4742 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4743 | |
| 4744 | pn = qel->pktns->tx.next_pn + 1; |
| 4745 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 4746 | ack, padding, cc, nb_pto_dgrams, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4747 | *err = -1; |
| 4748 | goto err; |
| 4749 | } |
| 4750 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4751 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4752 | payload = buf_pn + pn_len; |
| 4753 | payload_len = end - payload; |
| 4754 | aad_len = payload - beg; |
| 4755 | |
| 4756 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4757 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) { |
| 4758 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4759 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4760 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4761 | |
| 4762 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4763 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4764 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 4765 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4766 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4767 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4768 | goto err; |
| 4769 | } |
| 4770 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4771 | /* Consume a packet number */ |
| 4772 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 4773 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4774 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 4775 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4776 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 4777 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4778 | /* 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] | 4779 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4780 | pkt->in_flight_len = pkt->len; |
| 4781 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4782 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4783 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4784 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4785 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4786 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4787 | |
| 4788 | err: |
| 4789 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4790 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4791 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4792 | } |
| 4793 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4794 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 4795 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4796 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4797 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 4798 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4799 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4800 | size_t try, done = 0; |
| 4801 | |
| 4802 | if (!conn_ctrl_ready(conn)) |
| 4803 | return 0; |
| 4804 | |
| 4805 | if (!fd_recv_ready(conn->handle.fd)) |
| 4806 | return 0; |
| 4807 | |
| 4808 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4809 | |
| 4810 | /* read the largest possible block. For this, we perform only one call |
| 4811 | * 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] | 4812 | * 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] | 4813 | */ |
| 4814 | while (count > 0) { |
| 4815 | try = b_contig_space(buf); |
| 4816 | if (!try) |
| 4817 | break; |
| 4818 | |
| 4819 | if (try > count) |
| 4820 | try = count; |
| 4821 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4822 | b_add(buf, try); |
| 4823 | done += try; |
| 4824 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4828 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4829 | |
| 4830 | leave: |
| 4831 | return done; |
| 4832 | |
| 4833 | read0: |
| 4834 | conn_sock_read0(conn); |
| 4835 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4836 | |
| 4837 | /* Now a final check for a possible asynchronous low-level error |
| 4838 | * report. This can happen when a connection receives a reset |
| 4839 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 4840 | * we might have come from there by just checking POLL_HUP instead |
| 4841 | * of recv()'s return value 0, so we have no way to tell there was |
| 4842 | * an error without checking. |
| 4843 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 4844 | 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] | 4845 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4846 | goto leave; |
| 4847 | } |
| 4848 | |
| 4849 | |
| 4850 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 4851 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 4852 | * other pending data for example, but this flag is ignored at the moment. |
| 4853 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4854 | * a second call may be performed. The connection's flags are updated with |
| 4855 | * whatever special event is detected (error, empty). The caller is responsible |
| 4856 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4857 | * function does not call the connection's polling update function, so the caller |
| 4858 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 4859 | * based on the return value. |
| 4860 | */ |
| 4861 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 4862 | { |
| 4863 | ssize_t ret; |
| 4864 | size_t try, done; |
| 4865 | int send_flag; |
| 4866 | |
| 4867 | if (!conn_ctrl_ready(conn)) |
| 4868 | return 0; |
| 4869 | |
| 4870 | if (!fd_send_ready(conn->handle.fd)) |
| 4871 | return 0; |
| 4872 | |
| 4873 | done = 0; |
| 4874 | /* send the largest possible block. For this we perform only one call |
| 4875 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4876 | * in which case we accept to do it once again. |
| 4877 | */ |
| 4878 | while (count) { |
| 4879 | try = b_contig_data(buf, done); |
| 4880 | if (try > count) |
| 4881 | try = count; |
| 4882 | |
| 4883 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 4884 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 4885 | send_flag |= MSG_MORE; |
| 4886 | |
| 4887 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 4888 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 4889 | if (ret > 0) { |
| 4890 | count -= ret; |
| 4891 | done += ret; |
| 4892 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4893 | /* A send succeeded, so we can consider ourself connected */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4894 | conn->flags |= CO_FL_WAIT_L4L6; |
| 4895 | /* if the system buffer is full, don't insist */ |
| 4896 | if (ret < try) |
| 4897 | break; |
| 4898 | } |
| 4899 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 4900 | /* nothing written, we need to poll for write first */ |
| 4901 | fd_cant_send(conn->handle.fd); |
| 4902 | break; |
| 4903 | } |
| 4904 | else if (errno != EINTR) { |
| 4905 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4906 | break; |
| 4907 | } |
| 4908 | } |
| 4909 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4910 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4911 | |
| 4912 | if (done > 0) { |
| 4913 | /* we count the total bytes sent, and the send rate for 32-byte |
| 4914 | * blocks. The reason for the latter is that freq_ctr are |
| 4915 | * limited to 4GB and that it's not enough per second. |
| 4916 | */ |
| 4917 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 4918 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 4919 | } |
| 4920 | return done; |
| 4921 | } |
| 4922 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4923 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 4924 | * event subscriber <es> is not allowed to change from a previous call as long |
| 4925 | * as at least one event is still subscribed. The <event_type> must only be a |
| 4926 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 4927 | */ |
| 4928 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4929 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4930 | struct qcc *qcc = conn->qc->qcc; |
| 4931 | |
| 4932 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 4933 | BUG_ON(qcc->subs && qcc->subs != es); |
| 4934 | |
| 4935 | es->events |= event_type; |
| 4936 | qcc->subs = es; |
| 4937 | |
| 4938 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4939 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4940 | |
| 4941 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame^] | 4942 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4943 | |
| 4944 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 4948 | * The <es> pointer is not allowed to differ from the one passed to the |
| 4949 | * subscribe() call. It always returns zero. |
| 4950 | */ |
| 4951 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4952 | { |
| 4953 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 4954 | } |
| 4955 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 4956 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4957 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4958 | * parameters of this session. |
| 4959 | * This is the responsibility of the caller to check the validity of all the |
| 4960 | * pointers passed as parameter to this function. |
| 4961 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4962 | * CO_ER_SSL_NO_MEM. |
| 4963 | */ |
| 4964 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4965 | unsigned char *params, size_t params_len) |
| 4966 | { |
| 4967 | int retry; |
| 4968 | |
| 4969 | retry = 1; |
| 4970 | retry: |
| 4971 | *ssl = SSL_new(ssl_ctx); |
| 4972 | if (!*ssl) { |
| 4973 | if (!retry--) |
| 4974 | goto err; |
| 4975 | |
| 4976 | pool_gc(NULL); |
| 4977 | goto retry; |
| 4978 | } |
| 4979 | |
| 4980 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4981 | !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) || |
| 4982 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 4983 | goto err; |
| 4984 | |
| 4985 | SSL_free(*ssl); |
| 4986 | *ssl = NULL; |
| 4987 | if (!retry--) |
| 4988 | goto err; |
| 4989 | |
| 4990 | pool_gc(NULL); |
| 4991 | goto retry; |
| 4992 | } |
| 4993 | |
| 4994 | return 0; |
| 4995 | |
| 4996 | err: |
| 4997 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4998 | return -1; |
| 4999 | } |
| 5000 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5001 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 5002 | * connection with <xprt_ctx> as address of the xprt context. |
| 5003 | * Returns 1 if succeeded, 0 if not. |
| 5004 | */ |
| 5005 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5006 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5007 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5008 | |
| 5009 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5010 | |
| 5011 | if (*xprt_ctx) |
| 5012 | goto out; |
| 5013 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5014 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5015 | if (!ctx) { |
| 5016 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5017 | goto err; |
| 5018 | } |
| 5019 | |
| 5020 | ctx->wait_event.tasklet = tasklet_new(); |
| 5021 | if (!ctx->wait_event.tasklet) { |
| 5022 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5023 | goto err; |
| 5024 | } |
| 5025 | |
| 5026 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5027 | ctx->wait_event.tasklet->context = ctx; |
| 5028 | ctx->wait_event.events = 0; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5029 | HA_ATOMIC_STORE(&ctx->conn, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5030 | ctx->subs = NULL; |
| 5031 | ctx->xprt_ctx = NULL; |
| 5032 | |
| 5033 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5034 | if (objt_server(conn->target)) { |
| 5035 | /* Server */ |
| 5036 | struct server *srv = __objt_server(conn->target); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5037 | unsigned char dcid[QUIC_HAP_CID_LEN]; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5038 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5039 | int ssl_err, ipv4; |
| 5040 | |
| 5041 | ssl_err = SSL_ERROR_NONE; |
| 5042 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 5043 | goto err; |
| 5044 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5045 | ipv4 = conn->dst->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5046 | qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5047 | dcid, sizeof dcid, 0, NULL, 0, 0, srv); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5048 | if (qc == NULL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5049 | goto err; |
| 5050 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5051 | ctx->qc = qc; |
| 5052 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5053 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 5054 | ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len); |
| 5055 | |
| 5056 | conn->qc = qc; |
| 5057 | qc->conn = conn; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5058 | if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1, |
| 5059 | dcid, sizeof dcid, 0)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5060 | goto err; |
| 5061 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5062 | qc->rx.params = srv->quic_params; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5063 | /* Copy the initial source connection ID. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5064 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 5065 | qc->enc_params_len = |
| 5066 | quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params, |
| 5067 | &qc->rx.params, 0); |
| 5068 | if (!qc->enc_params_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5069 | goto err; |
| 5070 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5071 | if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl, |
| 5072 | qc->enc_params, qc->enc_params_len) == -1) |
| 5073 | goto err; |
| 5074 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5075 | 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] | 5076 | SSL_set_connect_state(ctx->ssl); |
| 5077 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 5078 | if (ssl_err != 1) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5079 | int st; |
| 5080 | |
| 5081 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 5083 | 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] | 5084 | 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] | 5085 | } |
| 5086 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5087 | 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] | 5088 | goto err; |
| 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | else if (objt_listener(conn->target)) { |
| 5093 | /* Listener */ |
| 5094 | 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] | 5095 | struct quic_conn *qc = ctx->conn->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5096 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5097 | ctx->qc = qc; |
| 5098 | |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 5099 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid); |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5100 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5101 | qc->enc_params, qc->enc_params_len) == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5102 | goto err; |
| 5103 | |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 5104 | /* Enabling 0-RTT */ |
| 5105 | if (bc->ssl_conf.early_data) |
| 5106 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5107 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5108 | SSL_set_accept_state(ctx->ssl); |
| 5109 | } |
| 5110 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5111 | HA_ATOMIC_STORE(xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5112 | |
| 5113 | /* Leave init state and start handshake */ |
| 5114 | 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] | 5115 | |
| 5116 | out: |
| 5117 | TRACE_LEAVE(QUIC_EV_CONN_NEW, conn); |
| 5118 | |
| 5119 | return 0; |
| 5120 | |
| 5121 | err: |
Willy Tarreau | 7deb28c | 2021-05-10 07:40:27 +0200 | [diff] [blame] | 5122 | if (ctx && ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5123 | tasklet_free(ctx->wait_event.tasklet); |
| 5124 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5125 | 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] | 5126 | return -1; |
| 5127 | } |
| 5128 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5129 | /* Start the QUIC transport layer */ |
| 5130 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5131 | { |
| 5132 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5133 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5134 | |
| 5135 | qc = conn->qc; |
| 5136 | if (!quic_conn_init_timer(qc)) { |
| 5137 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn); |
| 5138 | return 0; |
| 5139 | } |
| 5140 | |
| 5141 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5142 | return 1; |
| 5143 | } |
| 5144 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5145 | /* transport-layer operations for QUIC connections. */ |
| 5146 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5147 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5148 | .snd_buf = quic_conn_from_buf, |
| 5149 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5150 | .subscribe = quic_conn_subscribe, |
| 5151 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5152 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5153 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5154 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5155 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5156 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5157 | .name = "QUIC", |
| 5158 | }; |
| 5159 | |
| 5160 | __attribute__((constructor)) |
| 5161 | static void __quic_conn_init(void) |
| 5162 | { |
| 5163 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5164 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5165 | } |
| 5166 | |
| 5167 | __attribute__((destructor)) |
| 5168 | static void __quic_conn_deinit(void) |
| 5169 | { |
| 5170 | BIO_meth_free(ha_quic_meth); |
| 5171 | } |
| 5172 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5173 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5174 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5175 | * 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] | 5176 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5177 | 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] | 5178 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5179 | { |
| 5180 | unsigned char *pos; |
| 5181 | const unsigned char *end; |
| 5182 | struct quic_dgram_ctx dgram_ctx = { |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5183 | .qc = NULL, |
| 5184 | .dcid.len = 0, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5185 | .owner = owner, |
| 5186 | }; |
| 5187 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5188 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5189 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5190 | do { |
| 5191 | int ret; |
| 5192 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5193 | size_t pkt_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5194 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5195 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5196 | if (!pkt) |
| 5197 | goto err; |
| 5198 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5199 | quic_rx_packet_refinc(pkt); |
| 5200 | ret = func(&pos, end, pkt, &dgram_ctx, saddr); |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5201 | pkt_len = pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5202 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5203 | if (ret == -1 && !pkt_len) |
| 5204 | /* If the packet length could not be found, we cannot continue. */ |
| 5205 | break; |
| 5206 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5207 | } while (pos < end); |
| 5208 | |
| 5209 | /* Increasing the received bytes counter by the UDP datagram length |
| 5210 | * if this datagram could be associated to a connection. |
| 5211 | */ |
| 5212 | if (dgram_ctx.qc) |
| 5213 | dgram_ctx.qc->rx.bytes += len; |
| 5214 | |
| 5215 | return pos - (unsigned char *)buf; |
| 5216 | |
| 5217 | err: |
| 5218 | return -1; |
| 5219 | } |
| 5220 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5221 | 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] | 5222 | struct sockaddr_storage *saddr) |
| 5223 | { |
| 5224 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5225 | } |
| 5226 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5227 | /* Function to automatically activate QUIC traces on stdout. |
| 5228 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5229 | * Main use for now is in the docker image for QUIC interop testing. |
| 5230 | */ |
| 5231 | static void quic_init_stdout_traces(void) |
| 5232 | { |
| 5233 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5234 | trace_quic.sink = sink_find("stdout"); |
| 5235 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5236 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5237 | #endif |
| 5238 | } |
| 5239 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5240 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5241 | /* |
| 5242 | * Local variables: |
| 5243 | * c-indent-level: 8 |
| 5244 | * c-basic-offset: 8 |
| 5245 | * End: |
| 5246 | */ |