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 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <netinet/tcp.h> |
| 23 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 24 | #include <import/ebmbtree.h> |
| 25 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 26 | #include <haproxy/buf-t.h> |
| 27 | #include <haproxy/compat.h> |
| 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/debug.h> |
| 30 | #include <haproxy/tools.h> |
| 31 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 32 | |
| 33 | #include <haproxy/connection.h> |
| 34 | #include <haproxy/fd.h> |
| 35 | #include <haproxy/freq_ctr.h> |
| 36 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 37 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 38 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 39 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 40 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 41 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 47 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 48 | #include <haproxy/quic_stats-t.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 49 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 50 | #include <haproxy/cbuf.h> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 51 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 52 | #include <haproxy/quic_tls.h> |
| 53 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 54 | #include <haproxy/task.h> |
| 55 | #include <haproxy/trace.h> |
| 56 | #include <haproxy/xprt_quic.h> |
| 57 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 58 | /* list of supported QUIC versions by this implementation */ |
| 59 | static int quic_supported_version[] = { |
| 60 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 61 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 62 | |
| 63 | /* placeholder, do not add entry after this */ |
| 64 | 0x0 |
| 65 | }; |
| 66 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 67 | /* This is the values of some QUIC transport parameters when absent. |
| 68 | * Should be used to initialize any transport parameters (local or remote) |
| 69 | * before updating them with customized values. |
| 70 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 71 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 72 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 73 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 74 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 75 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* trace source and events */ |
| 79 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 80 | const struct trace_source *src, |
| 81 | const struct ist where, const struct ist func, |
| 82 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 83 | |
| 84 | static const struct trace_event quic_trace_events[] = { |
| 85 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 86 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 87 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 90 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 91 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 92 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 93 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 96 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 97 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 102 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 103 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 104 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 106 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 107 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 108 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 109 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 110 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 111 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 112 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 113 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 114 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 115 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 116 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 117 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 118 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 119 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 120 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 121 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 122 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 123 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 124 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 125 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 126 | { /* end */ } |
| 127 | }; |
| 128 | |
| 129 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 130 | /* arg1 */ { /* already used by the connection */ }, |
| 131 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 132 | /* arg3 */ { }, |
| 133 | /* arg4 */ { } |
| 134 | }; |
| 135 | |
| 136 | static const struct name_desc quic_trace_decoding[] = { |
| 137 | #define QUIC_VERB_CLEAN 1 |
| 138 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 139 | { /* end */ } |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | struct trace_source trace_quic = { |
| 144 | .name = IST("quic"), |
| 145 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 146 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 147 | .default_cb = quic_trace, |
| 148 | .known_events = quic_trace_events, |
| 149 | .lockon_args = quic_trace_lockon_args, |
| 150 | .decoding = quic_trace_decoding, |
| 151 | .report_events = ~0, /* report everything by default */ |
| 152 | }; |
| 153 | |
| 154 | #define TRACE_SOURCE &trace_quic |
| 155 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 156 | |
| 157 | static BIO_METHOD *ha_quic_meth; |
| 158 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 159 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 160 | 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] | 161 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 162 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 163 | 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] | 164 | DECLARE_POOL(pool_head_quic_connection_id, |
| 165 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 166 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 167 | 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] | 168 | 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] | 169 | 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 | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 170 | 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] | 171 | 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] | 172 | 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] | 173 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 174 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 175 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 176 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 177 | int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 178 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 179 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 180 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 181 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 182 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 183 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 184 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 185 | /* Only for debug purpose */ |
| 186 | struct enc_debug_info { |
| 187 | unsigned char *payload; |
| 188 | size_t payload_len; |
| 189 | unsigned char *aad; |
| 190 | size_t aad_len; |
| 191 | uint64_t pn; |
| 192 | }; |
| 193 | |
| 194 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 195 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 196 | unsigned char *payload, size_t payload_len, |
| 197 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 198 | { |
| 199 | edi->payload = payload; |
| 200 | edi->payload_len = payload_len; |
| 201 | edi->aad = aad; |
| 202 | edi->aad_len = aad_len; |
| 203 | edi->pn = pn; |
| 204 | } |
| 205 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 206 | /* Trace callback for QUIC. |
| 207 | * These traces always expect that arg1, if non-null, is of type connection. |
| 208 | */ |
| 209 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 210 | const struct ist where, const struct ist func, |
| 211 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 212 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 213 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 215 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 216 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 218 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 219 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 220 | chunk_appendf(&trace_buf, "\n odcid"); |
| 221 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 222 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 223 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 224 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 225 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 229 | const enum ssl_encryption_level_t *level = a2; |
| 230 | const size_t *len = a3; |
| 231 | |
| 232 | if (level) { |
| 233 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 234 | |
| 235 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 236 | } |
| 237 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 238 | 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] | 239 | } |
| 240 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 241 | /* Initial read & write secrets. */ |
| 242 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 243 | const unsigned char *rx_sec = a2; |
| 244 | const unsigned char *tx_sec = a3; |
| 245 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 246 | tls_ctx = &qc->els[level].tls_ctx; |
| 247 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 248 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 249 | if (rx_sec) |
| 250 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 251 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 252 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 253 | if (tx_sec) |
| 254 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 255 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 259 | const enum ssl_encryption_level_t *level = a2; |
| 260 | const unsigned char *secret = a3; |
| 261 | const size_t *secret_len = a4; |
| 262 | |
| 263 | if (level) { |
| 264 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 265 | |
| 266 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 267 | if (secret && secret_len) |
| 268 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 269 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 270 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 271 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 276 | const enum ssl_encryption_level_t *level = a2; |
| 277 | const unsigned char *secret = a3; |
| 278 | const size_t *secret_len = a4; |
| 279 | |
| 280 | if (level) { |
| 281 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 282 | |
| 283 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 284 | if (secret && secret_len) |
| 285 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 286 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 287 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 288 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 292 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 293 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 294 | const struct list *l = a2; |
| 295 | |
| 296 | if (l) { |
| 297 | const struct quic_frame *frm; |
| 298 | list_for_each_entry(frm, l, list) |
| 299 | chunk_frm_appendf(&trace_buf, frm); |
| 300 | } |
| 301 | } |
| 302 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 303 | 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] | 304 | const struct quic_tx_packet *pkt = a2; |
| 305 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 306 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 307 | |
| 308 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 309 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 310 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 311 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 312 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 313 | (unsigned long long)qc->path->cwnd, |
| 314 | (unsigned long long)qc->path->prep_in_flight, |
| 315 | (unsigned long long)qc->path->in_flight, |
| 316 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 317 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 318 | } |
| 319 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 320 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 321 | if (pkt->pn_node.key != (uint64_t)-1) |
| 322 | 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] | 323 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 324 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 325 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 326 | (unsigned long long)qc->rx.bytes, |
| 327 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (room) { |
| 331 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 332 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 333 | (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] | 334 | } |
| 335 | } |
| 336 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 337 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 338 | const enum quic_handshake_state *state = a2; |
| 339 | const int *err = a3; |
| 340 | |
| 341 | if (state) |
| 342 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 343 | if (err) |
| 344 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 345 | } |
| 346 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 347 | 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] | 348 | const struct quic_rx_packet *pkt = a2; |
| 349 | const unsigned long *pktlen = a3; |
| 350 | const SSL *ssl = a4; |
| 351 | |
| 352 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 353 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 354 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 355 | chunk_appendf(&trace_buf, " kp=%d", |
| 356 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 357 | chunk_appendf(&trace_buf, " el=%c", |
| 358 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 359 | if (pkt->pnl) |
| 360 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 361 | (unsigned long long)pkt->pn); |
| 362 | if (pkt->token_len) |
| 363 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 364 | (unsigned long long)pkt->token_len); |
| 365 | if (pkt->aad_len) |
| 366 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 367 | (unsigned long long)pkt->aad_len); |
| 368 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 369 | pkt->flags, (unsigned long long)pkt->len); |
| 370 | } |
| 371 | if (pktlen) |
| 372 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 373 | if (ssl) { |
| 374 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 375 | chunk_appendf(&trace_buf, " el=%c", |
| 376 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 381 | const struct quic_rx_packet *pkt = a2; |
| 382 | const struct quic_rx_crypto_frm *cf = a3; |
| 383 | const SSL *ssl = a4; |
| 384 | |
| 385 | if (pkt) |
| 386 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 387 | quic_packet_type_enc_level_char(pkt->type), |
| 388 | (unsigned long long)pkt->pn); |
| 389 | if (cf) |
| 390 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 391 | (unsigned long long)cf->offset_node.key, |
| 392 | (unsigned long long)cf->len); |
| 393 | if (ssl) { |
| 394 | 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] | 395 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 396 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 397 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 398 | |
| 399 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 400 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 404 | const struct quic_frame *frm = a2; |
| 405 | |
| 406 | if (frm) |
| 407 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 408 | } |
| 409 | |
| 410 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 411 | const struct quic_enc_level *qel = a2; |
| 412 | |
| 413 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 414 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 415 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 416 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 417 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 418 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 419 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 420 | (unsigned long long)qc->path->cwnd, |
| 421 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 422 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 423 | (unsigned long long)pktns->tx.in_flight, |
| 424 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 428 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 429 | const struct enc_debug_info *edi = a2; |
| 430 | |
| 431 | if (edi) |
| 432 | chunk_appendf(&trace_buf, |
| 433 | " payload=@%p payload_len=%llu" |
| 434 | " aad=@%p aad_len=%llu pn=%llu", |
| 435 | edi->payload, (unsigned long long)edi->payload_len, |
| 436 | edi->aad, (unsigned long long)edi->aad_len, |
| 437 | (unsigned long long)edi->pn); |
| 438 | } |
| 439 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 440 | if (mask & QUIC_EV_CONN_RMHP) { |
| 441 | const struct quic_rx_packet *pkt = a2; |
| 442 | |
| 443 | if (pkt) { |
| 444 | const int *ret = a3; |
| 445 | |
| 446 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 447 | if (ret && *ret) |
| 448 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 449 | pkt->pnl, (unsigned long long)pkt->pn); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 454 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 455 | const unsigned long *val1 = a3; |
| 456 | const unsigned long *val2 = a4; |
| 457 | |
| 458 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 459 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 460 | if (val1) |
| 461 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 462 | if (val2) |
| 463 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 464 | } |
| 465 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 466 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 467 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 468 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 469 | |
| 470 | if (s) |
| 471 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 472 | if (stream) |
| 473 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 476 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 477 | const unsigned int *rtt_sample = a2; |
| 478 | const unsigned int *ack_delay = a3; |
| 479 | const struct quic_loss *ql = a4; |
| 480 | |
| 481 | if (rtt_sample) |
| 482 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 483 | if (ack_delay) |
| 484 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 485 | if (ql) |
| 486 | chunk_appendf(&trace_buf, |
| 487 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 488 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 489 | } |
| 490 | if (mask & QUIC_EV_CONN_CC) { |
| 491 | const struct quic_cc_event *ev = a2; |
| 492 | const struct quic_cc *cc = a3; |
| 493 | |
| 494 | if (a2) |
| 495 | quic_cc_event_trace(&trace_buf, ev); |
| 496 | if (a3) |
| 497 | quic_cc_state_trace(&trace_buf, cc); |
| 498 | } |
| 499 | |
| 500 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 501 | const struct quic_pktns *pktns = a2; |
| 502 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 522 | const struct quic_pktns *pktns = a2; |
| 523 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 524 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 525 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 526 | if (ifae_pkts) |
| 527 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 528 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 529 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 530 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 531 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 532 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 533 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 534 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 535 | if (pktns->tx.in_flight) |
| 536 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 537 | if (pktns->tx.loss_time) |
| 538 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 539 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 540 | } |
| 541 | if (mask & QUIC_EV_CONN_SPTO) { |
| 542 | if (pktns->tx.time_of_last_eliciting) |
| 543 | chunk_appendf(&trace_buf, " tole=%dms", |
| 544 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 545 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 546 | 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] | 547 | } |
| 548 | } |
| 549 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 550 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 551 | chunk_appendf(&trace_buf, |
| 552 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 557 | const struct quic_tx_packet *pkt = a2; |
| 558 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 559 | chunk_appendf(&trace_buf, " err=%u cwnd=%llu ppif=%llu pif=%llu", qc->sendto_err, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 560 | (unsigned long long)qc->path->cwnd, |
| 561 | (unsigned long long)qc->path->prep_in_flight, |
| 562 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 563 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 564 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 565 | (unsigned long)pkt->pn_node.key, |
| 566 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 567 | 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] | 568 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 569 | } |
| 570 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 571 | |
| 572 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 573 | const uint8_t *alert = a2; |
| 574 | const enum ssl_encryption_level_t *level = a3; |
| 575 | |
| 576 | if (alert) |
| 577 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 578 | if (level) |
| 579 | chunk_appendf(&trace_buf, " el=%c", |
| 580 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 581 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 582 | |
| 583 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 584 | const size_t *sz1 = a2; |
| 585 | const size_t *sz2 = a3; |
| 586 | const size_t *sz3 = a4; |
| 587 | |
| 588 | if (sz1) |
| 589 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 590 | if (sz2) |
| 591 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 592 | if (sz3) |
| 593 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 594 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 595 | |
| 596 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 597 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 598 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 599 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 600 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 601 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 602 | } |
| 603 | if (mask & QUIC_EV_CONN_LPKT) { |
| 604 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 605 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 606 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 607 | if (pkt) { |
| 608 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 609 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 610 | if (pkt->pn_node.key != (uint64_t)-1) |
| 611 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 612 | } |
| 613 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 614 | if (len) |
| 615 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 618 | if (mask & QUIC_EV_STATELESS_RST) { |
| 619 | const struct quic_cid *cid = a2; |
| 620 | |
| 621 | if (cid) |
| 622 | quic_cid_dump(&trace_buf, cid); |
| 623 | } |
| 624 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 628 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 629 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 630 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 631 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 632 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 633 | return 1; |
| 634 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 635 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 636 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 637 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 638 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 639 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 640 | return 1; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 646 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 647 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 648 | static inline void qc_set_timer(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 649 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 650 | struct quic_pktns *pktns; |
| 651 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 652 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 653 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 654 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 655 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 656 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 657 | pktns = quic_loss_pktns(qc); |
| 658 | if (tick_isset(pktns->tx.loss_time)) { |
| 659 | qc->timer = pktns->tx.loss_time; |
| 660 | goto out; |
| 661 | } |
| 662 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 663 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 664 | * cancelled for a server which reached the anti-amplification limit. |
| 665 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 666 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 667 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 668 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 669 | qc->timer = TICK_ETERNITY; |
| 670 | goto out; |
| 671 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 672 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 673 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 674 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 675 | /* Timer cancellation. */ |
| 676 | qc->timer = TICK_ETERNITY; |
| 677 | goto out; |
| 678 | } |
| 679 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 680 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 681 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 682 | if (tick_isset(pto)) |
| 683 | qc->timer = pto; |
| 684 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 685 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 686 | if (tick_is_expired(qc->timer, now_ms)) { |
| 687 | TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 688 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 689 | } |
| 690 | else { |
| 691 | TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 692 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 693 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 694 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 695 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 698 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 699 | * connection. |
| 700 | * Return 1 if succeeded, 0 if not. |
| 701 | */ |
| 702 | static int quic_tls_key_update(struct quic_conn *qc) |
| 703 | { |
| 704 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 705 | struct quic_tls_secrets *rx, *tx; |
| 706 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 707 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 708 | |
| 709 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 710 | rx = &tls_ctx->rx; |
| 711 | tx = &tls_ctx->tx; |
| 712 | nxt_rx = &qc->ku.nxt_rx; |
| 713 | nxt_tx = &qc->ku.nxt_tx; |
| 714 | |
| 715 | /* Prepare new RX secrets */ |
| 716 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 717 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 718 | TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 723 | nxt_rx->key, nxt_rx->keylen, |
| 724 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 725 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 726 | TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* Prepare new TX secrets */ |
| 731 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 732 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 733 | TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 738 | nxt_tx->key, nxt_tx->keylen, |
| 739 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 740 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 741 | TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 745 | if (nxt_rx->ctx) { |
| 746 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 747 | nxt_rx->ctx = NULL; |
| 748 | } |
| 749 | |
| 750 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 751 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | if (nxt_tx->ctx) { |
| 756 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 757 | nxt_tx->ctx = NULL; |
| 758 | } |
| 759 | |
| 760 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 761 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 762 | return 0; |
| 763 | } |
| 764 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 765 | return 1; |
| 766 | } |
| 767 | |
| 768 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 769 | * Must be used after having updated them. |
| 770 | * Always succeeds. |
| 771 | */ |
| 772 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 773 | { |
| 774 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 775 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 776 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 777 | |
| 778 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 779 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 780 | curr_secret = tls_ctx->rx.secret; |
| 781 | curr_iv = tls_ctx->rx.iv; |
| 782 | curr_key = tls_ctx->rx.key; |
| 783 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 784 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 785 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 786 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 787 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 788 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 789 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 790 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 791 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 792 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 793 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 794 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 795 | qc->ku.prv_rx.secret = curr_secret; |
| 796 | qc->ku.prv_rx.iv = curr_iv; |
| 797 | qc->ku.prv_rx.key = curr_key; |
| 798 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 799 | |
| 800 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 801 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 802 | curr_secret = tls_ctx->tx.secret; |
| 803 | curr_iv = tls_ctx->tx.iv; |
| 804 | curr_key = tls_ctx->tx.key; |
| 805 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 806 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 807 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 808 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 809 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 810 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 811 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 812 | qc->ku.nxt_tx.secret = curr_secret; |
| 813 | qc->ku.nxt_tx.iv = curr_iv; |
| 814 | qc->ku.nxt_tx.key = curr_key; |
| 815 | } |
| 816 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 817 | #ifndef OPENSSL_IS_BORINGSSL |
| 818 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 819 | const uint8_t *read_secret, |
| 820 | const uint8_t *write_secret, size_t secret_len) |
| 821 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 822 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 823 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 824 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 825 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 826 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 827 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 828 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 829 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 830 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 831 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 832 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 833 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 834 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 835 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 836 | goto err; |
| 837 | } |
| 838 | |
| 839 | rx = &tls_ctx->rx; |
| 840 | tx = &tls_ctx->tx; |
| 841 | |
| 842 | rx->aead = tx->aead = tls_aead(cipher); |
| 843 | rx->md = tx->md = tls_md(cipher); |
| 844 | rx->hp = tx->hp = tls_hp(cipher); |
| 845 | |
| 846 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 847 | 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] | 848 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 849 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 850 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 853 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 854 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 855 | goto err; |
| 856 | } |
| 857 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 858 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 859 | * listener. Note that a listener derives only RX secrets for this |
| 860 | * level. |
| 861 | */ |
| 862 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 863 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 864 | |
| 865 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 866 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 867 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 868 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 869 | 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] | 870 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 871 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 872 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 875 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 876 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 877 | goto err; |
| 878 | } |
| 879 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 880 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 881 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 882 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 883 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 884 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 885 | /* These secrets must be stored only for Application encryption level */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 886 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 887 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 888 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 889 | goto err; |
| 890 | } |
| 891 | |
| 892 | memcpy(rx->secret, read_secret, secret_len); |
| 893 | rx->secretlen = secret_len; |
| 894 | memcpy(tx->secret, write_secret, secret_len); |
| 895 | tx->secretlen = secret_len; |
| 896 | /* Initialize all the secret keys lengths */ |
| 897 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 898 | /* Prepare the next key update */ |
| 899 | if (!quic_tls_key_update(qc)) |
| 900 | goto err; |
| 901 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 902 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 903 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 904 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 905 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 906 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 907 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 908 | |
| 909 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 910 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 911 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 912 | } |
| 913 | #else |
| 914 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 915 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 916 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 917 | */ |
| 918 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 919 | const SSL_CIPHER *cipher, |
| 920 | const uint8_t *secret, size_t secret_len) |
| 921 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 922 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 923 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 924 | &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 925 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 926 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 927 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 928 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 929 | goto out; |
| 930 | } |
| 931 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 932 | tls_ctx->rx.aead = tls_aead(cipher); |
| 933 | tls_ctx->rx.md = tls_md(cipher); |
| 934 | tls_ctx->rx.hp = tls_hp(cipher); |
| 935 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 936 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 937 | goto err; |
| 938 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 939 | 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] | 940 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 941 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 942 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 943 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 944 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 945 | goto err; |
| 946 | } |
| 947 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 948 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 949 | const unsigned char *buf; |
| 950 | size_t buflen; |
| 951 | |
| 952 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 953 | if (!buflen) |
| 954 | goto err; |
| 955 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 956 | if (!quic_transport_params_store(qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 957 | goto err; |
| 958 | } |
| 959 | |
| 960 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 961 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 962 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 963 | |
| 964 | return 1; |
| 965 | |
| 966 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 967 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 972 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 973 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 974 | */ |
| 975 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 976 | const SSL_CIPHER *cipher, |
| 977 | const uint8_t *secret, size_t secret_len) |
| 978 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 979 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 980 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 981 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 982 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 983 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 984 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 985 | goto out; |
| 986 | } |
| 987 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 988 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 989 | goto err; |
| 990 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 991 | tls_ctx->tx.aead = tls_aead(cipher); |
| 992 | tls_ctx->tx.md = tls_md(cipher); |
| 993 | tls_ctx->tx.hp = tls_hp(cipher); |
| 994 | |
| 995 | 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] | 996 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 997 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 998 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 999 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1000 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1001 | goto err; |
| 1002 | } |
| 1003 | |
| 1004 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1005 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1006 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1007 | return 1; |
| 1008 | |
| 1009 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1010 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | #endif |
| 1014 | |
| 1015 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1016 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1017 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1018 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 1019 | * store all the data. |
| 1020 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1021 | */ |
| 1022 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 1023 | const unsigned char *data, size_t len) |
| 1024 | { |
| 1025 | struct quic_crypto_buf **qcb; |
| 1026 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1027 | size_t cf_offset, cf_len, *nb_buf; |
| 1028 | unsigned char *pos; |
| 1029 | |
| 1030 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1031 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1032 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1033 | cf_len = len; |
| 1034 | |
| 1035 | while (len) { |
| 1036 | size_t to_copy, room; |
| 1037 | |
| 1038 | pos = (*qcb)->data + (*qcb)->sz; |
| 1039 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1040 | to_copy = len > room ? room : len; |
| 1041 | if (to_copy) { |
| 1042 | memcpy(pos, data, to_copy); |
| 1043 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1044 | qel->tx.crypto.sz += to_copy; |
| 1045 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1046 | len -= to_copy; |
| 1047 | data += to_copy; |
| 1048 | } |
| 1049 | else { |
| 1050 | struct quic_crypto_buf **tmp; |
| 1051 | |
| 1052 | tmp = realloc(qel->tx.crypto.bufs, |
| 1053 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1054 | if (tmp) { |
| 1055 | qel->tx.crypto.bufs = tmp; |
| 1056 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1057 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1058 | if (!*qcb) |
| 1059 | return 0; |
| 1060 | |
| 1061 | (*qcb)->sz = 0; |
| 1062 | ++*nb_buf; |
| 1063 | } |
| 1064 | else { |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1071 | * have been buffered. |
| 1072 | */ |
| 1073 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1074 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1075 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1076 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1077 | /* There is at most one CRYPTO frame in this packet number |
| 1078 | * space. Let's look for it. |
| 1079 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1080 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1081 | if (frm->type != QUIC_FT_CRYPTO) |
| 1082 | continue; |
| 1083 | |
| 1084 | /* Found */ |
| 1085 | found = frm; |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | if (found) { |
| 1090 | found->crypto.len += cf_len; |
| 1091 | } |
| 1092 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1093 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1094 | if (!frm) |
| 1095 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1096 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1097 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1098 | frm->type = QUIC_FT_CRYPTO; |
| 1099 | frm->crypto.offset = cf_offset; |
| 1100 | frm->crypto.len = cf_len; |
| 1101 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1102 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1103 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | return len == 0; |
| 1107 | } |
| 1108 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1109 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1110 | * activity for <qc> will be interrupted. |
| 1111 | */ |
| 1112 | void quic_set_connection_close(struct quic_conn *qc, int err) |
| 1113 | { |
| 1114 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1115 | return; |
| 1116 | |
| 1117 | qc->err_code = err; |
| 1118 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1119 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1120 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1121 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1122 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1123 | { |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 1124 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1125 | quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert); |
| 1126 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1127 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1130 | /* Set the application for <qc> QUIC connection. |
| 1131 | * Return 1 if succeeded, 0 if not. |
| 1132 | */ |
| 1133 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1134 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1135 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1136 | qc->app_ops = &h3_ops; |
| 1137 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1138 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1139 | else |
| 1140 | return 0; |
| 1141 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1142 | return 1; |
| 1143 | } |
| 1144 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1145 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1146 | * wants to provide the QUIC layer with CRYPTO data. |
| 1147 | * Returns 1 if succeeded, 0 if not. |
| 1148 | */ |
| 1149 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1150 | const uint8_t *data, size_t len) |
| 1151 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1152 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1153 | enum quic_tls_enc_level tel; |
| 1154 | struct quic_enc_level *qel; |
| 1155 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1156 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1157 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1158 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1159 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1160 | goto out; |
| 1161 | } |
| 1162 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1163 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1164 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1165 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1166 | goto err; |
| 1167 | } |
| 1168 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1169 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1170 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1171 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1172 | goto err; |
| 1173 | } |
| 1174 | |
| 1175 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1176 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1177 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1178 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1179 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1180 | return 1; |
| 1181 | |
| 1182 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1183 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | int ha_quic_flush_flight(SSL *ssl) |
| 1188 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1189 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1190 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1191 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1192 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1193 | |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
| 1197 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1198 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1199 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1200 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1201 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1202 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1203 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1204 | return 1; |
| 1205 | } |
| 1206 | |
| 1207 | /* QUIC TLS methods */ |
| 1208 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1209 | #ifdef OPENSSL_IS_BORINGSSL |
| 1210 | .set_read_secret = ha_set_rsec, |
| 1211 | .set_write_secret = ha_set_wsec, |
| 1212 | #else |
| 1213 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1214 | #endif |
| 1215 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1216 | .flush_flight = ha_quic_flush_flight, |
| 1217 | .send_alert = ha_quic_send_alert, |
| 1218 | }; |
| 1219 | |
| 1220 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1221 | * Returns an error count. |
| 1222 | */ |
| 1223 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1224 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1225 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1226 | int cfgerr = 0; |
| 1227 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1228 | long options = |
| 1229 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1230 | SSL_OP_SINGLE_ECDH_USE | |
| 1231 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1232 | SSL_CTX *ctx; |
| 1233 | |
| 1234 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1235 | bind_conf->initial_ctx = ctx; |
| 1236 | |
| 1237 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1238 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1239 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1240 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1241 | |
| 1242 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1243 | #ifdef OPENSSL_IS_BORINGSSL |
| 1244 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1245 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1246 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1247 | if (bind_conf->ssl_conf.early_data) { |
| 1248 | 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] | 1249 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1250 | } |
| 1251 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1252 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1253 | #else |
| 1254 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1255 | #endif |
| 1256 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1257 | #endif |
| 1258 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1259 | |
| 1260 | return cfgerr; |
| 1261 | } |
| 1262 | |
| 1263 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1264 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1265 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1266 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1267 | */ |
| 1268 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1269 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1270 | { |
| 1271 | uint64_t expected_pn = largest_pn + 1; |
| 1272 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1273 | uint64_t pn_hwin = pn_win / 2; |
| 1274 | uint64_t pn_mask = pn_win - 1; |
| 1275 | uint64_t candidate_pn; |
| 1276 | |
| 1277 | |
| 1278 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1279 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1280 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1281 | candidate_pn + pn_hwin <= expected_pn) |
| 1282 | return candidate_pn + pn_win; |
| 1283 | |
| 1284 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1285 | return candidate_pn - pn_win; |
| 1286 | |
| 1287 | return candidate_pn; |
| 1288 | } |
| 1289 | |
| 1290 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1291 | * cryptographic context. |
| 1292 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1293 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1294 | * <end> points to one byte past the end of this packet. |
| 1295 | * Returns 1 if succeeded, 0 if not. |
| 1296 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1297 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1298 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1299 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1300 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1301 | { |
| 1302 | int ret, outlen, i, pnlen; |
| 1303 | uint64_t packet_number; |
| 1304 | uint32_t truncated_pn = 0; |
| 1305 | unsigned char mask[5] = {0}; |
| 1306 | unsigned char *sample; |
| 1307 | EVP_CIPHER_CTX *cctx; |
| 1308 | unsigned char *hp_key; |
| 1309 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1310 | /* Check there is enough data in this packet. */ |
| 1311 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1312 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | cctx = EVP_CIPHER_CTX_new(); |
| 1317 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1318 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | ret = 0; |
| 1323 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1324 | |
| 1325 | hp_key = tls_ctx->rx.hp_key; |
| 1326 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1327 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1328 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1329 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1330 | goto out; |
| 1331 | } |
| 1332 | |
| 1333 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1334 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1335 | for (i = 0; i < pnlen; i++) { |
| 1336 | pn[i] ^= mask[i + 1]; |
| 1337 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1338 | } |
| 1339 | |
| 1340 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1341 | /* Store remaining information for this unprotected header */ |
| 1342 | pkt->pn = packet_number; |
| 1343 | pkt->pnl = pnlen; |
| 1344 | |
| 1345 | ret = 1; |
| 1346 | |
| 1347 | out: |
| 1348 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1349 | |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
| 1353 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1354 | * address, with <payload_len> as payload length, <aad> as address of |
| 1355 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1356 | * context. |
| 1357 | * Returns 1 if succeeded, 0 if not. |
| 1358 | */ |
| 1359 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1360 | unsigned char *aad, size_t aad_len, uint64_t pn, |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1361 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1362 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1363 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1364 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1365 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1366 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1367 | |
| 1368 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1369 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1370 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1374 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1375 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1376 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1380 | |
| 1381 | err: |
| 1382 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1383 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1384 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1388 | * Returns 1 if succeeded, 0 if not. |
| 1389 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1390 | 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] | 1391 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1392 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1393 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1394 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1395 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1396 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1397 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1398 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1399 | |
| 1400 | kp_changed = 0; |
| 1401 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1402 | /* The two tested bits are not at the same position, |
| 1403 | * this is why they are first both inversed. |
| 1404 | */ |
| 1405 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1406 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1407 | /* The lowest packet number of a previous key phase |
| 1408 | * cannot be null if it really stores previous key phase |
| 1409 | * secrets. |
| 1410 | */ |
| 1411 | if (!pkt->qc->ku.prv_rx.pn) |
| 1412 | return 0; |
| 1413 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1414 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1415 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1416 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1417 | } |
| 1418 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1419 | /* Next key phase */ |
| 1420 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1421 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1422 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1423 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1424 | } |
| 1425 | } |
| 1426 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1427 | |
| 1428 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1429 | return 0; |
| 1430 | |
| 1431 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1432 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1433 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1434 | if (!ret) |
| 1435 | return 0; |
| 1436 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1437 | /* Update the keys only if the packet decryption succeeded. */ |
| 1438 | if (kp_changed) { |
| 1439 | quic_tls_rotate_keys(pkt->qc); |
| 1440 | /* Toggle the Key Phase bit */ |
| 1441 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1442 | /* Store the lowest packet number received for the current key phase */ |
| 1443 | tls_ctx->rx.pn = pkt->pn; |
| 1444 | /* Prepare the next key update */ |
| 1445 | if (!quic_tls_key_update(pkt->qc)) |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1449 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1450 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1451 | |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1455 | |
| 1456 | /* Remove references to <frm> frame */ |
| 1457 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1458 | { |
| 1459 | uint64_t pn; |
| 1460 | struct quic_frame *f, *tmp; |
| 1461 | |
| 1462 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1463 | pn = f->pkt->pn_node.key; |
| 1464 | f->origin = NULL; |
| 1465 | LIST_DELETE(&f->ref); |
| 1466 | TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1467 | } |
| 1468 | } |
| 1469 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1470 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1471 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1472 | { |
| 1473 | uint64_t pn; |
| 1474 | struct quic_frame *origin, *f, *tmp; |
| 1475 | |
| 1476 | /* Identify this frame: a frame copy or one of its copies */ |
| 1477 | origin = frm->origin ? frm->origin : frm; |
| 1478 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1479 | * possibly a copy of <origin> |
| 1480 | */ |
| 1481 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1482 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1483 | * not release the packets (releasing the frames) at this time as |
| 1484 | * they are possibly also to be acknowledged alongside the |
| 1485 | * the current one. |
| 1486 | */ |
| 1487 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1488 | pn = f->pkt->pn_node.key; |
| 1489 | TRACE_PROTO("mark frame as acked from packet", |
| 1490 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1491 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1492 | f->origin = NULL; |
| 1493 | LIST_DELETE(&f->ref); |
| 1494 | } |
| 1495 | LIST_DELETE(&frm->list); |
| 1496 | pn = frm->pkt->pn_node.key; |
| 1497 | quic_tx_packet_refdec(frm->pkt); |
| 1498 | TRACE_PROTO("freeing frame from packet", |
| 1499 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1500 | pool_free(pool_head_quic_frame, frm); |
| 1501 | } |
| 1502 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1503 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1504 | * |
| 1505 | * Returns 1 if at least one frame was removed else 0. |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1506 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1507 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1508 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1509 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1510 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1511 | struct eb64_node *frm_node; |
| 1512 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1513 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1514 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1515 | while (frm_node) { |
| 1516 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1517 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1518 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1519 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1520 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1521 | offset = strm->offset.key; |
| 1522 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1523 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1524 | if (offset > stream->ack_offset) |
| 1525 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1526 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1527 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1528 | /* cf. next comment : frame may be freed at this stage. */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1529 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1530 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1531 | ret = 1; |
| 1532 | } |
| 1533 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1534 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1535 | * has been freed. with the stream frames tree. Nothing to do |
| 1536 | * anymore in here. |
| 1537 | */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1538 | if (!stream) |
| 1539 | return 1; |
| 1540 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1541 | frm_node = eb64_next(frm_node); |
| 1542 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1543 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1544 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1545 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1546 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1547 | |
| 1548 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1551 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1552 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1553 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1554 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1555 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1556 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1557 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1558 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1559 | switch (frm->type) { |
| 1560 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1561 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1562 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1563 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1564 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1565 | const size_t offset = strm_frm->offset.key; |
| 1566 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1567 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1568 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1569 | * might be freed at this stage. Use the id to do a proper |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1570 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1571 | * |
| 1572 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1573 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1574 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1575 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1576 | if (!node) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1577 | TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1578 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1579 | /* early return */ |
| 1580 | return; |
| 1581 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1582 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1583 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1584 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1585 | if (offset <= stream->ack_offset) { |
| 1586 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1587 | stream_acked = 1; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1588 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1589 | qc, strm_frm, stream); |
| 1590 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1591 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1592 | if (!stream) { |
| 1593 | /* no need to continue if stream freed. */ |
| 1594 | TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1595 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1596 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1599 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1600 | qc, strm_frm, stream); |
| 1601 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1602 | } |
| 1603 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1604 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1605 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1606 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1607 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1608 | } |
| 1609 | break; |
| 1610 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1611 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1612 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1613 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1614 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1615 | struct qcc *qcc = qc->qcc; |
| 1616 | |
| 1617 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1618 | tasklet_wakeup(qcc->subs->tasklet); |
| 1619 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1620 | if (!qcc->subs->events) |
| 1621 | qcc->subs = NULL; |
| 1622 | } |
| 1623 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1627 | * deallocating them, and their TX frames. |
| 1628 | * Returns the last node reached to be used for the next range. |
| 1629 | * May be NULL if <largest> node could not be found. |
| 1630 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1631 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1632 | struct eb_root *pkts, |
| 1633 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1634 | struct list *newly_acked_pkts, |
| 1635 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1636 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1637 | { |
| 1638 | struct eb64_node *node; |
| 1639 | struct quic_tx_packet *pkt; |
| 1640 | |
| 1641 | if (largest_node) |
| 1642 | node = largest_node; |
| 1643 | else { |
| 1644 | node = eb64_lookup(pkts, largest); |
| 1645 | while (!node && largest > smallest) { |
| 1646 | node = eb64_lookup(pkts, --largest); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1651 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1652 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1653 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1654 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1655 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1656 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1657 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1658 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1659 | node = eb64_prev(node); |
| 1660 | eb64_delete(&pkt->pn_node); |
| 1661 | } |
| 1662 | |
| 1663 | return node; |
| 1664 | } |
| 1665 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1666 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1667 | * same order they have been sent into <pktns_frm_list>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1668 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1669 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1670 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1671 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1672 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1673 | struct quic_frame *frm, *frmbak; |
| 1674 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1675 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1676 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1677 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1678 | /* Only for debug */ |
| 1679 | uint64_t pn; |
| 1680 | |
| 1681 | /* First remove this frame from the packet it was attached to */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1682 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1683 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1684 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1685 | /* At this time, this frame is not freed but removed from its packet */ |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1686 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1687 | /* Remove any reference to this frame */ |
| 1688 | qc_frm_unref(qc, frm); |
| 1689 | switch (frm->type) { |
| 1690 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1691 | { |
| 1692 | struct quic_stream *strm_frm = &frm->stream; |
| 1693 | struct eb64_node *node = NULL; |
| 1694 | struct qc_stream_desc *stream_desc; |
| 1695 | |
| 1696 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1697 | if (!node) { |
| 1698 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 1699 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1700 | qc, frm, &pn); |
| 1701 | pool_free(pool_head_quic_frame, frm); |
| 1702 | continue; |
| 1703 | } |
| 1704 | |
| 1705 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1706 | /* Do not resend this frame if in the "already acked range" */ |
| 1707 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1708 | TRACE_PROTO("ignored frame in already acked range", |
| 1709 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1710 | continue; |
| 1711 | } |
| 1712 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1713 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1714 | TRACE_PROTO("updated partially acked frame", |
| 1715 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1716 | } |
| 1717 | |
| 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | default: |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | /* Do not resend probing packet with old data */ |
| 1726 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1727 | TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1728 | qc, frm, &pn); |
| 1729 | if (frm->origin) |
| 1730 | LIST_DELETE(&frm->ref); |
| 1731 | pool_free(pool_head_quic_frame, frm); |
| 1732 | continue; |
| 1733 | } |
| 1734 | |
| 1735 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1736 | TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1737 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1738 | qc, frm, &pn); |
| 1739 | pool_free(pool_head_quic_frame, frm); |
| 1740 | } |
| 1741 | else { |
| 1742 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1743 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1744 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1745 | * will be performed to check the stream is not consumed or released. |
| 1746 | */ |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1747 | frm->flags = QUIC_FL_TX_FRAME_LOST; |
| 1748 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1749 | LIST_APPEND(&tmp, &frm->list); |
| 1750 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1753 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1754 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1755 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1756 | /* Free <pkt> TX packet and its attached frames. |
| 1757 | * This is the responsability of the caller to remove this packet of |
| 1758 | * any data structure it was possibly attached to. |
| 1759 | */ |
| 1760 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1761 | { |
| 1762 | struct quic_frame *frm, *frmbak; |
| 1763 | |
| 1764 | if (!pkt) |
| 1765 | return; |
| 1766 | |
| 1767 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1768 | LIST_DELETE(&frm->list); |
| 1769 | pool_free(pool_head_quic_frame, frm); |
| 1770 | } |
| 1771 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1772 | } |
| 1773 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1774 | /* Free the TX packets of <pkts> list */ |
| 1775 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1776 | { |
| 1777 | struct quic_tx_packet *pkt, *tmp; |
| 1778 | |
| 1779 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1780 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1781 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1782 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1786 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1787 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1788 | * updating the range which contains <largest_acked_pn>. |
| 1789 | * Never fails. |
| 1790 | */ |
| 1791 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1792 | int64_t largest_acked_pn) |
| 1793 | { |
| 1794 | struct eb64_node *ar, *next_ar; |
| 1795 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1796 | |
| 1797 | ar = eb64_first(&arngs->root); |
| 1798 | while (ar) { |
| 1799 | struct quic_arng_node *ar_node; |
| 1800 | |
| 1801 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1802 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1803 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1804 | break; |
| 1805 | |
| 1806 | if (largest_acked_pn < ar_node->last) { |
| 1807 | eb64_delete(ar); |
| 1808 | ar_node->first.key = largest_acked_pn + 1; |
| 1809 | eb64_insert(&arngs->root, ar); |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | eb64_delete(ar); |
| 1814 | pool_free(pool_head_quic_arng, ar_node); |
| 1815 | arngs->sz--; |
| 1816 | ar = next_ar; |
| 1817 | } |
| 1818 | } |
| 1819 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1820 | /* Send a packet ack event nofication for each newly acked packet of |
| 1821 | * <newly_acked_pkts> list and free them. |
| 1822 | * Always succeeds. |
| 1823 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1824 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1825 | struct list *newly_acked_pkts) |
| 1826 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1827 | struct quic_tx_packet *pkt, *tmp; |
| 1828 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1829 | |
| 1830 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1831 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1832 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1833 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1834 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1835 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1836 | /* If this packet contained an ACK frame, proceed to the |
| 1837 | * acknowledging of range of acks from the largest acknowledged |
| 1838 | * packet number which was sent in an ACK frame by this packet. |
| 1839 | */ |
| 1840 | if (pkt->largest_acked_pn != -1) |
| 1841 | qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1842 | ev.ack.acked = pkt->in_flight_len; |
| 1843 | ev.ack.time_sent = pkt->time_sent; |
| 1844 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1845 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1846 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1847 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | } |
| 1851 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1852 | /* Release all the frames attached to <pktns> packet number space */ |
| 1853 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1854 | { |
| 1855 | struct quic_frame *frm, *frmbak; |
| 1856 | |
| 1857 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1858 | LIST_DELETE(&frm->list); |
| 1859 | pool_free(pool_head_quic_frame, frm); |
| 1860 | } |
| 1861 | } |
| 1862 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1863 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1864 | * their TX frames. |
| 1865 | * Send a packet loss event to the congestion controller if |
| 1866 | * in flight packet have been lost. |
| 1867 | * Also frees the packet in <pkts> list. |
| 1868 | * Never fails. |
| 1869 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1870 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1871 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1872 | struct list *pkts, |
| 1873 | uint64_t now_us) |
| 1874 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1875 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1876 | uint64_t lost_bytes; |
| 1877 | |
| 1878 | lost_bytes = 0; |
| 1879 | oldest_lost = newest_lost = NULL; |
| 1880 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1881 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1882 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1883 | lost_bytes += pkt->in_flight_len; |
| 1884 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1885 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1886 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1887 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1888 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1889 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1890 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1891 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1892 | if (!oldest_lost) { |
| 1893 | oldest_lost = newest_lost = pkt; |
| 1894 | } |
| 1895 | else { |
| 1896 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1897 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1898 | newest_lost = pkt; |
| 1899 | } |
| 1900 | } |
| 1901 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1902 | if (newest_lost) { |
| 1903 | /* Sent a congestion event to the controller */ |
| 1904 | struct quic_cc_event ev = { |
| 1905 | .type = QUIC_CC_EVT_LOSS, |
| 1906 | .loss.time_sent = newest_lost->time_sent, |
| 1907 | }; |
| 1908 | |
| 1909 | quic_cc_event(&qc->path->cc, &ev); |
| 1910 | } |
| 1911 | |
| 1912 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1913 | * We must check if we are experiencing a persistent congestion. |
| 1914 | * If this is the case, the congestion controller must re-enter |
| 1915 | * slow start state. |
| 1916 | */ |
| 1917 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1918 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1919 | |
| 1920 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1921 | now_ms, qc->max_ack_delay)) |
| 1922 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1923 | } |
| 1924 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1925 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1926 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1927 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1928 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1932 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1933 | * 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] | 1934 | * 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] | 1935 | * acked ack-eliciting packet. |
| 1936 | * Return 1, if succeeded, 0 if not. |
| 1937 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1938 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1939 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1940 | struct quic_enc_level *qel, |
| 1941 | unsigned int *rtt_sample, |
| 1942 | const unsigned char **pos, const unsigned char *end) |
| 1943 | { |
| 1944 | struct quic_ack *ack = &frm->ack; |
| 1945 | uint64_t smallest, largest; |
| 1946 | struct eb_root *pkts; |
| 1947 | struct eb64_node *largest_node; |
| 1948 | unsigned int time_sent, pkt_flags; |
| 1949 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1950 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1951 | |
| 1952 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1953 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1954 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1955 | goto err; |
| 1956 | } |
| 1957 | |
| 1958 | if (ack->first_ack_range > ack->largest_ack) { |
| 1959 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1960 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1961 | goto err; |
| 1962 | } |
| 1963 | |
| 1964 | largest = ack->largest_ack; |
| 1965 | smallest = largest - ack->first_ack_range; |
| 1966 | pkts = &qel->pktns->tx.pkts; |
| 1967 | pkt_flags = 0; |
| 1968 | largest_node = NULL; |
| 1969 | time_sent = 0; |
| 1970 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1971 | if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1972 | largest_node = eb64_lookup(pkts, largest); |
| 1973 | if (!largest_node) { |
| 1974 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1975 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1976 | } |
| 1977 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1978 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1979 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1980 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1984 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1985 | do { |
| 1986 | uint64_t gap, ack_range; |
| 1987 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1988 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1989 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1990 | if (!ack->ack_range_num--) |
| 1991 | break; |
| 1992 | |
| 1993 | if (!quic_dec_int(&gap, pos, end)) |
| 1994 | goto err; |
| 1995 | |
| 1996 | if (smallest < gap + 2) { |
| 1997 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1998 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1999 | goto err; |
| 2000 | } |
| 2001 | |
| 2002 | largest = smallest - gap - 2; |
| 2003 | if (!quic_dec_int(&ack_range, pos, end)) |
| 2004 | goto err; |
| 2005 | |
| 2006 | if (largest < ack_range) { |
| 2007 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2008 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2009 | goto err; |
| 2010 | } |
| 2011 | |
| 2012 | /* Do not use this node anymore. */ |
| 2013 | largest_node = NULL; |
| 2014 | /* Next range */ |
| 2015 | smallest = largest - ack_range; |
| 2016 | |
| 2017 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2018 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2019 | } while (1); |
| 2020 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2021 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2022 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2023 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2027 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2028 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2029 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2030 | qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2031 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2032 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2033 | if (quic_peer_validated_addr(qc)) |
| 2034 | qc->path->loss.pto_count = 0; |
| 2035 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | |
| 2039 | return 1; |
| 2040 | |
| 2041 | err: |
| 2042 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2043 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
| 2046 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2047 | /* This function gives the detail of the SSL error. It is used only |
| 2048 | * if the debug mode and the verbose mode are activated. It dump all |
| 2049 | * the SSL error until the stack was empty. |
| 2050 | */ |
| 2051 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2052 | { |
| 2053 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2054 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2055 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2056 | unsigned long ret; |
| 2057 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2058 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2059 | ret = ERR_get_error(); |
| 2060 | if (!ret) |
| 2061 | return; |
| 2062 | |
| 2063 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2064 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | } |
| 2068 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2069 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2070 | const char **str, int *len); |
| 2071 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2072 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2073 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2074 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2075 | * Return 1 if succeeded, 0 if not. |
| 2076 | */ |
| 2077 | 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] | 2078 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2079 | const unsigned char *data, size_t len, |
| 2080 | struct quic_rx_packet *pkt, |
| 2081 | struct quic_rx_crypto_frm *cf) |
| 2082 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2083 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2084 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2085 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2086 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2087 | qc = ctx->qc; |
| 2088 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2089 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2090 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2091 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2092 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2093 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2094 | goto err; |
| 2095 | } |
| 2096 | |
| 2097 | el->rx.crypto.offset += len; |
| 2098 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2099 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2100 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2101 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2102 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2103 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2104 | if (ssl_err != 1) { |
| 2105 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2106 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2107 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2108 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2109 | goto out; |
| 2110 | } |
| 2111 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2112 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2113 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2114 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2115 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2116 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2117 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2118 | goto err; |
| 2119 | } |
| 2120 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2121 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | bc964bd | 2022-04-13 16:20:09 +0200 | [diff] [blame] | 2122 | |
| 2123 | /* Check the alpn could be negotiated */ |
| 2124 | if (!qc->app_ops) { |
| 2125 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2126 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2127 | goto err; |
| 2128 | } |
| 2129 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2130 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2131 | /* I/O callback switch */ |
| 2132 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2133 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2134 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2135 | /* The connection is ready to be accepted. */ |
| 2136 | quic_accept_push_qc(qc); |
| 2137 | } |
| 2138 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2139 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2140 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2141 | } else { |
| 2142 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2143 | if (ssl_err != 1) { |
| 2144 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2145 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2146 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2147 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2148 | goto out; |
| 2149 | } |
| 2150 | |
| 2151 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2152 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2153 | goto err; |
| 2154 | } |
| 2155 | |
| 2156 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2157 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2158 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2159 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2160 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2161 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2162 | return 1; |
| 2163 | |
| 2164 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2165 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2166 | return 0; |
| 2167 | } |
| 2168 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2169 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2170 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2171 | * If not, the STREAM frame is stored to be treated again later. |
| 2172 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2173 | * Return 1 if succeeded, 0 if not. |
| 2174 | */ |
| 2175 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2176 | struct quic_stream *strm_frm, |
| 2177 | struct quic_conn *qc) |
| 2178 | { |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2179 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2180 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2181 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2182 | strm_frm->offset.key, strm_frm->fin, |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 2183 | (char *)strm_frm->data); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2184 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 2185 | /* frame rejected - packet must not be acknowledeged */ |
| 2186 | if (ret) |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2187 | return 0; |
| 2188 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2189 | return 1; |
| 2190 | } |
| 2191 | |
| 2192 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2193 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2194 | * If not, the STREAM frame is stored to be treated again later. |
| 2195 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2196 | * Return 1 if succeeded, 0 if not. |
| 2197 | */ |
| 2198 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2199 | struct quic_stream *strm_frm, |
| 2200 | struct quic_conn *qc) |
| 2201 | { |
| 2202 | struct qcs *strm; |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2203 | enum ncb_ret ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2204 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2205 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2206 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2207 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2208 | return 0; |
| 2209 | } |
| 2210 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2211 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2212 | size_t diff; |
| 2213 | |
| 2214 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2215 | TRACE_PROTO("Already received STREAM data", |
| 2216 | QUIC_EV_CONN_PSTRM, qc); |
| 2217 | goto out; |
| 2218 | } |
| 2219 | |
| 2220 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2221 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2222 | strm_frm->offset.key = strm->rx.offset; |
| 2223 | strm_frm->len -= diff; |
| 2224 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2227 | qc_get_ncbuf(strm, &strm->rx.ncbuf); |
| 2228 | if (ncb_is_null(&strm->rx.ncbuf)) |
| 2229 | return 0; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2230 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2231 | ret = ncb_add(&strm->rx.ncbuf, strm_frm->offset.key - strm->rx.offset, |
| 2232 | (char *)strm_frm->data, strm_frm->len, NCB_ADD_COMPARE); |
| 2233 | if (ret != NCB_RET_OK) |
| 2234 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2235 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2236 | /* Inform the application of the arrival of this new stream */ |
| 2237 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
| 2238 | TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2239 | return 0; |
| 2240 | } |
| 2241 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2242 | qcs_notify_recv(strm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2243 | |
| 2244 | out: |
| 2245 | return 1; |
| 2246 | } |
| 2247 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2248 | /* Returns 1 on success or 0 on error. On error, the packet containing the |
| 2249 | * frame must not be acknowledged. |
| 2250 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2251 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2252 | struct quic_stream *strm_frm, |
| 2253 | struct quic_conn *qc) |
| 2254 | { |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2255 | /* RFC9000 13.1. Packet Processing |
| 2256 | * |
| 2257 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2258 | * successfully removed and all frames contained in the packet have |
| 2259 | * been processed. For STREAM frames, this means the data has been |
| 2260 | * enqueued in preparation to be received by the application protocol, |
| 2261 | * but it does not require that data be delivered and consumed. |
| 2262 | */ |
| 2263 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2264 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2265 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2266 | else |
| 2267 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2268 | } |
| 2269 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2270 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2271 | * for <qc> QUIC connection. |
| 2272 | * This is a best effort function which never fails even if no memory could be |
| 2273 | * allocated to duplicate these frames. |
| 2274 | */ |
| 2275 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2276 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2277 | { |
| 2278 | struct quic_frame *frm, *frmbak; |
| 2279 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2280 | |
| 2281 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2282 | struct quic_frame *dup_frm, *origin; |
| 2283 | |
| 2284 | switch (frm->type) { |
| 2285 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2286 | { |
| 2287 | struct quic_stream *strm_frm = &frm->stream; |
| 2288 | struct eb64_node *node = NULL; |
| 2289 | struct qc_stream_desc *stream_desc; |
| 2290 | |
| 2291 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2292 | if (!node) { |
| 2293 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 2294 | continue; |
| 2295 | } |
| 2296 | |
| 2297 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2298 | /* Do not resend this frame if in the "already acked range" */ |
| 2299 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2300 | TRACE_PROTO("ignored frame frame in already acked range", |
| 2301 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2302 | continue; |
| 2303 | } |
| 2304 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2305 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2306 | TRACE_PROTO("updated partially acked frame", |
| 2307 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2308 | } |
| 2309 | |
| 2310 | break; |
| 2311 | } |
| 2312 | |
| 2313 | default: |
| 2314 | break; |
| 2315 | } |
| 2316 | |
| 2317 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2318 | if (!dup_frm) { |
| 2319 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2320 | break; |
| 2321 | } |
| 2322 | |
| 2323 | /* If <frm> is already a copy of another frame, we must take |
| 2324 | * its original frame as source for the copy. |
| 2325 | */ |
| 2326 | origin = frm->origin ? frm->origin : frm; |
| 2327 | TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2328 | *dup_frm = *origin; |
| 2329 | LIST_INIT(&dup_frm->reflist); |
| 2330 | TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM, |
| 2331 | qc, NULL, &origin->pkt->pn_node.key); |
| 2332 | dup_frm->origin = origin; |
| 2333 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2334 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2335 | } |
| 2336 | |
| 2337 | LIST_SPLICE(out_frm_list, &tmp); |
| 2338 | } |
| 2339 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2340 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2341 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2342 | struct quic_enc_level *qel, |
| 2343 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2344 | { |
| 2345 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2346 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2347 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2348 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2349 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2350 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2351 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2352 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2353 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2354 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2355 | node = eb64_next(node); |
| 2356 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2357 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2358 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2362 | return; |
| 2363 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2364 | /* When building a packet from another one, the field which may increase the |
| 2365 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2366 | */ |
| 2367 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2368 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2369 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2370 | return; |
| 2371 | } |
| 2372 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2373 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2374 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2375 | if (frms == frms1 && frms2) { |
| 2376 | frms = frms2; |
| 2377 | goto start; |
| 2378 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2379 | } |
| 2380 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2381 | /* Prepare a fast retransmission during a handshake after a client |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2382 | * has resent Initial packets. According to the RFC a server may retransmit |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2383 | * Initial packets send them coalescing with others (Handshake here). |
| 2384 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2385 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2386 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2387 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2388 | { |
| 2389 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2390 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2391 | |
| 2392 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2393 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2394 | struct quic_enc_level *qel = iqel; |
| 2395 | struct eb_root *pkts; |
| 2396 | struct eb64_node *node; |
| 2397 | struct quic_tx_packet *pkt; |
| 2398 | struct list *tmp = &itmp; |
| 2399 | |
| 2400 | start: |
| 2401 | pkt = NULL; |
| 2402 | pkts = &qel->pktns->tx.pkts; |
| 2403 | node = eb64_first(pkts); |
| 2404 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2405 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2406 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2407 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2408 | break; |
| 2409 | node = eb64_next(node); |
| 2410 | } |
| 2411 | |
| 2412 | if (!pkt) |
| 2413 | goto end; |
| 2414 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2415 | /* When building a packet from another one, the field which may increase the |
| 2416 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2417 | */ |
| 2418 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2419 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2420 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2421 | goto end; |
| 2422 | } |
| 2423 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2424 | qel->pktns->tx.pto_probe += 1; |
| 2425 | requeue: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2426 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2427 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2428 | if (qel == iqel) { |
| 2429 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2430 | pkt = pkt->next; |
| 2431 | tmp = &htmp; |
| 2432 | hqel->pktns->tx.pto_probe += 1; |
| 2433 | goto requeue; |
| 2434 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2435 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2436 | |
| 2437 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2438 | LIST_SPLICE(ifrms, &itmp); |
| 2439 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2440 | } |
| 2441 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2442 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2443 | * as I/O handler context and <qel> as encryption level. |
| 2444 | * Returns 1 if succeeded, 0 if failed. |
| 2445 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2446 | 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] | 2447 | struct quic_enc_level *qel) |
| 2448 | { |
| 2449 | struct quic_frame frm; |
| 2450 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2451 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2452 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2453 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2454 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2455 | /* Skip the AAD */ |
| 2456 | pos = pkt->data + pkt->aad_len; |
| 2457 | end = pkt->data + pkt->len; |
| 2458 | |
| 2459 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2460 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2461 | goto err; |
| 2462 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2463 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2464 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2465 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | case QUIC_FT_PING: |
| 2468 | break; |
| 2469 | case QUIC_FT_ACK: |
| 2470 | { |
| 2471 | unsigned int rtt_sample; |
| 2472 | |
| 2473 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2474 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2475 | goto err; |
| 2476 | |
| 2477 | if (rtt_sample) { |
| 2478 | unsigned int ack_delay; |
| 2479 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2480 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2481 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2482 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2483 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2484 | 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] | 2485 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2486 | break; |
| 2487 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2488 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2489 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2490 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2491 | { |
| 2492 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2493 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2494 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2495 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2496 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2497 | |
| 2498 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2499 | cfdebug.len = frm.crypto.len; |
| 2500 | TRACE_PROTO("CRYPTO data discarded", |
| 2501 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2502 | break; |
| 2503 | } |
| 2504 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2505 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2506 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2507 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2508 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2509 | |
| 2510 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2511 | cfdebug.len = frm.crypto.len; |
| 2512 | /* Nothing to do */ |
| 2513 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2514 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2515 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2516 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2517 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2518 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2519 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2520 | else { |
| 2521 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2522 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2523 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2524 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2525 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2526 | cfdebug.len = frm.crypto.len; |
| 2527 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2528 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2529 | frm.crypto.len -= diff; |
| 2530 | frm.crypto.data += diff; |
| 2531 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2532 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2533 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2534 | |
| 2535 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2536 | /* 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] | 2537 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2538 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2539 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2540 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2541 | if (!qc_provide_cdata(qel, ctx, |
| 2542 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2543 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2544 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2545 | |
| 2546 | break; |
| 2547 | } |
| 2548 | |
| 2549 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2550 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2551 | if (!cf) { |
| 2552 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2553 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2554 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2555 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2556 | |
| 2557 | cf->offset_node.key = frm.crypto.offset; |
| 2558 | cf->len = frm.crypto.len; |
| 2559 | cf->data = frm.crypto.data; |
| 2560 | cf->pkt = pkt; |
| 2561 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2562 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2563 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2564 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2565 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2566 | { |
| 2567 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2568 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2569 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2570 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2571 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2572 | goto err; |
| 2573 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2574 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2575 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2576 | /* The upper layer may not be allocated. */ |
| 2577 | if (qc->mux_state != QC_MUX_READY) { |
| 2578 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2579 | TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2580 | break; |
| 2581 | } |
| 2582 | else { |
| 2583 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc); |
| 2584 | goto err; |
| 2585 | } |
| 2586 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2587 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2588 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2589 | goto err; |
| 2590 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2591 | break; |
| 2592 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2593 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2594 | if (qc->mux_state == QC_MUX_READY) { |
| 2595 | struct quic_max_data *data = &frm.max_data; |
| 2596 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2597 | } |
| 2598 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2599 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2600 | if (qc->mux_state == QC_MUX_READY) { |
| 2601 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2602 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2603 | data->max_stream_data); |
| 2604 | } |
| 2605 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2606 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2607 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2608 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2609 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2610 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2611 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2612 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2613 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2614 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2615 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2616 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2617 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2618 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2619 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2620 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2621 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2622 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2623 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2624 | break; |
| 2625 | case QUIC_FT_CONNECTION_CLOSE: |
| 2626 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2627 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
Frédéric Lécaille | 07968dc | 2022-05-20 20:53:20 +0200 | [diff] [blame] | 2628 | /* If the connection did not reached the handshake complete state, |
| 2629 | * the <conn_opening> counter was not decremented. Note that if |
| 2630 | * a TLS alert was received from the TLS stack, this counter |
| 2631 | * has already been decremented. |
| 2632 | */ |
| 2633 | if (qc->state < QUIC_HS_ST_COMPLETE && !(qc->flags & QUIC_FL_CONN_TLS_ALERT)) |
| 2634 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2635 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2636 | /* RFC 9000 10.2. Immediate Close: |
| 2637 | * The closing and draining connection states exist to ensure |
| 2638 | * that connections close cleanly and that delayed or reordered |
| 2639 | * packets are properly discarded. These states SHOULD persist |
| 2640 | * for at least three times the current PTO interval... |
| 2641 | * |
| 2642 | * Rearm the idle timeout only one time when entering draining |
| 2643 | * state. |
| 2644 | */ |
| 2645 | qc_idle_timer_do_rearm(qc); |
| 2646 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2647 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2648 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2649 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2650 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2651 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2652 | goto err; |
| 2653 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2654 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2655 | break; |
| 2656 | default: |
| 2657 | goto err; |
| 2658 | } |
| 2659 | } |
| 2660 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2661 | /* Flag this packet number space as having received a packet. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2662 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2663 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2664 | if (fast_retrans) { |
| 2665 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2666 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2667 | |
| 2668 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2669 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2670 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2671 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2672 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2673 | * be discarded. |
| 2674 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2675 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2676 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2677 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2678 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2679 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2680 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2681 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2682 | qc_set_timer(ctx->qc); |
| 2683 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2684 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2685 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2686 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2687 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2688 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2691 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2692 | return 1; |
| 2693 | |
| 2694 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2695 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2696 | return 0; |
| 2697 | } |
| 2698 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2699 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2700 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2701 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2702 | * index after having marked the end of written data. This the responsability |
| 2703 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2704 | * data made of a uint16_t null field. |
| 2705 | * |
| 2706 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2707 | * ^ ^ |
| 2708 | * r w |
| 2709 | * |
| 2710 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2711 | * ^ ^ |
| 2712 | * r w |
| 2713 | |
| 2714 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2715 | * ^ |
| 2716 | * (r = w) |
| 2717 | * |
| 2718 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2719 | * ^ ^ |
| 2720 | * w r |
| 2721 | */ |
| 2722 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2723 | { |
| 2724 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2725 | |
| 2726 | /* We can reset the writer index only if in front of the reader index and |
| 2727 | * if the reader index is not null. Resetting the writer when the reader |
| 2728 | * index is null would empty the buffer. |
| 2729 | * XXX Note than the writer index cannot reach the reader index. |
| 2730 | * Only the reader index can reach the writer index. |
| 2731 | */ |
| 2732 | if (rd && rd <= cbuf->wr) { |
| 2733 | /* Mark the end of contiguous data for the reader */ |
| 2734 | write_u16(cb_wr(cbuf), 0); |
| 2735 | cb_add(cbuf, sizeof(uint16_t)); |
| 2736 | cb_wr_reset(cbuf); |
| 2737 | return 1; |
| 2738 | } |
| 2739 | |
| 2740 | return 0; |
| 2741 | } |
| 2742 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2743 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2744 | * 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] | 2745 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2746 | * This function must be called only after having built a correct datagram. |
| 2747 | * Always succeeds. |
| 2748 | */ |
| 2749 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2750 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2751 | { |
| 2752 | write_u16(cb_wr(cbuf), dglen); |
| 2753 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2754 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2755 | } |
| 2756 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2757 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2758 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2759 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2760 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2761 | */ |
| 2762 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2763 | struct quic_enc_level *qel, int cc, int probe) |
| 2764 | { |
| 2765 | unsigned int must_ack = |
| 2766 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2767 | |
| 2768 | /* Do not build any more packet if the TX secrets are not available or |
| 2769 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2770 | * and if there is no more packets to send upon PTO expiration |
| 2771 | * and if there is no more ack-eliciting frames to send or in flight |
| 2772 | * congestion control limit is reached for prepared data |
| 2773 | */ |
| 2774 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2775 | (!cc && !probe && !must_ack && |
| 2776 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2777 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
| 2781 | return 1; |
| 2782 | } |
| 2783 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2784 | /* Prepare as much as possible short packets which are also datagrams into <qr> |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2785 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2786 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2787 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2788 | * by the address of the first packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2789 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2790 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2791 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2792 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2793 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2794 | { |
| 2795 | struct quic_enc_level *qel; |
| 2796 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2797 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2798 | struct quic_tx_packet *pkt; |
| 2799 | size_t total; |
| 2800 | size_t dg_headlen; |
| 2801 | |
| 2802 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2803 | /* Each datagram is prepended with its length followed by the |
| 2804 | * address of the first packet in the datagram. |
| 2805 | */ |
| 2806 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2807 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2808 | total = 0; |
| 2809 | start: |
| 2810 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2811 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2812 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2813 | * to ensure there is enough room to mark the end of prepared |
| 2814 | * contiguous data with a zero length. |
| 2815 | */ |
| 2816 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2817 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2818 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2819 | |
| 2820 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2821 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2822 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2823 | /* We do not probe if an immediate close was asked */ |
| 2824 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2825 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2826 | |
| 2827 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2828 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* Leave room for the datagram header */ |
| 2831 | pos += dg_headlen; |
| 2832 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2833 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2834 | } |
| 2835 | else { |
| 2836 | end = pos + qc->path->mtu; |
| 2837 | } |
| 2838 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2839 | pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2840 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2841 | switch (err) { |
| 2842 | case -2: |
| 2843 | goto err; |
| 2844 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2845 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2846 | * MTU, we are here because of the congestion control window. There is |
| 2847 | * no need to try to reuse this buffer. |
| 2848 | */ |
| 2849 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2850 | default: |
| 2851 | break; |
| 2852 | } |
| 2853 | |
| 2854 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2855 | if (!pkt) |
| 2856 | goto err; |
| 2857 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2858 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2859 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2860 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2861 | total += pkt->len; |
| 2862 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2863 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2864 | } |
| 2865 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2866 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2867 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2868 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2869 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2870 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | out: |
| 2874 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2875 | return total; |
| 2876 | |
| 2877 | err: |
| 2878 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2879 | return -1; |
| 2880 | } |
| 2881 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2882 | /* 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] | 2883 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2884 | * several packets in the same datagram. A header made of two fields is added |
| 2885 | * to each datagram: the datagram length followed by the address of the first |
| 2886 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2887 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2888 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2889 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2890 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2891 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2892 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2893 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2894 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2895 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2896 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2897 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2898 | /* length of datagrams */ |
| 2899 | uint16_t dglen; |
| 2900 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2901 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2902 | /* Each datagram is prepended with its length followed by the |
| 2903 | * address of the first packet in the datagram. |
| 2904 | */ |
| 2905 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2906 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2907 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2908 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2909 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2910 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2911 | qel = &qc->els[tel]; |
| 2912 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2913 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2914 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2915 | padding = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2916 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2917 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2918 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2919 | * to ensure there is enough room to mark the end of prepared |
| 2920 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2921 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2922 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2923 | first_pkt = prv_pkt = NULL; |
| 2924 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2925 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2926 | enum quic_pkt_type pkt_type; |
| 2927 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2928 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2929 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2930 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2931 | /* We do not probe if an immediate close was asked */ |
| 2932 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2933 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2934 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2935 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2936 | if (prv_pkt) |
| 2937 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2938 | /* Let's select the next encryption level */ |
| 2939 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2940 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2941 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2942 | qel = &qc->els[tel]; |
| 2943 | /* Build a new datagram */ |
| 2944 | prv_pkt = NULL; |
| 2945 | continue; |
| 2946 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2947 | break; |
| 2948 | } |
| 2949 | |
| 2950 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2951 | if (!prv_pkt) { |
| 2952 | /* Leave room for the datagram header */ |
| 2953 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2954 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2955 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2956 | } |
| 2957 | else { |
| 2958 | end = pos + qc->path->mtu; |
| 2959 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2960 | } |
| 2961 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2962 | cur_pkt = qc_build_pkt(&pos, end, qel, frms, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2963 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2964 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2965 | case -2: |
| 2966 | goto err; |
| 2967 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2968 | /* If there was already a correct packet present, set the |
| 2969 | * current datagram as prepared into <cbuf>. |
| 2970 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2971 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2972 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2973 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2974 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2975 | break; |
| 2976 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2977 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2978 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2979 | if (!cur_pkt) |
| 2980 | goto err; |
| 2981 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2982 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2983 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2984 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2985 | total += cur_pkt->len; |
| 2986 | /* keep trace of the first packet in the datagram */ |
| 2987 | if (!first_pkt) |
| 2988 | first_pkt = cur_pkt; |
| 2989 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2990 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2991 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2992 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 2993 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2994 | /* Let's say we have to build a new dgram */ |
| 2995 | prv_pkt = NULL; |
| 2996 | dglen += cur_pkt->len; |
| 2997 | /* Client: discard the Initial encryption keys as soon as |
| 2998 | * a handshake packet could be built. |
| 2999 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3000 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3001 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3002 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3003 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3004 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3005 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 3006 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 3007 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3008 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3009 | } |
| 3010 | /* If the data for the current encryption level have all been sent, |
| 3011 | * select the next level. |
| 3012 | */ |
| 3013 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3014 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3015 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3016 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3017 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3018 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3019 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3020 | frms = &qc->els[tel].pktns->tx.frms; |
| 3021 | else |
| 3022 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3023 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3024 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3025 | /* If there is data for the next level, do not |
| 3026 | * consume a datagram. |
| 3027 | */ |
| 3028 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3029 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3030 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3031 | /* If we have to build a new datagram, set the current datagram as |
| 3032 | * prepared into <cbuf>. |
| 3033 | */ |
| 3034 | if (!prv_pkt) { |
| 3035 | qc_set_dg(cbuf, dglen, first_pkt); |
| 3036 | first_pkt = NULL; |
| 3037 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3038 | padding = 0; |
| 3039 | } |
| 3040 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3041 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3042 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3043 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | stop_build: |
| 3048 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3049 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3050 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3051 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3052 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3056 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3057 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3058 | |
| 3059 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3060 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3061 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | /* 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] | 3065 | * 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] | 3066 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3067 | 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] | 3068 | { |
| 3069 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3070 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3071 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3072 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3073 | cbuf = qr->cbuf; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3074 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3075 | while (cb_contig_data(cbuf)) { |
| 3076 | unsigned char *pos; |
| 3077 | struct buffer tmpbuf = { }; |
| 3078 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3079 | uint16_t dglen; |
| 3080 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3081 | unsigned int time_sent; |
| 3082 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3083 | pos = cb_rd(cbuf); |
| 3084 | dglen = read_u16(pos); |
| 3085 | /* End of prepared datagrams. |
| 3086 | * Reset the reader index only if in front of the writer index. |
| 3087 | */ |
| 3088 | if (!dglen) { |
| 3089 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3090 | |
| 3091 | if (wr && wr < cbuf->rd) { |
| 3092 | cb_rd_reset(cbuf); |
| 3093 | continue; |
| 3094 | } |
| 3095 | break; |
| 3096 | } |
| 3097 | |
| 3098 | pos += sizeof dglen; |
| 3099 | first_pkt = read_ptr(pos); |
| 3100 | pos += sizeof first_pkt; |
| 3101 | tmpbuf.area = (char *)pos; |
| 3102 | tmpbuf.size = tmpbuf.data = dglen; |
| 3103 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3104 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3105 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3106 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3107 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3108 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3109 | qc->tx.bytes += tmpbuf.data; |
| 3110 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3111 | |
| 3112 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3113 | pkt->time_sent = time_sent; |
| 3114 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3115 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3116 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3117 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3118 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3119 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3120 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3121 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3122 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3123 | qc_notify_close(qc); |
| 3124 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3125 | /* RFC 9000 10.2. Immediate Close: |
| 3126 | * The closing and draining connection states exist to ensure |
| 3127 | * that connections close cleanly and that delayed or reordered |
| 3128 | * packets are properly discarded. These states SHOULD persist |
| 3129 | * for at least three times the current PTO interval... |
| 3130 | * |
| 3131 | * Rearm the idle timeout only one time when entering closing |
| 3132 | * state. |
| 3133 | */ |
| 3134 | qc_idle_timer_do_rearm(qc); |
| 3135 | if (qc->timer_task) { |
| 3136 | task_destroy(qc->timer_task); |
| 3137 | qc->timer_task = NULL; |
| 3138 | } |
| 3139 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3140 | qc->path->in_flight += pkt->in_flight_len; |
| 3141 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3142 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3143 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3144 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3145 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3146 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3147 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3148 | } |
| 3149 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3150 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3151 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3152 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3153 | return 1; |
| 3154 | } |
| 3155 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3156 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3157 | * <salt> salt input. This is the cluster secret which will be derived |
| 3158 | * as HKDF input secret to generate this token. |
| 3159 | * Return 1 if succeeded, 0 if not. |
| 3160 | */ |
| 3161 | static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len, |
| 3162 | const unsigned char *salt, size_t saltlen) |
| 3163 | { |
| 3164 | /* Input secret */ |
| 3165 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3166 | size_t keylen = strlen(global.cluster_secret); |
| 3167 | /* Info */ |
| 3168 | const unsigned char label[] = "stateless token"; |
| 3169 | size_t labellen = sizeof label - 1; |
| 3170 | |
| 3171 | return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3172 | key, keylen, salt, saltlen, label, labellen); |
| 3173 | } |
| 3174 | |
| 3175 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3176 | * Returns 1 if succeeded, 0 if not. |
| 3177 | */ |
| 3178 | static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid) |
| 3179 | { |
| 3180 | if (global.cluster_secret) { |
| 3181 | /* Output secret */ |
| 3182 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3183 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3184 | /* Salt */ |
| 3185 | const unsigned char *cid = quic_cid->cid.data; |
| 3186 | size_t cidlen = quic_cid->cid.len; |
| 3187 | |
| 3188 | return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen); |
| 3189 | } |
| 3190 | else { |
| 3191 | return RAND_bytes(quic_cid->stateless_reset_token, |
| 3192 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3193 | } |
| 3194 | } |
| 3195 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3196 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3197 | * ebtree. |
| 3198 | * |
| 3199 | * The CID is randomly generated in part with the result altered to be |
| 3200 | * associated with the current thread ID. This means this function must only |
| 3201 | * be called by the quic_conn thread. |
| 3202 | * |
| 3203 | * Returns the new CID if succeeded, NULL if not. |
| 3204 | */ |
| 3205 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3206 | struct quic_conn *qc, |
| 3207 | int seq_num) |
| 3208 | { |
| 3209 | struct quic_connection_id *cid; |
| 3210 | |
| 3211 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3212 | if (!cid) |
| 3213 | return NULL; |
| 3214 | |
| 3215 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3216 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3217 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3218 | |
| 3219 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3220 | if (quic_stateless_reset_token_init(cid) != 1) |
| 3221 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3222 | |
| 3223 | cid->qc = qc; |
| 3224 | |
| 3225 | cid->seq_num.key = seq_num; |
| 3226 | cid->retire_prior_to = 0; |
| 3227 | /* insert the allocated CID in the quic_conn tree */ |
| 3228 | eb64_insert(root, &cid->seq_num); |
| 3229 | |
| 3230 | return cid; |
| 3231 | |
| 3232 | err: |
| 3233 | pool_free(pool_head_quic_connection_id, cid); |
| 3234 | return NULL; |
| 3235 | } |
| 3236 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3237 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3238 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3239 | * a HANDSHAKE_DONE frame. |
| 3240 | * Return 1 if succeeded, 0 if not. |
| 3241 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3242 | 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] | 3243 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3244 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3245 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3246 | struct quic_frame *frm, *frmbak; |
| 3247 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3248 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3249 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3250 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3251 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3252 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3253 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3254 | if (!frm) |
| 3255 | return 0; |
| 3256 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3257 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3258 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3259 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3260 | } |
| 3261 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3262 | first = 1; |
| 3263 | max = qc->tx.params.active_connection_id_limit; |
| 3264 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3265 | struct quic_connection_id *cid; |
| 3266 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3267 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3268 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3269 | goto err; |
| 3270 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3271 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3272 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3273 | if (!cid) |
| 3274 | goto err; |
| 3275 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3276 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3277 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3278 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3279 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3280 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3281 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3282 | |
| 3283 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3284 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3285 | |
| 3286 | return 1; |
| 3287 | |
| 3288 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3289 | /* free the frames */ |
| 3290 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3291 | pool_free(pool_head_quic_frame, frm); |
| 3292 | |
| 3293 | node = eb64_first(&qc->cids); |
| 3294 | while (node) { |
| 3295 | struct quic_connection_id *cid; |
| 3296 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3297 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3298 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3299 | if (cid->seq_num.key >= max) |
| 3300 | break; |
| 3301 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3302 | if (cid->seq_num.key < first) |
| 3303 | continue; |
| 3304 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3305 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3306 | ebmb_delete(&cid->node); |
| 3307 | eb64_delete(&cid->seq_num); |
| 3308 | pool_free(pool_head_quic_connection_id, cid); |
| 3309 | } |
| 3310 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3311 | return 0; |
| 3312 | } |
| 3313 | |
| 3314 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3315 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3317 | struct eb64_node *n; |
| 3318 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3319 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3320 | n = eb64_first(&arngs->root); |
| 3321 | while (n) { |
| 3322 | struct eb64_node *next; |
| 3323 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3324 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3325 | next = eb64_next(n); |
| 3326 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3327 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3328 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3332 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3333 | * descending order. |
| 3334 | */ |
| 3335 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3336 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3337 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3338 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3343 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3344 | * 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] | 3345 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3346 | 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] | 3347 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3348 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3349 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3350 | last = eb64_last(&arngs->root); |
| 3351 | while (last && arngs->enc_sz > limit) { |
| 3352 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3353 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3354 | prev = eb64_prev(last); |
| 3355 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3356 | return 0; |
| 3357 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3358 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3359 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3360 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3361 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3362 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3363 | --arngs->sz; |
| 3364 | eb64_delete(last); |
| 3365 | pool_free(pool_head_quic_arng, last); |
| 3366 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | return 1; |
| 3370 | } |
| 3371 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3372 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3373 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3374 | { |
| 3375 | struct eb64_node *node, *next; |
| 3376 | struct quic_arng_node *ar, *ar_next; |
| 3377 | |
| 3378 | node = eb64_last(&arngs->root); |
| 3379 | if (!node) |
| 3380 | return; |
| 3381 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3382 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3383 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3384 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3385 | |
| 3386 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3387 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3388 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3389 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3390 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3391 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3395 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3396 | * 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] | 3397 | */ |
| 3398 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3399 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3400 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3401 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3402 | struct quic_arng_node *new_ar; |
| 3403 | |
| 3404 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3405 | if (new_ar) { |
| 3406 | new_ar->first.key = ar->first; |
| 3407 | new_ar->last = ar->last; |
| 3408 | eb64_insert(&arngs->root, &new_ar->first); |
| 3409 | arngs->sz++; |
| 3410 | } |
| 3411 | |
| 3412 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | /* 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] | 3416 | * 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] | 3417 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3418 | * |
| 3419 | * Descending order |
| 3420 | * -------------> |
| 3421 | * range1 range2 |
| 3422 | * ..........|--------|..............|--------| |
| 3423 | * ^ ^ ^ ^ |
| 3424 | * | | | | |
| 3425 | * last1 first1 last2 first2 |
| 3426 | * ..........+--------+--------------+--------+...... |
| 3427 | * diff1 gap12 diff2 |
| 3428 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3429 | * To encode the previous list of ranges we must encode integers as follows in |
| 3430 | * descending order: |
| 3431 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3432 | * with diff1 = last1 - first1 |
| 3433 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3434 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3435 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3436 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3437 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3438 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3439 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3440 | struct eb64_node *le; |
| 3441 | struct quic_arng_node *new_node; |
| 3442 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3443 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3444 | new = NULL; |
| 3445 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3446 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3447 | if (!new_node) |
| 3448 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3449 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3450 | goto out; |
| 3451 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3452 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3453 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3454 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3455 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3456 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3457 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3458 | |
| 3459 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3460 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3461 | else { |
| 3462 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3463 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3464 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3465 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3466 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3467 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3468 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3469 | if (le_ar->last + 1 >= ar->first) { |
| 3470 | le_ar->last = ar->last; |
| 3471 | new = le; |
| 3472 | new_node = le_ar; |
| 3473 | } |
| 3474 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3475 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3476 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3477 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3478 | |
| 3479 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3480 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3481 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3482 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3483 | /* Verify that the new inserted node does not overlap the nodes |
| 3484 | * which follow it. |
| 3485 | */ |
| 3486 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3487 | struct eb64_node *next; |
| 3488 | struct quic_arng_node *next_node; |
| 3489 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3490 | while ((next = eb64_next(new))) { |
| 3491 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3492 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3493 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3494 | break; |
| 3495 | |
| 3496 | if (next_node->last > new_node->last) |
| 3497 | new_node->last = next_node->last; |
| 3498 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3499 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3500 | /* Decrement the size of these ranges. */ |
| 3501 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3502 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3503 | } |
| 3504 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3505 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3506 | quic_arngs_set_enc_sz(arngs); |
| 3507 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3508 | return 1; |
| 3509 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3510 | /* Remove the header protection of packets at <el> encryption level. |
| 3511 | * Always succeeds. |
| 3512 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3513 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3514 | { |
| 3515 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3516 | struct quic_rx_packet *pqpkt; |
| 3517 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3518 | struct quic_enc_level *app_qel; |
| 3519 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3520 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3521 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3522 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3523 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3524 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3525 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3526 | goto out; |
| 3527 | } |
| 3528 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3529 | mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3530 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3531 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3532 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3533 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3534 | /* XXX TO DO XXX */ |
| 3535 | } |
| 3536 | else { |
| 3537 | /* The AAD includes the packet number field */ |
| 3538 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3539 | /* Store the packet into the tree of packets to decrypt. */ |
| 3540 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3541 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3542 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3543 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3544 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3545 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3546 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3547 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3548 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3552 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3556 | * Return 1 if succeeded, 0 if not. |
| 3557 | */ |
| 3558 | 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] | 3559 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3560 | { |
| 3561 | struct eb64_node *node; |
| 3562 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3563 | node = eb64_first(&el->rx.crypto.frms); |
| 3564 | while (node) { |
| 3565 | struct quic_rx_crypto_frm *cf; |
| 3566 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3567 | cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3568 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3569 | break; |
| 3570 | |
| 3571 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3572 | goto err; |
| 3573 | |
| 3574 | node = eb64_next(node); |
| 3575 | quic_rx_packet_refdec(cf->pkt); |
| 3576 | eb64_delete(&cf->offset_node); |
| 3577 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3578 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3579 | return 1; |
| 3580 | |
| 3581 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3582 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3583 | return 0; |
| 3584 | } |
| 3585 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3586 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3587 | * 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] | 3588 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3589 | * Return 1 if succeeded, 0 if not. |
| 3590 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3591 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3592 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3593 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3594 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3595 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3596 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3597 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3598 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3599 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3600 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3601 | qel = cur_el; |
| 3602 | next_tel: |
| 3603 | if (!qel) |
| 3604 | goto out; |
| 3605 | |
| 3606 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3607 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3608 | while (node) { |
| 3609 | struct quic_rx_packet *pkt; |
| 3610 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3611 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3612 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3613 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3614 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3615 | /* Drop the packet */ |
| 3616 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3617 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3618 | } |
| 3619 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3620 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3621 | /* Drop the packet */ |
| 3622 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3623 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3624 | } |
| 3625 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3626 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3627 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3628 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3629 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3630 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3631 | qc_idle_timer_rearm(qc, 1); |
| 3632 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3633 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3634 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3635 | largest_pn_time_received = pkt->time_received; |
| 3636 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3637 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3638 | 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] | 3639 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3640 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3644 | eb64_delete(&pkt->pn_node); |
| 3645 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3646 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3647 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3648 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3649 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3650 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3651 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3652 | /* Update the largest acknowledged packet timestamps */ |
| 3653 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3654 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3655 | } |
| 3656 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3657 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3658 | goto err; |
| 3659 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3660 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3661 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3662 | qel = next_el; |
| 3663 | goto next_tel; |
| 3664 | } |
| 3665 | |
| 3666 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3667 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3668 | return 1; |
| 3669 | |
| 3670 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3671 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3672 | return 0; |
| 3673 | } |
| 3674 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3675 | /* Check if it's possible to remove header protection for packets related to |
| 3676 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3677 | * |
| 3678 | * Return true if the operation is possible else false. |
| 3679 | */ |
| 3680 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3681 | { |
| 3682 | enum quic_tls_enc_level tel; |
| 3683 | |
| 3684 | if (!qel) |
| 3685 | return 0; |
| 3686 | |
| 3687 | tel = ssl_to_quic_enc_level(qel->level); |
| 3688 | |
| 3689 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3690 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3691 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3692 | return 0; |
| 3693 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3694 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3695 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3696 | return 0; |
| 3697 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3698 | /* check if the connection layer is ready before using app level */ |
Frédéric Lécaille | 298931d | 2022-01-28 21:41:06 +0100 | [diff] [blame] | 3699 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 3700 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3701 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3702 | |
| 3703 | return 1; |
| 3704 | } |
| 3705 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3706 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3707 | int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3708 | { |
| 3709 | int ret; |
| 3710 | struct qring *qr; |
| 3711 | |
| 3712 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3713 | if (!qr) |
| 3714 | /* Never happens */ |
| 3715 | return 1; |
| 3716 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3717 | if (old_data) |
| 3718 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3719 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3720 | if (ret == -1) |
| 3721 | goto err; |
| 3722 | else if (ret == 0) |
| 3723 | goto out; |
| 3724 | |
| 3725 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3726 | goto err; |
| 3727 | |
| 3728 | out: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3729 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3730 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3731 | return 1; |
| 3732 | |
| 3733 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3734 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3735 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3736 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3740 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3741 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 3742 | * QUIC connection |
| 3743 | * Returns 1 if succeeded, 0 if not. |
| 3744 | */ |
| 3745 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3746 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3747 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3748 | { |
| 3749 | int ret; |
| 3750 | struct qring *qr; |
| 3751 | |
| 3752 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3753 | if (!qr) |
| 3754 | /* Never happens */ |
| 3755 | return 1; |
| 3756 | |
| 3757 | if (old_data) |
| 3758 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3759 | ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms); |
| 3760 | if (ret == -1) |
| 3761 | goto err; |
| 3762 | else if (ret == 0) |
| 3763 | goto out; |
| 3764 | |
| 3765 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3766 | goto err; |
| 3767 | |
| 3768 | out: |
| 3769 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3770 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3771 | return 1; |
| 3772 | |
| 3773 | err: |
| 3774 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3775 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3776 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
| 3780 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3781 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3782 | { |
| 3783 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3784 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3785 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3786 | |
| 3787 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3788 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 3789 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 3790 | |
| 3791 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 3792 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 3793 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3794 | if (!LIST_ISEMPTY(&ifrms)) { |
| 3795 | iqel->pktns->tx.pto_probe = 1; |
| 3796 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3797 | hqel->pktns->tx.pto_probe = 1; |
| 3798 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 3799 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 3800 | } |
| 3801 | } |
| 3802 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3803 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 3804 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3805 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3806 | hqel->pktns->tx.pto_probe = 1; |
| 3807 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 3808 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3809 | } |
| 3810 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3811 | } |
| 3812 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3813 | } |
| 3814 | else { |
| 3815 | int i; |
| 3816 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 3817 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 3818 | |
| 3819 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3820 | hqel->pktns->tx.pto_probe = 0; |
| 3821 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 3822 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 3823 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3824 | if (!LIST_ISEMPTY(&frms1)) { |
| 3825 | hqel->pktns->tx.pto_probe = 1; |
| 3826 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 3827 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3828 | } |
| 3829 | } |
| 3830 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3831 | } |
| 3832 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3833 | aqel->pktns->tx.pto_probe = 0; |
| 3834 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 3835 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3836 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 3837 | if (!LIST_ISEMPTY(&frms1)) { |
| 3838 | aqel->pktns->tx.pto_probe = 1; |
| 3839 | qc_send_app_pkts(qc, 1, &frms1); |
| 3840 | } |
| 3841 | if (!LIST_ISEMPTY(&frms2)) { |
| 3842 | aqel->pktns->tx.pto_probe = 1; |
| 3843 | qc_send_app_pkts(qc, 1, &frms2); |
| 3844 | } |
| 3845 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3850 | /* QUIC connection packet handler task (post handshake) */ |
| 3851 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3852 | { |
| 3853 | struct ssl_sock_ctx *ctx; |
| 3854 | struct quic_conn *qc; |
| 3855 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3856 | |
| 3857 | |
| 3858 | ctx = context; |
| 3859 | qc = ctx->qc; |
| 3860 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3861 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3862 | TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3863 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3864 | /* Retranmissions */ |
| 3865 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3866 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 3867 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3868 | qc_dgrams_retransmit(qc); |
| 3869 | } |
| 3870 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3871 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3872 | qc_rm_hp_pkts(qc, qel); |
| 3873 | |
| 3874 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3875 | goto err; |
| 3876 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3877 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3878 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3879 | goto out; |
| 3880 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3881 | if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms)) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3882 | goto err; |
| 3883 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3884 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3885 | return t; |
| 3886 | |
| 3887 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3888 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3889 | return t; |
| 3890 | } |
| 3891 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3892 | /* QUIC connection packet handler task. */ |
| 3893 | 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] | 3894 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3895 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3896 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3897 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3898 | enum quic_tls_enc_level tel, next_tel; |
| 3899 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3900 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3901 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3902 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3903 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3904 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3905 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3906 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3907 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3908 | TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3909 | |
| 3910 | /* Retranmissions */ |
| 3911 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3912 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 3913 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3914 | qc_dgrams_retransmit(qc); |
| 3915 | } |
| 3916 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3917 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3918 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3919 | /* The I/O handler has been woken up by the dgram listener |
| 3920 | * after the anti-amplification was reached. |
| 3921 | */ |
| 3922 | qc_set_timer(qc); |
| 3923 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3924 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3925 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3926 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3927 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3928 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3929 | 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] | 3930 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3931 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3932 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3933 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3934 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3935 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3936 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3937 | } |
| 3938 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3939 | goto err; |
| 3940 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3941 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3942 | 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] | 3943 | |
| 3944 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3945 | /* Treat packets waiting for header packet protection decryption */ |
| 3946 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3947 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3948 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3949 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3950 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3951 | 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] | 3952 | goto err; |
| 3953 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3954 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3955 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3956 | goto out; |
| 3957 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 3958 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
| 3959 | !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
| 3960 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3961 | qel = next_qel; |
| 3962 | next_qel = NULL; |
| 3963 | goto next_level; |
| 3964 | } |
| 3965 | else { |
| 3966 | struct quic_rx_packet *pkt; |
| 3967 | struct mt_list *elt1, elt2; |
| 3968 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 3969 | |
| 3970 | /* Drop these 0-RTT packets */ |
| 3971 | TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3972 | mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) { |
| 3973 | MT_LIST_DELETE_SAFE(elt1); |
| 3974 | quic_rx_packet_refdec(pkt); |
| 3975 | } |
| 3976 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3979 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3980 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3981 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3982 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3983 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3984 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3985 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3986 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3987 | /* Discard the Handshake keys. */ |
| 3988 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3989 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3990 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3991 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3992 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 3993 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3997 | /* There may be remaining handshake to build (acks) */ |
| 3998 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3999 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4000 | } |
| 4001 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4002 | if (!qr) |
| 4003 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 4004 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4005 | * be considered. |
| 4006 | */ |
| 4007 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0)) |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 4008 | goto err; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 4009 | ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms, |
| 4010 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4011 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4012 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4013 | else if (ret == 0) |
| 4014 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4015 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4016 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4017 | goto err; |
| 4018 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4019 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4020 | /* Check if there is something to do for the next level. |
| 4021 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4022 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4023 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 4024 | (!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] | 4025 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4026 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4027 | goto next_level; |
| 4028 | } |
| 4029 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4030 | out: |
| 4031 | if (qr) |
| 4032 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4033 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4034 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4035 | |
| 4036 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4037 | if (qr) |
| 4038 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4039 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4040 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4043 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4044 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 4045 | { |
| 4046 | int i; |
| 4047 | |
| 4048 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4049 | if (qel->tx.crypto.bufs[i]) { |
| 4050 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4051 | qel->tx.crypto.bufs[i] = NULL; |
| 4052 | } |
| 4053 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4054 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4058 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4059 | * Returns 1 if succeeded, 0 if not. |
| 4060 | */ |
| 4061 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4062 | enum quic_tls_enc_level level) |
| 4063 | { |
| 4064 | struct quic_enc_level *qel; |
| 4065 | |
| 4066 | qel = &qc->els[level]; |
| 4067 | qel->level = quic_to_ssl_enc_level(level); |
| 4068 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4069 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4070 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 4071 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4072 | |
| 4073 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4074 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4075 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4076 | qel->rx.crypto.offset = 0; |
| 4077 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4078 | |
| 4079 | /* Allocate only one buffer. */ |
| 4080 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4081 | if (!qel->tx.crypto.bufs) |
| 4082 | goto err; |
| 4083 | |
| 4084 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4085 | if (!qel->tx.crypto.bufs[0]) |
| 4086 | goto err; |
| 4087 | |
| 4088 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4089 | qel->tx.crypto.nb_buf = 1; |
| 4090 | |
| 4091 | qel->tx.crypto.sz = 0; |
| 4092 | qel->tx.crypto.offset = 0; |
| 4093 | |
| 4094 | return 1; |
| 4095 | |
| 4096 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4097 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4098 | return 0; |
| 4099 | } |
| 4100 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4101 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4102 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4103 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4104 | * This function must only be called by the thread responsible of the quic_conn |
| 4105 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4106 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4107 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4108 | { |
| 4109 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4110 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4111 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4112 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4113 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4114 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4115 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4116 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4117 | /* free remaining stream descriptors */ |
| 4118 | node = eb64_first(&qc->streams_by_id); |
| 4119 | while (node) { |
| 4120 | struct qc_stream_desc *stream; |
| 4121 | |
| 4122 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4123 | node = eb64_next(node); |
| 4124 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4125 | /* all streams attached to the quic-conn are released, so |
| 4126 | * qc_stream_desc_free will liberate the stream instance. |
| 4127 | */ |
| 4128 | BUG_ON(!stream->release); |
| 4129 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4130 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4131 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4132 | if (qc->idle_timer_task) { |
| 4133 | task_destroy(qc->idle_timer_task); |
| 4134 | qc->idle_timer_task = NULL; |
| 4135 | } |
| 4136 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4137 | if (qc->timer_task) { |
| 4138 | task_destroy(qc->timer_task); |
| 4139 | qc->timer_task = NULL; |
| 4140 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4141 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4142 | /* remove the connection from receiver cids trees */ |
| 4143 | ebmb_delete(&qc->odcid_node); |
| 4144 | ebmb_delete(&qc->scid_node); |
| 4145 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4146 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4147 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4148 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4149 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4150 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4151 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4152 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4153 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4154 | quic_tls_ku_free(qc); |
| 4155 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4156 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4157 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4161 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4162 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4163 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4164 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 4165 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4166 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4167 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4168 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4169 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4170 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4171 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4172 | } |
| 4173 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4174 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4175 | { |
| 4176 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4177 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4178 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4179 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4180 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4181 | /* Next application data can be dropped. */ |
| 4182 | qc->mux_state = QC_MUX_RELEASED; |
| 4183 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4184 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4185 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4186 | quic_conn_release(qc); |
| 4187 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 4188 | return; |
| 4189 | } |
| 4190 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4191 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4192 | } |
| 4193 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4194 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4195 | 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] | 4196 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4197 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4198 | struct quic_conn *qc; |
| 4199 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4200 | |
| 4201 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4202 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4203 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4204 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4205 | task->expire = TICK_ETERNITY; |
| 4206 | pktns = quic_loss_pktns(qc); |
| 4207 | if (tick_isset(pktns->tx.loss_time)) { |
| 4208 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4209 | |
| 4210 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4211 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4212 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4213 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4214 | goto out; |
| 4215 | } |
| 4216 | |
| 4217 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4218 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | dafbde6 | 2022-04-26 13:54:28 +0200 | [diff] [blame] | 4219 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4220 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4221 | struct qcc *qcc = qc->qcc; |
| 4222 | |
| 4223 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4224 | tasklet_wakeup(qcc->subs->tasklet); |
| 4225 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4226 | if (!qcc->subs->events) |
| 4227 | qcc->subs = NULL; |
| 4228 | } |
| 4229 | else { |
| 4230 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4231 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4232 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4233 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 4234 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4235 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4236 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4237 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4238 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4239 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4240 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4241 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4242 | if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4243 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4244 | if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4245 | iel->pktns->tx.pto_probe = 1; |
| 4246 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4247 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4248 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4249 | qc->path->loss.pto_count++; |
| 4250 | |
| 4251 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4252 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4253 | |
| 4254 | return task; |
| 4255 | } |
| 4256 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4257 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4258 | * one byte past the end of this buffer. This will extract the ODCID |
| 4259 | * which will be stored into <odcid> |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4260 | * |
| 4261 | * Returns 0 on success else non-zero. |
| 4262 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4263 | static int parse_retry_token(const unsigned char *token, const unsigned char *end, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4264 | struct quic_cid *odcid) |
| 4265 | { |
| 4266 | uint64_t odcid_len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4267 | uint32_t timestamp; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4268 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4269 | if (!quic_dec_int(&odcid_len, &token, end)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4270 | return 1; |
| 4271 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4272 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4273 | * When an Initial packet is sent by a client that has not previously |
| 4274 | * received an Initial or Retry packet from the server, the client |
| 4275 | * populates the Destination Connection ID field with an unpredictable |
| 4276 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4277 | */ |
| 4278 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) |
| 4279 | return 1; |
| 4280 | |
| 4281 | if (end - token < odcid_len + sizeof timestamp) |
| 4282 | return 1; |
| 4283 | |
| 4284 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4285 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4286 | return 1; |
| 4287 | |
| 4288 | memcpy(odcid->data, token, odcid_len); |
| 4289 | odcid->len = odcid_len; |
| 4290 | |
| 4291 | return 0; |
| 4292 | } |
| 4293 | |
| 4294 | /* Initialize the transport parameters for <qc> QUIC connection attached |
| 4295 | * to <l> listener from <pkt> Initial packet information. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4296 | * Returns 1 if succeeded, 0 if not. |
| 4297 | */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4298 | static int qc_lstnr_params_init(struct quic_conn *qc, struct listener *l, |
| 4299 | const unsigned char *token, size_t token_len, |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4300 | const struct quic_connection_id *icid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4301 | const struct quic_cid *dcid, const struct quic_cid *odcid) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4302 | { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4303 | struct quic_cid *odcid_param = &qc->rx.params.original_destination_connection_id; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4304 | |
| 4305 | /* Copy the transport parameters. */ |
| 4306 | qc->rx.params = l->bind_conf->quic_params; |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4307 | /* Copy the stateless reset token */ |
| 4308 | memcpy(qc->rx.params.stateless_reset_token, icid->stateless_reset_token, |
| 4309 | sizeof qc->rx.params.stateless_reset_token); |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4310 | /* Copy original_destination_connection_id transport parameter. */ |
| 4311 | if (token_len) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4312 | memcpy(odcid_param->data, odcid->data, odcid->len); |
| 4313 | odcid_param->len = odcid->len; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4314 | /* Copy retry_source_connection_id transport parameter. */ |
| 4315 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, dcid); |
| 4316 | } |
| 4317 | else { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4318 | memcpy(odcid_param->data, dcid->data, dcid->len); |
| 4319 | odcid_param->len = dcid->len; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | /* Copy the initial source connection ID. */ |
| 4323 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4324 | |
| 4325 | return 1; |
| 4326 | } |
| 4327 | |
| 4328 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4329 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4330 | * for QUIC servers (or haproxy listeners). |
| 4331 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4332 | * <token> the token found to be used for this connection with <token_len> as |
| 4333 | * length. <saddr> is the source address. |
| 4334 | * Returns the connection if succeeded, NULL if not. |
| 4335 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4336 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4337 | struct quic_cid *dcid, struct quic_cid *scid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4338 | const struct quic_cid *odcid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4339 | struct sockaddr_storage *saddr, |
| 4340 | const unsigned char *token, size_t token_len, |
| 4341 | int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4342 | { |
| 4343 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4344 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4345 | /* Initial CID. */ |
| 4346 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4347 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4348 | struct listener *l = NULL; |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4349 | const unsigned char *salt = initial_salt_v1; |
| 4350 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4351 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4352 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4353 | qc = pool_zalloc(pool_head_quic_conn); |
| 4354 | if (!qc) { |
| 4355 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4356 | goto err; |
| 4357 | } |
| 4358 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4359 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4360 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4361 | TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4362 | goto err; |
| 4363 | } |
| 4364 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4365 | qc->cids = EB_ROOT; |
| 4366 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4367 | if (server) { |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4368 | struct proxy *prx; |
| 4369 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4370 | l = owner; |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4371 | prx = l->bind_conf->frontend; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4372 | |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4373 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4374 | &quic_stats_module); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4375 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4376 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4377 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4378 | qc->odcid.len = dcid->len; |
| 4379 | qc->odcid.addrlen = dcid->addrlen; |
| 4380 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4381 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4382 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4383 | if (scid->len) |
| 4384 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4385 | qc->dcid.len = scid->len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4386 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4387 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4388 | } |
| 4389 | /* QUIC Client (outgoing connection to servers) */ |
| 4390 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4391 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4392 | if (dcid->len) |
| 4393 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4394 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4395 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4396 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | |
| 4398 | /* Initialize the output buffer */ |
| 4399 | qc->obuf.pos = qc->obuf.data; |
| 4400 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4401 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4402 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4403 | TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4404 | goto err; |
| 4405 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4406 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4407 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4408 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4409 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4410 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4411 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4412 | qc->scid = icid->cid; |
| 4413 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4414 | /* Packet number spaces initialization. */ |
| 4415 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4416 | quic_pktns_init(&qc->pktns[i]); |
| 4417 | /* QUIC encryption level context initialization. */ |
| 4418 | 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] | 4419 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4420 | TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4421 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4422 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4423 | /* Initialize the packet number space. */ |
| 4424 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4425 | } |
| 4426 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4427 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4428 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4429 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4430 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4431 | /* TX part. */ |
| 4432 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4433 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4434 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4435 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4436 | /* RX part. */ |
| 4437 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4438 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 664741e | 2022-05-02 18:46:58 +0200 | [diff] [blame] | 4439 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4440 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4441 | |
| 4442 | qc->nb_pkt_for_cc = 1; |
| 4443 | qc->nb_pkt_since_cc = 0; |
| 4444 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4445 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4446 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4447 | TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4448 | goto err; |
| 4449 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4450 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4451 | /* XXX TO DO: Only one path at this time. */ |
| 4452 | qc->path = &qc->paths[0]; |
| 4453 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4454 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4455 | /* required to use MTLIST_IN_LIST */ |
| 4456 | MT_LIST_INIT(&qc->accept_list); |
| 4457 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4458 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4459 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 4460 | qc->sendto_err = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4461 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4462 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4463 | if (server && !qc_lstnr_params_init(qc, l, token, token_len, icid, dcid, odcid)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4464 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4465 | |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4466 | qc->enc_params_len = |
| 4467 | quic_transport_params_encode(qc->enc_params, |
| 4468 | qc->enc_params + sizeof qc->enc_params, |
| 4469 | &qc->rx.params, 1); |
| 4470 | if (!qc->enc_params_len) |
| 4471 | goto err; |
| 4472 | |
| 4473 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4474 | !quic_conn_init_timer(qc) || |
| 4475 | !quic_conn_init_idle_timer_task(qc)) |
| 4476 | goto err; |
| 4477 | |
| 4478 | if (version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4479 | salt = initial_salt_draft_29; |
| 4480 | salt_len = sizeof initial_salt_draft_29; |
| 4481 | } |
| 4482 | |
| 4483 | if (!qc_new_isecs(qc, salt, salt_len, dcid->data, dcid->len, 1)) |
| 4484 | goto err; |
| 4485 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4486 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4487 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4488 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4489 | |
| 4490 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4491 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4492 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4493 | if (qc) |
| 4494 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4495 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4496 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | /* Initialize the timer task of <qc> QUIC connection. |
| 4500 | * Returns 1 if succeeded, 0 if not. |
| 4501 | */ |
| 4502 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4503 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4504 | /* Attach this task to the same thread ID used for the connection */ |
| 4505 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4506 | if (!qc->timer_task) |
| 4507 | return 0; |
| 4508 | |
| 4509 | qc->timer = TICK_ETERNITY; |
| 4510 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4511 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4512 | |
| 4513 | return 1; |
| 4514 | } |
| 4515 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4516 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4517 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4518 | { |
| 4519 | unsigned int expire; |
| 4520 | |
| 4521 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4522 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4523 | } |
| 4524 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4525 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4526 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4527 | */ |
| 4528 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4529 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4530 | if (read) { |
| 4531 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4532 | } |
| 4533 | else { |
| 4534 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4535 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4536 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | /* The task handling the idle timeout */ |
| 4540 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4541 | { |
| 4542 | struct quic_conn *qc = ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4543 | struct quic_counters *prx_counters = qc->prx_counters; |
| 4544 | int qc_state = qc->state; |
| 4545 | unsigned int qc_flags = qc->flags; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4546 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4547 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4548 | * might free the quic-conn too early via quic_close(). |
| 4549 | */ |
| 4550 | qc_notify_close(qc); |
| 4551 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4552 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4553 | * responsible to call quic_close to release it. |
| 4554 | */ |
| 4555 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4556 | if (qc->mux_state != QC_MUX_READY) |
| 4557 | quic_conn_release(qc); |
| 4558 | |
| 4559 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4560 | * least clean some parts of it such as the tasklet. |
| 4561 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4562 | |
| 4563 | /* If the connection did not reached the handshake complete state, |
| 4564 | * the <conn_opening> counter was not decremented. Note that if |
| 4565 | * a TLS alert was received from the TLS stack, this counter |
| 4566 | * has already been decremented. |
| 4567 | */ |
Frédéric Lécaille | ad9895d | 2022-05-20 20:50:59 +0200 | [diff] [blame] | 4568 | if (qc_state < QUIC_HS_ST_COMPLETE && !(qc_flags & QUIC_FL_CONN_TLS_ALERT)) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4569 | HA_ATOMIC_DEC(&prx_counters->conn_opening); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4570 | |
| 4571 | return NULL; |
| 4572 | } |
| 4573 | |
| 4574 | /* Initialize the idle timeout task for <qc>. |
| 4575 | * Returns 1 if succeeded, 0 if not. |
| 4576 | */ |
| 4577 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4578 | { |
| 4579 | qc->idle_timer_task = task_new_here(); |
| 4580 | if (!qc->idle_timer_task) |
| 4581 | return 0; |
| 4582 | |
| 4583 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4584 | qc->idle_timer_task->context = qc; |
| 4585 | qc_idle_timer_rearm(qc, 1); |
| 4586 | task_queue(qc->idle_timer_task); |
| 4587 | |
| 4588 | return 1; |
| 4589 | } |
| 4590 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4591 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4592 | * past one byte of this buffer. |
| 4593 | */ |
| 4594 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4595 | struct quic_rx_packet *pkt) |
| 4596 | { |
| 4597 | unsigned char dcid_len, scid_len; |
| 4598 | |
| 4599 | /* Version */ |
| 4600 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4601 | return 0; |
| 4602 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4603 | /* Destination Connection ID Length */ |
| 4604 | dcid_len = *(*buf)++; |
| 4605 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4606 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4607 | /* XXX MUST BE DROPPED */ |
| 4608 | return 0; |
| 4609 | |
| 4610 | if (dcid_len) { |
| 4611 | /* Check that the length of this received DCID matches the CID lengths |
| 4612 | * of our implementation for non Initials packets only. |
| 4613 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4614 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4615 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4616 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4617 | return 0; |
| 4618 | |
| 4619 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4620 | } |
| 4621 | |
| 4622 | pkt->dcid.len = dcid_len; |
| 4623 | *buf += dcid_len; |
| 4624 | |
| 4625 | /* Source Connection ID Length */ |
| 4626 | scid_len = *(*buf)++; |
| 4627 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4628 | /* XXX MUST BE DROPPED */ |
| 4629 | return 0; |
| 4630 | |
| 4631 | if (scid_len) |
| 4632 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4633 | pkt->scid.len = scid_len; |
| 4634 | *buf += scid_len; |
| 4635 | |
| 4636 | return 1; |
| 4637 | } |
| 4638 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4639 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4640 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4641 | { |
| 4642 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4643 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4644 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4645 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4646 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | /* 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] | 4650 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4651 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4652 | * the packet first byte. |
| 4653 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4654 | * Returns 1 if succeeded, 0 if not. |
| 4655 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4656 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4657 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4658 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4659 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4660 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4661 | { |
| 4662 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4663 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4664 | struct quic_enc_level *qel; |
| 4665 | /* Only for traces. */ |
| 4666 | struct quic_rx_packet *qpkt_trace; |
| 4667 | |
| 4668 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4669 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4670 | /* The packet number is here. This is also the start minus |
| 4671 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4672 | * protection. |
| 4673 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4674 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4675 | |
| 4676 | tel = quic_packet_type_enc_level(pkt->type); |
| 4677 | qel = &qc->els[tel]; |
| 4678 | |
| 4679 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4680 | /* Note that the following function enables us to unprotect the packet |
| 4681 | * number and its length subsequently used to decrypt the entire |
| 4682 | * packets. |
| 4683 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4684 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4685 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4686 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4687 | goto err; |
| 4688 | } |
| 4689 | |
| 4690 | /* The AAD includes the packet number field found at <pn>. */ |
| 4691 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4692 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4693 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4694 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4695 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4696 | /* If the packet number space has been discarded, this packet |
| 4697 | * will be not parsed. |
| 4698 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4699 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4700 | goto out; |
| 4701 | } |
| 4702 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4703 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4704 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4705 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4706 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4707 | } |
| 4708 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4709 | *el = qel; |
| 4710 | /* No reference counter incrementation here!!! */ |
| 4711 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4712 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4713 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4714 | b_add(&qc->rx.buf, pkt->len); |
| 4715 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4716 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4717 | return 1; |
| 4718 | |
| 4719 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4720 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4721 | return 0; |
| 4722 | } |
| 4723 | |
| 4724 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4725 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4726 | */ |
| 4727 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4728 | unsigned char byte0, int *long_header) |
| 4729 | { |
| 4730 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4731 | pkt->type = |
| 4732 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4733 | *long_header = 1; |
| 4734 | } |
| 4735 | else { |
| 4736 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4737 | *long_header = 0; |
| 4738 | } |
| 4739 | } |
| 4740 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4741 | /* |
| 4742 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4743 | */ |
| 4744 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4745 | { |
| 4746 | int j = 0, version; |
| 4747 | |
| 4748 | do { |
| 4749 | version = quic_supported_version[j]; |
| 4750 | if (version == pkt->version) |
| 4751 | return 1; |
| 4752 | |
| 4753 | version = quic_supported_version[++j]; |
| 4754 | } while(version); |
| 4755 | |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4759 | /* |
| 4760 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4761 | * address <addr>. |
| 4762 | * Implementation of RFC9000 6. Version Negotiation |
| 4763 | * |
| 4764 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4765 | * |
| 4766 | * Returns 0 on success else non-zero |
| 4767 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4768 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4769 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4770 | { |
| 4771 | char buf[256]; |
| 4772 | int i = 0, j, version; |
| 4773 | const socklen_t addrlen = get_addr_len(addr); |
| 4774 | |
| 4775 | /* |
| 4776 | * header form |
| 4777 | * long header, fixed bit to 0 for Version Negotiation |
| 4778 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4779 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4780 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4781 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4782 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4783 | /* null version for Version Negotiation */ |
| 4784 | buf[i++] = '\x00'; |
| 4785 | buf[i++] = '\x00'; |
| 4786 | buf[i++] = '\x00'; |
| 4787 | buf[i++] = '\x00'; |
| 4788 | |
| 4789 | /* source connection id */ |
| 4790 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4791 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4792 | i += pkt->scid.len; |
| 4793 | |
| 4794 | /* destination connection id */ |
| 4795 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4796 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4797 | i += pkt->dcid.len; |
| 4798 | |
| 4799 | /* supported version */ |
| 4800 | j = 0; |
| 4801 | do { |
| 4802 | version = htonl(quic_supported_version[j]); |
| 4803 | memcpy(&buf[i], &version, sizeof(version)); |
| 4804 | i += sizeof(version); |
| 4805 | |
| 4806 | version = quic_supported_version[++j]; |
| 4807 | } while (version); |
| 4808 | |
| 4809 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4810 | return 1; |
| 4811 | |
| 4812 | return 0; |
| 4813 | } |
| 4814 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4815 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 4816 | * from <fd> UDP socket to <dst> |
| 4817 | * Return 1 if succeeded, 0 if not. |
| 4818 | */ |
| 4819 | static int send_stateless_reset(int fd, struct sockaddr_storage *dstaddr, |
| 4820 | struct quic_rx_packet *rxpkt) |
| 4821 | { |
| 4822 | int pktlen, rndlen; |
| 4823 | unsigned char pkt[64]; |
| 4824 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 4825 | |
| 4826 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 4827 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 4828 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 4829 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 4830 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 4831 | * connection ID length that it requests the peer to include in its packets, |
| 4832 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 4833 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 4834 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 4835 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 4836 | * than the packet it responds to. |
| 4837 | */ |
| 4838 | |
| 4839 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 4840 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 4841 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 4842 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 4843 | /* Put a header of random bytes */ |
| 4844 | if (RAND_bytes(pkt, rndlen) != 1) |
| 4845 | return 0; |
| 4846 | |
| 4847 | /* Clear the most significant bit, and set the second one */ |
| 4848 | *pkt = (*pkt & ~0x80) | 0x40; |
| 4849 | if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 4850 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 4851 | return 0; |
| 4852 | |
| 4853 | if (sendto(fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 4854 | return 0; |
| 4855 | |
| 4856 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 4857 | return 1; |
| 4858 | } |
| 4859 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4860 | /* QUIC server only function. |
| 4861 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 4862 | * This is the responsability of the caller to check <aad> size is big enough |
| 4863 | * to contain these data. |
| 4864 | * Return the number of bytes copied to <aad>. |
| 4865 | */ |
| 4866 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 4867 | uint32_t version, |
| 4868 | const struct quic_cid *cid, |
| 4869 | const struct sockaddr_storage *addr) |
| 4870 | { |
| 4871 | unsigned char *p; |
| 4872 | |
| 4873 | p = aad; |
| 4874 | memcpy(p, &version, sizeof version); |
| 4875 | p += sizeof version; |
| 4876 | p += quic_saddr_cpy(p, addr); |
| 4877 | memcpy(p, cid->data, cid->len); |
| 4878 | p += cid->len; |
| 4879 | |
| 4880 | return p - aad; |
| 4881 | } |
| 4882 | |
| 4883 | /* QUIC server only function. |
| 4884 | * Generate the token to be used in Retry packets. The token is written to |
| 4885 | * <buf> whith <len> as length. <odcid> is the original destination connection |
| 4886 | * ID and <dcid> is our side destination connection ID (or client source |
| 4887 | * connection ID). |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4888 | * Returns the length of the encoded token or 0 on error. |
| 4889 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4890 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 4891 | const uint32_t version, |
| 4892 | const struct quic_cid *odcid, |
| 4893 | const struct quic_cid *dcid, |
| 4894 | struct sockaddr_storage *addr) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4895 | { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4896 | unsigned char *p; |
| 4897 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4898 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4899 | size_t aadlen; |
| 4900 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 4901 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4902 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4903 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4904 | size_t seclen = strlen(global.cluster_secret); |
| 4905 | EVP_CIPHER_CTX *ctx = NULL; |
| 4906 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4907 | uint32_t timestamp = now_ms; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4908 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4909 | /* We copy the odcid into the token, prefixed by its one byte |
| 4910 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 4911 | * the random bytes used to derive the secret to encrypt the token. |
| 4912 | */ |
| 4913 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4914 | return 0; |
| 4915 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4916 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 4917 | if (RAND_bytes(salt, sizeof salt) != 1) |
| 4918 | goto err; |
| 4919 | |
| 4920 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 4921 | salt, sizeof salt, sec, seclen)) |
| 4922 | goto err; |
| 4923 | |
| 4924 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) |
| 4925 | goto err; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4926 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4927 | /* Token build */ |
| 4928 | p = buf; |
| 4929 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 4930 | *p++ = odcid->len; |
| 4931 | memcpy(p, odcid->data, odcid->len); |
| 4932 | p += odcid->len; |
| 4933 | write_u32(p, htonl(timestamp)); |
| 4934 | p += sizeof timestamp; |
| 4935 | |
| 4936 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4937 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) |
| 4938 | goto err; |
| 4939 | |
| 4940 | p += QUIC_TLS_TAG_LEN; |
| 4941 | memcpy(p, salt, sizeof salt); |
| 4942 | p += sizeof salt; |
| 4943 | EVP_CIPHER_CTX_free(ctx); |
| 4944 | |
| 4945 | return p - buf; |
| 4946 | |
| 4947 | err: |
| 4948 | if (ctx) |
| 4949 | EVP_CIPHER_CTX_free(ctx); |
| 4950 | return 0; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4953 | /* QUIC server only function. |
| 4954 | * Check the validity of the Retry token from <token> buffer with <tokenlen> |
| 4955 | * as length. If valid, the ODCID of <qc> QUIC connection will be put |
| 4956 | * into <odcid> connection ID. <dcid> is our side destination connection ID |
| 4957 | * of client source connection ID. |
| 4958 | * Return 1 if succeeded, 0 if not. |
| 4959 | */ |
| 4960 | static int quic_retry_token_check(unsigned char *token, size_t tokenlen, |
| 4961 | const uint32_t version, |
| 4962 | struct quic_cid *odcid, |
| 4963 | const struct quic_cid *dcid, |
| 4964 | struct quic_conn *qc, |
| 4965 | struct sockaddr_storage *addr) |
| 4966 | { |
| 4967 | unsigned char buf[128]; |
| 4968 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4969 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4970 | size_t aadlen; |
| 4971 | unsigned char *salt; |
| 4972 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4973 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4974 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4975 | size_t seclen = strlen(global.cluster_secret); |
| 4976 | EVP_CIPHER_CTX *ctx = NULL; |
| 4977 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4978 | |
| 4979 | if (sizeof buf < tokenlen) |
| 4980 | return 0; |
| 4981 | |
| 4982 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 4983 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 4984 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 4985 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) |
| 4986 | return 0; |
| 4987 | |
| 4988 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) |
| 4989 | goto err; |
| 4990 | |
| 4991 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4992 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 4993 | ctx, aead, key, iv)) |
| 4994 | goto err; |
| 4995 | |
| 4996 | if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 4997 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4998 | goto err; |
| 4999 | } |
| 5000 | |
| 5001 | EVP_CIPHER_CTX_free(ctx); |
| 5002 | return 1; |
| 5003 | |
| 5004 | err: |
| 5005 | if (ctx) |
| 5006 | EVP_CIPHER_CTX_free(ctx); |
| 5007 | return 0; |
| 5008 | } |
| 5009 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5010 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5011 | * the Initial <pkt> packet. |
| 5012 | * |
| 5013 | * Returns 0 on success else non-zero. |
| 5014 | */ |
| 5015 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 5016 | struct quic_rx_packet *pkt) |
| 5017 | { |
| 5018 | unsigned char buf[128]; |
| 5019 | int i = 0, token_len; |
| 5020 | const socklen_t addrlen = get_addr_len(addr); |
| 5021 | struct quic_cid scid; |
| 5022 | |
| 5023 | /* long header + fixed bit + packet type 0x3 */ |
| 5024 | buf[i++] = 0xf0; |
| 5025 | /* version */ |
| 5026 | buf[i++] = 0x00; |
| 5027 | buf[i++] = 0x00; |
| 5028 | buf[i++] = 0x00; |
| 5029 | buf[i++] = 0x01; |
| 5030 | |
| 5031 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5032 | buf[i++] = pkt->scid.len; |
| 5033 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5034 | i += pkt->scid.len; |
| 5035 | |
| 5036 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5037 | scid.len = QUIC_HAP_CID_LEN; |
| 5038 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 5039 | return 1; |
| 5040 | |
| 5041 | buf[i++] = scid.len; |
| 5042 | memcpy(&buf[i], scid.data, scid.len); |
| 5043 | i += scid.len; |
| 5044 | |
| 5045 | /* token */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5046 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, pkt->version, |
| 5047 | &pkt->dcid, &pkt->scid, addr))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5048 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 5049 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5050 | i += token_len; |
| 5051 | |
| 5052 | /* token integrity tag */ |
| 5053 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5054 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 5055 | pkt->dcid.len, buf, i)) { |
| 5056 | return 1; |
| 5057 | } |
| 5058 | |
| 5059 | i += QUIC_TLS_TAG_LEN; |
| 5060 | |
| 5061 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5062 | return 1; |
| 5063 | |
| 5064 | return 0; |
| 5065 | } |
| 5066 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5067 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5068 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5069 | * concatenated to the <pkt> DCID field. |
| 5070 | * |
| 5071 | * Returns the instance or NULL if not found. |
| 5072 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5073 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5074 | struct listener *l, |
| 5075 | struct sockaddr_storage *saddr) |
| 5076 | { |
| 5077 | struct quic_conn *qc = NULL; |
| 5078 | struct ebmb_node *node; |
| 5079 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5080 | /* set if the quic_conn is found in the second DCID tree */ |
| 5081 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5082 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5083 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5084 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5085 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5086 | /* DCIDs of first packets coming from multiple clients may have |
| 5087 | * the same values. Let's distinguish them by concatenating the |
| 5088 | * socket addresses. |
| 5089 | */ |
| 5090 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5091 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5092 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5093 | if (node) { |
| 5094 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5095 | goto end; |
| 5096 | } |
| 5097 | } |
| 5098 | |
| 5099 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5100 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5101 | * used. |
| 5102 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5103 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5104 | if (!node) |
| 5105 | goto end; |
| 5106 | |
| 5107 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5108 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5109 | found_in_dcid = 1; |
| 5110 | |
| 5111 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5112 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5113 | * If already done, this is a noop. |
| 5114 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 5115 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5116 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5117 | |
| 5118 | return qc; |
| 5119 | } |
| 5120 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5121 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5122 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5123 | * parameters of this session. |
| 5124 | * This is the responsibility of the caller to check the validity of all the |
| 5125 | * pointers passed as parameter to this function. |
| 5126 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5127 | * CO_ER_SSL_NO_MEM. |
| 5128 | */ |
| 5129 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5130 | unsigned char *params, size_t params_len) |
| 5131 | { |
| 5132 | int retry; |
| 5133 | |
| 5134 | retry = 1; |
| 5135 | retry: |
| 5136 | *ssl = SSL_new(ssl_ctx); |
| 5137 | if (!*ssl) { |
| 5138 | if (!retry--) |
| 5139 | goto err; |
| 5140 | |
| 5141 | pool_gc(NULL); |
| 5142 | goto retry; |
| 5143 | } |
| 5144 | |
| 5145 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5146 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 5147 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5148 | SSL_free(*ssl); |
| 5149 | *ssl = NULL; |
| 5150 | if (!retry--) |
| 5151 | goto err; |
| 5152 | |
| 5153 | pool_gc(NULL); |
| 5154 | goto retry; |
| 5155 | } |
| 5156 | |
| 5157 | return 0; |
| 5158 | |
| 5159 | err: |
| 5160 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5161 | return -1; |
| 5162 | } |
| 5163 | |
| 5164 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5165 | * used to process <qc> received packets. The allocated context is stored in |
| 5166 | * <qc.xprt_ctx>. |
| 5167 | * |
| 5168 | * Returns 0 on success else non-zero. |
| 5169 | */ |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 5170 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5171 | { |
| 5172 | struct bind_conf *bc = qc->li->bind_conf; |
| 5173 | struct ssl_sock_ctx *ctx = NULL; |
| 5174 | |
| 5175 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 5176 | if (!ctx) |
| 5177 | goto err; |
| 5178 | |
| 5179 | ctx->wait_event.tasklet = tasklet_new(); |
| 5180 | if (!ctx->wait_event.tasklet) |
| 5181 | goto err; |
| 5182 | |
| 5183 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5184 | ctx->wait_event.tasklet->context = ctx; |
| 5185 | ctx->wait_event.events = 0; |
| 5186 | ctx->subs = NULL; |
| 5187 | ctx->xprt_ctx = NULL; |
| 5188 | ctx->qc = qc; |
| 5189 | |
| 5190 | /* Set tasklet tid based on the SCID selected by us for this |
| 5191 | * connection. The upper layer will also be binded on the same thread. |
| 5192 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 5193 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5194 | |
| 5195 | if (qc_is_listener(qc)) { |
| 5196 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5197 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5198 | goto err; |
| 5199 | } |
| 5200 | |
| 5201 | /* Enabling 0-RTT */ |
| 5202 | if (bc->ssl_conf.early_data) |
| 5203 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5204 | |
| 5205 | SSL_set_accept_state(ctx->ssl); |
| 5206 | } |
| 5207 | |
| 5208 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5209 | |
| 5210 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5211 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5212 | |
| 5213 | return 0; |
| 5214 | |
| 5215 | err: |
| 5216 | if (ctx && ctx->wait_event.tasklet) |
| 5217 | tasklet_free(ctx->wait_event.tasklet); |
| 5218 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 5219 | |
| 5220 | return 1; |
| 5221 | } |
| 5222 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5223 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5224 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5225 | * with the information collected from the packet. |
| 5226 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5227 | * byte to enable the caller to run this function again to continue to parse |
| 5228 | * the remaing QUIC packets carried by the datagram. |
| 5229 | * Note that this function always sets this ->len value. If a paquet could |
| 5230 | * not be correctly found, ->len value will be set to the remaining number |
| 5231 | * bytes in the datagram to entirely consume this latter. |
| 5232 | */ |
| 5233 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5234 | struct quic_rx_packet *pkt, int first_pkt, |
| 5235 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5236 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5237 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5238 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5239 | struct listener *l; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5240 | struct proxy *prx; |
| 5241 | struct quic_counters *prx_counters; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5242 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5243 | int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5244 | size_t b_cspace; |
| 5245 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5246 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5247 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5248 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5249 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5250 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5251 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5252 | l = dgram->owner; |
| 5253 | prx = l->bind_conf->frontend; |
| 5254 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5255 | /* This ist only to please to traces and distinguish the |
| 5256 | * packet with parsed packet number from others. |
| 5257 | */ |
| 5258 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5259 | if (end <= buf) { |
| 5260 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5261 | goto drop; |
| 5262 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5263 | |
| 5264 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5265 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5266 | /* XXX TO BE DISCARDED */ |
| 5267 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5268 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5269 | } |
| 5270 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5271 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5272 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5273 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5274 | uint64_t len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5275 | struct quic_cid odcid; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5276 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5277 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5278 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5279 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5280 | } |
| 5281 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5282 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5283 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5284 | drop_no_conn = 1; |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5285 | } |
| 5286 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5287 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5288 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5289 | drop_no_conn = 1; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5290 | } |
| 5291 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5292 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5293 | * they must have the same DCID. |
| 5294 | */ |
| 5295 | if (!first_pkt && |
| 5296 | (pkt->dcid.len != dgram->dcid_len || |
| 5297 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5298 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5299 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5302 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 5303 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 5304 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5305 | goto drop; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5306 | } |
| 5307 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5308 | /* RFC9000 6. Version Negotiation */ |
| 5309 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5310 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5311 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5312 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 5313 | goto err; |
| 5314 | } |
| 5315 | |
| 5316 | TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5317 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5318 | } |
| 5319 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5320 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5321 | * there is no Initial connection IDs storage. |
| 5322 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5323 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5324 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5325 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5326 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5327 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5328 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5329 | goto drop; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5330 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5331 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5332 | /* TODO Retry should be automatically activated if |
| 5333 | * suspect network usage is detected. |
| 5334 | */ |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5335 | if (global.cluster_secret) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5336 | if (!token_len) { |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5337 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5338 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 5339 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
| 5340 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5341 | goto err; |
| 5342 | } |
| 5343 | |
| 5344 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5345 | goto err; |
| 5346 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5347 | } |
| 5348 | else { |
| 5349 | if (*buf == QUIC_TOKEN_FMT_RETRY) { |
| 5350 | if (!quic_retry_token_check(buf, token_len, pkt->version, &odcid, |
| 5351 | &pkt->scid, qc, &dgram->saddr)) { |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5352 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5353 | TRACE_PROTO("Wrong retry token", QUIC_EV_CONN_LPKT); |
| 5354 | /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection |
| 5355 | * with an INVALID_TOKEN error. |
| 5356 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5357 | goto drop; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5358 | } |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5359 | |
| 5360 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5361 | } |
| 5362 | else { |
| 5363 | /* TODO: New token check */ |
| 5364 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5365 | goto drop; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5366 | } |
| 5367 | } |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5368 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5369 | |
| 5370 | pkt->token = buf; |
| 5371 | pkt->token_len = token_len; |
| 5372 | buf += pkt->token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5373 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5374 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5375 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5376 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5377 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5378 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5381 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5382 | end - buf < len) { |
| 5383 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5384 | goto drop; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5385 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5386 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5387 | payload = buf; |
| 5388 | pkt->len = len + payload - beg; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5389 | if (drop_no_conn) |
| 5390 | goto drop_no_conn; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5391 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5392 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5393 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5394 | int ipv4; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5395 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5396 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5397 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 5398 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5399 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5400 | } |
| 5401 | |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5402 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5403 | HA_ATOMIC_LOAD(&prx_counters->conn_opening) >= global.tune.quic_retry_threshold) { |
| 5404 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 5405 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
| 5406 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5407 | goto err; |
| 5408 | } |
| 5409 | |
| 5410 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5411 | goto err; |
| 5412 | } |
| 5413 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5414 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5415 | * When an Initial packet is sent by a client that has not previously |
| 5416 | * received an Initial or Retry packet from the server, the client |
| 5417 | * populates the Destination Connection ID field with an unpredictable |
| 5418 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5419 | */ |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5420 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 5421 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5422 | goto drop; |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5423 | } |
| 5424 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5425 | pkt->saddr = dgram->saddr; |
| 5426 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5427 | qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &odcid, &pkt->saddr, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 5428 | pkt->token, pkt->token_len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5429 | if (qc == NULL) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5430 | goto drop; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5431 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5432 | HA_ATOMIC_INC(&prx_counters->conn_opening); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5433 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5434 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5435 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5436 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5437 | /* If the insertion failed, it means that another |
| 5438 | * thread has already allocated a QUIC connection for |
| 5439 | * the same CID. Liberate our allocated connection. |
| 5440 | */ |
| 5441 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5442 | qc_to_purge = qc; |
| 5443 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5444 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5445 | pkt->qc = qc; |
| 5446 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5447 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5448 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5449 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5450 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5451 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5452 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5453 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5454 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5455 | } |
| 5456 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5457 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5461 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5462 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5463 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5464 | } |
| 5465 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5466 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5467 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5468 | |
| 5469 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5470 | * they must have the same DCID. |
| 5471 | */ |
| 5472 | if (!first_pkt && |
| 5473 | (pkt->dcid.len != dgram->dcid_len || |
| 5474 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5475 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5476 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5477 | } |
| 5478 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5479 | buf += QUIC_HAP_CID_LEN; |
| 5480 | |
| 5481 | /* A short packet is the last one of a UDP datagram. */ |
| 5482 | payload = buf; |
| 5483 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5484 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5485 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5486 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5487 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5488 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5489 | if (global.cluster_secret && !send_stateless_reset(l->rx.fd, &dgram->saddr, pkt)) |
| 5490 | TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5491 | goto drop; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5492 | } |
| 5493 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5494 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5495 | } |
| 5496 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5497 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 5498 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 5499 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 5500 | qc->nb_pkt_for_cc++; |
| 5501 | qc->nb_pkt_since_cc = 0; |
| 5502 | } |
| 5503 | /* Skip the entire datagram */ |
| 5504 | pkt->len = end - beg; |
| 5505 | TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5506 | goto drop; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5507 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5508 | |
| 5509 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5510 | * they must have the same DCID. |
| 5511 | * |
| 5512 | * This check must be done after the final update to pkt.len to |
| 5513 | * properly drop the packet on failure. |
| 5514 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5515 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5516 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5517 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 5518 | QUIC_EV_CONN_LPKT, qc); |
| 5519 | /* Reset the anti-amplification bit. It will be set again |
| 5520 | * when sending the next packet if reached again. |
| 5521 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5522 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 5523 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5524 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5525 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5526 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5527 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5528 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5529 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5530 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5531 | goto out; |
| 5532 | } |
| 5533 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5534 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5535 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5536 | b_cspace = b_contig_space(&qc->rx.buf); |
| 5537 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 5538 | /* Do not consume buf if space not at the end. */ |
| 5539 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 5540 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5541 | goto err; |
| 5542 | } |
| 5543 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5544 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5545 | if (b_cspace) { |
| 5546 | b_putchr(&qc->rx.buf, 0x00); |
| 5547 | b_cspace--; |
| 5548 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5549 | b_add(&qc->rx.buf, b_cspace); |
| 5550 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5551 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 91ac6c3 | 2021-12-17 16:11:54 +0100 | [diff] [blame] | 5552 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5553 | goto drop; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5554 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5555 | } |
| 5556 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5557 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5558 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5559 | goto drop; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 5560 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5561 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5562 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5563 | if (pkt->aad_len) |
| 5564 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5565 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 5566 | /* Wake up the connection packet handler task from here only if all |
| 5567 | * the contexts have been initialized, especially the mux context |
| 5568 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 5569 | * will start it if these contexts for the connection are not already |
| 5570 | * initialized. |
| 5571 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5572 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5573 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5574 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5575 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5576 | drop_no_conn: |
| 5577 | if (drop_no_conn) |
| 5578 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5579 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5580 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5581 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5582 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5583 | drop: |
| 5584 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5585 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5586 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5587 | * This cannot be done by this thread. |
| 5588 | */ |
| 5589 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5590 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5591 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5592 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5593 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5594 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5595 | if (!pkt->len) |
| 5596 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5597 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5598 | qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5599 | } |
| 5600 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5601 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5602 | * 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] | 5603 | */ |
| 5604 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5605 | int type, size_t pn_len, struct quic_conn *conn) |
| 5606 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5607 | 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] | 5608 | return 0; |
| 5609 | |
| 5610 | /* #0 byte flags */ |
| 5611 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5612 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5613 | /* Version */ |
| 5614 | quic_write_uint32(buf, end, conn->version); |
| 5615 | *(*buf)++ = conn->dcid.len; |
| 5616 | /* Destination connection ID */ |
| 5617 | if (conn->dcid.len) { |
| 5618 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5619 | *buf += conn->dcid.len; |
| 5620 | } |
| 5621 | /* Source connection ID */ |
| 5622 | *(*buf)++ = conn->scid.len; |
| 5623 | if (conn->scid.len) { |
| 5624 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5625 | *buf += conn->scid.len; |
| 5626 | } |
| 5627 | |
| 5628 | return 1; |
| 5629 | } |
| 5630 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5631 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5632 | * 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] | 5633 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5634 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5635 | size_t pn_len, struct quic_conn *conn, |
| 5636 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5637 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5638 | if (end - *buf < 1 + conn->dcid.len) |
| 5639 | return 0; |
| 5640 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5641 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5642 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5643 | ((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] | 5644 | /* Destination connection ID */ |
| 5645 | if (conn->dcid.len) { |
| 5646 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5647 | *buf += conn->dcid.len; |
| 5648 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5649 | |
| 5650 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5654 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5655 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5656 | * Returns 1 if succeeded or 0 if failed. |
| 5657 | */ |
| 5658 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5659 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5660 | { |
| 5661 | int i, ret, outlen; |
| 5662 | EVP_CIPHER_CTX *ctx; |
| 5663 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5664 | * and at most 4 bytes for the packet number |
| 5665 | */ |
| 5666 | unsigned char mask[5] = {0}; |
| 5667 | |
| 5668 | ret = 0; |
| 5669 | ctx = EVP_CIPHER_CTX_new(); |
| 5670 | if (!ctx) |
| 5671 | return 0; |
| 5672 | |
| 5673 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5674 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5675 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5676 | goto out; |
| 5677 | |
| 5678 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5679 | for (i = 0; i < pnlen; i++) |
| 5680 | pn[i] ^= mask[i + 1]; |
| 5681 | |
| 5682 | ret = 1; |
| 5683 | |
| 5684 | out: |
| 5685 | EVP_CIPHER_CTX_free(ctx); |
| 5686 | |
| 5687 | return ret; |
| 5688 | } |
| 5689 | |
| 5690 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5691 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5692 | * Return 1 if succeeded, 0 if not. |
| 5693 | */ |
| 5694 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5695 | { |
| 5696 | size_t room, ack_delay_sz; |
| 5697 | |
| 5698 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5699 | /* A frame is made of 1 byte for the frame type. */ |
| 5700 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5701 | 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] | 5702 | return 0; |
| 5703 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5704 | 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] | 5705 | } |
| 5706 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5707 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5708 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5709 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5710 | * with the number of bytes already present in this buffer which must be taken |
| 5711 | * into an account for the Length packet field value. <headlen> is the number of |
| 5712 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5713 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5714 | * Update consequently <*len> to reflect the size of these frames built |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5715 | * by this function. Also attach these frames to <l> frame list. |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5716 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5717 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5718 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5719 | size_t room, size_t *len, size_t headlen, |
| 5720 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5721 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5722 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5723 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5724 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5725 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5726 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5727 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5728 | return 0; |
| 5729 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5730 | /* If we are not probing we must take into an account the congestion |
| 5731 | * control window. |
| 5732 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5733 | if (!qel->pktns->tx.pto_probe) { |
| 5734 | size_t remain = quic_path_prep_data(qc->path); |
| 5735 | |
| 5736 | if (headlen > remain) |
| 5737 | return 0; |
| 5738 | |
| 5739 | room = QUIC_MIN(room, remain - headlen); |
| 5740 | } |
| 5741 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5742 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5743 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5744 | |
| 5745 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 5746 | * returned by this function only if at least one frame could be built |
| 5747 | * in the switch/case block. |
| 5748 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5749 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5750 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5751 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5752 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5753 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5754 | break; |
| 5755 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5756 | switch (cf->type) { |
| 5757 | case QUIC_FT_CRYPTO: |
| 5758 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5759 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5760 | /* Compute the length of this CRYPTO frame header */ |
| 5761 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5762 | /* Compute the data length of this CRyPTO frame. */ |
| 5763 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5764 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5765 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5766 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5767 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5768 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5769 | /* CRYPTO frame length. */ |
| 5770 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5771 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5772 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5773 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5774 | * length and the length of this length. |
| 5775 | */ |
| 5776 | *len += flen; |
| 5777 | room -= flen; |
| 5778 | if (dlen == cf->crypto.len) { |
| 5779 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5780 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5781 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5782 | } |
| 5783 | else { |
| 5784 | struct quic_frame *new_cf; |
| 5785 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5786 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5787 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5788 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5789 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5790 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5791 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5792 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5793 | new_cf->type = QUIC_FT_CRYPTO; |
| 5794 | new_cf->crypto.len = dlen; |
| 5795 | new_cf->crypto.offset = cf->crypto.offset; |
| 5796 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5797 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5798 | /* Consume <dlen> bytes of the current frame. */ |
| 5799 | cf->crypto.len -= dlen; |
| 5800 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5801 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5802 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5803 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5804 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5805 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 5806 | struct eb64_node *node = NULL; |
| 5807 | struct qc_stream_desc *stream_desc = NULL; |
| 5808 | struct quic_stream *strm = &cf->stream; |
| 5809 | |
| 5810 | /* As this frame has been already lost, ensure the stream is always |
| 5811 | * available or the range of this frame is not consumed before |
| 5812 | * resending it. |
| 5813 | */ |
| 5814 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 5815 | if (!node) { |
| 5816 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5817 | LIST_DELETE(&cf->list); |
| 5818 | pool_free(pool_head_quic_frame, cf); |
| 5819 | continue; |
| 5820 | } |
| 5821 | |
| 5822 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5823 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 5824 | TRACE_PROTO("ignored frame frame in already acked range", |
| 5825 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5826 | LIST_DELETE(&cf->list); |
| 5827 | pool_free(pool_head_quic_frame, cf); |
| 5828 | continue; |
| 5829 | } |
| 5830 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 5831 | strm->offset.key = stream_desc->ack_offset; |
| 5832 | TRACE_PROTO("updated partially acked frame", |
| 5833 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5834 | } |
| 5835 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5836 | /* Note that these frames are accepted in short packets only without |
| 5837 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5838 | * 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] | 5839 | * |
| 5840 | * 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] | 5841 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5842 | */ |
| 5843 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5844 | ((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] | 5845 | /* Compute the data length of this STREAM frame. */ |
| 5846 | avail_room = room - hlen - *len; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5847 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5848 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5849 | if ((ssize_t)avail_room <= 0) |
| 5850 | continue; |
| 5851 | |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5852 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5853 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5854 | if (dlen > cf->stream.len) { |
| 5855 | dlen = cf->stream.len; |
| 5856 | } |
| 5857 | dlen_sz = quic_int_getsize(dlen); |
| 5858 | flen = hlen + dlen_sz + dlen; |
| 5859 | } |
| 5860 | else { |
| 5861 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5862 | flen = hlen + dlen; |
| 5863 | } |
| 5864 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5865 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5866 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5867 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5868 | /* Add the STREAM data length and its encoded length to the packet |
| 5869 | * length and the length of this length. |
| 5870 | */ |
| 5871 | *len += flen; |
| 5872 | room -= flen; |
| 5873 | if (dlen == cf->stream.len) { |
| 5874 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5875 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5876 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5877 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5878 | /* The MUX stream might be released at this |
| 5879 | * stage. This can most notably happen on |
| 5880 | * retransmission. |
| 5881 | */ |
| 5882 | if (qc->mux_state == QC_MUX_READY && |
| 5883 | !cf->stream.stream->release) { |
| 5884 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5885 | cf->stream.len, |
| 5886 | cf->stream.offset.key); |
| 5887 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5888 | } |
| 5889 | else { |
| 5890 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5891 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5892 | |
| 5893 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5894 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5895 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5896 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5897 | } |
| 5898 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5899 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5900 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5901 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5902 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5903 | new_cf->stream.id = cf->stream.id; |
| 5904 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5905 | new_cf->stream.offset = cf->stream.offset; |
| 5906 | new_cf->stream.len = dlen; |
| 5907 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5908 | /* FIN bit reset */ |
| 5909 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5910 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5911 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5912 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5913 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5914 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5915 | b_size(cf->stream.buf), |
| 5916 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5917 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5918 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5919 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5920 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5921 | /* The MUX stream might be released at this |
| 5922 | * stage. This can most notably happen on |
| 5923 | * retransmission. |
| 5924 | */ |
| 5925 | if (qc->mux_state == QC_MUX_READY && |
| 5926 | !cf->stream.stream->release) { |
| 5927 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5928 | new_cf->stream.len, |
| 5929 | new_cf->stream.offset.key); |
| 5930 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5931 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5932 | |
| 5933 | /* TODO the MUX is notified about the frame sending via |
| 5934 | * previous qcc_streams_sent_done call. However, the |
| 5935 | * sending can fail later, for example if the sendto |
| 5936 | * system call returns an error. As the MUX has been |
| 5937 | * notified, the transport layer is responsible to |
| 5938 | * bufferize and resent the announced data later. |
| 5939 | */ |
| 5940 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5941 | break; |
| 5942 | |
| 5943 | default: |
| 5944 | flen = qc_frm_len(cf); |
| 5945 | BUG_ON(!flen); |
| 5946 | if (flen > room) |
| 5947 | continue; |
| 5948 | |
| 5949 | *len += flen; |
| 5950 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5951 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5952 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5953 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5954 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5955 | |
| 5956 | /* Successful status as soon as a frame could be built */ |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5957 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5958 | } |
| 5959 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5960 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5961 | } |
| 5962 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5963 | /* 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] | 5964 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5965 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5966 | * filling the buffer with as much frames as possible from <frms> list of |
| 5967 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5968 | * 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] | 5969 | * 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] | 5970 | * having returned from this function. |
| 5971 | * 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] | 5972 | * number field in this packet. <pn_len> will also have the packet number |
| 5973 | * length as value. |
| 5974 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5975 | * 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] | 5976 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5977 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5978 | size_t dglen, struct quic_tx_packet *pkt, |
| 5979 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5980 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5981 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5982 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5983 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5984 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5985 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5986 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5987 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5988 | 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] | 5989 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5990 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5991 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5992 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5993 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5994 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5995 | /* Length field value with CRYPTO frames if present. */ |
| 5996 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5997 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5998 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5999 | * buffer to respect the congestion controller window. |
| 6000 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6001 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6002 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6003 | size_t path_room; |
| 6004 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6005 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6006 | if (end - beg > path_room) |
| 6007 | end = beg + path_room; |
| 6008 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6009 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6010 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6011 | * length field if any. |
| 6012 | */ |
| 6013 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6014 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6015 | goto no_room; |
| 6016 | |
| 6017 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6018 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6019 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6020 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6021 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6022 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6023 | !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] | 6024 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6025 | !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] | 6026 | goto no_room; |
| 6027 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6028 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 6029 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6030 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6031 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6032 | /* Build an ACK frame if required. */ |
| 6033 | ack_frm_len = 0; |
Frédéric Lécaille | 337108e | 2022-04-25 10:38:27 +0200 | [diff] [blame] | 6034 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6035 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6036 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6037 | ack_frm.tx_ack.arngs = arngs; |
| 6038 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 6039 | qel->pktns->tx.ack_delay = |
| 6040 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 6041 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 6042 | } |
| 6043 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6044 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 6045 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 6046 | * that from here, we do not know if we will have to send a PING frame. |
| 6047 | * This will be decided after having computed the ack-eliciting frames |
| 6048 | * to be added to this packet. |
| 6049 | */ |
| 6050 | 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] | 6051 | if (!ack_frm_len) |
| 6052 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6053 | } |
| 6054 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6055 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6056 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 6057 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6058 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6059 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6060 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6061 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6062 | /* Initialize the length of the frames built below to <len>. |
| 6063 | * If any frame could be successfully built by qc_build_frms(), |
| 6064 | * we will have len_frms > len. |
| 6065 | */ |
| 6066 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6067 | if (!qc_build_frms(&frm_list, frms, |
| 6068 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6069 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6070 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 6071 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 6072 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6073 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6074 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6075 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6076 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 6077 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 6078 | * for the encryption tag. It must be taken into an account for the length |
| 6079 | * of this packet. |
| 6080 | */ |
| 6081 | if (len_frms) |
| 6082 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 6083 | else |
| 6084 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6085 | /* CONNECTION_CLOSE frame */ |
| 6086 | if (cc) { |
| 6087 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6088 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6089 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6090 | len += qc_frm_len(&cc_frm); |
| 6091 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6092 | add_ping_frm = 0; |
| 6093 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6094 | len_sz = quic_int_getsize(len); |
| 6095 | /* Add this packet size to <dglen> */ |
| 6096 | dglen += head_len + len_sz + len; |
| 6097 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 6098 | /* This is a maximum padding size */ |
| 6099 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 6100 | /* The length field value is of this packet is <len> + <padding_len> |
| 6101 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 6102 | * <len_sz>. So, let's deduce the difference between these to packet |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6103 | * sizes from <padding_len>. |
| 6104 | */ |
| 6105 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 6106 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6107 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6108 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6109 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6110 | /* 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] | 6111 | add_ping_frm = 1; |
| 6112 | len += 1; |
| 6113 | } |
| 6114 | /* 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] | 6115 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6116 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 6117 | } |
| 6118 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6119 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 6120 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6121 | |
| 6122 | /* Packet number field address. */ |
| 6123 | *buf_pn = pos; |
| 6124 | |
| 6125 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6126 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 6127 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6128 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6129 | if (ack_frm_len) { |
| 6130 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 6131 | goto no_room; |
| 6132 | |
| 6133 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6134 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6135 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6136 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6137 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6138 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6139 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6140 | unsigned char *spos = pos; |
| 6141 | |
| 6142 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6143 | ssize_t room = end - pos; |
| 6144 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6145 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6146 | /* TODO: this should not have happened if qc_build_frms() |
| 6147 | * had correctly computed and sized the frames to be |
| 6148 | * added to this packet. Note that <cf> was added |
| 6149 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 6150 | */ |
| 6151 | LIST_DELETE(&cf->list); |
| 6152 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6153 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6154 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6155 | |
| 6156 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6157 | quic_tx_packet_refinc(pkt); |
| 6158 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | /* Build a PING frame if needed. */ |
| 6163 | if (add_ping_frm) { |
| 6164 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6165 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6166 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6167 | } |
| 6168 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6169 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6170 | if (cc) { |
| 6171 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 6172 | goto no_room; |
| 6173 | |
| 6174 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 6175 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6176 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6177 | /* Build a PADDING frame if needed. */ |
| 6178 | if (padding_len) { |
| 6179 | frm.type = QUIC_FT_PADDING; |
| 6180 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6181 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6182 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6183 | } |
| 6184 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6185 | /* If this packet is ack-eliciting and we are probing let's |
| 6186 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6187 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6188 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 6189 | qel->pktns->tx.pto_probe) |
| 6190 | qel->pktns->tx.pto_probe--; |
| 6191 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6192 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6193 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6194 | TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6195 | |
| 6196 | return 1; |
| 6197 | |
| 6198 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6199 | /* Replace the pre-built frames which could not be add to this packet */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6200 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | d8b798d | 2022-04-25 17:48:40 +0200 | [diff] [blame] | 6201 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6202 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6203 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6204 | } |
| 6205 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6206 | 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] | 6207 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6208 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6209 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6210 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 6211 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6212 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6213 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6214 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6215 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6216 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6217 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6218 | } |
| 6219 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6220 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6221 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 6222 | * of prebuilt frames. |
| 6223 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6224 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 6225 | * -1 if there was not enough room to build a packet. |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 6226 | * XXX NOTE XXX |
| 6227 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6228 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6229 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6230 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6231 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6232 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6233 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6234 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6235 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6236 | { |
| 6237 | /* The pointer to the packet number field. */ |
| 6238 | unsigned char *buf_pn; |
| 6239 | unsigned char *beg, *end, *payload; |
| 6240 | int64_t pn; |
| 6241 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6242 | struct quic_tls_ctx *tls_ctx; |
| 6243 | struct quic_tx_packet *pkt; |
| 6244 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6245 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6246 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6247 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6248 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6249 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6250 | *err = -2; |
| 6251 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6252 | } |
| 6253 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6254 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6255 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6256 | pn_len = 0; |
| 6257 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6258 | |
| 6259 | pn = qel->pktns->tx.next_pn + 1; |
| 6260 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6261 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6262 | *err = -1; |
| 6263 | goto err; |
| 6264 | } |
| 6265 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6266 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6267 | payload = buf_pn + pn_len; |
| 6268 | payload_len = end - payload; |
| 6269 | aad_len = payload - beg; |
| 6270 | |
| 6271 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6272 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6273 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6274 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6275 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6276 | |
| 6277 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6278 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6279 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 6280 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6281 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6282 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6283 | goto err; |
| 6284 | } |
| 6285 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6286 | /* Consume a packet number */ |
| 6287 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6288 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 6289 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6290 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6291 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6292 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6293 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6294 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6295 | /* 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] | 6296 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6297 | pkt->in_flight_len = pkt->len; |
| 6298 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6299 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6300 | /* Always reset this flags */ |
| 6301 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6302 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6303 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6304 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6305 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6306 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6307 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6308 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6309 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6310 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6311 | |
| 6312 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6313 | /* TODO: what about the frames which have been built |
| 6314 | * for this packet. |
| 6315 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6316 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6317 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6318 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6319 | } |
| 6320 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6321 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6322 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6323 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6324 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6325 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6326 | */ |
| 6327 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6328 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6329 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6330 | |
| 6331 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6332 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6333 | |
| 6334 | es->events |= event_type; |
| 6335 | qcc->subs = es; |
| 6336 | |
| 6337 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6338 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6339 | |
| 6340 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6341 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6342 | |
| 6343 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6347 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6348 | * subscribe() call. It always returns zero. |
| 6349 | */ |
| 6350 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6351 | { |
| 6352 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6353 | } |
| 6354 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6355 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 6356 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6357 | */ |
| 6358 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 6359 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 6360 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6361 | |
| 6362 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 6363 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6364 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6365 | if (*xprt_ctx) |
| 6366 | goto out; |
| 6367 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6368 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6369 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6370 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 6371 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6372 | |
| 6373 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6374 | } |
| 6375 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6376 | /* Start the QUIC transport layer */ |
| 6377 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 6378 | { |
| 6379 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 6380 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6381 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6382 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6383 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 6384 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 6385 | /* prepare a CONNECTION_CLOSE frame */ |
| 6386 | qc->err_code = QC_ERR_APPLICATION_ERROR; |
| 6387 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 6388 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | /* mux-quic can now be considered ready. */ |
| 6392 | qc->mux_state = QC_MUX_READY; |
| 6393 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6394 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 6395 | return 1; |
| 6396 | } |
| 6397 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6398 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 6399 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6400 | if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx) |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6401 | return NULL; |
| 6402 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6403 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6404 | } |
| 6405 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6406 | /* transport-layer operations for QUIC connections. */ |
| 6407 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 6408 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6409 | .subscribe = quic_conn_subscribe, |
| 6410 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6411 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6412 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6413 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 6414 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6415 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6416 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6417 | .name = "QUIC", |
| 6418 | }; |
| 6419 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6420 | static void __quic_conn_init(void) |
| 6421 | { |
| 6422 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 6423 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 6424 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6425 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6426 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6427 | static void __quic_conn_deinit(void) |
| 6428 | { |
| 6429 | BIO_meth_free(ha_quic_meth); |
| 6430 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6431 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6432 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6433 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 6434 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 6435 | * 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] | 6436 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6437 | struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6438 | { |
| 6439 | unsigned char *pos; |
| 6440 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6441 | struct quic_dghdlr *dghdlr = ctx; |
| 6442 | struct quic_dgram *dgram; |
| 6443 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6444 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6445 | while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6446 | pos = dgram->buf; |
| 6447 | end = pos + dgram->len; |
| 6448 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6449 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6450 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6451 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 6452 | if (!pkt) |
| 6453 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6454 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6455 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6456 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 6457 | qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6458 | first_pkt = 0; |
| 6459 | pos += pkt->len; |
| 6460 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6461 | } while (pos < end); |
| 6462 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6463 | /* Increasing the received bytes counter by the UDP datagram length |
| 6464 | * if this datagram could be associated to a connection. |
| 6465 | */ |
| 6466 | if (dgram->qc) |
| 6467 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 6468 | |
| 6469 | /* Mark this datagram as consumed */ |
| 6470 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6471 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6472 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6473 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6474 | |
| 6475 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6476 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6477 | } |
| 6478 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6479 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 6480 | * Returns 1 if succeeded, 0 if not. |
| 6481 | */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 6482 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 6483 | unsigned char **dcid, size_t *dcid_len) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6484 | { |
| 6485 | int long_header; |
| 6486 | size_t minlen, skip; |
| 6487 | |
| 6488 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 6489 | goto err; |
| 6490 | |
| 6491 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 6492 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 6493 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6494 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
Frédéric Lécaille | bfa3236 | 2022-02-02 10:44:36 +0100 | [diff] [blame] | 6495 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6496 | goto err; |
| 6497 | |
| 6498 | buf += skip; |
| 6499 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 6500 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 6501 | goto err; |
| 6502 | |
| 6503 | *dcid = buf; |
| 6504 | |
| 6505 | return 1; |
| 6506 | |
| 6507 | err: |
| 6508 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 6509 | return 0; |
| 6510 | } |
| 6511 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 6512 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 6513 | void qc_notify_close(struct quic_conn *qc) |
| 6514 | { |
| 6515 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 6516 | return; |
| 6517 | |
| 6518 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 6519 | |
| 6520 | /* wake up the MUX */ |
| 6521 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 6522 | qc->conn->mux->wake(qc->conn); |
| 6523 | } |
| 6524 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6525 | /* |
| 6526 | * Local variables: |
| 6527 | * c-indent-level: 8 |
| 6528 | * c-basic-offset: 8 |
| 6529 | * End: |
| 6530 | */ |