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> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 41 | #include <haproxy/pipe.h> |
| 42 | #include <haproxy/proxy.h> |
| 43 | #include <haproxy/quic_cc.h> |
| 44 | #include <haproxy/quic_frame.h> |
| 45 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 46 | #include <haproxy/quic_sock.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 47 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 48 | #include <haproxy/cbuf.h> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 49 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 51 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 52 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 53 | #include <haproxy/task.h> |
| 54 | #include <haproxy/trace.h> |
| 55 | #include <haproxy/xprt_quic.h> |
| 56 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 57 | /* list of supported QUIC versions by this implementation */ |
| 58 | static int quic_supported_version[] = { |
| 59 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 60 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 61 | |
| 62 | /* placeholder, do not add entry after this */ |
| 63 | 0x0 |
| 64 | }; |
| 65 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 66 | /* This is the values of some QUIC transport parameters when absent. |
| 67 | * Should be used to initialize any transport parameters (local or remote) |
| 68 | * before updating them with customized values. |
| 69 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 71 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 72 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 73 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 74 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* trace source and events */ |
| 78 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 79 | const struct trace_source *src, |
| 80 | const struct ist where, const struct ist func, |
| 81 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 82 | |
| 83 | static const struct trace_event quic_trace_events[] = { |
| 84 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 85 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 86 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 90 | { .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] | 91 | { .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] | 92 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 94 | { .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] | 95 | { .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] | 96 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 101 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 102 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 103 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 106 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 107 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 108 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 109 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 110 | { .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] | 111 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 112 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 113 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 114 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 115 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 116 | { .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] | 117 | { .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] | 118 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 119 | { .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] | 120 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 121 | { .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] | 122 | { .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] | 123 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 124 | { /* end */ } |
| 125 | }; |
| 126 | |
| 127 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 128 | /* arg1 */ { /* already used by the connection */ }, |
| 129 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 130 | /* arg3 */ { }, |
| 131 | /* arg4 */ { } |
| 132 | }; |
| 133 | |
| 134 | static const struct name_desc quic_trace_decoding[] = { |
| 135 | #define QUIC_VERB_CLEAN 1 |
| 136 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 137 | { /* end */ } |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | struct trace_source trace_quic = { |
| 142 | .name = IST("quic"), |
| 143 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 144 | .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] | 145 | .default_cb = quic_trace, |
| 146 | .known_events = quic_trace_events, |
| 147 | .lockon_args = quic_trace_lockon_args, |
| 148 | .decoding = quic_trace_decoding, |
| 149 | .report_events = ~0, /* report everything by default */ |
| 150 | }; |
| 151 | |
| 152 | #define TRACE_SOURCE &trace_quic |
| 153 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 154 | |
| 155 | static BIO_METHOD *ha_quic_meth; |
| 156 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 157 | 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] | 158 | 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] | 159 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 160 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 161 | 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] | 162 | DECLARE_POOL(pool_head_quic_connection_id, |
| 163 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 164 | 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] | 165 | 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] | 166 | 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] | 167 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 168 | DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 169 | 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] | 170 | 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] | 171 | 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] | 172 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 173 | 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] | 174 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 175 | 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] | 176 | int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 177 | 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] | 178 | 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] | 179 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 180 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 181 | /* Only for debug purpose */ |
| 182 | struct enc_debug_info { |
| 183 | unsigned char *payload; |
| 184 | size_t payload_len; |
| 185 | unsigned char *aad; |
| 186 | size_t aad_len; |
| 187 | uint64_t pn; |
| 188 | }; |
| 189 | |
| 190 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 191 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 192 | unsigned char *payload, size_t payload_len, |
| 193 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 194 | { |
| 195 | edi->payload = payload; |
| 196 | edi->payload_len = payload_len; |
| 197 | edi->aad = aad; |
| 198 | edi->aad_len = aad_len; |
| 199 | edi->pn = pn; |
| 200 | } |
| 201 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 202 | /* Trace callback for QUIC. |
| 203 | * These traces always expect that arg1, if non-null, is of type connection. |
| 204 | */ |
| 205 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 206 | const struct ist where, const struct ist func, |
| 207 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 208 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 209 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 210 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 211 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 212 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 213 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 214 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 216 | chunk_appendf(&trace_buf, "\n odcid"); |
| 217 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 218 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 219 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 220 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 221 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 225 | const enum ssl_encryption_level_t *level = a2; |
| 226 | const size_t *len = a3; |
| 227 | |
| 228 | if (level) { |
| 229 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 230 | |
| 231 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 232 | } |
| 233 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 234 | 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] | 235 | } |
| 236 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 237 | /* Initial read & write secrets. */ |
| 238 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 239 | const unsigned char *rx_sec = a2; |
| 240 | const unsigned char *tx_sec = a3; |
| 241 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 242 | tls_ctx = &qc->els[level].tls_ctx; |
| 243 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 244 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 245 | if (rx_sec) |
| 246 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 247 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 248 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 249 | if (tx_sec) |
| 250 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 251 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 255 | const enum ssl_encryption_level_t *level = a2; |
| 256 | const unsigned char *secret = a3; |
| 257 | const size_t *secret_len = a4; |
| 258 | |
| 259 | if (level) { |
| 260 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 261 | |
| 262 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 263 | if (secret && secret_len) |
| 264 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 265 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 266 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 267 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 272 | const enum ssl_encryption_level_t *level = a2; |
| 273 | const unsigned char *secret = a3; |
| 274 | const size_t *secret_len = a4; |
| 275 | |
| 276 | if (level) { |
| 277 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 278 | |
| 279 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 280 | if (secret && secret_len) |
| 281 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 282 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 283 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 284 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 288 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 289 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 290 | const struct list *l = a2; |
| 291 | |
| 292 | if (l) { |
| 293 | const struct quic_frame *frm; |
| 294 | list_for_each_entry(frm, l, list) |
| 295 | chunk_frm_appendf(&trace_buf, frm); |
| 296 | } |
| 297 | } |
| 298 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 299 | if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 300 | const struct quic_tx_packet *pkt = a2; |
| 301 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 302 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 303 | |
| 304 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 305 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 306 | 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] | 307 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 308 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 309 | (unsigned long long)qc->path->cwnd, |
| 310 | (unsigned long long)qc->path->prep_in_flight, |
| 311 | (unsigned long long)qc->path->in_flight, |
| 312 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 313 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 314 | } |
| 315 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 316 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 317 | if (pkt->pn_node.key != (uint64_t)-1) |
| 318 | 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] | 319 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 320 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 321 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 322 | (unsigned long long)qc->rx.bytes, |
| 323 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (room) { |
| 327 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 328 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 329 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 333 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 334 | const enum quic_handshake_state *state = a2; |
| 335 | const int *err = a3; |
| 336 | |
| 337 | if (state) |
| 338 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 339 | if (err) |
| 340 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 341 | } |
| 342 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 343 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 344 | const struct quic_rx_packet *pkt = a2; |
| 345 | const unsigned long *pktlen = a3; |
| 346 | const SSL *ssl = a4; |
| 347 | |
| 348 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 349 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 350 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 351 | chunk_appendf(&trace_buf, " kp=%d", |
| 352 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 353 | chunk_appendf(&trace_buf, " el=%c", |
| 354 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 355 | if (pkt->pnl) |
| 356 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 357 | (unsigned long long)pkt->pn); |
| 358 | if (pkt->token_len) |
| 359 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 360 | (unsigned long long)pkt->token_len); |
| 361 | if (pkt->aad_len) |
| 362 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 363 | (unsigned long long)pkt->aad_len); |
| 364 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 365 | pkt->flags, (unsigned long long)pkt->len); |
| 366 | } |
| 367 | if (pktlen) |
| 368 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 369 | if (ssl) { |
| 370 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 371 | chunk_appendf(&trace_buf, " el=%c", |
| 372 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 377 | const struct quic_rx_packet *pkt = a2; |
| 378 | const struct quic_rx_crypto_frm *cf = a3; |
| 379 | const SSL *ssl = a4; |
| 380 | |
| 381 | if (pkt) |
| 382 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 383 | quic_packet_type_enc_level_char(pkt->type), |
| 384 | (unsigned long long)pkt->pn); |
| 385 | if (cf) |
| 386 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 387 | (unsigned long long)cf->offset_node.key, |
| 388 | (unsigned long long)cf->len); |
| 389 | if (ssl) { |
| 390 | 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] | 391 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 392 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 393 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 394 | |
| 395 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 396 | 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] | 397 | } |
| 398 | |
| 399 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 400 | const struct quic_frame *frm = a2; |
| 401 | |
| 402 | if (frm) |
| 403 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 404 | } |
| 405 | |
| 406 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 407 | const struct quic_enc_level *qel = a2; |
| 408 | |
| 409 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 410 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 411 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 412 | " 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] | 413 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 414 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 415 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 416 | (unsigned long long)qc->path->cwnd, |
| 417 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 418 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 419 | (unsigned long long)pktns->tx.in_flight, |
| 420 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 424 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 425 | const struct enc_debug_info *edi = a2; |
| 426 | |
| 427 | if (edi) |
| 428 | chunk_appendf(&trace_buf, |
| 429 | " payload=@%p payload_len=%llu" |
| 430 | " aad=@%p aad_len=%llu pn=%llu", |
| 431 | edi->payload, (unsigned long long)edi->payload_len, |
| 432 | edi->aad, (unsigned long long)edi->aad_len, |
| 433 | (unsigned long long)edi->pn); |
| 434 | } |
| 435 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 436 | if (mask & QUIC_EV_CONN_RMHP) { |
| 437 | const struct quic_rx_packet *pkt = a2; |
| 438 | |
| 439 | if (pkt) { |
| 440 | const int *ret = a3; |
| 441 | |
| 442 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 443 | if (ret && *ret) |
| 444 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 445 | pkt->pnl, (unsigned long long)pkt->pn); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 450 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 451 | const unsigned long *val1 = a3; |
| 452 | const unsigned long *val2 = a4; |
| 453 | |
| 454 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 455 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 456 | if (val1) |
| 457 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 458 | if (val2) |
| 459 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 460 | } |
| 461 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 462 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 463 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 464 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 465 | |
| 466 | if (s) |
| 467 | 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] | 468 | if (stream) |
| 469 | 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] | 470 | } |
| 471 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 472 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 473 | const unsigned int *rtt_sample = a2; |
| 474 | const unsigned int *ack_delay = a3; |
| 475 | const struct quic_loss *ql = a4; |
| 476 | |
| 477 | if (rtt_sample) |
| 478 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 479 | if (ack_delay) |
| 480 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 481 | if (ql) |
| 482 | chunk_appendf(&trace_buf, |
| 483 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 484 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 485 | } |
| 486 | if (mask & QUIC_EV_CONN_CC) { |
| 487 | const struct quic_cc_event *ev = a2; |
| 488 | const struct quic_cc *cc = a3; |
| 489 | |
| 490 | if (a2) |
| 491 | quic_cc_event_trace(&trace_buf, ev); |
| 492 | if (a3) |
| 493 | quic_cc_state_trace(&trace_buf, cc); |
| 494 | } |
| 495 | |
| 496 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 497 | const struct quic_pktns *pktns = a2; |
| 498 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 499 | |
| 500 | if (pktns) { |
| 501 | chunk_appendf(&trace_buf, " pktns=%s", |
| 502 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 503 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 504 | if (pktns->tx.loss_time) |
| 505 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 506 | 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] | 507 | } |
| 508 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 509 | struct quic_tx_packet *pkt; |
| 510 | |
| 511 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 512 | list_for_each_entry(pkt, lost_pkts, list) |
| 513 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | 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] | 518 | const struct quic_pktns *pktns = a2; |
| 519 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 520 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 521 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 522 | if (ifae_pkts) |
| 523 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 524 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 525 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 526 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 527 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 528 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 529 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 530 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 531 | if (pktns->tx.in_flight) |
| 532 | 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] | 533 | if (pktns->tx.loss_time) |
| 534 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 535 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 536 | } |
| 537 | if (mask & QUIC_EV_CONN_SPTO) { |
| 538 | if (pktns->tx.time_of_last_eliciting) |
| 539 | chunk_appendf(&trace_buf, " tole=%dms", |
| 540 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 541 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 542 | 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] | 543 | } |
| 544 | } |
| 545 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 546 | 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] | 547 | chunk_appendf(&trace_buf, |
| 548 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 553 | const struct quic_tx_packet *pkt = a2; |
| 554 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 555 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 556 | (unsigned long long)qc->path->cwnd, |
| 557 | (unsigned long long)qc->path->prep_in_flight, |
| 558 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 559 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 560 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 561 | (unsigned long)pkt->pn_node.key, |
| 562 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 563 | 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] | 564 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 565 | } |
| 566 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 567 | |
| 568 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 569 | const uint8_t *alert = a2; |
| 570 | const enum ssl_encryption_level_t *level = a3; |
| 571 | |
| 572 | if (alert) |
| 573 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 574 | if (level) |
| 575 | chunk_appendf(&trace_buf, " el=%c", |
| 576 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 577 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 578 | |
| 579 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 580 | const size_t *sz1 = a2; |
| 581 | const size_t *sz2 = a3; |
| 582 | const size_t *sz3 = a4; |
| 583 | |
| 584 | if (sz1) |
| 585 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 586 | if (sz2) |
| 587 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 588 | if (sz3) |
| 589 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 590 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 591 | |
| 592 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 593 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 594 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 595 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 596 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 597 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 598 | } |
| 599 | if (mask & QUIC_EV_CONN_LPKT) { |
| 600 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 601 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 602 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 603 | if (pkt) { |
| 604 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 605 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 606 | if (pkt->pn_node.key != (uint64_t)-1) |
| 607 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 608 | } |
| 609 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 610 | if (len) |
| 611 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | } |
| 615 | |
| 616 | /* 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] | 617 | 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] | 618 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 619 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 620 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 621 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 622 | return 1; |
| 623 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 624 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 625 | 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] | 626 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 627 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 628 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 629 | return 1; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 635 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 636 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 637 | 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] | 638 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 639 | struct quic_pktns *pktns; |
| 640 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 641 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 642 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 643 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 644 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 645 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 646 | pktns = quic_loss_pktns(qc); |
| 647 | if (tick_isset(pktns->tx.loss_time)) { |
| 648 | qc->timer = pktns->tx.loss_time; |
| 649 | goto out; |
| 650 | } |
| 651 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 652 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 653 | * cancelled for a server which reached the anti-amplification limit. |
| 654 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 655 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 656 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 657 | 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] | 658 | qc->timer = TICK_ETERNITY; |
| 659 | goto out; |
| 660 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 661 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 662 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 663 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 664 | /* Timer cancellation. */ |
| 665 | qc->timer = TICK_ETERNITY; |
| 666 | goto out; |
| 667 | } |
| 668 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 669 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 670 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 671 | if (tick_isset(pto)) |
| 672 | qc->timer = pto; |
| 673 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 674 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 675 | if (tick_is_expired(qc->timer, now_ms)) { |
| 676 | 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] | 677 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 678 | } |
| 679 | else { |
| 680 | 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] | 681 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 682 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 683 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 684 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 687 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 688 | * connection. |
| 689 | * Return 1 if succeeded, 0 if not. |
| 690 | */ |
| 691 | static int quic_tls_key_update(struct quic_conn *qc) |
| 692 | { |
| 693 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 694 | struct quic_tls_secrets *rx, *tx; |
| 695 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 696 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 697 | |
| 698 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 699 | rx = &tls_ctx->rx; |
| 700 | tx = &tls_ctx->tx; |
| 701 | nxt_rx = &qc->ku.nxt_rx; |
| 702 | nxt_tx = &qc->ku.nxt_tx; |
| 703 | |
| 704 | /* Prepare new RX secrets */ |
| 705 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 706 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 707 | 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] | 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 712 | nxt_rx->key, nxt_rx->keylen, |
| 713 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 714 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 715 | 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] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /* Prepare new TX secrets */ |
| 720 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 721 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 722 | 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] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 727 | nxt_tx->key, nxt_tx->keylen, |
| 728 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 729 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 730 | 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] | 731 | return 0; |
| 732 | } |
| 733 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 734 | if (nxt_rx->ctx) { |
| 735 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 736 | nxt_rx->ctx = NULL; |
| 737 | } |
| 738 | |
| 739 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 740 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | if (nxt_tx->ctx) { |
| 745 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 746 | nxt_tx->ctx = NULL; |
| 747 | } |
| 748 | |
| 749 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 750 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 751 | return 0; |
| 752 | } |
| 753 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 758 | * Must be used after having updated them. |
| 759 | * Always succeeds. |
| 760 | */ |
| 761 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 762 | { |
| 763 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 764 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 765 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 766 | |
| 767 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 768 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 769 | curr_secret = tls_ctx->rx.secret; |
| 770 | curr_iv = tls_ctx->rx.iv; |
| 771 | curr_key = tls_ctx->rx.key; |
| 772 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 773 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 774 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 775 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 776 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 777 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 778 | 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] | 779 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 780 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 781 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 782 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 783 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 784 | qc->ku.prv_rx.secret = curr_secret; |
| 785 | qc->ku.prv_rx.iv = curr_iv; |
| 786 | qc->ku.prv_rx.key = curr_key; |
| 787 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 788 | |
| 789 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 790 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 791 | curr_secret = tls_ctx->tx.secret; |
| 792 | curr_iv = tls_ctx->tx.iv; |
| 793 | curr_key = tls_ctx->tx.key; |
| 794 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 795 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 796 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 797 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 798 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 799 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 800 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 801 | qc->ku.nxt_tx.secret = curr_secret; |
| 802 | qc->ku.nxt_tx.iv = curr_iv; |
| 803 | qc->ku.nxt_tx.key = curr_key; |
| 804 | } |
| 805 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 806 | #ifndef OPENSSL_IS_BORINGSSL |
| 807 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 808 | const uint8_t *read_secret, |
| 809 | const uint8_t *write_secret, size_t secret_len) |
| 810 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 811 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 812 | 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] | 813 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 814 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 815 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 816 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 817 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 818 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 819 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 820 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 821 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 822 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 823 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 824 | 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] | 825 | goto err; |
| 826 | } |
| 827 | |
| 828 | rx = &tls_ctx->rx; |
| 829 | tx = &tls_ctx->tx; |
| 830 | |
| 831 | rx->aead = tx->aead = tls_aead(cipher); |
| 832 | rx->md = tx->md = tls_md(cipher); |
| 833 | rx->hp = tx->hp = tls_hp(cipher); |
| 834 | |
| 835 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 836 | 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] | 837 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 838 | 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] | 839 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 842 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 843 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 844 | goto err; |
| 845 | } |
| 846 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 847 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 848 | * listener. Note that a listener derives only RX secrets for this |
| 849 | * level. |
| 850 | */ |
| 851 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 852 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 853 | |
| 854 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 855 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 856 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 857 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 858 | 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] | 859 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 860 | 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] | 861 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 864 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 865 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 866 | goto err; |
| 867 | } |
| 868 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 869 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 870 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 871 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 872 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 873 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 874 | /* 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] | 875 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 876 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 877 | 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] | 878 | goto err; |
| 879 | } |
| 880 | |
| 881 | memcpy(rx->secret, read_secret, secret_len); |
| 882 | rx->secretlen = secret_len; |
| 883 | memcpy(tx->secret, write_secret, secret_len); |
| 884 | tx->secretlen = secret_len; |
| 885 | /* Initialize all the secret keys lengths */ |
| 886 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 887 | /* Prepare the next key update */ |
| 888 | if (!quic_tls_key_update(qc)) |
| 889 | goto err; |
| 890 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 891 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 892 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 893 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 894 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 895 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 896 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 897 | |
| 898 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 899 | 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] | 900 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 901 | } |
| 902 | #else |
| 903 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 904 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 905 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 906 | */ |
| 907 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 908 | const SSL_CIPHER *cipher, |
| 909 | const uint8_t *secret, size_t secret_len) |
| 910 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 911 | 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] | 912 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 913 | &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] | 914 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 915 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 916 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 917 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 918 | goto out; |
| 919 | } |
| 920 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 921 | tls_ctx->rx.aead = tls_aead(cipher); |
| 922 | tls_ctx->rx.md = tls_md(cipher); |
| 923 | tls_ctx->rx.hp = tls_hp(cipher); |
| 924 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 925 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 926 | goto err; |
| 927 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 928 | 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] | 929 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 930 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 931 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 932 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 933 | 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] | 934 | goto err; |
| 935 | } |
| 936 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 937 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 938 | const unsigned char *buf; |
| 939 | size_t buflen; |
| 940 | |
| 941 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 942 | if (!buflen) |
| 943 | goto err; |
| 944 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 945 | 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] | 946 | goto err; |
| 947 | } |
| 948 | |
| 949 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 950 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 951 | 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] | 952 | |
| 953 | return 1; |
| 954 | |
| 955 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 956 | 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] | 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 961 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 962 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 963 | */ |
| 964 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 965 | const SSL_CIPHER *cipher, |
| 966 | const uint8_t *secret, size_t secret_len) |
| 967 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 968 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 969 | 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] | 970 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 971 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 972 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 973 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 974 | goto out; |
| 975 | } |
| 976 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 977 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 978 | goto err; |
| 979 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 980 | tls_ctx->tx.aead = tls_aead(cipher); |
| 981 | tls_ctx->tx.md = tls_md(cipher); |
| 982 | tls_ctx->tx.hp = tls_hp(cipher); |
| 983 | |
| 984 | 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] | 985 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 986 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 987 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 988 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 989 | 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] | 990 | goto err; |
| 991 | } |
| 992 | |
| 993 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 994 | 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] | 995 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 996 | return 1; |
| 997 | |
| 998 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 999 | 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] | 1000 | return 0; |
| 1001 | } |
| 1002 | #endif |
| 1003 | |
| 1004 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1005 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1006 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1007 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 1008 | * store all the data. |
| 1009 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1010 | */ |
| 1011 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 1012 | const unsigned char *data, size_t len) |
| 1013 | { |
| 1014 | struct quic_crypto_buf **qcb; |
| 1015 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1016 | size_t cf_offset, cf_len, *nb_buf; |
| 1017 | unsigned char *pos; |
| 1018 | |
| 1019 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1020 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1021 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1022 | cf_len = len; |
| 1023 | |
| 1024 | while (len) { |
| 1025 | size_t to_copy, room; |
| 1026 | |
| 1027 | pos = (*qcb)->data + (*qcb)->sz; |
| 1028 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1029 | to_copy = len > room ? room : len; |
| 1030 | if (to_copy) { |
| 1031 | memcpy(pos, data, to_copy); |
| 1032 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1033 | qel->tx.crypto.sz += to_copy; |
| 1034 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1035 | len -= to_copy; |
| 1036 | data += to_copy; |
| 1037 | } |
| 1038 | else { |
| 1039 | struct quic_crypto_buf **tmp; |
| 1040 | |
| 1041 | tmp = realloc(qel->tx.crypto.bufs, |
| 1042 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1043 | if (tmp) { |
| 1044 | qel->tx.crypto.bufs = tmp; |
| 1045 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1046 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1047 | if (!*qcb) |
| 1048 | return 0; |
| 1049 | |
| 1050 | (*qcb)->sz = 0; |
| 1051 | ++*nb_buf; |
| 1052 | } |
| 1053 | else { |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1060 | * have been buffered. |
| 1061 | */ |
| 1062 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1063 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1064 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1065 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1066 | /* There is at most one CRYPTO frame in this packet number |
| 1067 | * space. Let's look for it. |
| 1068 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1069 | 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] | 1070 | if (frm->type != QUIC_FT_CRYPTO) |
| 1071 | continue; |
| 1072 | |
| 1073 | /* Found */ |
| 1074 | found = frm; |
| 1075 | break; |
| 1076 | } |
| 1077 | |
| 1078 | if (found) { |
| 1079 | found->crypto.len += cf_len; |
| 1080 | } |
| 1081 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1082 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1083 | if (!frm) |
| 1084 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1085 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1086 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1087 | frm->type = QUIC_FT_CRYPTO; |
| 1088 | frm->crypto.offset = cf_offset; |
| 1089 | frm->crypto.len = cf_len; |
| 1090 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1091 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1092 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | return len == 0; |
| 1096 | } |
| 1097 | |
| 1098 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1099 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1100 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1101 | { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1102 | qc->err_code = QC_ERR_CRYPTO_ERROR | alert; |
| 1103 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1104 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1107 | /* Set the application for <qc> QUIC connection. |
| 1108 | * Return 1 if succeeded, 0 if not. |
| 1109 | */ |
| 1110 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1111 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1112 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1113 | qc->app_ops = &h3_ops; |
| 1114 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1115 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1116 | else |
| 1117 | return 0; |
| 1118 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1119 | return 1; |
| 1120 | } |
| 1121 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1122 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1123 | * wants to provide the QUIC layer with CRYPTO data. |
| 1124 | * Returns 1 if succeeded, 0 if not. |
| 1125 | */ |
| 1126 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1127 | const uint8_t *data, size_t len) |
| 1128 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1129 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1130 | enum quic_tls_enc_level tel; |
| 1131 | struct quic_enc_level *qel; |
| 1132 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1133 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1134 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1135 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1136 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1137 | goto out; |
| 1138 | } |
| 1139 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1140 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1141 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1142 | 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] | 1143 | goto err; |
| 1144 | } |
| 1145 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1146 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1147 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1148 | 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] | 1149 | goto err; |
| 1150 | } |
| 1151 | |
| 1152 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1153 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1154 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1155 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1156 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1157 | return 1; |
| 1158 | |
| 1159 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1160 | 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] | 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | int ha_quic_flush_flight(SSL *ssl) |
| 1165 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1166 | 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] | 1167 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1168 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1169 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1170 | |
| 1171 | return 1; |
| 1172 | } |
| 1173 | |
| 1174 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1175 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1176 | 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] | 1177 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1178 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1179 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1180 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1181 | return 1; |
| 1182 | } |
| 1183 | |
| 1184 | /* QUIC TLS methods */ |
| 1185 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1186 | #ifdef OPENSSL_IS_BORINGSSL |
| 1187 | .set_read_secret = ha_set_rsec, |
| 1188 | .set_write_secret = ha_set_wsec, |
| 1189 | #else |
| 1190 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1191 | #endif |
| 1192 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1193 | .flush_flight = ha_quic_flush_flight, |
| 1194 | .send_alert = ha_quic_send_alert, |
| 1195 | }; |
| 1196 | |
| 1197 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1198 | * Returns an error count. |
| 1199 | */ |
| 1200 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1201 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1202 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1203 | int cfgerr = 0; |
| 1204 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1205 | long options = |
| 1206 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1207 | SSL_OP_SINGLE_ECDH_USE | |
| 1208 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1209 | SSL_CTX *ctx; |
| 1210 | |
| 1211 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1212 | bind_conf->initial_ctx = ctx; |
| 1213 | |
| 1214 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1215 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1216 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1217 | 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] | 1218 | |
| 1219 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1220 | #ifdef OPENSSL_IS_BORINGSSL |
| 1221 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1222 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1223 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1224 | if (bind_conf->ssl_conf.early_data) { |
| 1225 | 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] | 1226 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1227 | } |
| 1228 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1229 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1230 | #else |
| 1231 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1232 | #endif |
| 1233 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1234 | #endif |
| 1235 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1236 | |
| 1237 | return cfgerr; |
| 1238 | } |
| 1239 | |
| 1240 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1241 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1242 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1243 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1244 | */ |
| 1245 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1246 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1247 | { |
| 1248 | uint64_t expected_pn = largest_pn + 1; |
| 1249 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1250 | uint64_t pn_hwin = pn_win / 2; |
| 1251 | uint64_t pn_mask = pn_win - 1; |
| 1252 | uint64_t candidate_pn; |
| 1253 | |
| 1254 | |
| 1255 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1256 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1257 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1258 | candidate_pn + pn_hwin <= expected_pn) |
| 1259 | return candidate_pn + pn_win; |
| 1260 | |
| 1261 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1262 | return candidate_pn - pn_win; |
| 1263 | |
| 1264 | return candidate_pn; |
| 1265 | } |
| 1266 | |
| 1267 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1268 | * cryptographic context. |
| 1269 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1270 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1271 | * <end> points to one byte past the end of this packet. |
| 1272 | * Returns 1 if succeeded, 0 if not. |
| 1273 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1274 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1275 | 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] | 1276 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1277 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1278 | { |
| 1279 | int ret, outlen, i, pnlen; |
| 1280 | uint64_t packet_number; |
| 1281 | uint32_t truncated_pn = 0; |
| 1282 | unsigned char mask[5] = {0}; |
| 1283 | unsigned char *sample; |
| 1284 | EVP_CIPHER_CTX *cctx; |
| 1285 | unsigned char *hp_key; |
| 1286 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1287 | /* Check there is enough data in this packet. */ |
| 1288 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1289 | 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] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | cctx = EVP_CIPHER_CTX_new(); |
| 1294 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1295 | 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] | 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | ret = 0; |
| 1300 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1301 | |
| 1302 | hp_key = tls_ctx->rx.hp_key; |
| 1303 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1304 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1305 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1306 | 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] | 1307 | goto out; |
| 1308 | } |
| 1309 | |
| 1310 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1311 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1312 | for (i = 0; i < pnlen; i++) { |
| 1313 | pn[i] ^= mask[i + 1]; |
| 1314 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1315 | } |
| 1316 | |
| 1317 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1318 | /* Store remaining information for this unprotected header */ |
| 1319 | pkt->pn = packet_number; |
| 1320 | pkt->pnl = pnlen; |
| 1321 | |
| 1322 | ret = 1; |
| 1323 | |
| 1324 | out: |
| 1325 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1326 | |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1331 | * address, with <payload_len> as payload length, <aad> as address of |
| 1332 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1333 | * context. |
| 1334 | * Returns 1 if succeeded, 0 if not. |
| 1335 | */ |
| 1336 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1337 | 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] | 1338 | 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] | 1339 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1340 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1341 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1342 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1343 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1344 | |
| 1345 | 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] | 1346 | 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] | 1347 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | 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] | 1351 | 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] | 1352 | 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] | 1353 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1357 | |
| 1358 | err: |
| 1359 | 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] | 1360 | 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] | 1361 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1365 | * Returns 1 if succeeded, 0 if not. |
| 1366 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1367 | 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] | 1368 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1369 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1370 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1371 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1372 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1373 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1374 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1375 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1376 | |
| 1377 | kp_changed = 0; |
| 1378 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1379 | /* The two tested bits are not at the same position, |
| 1380 | * this is why they are first both inversed. |
| 1381 | */ |
| 1382 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1383 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1384 | /* The lowest packet number of a previous key phase |
| 1385 | * cannot be null if it really stores previous key phase |
| 1386 | * secrets. |
| 1387 | */ |
| 1388 | if (!pkt->qc->ku.prv_rx.pn) |
| 1389 | return 0; |
| 1390 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1391 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1392 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1393 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1394 | } |
| 1395 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1396 | /* Next key phase */ |
| 1397 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1398 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1399 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1400 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1404 | |
| 1405 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1406 | return 0; |
| 1407 | |
| 1408 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1409 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1410 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1411 | if (!ret) |
| 1412 | return 0; |
| 1413 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1414 | /* Update the keys only if the packet decryption succeeded. */ |
| 1415 | if (kp_changed) { |
| 1416 | quic_tls_rotate_keys(pkt->qc); |
| 1417 | /* Toggle the Key Phase bit */ |
| 1418 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1419 | /* Store the lowest packet number received for the current key phase */ |
| 1420 | tls_ctx->rx.pn = pkt->pn; |
| 1421 | /* Prepare the next key update */ |
| 1422 | if (!quic_tls_key_update(pkt->qc)) |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1426 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1427 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1428 | |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1432 | |
| 1433 | /* Remove references to <frm> frame */ |
| 1434 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1435 | { |
| 1436 | uint64_t pn; |
| 1437 | struct quic_frame *f, *tmp; |
| 1438 | |
| 1439 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1440 | pn = f->pkt->pn_node.key; |
| 1441 | f->origin = NULL; |
| 1442 | LIST_DELETE(&f->ref); |
| 1443 | TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1447 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1448 | 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] | 1449 | { |
| 1450 | uint64_t pn; |
| 1451 | struct quic_frame *origin, *f, *tmp; |
| 1452 | |
| 1453 | /* Identify this frame: a frame copy or one of its copies */ |
| 1454 | origin = frm->origin ? frm->origin : frm; |
| 1455 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1456 | * possibly a copy of <origin> |
| 1457 | */ |
| 1458 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1459 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1460 | * not release the packets (releasing the frames) at this time as |
| 1461 | * they are possibly also to be acknowledged alongside the |
| 1462 | * the current one. |
| 1463 | */ |
| 1464 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1465 | pn = f->pkt->pn_node.key; |
| 1466 | TRACE_PROTO("mark frame as acked from packet", |
| 1467 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1468 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1469 | f->origin = NULL; |
| 1470 | LIST_DELETE(&f->ref); |
| 1471 | } |
| 1472 | LIST_DELETE(&frm->list); |
| 1473 | pn = frm->pkt->pn_node.key; |
| 1474 | quic_tx_packet_refdec(frm->pkt); |
| 1475 | TRACE_PROTO("freeing frame from packet", |
| 1476 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1477 | pool_free(pool_head_quic_frame, frm); |
| 1478 | } |
| 1479 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1480 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1481 | * |
| 1482 | * 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] | 1483 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1484 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1485 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1486 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1487 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1488 | struct eb64_node *frm_node; |
| 1489 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1490 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1491 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1492 | while (frm_node) { |
| 1493 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1494 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1495 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1496 | |
| 1497 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1498 | offset = strm->offset.key; |
| 1499 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1500 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1501 | if (offset > stream->ack_offset) |
| 1502 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1503 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1504 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1505 | /* cf. next comment : frame may be freed at this stage. */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1506 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1507 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1508 | ret = 1; |
| 1509 | } |
| 1510 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1511 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1512 | * has been freed. with the stream frames tree. Nothing to do |
| 1513 | * anymore in here. |
| 1514 | */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1515 | if (!stream) |
| 1516 | return 1; |
| 1517 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1518 | frm_node = eb64_next(frm_node); |
| 1519 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1520 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1521 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1522 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1523 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1524 | |
| 1525 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1528 | /* 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] | 1529 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1530 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1531 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1532 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1533 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1534 | 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] | 1535 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1536 | switch (frm->type) { |
| 1537 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1538 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1539 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1540 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1541 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1542 | const size_t offset = strm_frm->offset.key; |
| 1543 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1544 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1545 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1546 | * 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] | 1547 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1548 | * |
| 1549 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1550 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1551 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1552 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1553 | if (!node) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1554 | 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] | 1555 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1556 | /* early return */ |
| 1557 | return; |
| 1558 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1559 | 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] | 1560 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1561 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1562 | if (offset <= stream->ack_offset) { |
| 1563 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1564 | stream_acked = 1; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1565 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1566 | qc, strm_frm, stream); |
| 1567 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1568 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1569 | if (!stream) { |
| 1570 | /* no need to continue if stream freed. */ |
| 1571 | 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] | 1572 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1573 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1576 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1577 | qc, strm_frm, stream); |
| 1578 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1579 | } |
| 1580 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1581 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1582 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1583 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1584 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1585 | } |
| 1586 | break; |
| 1587 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1588 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1589 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1590 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1591 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1592 | struct qcc *qcc = qc->qcc; |
| 1593 | |
| 1594 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1595 | tasklet_wakeup(qcc->subs->tasklet); |
| 1596 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1597 | if (!qcc->subs->events) |
| 1598 | qcc->subs = NULL; |
| 1599 | } |
| 1600 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1604 | * deallocating them, and their TX frames. |
| 1605 | * Returns the last node reached to be used for the next range. |
| 1606 | * May be NULL if <largest> node could not be found. |
| 1607 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1608 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1609 | struct eb_root *pkts, |
| 1610 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1611 | struct list *newly_acked_pkts, |
| 1612 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1613 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1614 | { |
| 1615 | struct eb64_node *node; |
| 1616 | struct quic_tx_packet *pkt; |
| 1617 | |
| 1618 | if (largest_node) |
| 1619 | node = largest_node; |
| 1620 | else { |
| 1621 | node = eb64_lookup(pkts, largest); |
| 1622 | while (!node && largest > smallest) { |
| 1623 | node = eb64_lookup(pkts, --largest); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1628 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1629 | |
| 1630 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1631 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1632 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1633 | 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] | 1634 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1635 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1636 | node = eb64_prev(node); |
| 1637 | eb64_delete(&pkt->pn_node); |
| 1638 | } |
| 1639 | |
| 1640 | return node; |
| 1641 | } |
| 1642 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1643 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1644 | * 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] | 1645 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1646 | 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] | 1647 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1648 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1649 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1650 | struct quic_frame *frm, *frmbak; |
| 1651 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1652 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1653 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1654 | 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] | 1655 | /* Only for debug */ |
| 1656 | uint64_t pn; |
| 1657 | |
| 1658 | /* 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] | 1659 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1660 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1661 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1662 | /* 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] | 1663 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1664 | /* Remove any reference to this frame */ |
| 1665 | qc_frm_unref(qc, frm); |
| 1666 | switch (frm->type) { |
| 1667 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1668 | { |
| 1669 | struct quic_stream *strm_frm = &frm->stream; |
| 1670 | struct eb64_node *node = NULL; |
| 1671 | struct qc_stream_desc *stream_desc; |
| 1672 | |
| 1673 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1674 | if (!node) { |
| 1675 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 1676 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1677 | qc, frm, &pn); |
| 1678 | pool_free(pool_head_quic_frame, frm); |
| 1679 | continue; |
| 1680 | } |
| 1681 | |
| 1682 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1683 | /* Do not resend this frame if in the "already acked range" */ |
| 1684 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1685 | TRACE_PROTO("ignored frame in already acked range", |
| 1686 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1687 | continue; |
| 1688 | } |
| 1689 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1690 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1691 | TRACE_PROTO("updated partially acked frame", |
| 1692 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1693 | } |
| 1694 | |
| 1695 | break; |
| 1696 | } |
| 1697 | |
| 1698 | default: |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | /* Do not resend probing packet with old data */ |
| 1703 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1704 | TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1705 | qc, frm, &pn); |
| 1706 | if (frm->origin) |
| 1707 | LIST_DELETE(&frm->ref); |
| 1708 | pool_free(pool_head_quic_frame, frm); |
| 1709 | continue; |
| 1710 | } |
| 1711 | |
| 1712 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1713 | TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1714 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1715 | qc, frm, &pn); |
| 1716 | pool_free(pool_head_quic_frame, frm); |
| 1717 | } |
| 1718 | else { |
| 1719 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1720 | LIST_APPEND(&tmp, &frm->list); |
| 1721 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1724 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1725 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1726 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1727 | /* Free <pkt> TX packet and its attached frames. |
| 1728 | * This is the responsability of the caller to remove this packet of |
| 1729 | * any data structure it was possibly attached to. |
| 1730 | */ |
| 1731 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1732 | { |
| 1733 | struct quic_frame *frm, *frmbak; |
| 1734 | |
| 1735 | if (!pkt) |
| 1736 | return; |
| 1737 | |
| 1738 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1739 | LIST_DELETE(&frm->list); |
| 1740 | pool_free(pool_head_quic_frame, frm); |
| 1741 | } |
| 1742 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1743 | } |
| 1744 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1745 | /* Free the TX packets of <pkts> list */ |
| 1746 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1747 | { |
| 1748 | struct quic_tx_packet *pkt, *tmp; |
| 1749 | |
| 1750 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1751 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1752 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1753 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1757 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1758 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1759 | * updating the range which contains <largest_acked_pn>. |
| 1760 | * Never fails. |
| 1761 | */ |
| 1762 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1763 | int64_t largest_acked_pn) |
| 1764 | { |
| 1765 | struct eb64_node *ar, *next_ar; |
| 1766 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1767 | |
| 1768 | ar = eb64_first(&arngs->root); |
| 1769 | while (ar) { |
| 1770 | struct quic_arng_node *ar_node; |
| 1771 | |
| 1772 | next_ar = eb64_next(ar); |
| 1773 | ar_node = eb64_entry(&ar->node, struct quic_arng_node, first); |
| 1774 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1775 | break; |
| 1776 | |
| 1777 | if (largest_acked_pn < ar_node->last) { |
| 1778 | eb64_delete(ar); |
| 1779 | ar_node->first.key = largest_acked_pn + 1; |
| 1780 | eb64_insert(&arngs->root, ar); |
| 1781 | break; |
| 1782 | } |
| 1783 | |
| 1784 | eb64_delete(ar); |
| 1785 | pool_free(pool_head_quic_arng, ar_node); |
| 1786 | arngs->sz--; |
| 1787 | ar = next_ar; |
| 1788 | } |
| 1789 | } |
| 1790 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1791 | /* Send a packet ack event nofication for each newly acked packet of |
| 1792 | * <newly_acked_pkts> list and free them. |
| 1793 | * Always succeeds. |
| 1794 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1795 | 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] | 1796 | struct list *newly_acked_pkts) |
| 1797 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1798 | struct quic_tx_packet *pkt, *tmp; |
| 1799 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1800 | |
| 1801 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1802 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1803 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1804 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1805 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1806 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1807 | /* If this packet contained an ACK frame, proceed to the |
| 1808 | * acknowledging of range of acks from the largest acknowledged |
| 1809 | * packet number which was sent in an ACK frame by this packet. |
| 1810 | */ |
| 1811 | if (pkt->largest_acked_pn != -1) |
| 1812 | 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] | 1813 | ev.ack.acked = pkt->in_flight_len; |
| 1814 | ev.ack.time_sent = pkt->time_sent; |
| 1815 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1816 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1817 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1818 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | } |
| 1822 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1823 | /* Release all the frames attached to <pktns> packet number space */ |
| 1824 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1825 | { |
| 1826 | struct quic_frame *frm, *frmbak; |
| 1827 | |
| 1828 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1829 | LIST_DELETE(&frm->list); |
| 1830 | pool_free(pool_head_quic_frame, frm); |
| 1831 | } |
| 1832 | } |
| 1833 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1834 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1835 | * their TX frames. |
| 1836 | * Send a packet loss event to the congestion controller if |
| 1837 | * in flight packet have been lost. |
| 1838 | * Also frees the packet in <pkts> list. |
| 1839 | * Never fails. |
| 1840 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1841 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1842 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1843 | struct list *pkts, |
| 1844 | uint64_t now_us) |
| 1845 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1846 | 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] | 1847 | uint64_t lost_bytes; |
| 1848 | |
| 1849 | lost_bytes = 0; |
| 1850 | oldest_lost = newest_lost = NULL; |
| 1851 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1852 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1853 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1854 | lost_bytes += pkt->in_flight_len; |
| 1855 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1856 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1857 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1858 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1859 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1860 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1861 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1862 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1863 | if (!oldest_lost) { |
| 1864 | oldest_lost = newest_lost = pkt; |
| 1865 | } |
| 1866 | else { |
| 1867 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1868 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1869 | newest_lost = pkt; |
| 1870 | } |
| 1871 | } |
| 1872 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1873 | if (newest_lost) { |
| 1874 | /* Sent a congestion event to the controller */ |
| 1875 | struct quic_cc_event ev = { |
| 1876 | .type = QUIC_CC_EVT_LOSS, |
| 1877 | .loss.time_sent = newest_lost->time_sent, |
| 1878 | }; |
| 1879 | |
| 1880 | quic_cc_event(&qc->path->cc, &ev); |
| 1881 | } |
| 1882 | |
| 1883 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1884 | * We must check if we are experiencing a persistent congestion. |
| 1885 | * If this is the case, the congestion controller must re-enter |
| 1886 | * slow start state. |
| 1887 | */ |
| 1888 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1889 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1890 | |
| 1891 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1892 | now_ms, qc->max_ack_delay)) |
| 1893 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1894 | } |
| 1895 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1896 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1897 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1898 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1899 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1904 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1905 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1906 | * space if any not deemed lost. |
| 1907 | * Should be called after having received an ACK frame with newly acknowledged |
| 1908 | * packets or when the the loss detection timer has expired. |
| 1909 | * Always succeeds. |
| 1910 | */ |
| 1911 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1912 | struct quic_conn *qc, |
| 1913 | struct list *lost_pkts) |
| 1914 | { |
| 1915 | struct eb_root *pkts; |
| 1916 | struct eb64_node *node; |
| 1917 | struct quic_loss *ql; |
| 1918 | unsigned int loss_delay; |
| 1919 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1920 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1921 | pkts = &pktns->tx.pkts; |
| 1922 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1923 | if (eb_is_empty(pkts)) |
| 1924 | goto out; |
| 1925 | |
| 1926 | ql = &qc->path->loss; |
| 1927 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1928 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1929 | |
| 1930 | node = eb64_first(pkts); |
| 1931 | while (node) { |
| 1932 | struct quic_tx_packet *pkt; |
| 1933 | int64_t largest_acked_pn; |
| 1934 | unsigned int loss_time_limit, time_sent; |
| 1935 | |
| 1936 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1937 | largest_acked_pn = pktns->rx.largest_acked_pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1938 | node = eb64_next(node); |
| 1939 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1940 | break; |
| 1941 | |
| 1942 | time_sent = pkt->time_sent; |
| 1943 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1944 | if (tick_is_le(time_sent, now_ms) || |
| 1945 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1946 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1947 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1948 | } |
| 1949 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1950 | if (tick_isset(pktns->tx.loss_time)) |
| 1951 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1952 | else |
| 1953 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1958 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1962 | * 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] | 1963 | * 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] | 1964 | * acked ack-eliciting packet. |
| 1965 | * Return 1, if succeeded, 0 if not. |
| 1966 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1967 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1968 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1969 | struct quic_enc_level *qel, |
| 1970 | unsigned int *rtt_sample, |
| 1971 | const unsigned char **pos, const unsigned char *end) |
| 1972 | { |
| 1973 | struct quic_ack *ack = &frm->ack; |
| 1974 | uint64_t smallest, largest; |
| 1975 | struct eb_root *pkts; |
| 1976 | struct eb64_node *largest_node; |
| 1977 | unsigned int time_sent, pkt_flags; |
| 1978 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1979 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1980 | |
| 1981 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1982 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1983 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1984 | goto err; |
| 1985 | } |
| 1986 | |
| 1987 | if (ack->first_ack_range > ack->largest_ack) { |
| 1988 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1989 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1990 | goto err; |
| 1991 | } |
| 1992 | |
| 1993 | largest = ack->largest_ack; |
| 1994 | smallest = largest - ack->first_ack_range; |
| 1995 | pkts = &qel->pktns->tx.pkts; |
| 1996 | pkt_flags = 0; |
| 1997 | largest_node = NULL; |
| 1998 | time_sent = 0; |
| 1999 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2000 | 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] | 2001 | largest_node = eb64_lookup(pkts, largest); |
| 2002 | if (!largest_node) { |
| 2003 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2004 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 2005 | } |
| 2006 | else { |
| 2007 | time_sent = eb64_entry(&largest_node->node, |
| 2008 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2009 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2013 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2014 | do { |
| 2015 | uint64_t gap, ack_range; |
| 2016 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2017 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2018 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2019 | if (!ack->ack_range_num--) |
| 2020 | break; |
| 2021 | |
| 2022 | if (!quic_dec_int(&gap, pos, end)) |
| 2023 | goto err; |
| 2024 | |
| 2025 | if (smallest < gap + 2) { |
| 2026 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2027 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2028 | goto err; |
| 2029 | } |
| 2030 | |
| 2031 | largest = smallest - gap - 2; |
| 2032 | if (!quic_dec_int(&ack_range, pos, end)) |
| 2033 | goto err; |
| 2034 | |
| 2035 | if (largest < ack_range) { |
| 2036 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2037 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2038 | goto err; |
| 2039 | } |
| 2040 | |
| 2041 | /* Do not use this node anymore. */ |
| 2042 | largest_node = NULL; |
| 2043 | /* Next range */ |
| 2044 | smallest = largest - ack_range; |
| 2045 | |
| 2046 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2047 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2048 | } while (1); |
| 2049 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2050 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2051 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2052 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2056 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2057 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2058 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2059 | 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] | 2060 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2061 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2062 | if (quic_peer_validated_addr(qc)) |
| 2063 | qc->path->loss.pto_count = 0; |
| 2064 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | |
| 2068 | return 1; |
| 2069 | |
| 2070 | err: |
| 2071 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2072 | 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] | 2073 | return 0; |
| 2074 | } |
| 2075 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2076 | /* This function gives the detail of the SSL error. It is used only |
| 2077 | * if the debug mode and the verbose mode are activated. It dump all |
| 2078 | * the SSL error until the stack was empty. |
| 2079 | */ |
| 2080 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2081 | { |
| 2082 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2083 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2084 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2085 | unsigned long ret; |
| 2086 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2087 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2088 | ret = ERR_get_error(); |
| 2089 | if (!ret) |
| 2090 | return; |
| 2091 | |
| 2092 | 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] | 2093 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2098 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2099 | const char **str, int *len); |
| 2100 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2102 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2103 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2104 | * Return 1 if succeeded, 0 if not. |
| 2105 | */ |
| 2106 | 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] | 2107 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2108 | const unsigned char *data, size_t len, |
| 2109 | struct quic_rx_packet *pkt, |
| 2110 | struct quic_rx_crypto_frm *cf) |
| 2111 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2112 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2113 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2114 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2115 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2116 | qc = ctx->qc; |
| 2117 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2118 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2119 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2120 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2121 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2122 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2123 | goto err; |
| 2124 | } |
| 2125 | |
| 2126 | el->rx.crypto.offset += len; |
| 2127 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2128 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2129 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2130 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2131 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2132 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2133 | if (ssl_err != 1) { |
| 2134 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2135 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2136 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2137 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2138 | goto out; |
| 2139 | } |
| 2140 | |
| 2141 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2142 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2143 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2144 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2145 | goto err; |
| 2146 | } |
| 2147 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2148 | 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] | 2149 | |
| 2150 | /* Check the alpn could be negotiated */ |
| 2151 | if (!qc->app_ops) { |
| 2152 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2153 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2154 | goto err; |
| 2155 | } |
| 2156 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2157 | /* I/O callback switch */ |
| 2158 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2159 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2160 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2161 | /* The connection is ready to be accepted. */ |
| 2162 | quic_accept_push_qc(qc); |
| 2163 | } |
| 2164 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2165 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2166 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2167 | } else { |
| 2168 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2169 | if (ssl_err != 1) { |
| 2170 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2171 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2172 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2173 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2174 | goto out; |
| 2175 | } |
| 2176 | |
| 2177 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2178 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2179 | goto err; |
| 2180 | } |
| 2181 | |
| 2182 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2183 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2184 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2185 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2186 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2187 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2188 | return 1; |
| 2189 | |
| 2190 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2191 | 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] | 2192 | return 0; |
| 2193 | } |
| 2194 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2195 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 2196 | * <pkt> RX packet. |
| 2197 | * Return it if succeeded, NULL if not. |
| 2198 | */ |
| 2199 | static inline |
| 2200 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 2201 | struct quic_rx_packet *pkt) |
| 2202 | { |
| 2203 | struct quic_rx_strm_frm *frm; |
| 2204 | |
| 2205 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 2206 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2207 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2208 | frm->len = stream_frm->len; |
| 2209 | frm->data = stream_frm->data; |
| 2210 | frm->pkt = pkt; |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2211 | frm->fin = stream_frm->fin; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | return frm; |
| 2215 | } |
| 2216 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2217 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
Frédéric Lécaille | 3fe7df8 | 2021-12-15 15:32:55 +0100 | [diff] [blame] | 2218 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2219 | */ |
| 2220 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 2221 | { |
| 2222 | size_t ret; |
| 2223 | |
Amaury Denoyelle | 2d2d030 | 2022-02-28 10:00:54 +0100 | [diff] [blame] | 2224 | ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len); |
| 2225 | strm_frm->len -= ret; |
| 2226 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2227 | |
| 2228 | return ret; |
| 2229 | } |
| 2230 | |
| 2231 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2232 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2233 | * If not, the STREAM frame is stored to be treated again later. |
| 2234 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2235 | * Return 1 if succeeded, 0 if not. |
| 2236 | */ |
| 2237 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2238 | struct quic_stream *strm_frm, |
| 2239 | struct quic_conn *qc) |
| 2240 | { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2241 | struct quic_rx_strm_frm *frm; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2242 | struct eb64_node *frm_node; |
| 2243 | struct qcs *qcs = NULL; |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 2244 | size_t done, buf_was_full; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2245 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2246 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2247 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2248 | strm_frm->offset.key, strm_frm->fin, |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2249 | (char *)strm_frm->data, &qcs, &done); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2250 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2251 | /* invalid or already received frame */ |
| 2252 | if (ret == 1) |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2253 | return 1; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2254 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2255 | if (ret == 2) { |
| 2256 | /* frame cannot be parsed at the moment and should be |
| 2257 | * buffered. |
| 2258 | */ |
| 2259 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2260 | if (!frm) { |
| 2261 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2262 | QUIC_EV_CONN_PSTRM, qc); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2263 | return 0; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2266 | if (done) { |
| 2267 | BUG_ON(done >= frm->len); /* must never happen */ |
| 2268 | frm->len -= done; |
| 2269 | frm->data += done; |
| 2270 | frm->offset_node.key += done; |
| 2271 | } |
| 2272 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2273 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
| 2274 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2275 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2276 | /* interrupt only if frame was not received at all. */ |
| 2277 | if (!done) |
| 2278 | return 1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 2281 | /* Decode the data if buffer is already full as it's not possible to |
| 2282 | * dequeue a frame in this condition. |
| 2283 | */ |
| 2284 | if (b_full(&qcs->rx.buf)) |
| 2285 | qcc_decode_qcs(qc->qcc, qcs); |
| 2286 | |
| 2287 | retry: |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2288 | /* Frame received (partially or not) by the mux. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2289 | * If there is buffered frame for next offset, it may be possible to |
| 2290 | * receive them now. |
| 2291 | */ |
| 2292 | frm_node = eb64_first(&qcs->rx.frms); |
| 2293 | while (frm_node) { |
| 2294 | frm = eb64_entry(&frm_node->node, |
| 2295 | struct quic_rx_strm_frm, offset_node); |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2296 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 2297 | ret = qcc_recv(qc->qcc, qcs->id, frm->len, |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2298 | frm->offset_node.key, frm->fin, |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2299 | (char *)frm->data, &qcs, &done); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2300 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2301 | /* interrupt the parsing if the frame cannot be handled |
| 2302 | * entirely for the moment only. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2303 | */ |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2304 | if (ret == 2) { |
| 2305 | if (done) { |
| 2306 | BUG_ON(done >= frm->len); /* must never happen */ |
| 2307 | frm->len -= done; |
| 2308 | frm->data += done; |
| 2309 | |
| 2310 | eb64_delete(&frm->offset_node); |
| 2311 | frm->offset_node.key += done; |
| 2312 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
| 2313 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2314 | break; |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 2315 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2316 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2317 | /* Remove a newly received frame or an invalid one. */ |
| 2318 | frm_node = eb64_next(frm_node); |
| 2319 | eb64_delete(&frm->offset_node); |
| 2320 | quic_rx_packet_refdec(frm->pkt); |
| 2321 | pool_free(pool_head_quic_rx_strm_frm, frm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 2324 | buf_was_full = b_full(&qcs->rx.buf); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2325 | /* Decode the received data. */ |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2326 | qcc_decode_qcs(qc->qcc, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2327 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 2328 | /* Buffer was full so the reception was stopped. Now the buffer has |
| 2329 | * space available thanks to qcc_decode_qcs(). We can now retry to |
| 2330 | * handle more data. |
| 2331 | */ |
| 2332 | if (buf_was_full && !b_full(&qcs->rx.buf)) |
| 2333 | goto retry; |
| 2334 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2335 | return 1; |
| 2336 | } |
| 2337 | |
| 2338 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2339 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2340 | * If not, the STREAM frame is stored to be treated again later. |
| 2341 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2342 | * Return 1 if succeeded, 0 if not. |
| 2343 | */ |
| 2344 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2345 | struct quic_stream *strm_frm, |
| 2346 | struct quic_conn *qc) |
| 2347 | { |
| 2348 | struct qcs *strm; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2349 | struct quic_rx_strm_frm *frm; |
| 2350 | size_t strm_frm_len; |
| 2351 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2352 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2353 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2354 | 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] | 2355 | return 0; |
| 2356 | } |
| 2357 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2358 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2359 | size_t diff; |
| 2360 | |
| 2361 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2362 | TRACE_PROTO("Already received STREAM data", |
| 2363 | QUIC_EV_CONN_PSTRM, qc); |
| 2364 | goto out; |
| 2365 | } |
| 2366 | |
| 2367 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2368 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2369 | strm_frm->offset.key = strm->rx.offset; |
| 2370 | strm_frm->len -= diff; |
| 2371 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2375 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2376 | int ret; |
| 2377 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2378 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2379 | goto store_frm; |
| 2380 | |
| 2381 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2382 | * of bytes copied. |
| 2383 | */ |
| 2384 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2385 | /* Inform the application of the arrival of this new stream */ |
| 2386 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2387 | 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] | 2388 | return 0; |
| 2389 | } |
| 2390 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2391 | if (ret) |
| 2392 | qcs_notify_recv(strm); |
| 2393 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2394 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2395 | } |
| 2396 | /* Take this frame into an account for the stream flow control */ |
| 2397 | strm->rx.offset += strm_frm_len; |
| 2398 | /* It all the data were provided to the application, there is no need to |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2399 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2400 | */ |
| 2401 | if (!strm_frm->len) |
| 2402 | goto out; |
| 2403 | |
| 2404 | store_frm: |
| 2405 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2406 | if (!frm) { |
| 2407 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2408 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2409 | return 0; |
| 2410 | } |
| 2411 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2412 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2413 | quic_rx_packet_refinc(pkt); |
| 2414 | |
| 2415 | out: |
| 2416 | return 1; |
| 2417 | } |
| 2418 | |
| 2419 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2420 | struct quic_stream *strm_frm, |
| 2421 | struct quic_conn *qc) |
| 2422 | { |
| 2423 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2424 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2425 | else |
| 2426 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2427 | } |
| 2428 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2429 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2430 | * for <qc> QUIC connection. |
| 2431 | * This is a best effort function which never fails even if no memory could be |
| 2432 | * allocated to duplicate these frames. |
| 2433 | */ |
| 2434 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2435 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2436 | { |
| 2437 | struct quic_frame *frm, *frmbak; |
| 2438 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2439 | |
| 2440 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2441 | struct quic_frame *dup_frm, *origin; |
| 2442 | |
| 2443 | switch (frm->type) { |
| 2444 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2445 | { |
| 2446 | struct quic_stream *strm_frm = &frm->stream; |
| 2447 | struct eb64_node *node = NULL; |
| 2448 | struct qc_stream_desc *stream_desc; |
| 2449 | |
| 2450 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2451 | if (!node) { |
| 2452 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 2453 | continue; |
| 2454 | } |
| 2455 | |
| 2456 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2457 | /* Do not resend this frame if in the "already acked range" */ |
| 2458 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2459 | TRACE_PROTO("ignored frame frame in already acked range", |
| 2460 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2461 | continue; |
| 2462 | } |
| 2463 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2464 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2465 | TRACE_PROTO("updated partially acked frame", |
| 2466 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2467 | } |
| 2468 | |
| 2469 | break; |
| 2470 | } |
| 2471 | |
| 2472 | default: |
| 2473 | break; |
| 2474 | } |
| 2475 | |
| 2476 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2477 | if (!dup_frm) { |
| 2478 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2479 | break; |
| 2480 | } |
| 2481 | |
| 2482 | /* If <frm> is already a copy of another frame, we must take |
| 2483 | * its original frame as source for the copy. |
| 2484 | */ |
| 2485 | origin = frm->origin ? frm->origin : frm; |
| 2486 | TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2487 | *dup_frm = *origin; |
| 2488 | LIST_INIT(&dup_frm->reflist); |
| 2489 | TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM, |
| 2490 | qc, NULL, &origin->pkt->pn_node.key); |
| 2491 | dup_frm->origin = origin; |
| 2492 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2493 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2494 | } |
| 2495 | |
| 2496 | LIST_SPLICE(out_frm_list, &tmp); |
| 2497 | } |
| 2498 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2499 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2500 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2501 | struct quic_enc_level *qel, |
| 2502 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2503 | { |
| 2504 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2505 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2506 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2507 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2508 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2509 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2510 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2511 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2512 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2513 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2514 | node = eb64_next(node); |
| 2515 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2516 | 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] | 2517 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2521 | return; |
| 2522 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2523 | /* When building a packet from another one, the field which may increase the |
| 2524 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2525 | */ |
| 2526 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2527 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2528 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2529 | return; |
| 2530 | } |
| 2531 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2532 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2533 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2534 | if (frms == frms1 && frms2) { |
| 2535 | frms = frms2; |
| 2536 | goto start; |
| 2537 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2538 | } |
| 2539 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2540 | /* 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] | 2541 | * 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] | 2542 | * Initial packets send them coalescing with others (Handshake here). |
| 2543 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2544 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2545 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2546 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2547 | { |
| 2548 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2549 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2550 | |
| 2551 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2552 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2553 | struct quic_enc_level *qel = iqel; |
| 2554 | struct eb_root *pkts; |
| 2555 | struct eb64_node *node; |
| 2556 | struct quic_tx_packet *pkt; |
| 2557 | struct list *tmp = &itmp; |
| 2558 | |
| 2559 | start: |
| 2560 | pkt = NULL; |
| 2561 | pkts = &qel->pktns->tx.pkts; |
| 2562 | node = eb64_first(pkts); |
| 2563 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2564 | while (node) { |
| 2565 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2566 | 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] | 2567 | break; |
| 2568 | node = eb64_next(node); |
| 2569 | } |
| 2570 | |
| 2571 | if (!pkt) |
| 2572 | goto end; |
| 2573 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2574 | /* When building a packet from another one, the field which may increase the |
| 2575 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2576 | */ |
| 2577 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2578 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2579 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2580 | goto end; |
| 2581 | } |
| 2582 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2583 | qel->pktns->tx.pto_probe += 1; |
| 2584 | requeue: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2585 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2586 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2587 | if (qel == iqel) { |
| 2588 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2589 | pkt = pkt->next; |
| 2590 | tmp = &htmp; |
| 2591 | hqel->pktns->tx.pto_probe += 1; |
| 2592 | goto requeue; |
| 2593 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2594 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2595 | |
| 2596 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2597 | LIST_SPLICE(ifrms, &itmp); |
| 2598 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2601 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2602 | * as I/O handler context and <qel> as encryption level. |
| 2603 | * Returns 1 if succeeded, 0 if failed. |
| 2604 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2605 | 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] | 2606 | struct quic_enc_level *qel) |
| 2607 | { |
| 2608 | struct quic_frame frm; |
| 2609 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2610 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2611 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2612 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2613 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2614 | /* Skip the AAD */ |
| 2615 | pos = pkt->data + pkt->aad_len; |
| 2616 | end = pkt->data + pkt->len; |
| 2617 | |
| 2618 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2619 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2620 | goto err; |
| 2621 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2622 | 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] | 2623 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2624 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2625 | break; |
| 2626 | case QUIC_FT_PING: |
| 2627 | break; |
| 2628 | case QUIC_FT_ACK: |
| 2629 | { |
| 2630 | unsigned int rtt_sample; |
| 2631 | |
| 2632 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2633 | 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] | 2634 | goto err; |
| 2635 | |
| 2636 | if (rtt_sample) { |
| 2637 | unsigned int ack_delay; |
| 2638 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2639 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2640 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2641 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2642 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2643 | 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] | 2644 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2645 | break; |
| 2646 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2647 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2648 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2649 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2650 | { |
| 2651 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2652 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2653 | 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] | 2654 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2655 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2656 | |
| 2657 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2658 | cfdebug.len = frm.crypto.len; |
| 2659 | TRACE_PROTO("CRYPTO data discarded", |
| 2660 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2661 | break; |
| 2662 | } |
| 2663 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2664 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2665 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2666 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2667 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2668 | |
| 2669 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2670 | cfdebug.len = frm.crypto.len; |
| 2671 | /* Nothing to do */ |
| 2672 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2673 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2674 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2675 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2676 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2677 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2678 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2679 | else { |
| 2680 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2681 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2682 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2683 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2684 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2685 | cfdebug.len = frm.crypto.len; |
| 2686 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2687 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2688 | frm.crypto.len -= diff; |
| 2689 | frm.crypto.data += diff; |
| 2690 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2691 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2692 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2693 | |
| 2694 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2695 | /* 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] | 2696 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2697 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2698 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2699 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2700 | if (!qc_provide_cdata(qel, ctx, |
| 2701 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2702 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2703 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2704 | |
| 2705 | break; |
| 2706 | } |
| 2707 | |
| 2708 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2709 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2710 | if (!cf) { |
| 2711 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2712 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2713 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2714 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2715 | |
| 2716 | cf->offset_node.key = frm.crypto.offset; |
| 2717 | cf->len = frm.crypto.len; |
| 2718 | cf->data = frm.crypto.data; |
| 2719 | cf->pkt = pkt; |
| 2720 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2721 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2722 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2723 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2724 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2725 | { |
| 2726 | struct quic_stream *stream = &frm.stream; |
| 2727 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2728 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2729 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2730 | goto err; |
| 2731 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2732 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2733 | |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2734 | /* At the application layer the connection may have already been closed. */ |
| 2735 | if (qc->mux_state != QC_MUX_READY) |
| 2736 | break; |
| 2737 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2738 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2739 | goto err; |
| 2740 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2741 | break; |
| 2742 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2743 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2744 | if (qc->mux_state == QC_MUX_READY) { |
| 2745 | struct quic_max_data *data = &frm.max_data; |
| 2746 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2747 | } |
| 2748 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2749 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2750 | if (qc->mux_state == QC_MUX_READY) { |
| 2751 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2752 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2753 | data->max_stream_data); |
| 2754 | } |
| 2755 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2756 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2757 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2758 | case QUIC_FT_DATA_BLOCKED: |
| 2759 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2760 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2761 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2762 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2763 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2764 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2765 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2766 | break; |
| 2767 | case QUIC_FT_CONNECTION_CLOSE: |
| 2768 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2769 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2770 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2771 | /* RFC 9000 10.2. Immediate Close: |
| 2772 | * The closing and draining connection states exist to ensure |
| 2773 | * that connections close cleanly and that delayed or reordered |
| 2774 | * packets are properly discarded. These states SHOULD persist |
| 2775 | * for at least three times the current PTO interval... |
| 2776 | * |
| 2777 | * Rearm the idle timeout only one time when entering draining |
| 2778 | * state. |
| 2779 | */ |
| 2780 | qc_idle_timer_do_rearm(qc); |
| 2781 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2782 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2783 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2784 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2785 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2786 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2787 | goto err; |
| 2788 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2789 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2790 | break; |
| 2791 | default: |
| 2792 | goto err; |
| 2793 | } |
| 2794 | } |
| 2795 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2796 | /* 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] | 2797 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2798 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2799 | if (fast_retrans) { |
| 2800 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2801 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2802 | |
| 2803 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2804 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2805 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2806 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2807 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2808 | * be discarded. |
| 2809 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2810 | 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] | 2811 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2812 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2813 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2814 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2815 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2816 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2817 | qc_set_timer(ctx->qc); |
| 2818 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2819 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2820 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2821 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2822 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2823 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2826 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2827 | return 1; |
| 2828 | |
| 2829 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2830 | 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] | 2831 | return 0; |
| 2832 | } |
| 2833 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2834 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2835 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2836 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2837 | * index after having marked the end of written data. This the responsability |
| 2838 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2839 | * data made of a uint16_t null field. |
| 2840 | * |
| 2841 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2842 | * ^ ^ |
| 2843 | * r w |
| 2844 | * |
| 2845 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2846 | * ^ ^ |
| 2847 | * r w |
| 2848 | |
| 2849 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2850 | * ^ |
| 2851 | * (r = w) |
| 2852 | * |
| 2853 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2854 | * ^ ^ |
| 2855 | * w r |
| 2856 | */ |
| 2857 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2858 | { |
| 2859 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2860 | |
| 2861 | /* We can reset the writer index only if in front of the reader index and |
| 2862 | * if the reader index is not null. Resetting the writer when the reader |
| 2863 | * index is null would empty the buffer. |
| 2864 | * XXX Note than the writer index cannot reach the reader index. |
| 2865 | * Only the reader index can reach the writer index. |
| 2866 | */ |
| 2867 | if (rd && rd <= cbuf->wr) { |
| 2868 | /* Mark the end of contiguous data for the reader */ |
| 2869 | write_u16(cb_wr(cbuf), 0); |
| 2870 | cb_add(cbuf, sizeof(uint16_t)); |
| 2871 | cb_wr_reset(cbuf); |
| 2872 | return 1; |
| 2873 | } |
| 2874 | |
| 2875 | return 0; |
| 2876 | } |
| 2877 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2878 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2879 | * 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] | 2880 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2881 | * This function must be called only after having built a correct datagram. |
| 2882 | * Always succeeds. |
| 2883 | */ |
| 2884 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2885 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2886 | { |
| 2887 | write_u16(cb_wr(cbuf), dglen); |
| 2888 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2889 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2890 | } |
| 2891 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2892 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2893 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2894 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2895 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2896 | */ |
| 2897 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2898 | struct quic_enc_level *qel, int cc, int probe) |
| 2899 | { |
| 2900 | unsigned int must_ack = |
| 2901 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2902 | |
| 2903 | /* Do not build any more packet if the TX secrets are not available or |
| 2904 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2905 | * and if there is no more packets to send upon PTO expiration |
| 2906 | * and if there is no more ack-eliciting frames to send or in flight |
| 2907 | * congestion control limit is reached for prepared data |
| 2908 | */ |
| 2909 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2910 | (!cc && !probe && !must_ack && |
| 2911 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2912 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2913 | return 0; |
| 2914 | } |
| 2915 | |
| 2916 | return 1; |
| 2917 | } |
| 2918 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2919 | /* 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] | 2920 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2921 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2922 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2923 | * 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] | 2924 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2925 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2926 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2927 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2928 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2929 | { |
| 2930 | struct quic_enc_level *qel; |
| 2931 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2932 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2933 | struct quic_tx_packet *pkt; |
| 2934 | size_t total; |
| 2935 | size_t dg_headlen; |
| 2936 | |
| 2937 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2938 | /* Each datagram is prepended with its length followed by the |
| 2939 | * address of the first packet in the datagram. |
| 2940 | */ |
| 2941 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2942 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2943 | total = 0; |
| 2944 | start: |
| 2945 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2946 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2947 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2948 | * to ensure there is enough room to mark the end of prepared |
| 2949 | * contiguous data with a zero length. |
| 2950 | */ |
| 2951 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2952 | 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] | 2953 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2954 | |
| 2955 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2956 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2957 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2958 | /* We do not probe if an immediate close was asked */ |
| 2959 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2960 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2961 | |
| 2962 | 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] | 2963 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2964 | |
| 2965 | /* Leave room for the datagram header */ |
| 2966 | pos += dg_headlen; |
| 2967 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2968 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2969 | } |
| 2970 | else { |
| 2971 | end = pos + qc->path->mtu; |
| 2972 | } |
| 2973 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2974 | 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] | 2975 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2976 | switch (err) { |
| 2977 | case -2: |
| 2978 | goto err; |
| 2979 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2980 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2981 | * MTU, we are here because of the congestion control window. There is |
| 2982 | * no need to try to reuse this buffer. |
| 2983 | */ |
| 2984 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2985 | default: |
| 2986 | break; |
| 2987 | } |
| 2988 | |
| 2989 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2990 | if (!pkt) |
| 2991 | goto err; |
| 2992 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2993 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2994 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2995 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2996 | total += pkt->len; |
| 2997 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2998 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2999 | } |
| 3000 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 3001 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3002 | 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] | 3003 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3004 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 3005 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | out: |
| 3009 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3010 | return total; |
| 3011 | |
| 3012 | err: |
| 3013 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 3014 | return -1; |
| 3015 | } |
| 3016 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 3017 | /* 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] | 3018 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 3019 | * several packets in the same datagram. A header made of two fields is added |
| 3020 | * to each datagram: the datagram length followed by the address of the first |
| 3021 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 3022 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 3023 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 3025 | 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] | 3026 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3027 | 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] | 3028 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3029 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3030 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3031 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3032 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3033 | /* length of datagrams */ |
| 3034 | uint16_t dglen; |
| 3035 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3036 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3037 | /* Each datagram is prepended with its length followed by the |
| 3038 | * address of the first packet in the datagram. |
| 3039 | */ |
| 3040 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3041 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3042 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3043 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3044 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3045 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3046 | qel = &qc->els[tel]; |
| 3047 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3048 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3049 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3050 | padding = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3051 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3052 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3053 | /* Leave at least <dglen> bytes at the end of this buffer |
| 3054 | * to ensure there is enough room to mark the end of prepared |
| 3055 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3056 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3057 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 3058 | first_pkt = prv_pkt = NULL; |
| 3059 | 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] | 3060 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3061 | enum quic_pkt_type pkt_type; |
| 3062 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3063 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3064 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3065 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3066 | /* We do not probe if an immediate close was asked */ |
| 3067 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 3068 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3069 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3070 | 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] | 3071 | if (prv_pkt) |
| 3072 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3073 | /* Let's select the next encryption level */ |
| 3074 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3075 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3076 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3077 | qel = &qc->els[tel]; |
| 3078 | /* Build a new datagram */ |
| 3079 | prv_pkt = NULL; |
| 3080 | continue; |
| 3081 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3082 | break; |
| 3083 | } |
| 3084 | |
| 3085 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3086 | if (!prv_pkt) { |
| 3087 | /* Leave room for the datagram header */ |
| 3088 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3089 | 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] | 3090 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3091 | } |
| 3092 | else { |
| 3093 | end = pos + qc->path->mtu; |
| 3094 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3097 | cur_pkt = qc_build_pkt(&pos, end, qel, frms, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3098 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3099 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3100 | case -2: |
| 3101 | goto err; |
| 3102 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3103 | /* If there was already a correct packet present, set the |
| 3104 | * current datagram as prepared into <cbuf>. |
| 3105 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 3106 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3107 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 3108 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3109 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3110 | break; |
| 3111 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 3112 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3113 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3114 | if (!cur_pkt) |
| 3115 | goto err; |
| 3116 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 3117 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3118 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3119 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3120 | total += cur_pkt->len; |
| 3121 | /* keep trace of the first packet in the datagram */ |
| 3122 | if (!first_pkt) |
| 3123 | first_pkt = cur_pkt; |
| 3124 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3125 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3126 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3127 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3128 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3129 | /* Let's say we have to build a new dgram */ |
| 3130 | prv_pkt = NULL; |
| 3131 | dglen += cur_pkt->len; |
| 3132 | /* Client: discard the Initial encryption keys as soon as |
| 3133 | * a handshake packet could be built. |
| 3134 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3135 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3136 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3137 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3138 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3139 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3140 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 3141 | 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] | 3142 | 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] | 3143 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3144 | } |
| 3145 | /* If the data for the current encryption level have all been sent, |
| 3146 | * select the next level. |
| 3147 | */ |
| 3148 | 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] | 3149 | 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] | 3150 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3151 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3152 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3153 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3154 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3155 | frms = &qc->els[tel].pktns->tx.frms; |
| 3156 | else |
| 3157 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3158 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3159 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3160 | /* If there is data for the next level, do not |
| 3161 | * consume a datagram. |
| 3162 | */ |
| 3163 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3164 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3165 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3166 | /* If we have to build a new datagram, set the current datagram as |
| 3167 | * prepared into <cbuf>. |
| 3168 | */ |
| 3169 | if (!prv_pkt) { |
| 3170 | qc_set_dg(cbuf, dglen, first_pkt); |
| 3171 | first_pkt = NULL; |
| 3172 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3173 | padding = 0; |
| 3174 | } |
| 3175 | 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] | 3176 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3177 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3178 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | stop_build: |
| 3183 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3184 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3185 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3186 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3187 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3191 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3192 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3193 | |
| 3194 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3195 | 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] | 3196 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | /* 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] | 3200 | * 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] | 3201 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3202 | 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] | 3203 | { |
| 3204 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3205 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3206 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3207 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3208 | cbuf = qr->cbuf; |
| 3209 | while (cb_contig_data(cbuf)) { |
| 3210 | unsigned char *pos; |
| 3211 | struct buffer tmpbuf = { }; |
| 3212 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3213 | uint16_t dglen; |
| 3214 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3215 | unsigned int time_sent; |
| 3216 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3217 | pos = cb_rd(cbuf); |
| 3218 | dglen = read_u16(pos); |
| 3219 | /* End of prepared datagrams. |
| 3220 | * Reset the reader index only if in front of the writer index. |
| 3221 | */ |
| 3222 | if (!dglen) { |
| 3223 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3224 | |
| 3225 | if (wr && wr < cbuf->rd) { |
| 3226 | cb_rd_reset(cbuf); |
| 3227 | continue; |
| 3228 | } |
| 3229 | break; |
| 3230 | } |
| 3231 | |
| 3232 | pos += sizeof dglen; |
| 3233 | first_pkt = read_ptr(pos); |
| 3234 | pos += sizeof first_pkt; |
| 3235 | tmpbuf.area = (char *)pos; |
| 3236 | tmpbuf.size = tmpbuf.data = dglen; |
| 3237 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3238 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3239 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3240 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3241 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3242 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3243 | qc->tx.bytes += tmpbuf.data; |
| 3244 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3245 | |
| 3246 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3247 | pkt->time_sent = time_sent; |
| 3248 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3249 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3250 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3251 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3252 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3253 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3254 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3255 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3256 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3257 | qc_notify_close(qc); |
| 3258 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3259 | /* RFC 9000 10.2. Immediate Close: |
| 3260 | * The closing and draining connection states exist to ensure |
| 3261 | * that connections close cleanly and that delayed or reordered |
| 3262 | * packets are properly discarded. These states SHOULD persist |
| 3263 | * for at least three times the current PTO interval... |
| 3264 | * |
| 3265 | * Rearm the idle timeout only one time when entering closing |
| 3266 | * state. |
| 3267 | */ |
| 3268 | qc_idle_timer_do_rearm(qc); |
| 3269 | if (qc->timer_task) { |
| 3270 | task_destroy(qc->timer_task); |
| 3271 | qc->timer_task = NULL; |
| 3272 | } |
| 3273 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3274 | qc->path->in_flight += pkt->in_flight_len; |
| 3275 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3276 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3277 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3278 | 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] | 3279 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3280 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3281 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3282 | } |
| 3283 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3284 | |
| 3285 | return 1; |
| 3286 | } |
| 3287 | |
| 3288 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3289 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3290 | * a HANDSHAKE_DONE frame. |
| 3291 | * Return 1 if succeeded, 0 if not. |
| 3292 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3293 | 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] | 3294 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3295 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3296 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3297 | struct quic_frame *frm, *frmbak; |
| 3298 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3299 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3300 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3301 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3302 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3303 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3304 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3305 | if (!frm) |
| 3306 | return 0; |
| 3307 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3308 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3309 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3310 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3311 | } |
| 3312 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3313 | first = 1; |
| 3314 | max = qc->tx.params.active_connection_id_limit; |
| 3315 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | struct quic_connection_id *cid; |
| 3317 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3318 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3319 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3320 | goto err; |
| 3321 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3322 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3323 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3324 | if (!cid) |
| 3325 | goto err; |
| 3326 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3327 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3328 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3329 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3330 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3331 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3332 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3333 | |
| 3334 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3335 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3336 | |
| 3337 | return 1; |
| 3338 | |
| 3339 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3340 | /* free the frames */ |
| 3341 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3342 | pool_free(pool_head_quic_frame, frm); |
| 3343 | |
| 3344 | node = eb64_first(&qc->cids); |
| 3345 | while (node) { |
| 3346 | struct quic_connection_id *cid; |
| 3347 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3348 | |
| 3349 | cid = eb64_entry(&node->node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3350 | if (cid->seq_num.key >= max) |
| 3351 | break; |
| 3352 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3353 | if (cid->seq_num.key < first) |
| 3354 | continue; |
| 3355 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3356 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3357 | ebmb_delete(&cid->node); |
| 3358 | eb64_delete(&cid->seq_num); |
| 3359 | pool_free(pool_head_quic_connection_id, cid); |
| 3360 | } |
| 3361 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3362 | return 0; |
| 3363 | } |
| 3364 | |
| 3365 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3366 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3367 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3368 | struct eb64_node *n; |
| 3369 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3370 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3371 | n = eb64_first(&arngs->root); |
| 3372 | while (n) { |
| 3373 | struct eb64_node *next; |
| 3374 | |
| 3375 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 3376 | next = eb64_next(n); |
| 3377 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3378 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3379 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3383 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3384 | * descending order. |
| 3385 | */ |
| 3386 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3387 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3388 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3389 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | |
| 3393 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3394 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3395 | * 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] | 3396 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3397 | 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] | 3398 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3399 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3400 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3401 | last = eb64_last(&arngs->root); |
| 3402 | while (last && arngs->enc_sz > limit) { |
| 3403 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3404 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3405 | prev = eb64_prev(last); |
| 3406 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3407 | return 0; |
| 3408 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3409 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 3410 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 3411 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3412 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3413 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3414 | --arngs->sz; |
| 3415 | eb64_delete(last); |
| 3416 | pool_free(pool_head_quic_arng, last); |
| 3417 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | return 1; |
| 3421 | } |
| 3422 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3423 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3424 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3425 | { |
| 3426 | struct eb64_node *node, *next; |
| 3427 | struct quic_arng_node *ar, *ar_next; |
| 3428 | |
| 3429 | node = eb64_last(&arngs->root); |
| 3430 | if (!node) |
| 3431 | return; |
| 3432 | |
| 3433 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3434 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3435 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3436 | |
| 3437 | while ((next = eb64_prev(node))) { |
| 3438 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 3439 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3440 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3441 | node = next; |
| 3442 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3443 | } |
| 3444 | } |
| 3445 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3446 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3447 | * 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] | 3448 | */ |
| 3449 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3450 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3451 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3452 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3453 | struct quic_arng_node *new_ar; |
| 3454 | |
| 3455 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3456 | if (new_ar) { |
| 3457 | new_ar->first.key = ar->first; |
| 3458 | new_ar->last = ar->last; |
| 3459 | eb64_insert(&arngs->root, &new_ar->first); |
| 3460 | arngs->sz++; |
| 3461 | } |
| 3462 | |
| 3463 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | /* 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] | 3467 | * 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] | 3468 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3469 | * |
| 3470 | * Descending order |
| 3471 | * -------------> |
| 3472 | * range1 range2 |
| 3473 | * ..........|--------|..............|--------| |
| 3474 | * ^ ^ ^ ^ |
| 3475 | * | | | | |
| 3476 | * last1 first1 last2 first2 |
| 3477 | * ..........+--------+--------------+--------+...... |
| 3478 | * diff1 gap12 diff2 |
| 3479 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3480 | * To encode the previous list of ranges we must encode integers as follows in |
| 3481 | * descending order: |
| 3482 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3483 | * with diff1 = last1 - first1 |
| 3484 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3485 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3486 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3487 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3488 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3489 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3490 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3491 | struct eb64_node *le; |
| 3492 | struct quic_arng_node *new_node; |
| 3493 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3494 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3495 | new = NULL; |
| 3496 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3497 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3498 | if (!new_node) |
| 3499 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3500 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3501 | goto out; |
| 3502 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3503 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3504 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3505 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3506 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3507 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3508 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3509 | |
| 3510 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3511 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3512 | else { |
| 3513 | struct quic_arng_node *le_ar = |
| 3514 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3515 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3516 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3517 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3518 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3519 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3520 | if (le_ar->last + 1 >= ar->first) { |
| 3521 | le_ar->last = ar->last; |
| 3522 | new = le; |
| 3523 | new_node = le_ar; |
| 3524 | } |
| 3525 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3526 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3527 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3528 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3529 | |
| 3530 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3531 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3532 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3533 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3534 | /* Verify that the new inserted node does not overlap the nodes |
| 3535 | * which follow it. |
| 3536 | */ |
| 3537 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3538 | struct eb64_node *next; |
| 3539 | struct quic_arng_node *next_node; |
| 3540 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3541 | while ((next = eb64_next(new))) { |
| 3542 | next_node = |
| 3543 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3544 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3545 | break; |
| 3546 | |
| 3547 | if (next_node->last > new_node->last) |
| 3548 | new_node->last = next_node->last; |
| 3549 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3550 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3551 | /* Decrement the size of these ranges. */ |
| 3552 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3553 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3556 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3557 | quic_arngs_set_enc_sz(arngs); |
| 3558 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3559 | return 1; |
| 3560 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3561 | /* Remove the header protection of packets at <el> encryption level. |
| 3562 | * Always succeeds. |
| 3563 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3564 | 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] | 3565 | { |
| 3566 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3567 | struct quic_rx_packet *pqpkt; |
| 3568 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3569 | struct quic_enc_level *app_qel; |
| 3570 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3571 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3572 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3573 | /* 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] | 3574 | 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] | 3575 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3576 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3577 | goto out; |
| 3578 | } |
| 3579 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3580 | 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] | 3581 | 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] | 3582 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3583 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3584 | 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] | 3585 | /* XXX TO DO XXX */ |
| 3586 | } |
| 3587 | else { |
| 3588 | /* The AAD includes the packet number field */ |
| 3589 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3590 | /* Store the packet into the tree of packets to decrypt. */ |
| 3591 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3592 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3593 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3594 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3595 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3596 | 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] | 3597 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3598 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3599 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3603 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3607 | * Return 1 if succeeded, 0 if not. |
| 3608 | */ |
| 3609 | 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] | 3610 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3611 | { |
| 3612 | struct eb64_node *node; |
| 3613 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3614 | node = eb64_first(&el->rx.crypto.frms); |
| 3615 | while (node) { |
| 3616 | struct quic_rx_crypto_frm *cf; |
| 3617 | |
| 3618 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3619 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3620 | break; |
| 3621 | |
| 3622 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3623 | goto err; |
| 3624 | |
| 3625 | node = eb64_next(node); |
| 3626 | quic_rx_packet_refdec(cf->pkt); |
| 3627 | eb64_delete(&cf->offset_node); |
| 3628 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3629 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3630 | return 1; |
| 3631 | |
| 3632 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3633 | 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] | 3634 | return 0; |
| 3635 | } |
| 3636 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3637 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3638 | * 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] | 3639 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | * Return 1 if succeeded, 0 if not. |
| 3641 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3642 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3643 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3644 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3645 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3646 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3647 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3648 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3649 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3650 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3651 | qel = cur_el; |
| 3652 | next_tel: |
| 3653 | if (!qel) |
| 3654 | goto out; |
| 3655 | |
| 3656 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3657 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3658 | while (node) { |
| 3659 | struct quic_rx_packet *pkt; |
| 3660 | |
| 3661 | pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3662 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3663 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3664 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3665 | /* Drop the packet */ |
| 3666 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3667 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3668 | } |
| 3669 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3670 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3671 | /* Drop the packet */ |
| 3672 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3673 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3674 | } |
| 3675 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3676 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3677 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3678 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3679 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3680 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3681 | qc_idle_timer_rearm(qc, 1); |
| 3682 | } |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3683 | if (pkt->pn > largest_pn) |
| 3684 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3685 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3686 | 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] | 3687 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3688 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3689 | } |
| 3690 | } |
| 3691 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3692 | eb64_delete(&pkt->pn_node); |
| 3693 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3694 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3695 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3696 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3697 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3698 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) |
| 3699 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3700 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3701 | goto err; |
| 3702 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3703 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3704 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3705 | qel = next_el; |
| 3706 | goto next_tel; |
| 3707 | } |
| 3708 | |
| 3709 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3710 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3711 | return 1; |
| 3712 | |
| 3713 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3714 | 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] | 3715 | return 0; |
| 3716 | } |
| 3717 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3718 | /* Check if it's possible to remove header protection for packets related to |
| 3719 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3720 | * |
| 3721 | * Return true if the operation is possible else false. |
| 3722 | */ |
| 3723 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3724 | { |
| 3725 | enum quic_tls_enc_level tel; |
| 3726 | |
| 3727 | if (!qel) |
| 3728 | return 0; |
| 3729 | |
| 3730 | tel = ssl_to_quic_enc_level(qel->level); |
| 3731 | |
| 3732 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3733 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3734 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3735 | return 0; |
| 3736 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3737 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3738 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3739 | return 0; |
| 3740 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3741 | /* 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] | 3742 | 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] | 3743 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3744 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3745 | |
| 3746 | return 1; |
| 3747 | } |
| 3748 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3749 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3750 | 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] | 3751 | { |
| 3752 | int ret; |
| 3753 | struct qring *qr; |
| 3754 | |
| 3755 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3756 | if (!qr) |
| 3757 | /* Never happens */ |
| 3758 | return 1; |
| 3759 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3760 | if (old_data) |
| 3761 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3762 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3763 | if (ret == -1) |
| 3764 | goto err; |
| 3765 | else if (ret == 0) |
| 3766 | goto out; |
| 3767 | |
| 3768 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3769 | goto err; |
| 3770 | |
| 3771 | out: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3772 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3773 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3774 | return 1; |
| 3775 | |
| 3776 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3777 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3778 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3779 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3780 | return 0; |
| 3781 | } |
| 3782 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3783 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3784 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 3785 | * QUIC connection |
| 3786 | * Returns 1 if succeeded, 0 if not. |
| 3787 | */ |
| 3788 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3789 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3790 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3791 | { |
| 3792 | int ret; |
| 3793 | struct qring *qr; |
| 3794 | |
| 3795 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3796 | if (!qr) |
| 3797 | /* Never happens */ |
| 3798 | return 1; |
| 3799 | |
| 3800 | if (old_data) |
| 3801 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3802 | ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms); |
| 3803 | if (ret == -1) |
| 3804 | goto err; |
| 3805 | else if (ret == 0) |
| 3806 | goto out; |
| 3807 | |
| 3808 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3809 | goto err; |
| 3810 | |
| 3811 | out: |
| 3812 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3813 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3814 | return 1; |
| 3815 | |
| 3816 | err: |
| 3817 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3818 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3819 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3820 | return 0; |
| 3821 | } |
| 3822 | |
| 3823 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3824 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3825 | { |
| 3826 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3827 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3828 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3829 | |
| 3830 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3831 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 3832 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 3833 | |
| 3834 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 3835 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 3836 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3837 | if (!LIST_ISEMPTY(&ifrms)) { |
| 3838 | iqel->pktns->tx.pto_probe = 1; |
| 3839 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3840 | hqel->pktns->tx.pto_probe = 1; |
| 3841 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 3842 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 3843 | } |
| 3844 | } |
| 3845 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3846 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 3847 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3848 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3849 | hqel->pktns->tx.pto_probe = 1; |
| 3850 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 3851 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3852 | } |
| 3853 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3854 | } |
| 3855 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3856 | } |
| 3857 | else { |
| 3858 | int i; |
| 3859 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 3860 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 3861 | |
| 3862 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3863 | hqel->pktns->tx.pto_probe = 0; |
| 3864 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 3865 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 3866 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3867 | if (!LIST_ISEMPTY(&frms1)) { |
| 3868 | hqel->pktns->tx.pto_probe = 1; |
| 3869 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 3870 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3871 | } |
| 3872 | } |
| 3873 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3874 | } |
| 3875 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3876 | aqel->pktns->tx.pto_probe = 0; |
| 3877 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 3878 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3879 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 3880 | if (!LIST_ISEMPTY(&frms1)) { |
| 3881 | aqel->pktns->tx.pto_probe = 1; |
| 3882 | qc_send_app_pkts(qc, 1, &frms1); |
| 3883 | } |
| 3884 | if (!LIST_ISEMPTY(&frms2)) { |
| 3885 | aqel->pktns->tx.pto_probe = 1; |
| 3886 | qc_send_app_pkts(qc, 1, &frms2); |
| 3887 | } |
| 3888 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3889 | } |
| 3890 | } |
| 3891 | } |
| 3892 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3893 | /* QUIC connection packet handler task (post handshake) */ |
| 3894 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3895 | { |
| 3896 | struct ssl_sock_ctx *ctx; |
| 3897 | struct quic_conn *qc; |
| 3898 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3899 | |
| 3900 | |
| 3901 | ctx = context; |
| 3902 | qc = ctx->qc; |
| 3903 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3904 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3905 | 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] | 3906 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3907 | /* Retranmissions */ |
| 3908 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3909 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 3910 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3911 | qc_dgrams_retransmit(qc); |
| 3912 | } |
| 3913 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3914 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3915 | qc_rm_hp_pkts(qc, qel); |
| 3916 | |
| 3917 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3918 | goto err; |
| 3919 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3920 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3921 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3922 | goto out; |
| 3923 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3924 | 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] | 3925 | goto err; |
| 3926 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3927 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3928 | return t; |
| 3929 | |
| 3930 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3931 | 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] | 3932 | return t; |
| 3933 | } |
| 3934 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3935 | /* QUIC connection packet handler task. */ |
| 3936 | 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] | 3937 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3938 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3939 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3940 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3941 | enum quic_tls_enc_level tel, next_tel; |
| 3942 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3943 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3944 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3945 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3946 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3947 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3948 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3949 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3950 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3951 | 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] | 3952 | |
| 3953 | /* Retranmissions */ |
| 3954 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3955 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 3956 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3957 | qc_dgrams_retransmit(qc); |
| 3958 | } |
| 3959 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3960 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3961 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3962 | /* The I/O handler has been woken up by the dgram listener |
| 3963 | * after the anti-amplification was reached. |
| 3964 | */ |
| 3965 | qc_set_timer(qc); |
| 3966 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3967 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3968 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3969 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3970 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3971 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3972 | 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] | 3973 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3974 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3975 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3976 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3977 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3978 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3979 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3980 | } |
| 3981 | 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] | 3982 | goto err; |
| 3983 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3984 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3985 | 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] | 3986 | |
| 3987 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3988 | /* Treat packets waiting for header packet protection decryption */ |
| 3989 | 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] | 3990 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3991 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3992 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3993 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3994 | 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] | 3995 | goto err; |
| 3996 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3997 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3998 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3999 | goto out; |
| 4000 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4001 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4002 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4003 | qel = next_qel; |
| 4004 | next_qel = NULL; |
| 4005 | goto next_level; |
| 4006 | } |
| 4007 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4008 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4009 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4010 | 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] | 4011 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4012 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 4013 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4014 | 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] | 4015 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4016 | /* Discard the Handshake keys. */ |
| 4017 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4018 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4019 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4020 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4021 | 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] | 4022 | 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] | 4023 | } |
| 4024 | |
| 4025 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4026 | /* There may be remaining handshake to build (acks) */ |
| 4027 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4028 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4031 | if (!qr) |
| 4032 | 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] | 4033 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4034 | * be considered. |
| 4035 | */ |
| 4036 | 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] | 4037 | goto err; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 4038 | ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms, |
| 4039 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4040 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4041 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4042 | else if (ret == 0) |
| 4043 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4044 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4045 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4046 | goto err; |
| 4047 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4048 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4049 | /* Check if there is something to do for the next level. |
| 4050 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4051 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4052 | (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] | 4053 | (!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] | 4054 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4055 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4056 | goto next_level; |
| 4057 | } |
| 4058 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4059 | out: |
| 4060 | if (qr) |
| 4061 | 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] | 4062 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4063 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4064 | |
| 4065 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4066 | if (qr) |
| 4067 | 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] | 4068 | 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] | 4069 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4070 | } |
| 4071 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4072 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4073 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 4074 | { |
| 4075 | int i; |
| 4076 | |
| 4077 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4078 | if (qel->tx.crypto.bufs[i]) { |
| 4079 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4080 | qel->tx.crypto.bufs[i] = NULL; |
| 4081 | } |
| 4082 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4083 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4087 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4088 | * Returns 1 if succeeded, 0 if not. |
| 4089 | */ |
| 4090 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4091 | enum quic_tls_enc_level level) |
| 4092 | { |
| 4093 | struct quic_enc_level *qel; |
| 4094 | |
| 4095 | qel = &qc->els[level]; |
| 4096 | qel->level = quic_to_ssl_enc_level(level); |
| 4097 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4098 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4099 | 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] | 4100 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4101 | |
| 4102 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4103 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4104 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4105 | qel->rx.crypto.offset = 0; |
| 4106 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4107 | |
| 4108 | /* Allocate only one buffer. */ |
| 4109 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4110 | if (!qel->tx.crypto.bufs) |
| 4111 | goto err; |
| 4112 | |
| 4113 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4114 | if (!qel->tx.crypto.bufs[0]) |
| 4115 | goto err; |
| 4116 | |
| 4117 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4118 | qel->tx.crypto.nb_buf = 1; |
| 4119 | |
| 4120 | qel->tx.crypto.sz = 0; |
| 4121 | qel->tx.crypto.offset = 0; |
| 4122 | |
| 4123 | return 1; |
| 4124 | |
| 4125 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4126 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4127 | return 0; |
| 4128 | } |
| 4129 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4130 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4131 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4132 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4133 | * This function must only be called by the thread responsible of the quic_conn |
| 4134 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4135 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4136 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4137 | { |
| 4138 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4139 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4140 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4141 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4142 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4143 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4144 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4145 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4146 | /* free remaining stream descriptors */ |
| 4147 | node = eb64_first(&qc->streams_by_id); |
| 4148 | while (node) { |
| 4149 | struct qc_stream_desc *stream; |
| 4150 | |
| 4151 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4152 | node = eb64_next(node); |
| 4153 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4154 | /* all streams attached to the quic-conn are released, so |
| 4155 | * qc_stream_desc_free will liberate the stream instance. |
| 4156 | */ |
| 4157 | BUG_ON(!stream->release); |
| 4158 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4159 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4160 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4161 | if (qc->idle_timer_task) { |
| 4162 | task_destroy(qc->idle_timer_task); |
| 4163 | qc->idle_timer_task = NULL; |
| 4164 | } |
| 4165 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4166 | if (qc->timer_task) { |
| 4167 | task_destroy(qc->timer_task); |
| 4168 | qc->timer_task = NULL; |
| 4169 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4170 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4171 | /* remove the connection from receiver cids trees */ |
| 4172 | ebmb_delete(&qc->odcid_node); |
| 4173 | ebmb_delete(&qc->scid_node); |
| 4174 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4175 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4176 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4177 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4178 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4179 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4180 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4181 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4182 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4183 | quic_tls_ku_free(qc); |
| 4184 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4185 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4186 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4190 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4191 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4192 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4193 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 4194 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4195 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4196 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4197 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4198 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4199 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4200 | 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] | 4201 | } |
| 4202 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4203 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4204 | { |
| 4205 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4206 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4207 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4208 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4209 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4210 | /* Next application data can be dropped. */ |
| 4211 | qc->mux_state = QC_MUX_RELEASED; |
| 4212 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4213 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4214 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4215 | quic_conn_release(qc); |
| 4216 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 4217 | return; |
| 4218 | } |
| 4219 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4220 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4223 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4224 | 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] | 4225 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4226 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4227 | struct quic_conn *qc; |
| 4228 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4229 | |
| 4230 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4231 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4232 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4233 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4234 | task->expire = TICK_ETERNITY; |
| 4235 | pktns = quic_loss_pktns(qc); |
| 4236 | if (tick_isset(pktns->tx.loss_time)) { |
| 4237 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4238 | |
| 4239 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4240 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4241 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4242 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4243 | goto out; |
| 4244 | } |
| 4245 | |
| 4246 | if (qc->path->in_flight) { |
Frédéric Lécaille | 3ef729a | 2022-04-21 18:26:22 +0200 | [diff] [blame] | 4247 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4248 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | 3ef729a | 2022-04-21 18:26:22 +0200 | [diff] [blame] | 4249 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4250 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4251 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
Frédéric Lécaille | 3ef729a | 2022-04-21 18:26:22 +0200 | [diff] [blame] | 4252 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4253 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4254 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4255 | 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] | 4256 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4257 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4258 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4259 | 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] | 4260 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4261 | 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] | 4262 | iel->pktns->tx.pto_probe = 1; |
| 4263 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4264 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4265 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4266 | qc->path->loss.pto_count++; |
| 4267 | |
| 4268 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4269 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4270 | |
| 4271 | return task; |
| 4272 | } |
| 4273 | |
| 4274 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 4275 | * connections used to identify the first Initial packets of client connecting |
| 4276 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 4277 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 4278 | * <scid> is the source connection ID with <scid_len> as length. |
| 4279 | * Returns 1 if succeeded, 0 if not. |
| 4280 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4281 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4282 | unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4283 | unsigned char *scid, size_t scid_len, int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4284 | { |
| 4285 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4286 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4287 | /* Initial CID. */ |
| 4288 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4289 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4290 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4291 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4292 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4293 | qc = pool_zalloc(pool_head_quic_conn); |
| 4294 | if (!qc) { |
| 4295 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4296 | goto err; |
| 4297 | } |
| 4298 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4299 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4300 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4301 | 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] | 4302 | goto err; |
| 4303 | } |
| 4304 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4305 | qc->cids = EB_ROOT; |
| 4306 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4307 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4308 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4309 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4310 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4311 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4312 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4313 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4314 | qc->odcid.addrlen = dcid_addr_len; |
| 4315 | memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4316 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4317 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4318 | if (scid_len) |
| 4319 | memcpy(qc->dcid.data, scid, scid_len); |
| 4320 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4321 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4322 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4323 | } |
| 4324 | /* QUIC Client (outgoing connection to servers) */ |
| 4325 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4326 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4327 | if (dcid_len) |
| 4328 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 4329 | qc->dcid.len = dcid_len; |
| 4330 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4331 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4332 | |
| 4333 | /* Initialize the output buffer */ |
| 4334 | qc->obuf.pos = qc->obuf.data; |
| 4335 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4336 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4337 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4338 | 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] | 4339 | goto err; |
| 4340 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4341 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4342 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4343 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4344 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4345 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4346 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4347 | qc->scid = icid->cid; |
| 4348 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4349 | /* Packet number spaces initialization. */ |
| 4350 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4351 | quic_pktns_init(&qc->pktns[i]); |
| 4352 | /* QUIC encryption level context initialization. */ |
| 4353 | 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] | 4354 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4355 | 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] | 4356 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4357 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4358 | /* Initialize the packet number space. */ |
| 4359 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4360 | } |
| 4361 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4362 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4363 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4364 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4365 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4366 | /* TX part. */ |
| 4367 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4368 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4369 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4370 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4371 | /* RX part. */ |
| 4372 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4373 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4374 | |
| 4375 | qc->nb_pkt_for_cc = 1; |
| 4376 | qc->nb_pkt_since_cc = 0; |
| 4377 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4378 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4379 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4380 | 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] | 4381 | goto err; |
| 4382 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4383 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4384 | /* XXX TO DO: Only one path at this time. */ |
| 4385 | qc->path = &qc->paths[0]; |
| 4386 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4387 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4388 | /* required to use MTLIST_IN_LIST */ |
| 4389 | MT_LIST_INIT(&qc->accept_list); |
| 4390 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4391 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4392 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4393 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4394 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4395 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4396 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | |
| 4398 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4399 | 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] | 4400 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4401 | if (qc) |
| 4402 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4403 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4404 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | /* Initialize the timer task of <qc> QUIC connection. |
| 4408 | * Returns 1 if succeeded, 0 if not. |
| 4409 | */ |
| 4410 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4411 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4412 | /* Attach this task to the same thread ID used for the connection */ |
| 4413 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4414 | if (!qc->timer_task) |
| 4415 | return 0; |
| 4416 | |
| 4417 | qc->timer = TICK_ETERNITY; |
| 4418 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4419 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4420 | |
| 4421 | return 1; |
| 4422 | } |
| 4423 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4424 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4425 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4426 | { |
| 4427 | unsigned int expire; |
| 4428 | |
| 4429 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4430 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4431 | } |
| 4432 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4433 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4434 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4435 | */ |
| 4436 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4437 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4438 | if (read) { |
| 4439 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4440 | } |
| 4441 | else { |
| 4442 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4443 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4444 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | /* The task handling the idle timeout */ |
| 4448 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4449 | { |
| 4450 | struct quic_conn *qc = ctx; |
| 4451 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4452 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4453 | * might free the quic-conn too early via quic_close(). |
| 4454 | */ |
| 4455 | qc_notify_close(qc); |
| 4456 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4457 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4458 | * responsible to call quic_close to release it. |
| 4459 | */ |
| 4460 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4461 | if (qc->mux_state != QC_MUX_READY) |
| 4462 | quic_conn_release(qc); |
| 4463 | |
| 4464 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4465 | * least clean some parts of it such as the tasklet. |
| 4466 | */ |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4467 | |
| 4468 | return NULL; |
| 4469 | } |
| 4470 | |
| 4471 | /* Initialize the idle timeout task for <qc>. |
| 4472 | * Returns 1 if succeeded, 0 if not. |
| 4473 | */ |
| 4474 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4475 | { |
| 4476 | qc->idle_timer_task = task_new_here(); |
| 4477 | if (!qc->idle_timer_task) |
| 4478 | return 0; |
| 4479 | |
| 4480 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4481 | qc->idle_timer_task->context = qc; |
| 4482 | qc_idle_timer_rearm(qc, 1); |
| 4483 | task_queue(qc->idle_timer_task); |
| 4484 | |
| 4485 | return 1; |
| 4486 | } |
| 4487 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4488 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4489 | * past one byte of this buffer. |
| 4490 | */ |
| 4491 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4492 | struct quic_rx_packet *pkt) |
| 4493 | { |
| 4494 | unsigned char dcid_len, scid_len; |
| 4495 | |
| 4496 | /* Version */ |
| 4497 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4498 | return 0; |
| 4499 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4500 | /* Destination Connection ID Length */ |
| 4501 | dcid_len = *(*buf)++; |
| 4502 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4503 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4504 | /* XXX MUST BE DROPPED */ |
| 4505 | return 0; |
| 4506 | |
| 4507 | if (dcid_len) { |
| 4508 | /* Check that the length of this received DCID matches the CID lengths |
| 4509 | * of our implementation for non Initials packets only. |
| 4510 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4511 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4512 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4513 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4514 | return 0; |
| 4515 | |
| 4516 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4517 | } |
| 4518 | |
| 4519 | pkt->dcid.len = dcid_len; |
| 4520 | *buf += dcid_len; |
| 4521 | |
| 4522 | /* Source Connection ID Length */ |
| 4523 | scid_len = *(*buf)++; |
| 4524 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4525 | /* XXX MUST BE DROPPED */ |
| 4526 | return 0; |
| 4527 | |
| 4528 | if (scid_len) |
| 4529 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4530 | pkt->scid.len = scid_len; |
| 4531 | *buf += scid_len; |
| 4532 | |
| 4533 | return 1; |
| 4534 | } |
| 4535 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4536 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4537 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4538 | { |
| 4539 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4540 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4541 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4542 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4543 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | /* 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] | 4547 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4548 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4549 | * the packet first byte. |
| 4550 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4551 | * Returns 1 if succeeded, 0 if not. |
| 4552 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4553 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4554 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4555 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4556 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4557 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4558 | { |
| 4559 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4560 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4561 | struct quic_enc_level *qel; |
| 4562 | /* Only for traces. */ |
| 4563 | struct quic_rx_packet *qpkt_trace; |
| 4564 | |
| 4565 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4566 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4567 | /* The packet number is here. This is also the start minus |
| 4568 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4569 | * protection. |
| 4570 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4571 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4572 | |
| 4573 | tel = quic_packet_type_enc_level(pkt->type); |
| 4574 | qel = &qc->els[tel]; |
| 4575 | |
| 4576 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4577 | /* Note that the following function enables us to unprotect the packet |
| 4578 | * number and its length subsequently used to decrypt the entire |
| 4579 | * packets. |
| 4580 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4581 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4582 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4583 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4584 | goto err; |
| 4585 | } |
| 4586 | |
| 4587 | /* The AAD includes the packet number field found at <pn>. */ |
| 4588 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4589 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4590 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4591 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4592 | 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] | 4593 | /* If the packet number space has been discarded, this packet |
| 4594 | * will be not parsed. |
| 4595 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4596 | 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] | 4597 | goto out; |
| 4598 | } |
| 4599 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4600 | 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] | 4601 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4602 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4603 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4604 | } |
| 4605 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4606 | *el = qel; |
| 4607 | /* No reference counter incrementation here!!! */ |
| 4608 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4609 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4610 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4611 | b_add(&qc->rx.buf, pkt->len); |
| 4612 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4613 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4614 | return 1; |
| 4615 | |
| 4616 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4617 | 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] | 4618 | return 0; |
| 4619 | } |
| 4620 | |
| 4621 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4622 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4623 | */ |
| 4624 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4625 | unsigned char byte0, int *long_header) |
| 4626 | { |
| 4627 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4628 | pkt->type = |
| 4629 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4630 | *long_header = 1; |
| 4631 | } |
| 4632 | else { |
| 4633 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4634 | *long_header = 0; |
| 4635 | } |
| 4636 | } |
| 4637 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4638 | /* |
| 4639 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4640 | */ |
| 4641 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4642 | { |
| 4643 | int j = 0, version; |
| 4644 | |
| 4645 | do { |
| 4646 | version = quic_supported_version[j]; |
| 4647 | if (version == pkt->version) |
| 4648 | return 1; |
| 4649 | |
| 4650 | version = quic_supported_version[++j]; |
| 4651 | } while(version); |
| 4652 | |
| 4653 | return 0; |
| 4654 | } |
| 4655 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4656 | /* |
| 4657 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4658 | * address <addr>. |
| 4659 | * Implementation of RFC9000 6. Version Negotiation |
| 4660 | * |
| 4661 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4662 | * |
| 4663 | * Returns 0 on success else non-zero |
| 4664 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4665 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4666 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4667 | { |
| 4668 | char buf[256]; |
| 4669 | int i = 0, j, version; |
| 4670 | const socklen_t addrlen = get_addr_len(addr); |
| 4671 | |
| 4672 | /* |
| 4673 | * header form |
| 4674 | * long header, fixed bit to 0 for Version Negotiation |
| 4675 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4676 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4677 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4678 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4679 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4680 | /* null version for Version Negotiation */ |
| 4681 | buf[i++] = '\x00'; |
| 4682 | buf[i++] = '\x00'; |
| 4683 | buf[i++] = '\x00'; |
| 4684 | buf[i++] = '\x00'; |
| 4685 | |
| 4686 | /* source connection id */ |
| 4687 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4688 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4689 | i += pkt->scid.len; |
| 4690 | |
| 4691 | /* destination connection id */ |
| 4692 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4693 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4694 | i += pkt->dcid.len; |
| 4695 | |
| 4696 | /* supported version */ |
| 4697 | j = 0; |
| 4698 | do { |
| 4699 | version = htonl(quic_supported_version[j]); |
| 4700 | memcpy(&buf[i], &version, sizeof(version)); |
| 4701 | i += sizeof(version); |
| 4702 | |
| 4703 | version = quic_supported_version[++j]; |
| 4704 | } while (version); |
| 4705 | |
| 4706 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4707 | return 1; |
| 4708 | |
| 4709 | return 0; |
| 4710 | } |
| 4711 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4712 | /* Generate the token to be used in Retry packets. The token is written to |
| 4713 | * <buf> which is expected to be <len> bytes. |
| 4714 | * |
| 4715 | * Various parameters are expected to be encoded in the token. For now, only |
| 4716 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4717 | * as this CID must be repeated by the server in the transport parameters. |
| 4718 | * |
| 4719 | * TODO add the client address to validate the token origin. |
| 4720 | * |
| 4721 | * Returns the length of the encoded token or 0 on error. |
| 4722 | */ |
| 4723 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4724 | struct quic_rx_packet *pkt) |
| 4725 | { |
| 4726 | const size_t token_len = 1 + pkt->dcid.len; |
| 4727 | unsigned char i = 0; |
| 4728 | |
| 4729 | if (token_len > len) |
| 4730 | return 0; |
| 4731 | |
| 4732 | buf[i++] = pkt->dcid.len; |
| 4733 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4734 | i += pkt->dcid.len; |
| 4735 | |
| 4736 | return i; |
| 4737 | } |
| 4738 | |
| 4739 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4740 | * the Initial <pkt> packet. |
| 4741 | * |
| 4742 | * Returns 0 on success else non-zero. |
| 4743 | */ |
| 4744 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4745 | struct quic_rx_packet *pkt) |
| 4746 | { |
| 4747 | unsigned char buf[128]; |
| 4748 | int i = 0, token_len; |
| 4749 | const socklen_t addrlen = get_addr_len(addr); |
| 4750 | struct quic_cid scid; |
| 4751 | |
| 4752 | /* long header + fixed bit + packet type 0x3 */ |
| 4753 | buf[i++] = 0xf0; |
| 4754 | /* version */ |
| 4755 | buf[i++] = 0x00; |
| 4756 | buf[i++] = 0x00; |
| 4757 | buf[i++] = 0x00; |
| 4758 | buf[i++] = 0x01; |
| 4759 | |
| 4760 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4761 | buf[i++] = pkt->scid.len; |
| 4762 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4763 | i += pkt->scid.len; |
| 4764 | |
| 4765 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4766 | scid.len = QUIC_HAP_CID_LEN; |
| 4767 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4768 | return 1; |
| 4769 | |
| 4770 | buf[i++] = scid.len; |
| 4771 | memcpy(&buf[i], scid.data, scid.len); |
| 4772 | i += scid.len; |
| 4773 | |
| 4774 | /* token */ |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4775 | if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4776 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4777 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4778 | i += token_len; |
| 4779 | |
| 4780 | /* token integrity tag */ |
| 4781 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4782 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4783 | pkt->dcid.len, buf, i)) { |
| 4784 | return 1; |
| 4785 | } |
| 4786 | |
| 4787 | i += QUIC_TLS_TAG_LEN; |
| 4788 | |
| 4789 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4790 | return 1; |
| 4791 | |
| 4792 | return 0; |
| 4793 | } |
| 4794 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4795 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4796 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4797 | * concatenated to the <pkt> DCID field. |
| 4798 | * |
| 4799 | * Returns the instance or NULL if not found. |
| 4800 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4801 | 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] | 4802 | struct listener *l, |
| 4803 | struct sockaddr_storage *saddr) |
| 4804 | { |
| 4805 | struct quic_conn *qc = NULL; |
| 4806 | struct ebmb_node *node; |
| 4807 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4808 | /* set if the quic_conn is found in the second DCID tree */ |
| 4809 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4810 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4811 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4812 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4813 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4814 | /* DCIDs of first packets coming from multiple clients may have |
| 4815 | * the same values. Let's distinguish them by concatenating the |
| 4816 | * socket addresses. |
| 4817 | */ |
| 4818 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4819 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4820 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4821 | if (node) { |
| 4822 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4823 | goto end; |
| 4824 | } |
| 4825 | } |
| 4826 | |
| 4827 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4828 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4829 | * used. |
| 4830 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4831 | 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] | 4832 | if (!node) |
| 4833 | goto end; |
| 4834 | |
| 4835 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4836 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4837 | found_in_dcid = 1; |
| 4838 | |
| 4839 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4840 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4841 | * If already done, this is a noop. |
| 4842 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4843 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4844 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4845 | |
| 4846 | return qc; |
| 4847 | } |
| 4848 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4849 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4850 | * will extract the parameters stored in the token : <odcid>. |
| 4851 | * |
| 4852 | * Returns 0 on success else non-zero. |
| 4853 | */ |
| 4854 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4855 | struct quic_cid *odcid) |
| 4856 | { |
| 4857 | uint64_t odcid_len; |
| 4858 | |
| 4859 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4860 | return 1; |
| 4861 | |
Frédéric Lécaille | f1f812b | 2022-03-17 16:22:02 +0100 | [diff] [blame] | 4862 | if (odcid_len > QUIC_CID_MAXLEN) |
| 4863 | return 1; |
| 4864 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4865 | memcpy(odcid->data, token, odcid_len); |
| 4866 | odcid->len = odcid_len; |
| 4867 | |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4871 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4872 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4873 | * parameters of this session. |
| 4874 | * This is the responsibility of the caller to check the validity of all the |
| 4875 | * pointers passed as parameter to this function. |
| 4876 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4877 | * CO_ER_SSL_NO_MEM. |
| 4878 | */ |
| 4879 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4880 | unsigned char *params, size_t params_len) |
| 4881 | { |
| 4882 | int retry; |
| 4883 | |
| 4884 | retry = 1; |
| 4885 | retry: |
| 4886 | *ssl = SSL_new(ssl_ctx); |
| 4887 | if (!*ssl) { |
| 4888 | if (!retry--) |
| 4889 | goto err; |
| 4890 | |
| 4891 | pool_gc(NULL); |
| 4892 | goto retry; |
| 4893 | } |
| 4894 | |
| 4895 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4896 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4897 | !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] | 4898 | SSL_free(*ssl); |
| 4899 | *ssl = NULL; |
| 4900 | if (!retry--) |
| 4901 | goto err; |
| 4902 | |
| 4903 | pool_gc(NULL); |
| 4904 | goto retry; |
| 4905 | } |
| 4906 | |
| 4907 | return 0; |
| 4908 | |
| 4909 | err: |
| 4910 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4911 | return -1; |
| 4912 | } |
| 4913 | |
| 4914 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4915 | * used to process <qc> received packets. The allocated context is stored in |
| 4916 | * <qc.xprt_ctx>. |
| 4917 | * |
| 4918 | * Returns 0 on success else non-zero. |
| 4919 | */ |
| 4920 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4921 | { |
| 4922 | struct bind_conf *bc = qc->li->bind_conf; |
| 4923 | struct ssl_sock_ctx *ctx = NULL; |
| 4924 | |
| 4925 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4926 | if (!ctx) |
| 4927 | goto err; |
| 4928 | |
| 4929 | ctx->wait_event.tasklet = tasklet_new(); |
| 4930 | if (!ctx->wait_event.tasklet) |
| 4931 | goto err; |
| 4932 | |
| 4933 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4934 | ctx->wait_event.tasklet->context = ctx; |
| 4935 | ctx->wait_event.events = 0; |
| 4936 | ctx->subs = NULL; |
| 4937 | ctx->xprt_ctx = NULL; |
| 4938 | ctx->qc = qc; |
| 4939 | |
| 4940 | /* Set tasklet tid based on the SCID selected by us for this |
| 4941 | * connection. The upper layer will also be binded on the same thread. |
| 4942 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4943 | 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] | 4944 | |
| 4945 | if (qc_is_listener(qc)) { |
| 4946 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4947 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4948 | goto err; |
| 4949 | } |
| 4950 | |
| 4951 | /* Enabling 0-RTT */ |
| 4952 | if (bc->ssl_conf.early_data) |
| 4953 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4954 | |
| 4955 | SSL_set_accept_state(ctx->ssl); |
| 4956 | } |
| 4957 | |
| 4958 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4959 | |
| 4960 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4961 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4962 | |
| 4963 | return 0; |
| 4964 | |
| 4965 | err: |
| 4966 | if (ctx && ctx->wait_event.tasklet) |
| 4967 | tasklet_free(ctx->wait_event.tasklet); |
| 4968 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4969 | |
| 4970 | return 1; |
| 4971 | } |
| 4972 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4973 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4974 | struct quic_rx_packet *pkt, int first_pkt, |
| 4975 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4976 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4977 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4978 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4979 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4980 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4981 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4982 | size_t b_cspace; |
| 4983 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4984 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4985 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4986 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4987 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4988 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4989 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4990 | /* This ist only to please to traces and distinguish the |
| 4991 | * packet with parsed packet number from others. |
| 4992 | */ |
| 4993 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4994 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4995 | goto err; |
| 4996 | |
| 4997 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4998 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4999 | /* XXX TO BE DISCARDED */ |
| 5000 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5001 | goto err; |
| 5002 | } |
| 5003 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5004 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5005 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5006 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5007 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5008 | uint64_t len; |
| 5009 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5010 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5011 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5012 | goto err; |
| 5013 | } |
| 5014 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5015 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5016 | * they must have the same DCID. |
| 5017 | */ |
| 5018 | if (!first_pkt && |
| 5019 | (pkt->dcid.len != dgram->dcid_len || |
| 5020 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5021 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5022 | goto err; |
| 5023 | } |
| 5024 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5025 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 5026 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 5027 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5028 | goto err; |
| 5029 | } |
| 5030 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5031 | /* RFC9000 6. Version Negotiation */ |
| 5032 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5033 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5034 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5035 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 5036 | goto err; |
| 5037 | } |
| 5038 | |
| 5039 | 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] | 5040 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5041 | } |
| 5042 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5043 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5044 | * there is no Initial connection IDs storage. |
| 5045 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5046 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5047 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5048 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5049 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5050 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5051 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5052 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5053 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5054 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 5055 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 5056 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5057 | */ |
| 5058 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5059 | |
| 5060 | /* TODO Retry should be automatically activated if |
| 5061 | * suspect network usage is detected. |
| 5062 | */ |
| 5063 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 5064 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5065 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5066 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5067 | goto err; |
| 5068 | } |
| 5069 | |
| 5070 | goto err; |
| 5071 | } |
| 5072 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5073 | pkt->token = buf; |
| 5074 | buf += pkt->token_len; |
| 5075 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5076 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5077 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5078 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5079 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5080 | goto err; |
| 5081 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5084 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5085 | end - buf < len) { |
| 5086 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5087 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5088 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5089 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5090 | payload = buf; |
| 5091 | pkt->len = len + payload - beg; |
| 5092 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5093 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5094 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5095 | int ipv4; |
| 5096 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5097 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5098 | const unsigned char *salt = initial_salt_v1; |
| 5099 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5100 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5101 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 5102 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5103 | goto err; |
| 5104 | } |
| 5105 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5106 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 5107 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 5108 | goto err; |
| 5109 | } |
| 5110 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5111 | pkt->saddr = dgram->saddr; |
| 5112 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5113 | qc = qc_new_conn(pkt->version, ipv4, |
| 5114 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 5115 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5116 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5117 | goto err; |
| 5118 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 5119 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 5120 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5121 | odcid = &qc->rx.params.original_destination_connection_id; |
| 5122 | /* Copy the transport parameters. */ |
| 5123 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5124 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5125 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5126 | if (pkt->token_len) { |
| 5127 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 5128 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 5129 | goto err; |
| 5130 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 5131 | /* Copy retry_source_connection_id transport parameter. */ |
| 5132 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 5133 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5134 | } |
| 5135 | else { |
| 5136 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 5137 | odcid->len = pkt->dcid.len; |
| 5138 | } |
| 5139 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5140 | /* Copy the initial source connection ID. */ |
| 5141 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 5142 | qc->enc_params_len = |
| 5143 | quic_transport_params_encode(qc->enc_params, |
| 5144 | qc->enc_params + sizeof qc->enc_params, |
| 5145 | &qc->rx.params, 1); |
| 5146 | if (!qc->enc_params_len) |
| 5147 | goto err; |
| 5148 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5149 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 5150 | goto err; |
| 5151 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 5152 | if (!quic_conn_init_timer(qc)) |
| 5153 | goto err; |
| 5154 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 5155 | if (!quic_conn_init_idle_timer_task(qc)) |
| 5156 | goto err; |
| 5157 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 5158 | /* NOTE: the socket address has been concatenated to the destination ID |
| 5159 | * chosen by the client for Initial packets. |
| 5160 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5161 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 5162 | salt = initial_salt_draft_29; |
| 5163 | salt_len = sizeof initial_salt_draft_29; |
| 5164 | } |
| 5165 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5166 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5167 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 5168 | goto err; |
| 5169 | } |
| 5170 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5171 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5172 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5173 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5174 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5175 | /* If the insertion failed, it means that another |
| 5176 | * thread has already allocated a QUIC connection for |
| 5177 | * the same CID. Liberate our allocated connection. |
| 5178 | */ |
| 5179 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5180 | qc_to_purge = qc; |
| 5181 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5182 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5183 | pkt->qc = qc; |
| 5184 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5185 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5186 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5187 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5188 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5189 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5190 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5191 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5192 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5193 | } |
| 5194 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5195 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5199 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5200 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5201 | goto err; |
| 5202 | } |
| 5203 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5204 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5205 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5206 | |
| 5207 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5208 | * they must have the same DCID. |
| 5209 | */ |
| 5210 | if (!first_pkt && |
| 5211 | (pkt->dcid.len != dgram->dcid_len || |
| 5212 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5213 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5214 | goto err; |
| 5215 | } |
| 5216 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5217 | buf += QUIC_HAP_CID_LEN; |
| 5218 | |
| 5219 | /* A short packet is the last one of a UDP datagram. */ |
| 5220 | payload = buf; |
| 5221 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5222 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5223 | 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] | 5224 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5225 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5226 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 5227 | goto err; |
| 5228 | } |
| 5229 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5230 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5233 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 5234 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 5235 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 5236 | qc->nb_pkt_for_cc++; |
| 5237 | qc->nb_pkt_since_cc = 0; |
| 5238 | } |
| 5239 | /* Skip the entire datagram */ |
| 5240 | pkt->len = end - beg; |
| 5241 | TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc); |
| 5242 | goto out; |
| 5243 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5244 | |
| 5245 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5246 | * they must have the same DCID. |
| 5247 | * |
| 5248 | * This check must be done after the final update to pkt.len to |
| 5249 | * properly drop the packet on failure. |
| 5250 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5251 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5252 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5253 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 5254 | QUIC_EV_CONN_LPKT, qc); |
| 5255 | /* Reset the anti-amplification bit. It will be set again |
| 5256 | * when sending the next packet if reached again. |
| 5257 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5258 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 5259 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5260 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5261 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5262 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5263 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5264 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5265 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5266 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5267 | goto out; |
| 5268 | } |
| 5269 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5270 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5271 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5272 | b_cspace = b_contig_space(&qc->rx.buf); |
| 5273 | if (b_cspace < pkt->len) { |
| 5274 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5275 | if (b_cspace) { |
| 5276 | b_putchr(&qc->rx.buf, 0x00); |
| 5277 | b_cspace--; |
| 5278 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5279 | b_add(&qc->rx.buf, b_cspace); |
| 5280 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5281 | 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] | 5282 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5283 | goto err; |
| 5284 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5285 | } |
| 5286 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5287 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5288 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 5289 | goto err; |
| 5290 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5291 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5292 | 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] | 5293 | if (pkt->aad_len) |
| 5294 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5295 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 5296 | /* Wake up the connection packet handler task from here only if all |
| 5297 | * the contexts have been initialized, especially the mux context |
| 5298 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 5299 | * will start it if these contexts for the connection are not already |
| 5300 | * initialized. |
| 5301 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5302 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5303 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5304 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5305 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5306 | 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] | 5307 | |
| 5308 | return pkt->len; |
| 5309 | |
| 5310 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5311 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5312 | * This cannot be done by this thread. |
| 5313 | */ |
| 5314 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5315 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5316 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5317 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5318 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5319 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5320 | if (!pkt->len) |
| 5321 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5322 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5323 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5324 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5325 | return -1; |
| 5326 | } |
| 5327 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5328 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5329 | * 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] | 5330 | */ |
| 5331 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5332 | int type, size_t pn_len, struct quic_conn *conn) |
| 5333 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5334 | 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] | 5335 | return 0; |
| 5336 | |
| 5337 | /* #0 byte flags */ |
| 5338 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5339 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5340 | /* Version */ |
| 5341 | quic_write_uint32(buf, end, conn->version); |
| 5342 | *(*buf)++ = conn->dcid.len; |
| 5343 | /* Destination connection ID */ |
| 5344 | if (conn->dcid.len) { |
| 5345 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5346 | *buf += conn->dcid.len; |
| 5347 | } |
| 5348 | /* Source connection ID */ |
| 5349 | *(*buf)++ = conn->scid.len; |
| 5350 | if (conn->scid.len) { |
| 5351 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5352 | *buf += conn->scid.len; |
| 5353 | } |
| 5354 | |
| 5355 | return 1; |
| 5356 | } |
| 5357 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5358 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5359 | * 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] | 5360 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5361 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5362 | size_t pn_len, struct quic_conn *conn, |
| 5363 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5364 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5365 | if (end - *buf < 1 + conn->dcid.len) |
| 5366 | return 0; |
| 5367 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5368 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5369 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5370 | ((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] | 5371 | /* Destination connection ID */ |
| 5372 | if (conn->dcid.len) { |
| 5373 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5374 | *buf += conn->dcid.len; |
| 5375 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5376 | |
| 5377 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5381 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5382 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5383 | * Returns 1 if succeeded or 0 if failed. |
| 5384 | */ |
| 5385 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5386 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5387 | { |
| 5388 | int i, ret, outlen; |
| 5389 | EVP_CIPHER_CTX *ctx; |
| 5390 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5391 | * and at most 4 bytes for the packet number |
| 5392 | */ |
| 5393 | unsigned char mask[5] = {0}; |
| 5394 | |
| 5395 | ret = 0; |
| 5396 | ctx = EVP_CIPHER_CTX_new(); |
| 5397 | if (!ctx) |
| 5398 | return 0; |
| 5399 | |
| 5400 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5401 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5402 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5403 | goto out; |
| 5404 | |
| 5405 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5406 | for (i = 0; i < pnlen; i++) |
| 5407 | pn[i] ^= mask[i + 1]; |
| 5408 | |
| 5409 | ret = 1; |
| 5410 | |
| 5411 | out: |
| 5412 | EVP_CIPHER_CTX_free(ctx); |
| 5413 | |
| 5414 | return ret; |
| 5415 | } |
| 5416 | |
| 5417 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5418 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5419 | * Return 1 if succeeded, 0 if not. |
| 5420 | */ |
| 5421 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5422 | { |
| 5423 | size_t room, ack_delay_sz; |
| 5424 | |
| 5425 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5426 | /* A frame is made of 1 byte for the frame type. */ |
| 5427 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5428 | 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] | 5429 | return 0; |
| 5430 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5431 | 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] | 5432 | } |
| 5433 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5434 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5435 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5436 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5437 | * with the number of bytes already present in this buffer which must be taken |
| 5438 | * into an account for the Length packet field value. <headlen> is the number of |
| 5439 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5440 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5441 | * 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] | 5442 | * 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^] | 5443 | * 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] | 5444 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5445 | 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] | 5446 | size_t room, size_t *len, size_t headlen, |
| 5447 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5448 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5449 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5450 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5451 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5452 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5453 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5454 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5455 | return 0; |
| 5456 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5457 | /* If we are not probing we must take into an account the congestion |
| 5458 | * control window. |
| 5459 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5460 | if (!qel->pktns->tx.pto_probe) { |
| 5461 | size_t remain = quic_path_prep_data(qc->path); |
| 5462 | |
| 5463 | if (headlen > remain) |
| 5464 | return 0; |
| 5465 | |
| 5466 | room = QUIC_MIN(room, remain - headlen); |
| 5467 | } |
| 5468 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5469 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5470 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame^] | 5471 | |
| 5472 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 5473 | * returned by this function only if at least one frame could be built |
| 5474 | * in the switch/case block. |
| 5475 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5476 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5477 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5478 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5479 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5480 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5481 | break; |
| 5482 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5483 | switch (cf->type) { |
| 5484 | case QUIC_FT_CRYPTO: |
| 5485 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5486 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5487 | /* Compute the length of this CRYPTO frame header */ |
| 5488 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5489 | /* Compute the data length of this CRyPTO frame. */ |
| 5490 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5491 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5492 | 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] | 5493 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame^] | 5494 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5495 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5496 | /* CRYPTO frame length. */ |
| 5497 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5498 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5499 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5500 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5501 | * length and the length of this length. |
| 5502 | */ |
| 5503 | *len += flen; |
| 5504 | room -= flen; |
| 5505 | if (dlen == cf->crypto.len) { |
| 5506 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5507 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5508 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5509 | } |
| 5510 | else { |
| 5511 | struct quic_frame *new_cf; |
| 5512 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5513 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5514 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5515 | 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^] | 5516 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5517 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5518 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5519 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5520 | new_cf->type = QUIC_FT_CRYPTO; |
| 5521 | new_cf->crypto.len = dlen; |
| 5522 | new_cf->crypto.offset = cf->crypto.offset; |
| 5523 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5524 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5525 | /* Consume <dlen> bytes of the current frame. */ |
| 5526 | cf->crypto.len -= dlen; |
| 5527 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5528 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5529 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5530 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5531 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5532 | /* Note that these frames are accepted in short packets only without |
| 5533 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5534 | * 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] | 5535 | * |
| 5536 | * 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] | 5537 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5538 | */ |
| 5539 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5540 | ((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] | 5541 | /* Compute the data length of this STREAM frame. */ |
| 5542 | avail_room = room - hlen - *len; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5543 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5544 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame^] | 5545 | if ((ssize_t)avail_room <= 0) |
| 5546 | continue; |
| 5547 | |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5548 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5549 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5550 | if (dlen > cf->stream.len) { |
| 5551 | dlen = cf->stream.len; |
| 5552 | } |
| 5553 | dlen_sz = quic_int_getsize(dlen); |
| 5554 | flen = hlen + dlen_sz + dlen; |
| 5555 | } |
| 5556 | else { |
| 5557 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5558 | flen = hlen + dlen; |
| 5559 | } |
| 5560 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5561 | 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] | 5562 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5563 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5564 | /* Add the STREAM data length and its encoded length to the packet |
| 5565 | * length and the length of this length. |
| 5566 | */ |
| 5567 | *len += flen; |
| 5568 | room -= flen; |
| 5569 | if (dlen == cf->stream.len) { |
| 5570 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5571 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5572 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5573 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5574 | /* The MUX stream might be released at this |
| 5575 | * stage. This can most notably happen on |
| 5576 | * retransmission. |
| 5577 | */ |
| 5578 | if (qc->mux_state == QC_MUX_READY && |
| 5579 | !cf->stream.stream->release) { |
| 5580 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5581 | cf->stream.len, |
| 5582 | cf->stream.offset.key); |
| 5583 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5584 | } |
| 5585 | else { |
| 5586 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5587 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5588 | |
| 5589 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5590 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5591 | 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^] | 5592 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5593 | } |
| 5594 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5595 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5596 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5597 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5598 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5599 | new_cf->stream.id = cf->stream.id; |
| 5600 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5601 | new_cf->stream.offset = cf->stream.offset; |
| 5602 | new_cf->stream.len = dlen; |
| 5603 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5604 | /* FIN bit reset */ |
| 5605 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5606 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5607 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5608 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5609 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5610 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5611 | b_size(cf->stream.buf), |
| 5612 | (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] | 5613 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5614 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5615 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5616 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5617 | /* The MUX stream might be released at this |
| 5618 | * stage. This can most notably happen on |
| 5619 | * retransmission. |
| 5620 | */ |
| 5621 | if (qc->mux_state == QC_MUX_READY && |
| 5622 | !cf->stream.stream->release) { |
| 5623 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5624 | new_cf->stream.len, |
| 5625 | new_cf->stream.offset.key); |
| 5626 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5627 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5628 | |
| 5629 | /* TODO the MUX is notified about the frame sending via |
| 5630 | * previous qcc_streams_sent_done call. However, the |
| 5631 | * sending can fail later, for example if the sendto |
| 5632 | * system call returns an error. As the MUX has been |
| 5633 | * notified, the transport layer is responsible to |
| 5634 | * bufferize and resent the announced data later. |
| 5635 | */ |
| 5636 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5637 | break; |
| 5638 | |
| 5639 | default: |
| 5640 | flen = qc_frm_len(cf); |
| 5641 | BUG_ON(!flen); |
| 5642 | if (flen > room) |
| 5643 | continue; |
| 5644 | |
| 5645 | *len += flen; |
| 5646 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5647 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5648 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5649 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5650 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame^] | 5651 | |
| 5652 | /* 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] | 5653 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5654 | } |
| 5655 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5656 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5659 | /* 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] | 5660 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5661 | * 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] | 5662 | * filling the buffer with as much frames as possible from <frms> list of |
| 5663 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5664 | * 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] | 5665 | * 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] | 5666 | * having returned from this function. |
| 5667 | * 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] | 5668 | * number field in this packet. <pn_len> will also have the packet number |
| 5669 | * length as value. |
| 5670 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5671 | * 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] | 5672 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5673 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5674 | size_t dglen, struct quic_tx_packet *pkt, |
| 5675 | 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] | 5676 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5677 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5678 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5679 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5680 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5681 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5682 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5683 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5684 | 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] | 5685 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5686 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5687 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5688 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5689 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5690 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5691 | /* Length field value with CRYPTO frames if present. */ |
| 5692 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5693 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5694 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5695 | * buffer to respect the congestion controller window. |
| 5696 | * 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] | 5697 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5698 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5699 | size_t path_room; |
| 5700 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5701 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5702 | if (end - beg > path_room) |
| 5703 | end = beg + path_room; |
| 5704 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5705 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5706 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5707 | * length field if any. |
| 5708 | */ |
| 5709 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5710 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5711 | goto no_room; |
| 5712 | |
| 5713 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5714 | 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] | 5715 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5716 | *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] | 5717 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5718 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5719 | !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] | 5720 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5721 | !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] | 5722 | goto no_room; |
| 5723 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5724 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5725 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5726 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5727 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5728 | /* Build an ACK frame if required. */ |
| 5729 | ack_frm_len = 0; |
Frédéric Lécaille | 337108e | 2022-04-25 10:38:27 +0200 | [diff] [blame] | 5730 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5731 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5732 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5733 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5734 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5735 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5736 | * that from here, we do not know if we will have to send a PING frame. |
| 5737 | * This will be decided after having computed the ack-eliciting frames |
| 5738 | * to be added to this packet. |
| 5739 | */ |
| 5740 | 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] | 5741 | if (!ack_frm_len) |
| 5742 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5743 | } |
| 5744 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5745 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5746 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5747 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5748 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5749 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5750 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5751 | 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] | 5752 | /* Initialize the length of the frames built below to <len>. |
| 5753 | * If any frame could be successfully built by qc_build_frms(), |
| 5754 | * we will have len_frms > len. |
| 5755 | */ |
| 5756 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5757 | if (!qc_build_frms(&frm_list, frms, |
| 5758 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5759 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5760 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5761 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5762 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5763 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5764 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5765 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5766 | * for the encryption tag. It must be taken into an account for the length |
| 5767 | * of this packet. |
| 5768 | */ |
| 5769 | if (len_frms) |
| 5770 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5771 | else |
| 5772 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5773 | /* CONNECTION_CLOSE frame */ |
| 5774 | if (cc) { |
| 5775 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5776 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5777 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5778 | len += qc_frm_len(&cc_frm); |
| 5779 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5780 | add_ping_frm = 0; |
| 5781 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5782 | len_sz = quic_int_getsize(len); |
| 5783 | /* Add this packet size to <dglen> */ |
| 5784 | dglen += head_len + len_sz + len; |
| 5785 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5786 | /* This is a maximum padding size */ |
| 5787 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5788 | /* The length field value is of this packet is <len> + <padding_len> |
| 5789 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5790 | * <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] | 5791 | * sizes from <padding_len>. |
| 5792 | */ |
| 5793 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5794 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5795 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5796 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5797 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5798 | /* 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] | 5799 | add_ping_frm = 1; |
| 5800 | len += 1; |
| 5801 | } |
| 5802 | /* 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] | 5803 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5804 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5805 | } |
| 5806 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5807 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5808 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5809 | |
| 5810 | /* Packet number field address. */ |
| 5811 | *buf_pn = pos; |
| 5812 | |
| 5813 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5814 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5815 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5816 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5817 | if (ack_frm_len) { |
| 5818 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 5819 | goto no_room; |
| 5820 | |
| 5821 | 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] | 5822 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5823 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5824 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5825 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5826 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5827 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5828 | unsigned char *spos = pos; |
| 5829 | |
| 5830 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5831 | ssize_t room = end - pos; |
| 5832 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5833 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5834 | /* TODO: this should not have happened if qc_build_frms() |
| 5835 | * had correctly computed and sized the frames to be |
| 5836 | * added to this packet. Note that <cf> was added |
| 5837 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 5838 | */ |
| 5839 | LIST_DELETE(&cf->list); |
| 5840 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5841 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5842 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5843 | |
| 5844 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5845 | quic_tx_packet_refinc(pkt); |
| 5846 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | /* Build a PING frame if needed. */ |
| 5851 | if (add_ping_frm) { |
| 5852 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5853 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5854 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5855 | } |
| 5856 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5857 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5858 | if (cc) { |
| 5859 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 5860 | goto no_room; |
| 5861 | |
| 5862 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 5863 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5864 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5865 | /* Build a PADDING frame if needed. */ |
| 5866 | if (padding_len) { |
| 5867 | frm.type = QUIC_FT_PADDING; |
| 5868 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5869 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5870 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5871 | } |
| 5872 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5873 | /* If this packet is ack-eliciting and we are probing let's |
| 5874 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5875 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5876 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5877 | qel->pktns->tx.pto_probe) |
| 5878 | qel->pktns->tx.pto_probe--; |
| 5879 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5880 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5881 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5882 | 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] | 5883 | |
| 5884 | return 1; |
| 5885 | |
| 5886 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5887 | /* 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] | 5888 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5889 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt); |
| 5890 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5891 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5892 | } |
| 5893 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5894 | 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] | 5895 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5896 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5897 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5898 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5899 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5900 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5901 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5902 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5903 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5904 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5905 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5906 | } |
| 5907 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5908 | /* 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] | 5909 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 5910 | * of prebuilt frames. |
| 5911 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5912 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5913 | * -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] | 5914 | * XXX NOTE XXX |
| 5915 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 5916 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 5917 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5918 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5919 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5920 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5921 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5922 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5923 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5924 | { |
| 5925 | /* The pointer to the packet number field. */ |
| 5926 | unsigned char *buf_pn; |
| 5927 | unsigned char *beg, *end, *payload; |
| 5928 | int64_t pn; |
| 5929 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5930 | struct quic_tls_ctx *tls_ctx; |
| 5931 | struct quic_tx_packet *pkt; |
| 5932 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5933 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5934 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5935 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5936 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5937 | 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] | 5938 | *err = -2; |
| 5939 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5940 | } |
| 5941 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5942 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5943 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5944 | pn_len = 0; |
| 5945 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5946 | |
| 5947 | pn = qel->pktns->tx.next_pn + 1; |
| 5948 | 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] | 5949 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5950 | *err = -1; |
| 5951 | goto err; |
| 5952 | } |
| 5953 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5954 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5955 | payload = buf_pn + pn_len; |
| 5956 | payload_len = end - payload; |
| 5957 | aad_len = payload - beg; |
| 5958 | |
| 5959 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5960 | 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] | 5961 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5962 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5963 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5964 | |
| 5965 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5966 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5967 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5968 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5969 | 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] | 5970 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5971 | goto err; |
| 5972 | } |
| 5973 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5974 | /* Consume a packet number */ |
| 5975 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5976 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 5977 | 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] | 5978 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5979 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5980 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5981 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5982 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5983 | /* 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] | 5984 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5985 | pkt->in_flight_len = pkt->len; |
| 5986 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5987 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 5988 | /* Always reset this flags */ |
| 5989 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5990 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 5991 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 5992 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 5993 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5994 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5995 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5996 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5997 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5998 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5999 | |
| 6000 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6001 | /* TODO: what about the frames which have been built |
| 6002 | * for this packet. |
| 6003 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6004 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6005 | 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] | 6006 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6007 | } |
| 6008 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6009 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6010 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6011 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6012 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6013 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6014 | */ |
| 6015 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6016 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6017 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6018 | |
| 6019 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6020 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6021 | |
| 6022 | es->events |= event_type; |
| 6023 | qcc->subs = es; |
| 6024 | |
| 6025 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6026 | 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] | 6027 | |
| 6028 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6029 | 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] | 6030 | |
| 6031 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6035 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6036 | * subscribe() call. It always returns zero. |
| 6037 | */ |
| 6038 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6039 | { |
| 6040 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6041 | } |
| 6042 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6043 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 6044 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6045 | */ |
| 6046 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 6047 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 6048 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6049 | |
| 6050 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 6051 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6052 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6053 | if (*xprt_ctx) |
| 6054 | goto out; |
| 6055 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6056 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6057 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6058 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 6059 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6060 | |
| 6061 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6064 | /* Start the QUIC transport layer */ |
| 6065 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 6066 | { |
| 6067 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 6068 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6069 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6070 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6071 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 6072 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 6073 | /* prepare a CONNECTION_CLOSE frame */ |
| 6074 | qc->err_code = QC_ERR_APPLICATION_ERROR; |
| 6075 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 6076 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6077 | } |
| 6078 | |
| 6079 | /* mux-quic can now be considered ready. */ |
| 6080 | qc->mux_state = QC_MUX_READY; |
| 6081 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6082 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 6083 | return 1; |
| 6084 | } |
| 6085 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6086 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 6087 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6088 | 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] | 6089 | return NULL; |
| 6090 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6091 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6092 | } |
| 6093 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6094 | /* transport-layer operations for QUIC connections. */ |
| 6095 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 6096 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6097 | .subscribe = quic_conn_subscribe, |
| 6098 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6099 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6100 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6101 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 6102 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6103 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6104 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6105 | .name = "QUIC", |
| 6106 | }; |
| 6107 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6108 | static void __quic_conn_init(void) |
| 6109 | { |
| 6110 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 6111 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 6112 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6113 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6114 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6115 | static void __quic_conn_deinit(void) |
| 6116 | { |
| 6117 | BIO_meth_free(ha_quic_meth); |
| 6118 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6119 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6120 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6121 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 6122 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 6123 | * 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] | 6124 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6125 | 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] | 6126 | { |
| 6127 | unsigned char *pos; |
| 6128 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6129 | struct quic_dghdlr *dghdlr = ctx; |
| 6130 | struct quic_dgram *dgram; |
| 6131 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6132 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6133 | 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] | 6134 | pos = dgram->buf; |
| 6135 | end = pos + dgram->len; |
| 6136 | do { |
| 6137 | int ret; |
| 6138 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6139 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6140 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 6141 | if (!pkt) |
| 6142 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6143 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6144 | quic_rx_packet_refinc(pkt); |
| 6145 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 6146 | first_pkt = 0; |
| 6147 | pos += pkt->len; |
| 6148 | quic_rx_packet_refdec(pkt); |
| 6149 | if (ret == -1) |
| 6150 | /* If the packet length could not be found, we cannot continue. */ |
| 6151 | break; |
| 6152 | } while (pos < end); |
| 6153 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6154 | /* Increasing the received bytes counter by the UDP datagram length |
| 6155 | * if this datagram could be associated to a connection. |
| 6156 | */ |
| 6157 | if (dgram->qc) |
| 6158 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 6159 | |
| 6160 | /* Mark this datagram as consumed */ |
| 6161 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6162 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6163 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6164 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6165 | |
| 6166 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6167 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6168 | } |
| 6169 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6170 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 6171 | * Returns 1 if succeeded, 0 if not. |
| 6172 | */ |
| 6173 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 6174 | unsigned char **dcid, size_t *dcid_len) |
| 6175 | { |
| 6176 | int long_header; |
| 6177 | size_t minlen, skip; |
| 6178 | |
| 6179 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 6180 | goto err; |
| 6181 | |
| 6182 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 6183 | minlen = long_header ? |
| 6184 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 6185 | 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] | 6186 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6187 | goto err; |
| 6188 | |
| 6189 | buf += skip; |
| 6190 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 6191 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 6192 | goto err; |
| 6193 | |
| 6194 | *dcid = buf; |
| 6195 | |
| 6196 | return 1; |
| 6197 | |
| 6198 | err: |
| 6199 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 6200 | return 0; |
| 6201 | } |
| 6202 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 6203 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 6204 | * correct datagram handler. |
| 6205 | * Return 1 if a correct datagram could be found, 0 if not. |
| 6206 | */ |
| 6207 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 6208 | struct sockaddr_storage *saddr, |
| 6209 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6210 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6211 | struct quic_dgram *dgram; |
| 6212 | unsigned char *dcid; |
| 6213 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 6214 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6215 | |
| 6216 | if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len)) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6217 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6218 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 6219 | dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6220 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6221 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6222 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 6223 | cid_tid = quic_get_cid_tid(dcid); |
| 6224 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6225 | /* All the members must be initialized! */ |
| 6226 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6227 | dgram->buf = buf; |
| 6228 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6229 | dgram->dcid = dcid; |
| 6230 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6231 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6232 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6233 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 6234 | MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6235 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 6236 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6237 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6238 | return 1; |
| 6239 | |
| 6240 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6241 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6242 | } |
| 6243 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 6244 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 6245 | void qc_notify_close(struct quic_conn *qc) |
| 6246 | { |
| 6247 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 6248 | return; |
| 6249 | |
| 6250 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 6251 | |
| 6252 | /* wake up the MUX */ |
| 6253 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 6254 | qc->conn->mux->wake(qc->conn); |
| 6255 | } |
| 6256 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6257 | /* Function to automatically activate QUIC traces on stdout. |
| 6258 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 6259 | * Main use for now is in the docker image for QUIC interop testing. |
| 6260 | */ |
| 6261 | static void quic_init_stdout_traces(void) |
| 6262 | { |
| 6263 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 6264 | trace_quic.sink = sink_find("stdout"); |
| 6265 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6266 | trace_quic.state = TRACE_STATE_RUNNING; |
| 6267 | #endif |
| 6268 | } |
| 6269 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 6270 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6271 | /* |
| 6272 | * Local variables: |
| 6273 | * c-indent-level: 8 |
| 6274 | * c-basic-offset: 8 |
| 6275 | * End: |
| 6276 | */ |