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