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> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 53 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 54 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 55 | #include <haproxy/task.h> |
| 56 | #include <haproxy/trace.h> |
| 57 | #include <haproxy/xprt_quic.h> |
| 58 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 59 | /* list of supported QUIC versions by this implementation */ |
| 60 | static int quic_supported_version[] = { |
| 61 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 62 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 63 | |
| 64 | /* placeholder, do not add entry after this */ |
| 65 | 0x0 |
| 66 | }; |
| 67 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 68 | /* This is the values of some QUIC transport parameters when absent. |
| 69 | * Should be used to initialize any transport parameters (local or remote) |
| 70 | * before updating them with customized values. |
| 71 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 72 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 73 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 74 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 75 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 76 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* trace source and events */ |
| 80 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 81 | const struct trace_source *src, |
| 82 | const struct ist where, const struct ist func, |
| 83 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 84 | |
| 85 | static const struct trace_event quic_trace_events[] = { |
| 86 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 87 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 88 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 90 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 91 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 92 | { .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] | 93 | { .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] | 94 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 95 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 96 | { .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] | 97 | { .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] | 98 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 101 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 102 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 103 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 104 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 105 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 106 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 107 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 108 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 109 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 110 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 111 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 112 | { .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] | 113 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 114 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 115 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 116 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 117 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 118 | { .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] | 119 | { .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] | 120 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 121 | { .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] | 122 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 123 | { .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] | 124 | { .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] | 125 | { .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] | 126 | { .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] | 127 | { /* end */ } |
| 128 | }; |
| 129 | |
| 130 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 131 | /* arg1 */ { /* already used by the connection */ }, |
| 132 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 133 | /* arg3 */ { }, |
| 134 | /* arg4 */ { } |
| 135 | }; |
| 136 | |
| 137 | static const struct name_desc quic_trace_decoding[] = { |
| 138 | #define QUIC_VERB_CLEAN 1 |
| 139 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 140 | { /* end */ } |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | struct trace_source trace_quic = { |
| 145 | .name = IST("quic"), |
| 146 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 147 | .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] | 148 | .default_cb = quic_trace, |
| 149 | .known_events = quic_trace_events, |
| 150 | .lockon_args = quic_trace_lockon_args, |
| 151 | .decoding = quic_trace_decoding, |
| 152 | .report_events = ~0, /* report everything by default */ |
| 153 | }; |
| 154 | |
| 155 | #define TRACE_SOURCE &trace_quic |
| 156 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 157 | |
| 158 | static BIO_METHOD *ha_quic_meth; |
| 159 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 160 | 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] | 161 | 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] | 162 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 163 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | 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] | 165 | DECLARE_POOL(pool_head_quic_connection_id, |
| 166 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 167 | 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] | 168 | 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] | 169 | 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] | 170 | 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] | 171 | 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] | 172 | 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] | 173 | 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] | 174 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 175 | 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] | 176 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 177 | 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] | 178 | int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 179 | 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] | 180 | 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] | 181 | 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] | 182 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 183 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 184 | 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] | 185 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 186 | /* Only for debug purpose */ |
| 187 | struct enc_debug_info { |
| 188 | unsigned char *payload; |
| 189 | size_t payload_len; |
| 190 | unsigned char *aad; |
| 191 | size_t aad_len; |
| 192 | uint64_t pn; |
| 193 | }; |
| 194 | |
| 195 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 196 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 197 | unsigned char *payload, size_t payload_len, |
| 198 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 199 | { |
| 200 | edi->payload = payload; |
| 201 | edi->payload_len = payload_len; |
| 202 | edi->aad = aad; |
| 203 | edi->aad_len = aad_len; |
| 204 | edi->pn = pn; |
| 205 | } |
| 206 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 207 | /* Trace callback for QUIC. |
| 208 | * These traces always expect that arg1, if non-null, is of type connection. |
| 209 | */ |
| 210 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 211 | const struct ist where, const struct ist func, |
| 212 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 213 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 214 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 216 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 217 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 218 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 219 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 220 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 221 | chunk_appendf(&trace_buf, "\n odcid"); |
| 222 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 223 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 224 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 225 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 226 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 230 | const enum ssl_encryption_level_t *level = a2; |
| 231 | const size_t *len = a3; |
| 232 | |
| 233 | if (level) { |
| 234 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 235 | |
| 236 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 237 | } |
| 238 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 239 | 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] | 240 | } |
| 241 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 242 | /* Initial read & write secrets. */ |
| 243 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 244 | const unsigned char *rx_sec = a2; |
| 245 | const unsigned char *tx_sec = a3; |
| 246 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 247 | tls_ctx = &qc->els[level].tls_ctx; |
| 248 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 249 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 250 | if (rx_sec) |
| 251 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 252 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 253 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 254 | if (tx_sec) |
| 255 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 256 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 260 | const enum ssl_encryption_level_t *level = a2; |
| 261 | const unsigned char *secret = a3; |
| 262 | const size_t *secret_len = a4; |
| 263 | |
| 264 | if (level) { |
| 265 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 266 | |
| 267 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 268 | if (secret && secret_len) |
| 269 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 270 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 271 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 272 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 277 | const enum ssl_encryption_level_t *level = a2; |
| 278 | const unsigned char *secret = a3; |
| 279 | const size_t *secret_len = a4; |
| 280 | |
| 281 | if (level) { |
| 282 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 283 | |
| 284 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 285 | if (secret && secret_len) |
| 286 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 287 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 288 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 289 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 293 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 294 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 295 | const struct list *l = a2; |
| 296 | |
| 297 | if (l) { |
| 298 | const struct quic_frame *frm; |
| 299 | list_for_each_entry(frm, l, list) |
| 300 | chunk_frm_appendf(&trace_buf, frm); |
| 301 | } |
| 302 | } |
| 303 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 304 | 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] | 305 | const struct quic_tx_packet *pkt = a2; |
| 306 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 307 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 308 | |
| 309 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 310 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 311 | 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] | 312 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 313 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 314 | (unsigned long long)qc->path->cwnd, |
| 315 | (unsigned long long)qc->path->prep_in_flight, |
| 316 | (unsigned long long)qc->path->in_flight, |
| 317 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 318 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 319 | } |
| 320 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 321 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 322 | if (pkt->pn_node.key != (uint64_t)-1) |
| 323 | 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] | 324 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 325 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 326 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 327 | (unsigned long long)qc->rx.bytes, |
| 328 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | if (room) { |
| 332 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 333 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 334 | (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] | 335 | } |
| 336 | } |
| 337 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 338 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 339 | const enum quic_handshake_state *state = a2; |
| 340 | const int *err = a3; |
| 341 | |
| 342 | if (state) |
| 343 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 344 | if (err) |
| 345 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 346 | } |
| 347 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 348 | 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] | 349 | const struct quic_rx_packet *pkt = a2; |
| 350 | const unsigned long *pktlen = a3; |
| 351 | const SSL *ssl = a4; |
| 352 | |
| 353 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 354 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 355 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 356 | chunk_appendf(&trace_buf, " kp=%d", |
| 357 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 358 | chunk_appendf(&trace_buf, " el=%c", |
| 359 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 360 | if (pkt->pnl) |
| 361 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 362 | (unsigned long long)pkt->pn); |
| 363 | if (pkt->token_len) |
| 364 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 365 | (unsigned long long)pkt->token_len); |
| 366 | if (pkt->aad_len) |
| 367 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 368 | (unsigned long long)pkt->aad_len); |
| 369 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 370 | pkt->flags, (unsigned long long)pkt->len); |
| 371 | } |
| 372 | if (pktlen) |
| 373 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 374 | if (ssl) { |
| 375 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 376 | chunk_appendf(&trace_buf, " el=%c", |
| 377 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 382 | const struct quic_rx_packet *pkt = a2; |
| 383 | const struct quic_rx_crypto_frm *cf = a3; |
| 384 | const SSL *ssl = a4; |
| 385 | |
| 386 | if (pkt) |
| 387 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 388 | quic_packet_type_enc_level_char(pkt->type), |
| 389 | (unsigned long long)pkt->pn); |
| 390 | if (cf) |
| 391 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 392 | (unsigned long long)cf->offset_node.key, |
| 393 | (unsigned long long)cf->len); |
| 394 | if (ssl) { |
| 395 | 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] | 396 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 397 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 398 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 399 | |
| 400 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 401 | 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] | 402 | } |
| 403 | |
| 404 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 405 | const struct quic_frame *frm = a2; |
| 406 | |
| 407 | if (frm) |
| 408 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 409 | } |
| 410 | |
| 411 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 412 | const struct quic_enc_level *qel = a2; |
| 413 | |
| 414 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 415 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 416 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 417 | " 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] | 418 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 419 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 420 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 421 | (unsigned long long)qc->path->cwnd, |
| 422 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 423 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 424 | (unsigned long long)pktns->tx.in_flight, |
| 425 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 429 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 430 | const struct enc_debug_info *edi = a2; |
| 431 | |
| 432 | if (edi) |
| 433 | chunk_appendf(&trace_buf, |
| 434 | " payload=@%p payload_len=%llu" |
| 435 | " aad=@%p aad_len=%llu pn=%llu", |
| 436 | edi->payload, (unsigned long long)edi->payload_len, |
| 437 | edi->aad, (unsigned long long)edi->aad_len, |
| 438 | (unsigned long long)edi->pn); |
| 439 | } |
| 440 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 441 | if (mask & QUIC_EV_CONN_RMHP) { |
| 442 | const struct quic_rx_packet *pkt = a2; |
| 443 | |
| 444 | if (pkt) { |
| 445 | const int *ret = a3; |
| 446 | |
| 447 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 448 | if (ret && *ret) |
| 449 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 450 | pkt->pnl, (unsigned long long)pkt->pn); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 455 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 456 | const unsigned long *val1 = a3; |
| 457 | const unsigned long *val2 = a4; |
| 458 | |
| 459 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 460 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 461 | if (val1) |
| 462 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 463 | if (val2) |
| 464 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 465 | } |
| 466 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 467 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 468 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 469 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 470 | |
| 471 | if (s) |
| 472 | 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] | 473 | if (stream) |
| 474 | 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] | 475 | } |
| 476 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 477 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 478 | const unsigned int *rtt_sample = a2; |
| 479 | const unsigned int *ack_delay = a3; |
| 480 | const struct quic_loss *ql = a4; |
| 481 | |
| 482 | if (rtt_sample) |
| 483 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 484 | if (ack_delay) |
| 485 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 486 | if (ql) |
| 487 | chunk_appendf(&trace_buf, |
| 488 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 489 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 490 | } |
| 491 | if (mask & QUIC_EV_CONN_CC) { |
| 492 | const struct quic_cc_event *ev = a2; |
| 493 | const struct quic_cc *cc = a3; |
| 494 | |
| 495 | if (a2) |
| 496 | quic_cc_event_trace(&trace_buf, ev); |
| 497 | if (a3) |
| 498 | quic_cc_state_trace(&trace_buf, cc); |
| 499 | } |
| 500 | |
| 501 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 502 | const struct quic_pktns *pktns = a2; |
| 503 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 504 | |
| 505 | if (pktns) { |
| 506 | chunk_appendf(&trace_buf, " pktns=%s", |
| 507 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 508 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 509 | if (pktns->tx.loss_time) |
| 510 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 511 | 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] | 512 | } |
| 513 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 514 | struct quic_tx_packet *pkt; |
| 515 | |
| 516 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 517 | list_for_each_entry(pkt, lost_pkts, list) |
| 518 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | 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] | 523 | const struct quic_pktns *pktns = a2; |
| 524 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 525 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 526 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 527 | if (ifae_pkts) |
| 528 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 529 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 530 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 531 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 532 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 533 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 534 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 535 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 536 | if (pktns->tx.in_flight) |
| 537 | 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] | 538 | if (pktns->tx.loss_time) |
| 539 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 540 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 541 | } |
| 542 | if (mask & QUIC_EV_CONN_SPTO) { |
| 543 | if (pktns->tx.time_of_last_eliciting) |
| 544 | chunk_appendf(&trace_buf, " tole=%dms", |
| 545 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 546 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 547 | 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] | 548 | } |
| 549 | } |
| 550 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 551 | 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] | 552 | chunk_appendf(&trace_buf, |
| 553 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 558 | const struct quic_tx_packet *pkt = a2; |
| 559 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 560 | 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] | 561 | (unsigned long long)qc->path->cwnd, |
| 562 | (unsigned long long)qc->path->prep_in_flight, |
| 563 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 564 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 565 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 566 | (unsigned long)pkt->pn_node.key, |
| 567 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 568 | 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] | 569 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 570 | } |
| 571 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 572 | |
| 573 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 574 | const uint8_t *alert = a2; |
| 575 | const enum ssl_encryption_level_t *level = a3; |
| 576 | |
| 577 | if (alert) |
| 578 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 579 | if (level) |
| 580 | chunk_appendf(&trace_buf, " el=%c", |
| 581 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 582 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 583 | |
| 584 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 585 | const size_t *sz1 = a2; |
| 586 | const size_t *sz2 = a3; |
| 587 | const size_t *sz3 = a4; |
| 588 | |
| 589 | if (sz1) |
| 590 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 591 | if (sz2) |
| 592 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 593 | if (sz3) |
| 594 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 595 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 596 | |
| 597 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 598 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 599 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 600 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 601 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 602 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 603 | } |
| 604 | if (mask & QUIC_EV_CONN_LPKT) { |
| 605 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 606 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 607 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 608 | if (pkt) { |
| 609 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 610 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 611 | if (pkt->pn_node.key != (uint64_t)-1) |
| 612 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 613 | } |
| 614 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 615 | if (len) |
| 616 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 619 | if (mask & QUIC_EV_STATELESS_RST) { |
| 620 | const struct quic_cid *cid = a2; |
| 621 | |
| 622 | if (cid) |
| 623 | quic_cid_dump(&trace_buf, cid); |
| 624 | } |
| 625 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* 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] | 629 | 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] | 630 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 631 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 632 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 633 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 634 | return 1; |
| 635 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 636 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 637 | 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] | 638 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 639 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 640 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 641 | return 1; |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 647 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 648 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 649 | 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] | 650 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 651 | struct quic_pktns *pktns; |
| 652 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 653 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 654 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 655 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 656 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 657 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 658 | pktns = quic_loss_pktns(qc); |
| 659 | if (tick_isset(pktns->tx.loss_time)) { |
| 660 | qc->timer = pktns->tx.loss_time; |
| 661 | goto out; |
| 662 | } |
| 663 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 664 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 665 | * cancelled for a server which reached the anti-amplification limit. |
| 666 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 667 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 668 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 669 | 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] | 670 | qc->timer = TICK_ETERNITY; |
| 671 | goto out; |
| 672 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 673 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 674 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 675 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 676 | /* Timer cancellation. */ |
| 677 | qc->timer = TICK_ETERNITY; |
| 678 | goto out; |
| 679 | } |
| 680 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 681 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 682 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 683 | if (tick_isset(pto)) |
| 684 | qc->timer = pto; |
| 685 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 686 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 687 | if (tick_is_expired(qc->timer, now_ms)) { |
| 688 | 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] | 689 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 690 | } |
| 691 | else { |
| 692 | 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] | 693 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 694 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 695 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 696 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 699 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 700 | * connection. |
| 701 | * Return 1 if succeeded, 0 if not. |
| 702 | */ |
| 703 | static int quic_tls_key_update(struct quic_conn *qc) |
| 704 | { |
| 705 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 706 | struct quic_tls_secrets *rx, *tx; |
| 707 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 708 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 709 | |
| 710 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 711 | rx = &tls_ctx->rx; |
| 712 | tx = &tls_ctx->tx; |
| 713 | nxt_rx = &qc->ku.nxt_rx; |
| 714 | nxt_tx = &qc->ku.nxt_tx; |
| 715 | |
| 716 | /* Prepare new RX secrets */ |
| 717 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 718 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 719 | 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] | 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 724 | nxt_rx->key, nxt_rx->keylen, |
| 725 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 726 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 727 | 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] | 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* Prepare new TX secrets */ |
| 732 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 733 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 734 | 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] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 739 | nxt_tx->key, nxt_tx->keylen, |
| 740 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 741 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 742 | 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] | 743 | return 0; |
| 744 | } |
| 745 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 746 | if (nxt_rx->ctx) { |
| 747 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 748 | nxt_rx->ctx = NULL; |
| 749 | } |
| 750 | |
| 751 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 752 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | if (nxt_tx->ctx) { |
| 757 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 758 | nxt_tx->ctx = NULL; |
| 759 | } |
| 760 | |
| 761 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 762 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 763 | return 0; |
| 764 | } |
| 765 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 770 | * Must be used after having updated them. |
| 771 | * Always succeeds. |
| 772 | */ |
| 773 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 774 | { |
| 775 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 776 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 777 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 778 | |
| 779 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 780 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 781 | curr_secret = tls_ctx->rx.secret; |
| 782 | curr_iv = tls_ctx->rx.iv; |
| 783 | curr_key = tls_ctx->rx.key; |
| 784 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 785 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 786 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 787 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 788 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 789 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 790 | 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] | 791 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 792 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 793 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 794 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 795 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 796 | qc->ku.prv_rx.secret = curr_secret; |
| 797 | qc->ku.prv_rx.iv = curr_iv; |
| 798 | qc->ku.prv_rx.key = curr_key; |
| 799 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 800 | |
| 801 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 802 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 803 | curr_secret = tls_ctx->tx.secret; |
| 804 | curr_iv = tls_ctx->tx.iv; |
| 805 | curr_key = tls_ctx->tx.key; |
| 806 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 807 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 808 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 809 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 810 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 811 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 812 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 813 | qc->ku.nxt_tx.secret = curr_secret; |
| 814 | qc->ku.nxt_tx.iv = curr_iv; |
| 815 | qc->ku.nxt_tx.key = curr_key; |
| 816 | } |
| 817 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 818 | #ifndef OPENSSL_IS_BORINGSSL |
| 819 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 820 | const uint8_t *read_secret, |
| 821 | const uint8_t *write_secret, size_t secret_len) |
| 822 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 823 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 824 | 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] | 825 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 826 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 827 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 828 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 829 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 830 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 831 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 832 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 833 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 834 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 835 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 836 | 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] | 837 | goto err; |
| 838 | } |
| 839 | |
| 840 | rx = &tls_ctx->rx; |
| 841 | tx = &tls_ctx->tx; |
| 842 | |
| 843 | rx->aead = tx->aead = tls_aead(cipher); |
| 844 | rx->md = tx->md = tls_md(cipher); |
| 845 | rx->hp = tx->hp = tls_hp(cipher); |
| 846 | |
| 847 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 848 | 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] | 849 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 850 | 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] | 851 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 852 | } |
| 853 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 854 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 855 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 856 | goto err; |
| 857 | } |
| 858 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 859 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 860 | * listener. Note that a listener derives only RX secrets for this |
| 861 | * level. |
| 862 | */ |
| 863 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 864 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 865 | |
| 866 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 867 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 868 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 869 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 870 | 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] | 871 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 872 | 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] | 873 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 876 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 877 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 878 | goto err; |
| 879 | } |
| 880 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 881 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 882 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 883 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 884 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 885 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 886 | /* 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] | 887 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 888 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 889 | 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] | 890 | goto err; |
| 891 | } |
| 892 | |
| 893 | memcpy(rx->secret, read_secret, secret_len); |
| 894 | rx->secretlen = secret_len; |
| 895 | memcpy(tx->secret, write_secret, secret_len); |
| 896 | tx->secretlen = secret_len; |
| 897 | /* Initialize all the secret keys lengths */ |
| 898 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 899 | /* Prepare the next key update */ |
| 900 | if (!quic_tls_key_update(qc)) |
| 901 | goto err; |
| 902 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 903 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 904 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 905 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 906 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 907 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 908 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 909 | |
| 910 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 911 | 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] | 912 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 913 | } |
| 914 | #else |
| 915 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 916 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 917 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 918 | */ |
| 919 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 920 | const SSL_CIPHER *cipher, |
| 921 | const uint8_t *secret, size_t secret_len) |
| 922 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 923 | 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] | 924 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 925 | &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] | 926 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 927 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 928 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 929 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 930 | goto out; |
| 931 | } |
| 932 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 933 | tls_ctx->rx.aead = tls_aead(cipher); |
| 934 | tls_ctx->rx.md = tls_md(cipher); |
| 935 | tls_ctx->rx.hp = tls_hp(cipher); |
| 936 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 937 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 938 | goto err; |
| 939 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 940 | 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] | 941 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 942 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 943 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 944 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 945 | 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] | 946 | goto err; |
| 947 | } |
| 948 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 949 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 950 | const unsigned char *buf; |
| 951 | size_t buflen; |
| 952 | |
| 953 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 954 | if (!buflen) |
| 955 | goto err; |
| 956 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 957 | 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] | 958 | goto err; |
| 959 | } |
| 960 | |
| 961 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 962 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 963 | 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] | 964 | |
| 965 | return 1; |
| 966 | |
| 967 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 968 | 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] | 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 973 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 974 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 975 | */ |
| 976 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 977 | const SSL_CIPHER *cipher, |
| 978 | const uint8_t *secret, size_t secret_len) |
| 979 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 980 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 981 | 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] | 982 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 983 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 984 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 985 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 986 | goto out; |
| 987 | } |
| 988 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 989 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 990 | goto err; |
| 991 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 992 | tls_ctx->tx.aead = tls_aead(cipher); |
| 993 | tls_ctx->tx.md = tls_md(cipher); |
| 994 | tls_ctx->tx.hp = tls_hp(cipher); |
| 995 | |
| 996 | 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] | 997 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 998 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 999 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 1000 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1001 | 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] | 1002 | goto err; |
| 1003 | } |
| 1004 | |
| 1005 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1006 | 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] | 1007 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1008 | return 1; |
| 1009 | |
| 1010 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1011 | 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] | 1012 | return 0; |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
| 1016 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1017 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1018 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1019 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 1020 | * store all the data. |
| 1021 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1022 | */ |
| 1023 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 1024 | const unsigned char *data, size_t len) |
| 1025 | { |
| 1026 | struct quic_crypto_buf **qcb; |
| 1027 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1028 | size_t cf_offset, cf_len, *nb_buf; |
| 1029 | unsigned char *pos; |
| 1030 | |
| 1031 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1032 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1033 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1034 | cf_len = len; |
| 1035 | |
| 1036 | while (len) { |
| 1037 | size_t to_copy, room; |
| 1038 | |
| 1039 | pos = (*qcb)->data + (*qcb)->sz; |
| 1040 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1041 | to_copy = len > room ? room : len; |
| 1042 | if (to_copy) { |
| 1043 | memcpy(pos, data, to_copy); |
| 1044 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1045 | qel->tx.crypto.sz += to_copy; |
| 1046 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1047 | len -= to_copy; |
| 1048 | data += to_copy; |
| 1049 | } |
| 1050 | else { |
| 1051 | struct quic_crypto_buf **tmp; |
| 1052 | |
| 1053 | tmp = realloc(qel->tx.crypto.bufs, |
| 1054 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1055 | if (tmp) { |
| 1056 | qel->tx.crypto.bufs = tmp; |
| 1057 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1058 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1059 | if (!*qcb) |
| 1060 | return 0; |
| 1061 | |
| 1062 | (*qcb)->sz = 0; |
| 1063 | ++*nb_buf; |
| 1064 | } |
| 1065 | else { |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1072 | * have been buffered. |
| 1073 | */ |
| 1074 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1075 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1076 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1077 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1078 | /* There is at most one CRYPTO frame in this packet number |
| 1079 | * space. Let's look for it. |
| 1080 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1081 | 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] | 1082 | if (frm->type != QUIC_FT_CRYPTO) |
| 1083 | continue; |
| 1084 | |
| 1085 | /* Found */ |
| 1086 | found = frm; |
| 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | if (found) { |
| 1091 | found->crypto.len += cf_len; |
| 1092 | } |
| 1093 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1094 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1095 | if (!frm) |
| 1096 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1097 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1098 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1099 | frm->type = QUIC_FT_CRYPTO; |
| 1100 | frm->crypto.offset = cf_offset; |
| 1101 | frm->crypto.len = cf_len; |
| 1102 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1103 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1104 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | return len == 0; |
| 1108 | } |
| 1109 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1110 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1111 | * activity for <qc> will be interrupted. |
| 1112 | */ |
| 1113 | void quic_set_connection_close(struct quic_conn *qc, int err) |
| 1114 | { |
| 1115 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1116 | return; |
| 1117 | |
| 1118 | qc->err_code = err; |
| 1119 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1120 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1121 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1122 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1123 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1124 | { |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 1125 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1126 | quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert); |
| 1127 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1128 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1131 | /* Set the application for <qc> QUIC connection. |
| 1132 | * Return 1 if succeeded, 0 if not. |
| 1133 | */ |
| 1134 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1135 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1136 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1137 | qc->app_ops = &h3_ops; |
| 1138 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1139 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1140 | else |
| 1141 | return 0; |
| 1142 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1143 | return 1; |
| 1144 | } |
| 1145 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1146 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1147 | * wants to provide the QUIC layer with CRYPTO data. |
| 1148 | * Returns 1 if succeeded, 0 if not. |
| 1149 | */ |
| 1150 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1151 | const uint8_t *data, size_t len) |
| 1152 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1153 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1154 | enum quic_tls_enc_level tel; |
| 1155 | struct quic_enc_level *qel; |
| 1156 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1157 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1158 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1159 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1160 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1161 | goto out; |
| 1162 | } |
| 1163 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1164 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1165 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1166 | 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] | 1167 | goto err; |
| 1168 | } |
| 1169 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1170 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1171 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1172 | 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] | 1173 | goto err; |
| 1174 | } |
| 1175 | |
| 1176 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1177 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1178 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1179 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1180 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1181 | return 1; |
| 1182 | |
| 1183 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1184 | 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] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | int ha_quic_flush_flight(SSL *ssl) |
| 1189 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1190 | 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] | 1191 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1192 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1193 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1194 | |
| 1195 | return 1; |
| 1196 | } |
| 1197 | |
| 1198 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1199 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1200 | 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] | 1201 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1202 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1203 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1204 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1205 | return 1; |
| 1206 | } |
| 1207 | |
| 1208 | /* QUIC TLS methods */ |
| 1209 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1210 | #ifdef OPENSSL_IS_BORINGSSL |
| 1211 | .set_read_secret = ha_set_rsec, |
| 1212 | .set_write_secret = ha_set_wsec, |
| 1213 | #else |
| 1214 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1215 | #endif |
| 1216 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1217 | .flush_flight = ha_quic_flush_flight, |
| 1218 | .send_alert = ha_quic_send_alert, |
| 1219 | }; |
| 1220 | |
| 1221 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1222 | * Returns an error count. |
| 1223 | */ |
| 1224 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1225 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1226 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1227 | int cfgerr = 0; |
| 1228 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1229 | long options = |
| 1230 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1231 | SSL_OP_SINGLE_ECDH_USE | |
| 1232 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1233 | SSL_CTX *ctx; |
| 1234 | |
| 1235 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1236 | bind_conf->initial_ctx = ctx; |
| 1237 | |
| 1238 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1239 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1240 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1241 | 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] | 1242 | |
| 1243 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1244 | #ifdef OPENSSL_IS_BORINGSSL |
| 1245 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1246 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1247 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1248 | if (bind_conf->ssl_conf.early_data) { |
| 1249 | 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] | 1250 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1251 | } |
| 1252 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1253 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1254 | #else |
| 1255 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1256 | #endif |
| 1257 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1258 | #endif |
| 1259 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1260 | |
| 1261 | return cfgerr; |
| 1262 | } |
| 1263 | |
| 1264 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1265 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1266 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1267 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1268 | */ |
| 1269 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1270 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1271 | { |
| 1272 | uint64_t expected_pn = largest_pn + 1; |
| 1273 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1274 | uint64_t pn_hwin = pn_win / 2; |
| 1275 | uint64_t pn_mask = pn_win - 1; |
| 1276 | uint64_t candidate_pn; |
| 1277 | |
| 1278 | |
| 1279 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1280 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1281 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1282 | candidate_pn + pn_hwin <= expected_pn) |
| 1283 | return candidate_pn + pn_win; |
| 1284 | |
| 1285 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1286 | return candidate_pn - pn_win; |
| 1287 | |
| 1288 | return candidate_pn; |
| 1289 | } |
| 1290 | |
| 1291 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1292 | * cryptographic context. |
| 1293 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1294 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1295 | * <end> points to one byte past the end of this packet. |
| 1296 | * Returns 1 if succeeded, 0 if not. |
| 1297 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1298 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1299 | 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] | 1300 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1301 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1302 | { |
| 1303 | int ret, outlen, i, pnlen; |
| 1304 | uint64_t packet_number; |
| 1305 | uint32_t truncated_pn = 0; |
| 1306 | unsigned char mask[5] = {0}; |
| 1307 | unsigned char *sample; |
| 1308 | EVP_CIPHER_CTX *cctx; |
| 1309 | unsigned char *hp_key; |
| 1310 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1311 | /* Check there is enough data in this packet. */ |
| 1312 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1313 | 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] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | cctx = EVP_CIPHER_CTX_new(); |
| 1318 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1319 | 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] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | ret = 0; |
| 1324 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1325 | |
| 1326 | hp_key = tls_ctx->rx.hp_key; |
| 1327 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1328 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1329 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1330 | 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] | 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1335 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1336 | for (i = 0; i < pnlen; i++) { |
| 1337 | pn[i] ^= mask[i + 1]; |
| 1338 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1339 | } |
| 1340 | |
| 1341 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1342 | /* Store remaining information for this unprotected header */ |
| 1343 | pkt->pn = packet_number; |
| 1344 | pkt->pnl = pnlen; |
| 1345 | |
| 1346 | ret = 1; |
| 1347 | |
| 1348 | out: |
| 1349 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1350 | |
| 1351 | return ret; |
| 1352 | } |
| 1353 | |
| 1354 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1355 | * address, with <payload_len> as payload length, <aad> as address of |
| 1356 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1357 | * context. |
| 1358 | * Returns 1 if succeeded, 0 if not. |
| 1359 | */ |
| 1360 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1361 | 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] | 1362 | 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] | 1363 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1364 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1365 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1366 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1367 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1368 | |
| 1369 | 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] | 1370 | 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] | 1371 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | 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] | 1375 | 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] | 1376 | 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] | 1377 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1381 | |
| 1382 | err: |
| 1383 | 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] | 1384 | 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] | 1385 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1389 | * Returns 1 if succeeded, 0 if not. |
| 1390 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1391 | 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] | 1392 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1393 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1394 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1395 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1396 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1397 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1398 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1399 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1400 | |
| 1401 | kp_changed = 0; |
| 1402 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1403 | /* The two tested bits are not at the same position, |
| 1404 | * this is why they are first both inversed. |
| 1405 | */ |
| 1406 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1407 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1408 | /* The lowest packet number of a previous key phase |
| 1409 | * cannot be null if it really stores previous key phase |
| 1410 | * secrets. |
| 1411 | */ |
| 1412 | if (!pkt->qc->ku.prv_rx.pn) |
| 1413 | return 0; |
| 1414 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1415 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1416 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1417 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1418 | } |
| 1419 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1420 | /* Next key phase */ |
| 1421 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1422 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1423 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1424 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1428 | |
| 1429 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1430 | return 0; |
| 1431 | |
| 1432 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1433 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1434 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1435 | if (!ret) |
| 1436 | return 0; |
| 1437 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1438 | /* Update the keys only if the packet decryption succeeded. */ |
| 1439 | if (kp_changed) { |
| 1440 | quic_tls_rotate_keys(pkt->qc); |
| 1441 | /* Toggle the Key Phase bit */ |
| 1442 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1443 | /* Store the lowest packet number received for the current key phase */ |
| 1444 | tls_ctx->rx.pn = pkt->pn; |
| 1445 | /* Prepare the next key update */ |
| 1446 | if (!quic_tls_key_update(pkt->qc)) |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1450 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1451 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1452 | |
| 1453 | return 1; |
| 1454 | } |
| 1455 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1456 | |
| 1457 | /* Remove references to <frm> frame */ |
| 1458 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1459 | { |
| 1460 | uint64_t pn; |
| 1461 | struct quic_frame *f, *tmp; |
| 1462 | |
| 1463 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1464 | pn = f->pkt->pn_node.key; |
| 1465 | f->origin = NULL; |
| 1466 | LIST_DELETE(&f->ref); |
| 1467 | TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1468 | } |
| 1469 | } |
| 1470 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1471 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1472 | 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] | 1473 | { |
| 1474 | uint64_t pn; |
| 1475 | struct quic_frame *origin, *f, *tmp; |
| 1476 | |
| 1477 | /* Identify this frame: a frame copy or one of its copies */ |
| 1478 | origin = frm->origin ? frm->origin : frm; |
| 1479 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1480 | * possibly a copy of <origin> |
| 1481 | */ |
| 1482 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1483 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1484 | * not release the packets (releasing the frames) at this time as |
| 1485 | * they are possibly also to be acknowledged alongside the |
| 1486 | * the current one. |
| 1487 | */ |
| 1488 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1489 | pn = f->pkt->pn_node.key; |
| 1490 | TRACE_PROTO("mark frame as acked from packet", |
| 1491 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1492 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1493 | f->origin = NULL; |
| 1494 | LIST_DELETE(&f->ref); |
| 1495 | } |
| 1496 | LIST_DELETE(&frm->list); |
| 1497 | pn = frm->pkt->pn_node.key; |
| 1498 | quic_tx_packet_refdec(frm->pkt); |
| 1499 | TRACE_PROTO("freeing frame from packet", |
| 1500 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1501 | pool_free(pool_head_quic_frame, frm); |
| 1502 | } |
| 1503 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1504 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1505 | * |
| 1506 | * 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] | 1507 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1508 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1509 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1510 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1511 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1512 | struct eb64_node *frm_node; |
| 1513 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1514 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1515 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1516 | while (frm_node) { |
| 1517 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1518 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1519 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1520 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1521 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1522 | offset = strm->offset.key; |
| 1523 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1524 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1525 | if (offset > stream->ack_offset) |
| 1526 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1527 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1528 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1529 | /* cf. next comment : frame may be freed at this stage. */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1530 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1531 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1532 | ret = 1; |
| 1533 | } |
| 1534 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1535 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1536 | * has been freed. with the stream frames tree. Nothing to do |
| 1537 | * anymore in here. |
| 1538 | */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1539 | if (!stream) |
| 1540 | return 1; |
| 1541 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1542 | frm_node = eb64_next(frm_node); |
| 1543 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1544 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1545 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1546 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1547 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1548 | |
| 1549 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1552 | /* 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] | 1553 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1554 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1555 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1556 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1557 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1558 | 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] | 1559 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1560 | switch (frm->type) { |
| 1561 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1562 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1563 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1564 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1565 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1566 | const size_t offset = strm_frm->offset.key; |
| 1567 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1568 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1569 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1570 | * 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] | 1571 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1572 | * |
| 1573 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1574 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1575 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1576 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1577 | if (!node) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1578 | 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] | 1579 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1580 | /* early return */ |
| 1581 | return; |
| 1582 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1583 | 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] | 1584 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1585 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1586 | if (offset <= stream->ack_offset) { |
| 1587 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1588 | stream_acked = 1; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1589 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1590 | qc, strm_frm, stream); |
| 1591 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1592 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1593 | if (!stream) { |
| 1594 | /* no need to continue if stream freed. */ |
| 1595 | 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] | 1596 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1597 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1598 | } |
| 1599 | |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1600 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1601 | qc, strm_frm, stream); |
| 1602 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1603 | } |
| 1604 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1605 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1606 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1607 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1608 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1609 | } |
| 1610 | break; |
| 1611 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1612 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1613 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1614 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1615 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1616 | struct qcc *qcc = qc->qcc; |
| 1617 | |
| 1618 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1619 | tasklet_wakeup(qcc->subs->tasklet); |
| 1620 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1621 | if (!qcc->subs->events) |
| 1622 | qcc->subs = NULL; |
| 1623 | } |
| 1624 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1628 | * deallocating them, and their TX frames. |
| 1629 | * Returns the last node reached to be used for the next range. |
| 1630 | * May be NULL if <largest> node could not be found. |
| 1631 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1632 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1633 | struct eb_root *pkts, |
| 1634 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1635 | struct list *newly_acked_pkts, |
| 1636 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1637 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1638 | { |
| 1639 | struct eb64_node *node; |
| 1640 | struct quic_tx_packet *pkt; |
| 1641 | |
| 1642 | if (largest_node) |
| 1643 | node = largest_node; |
| 1644 | else { |
| 1645 | node = eb64_lookup(pkts, largest); |
| 1646 | while (!node && largest > smallest) { |
| 1647 | node = eb64_lookup(pkts, --largest); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1652 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1653 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1654 | 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] | 1655 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1656 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1657 | 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] | 1658 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1659 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1660 | node = eb64_prev(node); |
| 1661 | eb64_delete(&pkt->pn_node); |
| 1662 | } |
| 1663 | |
| 1664 | return node; |
| 1665 | } |
| 1666 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1667 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1668 | * 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] | 1669 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1670 | 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] | 1671 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1672 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1673 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1674 | struct quic_frame *frm, *frmbak; |
| 1675 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1676 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1677 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1678 | 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] | 1679 | /* Only for debug */ |
| 1680 | uint64_t pn; |
| 1681 | |
| 1682 | /* 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] | 1683 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1684 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1685 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1686 | /* 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] | 1687 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1688 | /* Remove any reference to this frame */ |
| 1689 | qc_frm_unref(qc, frm); |
| 1690 | switch (frm->type) { |
| 1691 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1692 | { |
| 1693 | struct quic_stream *strm_frm = &frm->stream; |
| 1694 | struct eb64_node *node = NULL; |
| 1695 | struct qc_stream_desc *stream_desc; |
| 1696 | |
| 1697 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1698 | if (!node) { |
| 1699 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 1700 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1701 | qc, frm, &pn); |
| 1702 | pool_free(pool_head_quic_frame, frm); |
| 1703 | continue; |
| 1704 | } |
| 1705 | |
| 1706 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1707 | /* Do not resend this frame if in the "already acked range" */ |
| 1708 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1709 | TRACE_PROTO("ignored frame in already acked range", |
| 1710 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1711 | continue; |
| 1712 | } |
| 1713 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1714 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1715 | TRACE_PROTO("updated partially acked frame", |
| 1716 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1717 | } |
| 1718 | |
| 1719 | break; |
| 1720 | } |
| 1721 | |
| 1722 | default: |
| 1723 | break; |
| 1724 | } |
| 1725 | |
| 1726 | /* Do not resend probing packet with old data */ |
| 1727 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1728 | TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1729 | qc, frm, &pn); |
| 1730 | if (frm->origin) |
| 1731 | LIST_DELETE(&frm->ref); |
| 1732 | pool_free(pool_head_quic_frame, frm); |
| 1733 | continue; |
| 1734 | } |
| 1735 | |
| 1736 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1737 | TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1738 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1739 | qc, frm, &pn); |
| 1740 | pool_free(pool_head_quic_frame, frm); |
| 1741 | } |
| 1742 | else { |
| 1743 | 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] | 1744 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1745 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1746 | * will be performed to check the stream is not consumed or released. |
| 1747 | */ |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1748 | frm->flags = QUIC_FL_TX_FRAME_LOST; |
| 1749 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1750 | LIST_APPEND(&tmp, &frm->list); |
| 1751 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1754 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1755 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1756 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1757 | /* Free <pkt> TX packet and its attached frames. |
| 1758 | * This is the responsability of the caller to remove this packet of |
| 1759 | * any data structure it was possibly attached to. |
| 1760 | */ |
| 1761 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1762 | { |
| 1763 | struct quic_frame *frm, *frmbak; |
| 1764 | |
| 1765 | if (!pkt) |
| 1766 | return; |
| 1767 | |
| 1768 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1769 | LIST_DELETE(&frm->list); |
| 1770 | pool_free(pool_head_quic_frame, frm); |
| 1771 | } |
| 1772 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1773 | } |
| 1774 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1775 | /* Free the TX packets of <pkts> list */ |
| 1776 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1777 | { |
| 1778 | struct quic_tx_packet *pkt, *tmp; |
| 1779 | |
| 1780 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1781 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1782 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1783 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1787 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1788 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1789 | * updating the range which contains <largest_acked_pn>. |
| 1790 | * Never fails. |
| 1791 | */ |
| 1792 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1793 | int64_t largest_acked_pn) |
| 1794 | { |
| 1795 | struct eb64_node *ar, *next_ar; |
| 1796 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1797 | |
| 1798 | ar = eb64_first(&arngs->root); |
| 1799 | while (ar) { |
| 1800 | struct quic_arng_node *ar_node; |
| 1801 | |
| 1802 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1803 | 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] | 1804 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1805 | break; |
| 1806 | |
| 1807 | if (largest_acked_pn < ar_node->last) { |
| 1808 | eb64_delete(ar); |
| 1809 | ar_node->first.key = largest_acked_pn + 1; |
| 1810 | eb64_insert(&arngs->root, ar); |
| 1811 | break; |
| 1812 | } |
| 1813 | |
| 1814 | eb64_delete(ar); |
| 1815 | pool_free(pool_head_quic_arng, ar_node); |
| 1816 | arngs->sz--; |
| 1817 | ar = next_ar; |
| 1818 | } |
| 1819 | } |
| 1820 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1821 | /* Send a packet ack event nofication for each newly acked packet of |
| 1822 | * <newly_acked_pkts> list and free them. |
| 1823 | * Always succeeds. |
| 1824 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1825 | 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] | 1826 | struct list *newly_acked_pkts) |
| 1827 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1828 | struct quic_tx_packet *pkt, *tmp; |
| 1829 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1830 | |
| 1831 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1832 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1833 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1834 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1835 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1836 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1837 | /* If this packet contained an ACK frame, proceed to the |
| 1838 | * acknowledging of range of acks from the largest acknowledged |
| 1839 | * packet number which was sent in an ACK frame by this packet. |
| 1840 | */ |
| 1841 | if (pkt->largest_acked_pn != -1) |
| 1842 | 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] | 1843 | ev.ack.acked = pkt->in_flight_len; |
| 1844 | ev.ack.time_sent = pkt->time_sent; |
| 1845 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1846 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1847 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1848 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | } |
| 1852 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1853 | /* Release all the frames attached to <pktns> packet number space */ |
| 1854 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1855 | { |
| 1856 | struct quic_frame *frm, *frmbak; |
| 1857 | |
| 1858 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1859 | LIST_DELETE(&frm->list); |
| 1860 | pool_free(pool_head_quic_frame, frm); |
| 1861 | } |
| 1862 | } |
| 1863 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1864 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1865 | * their TX frames. |
| 1866 | * Send a packet loss event to the congestion controller if |
| 1867 | * in flight packet have been lost. |
| 1868 | * Also frees the packet in <pkts> list. |
| 1869 | * Never fails. |
| 1870 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1871 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1872 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1873 | struct list *pkts, |
| 1874 | uint64_t now_us) |
| 1875 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1876 | 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] | 1877 | uint64_t lost_bytes; |
| 1878 | |
| 1879 | lost_bytes = 0; |
| 1880 | oldest_lost = newest_lost = NULL; |
| 1881 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1882 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1883 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1884 | lost_bytes += pkt->in_flight_len; |
| 1885 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1886 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1887 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1888 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1889 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1890 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1891 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1892 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1893 | if (!oldest_lost) { |
| 1894 | oldest_lost = newest_lost = pkt; |
| 1895 | } |
| 1896 | else { |
| 1897 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1898 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1899 | newest_lost = pkt; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1903 | if (newest_lost) { |
| 1904 | /* Sent a congestion event to the controller */ |
| 1905 | struct quic_cc_event ev = { |
| 1906 | .type = QUIC_CC_EVT_LOSS, |
| 1907 | .loss.time_sent = newest_lost->time_sent, |
| 1908 | }; |
| 1909 | |
| 1910 | quic_cc_event(&qc->path->cc, &ev); |
| 1911 | } |
| 1912 | |
| 1913 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1914 | * We must check if we are experiencing a persistent congestion. |
| 1915 | * If this is the case, the congestion controller must re-enter |
| 1916 | * slow start state. |
| 1917 | */ |
| 1918 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1919 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1920 | |
| 1921 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1922 | now_ms, qc->max_ack_delay)) |
| 1923 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1924 | } |
| 1925 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1926 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1927 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1928 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1929 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1933 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1934 | * 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] | 1935 | * 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] | 1936 | * acked ack-eliciting packet. |
| 1937 | * Return 1, if succeeded, 0 if not. |
| 1938 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1939 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1940 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1941 | struct quic_enc_level *qel, |
| 1942 | unsigned int *rtt_sample, |
| 1943 | const unsigned char **pos, const unsigned char *end) |
| 1944 | { |
| 1945 | struct quic_ack *ack = &frm->ack; |
| 1946 | uint64_t smallest, largest; |
| 1947 | struct eb_root *pkts; |
| 1948 | struct eb64_node *largest_node; |
| 1949 | unsigned int time_sent, pkt_flags; |
| 1950 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1951 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1952 | |
| 1953 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1954 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1955 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1956 | goto err; |
| 1957 | } |
| 1958 | |
| 1959 | if (ack->first_ack_range > ack->largest_ack) { |
| 1960 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1961 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1962 | goto err; |
| 1963 | } |
| 1964 | |
| 1965 | largest = ack->largest_ack; |
| 1966 | smallest = largest - ack->first_ack_range; |
| 1967 | pkts = &qel->pktns->tx.pkts; |
| 1968 | pkt_flags = 0; |
| 1969 | largest_node = NULL; |
| 1970 | time_sent = 0; |
| 1971 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1972 | 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] | 1973 | largest_node = eb64_lookup(pkts, largest); |
| 1974 | if (!largest_node) { |
| 1975 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1976 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1977 | } |
| 1978 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1979 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1980 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1981 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1985 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1986 | do { |
| 1987 | uint64_t gap, ack_range; |
| 1988 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1989 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1990 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1991 | if (!ack->ack_range_num--) |
| 1992 | break; |
| 1993 | |
| 1994 | if (!quic_dec_int(&gap, pos, end)) |
| 1995 | goto err; |
| 1996 | |
| 1997 | if (smallest < gap + 2) { |
| 1998 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1999 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2000 | goto err; |
| 2001 | } |
| 2002 | |
| 2003 | largest = smallest - gap - 2; |
| 2004 | if (!quic_dec_int(&ack_range, pos, end)) |
| 2005 | goto err; |
| 2006 | |
| 2007 | if (largest < ack_range) { |
| 2008 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2009 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2010 | goto err; |
| 2011 | } |
| 2012 | |
| 2013 | /* Do not use this node anymore. */ |
| 2014 | largest_node = NULL; |
| 2015 | /* Next range */ |
| 2016 | smallest = largest - ack_range; |
| 2017 | |
| 2018 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2019 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2020 | } while (1); |
| 2021 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2022 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2023 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2024 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2028 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2029 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2030 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2031 | 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] | 2032 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2033 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2034 | if (quic_peer_validated_addr(qc)) |
| 2035 | qc->path->loss.pto_count = 0; |
| 2036 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | return 1; |
| 2041 | |
| 2042 | err: |
| 2043 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2044 | 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] | 2045 | return 0; |
| 2046 | } |
| 2047 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2048 | /* This function gives the detail of the SSL error. It is used only |
| 2049 | * if the debug mode and the verbose mode are activated. It dump all |
| 2050 | * the SSL error until the stack was empty. |
| 2051 | */ |
| 2052 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2053 | { |
| 2054 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2055 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2056 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2057 | unsigned long ret; |
| 2058 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2059 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2060 | ret = ERR_get_error(); |
| 2061 | if (!ret) |
| 2062 | return; |
| 2063 | |
| 2064 | 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] | 2065 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2070 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2071 | const char **str, int *len); |
| 2072 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2073 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2074 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2075 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2076 | * Return 1 if succeeded, 0 if not. |
| 2077 | */ |
| 2078 | 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] | 2079 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2080 | const unsigned char *data, size_t len, |
| 2081 | struct quic_rx_packet *pkt, |
| 2082 | struct quic_rx_crypto_frm *cf) |
| 2083 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2084 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2085 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2086 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2087 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2088 | qc = ctx->qc; |
| 2089 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2090 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2091 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2092 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2093 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2094 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2095 | goto err; |
| 2096 | } |
| 2097 | |
| 2098 | el->rx.crypto.offset += len; |
| 2099 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2100 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2102 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2103 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2104 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2105 | if (ssl_err != 1) { |
| 2106 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2107 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2108 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2109 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2110 | goto out; |
| 2111 | } |
| 2112 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2113 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2114 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2115 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2116 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2117 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2118 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2119 | goto err; |
| 2120 | } |
| 2121 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2122 | 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] | 2123 | |
| 2124 | /* Check the alpn could be negotiated */ |
| 2125 | if (!qc->app_ops) { |
| 2126 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2127 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2128 | goto err; |
| 2129 | } |
| 2130 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2131 | HA_ATOMIC_DEC(&qc->prx_counters->conn_opening); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2132 | /* I/O callback switch */ |
| 2133 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2134 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2135 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2136 | /* The connection is ready to be accepted. */ |
| 2137 | quic_accept_push_qc(qc); |
| 2138 | } |
| 2139 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2140 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2141 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2142 | } else { |
| 2143 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2144 | if (ssl_err != 1) { |
| 2145 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2146 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2147 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2148 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2149 | goto out; |
| 2150 | } |
| 2151 | |
| 2152 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2153 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2154 | goto err; |
| 2155 | } |
| 2156 | |
| 2157 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2158 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2159 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2160 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2161 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2162 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2163 | return 1; |
| 2164 | |
| 2165 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2166 | 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] | 2167 | return 0; |
| 2168 | } |
| 2169 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2170 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2171 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2172 | * If not, the STREAM frame is stored to be treated again later. |
| 2173 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2174 | * Return 1 if succeeded, 0 if not. |
| 2175 | */ |
| 2176 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2177 | struct quic_stream *strm_frm, |
| 2178 | struct quic_conn *qc) |
| 2179 | { |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2180 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2181 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2182 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2183 | strm_frm->offset.key, strm_frm->fin, |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 2184 | (char *)strm_frm->data); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2185 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 2186 | /* frame rejected - packet must not be acknowledeged */ |
| 2187 | if (ret) |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2188 | return 0; |
| 2189 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2190 | return 1; |
| 2191 | } |
| 2192 | |
| 2193 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2194 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2195 | * If not, the STREAM frame is stored to be treated again later. |
| 2196 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2197 | * Return 1 if succeeded, 0 if not. |
| 2198 | */ |
| 2199 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2200 | struct quic_stream *strm_frm, |
| 2201 | struct quic_conn *qc) |
| 2202 | { |
| 2203 | struct qcs *strm; |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2204 | enum ncb_ret ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2205 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2206 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2207 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2208 | 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] | 2209 | return 0; |
| 2210 | } |
| 2211 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2212 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2213 | size_t diff; |
| 2214 | |
| 2215 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2216 | TRACE_PROTO("Already received STREAM data", |
| 2217 | QUIC_EV_CONN_PSTRM, qc); |
| 2218 | goto out; |
| 2219 | } |
| 2220 | |
| 2221 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2222 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2223 | strm_frm->offset.key = strm->rx.offset; |
| 2224 | strm_frm->len -= diff; |
| 2225 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2226 | } |
| 2227 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2228 | qc_get_ncbuf(strm, &strm->rx.ncbuf); |
| 2229 | if (ncb_is_null(&strm->rx.ncbuf)) |
| 2230 | return 0; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2231 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2232 | ret = ncb_add(&strm->rx.ncbuf, strm_frm->offset.key - strm->rx.offset, |
| 2233 | (char *)strm_frm->data, strm_frm->len, NCB_ADD_COMPARE); |
| 2234 | if (ret != NCB_RET_OK) |
| 2235 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2236 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2237 | /* Inform the application of the arrival of this new stream */ |
| 2238 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
| 2239 | 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] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2243 | qcs_notify_recv(strm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2244 | |
| 2245 | out: |
| 2246 | return 1; |
| 2247 | } |
| 2248 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2249 | /* Returns 1 on success or 0 on error. On error, the packet containing the |
| 2250 | * frame must not be acknowledged. |
| 2251 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2252 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2253 | struct quic_stream *strm_frm, |
| 2254 | struct quic_conn *qc) |
| 2255 | { |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2256 | /* RFC9000 13.1. Packet Processing |
| 2257 | * |
| 2258 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2259 | * successfully removed and all frames contained in the packet have |
| 2260 | * been processed. For STREAM frames, this means the data has been |
| 2261 | * enqueued in preparation to be received by the application protocol, |
| 2262 | * but it does not require that data be delivered and consumed. |
| 2263 | */ |
| 2264 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2265 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2266 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2267 | else |
| 2268 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2269 | } |
| 2270 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2271 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2272 | * for <qc> QUIC connection. |
| 2273 | * This is a best effort function which never fails even if no memory could be |
| 2274 | * allocated to duplicate these frames. |
| 2275 | */ |
| 2276 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2277 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2278 | { |
| 2279 | struct quic_frame *frm, *frmbak; |
| 2280 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2281 | |
| 2282 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2283 | struct quic_frame *dup_frm, *origin; |
| 2284 | |
| 2285 | switch (frm->type) { |
| 2286 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2287 | { |
| 2288 | struct quic_stream *strm_frm = &frm->stream; |
| 2289 | struct eb64_node *node = NULL; |
| 2290 | struct qc_stream_desc *stream_desc; |
| 2291 | |
| 2292 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2293 | if (!node) { |
| 2294 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 2295 | continue; |
| 2296 | } |
| 2297 | |
| 2298 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2299 | /* Do not resend this frame if in the "already acked range" */ |
| 2300 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2301 | TRACE_PROTO("ignored frame frame in already acked range", |
| 2302 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2303 | continue; |
| 2304 | } |
| 2305 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2306 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2307 | TRACE_PROTO("updated partially acked frame", |
| 2308 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2309 | } |
| 2310 | |
| 2311 | break; |
| 2312 | } |
| 2313 | |
| 2314 | default: |
| 2315 | break; |
| 2316 | } |
| 2317 | |
| 2318 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2319 | if (!dup_frm) { |
| 2320 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2321 | break; |
| 2322 | } |
| 2323 | |
| 2324 | /* If <frm> is already a copy of another frame, we must take |
| 2325 | * its original frame as source for the copy. |
| 2326 | */ |
| 2327 | origin = frm->origin ? frm->origin : frm; |
| 2328 | TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2329 | *dup_frm = *origin; |
| 2330 | LIST_INIT(&dup_frm->reflist); |
| 2331 | TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM, |
| 2332 | qc, NULL, &origin->pkt->pn_node.key); |
| 2333 | dup_frm->origin = origin; |
| 2334 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2335 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2336 | } |
| 2337 | |
| 2338 | LIST_SPLICE(out_frm_list, &tmp); |
| 2339 | } |
| 2340 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2341 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2342 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2343 | struct quic_enc_level *qel, |
| 2344 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2345 | { |
| 2346 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2347 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2348 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2349 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2350 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2351 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2352 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2353 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2354 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2355 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2356 | node = eb64_next(node); |
| 2357 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2358 | 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] | 2359 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2363 | return; |
| 2364 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2365 | /* When building a packet from another one, the field which may increase the |
| 2366 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2367 | */ |
| 2368 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2369 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2370 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2371 | return; |
| 2372 | } |
| 2373 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2374 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2375 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2376 | if (frms == frms1 && frms2) { |
| 2377 | frms = frms2; |
| 2378 | goto start; |
| 2379 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2380 | } |
| 2381 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2382 | /* 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] | 2383 | * 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] | 2384 | * Initial packets send them coalescing with others (Handshake here). |
| 2385 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2386 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2387 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2388 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2389 | { |
| 2390 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2391 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2392 | |
| 2393 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2394 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2395 | struct quic_enc_level *qel = iqel; |
| 2396 | struct eb_root *pkts; |
| 2397 | struct eb64_node *node; |
| 2398 | struct quic_tx_packet *pkt; |
| 2399 | struct list *tmp = &itmp; |
| 2400 | |
| 2401 | start: |
| 2402 | pkt = NULL; |
| 2403 | pkts = &qel->pktns->tx.pkts; |
| 2404 | node = eb64_first(pkts); |
| 2405 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2406 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2407 | 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] | 2408 | 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] | 2409 | break; |
| 2410 | node = eb64_next(node); |
| 2411 | } |
| 2412 | |
| 2413 | if (!pkt) |
| 2414 | goto end; |
| 2415 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2416 | /* When building a packet from another one, the field which may increase the |
| 2417 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2418 | */ |
| 2419 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2420 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2421 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2422 | goto end; |
| 2423 | } |
| 2424 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2425 | qel->pktns->tx.pto_probe += 1; |
| 2426 | requeue: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2427 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2428 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2429 | if (qel == iqel) { |
| 2430 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2431 | pkt = pkt->next; |
| 2432 | tmp = &htmp; |
| 2433 | hqel->pktns->tx.pto_probe += 1; |
| 2434 | goto requeue; |
| 2435 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2436 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2437 | |
| 2438 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2439 | LIST_SPLICE(ifrms, &itmp); |
| 2440 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2443 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2444 | * as I/O handler context and <qel> as encryption level. |
| 2445 | * Returns 1 if succeeded, 0 if failed. |
| 2446 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2447 | 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] | 2448 | struct quic_enc_level *qel) |
| 2449 | { |
| 2450 | struct quic_frame frm; |
| 2451 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2452 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2453 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2454 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2455 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2456 | /* Skip the AAD */ |
| 2457 | pos = pkt->data + pkt->aad_len; |
| 2458 | end = pkt->data + pkt->len; |
| 2459 | |
| 2460 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2461 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2462 | goto err; |
| 2463 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2464 | 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] | 2465 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2466 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2467 | break; |
| 2468 | case QUIC_FT_PING: |
| 2469 | break; |
| 2470 | case QUIC_FT_ACK: |
| 2471 | { |
| 2472 | unsigned int rtt_sample; |
| 2473 | |
| 2474 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2475 | 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] | 2476 | goto err; |
| 2477 | |
| 2478 | if (rtt_sample) { |
| 2479 | unsigned int ack_delay; |
| 2480 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2481 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2482 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2483 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2484 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2485 | 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] | 2486 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2487 | break; |
| 2488 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2489 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2490 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2491 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2492 | { |
| 2493 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2494 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2495 | 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] | 2496 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2497 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2498 | |
| 2499 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2500 | cfdebug.len = frm.crypto.len; |
| 2501 | TRACE_PROTO("CRYPTO data discarded", |
| 2502 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2503 | break; |
| 2504 | } |
| 2505 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2506 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2507 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2508 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2509 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2510 | |
| 2511 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2512 | cfdebug.len = frm.crypto.len; |
| 2513 | /* Nothing to do */ |
| 2514 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2515 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2516 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2517 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2518 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2519 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2520 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2521 | else { |
| 2522 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2523 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2524 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2525 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2526 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2527 | cfdebug.len = frm.crypto.len; |
| 2528 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2529 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2530 | frm.crypto.len -= diff; |
| 2531 | frm.crypto.data += diff; |
| 2532 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2533 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2534 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2535 | |
| 2536 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2537 | /* 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] | 2538 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2539 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2540 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2541 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2542 | if (!qc_provide_cdata(qel, ctx, |
| 2543 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2544 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2545 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2546 | |
| 2547 | break; |
| 2548 | } |
| 2549 | |
| 2550 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2551 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2552 | if (!cf) { |
| 2553 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2554 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2555 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2556 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2557 | |
| 2558 | cf->offset_node.key = frm.crypto.offset; |
| 2559 | cf->len = frm.crypto.len; |
| 2560 | cf->data = frm.crypto.data; |
| 2561 | cf->pkt = pkt; |
| 2562 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2563 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2564 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2565 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2566 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2567 | { |
| 2568 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2569 | 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] | 2570 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2571 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2572 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2573 | goto err; |
| 2574 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2575 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2576 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2577 | /* The upper layer may not be allocated. */ |
| 2578 | if (qc->mux_state != QC_MUX_READY) { |
| 2579 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2580 | TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2581 | break; |
| 2582 | } |
| 2583 | else { |
| 2584 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc); |
| 2585 | goto err; |
| 2586 | } |
| 2587 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2588 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2589 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2590 | goto err; |
| 2591 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2592 | break; |
| 2593 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2594 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2595 | if (qc->mux_state == QC_MUX_READY) { |
| 2596 | struct quic_max_data *data = &frm.max_data; |
| 2597 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2598 | } |
| 2599 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2600 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2601 | if (qc->mux_state == QC_MUX_READY) { |
| 2602 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2603 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2604 | data->max_stream_data); |
| 2605 | } |
| 2606 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2607 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2608 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2609 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2610 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2611 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2612 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2613 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2614 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2615 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2616 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2617 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2618 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2619 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2620 | 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] | 2621 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2622 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2623 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2624 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2625 | break; |
| 2626 | case QUIC_FT_CONNECTION_CLOSE: |
| 2627 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2628 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2629 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2630 | /* RFC 9000 10.2. Immediate Close: |
| 2631 | * The closing and draining connection states exist to ensure |
| 2632 | * that connections close cleanly and that delayed or reordered |
| 2633 | * packets are properly discarded. These states SHOULD persist |
| 2634 | * for at least three times the current PTO interval... |
| 2635 | * |
| 2636 | * Rearm the idle timeout only one time when entering draining |
| 2637 | * state. |
| 2638 | */ |
| 2639 | qc_idle_timer_do_rearm(qc); |
| 2640 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2641 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2642 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2643 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2644 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2645 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2646 | goto err; |
| 2647 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2648 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2649 | break; |
| 2650 | default: |
| 2651 | goto err; |
| 2652 | } |
| 2653 | } |
| 2654 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2655 | /* 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] | 2656 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2657 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2658 | if (fast_retrans) { |
| 2659 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2660 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2661 | |
| 2662 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2663 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2664 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2665 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2666 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2667 | * be discarded. |
| 2668 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2669 | 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] | 2670 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2671 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2672 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2673 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2674 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2675 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2676 | qc_set_timer(ctx->qc); |
| 2677 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2678 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2679 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2680 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2681 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2682 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2683 | } |
| 2684 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2685 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2686 | return 1; |
| 2687 | |
| 2688 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2689 | 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] | 2690 | return 0; |
| 2691 | } |
| 2692 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2693 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2694 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2695 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2696 | * index after having marked the end of written data. This the responsability |
| 2697 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2698 | * data made of a uint16_t null field. |
| 2699 | * |
| 2700 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2701 | * ^ ^ |
| 2702 | * r w |
| 2703 | * |
| 2704 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2705 | * ^ ^ |
| 2706 | * r w |
| 2707 | |
| 2708 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2709 | * ^ |
| 2710 | * (r = w) |
| 2711 | * |
| 2712 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2713 | * ^ ^ |
| 2714 | * w r |
| 2715 | */ |
| 2716 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2717 | { |
| 2718 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2719 | |
| 2720 | /* We can reset the writer index only if in front of the reader index and |
| 2721 | * if the reader index is not null. Resetting the writer when the reader |
| 2722 | * index is null would empty the buffer. |
| 2723 | * XXX Note than the writer index cannot reach the reader index. |
| 2724 | * Only the reader index can reach the writer index. |
| 2725 | */ |
| 2726 | if (rd && rd <= cbuf->wr) { |
| 2727 | /* Mark the end of contiguous data for the reader */ |
| 2728 | write_u16(cb_wr(cbuf), 0); |
| 2729 | cb_add(cbuf, sizeof(uint16_t)); |
| 2730 | cb_wr_reset(cbuf); |
| 2731 | return 1; |
| 2732 | } |
| 2733 | |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2737 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2738 | * 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] | 2739 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2740 | * This function must be called only after having built a correct datagram. |
| 2741 | * Always succeeds. |
| 2742 | */ |
| 2743 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2744 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2745 | { |
| 2746 | write_u16(cb_wr(cbuf), dglen); |
| 2747 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2748 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2749 | } |
| 2750 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2751 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2752 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2753 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2754 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2755 | */ |
| 2756 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2757 | struct quic_enc_level *qel, int cc, int probe) |
| 2758 | { |
| 2759 | unsigned int must_ack = |
| 2760 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2761 | |
| 2762 | /* Do not build any more packet if the TX secrets are not available or |
| 2763 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2764 | * and if there is no more packets to send upon PTO expiration |
| 2765 | * and if there is no more ack-eliciting frames to send or in flight |
| 2766 | * congestion control limit is reached for prepared data |
| 2767 | */ |
| 2768 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2769 | (!cc && !probe && !must_ack && |
| 2770 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2771 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | return 1; |
| 2776 | } |
| 2777 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2778 | /* 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] | 2779 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2780 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2781 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2782 | * 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] | 2783 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2784 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2785 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2786 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2787 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2788 | { |
| 2789 | struct quic_enc_level *qel; |
| 2790 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2791 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2792 | struct quic_tx_packet *pkt; |
| 2793 | size_t total; |
| 2794 | size_t dg_headlen; |
| 2795 | |
| 2796 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2797 | /* Each datagram is prepended with its length followed by the |
| 2798 | * address of the first packet in the datagram. |
| 2799 | */ |
| 2800 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2801 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2802 | total = 0; |
| 2803 | start: |
| 2804 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2805 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2806 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2807 | * to ensure there is enough room to mark the end of prepared |
| 2808 | * contiguous data with a zero length. |
| 2809 | */ |
| 2810 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2811 | 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] | 2812 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2813 | |
| 2814 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2815 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2816 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2817 | /* We do not probe if an immediate close was asked */ |
| 2818 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2819 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2820 | |
| 2821 | 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] | 2822 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2823 | |
| 2824 | /* Leave room for the datagram header */ |
| 2825 | pos += dg_headlen; |
| 2826 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2827 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2828 | } |
| 2829 | else { |
| 2830 | end = pos + qc->path->mtu; |
| 2831 | } |
| 2832 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2833 | 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] | 2834 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2835 | switch (err) { |
| 2836 | case -2: |
| 2837 | goto err; |
| 2838 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2839 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2840 | * MTU, we are here because of the congestion control window. There is |
| 2841 | * no need to try to reuse this buffer. |
| 2842 | */ |
| 2843 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2844 | default: |
| 2845 | break; |
| 2846 | } |
| 2847 | |
| 2848 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2849 | if (!pkt) |
| 2850 | goto err; |
| 2851 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2852 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2853 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2854 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2855 | total += pkt->len; |
| 2856 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2857 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2858 | } |
| 2859 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2860 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2861 | 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] | 2862 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2863 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2864 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | out: |
| 2868 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2869 | return total; |
| 2870 | |
| 2871 | err: |
| 2872 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2873 | return -1; |
| 2874 | } |
| 2875 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2876 | /* 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] | 2877 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2878 | * several packets in the same datagram. A header made of two fields is added |
| 2879 | * to each datagram: the datagram length followed by the address of the first |
| 2880 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2881 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2882 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2883 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2884 | 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] | 2885 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2886 | 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] | 2887 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2888 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2889 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2890 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2891 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2892 | /* length of datagrams */ |
| 2893 | uint16_t dglen; |
| 2894 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2895 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2896 | /* Each datagram is prepended with its length followed by the |
| 2897 | * address of the first packet in the datagram. |
| 2898 | */ |
| 2899 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2900 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2901 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2902 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2903 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2904 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2905 | qel = &qc->els[tel]; |
| 2906 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2907 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2908 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2909 | padding = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2910 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2911 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2912 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2913 | * to ensure there is enough room to mark the end of prepared |
| 2914 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2915 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2916 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2917 | first_pkt = prv_pkt = NULL; |
| 2918 | 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] | 2919 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2920 | enum quic_pkt_type pkt_type; |
| 2921 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2922 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2923 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2924 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2925 | /* We do not probe if an immediate close was asked */ |
| 2926 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2927 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2928 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2929 | 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] | 2930 | if (prv_pkt) |
| 2931 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2932 | /* Let's select the next encryption level */ |
| 2933 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2934 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2935 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2936 | qel = &qc->els[tel]; |
| 2937 | /* Build a new datagram */ |
| 2938 | prv_pkt = NULL; |
| 2939 | continue; |
| 2940 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2941 | break; |
| 2942 | } |
| 2943 | |
| 2944 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2945 | if (!prv_pkt) { |
| 2946 | /* Leave room for the datagram header */ |
| 2947 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2948 | 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] | 2949 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2950 | } |
| 2951 | else { |
| 2952 | end = pos + qc->path->mtu; |
| 2953 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2956 | cur_pkt = qc_build_pkt(&pos, end, qel, frms, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2957 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2958 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2959 | case -2: |
| 2960 | goto err; |
| 2961 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2962 | /* If there was already a correct packet present, set the |
| 2963 | * current datagram as prepared into <cbuf>. |
| 2964 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2965 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2966 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2967 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2968 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2969 | break; |
| 2970 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2971 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2972 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2973 | if (!cur_pkt) |
| 2974 | goto err; |
| 2975 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2976 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2977 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2978 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2979 | total += cur_pkt->len; |
| 2980 | /* keep trace of the first packet in the datagram */ |
| 2981 | if (!first_pkt) |
| 2982 | first_pkt = cur_pkt; |
| 2983 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2984 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2985 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2986 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 2987 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2988 | /* Let's say we have to build a new dgram */ |
| 2989 | prv_pkt = NULL; |
| 2990 | dglen += cur_pkt->len; |
| 2991 | /* Client: discard the Initial encryption keys as soon as |
| 2992 | * a handshake packet could be built. |
| 2993 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2994 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2995 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2996 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2997 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2998 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2999 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 3000 | 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] | 3001 | 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] | 3002 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3003 | } |
| 3004 | /* If the data for the current encryption level have all been sent, |
| 3005 | * select the next level. |
| 3006 | */ |
| 3007 | 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] | 3008 | 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] | 3009 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3010 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3011 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3012 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3013 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3014 | frms = &qc->els[tel].pktns->tx.frms; |
| 3015 | else |
| 3016 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3017 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3018 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3019 | /* If there is data for the next level, do not |
| 3020 | * consume a datagram. |
| 3021 | */ |
| 3022 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3023 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3024 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3025 | /* If we have to build a new datagram, set the current datagram as |
| 3026 | * prepared into <cbuf>. |
| 3027 | */ |
| 3028 | if (!prv_pkt) { |
| 3029 | qc_set_dg(cbuf, dglen, first_pkt); |
| 3030 | first_pkt = NULL; |
| 3031 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3032 | padding = 0; |
| 3033 | } |
| 3034 | 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] | 3035 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3036 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3037 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | stop_build: |
| 3042 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3043 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3044 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3045 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3046 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3050 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3051 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3052 | |
| 3053 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3054 | 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] | 3055 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | /* 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] | 3059 | * 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] | 3060 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3061 | 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] | 3062 | { |
| 3063 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3064 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3065 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3066 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3067 | cbuf = qr->cbuf; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3068 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3069 | while (cb_contig_data(cbuf)) { |
| 3070 | unsigned char *pos; |
| 3071 | struct buffer tmpbuf = { }; |
| 3072 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3073 | uint16_t dglen; |
| 3074 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3075 | unsigned int time_sent; |
| 3076 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3077 | pos = cb_rd(cbuf); |
| 3078 | dglen = read_u16(pos); |
| 3079 | /* End of prepared datagrams. |
| 3080 | * Reset the reader index only if in front of the writer index. |
| 3081 | */ |
| 3082 | if (!dglen) { |
| 3083 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3084 | |
| 3085 | if (wr && wr < cbuf->rd) { |
| 3086 | cb_rd_reset(cbuf); |
| 3087 | continue; |
| 3088 | } |
| 3089 | break; |
| 3090 | } |
| 3091 | |
| 3092 | pos += sizeof dglen; |
| 3093 | first_pkt = read_ptr(pos); |
| 3094 | pos += sizeof first_pkt; |
| 3095 | tmpbuf.area = (char *)pos; |
| 3096 | tmpbuf.size = tmpbuf.data = dglen; |
| 3097 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3098 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3099 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3100 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3101 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3102 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3103 | qc->tx.bytes += tmpbuf.data; |
| 3104 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3105 | |
| 3106 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3107 | pkt->time_sent = time_sent; |
| 3108 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3109 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3110 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3111 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3112 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3113 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3114 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3115 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3116 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3117 | qc_notify_close(qc); |
| 3118 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3119 | /* RFC 9000 10.2. Immediate Close: |
| 3120 | * The closing and draining connection states exist to ensure |
| 3121 | * that connections close cleanly and that delayed or reordered |
| 3122 | * packets are properly discarded. These states SHOULD persist |
| 3123 | * for at least three times the current PTO interval... |
| 3124 | * |
| 3125 | * Rearm the idle timeout only one time when entering closing |
| 3126 | * state. |
| 3127 | */ |
| 3128 | qc_idle_timer_do_rearm(qc); |
| 3129 | if (qc->timer_task) { |
| 3130 | task_destroy(qc->timer_task); |
| 3131 | qc->timer_task = NULL; |
| 3132 | } |
| 3133 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3134 | qc->path->in_flight += pkt->in_flight_len; |
| 3135 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3136 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3137 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3138 | 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] | 3139 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3140 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3141 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3142 | } |
| 3143 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3144 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3145 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3146 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | return 1; |
| 3148 | } |
| 3149 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3150 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3151 | * <salt> salt input. This is the cluster secret which will be derived |
| 3152 | * as HKDF input secret to generate this token. |
| 3153 | * Return 1 if succeeded, 0 if not. |
| 3154 | */ |
| 3155 | static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len, |
| 3156 | const unsigned char *salt, size_t saltlen) |
| 3157 | { |
| 3158 | /* Input secret */ |
| 3159 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3160 | size_t keylen = strlen(global.cluster_secret); |
| 3161 | /* Info */ |
| 3162 | const unsigned char label[] = "stateless token"; |
| 3163 | size_t labellen = sizeof label - 1; |
| 3164 | |
| 3165 | return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3166 | key, keylen, salt, saltlen, label, labellen); |
| 3167 | } |
| 3168 | |
| 3169 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3170 | * Returns 1 if succeeded, 0 if not. |
| 3171 | */ |
| 3172 | static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid) |
| 3173 | { |
| 3174 | if (global.cluster_secret) { |
| 3175 | /* Output secret */ |
| 3176 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3177 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3178 | /* Salt */ |
| 3179 | const unsigned char *cid = quic_cid->cid.data; |
| 3180 | size_t cidlen = quic_cid->cid.len; |
| 3181 | |
| 3182 | return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen); |
| 3183 | } |
| 3184 | else { |
| 3185 | return RAND_bytes(quic_cid->stateless_reset_token, |
| 3186 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3187 | } |
| 3188 | } |
| 3189 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3190 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3191 | * ebtree. |
| 3192 | * |
| 3193 | * The CID is randomly generated in part with the result altered to be |
| 3194 | * associated with the current thread ID. This means this function must only |
| 3195 | * be called by the quic_conn thread. |
| 3196 | * |
| 3197 | * Returns the new CID if succeeded, NULL if not. |
| 3198 | */ |
| 3199 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3200 | struct quic_conn *qc, |
| 3201 | int seq_num) |
| 3202 | { |
| 3203 | struct quic_connection_id *cid; |
| 3204 | |
| 3205 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3206 | if (!cid) |
| 3207 | return NULL; |
| 3208 | |
| 3209 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3210 | 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] | 3211 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3212 | |
| 3213 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3214 | if (quic_stateless_reset_token_init(cid) != 1) |
| 3215 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3216 | |
| 3217 | cid->qc = qc; |
| 3218 | |
| 3219 | cid->seq_num.key = seq_num; |
| 3220 | cid->retire_prior_to = 0; |
| 3221 | /* insert the allocated CID in the quic_conn tree */ |
| 3222 | eb64_insert(root, &cid->seq_num); |
| 3223 | |
| 3224 | return cid; |
| 3225 | |
| 3226 | err: |
| 3227 | pool_free(pool_head_quic_connection_id, cid); |
| 3228 | return NULL; |
| 3229 | } |
| 3230 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3231 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3232 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3233 | * a HANDSHAKE_DONE frame. |
| 3234 | * Return 1 if succeeded, 0 if not. |
| 3235 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3236 | 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] | 3237 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3238 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3239 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3240 | struct quic_frame *frm, *frmbak; |
| 3241 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3242 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3243 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3244 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3245 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3246 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3247 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3248 | if (!frm) |
| 3249 | return 0; |
| 3250 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3251 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3252 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3253 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3254 | } |
| 3255 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3256 | first = 1; |
| 3257 | max = qc->tx.params.active_connection_id_limit; |
| 3258 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3259 | struct quic_connection_id *cid; |
| 3260 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3261 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3262 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3263 | goto err; |
| 3264 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3265 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3266 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3267 | if (!cid) |
| 3268 | goto err; |
| 3269 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3270 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3271 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3272 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3273 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3274 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3275 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3276 | |
| 3277 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3278 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3279 | |
| 3280 | return 1; |
| 3281 | |
| 3282 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3283 | /* free the frames */ |
| 3284 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3285 | pool_free(pool_head_quic_frame, frm); |
| 3286 | |
| 3287 | node = eb64_first(&qc->cids); |
| 3288 | while (node) { |
| 3289 | struct quic_connection_id *cid; |
| 3290 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3291 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3292 | 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] | 3293 | if (cid->seq_num.key >= max) |
| 3294 | break; |
| 3295 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3296 | if (cid->seq_num.key < first) |
| 3297 | continue; |
| 3298 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3299 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3300 | ebmb_delete(&cid->node); |
| 3301 | eb64_delete(&cid->seq_num); |
| 3302 | pool_free(pool_head_quic_connection_id, cid); |
| 3303 | } |
| 3304 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3305 | return 0; |
| 3306 | } |
| 3307 | |
| 3308 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3309 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3310 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3311 | struct eb64_node *n; |
| 3312 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3313 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3314 | n = eb64_first(&arngs->root); |
| 3315 | while (n) { |
| 3316 | struct eb64_node *next; |
| 3317 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3318 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3319 | next = eb64_next(n); |
| 3320 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3321 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3322 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3326 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3327 | * descending order. |
| 3328 | */ |
| 3329 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3330 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3331 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3332 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | |
| 3336 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3337 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3338 | * 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] | 3339 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3340 | 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] | 3341 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3342 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3343 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3344 | last = eb64_last(&arngs->root); |
| 3345 | while (last && arngs->enc_sz > limit) { |
| 3346 | struct quic_arng_node *last_node, *prev_node; |
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 | prev = eb64_prev(last); |
| 3349 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3350 | return 0; |
| 3351 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3352 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3353 | 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] | 3354 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3355 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3356 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3357 | --arngs->sz; |
| 3358 | eb64_delete(last); |
| 3359 | pool_free(pool_head_quic_arng, last); |
| 3360 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | return 1; |
| 3364 | } |
| 3365 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3366 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3367 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3368 | { |
| 3369 | struct eb64_node *node, *next; |
| 3370 | struct quic_arng_node *ar, *ar_next; |
| 3371 | |
| 3372 | node = eb64_last(&arngs->root); |
| 3373 | if (!node) |
| 3374 | return; |
| 3375 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3376 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3377 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3378 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3379 | |
| 3380 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3381 | 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] | 3382 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3383 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3384 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3385 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3389 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3390 | * 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] | 3391 | */ |
| 3392 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3393 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3394 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3395 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3396 | struct quic_arng_node *new_ar; |
| 3397 | |
| 3398 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3399 | if (new_ar) { |
| 3400 | new_ar->first.key = ar->first; |
| 3401 | new_ar->last = ar->last; |
| 3402 | eb64_insert(&arngs->root, &new_ar->first); |
| 3403 | arngs->sz++; |
| 3404 | } |
| 3405 | |
| 3406 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | /* 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] | 3410 | * 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] | 3411 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3412 | * |
| 3413 | * Descending order |
| 3414 | * -------------> |
| 3415 | * range1 range2 |
| 3416 | * ..........|--------|..............|--------| |
| 3417 | * ^ ^ ^ ^ |
| 3418 | * | | | | |
| 3419 | * last1 first1 last2 first2 |
| 3420 | * ..........+--------+--------------+--------+...... |
| 3421 | * diff1 gap12 diff2 |
| 3422 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3423 | * To encode the previous list of ranges we must encode integers as follows in |
| 3424 | * descending order: |
| 3425 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3426 | * with diff1 = last1 - first1 |
| 3427 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3428 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3429 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3431 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3432 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3433 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3434 | struct eb64_node *le; |
| 3435 | struct quic_arng_node *new_node; |
| 3436 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3437 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3438 | new = NULL; |
| 3439 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3440 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3441 | if (!new_node) |
| 3442 | return 0; |
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 | goto out; |
| 3445 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3446 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3447 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3448 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3449 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3450 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3451 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3452 | |
| 3453 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3454 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3455 | else { |
| 3456 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3457 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3458 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3459 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3460 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3461 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3462 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3463 | if (le_ar->last + 1 >= ar->first) { |
| 3464 | le_ar->last = ar->last; |
| 3465 | new = le; |
| 3466 | new_node = le_ar; |
| 3467 | } |
| 3468 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3469 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3470 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3471 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3472 | |
| 3473 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3474 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3475 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3476 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3477 | /* Verify that the new inserted node does not overlap the nodes |
| 3478 | * which follow it. |
| 3479 | */ |
| 3480 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3481 | struct eb64_node *next; |
| 3482 | struct quic_arng_node *next_node; |
| 3483 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3484 | while ((next = eb64_next(new))) { |
| 3485 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3486 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3487 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3488 | break; |
| 3489 | |
| 3490 | if (next_node->last > new_node->last) |
| 3491 | new_node->last = next_node->last; |
| 3492 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3493 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3494 | /* Decrement the size of these ranges. */ |
| 3495 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3496 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3497 | } |
| 3498 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3499 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3500 | quic_arngs_set_enc_sz(arngs); |
| 3501 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3502 | return 1; |
| 3503 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3504 | /* Remove the header protection of packets at <el> encryption level. |
| 3505 | * Always succeeds. |
| 3506 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3507 | 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] | 3508 | { |
| 3509 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3510 | struct quic_rx_packet *pqpkt; |
| 3511 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3512 | struct quic_enc_level *app_qel; |
| 3513 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3514 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3515 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3516 | /* 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] | 3517 | 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] | 3518 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3519 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3520 | goto out; |
| 3521 | } |
| 3522 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3523 | 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] | 3524 | 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] | 3525 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3526 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3527 | 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] | 3528 | /* XXX TO DO XXX */ |
| 3529 | } |
| 3530 | else { |
| 3531 | /* The AAD includes the packet number field */ |
| 3532 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3533 | /* Store the packet into the tree of packets to decrypt. */ |
| 3534 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3535 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3536 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3537 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3538 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3539 | 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] | 3540 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3541 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3542 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3546 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3550 | * Return 1 if succeeded, 0 if not. |
| 3551 | */ |
| 3552 | 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] | 3553 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3554 | { |
| 3555 | struct eb64_node *node; |
| 3556 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3557 | node = eb64_first(&el->rx.crypto.frms); |
| 3558 | while (node) { |
| 3559 | struct quic_rx_crypto_frm *cf; |
| 3560 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3561 | 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] | 3562 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3563 | break; |
| 3564 | |
| 3565 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3566 | goto err; |
| 3567 | |
| 3568 | node = eb64_next(node); |
| 3569 | quic_rx_packet_refdec(cf->pkt); |
| 3570 | eb64_delete(&cf->offset_node); |
| 3571 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3572 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3573 | return 1; |
| 3574 | |
| 3575 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3576 | 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] | 3577 | return 0; |
| 3578 | } |
| 3579 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3580 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3581 | * 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] | 3582 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3583 | * Return 1 if succeeded, 0 if not. |
| 3584 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3585 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3586 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3587 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3588 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3589 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3590 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3591 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3592 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3593 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3594 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3595 | qel = cur_el; |
| 3596 | next_tel: |
| 3597 | if (!qel) |
| 3598 | goto out; |
| 3599 | |
| 3600 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3601 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3602 | while (node) { |
| 3603 | struct quic_rx_packet *pkt; |
| 3604 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3605 | 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] | 3606 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3607 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3608 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3609 | /* Drop the packet */ |
| 3610 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3611 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3612 | } |
| 3613 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3614 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3615 | /* Drop the packet */ |
| 3616 | TRACE_PROTO("packet parsing 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 | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3620 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3621 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3622 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3623 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3624 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3625 | qc_idle_timer_rearm(qc, 1); |
| 3626 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3627 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3628 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3629 | largest_pn_time_received = pkt->time_received; |
| 3630 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3631 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3632 | 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] | 3633 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3634 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3635 | } |
| 3636 | } |
| 3637 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3638 | eb64_delete(&pkt->pn_node); |
| 3639 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3641 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3642 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3643 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3644 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3645 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3646 | /* Update the largest acknowledged packet timestamps */ |
| 3647 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3648 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3649 | } |
| 3650 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3651 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3652 | goto err; |
| 3653 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3654 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3655 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3656 | qel = next_el; |
| 3657 | goto next_tel; |
| 3658 | } |
| 3659 | |
| 3660 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3661 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3662 | return 1; |
| 3663 | |
| 3664 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3665 | 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] | 3666 | return 0; |
| 3667 | } |
| 3668 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3669 | /* Check if it's possible to remove header protection for packets related to |
| 3670 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3671 | * |
| 3672 | * Return true if the operation is possible else false. |
| 3673 | */ |
| 3674 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3675 | { |
| 3676 | enum quic_tls_enc_level tel; |
| 3677 | |
| 3678 | if (!qel) |
| 3679 | return 0; |
| 3680 | |
| 3681 | tel = ssl_to_quic_enc_level(qel->level); |
| 3682 | |
| 3683 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3684 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3685 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3686 | return 0; |
| 3687 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3688 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3689 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3690 | return 0; |
| 3691 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3692 | /* 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] | 3693 | 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] | 3694 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3695 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3696 | |
| 3697 | return 1; |
| 3698 | } |
| 3699 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3700 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3701 | 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] | 3702 | { |
| 3703 | int ret; |
| 3704 | struct qring *qr; |
| 3705 | |
| 3706 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3707 | if (!qr) |
| 3708 | /* Never happens */ |
| 3709 | return 1; |
| 3710 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3711 | if (old_data) |
| 3712 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3713 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3714 | if (ret == -1) |
| 3715 | goto err; |
| 3716 | else if (ret == 0) |
| 3717 | goto out; |
| 3718 | |
| 3719 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3720 | goto err; |
| 3721 | |
| 3722 | out: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3723 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3724 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3725 | return 1; |
| 3726 | |
| 3727 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3728 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3729 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3730 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3731 | return 0; |
| 3732 | } |
| 3733 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3734 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3735 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 3736 | * QUIC connection |
| 3737 | * Returns 1 if succeeded, 0 if not. |
| 3738 | */ |
| 3739 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3740 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3741 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3742 | { |
| 3743 | int ret; |
| 3744 | struct qring *qr; |
| 3745 | |
| 3746 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3747 | if (!qr) |
| 3748 | /* Never happens */ |
| 3749 | return 1; |
| 3750 | |
| 3751 | if (old_data) |
| 3752 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3753 | ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms); |
| 3754 | if (ret == -1) |
| 3755 | goto err; |
| 3756 | else if (ret == 0) |
| 3757 | goto out; |
| 3758 | |
| 3759 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3760 | goto err; |
| 3761 | |
| 3762 | out: |
| 3763 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3764 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3765 | return 1; |
| 3766 | |
| 3767 | err: |
| 3768 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3769 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3770 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3771 | return 0; |
| 3772 | } |
| 3773 | |
| 3774 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3775 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3776 | { |
| 3777 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3778 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3779 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3780 | |
| 3781 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3782 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 3783 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 3784 | |
| 3785 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 3786 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 3787 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3788 | if (!LIST_ISEMPTY(&ifrms)) { |
| 3789 | iqel->pktns->tx.pto_probe = 1; |
| 3790 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3791 | hqel->pktns->tx.pto_probe = 1; |
| 3792 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 3793 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 3794 | } |
| 3795 | } |
| 3796 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3797 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 3798 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3799 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3800 | hqel->pktns->tx.pto_probe = 1; |
| 3801 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 3802 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3803 | } |
| 3804 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3805 | } |
| 3806 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3807 | } |
| 3808 | else { |
| 3809 | int i; |
| 3810 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 3811 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 3812 | |
| 3813 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3814 | hqel->pktns->tx.pto_probe = 0; |
| 3815 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 3816 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 3817 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3818 | if (!LIST_ISEMPTY(&frms1)) { |
| 3819 | hqel->pktns->tx.pto_probe = 1; |
| 3820 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 3821 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3822 | } |
| 3823 | } |
| 3824 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3825 | } |
| 3826 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3827 | aqel->pktns->tx.pto_probe = 0; |
| 3828 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 3829 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3830 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 3831 | if (!LIST_ISEMPTY(&frms1)) { |
| 3832 | aqel->pktns->tx.pto_probe = 1; |
| 3833 | qc_send_app_pkts(qc, 1, &frms1); |
| 3834 | } |
| 3835 | if (!LIST_ISEMPTY(&frms2)) { |
| 3836 | aqel->pktns->tx.pto_probe = 1; |
| 3837 | qc_send_app_pkts(qc, 1, &frms2); |
| 3838 | } |
| 3839 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3844 | /* QUIC connection packet handler task (post handshake) */ |
| 3845 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3846 | { |
| 3847 | struct ssl_sock_ctx *ctx; |
| 3848 | struct quic_conn *qc; |
| 3849 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3850 | |
| 3851 | |
| 3852 | ctx = context; |
| 3853 | qc = ctx->qc; |
| 3854 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3855 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3856 | 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] | 3857 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3858 | /* Retranmissions */ |
| 3859 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3860 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 3861 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3862 | qc_dgrams_retransmit(qc); |
| 3863 | } |
| 3864 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3865 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3866 | qc_rm_hp_pkts(qc, qel); |
| 3867 | |
| 3868 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3869 | goto err; |
| 3870 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3871 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3872 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3873 | goto out; |
| 3874 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3875 | 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] | 3876 | goto err; |
| 3877 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3878 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3879 | return t; |
| 3880 | |
| 3881 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3882 | 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] | 3883 | return t; |
| 3884 | } |
| 3885 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3886 | /* QUIC connection packet handler task. */ |
| 3887 | 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] | 3888 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3889 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3890 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3891 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3892 | enum quic_tls_enc_level tel, next_tel; |
| 3893 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3894 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3895 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3896 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3897 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3898 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3899 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3900 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3901 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3902 | 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] | 3903 | |
| 3904 | /* Retranmissions */ |
| 3905 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3906 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 3907 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3908 | qc_dgrams_retransmit(qc); |
| 3909 | } |
| 3910 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3911 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3912 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3913 | /* The I/O handler has been woken up by the dgram listener |
| 3914 | * after the anti-amplification was reached. |
| 3915 | */ |
| 3916 | qc_set_timer(qc); |
| 3917 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3918 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3919 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3920 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3921 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3922 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3923 | 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] | 3924 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3925 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3926 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3927 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3928 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3929 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3930 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3931 | } |
| 3932 | 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] | 3933 | goto err; |
| 3934 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3935 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3936 | 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] | 3937 | |
| 3938 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3939 | /* Treat packets waiting for header packet protection decryption */ |
| 3940 | 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] | 3941 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3942 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3943 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3944 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3945 | 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] | 3946 | goto err; |
| 3947 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3948 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3949 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3950 | goto out; |
| 3951 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 3952 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
| 3953 | !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
| 3954 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3955 | qel = next_qel; |
| 3956 | next_qel = NULL; |
| 3957 | goto next_level; |
| 3958 | } |
| 3959 | else { |
| 3960 | struct quic_rx_packet *pkt; |
| 3961 | struct mt_list *elt1, elt2; |
| 3962 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 3963 | |
| 3964 | /* Drop these 0-RTT packets */ |
| 3965 | TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3966 | mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) { |
| 3967 | MT_LIST_DELETE_SAFE(elt1); |
| 3968 | quic_rx_packet_refdec(pkt); |
| 3969 | } |
| 3970 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3971 | } |
| 3972 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3973 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3974 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3975 | 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] | 3976 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3977 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3978 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3979 | 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] | 3980 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3981 | /* Discard the Handshake keys. */ |
| 3982 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3983 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3984 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3985 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3986 | 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] | 3987 | 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] | 3988 | } |
| 3989 | |
| 3990 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3991 | /* There may be remaining handshake to build (acks) */ |
| 3992 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3993 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3996 | if (!qr) |
| 3997 | 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] | 3998 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3999 | * be considered. |
| 4000 | */ |
| 4001 | 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] | 4002 | goto err; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 4003 | ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms, |
| 4004 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4005 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4006 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4007 | else if (ret == 0) |
| 4008 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4009 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4010 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4011 | goto err; |
| 4012 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4013 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4014 | /* Check if there is something to do for the next level. |
| 4015 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4016 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4017 | (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] | 4018 | (!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] | 4019 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4020 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4021 | goto next_level; |
| 4022 | } |
| 4023 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4024 | out: |
| 4025 | if (qr) |
| 4026 | 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] | 4027 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4028 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4029 | |
| 4030 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 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_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] | 4034 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4035 | } |
| 4036 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4037 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4038 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 4039 | { |
| 4040 | int i; |
| 4041 | |
| 4042 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4043 | if (qel->tx.crypto.bufs[i]) { |
| 4044 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4045 | qel->tx.crypto.bufs[i] = NULL; |
| 4046 | } |
| 4047 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4048 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4052 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4053 | * Returns 1 if succeeded, 0 if not. |
| 4054 | */ |
| 4055 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4056 | enum quic_tls_enc_level level) |
| 4057 | { |
| 4058 | struct quic_enc_level *qel; |
| 4059 | |
| 4060 | qel = &qc->els[level]; |
| 4061 | qel->level = quic_to_ssl_enc_level(level); |
| 4062 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4063 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4064 | 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] | 4065 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4066 | |
| 4067 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4068 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4069 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4070 | qel->rx.crypto.offset = 0; |
| 4071 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4072 | |
| 4073 | /* Allocate only one buffer. */ |
| 4074 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4075 | if (!qel->tx.crypto.bufs) |
| 4076 | goto err; |
| 4077 | |
| 4078 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4079 | if (!qel->tx.crypto.bufs[0]) |
| 4080 | goto err; |
| 4081 | |
| 4082 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4083 | qel->tx.crypto.nb_buf = 1; |
| 4084 | |
| 4085 | qel->tx.crypto.sz = 0; |
| 4086 | qel->tx.crypto.offset = 0; |
| 4087 | |
| 4088 | return 1; |
| 4089 | |
| 4090 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4091 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4092 | return 0; |
| 4093 | } |
| 4094 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4095 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4096 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4097 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4098 | * This function must only be called by the thread responsible of the quic_conn |
| 4099 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4100 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4101 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4102 | { |
| 4103 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4104 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4105 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4106 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4107 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4108 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4109 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4110 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4111 | /* free remaining stream descriptors */ |
| 4112 | node = eb64_first(&qc->streams_by_id); |
| 4113 | while (node) { |
| 4114 | struct qc_stream_desc *stream; |
| 4115 | |
| 4116 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4117 | node = eb64_next(node); |
| 4118 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4119 | /* all streams attached to the quic-conn are released, so |
| 4120 | * qc_stream_desc_free will liberate the stream instance. |
| 4121 | */ |
| 4122 | BUG_ON(!stream->release); |
| 4123 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4124 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4125 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4126 | if (qc->idle_timer_task) { |
| 4127 | task_destroy(qc->idle_timer_task); |
| 4128 | qc->idle_timer_task = NULL; |
| 4129 | } |
| 4130 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4131 | if (qc->timer_task) { |
| 4132 | task_destroy(qc->timer_task); |
| 4133 | qc->timer_task = NULL; |
| 4134 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4135 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4136 | /* remove the connection from receiver cids trees */ |
| 4137 | ebmb_delete(&qc->odcid_node); |
| 4138 | ebmb_delete(&qc->scid_node); |
| 4139 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4140 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4141 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4142 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4143 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4144 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4145 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4146 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4147 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4148 | quic_tls_ku_free(qc); |
| 4149 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4150 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4151 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4155 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4156 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4157 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4158 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 4159 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4160 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4161 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4162 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4163 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4164 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4165 | 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] | 4166 | } |
| 4167 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4168 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4169 | { |
| 4170 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4171 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4172 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4173 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4174 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4175 | /* Next application data can be dropped. */ |
| 4176 | qc->mux_state = QC_MUX_RELEASED; |
| 4177 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4178 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4179 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4180 | quic_conn_release(qc); |
| 4181 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 4182 | return; |
| 4183 | } |
| 4184 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4185 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4186 | } |
| 4187 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4188 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4189 | 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] | 4190 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4191 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4192 | struct quic_conn *qc; |
| 4193 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4194 | |
| 4195 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4196 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4197 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4198 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4199 | task->expire = TICK_ETERNITY; |
| 4200 | pktns = quic_loss_pktns(qc); |
| 4201 | if (tick_isset(pktns->tx.loss_time)) { |
| 4202 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4203 | |
| 4204 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4205 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4206 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4207 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4208 | goto out; |
| 4209 | } |
| 4210 | |
| 4211 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4212 | 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] | 4213 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4214 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4215 | struct qcc *qcc = qc->qcc; |
| 4216 | |
| 4217 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4218 | tasklet_wakeup(qcc->subs->tasklet); |
| 4219 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4220 | if (!qcc->subs->events) |
| 4221 | qcc->subs = NULL; |
| 4222 | } |
| 4223 | else { |
| 4224 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4225 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4226 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4227 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 4228 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4229 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4230 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4231 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4232 | 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] | 4233 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4234 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4235 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4236 | 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] | 4237 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4238 | 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] | 4239 | iel->pktns->tx.pto_probe = 1; |
| 4240 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4241 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4242 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4243 | qc->path->loss.pto_count++; |
| 4244 | |
| 4245 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4246 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4247 | |
| 4248 | return task; |
| 4249 | } |
| 4250 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4251 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4252 | * one byte past the end of this buffer. This will extract the ODCID |
| 4253 | * which will be stored into <odcid> |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4254 | * |
| 4255 | * Returns 0 on success else non-zero. |
| 4256 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4257 | 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] | 4258 | struct quic_cid *odcid) |
| 4259 | { |
| 4260 | uint64_t odcid_len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4261 | uint32_t timestamp; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4262 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4263 | if (!quic_dec_int(&odcid_len, &token, end)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4264 | return 1; |
| 4265 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4266 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4267 | * When an Initial packet is sent by a client that has not previously |
| 4268 | * received an Initial or Retry packet from the server, the client |
| 4269 | * populates the Destination Connection ID field with an unpredictable |
| 4270 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4271 | */ |
| 4272 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) |
| 4273 | return 1; |
| 4274 | |
| 4275 | if (end - token < odcid_len + sizeof timestamp) |
| 4276 | return 1; |
| 4277 | |
| 4278 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4279 | 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] | 4280 | return 1; |
| 4281 | |
| 4282 | memcpy(odcid->data, token, odcid_len); |
| 4283 | odcid->len = odcid_len; |
| 4284 | |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
| 4288 | /* Initialize the transport parameters for <qc> QUIC connection attached |
| 4289 | * to <l> listener from <pkt> Initial packet information. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4290 | * Returns 1 if succeeded, 0 if not. |
| 4291 | */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4292 | static int qc_lstnr_params_init(struct quic_conn *qc, struct listener *l, |
| 4293 | const unsigned char *token, size_t token_len, |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4294 | const struct quic_connection_id *icid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4295 | 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] | 4296 | { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4297 | 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] | 4298 | |
| 4299 | /* Copy the transport parameters. */ |
| 4300 | qc->rx.params = l->bind_conf->quic_params; |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4301 | /* Copy the stateless reset token */ |
| 4302 | memcpy(qc->rx.params.stateless_reset_token, icid->stateless_reset_token, |
| 4303 | sizeof qc->rx.params.stateless_reset_token); |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4304 | /* Copy original_destination_connection_id transport parameter. */ |
| 4305 | if (token_len) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4306 | memcpy(odcid_param->data, odcid->data, odcid->len); |
| 4307 | odcid_param->len = odcid->len; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4308 | /* Copy retry_source_connection_id transport parameter. */ |
| 4309 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, dcid); |
| 4310 | } |
| 4311 | else { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4312 | memcpy(odcid_param->data, dcid->data, dcid->len); |
| 4313 | odcid_param->len = dcid->len; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | /* Copy the initial source connection ID. */ |
| 4317 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4318 | |
| 4319 | return 1; |
| 4320 | } |
| 4321 | |
| 4322 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4323 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4324 | * for QUIC servers (or haproxy listeners). |
| 4325 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4326 | * <token> the token found to be used for this connection with <token_len> as |
| 4327 | * length. <saddr> is the source address. |
| 4328 | * Returns the connection if succeeded, NULL if not. |
| 4329 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4330 | 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] | 4331 | struct quic_cid *dcid, struct quic_cid *scid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4332 | const struct quic_cid *odcid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4333 | struct sockaddr_storage *saddr, |
| 4334 | const unsigned char *token, size_t token_len, |
| 4335 | int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4336 | { |
| 4337 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4338 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4339 | /* Initial CID. */ |
| 4340 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4341 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4342 | struct listener *l = NULL; |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4343 | const unsigned char *salt = initial_salt_v1; |
| 4344 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4345 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4346 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4347 | qc = pool_zalloc(pool_head_quic_conn); |
| 4348 | if (!qc) { |
| 4349 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4350 | goto err; |
| 4351 | } |
| 4352 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4353 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4354 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4355 | 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] | 4356 | goto err; |
| 4357 | } |
| 4358 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4359 | qc->cids = EB_ROOT; |
| 4360 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4361 | if (server) { |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4362 | struct proxy *prx; |
| 4363 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4364 | l = owner; |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4365 | prx = l->bind_conf->frontend; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4366 | |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4367 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4368 | &quic_stats_module); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4369 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4370 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4371 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4372 | qc->odcid.len = dcid->len; |
| 4373 | qc->odcid.addrlen = dcid->addrlen; |
| 4374 | 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] | 4375 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4376 | /* 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] | 4377 | if (scid->len) |
| 4378 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4379 | qc->dcid.len = scid->len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4380 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4381 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4382 | } |
| 4383 | /* QUIC Client (outgoing connection to servers) */ |
| 4384 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4385 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4386 | if (dcid->len) |
| 4387 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4388 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4389 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4390 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4391 | |
| 4392 | /* Initialize the output buffer */ |
| 4393 | qc->obuf.pos = qc->obuf.data; |
| 4394 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4395 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4396 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4397 | 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] | 4398 | goto err; |
| 4399 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4400 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4401 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4402 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4403 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4404 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4405 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4406 | qc->scid = icid->cid; |
| 4407 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4408 | /* Packet number spaces initialization. */ |
| 4409 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4410 | quic_pktns_init(&qc->pktns[i]); |
| 4411 | /* QUIC encryption level context initialization. */ |
| 4412 | 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] | 4413 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4414 | 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] | 4415 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4416 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4417 | /* Initialize the packet number space. */ |
| 4418 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4419 | } |
| 4420 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4421 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4422 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4423 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4424 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4425 | /* TX part. */ |
| 4426 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4427 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4428 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4429 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4430 | /* RX part. */ |
| 4431 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4432 | 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] | 4433 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4434 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4435 | |
| 4436 | qc->nb_pkt_for_cc = 1; |
| 4437 | qc->nb_pkt_since_cc = 0; |
| 4438 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4439 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4440 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4441 | 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] | 4442 | goto err; |
| 4443 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4444 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4445 | /* XXX TO DO: Only one path at this time. */ |
| 4446 | qc->path = &qc->paths[0]; |
| 4447 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4448 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4449 | /* required to use MTLIST_IN_LIST */ |
| 4450 | MT_LIST_INIT(&qc->accept_list); |
| 4451 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4452 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4453 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 4454 | qc->sendto_err = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4455 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4456 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4457 | 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] | 4458 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4459 | |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4460 | qc->enc_params_len = |
| 4461 | quic_transport_params_encode(qc->enc_params, |
| 4462 | qc->enc_params + sizeof qc->enc_params, |
| 4463 | &qc->rx.params, 1); |
| 4464 | if (!qc->enc_params_len) |
| 4465 | goto err; |
| 4466 | |
| 4467 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4468 | !quic_conn_init_timer(qc) || |
| 4469 | !quic_conn_init_idle_timer_task(qc)) |
| 4470 | goto err; |
| 4471 | |
| 4472 | if (version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4473 | salt = initial_salt_draft_29; |
| 4474 | salt_len = sizeof initial_salt_draft_29; |
| 4475 | } |
| 4476 | |
| 4477 | if (!qc_new_isecs(qc, salt, salt_len, dcid->data, dcid->len, 1)) |
| 4478 | goto err; |
| 4479 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4480 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4481 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4482 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4483 | |
| 4484 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4485 | 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] | 4486 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4487 | if (qc) |
| 4488 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4489 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4490 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | /* Initialize the timer task of <qc> QUIC connection. |
| 4494 | * Returns 1 if succeeded, 0 if not. |
| 4495 | */ |
| 4496 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4497 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4498 | /* Attach this task to the same thread ID used for the connection */ |
| 4499 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4500 | if (!qc->timer_task) |
| 4501 | return 0; |
| 4502 | |
| 4503 | qc->timer = TICK_ETERNITY; |
| 4504 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4505 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4506 | |
| 4507 | return 1; |
| 4508 | } |
| 4509 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4510 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4511 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4512 | { |
| 4513 | unsigned int expire; |
| 4514 | |
| 4515 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4516 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4517 | } |
| 4518 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4519 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4520 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4521 | */ |
| 4522 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4523 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4524 | if (read) { |
| 4525 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4526 | } |
| 4527 | else { |
| 4528 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4529 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4530 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | /* The task handling the idle timeout */ |
| 4534 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4535 | { |
| 4536 | struct quic_conn *qc = ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4537 | struct quic_counters *prx_counters = qc->prx_counters; |
| 4538 | int qc_state = qc->state; |
| 4539 | unsigned int qc_flags = qc->flags; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4540 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4541 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4542 | * might free the quic-conn too early via quic_close(). |
| 4543 | */ |
| 4544 | qc_notify_close(qc); |
| 4545 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4546 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4547 | * responsible to call quic_close to release it. |
| 4548 | */ |
| 4549 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4550 | if (qc->mux_state != QC_MUX_READY) |
| 4551 | quic_conn_release(qc); |
| 4552 | |
| 4553 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4554 | * least clean some parts of it such as the tasklet. |
| 4555 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4556 | |
| 4557 | /* If the connection did not reached the handshake complete state, |
| 4558 | * the <conn_opening> counter was not decremented. Note that if |
| 4559 | * a TLS alert was received from the TLS stack, this counter |
| 4560 | * has already been decremented. |
| 4561 | */ |
Frédéric Lécaille | ad9895d | 2022-05-20 20:50:59 +0200 | [diff] [blame^] | 4562 | 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] | 4563 | HA_ATOMIC_DEC(&prx_counters->conn_opening); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4564 | |
| 4565 | return NULL; |
| 4566 | } |
| 4567 | |
| 4568 | /* Initialize the idle timeout task for <qc>. |
| 4569 | * Returns 1 if succeeded, 0 if not. |
| 4570 | */ |
| 4571 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4572 | { |
| 4573 | qc->idle_timer_task = task_new_here(); |
| 4574 | if (!qc->idle_timer_task) |
| 4575 | return 0; |
| 4576 | |
| 4577 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4578 | qc->idle_timer_task->context = qc; |
| 4579 | qc_idle_timer_rearm(qc, 1); |
| 4580 | task_queue(qc->idle_timer_task); |
| 4581 | |
| 4582 | return 1; |
| 4583 | } |
| 4584 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4585 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4586 | * past one byte of this buffer. |
| 4587 | */ |
| 4588 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4589 | struct quic_rx_packet *pkt) |
| 4590 | { |
| 4591 | unsigned char dcid_len, scid_len; |
| 4592 | |
| 4593 | /* Version */ |
| 4594 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4595 | return 0; |
| 4596 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4597 | /* Destination Connection ID Length */ |
| 4598 | dcid_len = *(*buf)++; |
| 4599 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4600 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4601 | /* XXX MUST BE DROPPED */ |
| 4602 | return 0; |
| 4603 | |
| 4604 | if (dcid_len) { |
| 4605 | /* Check that the length of this received DCID matches the CID lengths |
| 4606 | * of our implementation for non Initials packets only. |
| 4607 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4608 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4609 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4610 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4611 | return 0; |
| 4612 | |
| 4613 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4614 | } |
| 4615 | |
| 4616 | pkt->dcid.len = dcid_len; |
| 4617 | *buf += dcid_len; |
| 4618 | |
| 4619 | /* Source Connection ID Length */ |
| 4620 | scid_len = *(*buf)++; |
| 4621 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4622 | /* XXX MUST BE DROPPED */ |
| 4623 | return 0; |
| 4624 | |
| 4625 | if (scid_len) |
| 4626 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4627 | pkt->scid.len = scid_len; |
| 4628 | *buf += scid_len; |
| 4629 | |
| 4630 | return 1; |
| 4631 | } |
| 4632 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4633 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4634 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4635 | { |
| 4636 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4637 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4638 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4639 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4640 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /* 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] | 4644 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4645 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4646 | * the packet first byte. |
| 4647 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4648 | * Returns 1 if succeeded, 0 if not. |
| 4649 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4650 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4651 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4652 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4653 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4654 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4655 | { |
| 4656 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4657 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4658 | struct quic_enc_level *qel; |
| 4659 | /* Only for traces. */ |
| 4660 | struct quic_rx_packet *qpkt_trace; |
| 4661 | |
| 4662 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4663 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4664 | /* The packet number is here. This is also the start minus |
| 4665 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4666 | * protection. |
| 4667 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4668 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4669 | |
| 4670 | tel = quic_packet_type_enc_level(pkt->type); |
| 4671 | qel = &qc->els[tel]; |
| 4672 | |
| 4673 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4674 | /* Note that the following function enables us to unprotect the packet |
| 4675 | * number and its length subsequently used to decrypt the entire |
| 4676 | * packets. |
| 4677 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4678 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4679 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4680 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4681 | goto err; |
| 4682 | } |
| 4683 | |
| 4684 | /* The AAD includes the packet number field found at <pn>. */ |
| 4685 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4686 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4687 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4688 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4689 | 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] | 4690 | /* If the packet number space has been discarded, this packet |
| 4691 | * will be not parsed. |
| 4692 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4693 | 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] | 4694 | goto out; |
| 4695 | } |
| 4696 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4697 | 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] | 4698 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4699 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4700 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4701 | } |
| 4702 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4703 | *el = qel; |
| 4704 | /* No reference counter incrementation here!!! */ |
| 4705 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4706 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4707 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4708 | b_add(&qc->rx.buf, pkt->len); |
| 4709 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4710 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4711 | return 1; |
| 4712 | |
| 4713 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4714 | 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] | 4715 | return 0; |
| 4716 | } |
| 4717 | |
| 4718 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4719 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4720 | */ |
| 4721 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4722 | unsigned char byte0, int *long_header) |
| 4723 | { |
| 4724 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4725 | pkt->type = |
| 4726 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4727 | *long_header = 1; |
| 4728 | } |
| 4729 | else { |
| 4730 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4731 | *long_header = 0; |
| 4732 | } |
| 4733 | } |
| 4734 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4735 | /* |
| 4736 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4737 | */ |
| 4738 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4739 | { |
| 4740 | int j = 0, version; |
| 4741 | |
| 4742 | do { |
| 4743 | version = quic_supported_version[j]; |
| 4744 | if (version == pkt->version) |
| 4745 | return 1; |
| 4746 | |
| 4747 | version = quic_supported_version[++j]; |
| 4748 | } while(version); |
| 4749 | |
| 4750 | return 0; |
| 4751 | } |
| 4752 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4753 | /* |
| 4754 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4755 | * address <addr>. |
| 4756 | * Implementation of RFC9000 6. Version Negotiation |
| 4757 | * |
| 4758 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4759 | * |
| 4760 | * Returns 0 on success else non-zero |
| 4761 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4762 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4763 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4764 | { |
| 4765 | char buf[256]; |
| 4766 | int i = 0, j, version; |
| 4767 | const socklen_t addrlen = get_addr_len(addr); |
| 4768 | |
| 4769 | /* |
| 4770 | * header form |
| 4771 | * long header, fixed bit to 0 for Version Negotiation |
| 4772 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4773 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4774 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4775 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4776 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4777 | /* null version for Version Negotiation */ |
| 4778 | buf[i++] = '\x00'; |
| 4779 | buf[i++] = '\x00'; |
| 4780 | buf[i++] = '\x00'; |
| 4781 | buf[i++] = '\x00'; |
| 4782 | |
| 4783 | /* source connection id */ |
| 4784 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4785 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4786 | i += pkt->scid.len; |
| 4787 | |
| 4788 | /* destination connection id */ |
| 4789 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4790 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4791 | i += pkt->dcid.len; |
| 4792 | |
| 4793 | /* supported version */ |
| 4794 | j = 0; |
| 4795 | do { |
| 4796 | version = htonl(quic_supported_version[j]); |
| 4797 | memcpy(&buf[i], &version, sizeof(version)); |
| 4798 | i += sizeof(version); |
| 4799 | |
| 4800 | version = quic_supported_version[++j]; |
| 4801 | } while (version); |
| 4802 | |
| 4803 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4804 | return 1; |
| 4805 | |
| 4806 | return 0; |
| 4807 | } |
| 4808 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4809 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 4810 | * from <fd> UDP socket to <dst> |
| 4811 | * Return 1 if succeeded, 0 if not. |
| 4812 | */ |
| 4813 | static int send_stateless_reset(int fd, struct sockaddr_storage *dstaddr, |
| 4814 | struct quic_rx_packet *rxpkt) |
| 4815 | { |
| 4816 | int pktlen, rndlen; |
| 4817 | unsigned char pkt[64]; |
| 4818 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 4819 | |
| 4820 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 4821 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 4822 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 4823 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 4824 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 4825 | * connection ID length that it requests the peer to include in its packets, |
| 4826 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 4827 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 4828 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 4829 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 4830 | * than the packet it responds to. |
| 4831 | */ |
| 4832 | |
| 4833 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 4834 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 4835 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 4836 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 4837 | /* Put a header of random bytes */ |
| 4838 | if (RAND_bytes(pkt, rndlen) != 1) |
| 4839 | return 0; |
| 4840 | |
| 4841 | /* Clear the most significant bit, and set the second one */ |
| 4842 | *pkt = (*pkt & ~0x80) | 0x40; |
| 4843 | if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 4844 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 4845 | return 0; |
| 4846 | |
| 4847 | if (sendto(fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 4848 | return 0; |
| 4849 | |
| 4850 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 4851 | return 1; |
| 4852 | } |
| 4853 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4854 | /* QUIC server only function. |
| 4855 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 4856 | * This is the responsability of the caller to check <aad> size is big enough |
| 4857 | * to contain these data. |
| 4858 | * Return the number of bytes copied to <aad>. |
| 4859 | */ |
| 4860 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 4861 | uint32_t version, |
| 4862 | const struct quic_cid *cid, |
| 4863 | const struct sockaddr_storage *addr) |
| 4864 | { |
| 4865 | unsigned char *p; |
| 4866 | |
| 4867 | p = aad; |
| 4868 | memcpy(p, &version, sizeof version); |
| 4869 | p += sizeof version; |
| 4870 | p += quic_saddr_cpy(p, addr); |
| 4871 | memcpy(p, cid->data, cid->len); |
| 4872 | p += cid->len; |
| 4873 | |
| 4874 | return p - aad; |
| 4875 | } |
| 4876 | |
| 4877 | /* QUIC server only function. |
| 4878 | * Generate the token to be used in Retry packets. The token is written to |
| 4879 | * <buf> whith <len> as length. <odcid> is the original destination connection |
| 4880 | * ID and <dcid> is our side destination connection ID (or client source |
| 4881 | * connection ID). |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4882 | * Returns the length of the encoded token or 0 on error. |
| 4883 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4884 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 4885 | const uint32_t version, |
| 4886 | const struct quic_cid *odcid, |
| 4887 | const struct quic_cid *dcid, |
| 4888 | struct sockaddr_storage *addr) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4889 | { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4890 | unsigned char *p; |
| 4891 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4892 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4893 | size_t aadlen; |
| 4894 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 4895 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4896 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4897 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4898 | size_t seclen = strlen(global.cluster_secret); |
| 4899 | EVP_CIPHER_CTX *ctx = NULL; |
| 4900 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4901 | uint32_t timestamp = now_ms; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4902 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4903 | /* We copy the odcid into the token, prefixed by its one byte |
| 4904 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 4905 | * the random bytes used to derive the secret to encrypt the token. |
| 4906 | */ |
| 4907 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4908 | return 0; |
| 4909 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4910 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 4911 | if (RAND_bytes(salt, sizeof salt) != 1) |
| 4912 | goto err; |
| 4913 | |
| 4914 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 4915 | salt, sizeof salt, sec, seclen)) |
| 4916 | goto err; |
| 4917 | |
| 4918 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) |
| 4919 | goto err; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4920 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4921 | /* Token build */ |
| 4922 | p = buf; |
| 4923 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 4924 | *p++ = odcid->len; |
| 4925 | memcpy(p, odcid->data, odcid->len); |
| 4926 | p += odcid->len; |
| 4927 | write_u32(p, htonl(timestamp)); |
| 4928 | p += sizeof timestamp; |
| 4929 | |
| 4930 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4931 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) |
| 4932 | goto err; |
| 4933 | |
| 4934 | p += QUIC_TLS_TAG_LEN; |
| 4935 | memcpy(p, salt, sizeof salt); |
| 4936 | p += sizeof salt; |
| 4937 | EVP_CIPHER_CTX_free(ctx); |
| 4938 | |
| 4939 | return p - buf; |
| 4940 | |
| 4941 | err: |
| 4942 | if (ctx) |
| 4943 | EVP_CIPHER_CTX_free(ctx); |
| 4944 | return 0; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4947 | /* QUIC server only function. |
| 4948 | * Check the validity of the Retry token from <token> buffer with <tokenlen> |
| 4949 | * as length. If valid, the ODCID of <qc> QUIC connection will be put |
| 4950 | * into <odcid> connection ID. <dcid> is our side destination connection ID |
| 4951 | * of client source connection ID. |
| 4952 | * Return 1 if succeeded, 0 if not. |
| 4953 | */ |
| 4954 | static int quic_retry_token_check(unsigned char *token, size_t tokenlen, |
| 4955 | const uint32_t version, |
| 4956 | struct quic_cid *odcid, |
| 4957 | const struct quic_cid *dcid, |
| 4958 | struct quic_conn *qc, |
| 4959 | struct sockaddr_storage *addr) |
| 4960 | { |
| 4961 | unsigned char buf[128]; |
| 4962 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4963 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4964 | size_t aadlen; |
| 4965 | unsigned char *salt; |
| 4966 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4967 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4968 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4969 | size_t seclen = strlen(global.cluster_secret); |
| 4970 | EVP_CIPHER_CTX *ctx = NULL; |
| 4971 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4972 | |
| 4973 | if (sizeof buf < tokenlen) |
| 4974 | return 0; |
| 4975 | |
| 4976 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 4977 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 4978 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 4979 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) |
| 4980 | return 0; |
| 4981 | |
| 4982 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) |
| 4983 | goto err; |
| 4984 | |
| 4985 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4986 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 4987 | ctx, aead, key, iv)) |
| 4988 | goto err; |
| 4989 | |
| 4990 | if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 4991 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4992 | goto err; |
| 4993 | } |
| 4994 | |
| 4995 | EVP_CIPHER_CTX_free(ctx); |
| 4996 | return 1; |
| 4997 | |
| 4998 | err: |
| 4999 | if (ctx) |
| 5000 | EVP_CIPHER_CTX_free(ctx); |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5004 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5005 | * the Initial <pkt> packet. |
| 5006 | * |
| 5007 | * Returns 0 on success else non-zero. |
| 5008 | */ |
| 5009 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 5010 | struct quic_rx_packet *pkt) |
| 5011 | { |
| 5012 | unsigned char buf[128]; |
| 5013 | int i = 0, token_len; |
| 5014 | const socklen_t addrlen = get_addr_len(addr); |
| 5015 | struct quic_cid scid; |
| 5016 | |
| 5017 | /* long header + fixed bit + packet type 0x3 */ |
| 5018 | buf[i++] = 0xf0; |
| 5019 | /* version */ |
| 5020 | buf[i++] = 0x00; |
| 5021 | buf[i++] = 0x00; |
| 5022 | buf[i++] = 0x00; |
| 5023 | buf[i++] = 0x01; |
| 5024 | |
| 5025 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5026 | buf[i++] = pkt->scid.len; |
| 5027 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5028 | i += pkt->scid.len; |
| 5029 | |
| 5030 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5031 | scid.len = QUIC_HAP_CID_LEN; |
| 5032 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 5033 | return 1; |
| 5034 | |
| 5035 | buf[i++] = scid.len; |
| 5036 | memcpy(&buf[i], scid.data, scid.len); |
| 5037 | i += scid.len; |
| 5038 | |
| 5039 | /* token */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5040 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, pkt->version, |
| 5041 | &pkt->dcid, &pkt->scid, addr))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5042 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 5043 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5044 | i += token_len; |
| 5045 | |
| 5046 | /* token integrity tag */ |
| 5047 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5048 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 5049 | pkt->dcid.len, buf, i)) { |
| 5050 | return 1; |
| 5051 | } |
| 5052 | |
| 5053 | i += QUIC_TLS_TAG_LEN; |
| 5054 | |
| 5055 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5056 | return 1; |
| 5057 | |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5061 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5062 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5063 | * concatenated to the <pkt> DCID field. |
| 5064 | * |
| 5065 | * Returns the instance or NULL if not found. |
| 5066 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5067 | 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] | 5068 | struct listener *l, |
| 5069 | struct sockaddr_storage *saddr) |
| 5070 | { |
| 5071 | struct quic_conn *qc = NULL; |
| 5072 | struct ebmb_node *node; |
| 5073 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5074 | /* set if the quic_conn is found in the second DCID tree */ |
| 5075 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5076 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5077 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5078 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5079 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5080 | /* DCIDs of first packets coming from multiple clients may have |
| 5081 | * the same values. Let's distinguish them by concatenating the |
| 5082 | * socket addresses. |
| 5083 | */ |
| 5084 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5085 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5086 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5087 | if (node) { |
| 5088 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5089 | goto end; |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5094 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5095 | * used. |
| 5096 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5097 | 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] | 5098 | if (!node) |
| 5099 | goto end; |
| 5100 | |
| 5101 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5102 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5103 | found_in_dcid = 1; |
| 5104 | |
| 5105 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5106 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5107 | * If already done, this is a noop. |
| 5108 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 5109 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5110 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5111 | |
| 5112 | return qc; |
| 5113 | } |
| 5114 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5115 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5116 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5117 | * parameters of this session. |
| 5118 | * This is the responsibility of the caller to check the validity of all the |
| 5119 | * pointers passed as parameter to this function. |
| 5120 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5121 | * CO_ER_SSL_NO_MEM. |
| 5122 | */ |
| 5123 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5124 | unsigned char *params, size_t params_len) |
| 5125 | { |
| 5126 | int retry; |
| 5127 | |
| 5128 | retry = 1; |
| 5129 | retry: |
| 5130 | *ssl = SSL_new(ssl_ctx); |
| 5131 | if (!*ssl) { |
| 5132 | if (!retry--) |
| 5133 | goto err; |
| 5134 | |
| 5135 | pool_gc(NULL); |
| 5136 | goto retry; |
| 5137 | } |
| 5138 | |
| 5139 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5140 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 5141 | !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] | 5142 | SSL_free(*ssl); |
| 5143 | *ssl = NULL; |
| 5144 | if (!retry--) |
| 5145 | goto err; |
| 5146 | |
| 5147 | pool_gc(NULL); |
| 5148 | goto retry; |
| 5149 | } |
| 5150 | |
| 5151 | return 0; |
| 5152 | |
| 5153 | err: |
| 5154 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5155 | return -1; |
| 5156 | } |
| 5157 | |
| 5158 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5159 | * used to process <qc> received packets. The allocated context is stored in |
| 5160 | * <qc.xprt_ctx>. |
| 5161 | * |
| 5162 | * Returns 0 on success else non-zero. |
| 5163 | */ |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 5164 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5165 | { |
| 5166 | struct bind_conf *bc = qc->li->bind_conf; |
| 5167 | struct ssl_sock_ctx *ctx = NULL; |
| 5168 | |
| 5169 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 5170 | if (!ctx) |
| 5171 | goto err; |
| 5172 | |
| 5173 | ctx->wait_event.tasklet = tasklet_new(); |
| 5174 | if (!ctx->wait_event.tasklet) |
| 5175 | goto err; |
| 5176 | |
| 5177 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5178 | ctx->wait_event.tasklet->context = ctx; |
| 5179 | ctx->wait_event.events = 0; |
| 5180 | ctx->subs = NULL; |
| 5181 | ctx->xprt_ctx = NULL; |
| 5182 | ctx->qc = qc; |
| 5183 | |
| 5184 | /* Set tasklet tid based on the SCID selected by us for this |
| 5185 | * connection. The upper layer will also be binded on the same thread. |
| 5186 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 5187 | 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] | 5188 | |
| 5189 | if (qc_is_listener(qc)) { |
| 5190 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5191 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5192 | goto err; |
| 5193 | } |
| 5194 | |
| 5195 | /* Enabling 0-RTT */ |
| 5196 | if (bc->ssl_conf.early_data) |
| 5197 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5198 | |
| 5199 | SSL_set_accept_state(ctx->ssl); |
| 5200 | } |
| 5201 | |
| 5202 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5203 | |
| 5204 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5205 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5206 | |
| 5207 | return 0; |
| 5208 | |
| 5209 | err: |
| 5210 | if (ctx && ctx->wait_event.tasklet) |
| 5211 | tasklet_free(ctx->wait_event.tasklet); |
| 5212 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 5213 | |
| 5214 | return 1; |
| 5215 | } |
| 5216 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5217 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5218 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5219 | * with the information collected from the packet. |
| 5220 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5221 | * byte to enable the caller to run this function again to continue to parse |
| 5222 | * the remaing QUIC packets carried by the datagram. |
| 5223 | * Note that this function always sets this ->len value. If a paquet could |
| 5224 | * not be correctly found, ->len value will be set to the remaining number |
| 5225 | * bytes in the datagram to entirely consume this latter. |
| 5226 | */ |
| 5227 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5228 | struct quic_rx_packet *pkt, int first_pkt, |
| 5229 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5230 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5231 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5232 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5233 | struct listener *l; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5234 | struct proxy *prx; |
| 5235 | struct quic_counters *prx_counters; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5236 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5237 | 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] | 5238 | size_t b_cspace; |
| 5239 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5240 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5241 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5242 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5243 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5244 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5245 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5246 | l = dgram->owner; |
| 5247 | prx = l->bind_conf->frontend; |
| 5248 | 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] | 5249 | /* This ist only to please to traces and distinguish the |
| 5250 | * packet with parsed packet number from others. |
| 5251 | */ |
| 5252 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5253 | if (end <= buf) { |
| 5254 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5255 | goto drop; |
| 5256 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5257 | |
| 5258 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5259 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5260 | /* XXX TO BE DISCARDED */ |
| 5261 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5262 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5263 | } |
| 5264 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5265 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5266 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5267 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5268 | uint64_t len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5269 | struct quic_cid odcid; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5270 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5271 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5272 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5273 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5274 | } |
| 5275 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5276 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5277 | 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] | 5278 | drop_no_conn = 1; |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5279 | } |
| 5280 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5281 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5282 | 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] | 5283 | drop_no_conn = 1; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5284 | } |
| 5285 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5286 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5287 | * they must have the same DCID. |
| 5288 | */ |
| 5289 | if (!first_pkt && |
| 5290 | (pkt->dcid.len != dgram->dcid_len || |
| 5291 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5292 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5293 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5294 | } |
| 5295 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5296 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 5297 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 5298 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5299 | goto drop; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5302 | /* RFC9000 6. Version Negotiation */ |
| 5303 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5304 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5305 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5306 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 5307 | goto err; |
| 5308 | } |
| 5309 | |
| 5310 | 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] | 5311 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5314 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5315 | * there is no Initial connection IDs storage. |
| 5316 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5317 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5318 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5319 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5320 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5321 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5322 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5323 | goto drop; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5324 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5325 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5326 | /* TODO Retry should be automatically activated if |
| 5327 | * suspect network usage is detected. |
| 5328 | */ |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5329 | if (global.cluster_secret) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5330 | if (!token_len) { |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5331 | 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] | 5332 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 5333 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
| 5334 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5335 | goto err; |
| 5336 | } |
| 5337 | |
| 5338 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5339 | goto err; |
| 5340 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5341 | } |
| 5342 | else { |
| 5343 | if (*buf == QUIC_TOKEN_FMT_RETRY) { |
| 5344 | if (!quic_retry_token_check(buf, token_len, pkt->version, &odcid, |
| 5345 | &pkt->scid, qc, &dgram->saddr)) { |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5346 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5347 | TRACE_PROTO("Wrong retry token", QUIC_EV_CONN_LPKT); |
| 5348 | /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection |
| 5349 | * with an INVALID_TOKEN error. |
| 5350 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5351 | goto drop; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5352 | } |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5353 | |
| 5354 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5355 | } |
| 5356 | else { |
| 5357 | /* TODO: New token check */ |
| 5358 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5359 | goto drop; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5360 | } |
| 5361 | } |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5362 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5363 | |
| 5364 | pkt->token = buf; |
| 5365 | pkt->token_len = token_len; |
| 5366 | buf += pkt->token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5367 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5368 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5369 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5370 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5371 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5372 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5373 | } |
| 5374 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5375 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5376 | end - buf < len) { |
| 5377 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5378 | goto drop; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5379 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5380 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5381 | payload = buf; |
| 5382 | pkt->len = len + payload - beg; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5383 | if (drop_no_conn) |
| 5384 | goto drop_no_conn; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5385 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5386 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5387 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5388 | int ipv4; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5389 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5390 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5391 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 5392 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5393 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5394 | } |
| 5395 | |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5396 | 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] | 5397 | HA_ATOMIC_LOAD(&prx_counters->conn_opening) >= global.tune.quic_retry_threshold) { |
| 5398 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 5399 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
| 5400 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5401 | goto err; |
| 5402 | } |
| 5403 | |
| 5404 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5405 | goto err; |
| 5406 | } |
| 5407 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5408 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5409 | * When an Initial packet is sent by a client that has not previously |
| 5410 | * received an Initial or Retry packet from the server, the client |
| 5411 | * populates the Destination Connection ID field with an unpredictable |
| 5412 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5413 | */ |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5414 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 5415 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5416 | goto drop; |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5417 | } |
| 5418 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5419 | pkt->saddr = dgram->saddr; |
| 5420 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5421 | 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] | 5422 | pkt->token, pkt->token_len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5423 | if (qc == NULL) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5424 | goto drop; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5425 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5426 | HA_ATOMIC_INC(&prx_counters->conn_opening); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5427 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5428 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5429 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5430 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5431 | /* If the insertion failed, it means that another |
| 5432 | * thread has already allocated a QUIC connection for |
| 5433 | * the same CID. Liberate our allocated connection. |
| 5434 | */ |
| 5435 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5436 | qc_to_purge = qc; |
| 5437 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5438 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5439 | pkt->qc = qc; |
| 5440 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5441 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5442 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5443 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5444 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5445 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5446 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5447 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5448 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5449 | } |
| 5450 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5451 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5452 | } |
| 5453 | } |
| 5454 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5455 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5456 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5457 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5458 | } |
| 5459 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5460 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5461 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5462 | |
| 5463 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5464 | * they must have the same DCID. |
| 5465 | */ |
| 5466 | if (!first_pkt && |
| 5467 | (pkt->dcid.len != dgram->dcid_len || |
| 5468 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5469 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5470 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5471 | } |
| 5472 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5473 | buf += QUIC_HAP_CID_LEN; |
| 5474 | |
| 5475 | /* A short packet is the last one of a UDP datagram. */ |
| 5476 | payload = buf; |
| 5477 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5478 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5479 | 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] | 5480 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5481 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5482 | 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] | 5483 | if (global.cluster_secret && !send_stateless_reset(l->rx.fd, &dgram->saddr, pkt)) |
| 5484 | 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] | 5485 | goto drop; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5486 | } |
| 5487 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5488 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5491 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 5492 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 5493 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 5494 | qc->nb_pkt_for_cc++; |
| 5495 | qc->nb_pkt_since_cc = 0; |
| 5496 | } |
| 5497 | /* Skip the entire datagram */ |
| 5498 | pkt->len = end - beg; |
| 5499 | 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] | 5500 | goto drop; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5501 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5502 | |
| 5503 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5504 | * they must have the same DCID. |
| 5505 | * |
| 5506 | * This check must be done after the final update to pkt.len to |
| 5507 | * properly drop the packet on failure. |
| 5508 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5509 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5510 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5511 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 5512 | QUIC_EV_CONN_LPKT, qc); |
| 5513 | /* Reset the anti-amplification bit. It will be set again |
| 5514 | * when sending the next packet if reached again. |
| 5515 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5516 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 5517 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5518 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5519 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5520 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5521 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5522 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5523 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5524 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5525 | goto out; |
| 5526 | } |
| 5527 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5528 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5529 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5530 | b_cspace = b_contig_space(&qc->rx.buf); |
| 5531 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 5532 | /* Do not consume buf if space not at the end. */ |
| 5533 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 5534 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5535 | goto err; |
| 5536 | } |
| 5537 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5538 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5539 | if (b_cspace) { |
| 5540 | b_putchr(&qc->rx.buf, 0x00); |
| 5541 | b_cspace--; |
| 5542 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5543 | b_add(&qc->rx.buf, b_cspace); |
| 5544 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5545 | 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] | 5546 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5547 | goto drop; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5548 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5549 | } |
| 5550 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5551 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5552 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5553 | goto drop; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 5554 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5555 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5556 | 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] | 5557 | if (pkt->aad_len) |
| 5558 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5559 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 5560 | /* Wake up the connection packet handler task from here only if all |
| 5561 | * the contexts have been initialized, especially the mux context |
| 5562 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 5563 | * will start it if these contexts for the connection are not already |
| 5564 | * initialized. |
| 5565 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5566 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5567 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5568 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5569 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5570 | drop_no_conn: |
| 5571 | if (drop_no_conn) |
| 5572 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5573 | 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] | 5574 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5575 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5576 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5577 | drop: |
| 5578 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5579 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5580 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5581 | * This cannot be done by this thread. |
| 5582 | */ |
| 5583 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5584 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5585 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5586 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5587 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5588 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5589 | if (!pkt->len) |
| 5590 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5591 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5592 | qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5593 | } |
| 5594 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5595 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5596 | * 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] | 5597 | */ |
| 5598 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5599 | int type, size_t pn_len, struct quic_conn *conn) |
| 5600 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5601 | 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] | 5602 | return 0; |
| 5603 | |
| 5604 | /* #0 byte flags */ |
| 5605 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5606 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5607 | /* Version */ |
| 5608 | quic_write_uint32(buf, end, conn->version); |
| 5609 | *(*buf)++ = conn->dcid.len; |
| 5610 | /* Destination connection ID */ |
| 5611 | if (conn->dcid.len) { |
| 5612 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5613 | *buf += conn->dcid.len; |
| 5614 | } |
| 5615 | /* Source connection ID */ |
| 5616 | *(*buf)++ = conn->scid.len; |
| 5617 | if (conn->scid.len) { |
| 5618 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5619 | *buf += conn->scid.len; |
| 5620 | } |
| 5621 | |
| 5622 | return 1; |
| 5623 | } |
| 5624 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5625 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5626 | * 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] | 5627 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5628 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5629 | size_t pn_len, struct quic_conn *conn, |
| 5630 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5631 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5632 | if (end - *buf < 1 + conn->dcid.len) |
| 5633 | return 0; |
| 5634 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5635 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5636 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5637 | ((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] | 5638 | /* Destination connection ID */ |
| 5639 | if (conn->dcid.len) { |
| 5640 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5641 | *buf += conn->dcid.len; |
| 5642 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5643 | |
| 5644 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5648 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5649 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5650 | * Returns 1 if succeeded or 0 if failed. |
| 5651 | */ |
| 5652 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5653 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5654 | { |
| 5655 | int i, ret, outlen; |
| 5656 | EVP_CIPHER_CTX *ctx; |
| 5657 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5658 | * and at most 4 bytes for the packet number |
| 5659 | */ |
| 5660 | unsigned char mask[5] = {0}; |
| 5661 | |
| 5662 | ret = 0; |
| 5663 | ctx = EVP_CIPHER_CTX_new(); |
| 5664 | if (!ctx) |
| 5665 | return 0; |
| 5666 | |
| 5667 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5668 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5669 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5670 | goto out; |
| 5671 | |
| 5672 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5673 | for (i = 0; i < pnlen; i++) |
| 5674 | pn[i] ^= mask[i + 1]; |
| 5675 | |
| 5676 | ret = 1; |
| 5677 | |
| 5678 | out: |
| 5679 | EVP_CIPHER_CTX_free(ctx); |
| 5680 | |
| 5681 | return ret; |
| 5682 | } |
| 5683 | |
| 5684 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5685 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5686 | * Return 1 if succeeded, 0 if not. |
| 5687 | */ |
| 5688 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5689 | { |
| 5690 | size_t room, ack_delay_sz; |
| 5691 | |
| 5692 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5693 | /* A frame is made of 1 byte for the frame type. */ |
| 5694 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5695 | 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] | 5696 | return 0; |
| 5697 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5698 | 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] | 5699 | } |
| 5700 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5701 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5702 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5703 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5704 | * with the number of bytes already present in this buffer which must be taken |
| 5705 | * into an account for the Length packet field value. <headlen> is the number of |
| 5706 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5707 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5708 | * 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] | 5709 | * 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] | 5710 | * 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] | 5711 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5712 | 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] | 5713 | size_t room, size_t *len, size_t headlen, |
| 5714 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5715 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5716 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5717 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5718 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5719 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5720 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5721 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5722 | return 0; |
| 5723 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5724 | /* If we are not probing we must take into an account the congestion |
| 5725 | * control window. |
| 5726 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5727 | if (!qel->pktns->tx.pto_probe) { |
| 5728 | size_t remain = quic_path_prep_data(qc->path); |
| 5729 | |
| 5730 | if (headlen > remain) |
| 5731 | return 0; |
| 5732 | |
| 5733 | room = QUIC_MIN(room, remain - headlen); |
| 5734 | } |
| 5735 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5736 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5737 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5738 | |
| 5739 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 5740 | * returned by this function only if at least one frame could be built |
| 5741 | * in the switch/case block. |
| 5742 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5743 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5744 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5745 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5746 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5747 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5748 | break; |
| 5749 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5750 | switch (cf->type) { |
| 5751 | case QUIC_FT_CRYPTO: |
| 5752 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5753 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5754 | /* Compute the length of this CRYPTO frame header */ |
| 5755 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5756 | /* Compute the data length of this CRyPTO frame. */ |
| 5757 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5758 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5759 | 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] | 5760 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5761 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5762 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5763 | /* CRYPTO frame length. */ |
| 5764 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5765 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5766 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5767 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5768 | * length and the length of this length. |
| 5769 | */ |
| 5770 | *len += flen; |
| 5771 | room -= flen; |
| 5772 | if (dlen == cf->crypto.len) { |
| 5773 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5774 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5775 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5776 | } |
| 5777 | else { |
| 5778 | struct quic_frame *new_cf; |
| 5779 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5780 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5781 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5782 | 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] | 5783 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5784 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5785 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5786 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5787 | new_cf->type = QUIC_FT_CRYPTO; |
| 5788 | new_cf->crypto.len = dlen; |
| 5789 | new_cf->crypto.offset = cf->crypto.offset; |
| 5790 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5791 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5792 | /* Consume <dlen> bytes of the current frame. */ |
| 5793 | cf->crypto.len -= dlen; |
| 5794 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5795 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5796 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5797 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5798 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5799 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 5800 | struct eb64_node *node = NULL; |
| 5801 | struct qc_stream_desc *stream_desc = NULL; |
| 5802 | struct quic_stream *strm = &cf->stream; |
| 5803 | |
| 5804 | /* As this frame has been already lost, ensure the stream is always |
| 5805 | * available or the range of this frame is not consumed before |
| 5806 | * resending it. |
| 5807 | */ |
| 5808 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 5809 | if (!node) { |
| 5810 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5811 | LIST_DELETE(&cf->list); |
| 5812 | pool_free(pool_head_quic_frame, cf); |
| 5813 | continue; |
| 5814 | } |
| 5815 | |
| 5816 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5817 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 5818 | TRACE_PROTO("ignored frame frame in already acked range", |
| 5819 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5820 | LIST_DELETE(&cf->list); |
| 5821 | pool_free(pool_head_quic_frame, cf); |
| 5822 | continue; |
| 5823 | } |
| 5824 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 5825 | strm->offset.key = stream_desc->ack_offset; |
| 5826 | TRACE_PROTO("updated partially acked frame", |
| 5827 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5828 | } |
| 5829 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5830 | /* Note that these frames are accepted in short packets only without |
| 5831 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5832 | * 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] | 5833 | * |
| 5834 | * 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] | 5835 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5836 | */ |
| 5837 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5838 | ((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] | 5839 | /* Compute the data length of this STREAM frame. */ |
| 5840 | avail_room = room - hlen - *len; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5841 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5842 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5843 | if ((ssize_t)avail_room <= 0) |
| 5844 | continue; |
| 5845 | |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5846 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5847 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5848 | if (dlen > cf->stream.len) { |
| 5849 | dlen = cf->stream.len; |
| 5850 | } |
| 5851 | dlen_sz = quic_int_getsize(dlen); |
| 5852 | flen = hlen + dlen_sz + dlen; |
| 5853 | } |
| 5854 | else { |
| 5855 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5856 | flen = hlen + dlen; |
| 5857 | } |
| 5858 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5859 | 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] | 5860 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5861 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5862 | /* Add the STREAM data length and its encoded length to the packet |
| 5863 | * length and the length of this length. |
| 5864 | */ |
| 5865 | *len += flen; |
| 5866 | room -= flen; |
| 5867 | if (dlen == cf->stream.len) { |
| 5868 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5869 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5870 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5871 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5872 | /* The MUX stream might be released at this |
| 5873 | * stage. This can most notably happen on |
| 5874 | * retransmission. |
| 5875 | */ |
| 5876 | if (qc->mux_state == QC_MUX_READY && |
| 5877 | !cf->stream.stream->release) { |
| 5878 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5879 | cf->stream.len, |
| 5880 | cf->stream.offset.key); |
| 5881 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5882 | } |
| 5883 | else { |
| 5884 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5885 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5886 | |
| 5887 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5888 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5889 | 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] | 5890 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5891 | } |
| 5892 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5893 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5894 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5895 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5896 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5897 | new_cf->stream.id = cf->stream.id; |
| 5898 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5899 | new_cf->stream.offset = cf->stream.offset; |
| 5900 | new_cf->stream.len = dlen; |
| 5901 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5902 | /* FIN bit reset */ |
| 5903 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5904 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5905 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5906 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5907 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5908 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5909 | b_size(cf->stream.buf), |
| 5910 | (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] | 5911 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5912 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5913 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5914 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5915 | /* The MUX stream might be released at this |
| 5916 | * stage. This can most notably happen on |
| 5917 | * retransmission. |
| 5918 | */ |
| 5919 | if (qc->mux_state == QC_MUX_READY && |
| 5920 | !cf->stream.stream->release) { |
| 5921 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5922 | new_cf->stream.len, |
| 5923 | new_cf->stream.offset.key); |
| 5924 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5925 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5926 | |
| 5927 | /* TODO the MUX is notified about the frame sending via |
| 5928 | * previous qcc_streams_sent_done call. However, the |
| 5929 | * sending can fail later, for example if the sendto |
| 5930 | * system call returns an error. As the MUX has been |
| 5931 | * notified, the transport layer is responsible to |
| 5932 | * bufferize and resent the announced data later. |
| 5933 | */ |
| 5934 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5935 | break; |
| 5936 | |
| 5937 | default: |
| 5938 | flen = qc_frm_len(cf); |
| 5939 | BUG_ON(!flen); |
| 5940 | if (flen > room) |
| 5941 | continue; |
| 5942 | |
| 5943 | *len += flen; |
| 5944 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5945 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5946 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5947 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5948 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5949 | |
| 5950 | /* 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] | 5951 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5952 | } |
| 5953 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5954 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5955 | } |
| 5956 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5957 | /* 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] | 5958 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5959 | * 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] | 5960 | * filling the buffer with as much frames as possible from <frms> list of |
| 5961 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5962 | * 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] | 5963 | * 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] | 5964 | * having returned from this function. |
| 5965 | * 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] | 5966 | * number field in this packet. <pn_len> will also have the packet number |
| 5967 | * length as value. |
| 5968 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5969 | * 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] | 5970 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5971 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5972 | size_t dglen, struct quic_tx_packet *pkt, |
| 5973 | 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] | 5974 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5975 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5976 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5977 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5978 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5979 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5980 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5981 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5982 | 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] | 5983 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5984 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5985 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5986 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5987 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5988 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5989 | /* Length field value with CRYPTO frames if present. */ |
| 5990 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5991 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5992 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5993 | * buffer to respect the congestion controller window. |
| 5994 | * 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] | 5995 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5996 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5997 | size_t path_room; |
| 5998 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5999 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6000 | if (end - beg > path_room) |
| 6001 | end = beg + path_room; |
| 6002 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6003 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6004 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6005 | * length field if any. |
| 6006 | */ |
| 6007 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6008 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6009 | goto no_room; |
| 6010 | |
| 6011 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6012 | 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] | 6013 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6014 | *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] | 6015 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6016 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6017 | !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] | 6018 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6019 | !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] | 6020 | goto no_room; |
| 6021 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6022 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 6023 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6024 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6025 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6026 | /* Build an ACK frame if required. */ |
| 6027 | ack_frm_len = 0; |
Frédéric Lécaille | 337108e | 2022-04-25 10:38:27 +0200 | [diff] [blame] | 6028 | 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] | 6029 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6030 | 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] | 6031 | ack_frm.tx_ack.arngs = arngs; |
| 6032 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 6033 | qel->pktns->tx.ack_delay = |
| 6034 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 6035 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 6036 | } |
| 6037 | 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] | 6038 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 6039 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 6040 | * that from here, we do not know if we will have to send a PING frame. |
| 6041 | * This will be decided after having computed the ack-eliciting frames |
| 6042 | * to be added to this packet. |
| 6043 | */ |
| 6044 | 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] | 6045 | if (!ack_frm_len) |
| 6046 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6047 | } |
| 6048 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6049 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6050 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 6051 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6052 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6053 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6054 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6055 | 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] | 6056 | /* Initialize the length of the frames built below to <len>. |
| 6057 | * If any frame could be successfully built by qc_build_frms(), |
| 6058 | * we will have len_frms > len. |
| 6059 | */ |
| 6060 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6061 | if (!qc_build_frms(&frm_list, frms, |
| 6062 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6063 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6064 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 6065 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 6066 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6067 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6068 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6069 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6070 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 6071 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 6072 | * for the encryption tag. It must be taken into an account for the length |
| 6073 | * of this packet. |
| 6074 | */ |
| 6075 | if (len_frms) |
| 6076 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 6077 | else |
| 6078 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6079 | /* CONNECTION_CLOSE frame */ |
| 6080 | if (cc) { |
| 6081 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6082 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6083 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6084 | len += qc_frm_len(&cc_frm); |
| 6085 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6086 | add_ping_frm = 0; |
| 6087 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6088 | len_sz = quic_int_getsize(len); |
| 6089 | /* Add this packet size to <dglen> */ |
| 6090 | dglen += head_len + len_sz + len; |
| 6091 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 6092 | /* This is a maximum padding size */ |
| 6093 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 6094 | /* The length field value is of this packet is <len> + <padding_len> |
| 6095 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 6096 | * <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] | 6097 | * sizes from <padding_len>. |
| 6098 | */ |
| 6099 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 6100 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6101 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6102 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6103 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6104 | /* 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] | 6105 | add_ping_frm = 1; |
| 6106 | len += 1; |
| 6107 | } |
| 6108 | /* 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] | 6109 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6110 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 6111 | } |
| 6112 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6113 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 6114 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6115 | |
| 6116 | /* Packet number field address. */ |
| 6117 | *buf_pn = pos; |
| 6118 | |
| 6119 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6120 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 6121 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6122 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6123 | if (ack_frm_len) { |
| 6124 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 6125 | goto no_room; |
| 6126 | |
| 6127 | 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] | 6128 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6129 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6130 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6131 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6132 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6133 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6134 | unsigned char *spos = pos; |
| 6135 | |
| 6136 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6137 | ssize_t room = end - pos; |
| 6138 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6139 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6140 | /* TODO: this should not have happened if qc_build_frms() |
| 6141 | * had correctly computed and sized the frames to be |
| 6142 | * added to this packet. Note that <cf> was added |
| 6143 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 6144 | */ |
| 6145 | LIST_DELETE(&cf->list); |
| 6146 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6147 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6148 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6149 | |
| 6150 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6151 | quic_tx_packet_refinc(pkt); |
| 6152 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | /* Build a PING frame if needed. */ |
| 6157 | if (add_ping_frm) { |
| 6158 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6159 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6160 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6161 | } |
| 6162 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6163 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6164 | if (cc) { |
| 6165 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 6166 | goto no_room; |
| 6167 | |
| 6168 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 6169 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6170 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6171 | /* Build a PADDING frame if needed. */ |
| 6172 | if (padding_len) { |
| 6173 | frm.type = QUIC_FT_PADDING; |
| 6174 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6175 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6176 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6177 | } |
| 6178 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6179 | /* If this packet is ack-eliciting and we are probing let's |
| 6180 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6181 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6182 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 6183 | qel->pktns->tx.pto_probe) |
| 6184 | qel->pktns->tx.pto_probe--; |
| 6185 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6186 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6187 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6188 | 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] | 6189 | |
| 6190 | return 1; |
| 6191 | |
| 6192 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6193 | /* 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] | 6194 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | d8b798d | 2022-04-25 17:48:40 +0200 | [diff] [blame] | 6195 | 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] | 6196 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6197 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6198 | } |
| 6199 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6200 | 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] | 6201 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6202 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6203 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6204 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 6205 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6206 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6207 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6208 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6209 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6210 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6211 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6212 | } |
| 6213 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6214 | /* 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] | 6215 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 6216 | * of prebuilt frames. |
| 6217 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6218 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 6219 | * -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] | 6220 | * XXX NOTE XXX |
| 6221 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6222 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6223 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6224 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6225 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6226 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6227 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6228 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6229 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6230 | { |
| 6231 | /* The pointer to the packet number field. */ |
| 6232 | unsigned char *buf_pn; |
| 6233 | unsigned char *beg, *end, *payload; |
| 6234 | int64_t pn; |
| 6235 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6236 | struct quic_tls_ctx *tls_ctx; |
| 6237 | struct quic_tx_packet *pkt; |
| 6238 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6239 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6240 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6241 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6242 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6243 | 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] | 6244 | *err = -2; |
| 6245 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6246 | } |
| 6247 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6248 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6249 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6250 | pn_len = 0; |
| 6251 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6252 | |
| 6253 | pn = qel->pktns->tx.next_pn + 1; |
| 6254 | 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] | 6255 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6256 | *err = -1; |
| 6257 | goto err; |
| 6258 | } |
| 6259 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6260 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6261 | payload = buf_pn + pn_len; |
| 6262 | payload_len = end - payload; |
| 6263 | aad_len = payload - beg; |
| 6264 | |
| 6265 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6266 | 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] | 6267 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6268 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6269 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6270 | |
| 6271 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6272 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6273 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 6274 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6275 | 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] | 6276 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6277 | goto err; |
| 6278 | } |
| 6279 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6280 | /* Consume a packet number */ |
| 6281 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6282 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 6283 | 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] | 6284 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6285 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6286 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6287 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6288 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6289 | /* 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] | 6290 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6291 | pkt->in_flight_len = pkt->len; |
| 6292 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6293 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6294 | /* Always reset this flags */ |
| 6295 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6296 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6297 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6298 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6299 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6300 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6301 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6302 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6303 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6304 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6305 | |
| 6306 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6307 | /* TODO: what about the frames which have been built |
| 6308 | * for this packet. |
| 6309 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6310 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6311 | 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] | 6312 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6313 | } |
| 6314 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6315 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6316 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6317 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6318 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6319 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6320 | */ |
| 6321 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6322 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6323 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6324 | |
| 6325 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6326 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6327 | |
| 6328 | es->events |= event_type; |
| 6329 | qcc->subs = es; |
| 6330 | |
| 6331 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6332 | 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] | 6333 | |
| 6334 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6335 | 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] | 6336 | |
| 6337 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6338 | } |
| 6339 | |
| 6340 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6341 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6342 | * subscribe() call. It always returns zero. |
| 6343 | */ |
| 6344 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6345 | { |
| 6346 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6347 | } |
| 6348 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6349 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 6350 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6351 | */ |
| 6352 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 6353 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 6354 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6355 | |
| 6356 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 6357 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6358 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6359 | if (*xprt_ctx) |
| 6360 | goto out; |
| 6361 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6362 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6363 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6364 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 6365 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6366 | |
| 6367 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6368 | } |
| 6369 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6370 | /* Start the QUIC transport layer */ |
| 6371 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 6372 | { |
| 6373 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 6374 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6375 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6376 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6377 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 6378 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 6379 | /* prepare a CONNECTION_CLOSE frame */ |
| 6380 | qc->err_code = QC_ERR_APPLICATION_ERROR; |
| 6381 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 6382 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | /* mux-quic can now be considered ready. */ |
| 6386 | qc->mux_state = QC_MUX_READY; |
| 6387 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6388 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 6389 | return 1; |
| 6390 | } |
| 6391 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6392 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 6393 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6394 | 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] | 6395 | return NULL; |
| 6396 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6397 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6398 | } |
| 6399 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6400 | /* transport-layer operations for QUIC connections. */ |
| 6401 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 6402 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6403 | .subscribe = quic_conn_subscribe, |
| 6404 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6405 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6406 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6407 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 6408 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6409 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6410 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6411 | .name = "QUIC", |
| 6412 | }; |
| 6413 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6414 | static void __quic_conn_init(void) |
| 6415 | { |
| 6416 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 6417 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 6418 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6419 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6420 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6421 | static void __quic_conn_deinit(void) |
| 6422 | { |
| 6423 | BIO_meth_free(ha_quic_meth); |
| 6424 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6425 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6426 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6427 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 6428 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 6429 | * 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] | 6430 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6431 | 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] | 6432 | { |
| 6433 | unsigned char *pos; |
| 6434 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6435 | struct quic_dghdlr *dghdlr = ctx; |
| 6436 | struct quic_dgram *dgram; |
| 6437 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6438 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6439 | 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] | 6440 | pos = dgram->buf; |
| 6441 | end = pos + dgram->len; |
| 6442 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6443 | struct quic_rx_packet *pkt; |
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 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 6446 | if (!pkt) |
| 6447 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6448 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6449 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6450 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 6451 | 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] | 6452 | first_pkt = 0; |
| 6453 | pos += pkt->len; |
| 6454 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6455 | } while (pos < end); |
| 6456 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6457 | /* Increasing the received bytes counter by the UDP datagram length |
| 6458 | * if this datagram could be associated to a connection. |
| 6459 | */ |
| 6460 | if (dgram->qc) |
| 6461 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 6462 | |
| 6463 | /* Mark this datagram as consumed */ |
| 6464 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6465 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6466 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6467 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6468 | |
| 6469 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6470 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6471 | } |
| 6472 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6473 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 6474 | * Returns 1 if succeeded, 0 if not. |
| 6475 | */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 6476 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 6477 | unsigned char **dcid, size_t *dcid_len) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6478 | { |
| 6479 | int long_header; |
| 6480 | size_t minlen, skip; |
| 6481 | |
| 6482 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 6483 | goto err; |
| 6484 | |
| 6485 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 6486 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 6487 | 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] | 6488 | 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] | 6489 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6490 | goto err; |
| 6491 | |
| 6492 | buf += skip; |
| 6493 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 6494 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 6495 | goto err; |
| 6496 | |
| 6497 | *dcid = buf; |
| 6498 | |
| 6499 | return 1; |
| 6500 | |
| 6501 | err: |
| 6502 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 6503 | return 0; |
| 6504 | } |
| 6505 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 6506 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 6507 | void qc_notify_close(struct quic_conn *qc) |
| 6508 | { |
| 6509 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 6510 | return; |
| 6511 | |
| 6512 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 6513 | |
| 6514 | /* wake up the MUX */ |
| 6515 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 6516 | qc->conn->mux->wake(qc->conn); |
| 6517 | } |
| 6518 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6519 | /* Function to automatically activate QUIC traces on stdout. |
| 6520 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 6521 | * Main use for now is in the docker image for QUIC interop testing. |
| 6522 | */ |
| 6523 | static void quic_init_stdout_traces(void) |
| 6524 | { |
| 6525 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 6526 | trace_quic.sink = sink_find("stdout"); |
| 6527 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6528 | trace_quic.state = TRACE_STATE_RUNNING; |
| 6529 | #endif |
| 6530 | } |
| 6531 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 6532 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6533 | /* |
| 6534 | * Local variables: |
| 6535 | * c-indent-level: 8 |
| 6536 | * c-basic-offset: 8 |
| 6537 | * End: |
| 6538 | */ |