Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <netinet/tcp.h> |
| 23 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 24 | #include <import/ebmbtree.h> |
| 25 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 26 | #include <haproxy/buf-t.h> |
| 27 | #include <haproxy/compat.h> |
| 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/debug.h> |
| 30 | #include <haproxy/tools.h> |
| 31 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 32 | |
| 33 | #include <haproxy/connection.h> |
| 34 | #include <haproxy/fd.h> |
| 35 | #include <haproxy/freq_ctr.h> |
| 36 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 37 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 38 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 39 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 40 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 41 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 47 | #include <haproxy/quic_sock.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 48 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 49 | #include <haproxy/cbuf.h> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 50 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 51 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 52 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 53 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 54 | #include <haproxy/task.h> |
| 55 | #include <haproxy/trace.h> |
| 56 | #include <haproxy/xprt_quic.h> |
| 57 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 58 | /* list of supported QUIC versions by this implementation */ |
| 59 | static int quic_supported_version[] = { |
| 60 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 61 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 62 | |
| 63 | /* placeholder, do not add entry after this */ |
| 64 | 0x0 |
| 65 | }; |
| 66 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 67 | /* This is the values of some QUIC transport parameters when absent. |
| 68 | * Should be used to initialize any transport parameters (local or remote) |
| 69 | * before updating them with customized values. |
| 70 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 71 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 72 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 73 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 74 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 75 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* trace source and events */ |
| 79 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 80 | const struct trace_source *src, |
| 81 | const struct ist where, const struct ist func, |
| 82 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 83 | |
| 84 | static const struct trace_event quic_trace_events[] = { |
| 85 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 86 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 87 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 90 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 91 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 92 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 93 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 96 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 97 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 102 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 103 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 104 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 106 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 107 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 108 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 109 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 110 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 111 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 112 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 113 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 114 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 115 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 116 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 117 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 118 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 119 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 120 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 121 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 122 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 123 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 124 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 125 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 126 | { /* end */ } |
| 127 | }; |
| 128 | |
| 129 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 130 | /* arg1 */ { /* already used by the connection */ }, |
| 131 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 132 | /* arg3 */ { }, |
| 133 | /* arg4 */ { } |
| 134 | }; |
| 135 | |
| 136 | static const struct name_desc quic_trace_decoding[] = { |
| 137 | #define QUIC_VERB_CLEAN 1 |
| 138 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 139 | { /* end */ } |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | struct trace_source trace_quic = { |
| 144 | .name = IST("quic"), |
| 145 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 146 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 147 | .default_cb = quic_trace, |
| 148 | .known_events = quic_trace_events, |
| 149 | .lockon_args = quic_trace_lockon_args, |
| 150 | .decoding = quic_trace_decoding, |
| 151 | .report_events = ~0, /* report everything by default */ |
| 152 | }; |
| 153 | |
| 154 | #define TRACE_SOURCE &trace_quic |
| 155 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 156 | |
| 157 | static BIO_METHOD *ha_quic_meth; |
| 158 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 159 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 160 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 161 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 162 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 163 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | DECLARE_POOL(pool_head_quic_connection_id, |
| 165 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 166 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 167 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 168 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 169 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 171 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 172 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 173 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 174 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 175 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 176 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 177 | int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 178 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 179 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 180 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 181 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 182 | /* Only for debug purpose */ |
| 183 | struct enc_debug_info { |
| 184 | unsigned char *payload; |
| 185 | size_t payload_len; |
| 186 | unsigned char *aad; |
| 187 | size_t aad_len; |
| 188 | uint64_t pn; |
| 189 | }; |
| 190 | |
| 191 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 192 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 193 | unsigned char *payload, size_t payload_len, |
| 194 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 195 | { |
| 196 | edi->payload = payload; |
| 197 | edi->payload_len = payload_len; |
| 198 | edi->aad = aad; |
| 199 | edi->aad_len = aad_len; |
| 200 | edi->pn = pn; |
| 201 | } |
| 202 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 203 | /* Trace callback for QUIC. |
| 204 | * These traces always expect that arg1, if non-null, is of type connection. |
| 205 | */ |
| 206 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 207 | const struct ist where, const struct ist func, |
| 208 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 209 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 210 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 212 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 213 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 215 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 216 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 217 | chunk_appendf(&trace_buf, "\n odcid"); |
| 218 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 219 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 220 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 221 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 222 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 226 | const enum ssl_encryption_level_t *level = a2; |
| 227 | const size_t *len = a3; |
| 228 | |
| 229 | if (level) { |
| 230 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 231 | |
| 232 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 233 | } |
| 234 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 235 | 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] | 236 | } |
| 237 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 238 | /* Initial read & write secrets. */ |
| 239 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 240 | const unsigned char *rx_sec = a2; |
| 241 | const unsigned char *tx_sec = a3; |
| 242 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 243 | tls_ctx = &qc->els[level].tls_ctx; |
| 244 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 245 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 246 | if (rx_sec) |
| 247 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 248 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 249 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 250 | if (tx_sec) |
| 251 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 252 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 256 | const enum ssl_encryption_level_t *level = a2; |
| 257 | const unsigned char *secret = a3; |
| 258 | const size_t *secret_len = a4; |
| 259 | |
| 260 | if (level) { |
| 261 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 262 | |
| 263 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 264 | if (secret && secret_len) |
| 265 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 266 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 267 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 268 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 273 | const enum ssl_encryption_level_t *level = a2; |
| 274 | const unsigned char *secret = a3; |
| 275 | const size_t *secret_len = a4; |
| 276 | |
| 277 | if (level) { |
| 278 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 279 | |
| 280 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 281 | if (secret && secret_len) |
| 282 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 283 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 284 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 285 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 290 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 291 | const struct list *l = a2; |
| 292 | |
| 293 | if (l) { |
| 294 | const struct quic_frame *frm; |
| 295 | list_for_each_entry(frm, l, list) |
| 296 | chunk_frm_appendf(&trace_buf, frm); |
| 297 | } |
| 298 | } |
| 299 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 300 | 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] | 301 | const struct quic_tx_packet *pkt = a2; |
| 302 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 303 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 304 | |
| 305 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 306 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 307 | 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] | 308 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 309 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 310 | (unsigned long long)qc->path->cwnd, |
| 311 | (unsigned long long)qc->path->prep_in_flight, |
| 312 | (unsigned long long)qc->path->in_flight, |
| 313 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 314 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 315 | } |
| 316 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 317 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 318 | if (pkt->pn_node.key != (uint64_t)-1) |
| 319 | 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] | 320 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 321 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 322 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 323 | (unsigned long long)qc->rx.bytes, |
| 324 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (room) { |
| 328 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 329 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 330 | (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] | 331 | } |
| 332 | } |
| 333 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 334 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 335 | const enum quic_handshake_state *state = a2; |
| 336 | const int *err = a3; |
| 337 | |
| 338 | if (state) |
| 339 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 340 | if (err) |
| 341 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 342 | } |
| 343 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 344 | 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] | 345 | const struct quic_rx_packet *pkt = a2; |
| 346 | const unsigned long *pktlen = a3; |
| 347 | const SSL *ssl = a4; |
| 348 | |
| 349 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 350 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 351 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 352 | chunk_appendf(&trace_buf, " kp=%d", |
| 353 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 354 | chunk_appendf(&trace_buf, " el=%c", |
| 355 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 356 | if (pkt->pnl) |
| 357 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 358 | (unsigned long long)pkt->pn); |
| 359 | if (pkt->token_len) |
| 360 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 361 | (unsigned long long)pkt->token_len); |
| 362 | if (pkt->aad_len) |
| 363 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 364 | (unsigned long long)pkt->aad_len); |
| 365 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 366 | pkt->flags, (unsigned long long)pkt->len); |
| 367 | } |
| 368 | if (pktlen) |
| 369 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 370 | if (ssl) { |
| 371 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 372 | chunk_appendf(&trace_buf, " el=%c", |
| 373 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 378 | const struct quic_rx_packet *pkt = a2; |
| 379 | const struct quic_rx_crypto_frm *cf = a3; |
| 380 | const SSL *ssl = a4; |
| 381 | |
| 382 | if (pkt) |
| 383 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 384 | quic_packet_type_enc_level_char(pkt->type), |
| 385 | (unsigned long long)pkt->pn); |
| 386 | if (cf) |
| 387 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 388 | (unsigned long long)cf->offset_node.key, |
| 389 | (unsigned long long)cf->len); |
| 390 | if (ssl) { |
| 391 | 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] | 392 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 393 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 394 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 395 | |
| 396 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 397 | 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] | 398 | } |
| 399 | |
| 400 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 401 | const struct quic_frame *frm = a2; |
| 402 | |
| 403 | if (frm) |
| 404 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 405 | } |
| 406 | |
| 407 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 408 | const struct quic_enc_level *qel = a2; |
| 409 | |
| 410 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 411 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 412 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 413 | " 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] | 414 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 415 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 416 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 417 | (unsigned long long)qc->path->cwnd, |
| 418 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 419 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 420 | (unsigned long long)pktns->tx.in_flight, |
| 421 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 425 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 426 | const struct enc_debug_info *edi = a2; |
| 427 | |
| 428 | if (edi) |
| 429 | chunk_appendf(&trace_buf, |
| 430 | " payload=@%p payload_len=%llu" |
| 431 | " aad=@%p aad_len=%llu pn=%llu", |
| 432 | edi->payload, (unsigned long long)edi->payload_len, |
| 433 | edi->aad, (unsigned long long)edi->aad_len, |
| 434 | (unsigned long long)edi->pn); |
| 435 | } |
| 436 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 437 | if (mask & QUIC_EV_CONN_RMHP) { |
| 438 | const struct quic_rx_packet *pkt = a2; |
| 439 | |
| 440 | if (pkt) { |
| 441 | const int *ret = a3; |
| 442 | |
| 443 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 444 | if (ret && *ret) |
| 445 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 446 | pkt->pnl, (unsigned long long)pkt->pn); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 451 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 452 | const unsigned long *val1 = a3; |
| 453 | const unsigned long *val2 = a4; |
| 454 | |
| 455 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 456 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 457 | if (val1) |
| 458 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 459 | if (val2) |
| 460 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 461 | } |
| 462 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 463 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 464 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 465 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 466 | |
| 467 | if (s) |
| 468 | 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] | 469 | if (stream) |
| 470 | 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] | 471 | } |
| 472 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 473 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 474 | const unsigned int *rtt_sample = a2; |
| 475 | const unsigned int *ack_delay = a3; |
| 476 | const struct quic_loss *ql = a4; |
| 477 | |
| 478 | if (rtt_sample) |
| 479 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 480 | if (ack_delay) |
| 481 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 482 | if (ql) |
| 483 | chunk_appendf(&trace_buf, |
| 484 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 485 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 486 | } |
| 487 | if (mask & QUIC_EV_CONN_CC) { |
| 488 | const struct quic_cc_event *ev = a2; |
| 489 | const struct quic_cc *cc = a3; |
| 490 | |
| 491 | if (a2) |
| 492 | quic_cc_event_trace(&trace_buf, ev); |
| 493 | if (a3) |
| 494 | quic_cc_state_trace(&trace_buf, cc); |
| 495 | } |
| 496 | |
| 497 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 498 | const struct quic_pktns *pktns = a2; |
| 499 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 500 | |
| 501 | if (pktns) { |
| 502 | chunk_appendf(&trace_buf, " pktns=%s", |
| 503 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 504 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 505 | if (pktns->tx.loss_time) |
| 506 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 507 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 508 | } |
| 509 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 510 | struct quic_tx_packet *pkt; |
| 511 | |
| 512 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 513 | list_for_each_entry(pkt, lost_pkts, list) |
| 514 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | const struct quic_pktns *pktns = a2; |
| 520 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 521 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 522 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 523 | if (ifae_pkts) |
| 524 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 525 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 526 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 527 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 528 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 529 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 530 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 531 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 532 | if (pktns->tx.in_flight) |
| 533 | 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] | 534 | if (pktns->tx.loss_time) |
| 535 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 536 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 537 | } |
| 538 | if (mask & QUIC_EV_CONN_SPTO) { |
| 539 | if (pktns->tx.time_of_last_eliciting) |
| 540 | chunk_appendf(&trace_buf, " tole=%dms", |
| 541 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 542 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 543 | 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] | 544 | } |
| 545 | } |
| 546 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 547 | 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] | 548 | chunk_appendf(&trace_buf, |
| 549 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 554 | const struct quic_tx_packet *pkt = a2; |
| 555 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 556 | chunk_appendf(&trace_buf, " err=%u cwnd=%llu ppif=%llu pif=%llu", qc->sendto_err, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 557 | (unsigned long long)qc->path->cwnd, |
| 558 | (unsigned long long)qc->path->prep_in_flight, |
| 559 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 560 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 561 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 562 | (unsigned long)pkt->pn_node.key, |
| 563 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 564 | 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] | 565 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 566 | } |
| 567 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 568 | |
| 569 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 570 | const uint8_t *alert = a2; |
| 571 | const enum ssl_encryption_level_t *level = a3; |
| 572 | |
| 573 | if (alert) |
| 574 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 575 | if (level) |
| 576 | chunk_appendf(&trace_buf, " el=%c", |
| 577 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 578 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 579 | |
| 580 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 581 | const size_t *sz1 = a2; |
| 582 | const size_t *sz2 = a3; |
| 583 | const size_t *sz3 = a4; |
| 584 | |
| 585 | if (sz1) |
| 586 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 587 | if (sz2) |
| 588 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 589 | if (sz3) |
| 590 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 591 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 592 | |
| 593 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 594 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 595 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 596 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 597 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 598 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 599 | } |
| 600 | if (mask & QUIC_EV_CONN_LPKT) { |
| 601 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 602 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 603 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 604 | if (pkt) { |
| 605 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 606 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 607 | if (pkt->pn_node.key != (uint64_t)-1) |
| 608 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 609 | } |
| 610 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 611 | if (len) |
| 612 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 615 | if (mask & QUIC_EV_STATELESS_RST) { |
| 616 | const struct quic_cid *cid = a2; |
| 617 | |
| 618 | if (cid) |
| 619 | quic_cid_dump(&trace_buf, cid); |
| 620 | } |
| 621 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* 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] | 625 | 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] | 626 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 627 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 628 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 629 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 630 | return 1; |
| 631 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 632 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 633 | 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] | 634 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 635 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 636 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 637 | return 1; |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 643 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 644 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 645 | 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] | 646 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 647 | struct quic_pktns *pktns; |
| 648 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 649 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 650 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 651 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 652 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 653 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 654 | pktns = quic_loss_pktns(qc); |
| 655 | if (tick_isset(pktns->tx.loss_time)) { |
| 656 | qc->timer = pktns->tx.loss_time; |
| 657 | goto out; |
| 658 | } |
| 659 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 660 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 661 | * cancelled for a server which reached the anti-amplification limit. |
| 662 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 663 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 664 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 665 | 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] | 666 | qc->timer = TICK_ETERNITY; |
| 667 | goto out; |
| 668 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 669 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 670 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 671 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 672 | /* Timer cancellation. */ |
| 673 | qc->timer = TICK_ETERNITY; |
| 674 | goto out; |
| 675 | } |
| 676 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 677 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 678 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 679 | if (tick_isset(pto)) |
| 680 | qc->timer = pto; |
| 681 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 682 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 683 | if (tick_is_expired(qc->timer, now_ms)) { |
| 684 | 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] | 685 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 686 | } |
| 687 | else { |
| 688 | 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] | 689 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 690 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 691 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 692 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 695 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 696 | * connection. |
| 697 | * Return 1 if succeeded, 0 if not. |
| 698 | */ |
| 699 | static int quic_tls_key_update(struct quic_conn *qc) |
| 700 | { |
| 701 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 702 | struct quic_tls_secrets *rx, *tx; |
| 703 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 704 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 705 | |
| 706 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 707 | rx = &tls_ctx->rx; |
| 708 | tx = &tls_ctx->tx; |
| 709 | nxt_rx = &qc->ku.nxt_rx; |
| 710 | nxt_tx = &qc->ku.nxt_tx; |
| 711 | |
| 712 | /* Prepare new RX secrets */ |
| 713 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 714 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 715 | 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] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 720 | nxt_rx->key, nxt_rx->keylen, |
| 721 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 722 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 723 | 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] | 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | /* Prepare new TX secrets */ |
| 728 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 729 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 730 | 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] | 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 735 | nxt_tx->key, nxt_tx->keylen, |
| 736 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 737 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 738 | 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] | 739 | return 0; |
| 740 | } |
| 741 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 742 | if (nxt_rx->ctx) { |
| 743 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 744 | nxt_rx->ctx = NULL; |
| 745 | } |
| 746 | |
| 747 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 748 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | if (nxt_tx->ctx) { |
| 753 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 754 | nxt_tx->ctx = NULL; |
| 755 | } |
| 756 | |
| 757 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 758 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 759 | return 0; |
| 760 | } |
| 761 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 762 | return 1; |
| 763 | } |
| 764 | |
| 765 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 766 | * Must be used after having updated them. |
| 767 | * Always succeeds. |
| 768 | */ |
| 769 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 770 | { |
| 771 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 772 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 773 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 774 | |
| 775 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 776 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 777 | curr_secret = tls_ctx->rx.secret; |
| 778 | curr_iv = tls_ctx->rx.iv; |
| 779 | curr_key = tls_ctx->rx.key; |
| 780 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 781 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 782 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 783 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 784 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 785 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 786 | 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] | 787 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 788 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 789 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 790 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 791 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 792 | qc->ku.prv_rx.secret = curr_secret; |
| 793 | qc->ku.prv_rx.iv = curr_iv; |
| 794 | qc->ku.prv_rx.key = curr_key; |
| 795 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 796 | |
| 797 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 798 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 799 | curr_secret = tls_ctx->tx.secret; |
| 800 | curr_iv = tls_ctx->tx.iv; |
| 801 | curr_key = tls_ctx->tx.key; |
| 802 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 803 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 804 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 805 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 806 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 807 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 808 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 809 | qc->ku.nxt_tx.secret = curr_secret; |
| 810 | qc->ku.nxt_tx.iv = curr_iv; |
| 811 | qc->ku.nxt_tx.key = curr_key; |
| 812 | } |
| 813 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 814 | #ifndef OPENSSL_IS_BORINGSSL |
| 815 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 816 | const uint8_t *read_secret, |
| 817 | const uint8_t *write_secret, size_t secret_len) |
| 818 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 819 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 820 | 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] | 821 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 822 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 823 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 824 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 825 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 826 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 827 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 828 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 829 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 830 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 831 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 832 | 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] | 833 | goto err; |
| 834 | } |
| 835 | |
| 836 | rx = &tls_ctx->rx; |
| 837 | tx = &tls_ctx->tx; |
| 838 | |
| 839 | rx->aead = tx->aead = tls_aead(cipher); |
| 840 | rx->md = tx->md = tls_md(cipher); |
| 841 | rx->hp = tx->hp = tls_hp(cipher); |
| 842 | |
| 843 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 844 | 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] | 845 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 846 | 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] | 847 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 850 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 851 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 852 | goto err; |
| 853 | } |
| 854 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 855 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 856 | * listener. Note that a listener derives only RX secrets for this |
| 857 | * level. |
| 858 | */ |
| 859 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 860 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 861 | |
| 862 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 863 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 864 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 865 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 866 | 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] | 867 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 868 | 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] | 869 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 872 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 873 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 874 | goto err; |
| 875 | } |
| 876 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 877 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 878 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 879 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 880 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 881 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 882 | /* 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] | 883 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 884 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 885 | 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] | 886 | goto err; |
| 887 | } |
| 888 | |
| 889 | memcpy(rx->secret, read_secret, secret_len); |
| 890 | rx->secretlen = secret_len; |
| 891 | memcpy(tx->secret, write_secret, secret_len); |
| 892 | tx->secretlen = secret_len; |
| 893 | /* Initialize all the secret keys lengths */ |
| 894 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 895 | /* Prepare the next key update */ |
| 896 | if (!quic_tls_key_update(qc)) |
| 897 | goto err; |
| 898 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 899 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 900 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 901 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 902 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 903 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 904 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 905 | |
| 906 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 907 | 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] | 908 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 909 | } |
| 910 | #else |
| 911 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 912 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 913 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 914 | */ |
| 915 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 916 | const SSL_CIPHER *cipher, |
| 917 | const uint8_t *secret, size_t secret_len) |
| 918 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 919 | 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] | 920 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 921 | &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] | 922 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 923 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 924 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 925 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 926 | goto out; |
| 927 | } |
| 928 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 929 | tls_ctx->rx.aead = tls_aead(cipher); |
| 930 | tls_ctx->rx.md = tls_md(cipher); |
| 931 | tls_ctx->rx.hp = tls_hp(cipher); |
| 932 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 933 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 934 | goto err; |
| 935 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 936 | 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] | 937 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 938 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 939 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 940 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 941 | 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] | 942 | goto err; |
| 943 | } |
| 944 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 945 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 946 | const unsigned char *buf; |
| 947 | size_t buflen; |
| 948 | |
| 949 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 950 | if (!buflen) |
| 951 | goto err; |
| 952 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 953 | 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] | 954 | goto err; |
| 955 | } |
| 956 | |
| 957 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 958 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 959 | 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] | 960 | |
| 961 | return 1; |
| 962 | |
| 963 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 964 | 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] | 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 969 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 970 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 971 | */ |
| 972 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 973 | const SSL_CIPHER *cipher, |
| 974 | const uint8_t *secret, size_t secret_len) |
| 975 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 976 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 977 | 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] | 978 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 979 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 980 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 981 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 982 | goto out; |
| 983 | } |
| 984 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 985 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 986 | goto err; |
| 987 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 988 | tls_ctx->tx.aead = tls_aead(cipher); |
| 989 | tls_ctx->tx.md = tls_md(cipher); |
| 990 | tls_ctx->tx.hp = tls_hp(cipher); |
| 991 | |
| 992 | 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] | 993 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 994 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 995 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 996 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 997 | 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] | 998 | goto err; |
| 999 | } |
| 1000 | |
| 1001 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1002 | 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] | 1003 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1004 | return 1; |
| 1005 | |
| 1006 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1007 | 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] | 1008 | return 0; |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
| 1012 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1013 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1014 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1015 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 1016 | * store all the data. |
| 1017 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1018 | */ |
| 1019 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 1020 | const unsigned char *data, size_t len) |
| 1021 | { |
| 1022 | struct quic_crypto_buf **qcb; |
| 1023 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1024 | size_t cf_offset, cf_len, *nb_buf; |
| 1025 | unsigned char *pos; |
| 1026 | |
| 1027 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1028 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1029 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1030 | cf_len = len; |
| 1031 | |
| 1032 | while (len) { |
| 1033 | size_t to_copy, room; |
| 1034 | |
| 1035 | pos = (*qcb)->data + (*qcb)->sz; |
| 1036 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1037 | to_copy = len > room ? room : len; |
| 1038 | if (to_copy) { |
| 1039 | memcpy(pos, data, to_copy); |
| 1040 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1041 | qel->tx.crypto.sz += to_copy; |
| 1042 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1043 | len -= to_copy; |
| 1044 | data += to_copy; |
| 1045 | } |
| 1046 | else { |
| 1047 | struct quic_crypto_buf **tmp; |
| 1048 | |
| 1049 | tmp = realloc(qel->tx.crypto.bufs, |
| 1050 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1051 | if (tmp) { |
| 1052 | qel->tx.crypto.bufs = tmp; |
| 1053 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1054 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1055 | if (!*qcb) |
| 1056 | return 0; |
| 1057 | |
| 1058 | (*qcb)->sz = 0; |
| 1059 | ++*nb_buf; |
| 1060 | } |
| 1061 | else { |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1068 | * have been buffered. |
| 1069 | */ |
| 1070 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1071 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1072 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1073 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1074 | /* There is at most one CRYPTO frame in this packet number |
| 1075 | * space. Let's look for it. |
| 1076 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1077 | 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] | 1078 | if (frm->type != QUIC_FT_CRYPTO) |
| 1079 | continue; |
| 1080 | |
| 1081 | /* Found */ |
| 1082 | found = frm; |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | if (found) { |
| 1087 | found->crypto.len += cf_len; |
| 1088 | } |
| 1089 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1090 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1091 | if (!frm) |
| 1092 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1094 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1095 | frm->type = QUIC_FT_CRYPTO; |
| 1096 | frm->crypto.offset = cf_offset; |
| 1097 | frm->crypto.len = cf_len; |
| 1098 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1099 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1100 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | return len == 0; |
| 1104 | } |
| 1105 | |
| 1106 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1107 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1108 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1109 | { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1110 | qc->err_code = QC_ERR_CRYPTO_ERROR | alert; |
| 1111 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1112 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1115 | /* Set the application for <qc> QUIC connection. |
| 1116 | * Return 1 if succeeded, 0 if not. |
| 1117 | */ |
| 1118 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1119 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1120 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1121 | qc->app_ops = &h3_ops; |
| 1122 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1123 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1124 | else |
| 1125 | return 0; |
| 1126 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1127 | return 1; |
| 1128 | } |
| 1129 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1130 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1131 | * wants to provide the QUIC layer with CRYPTO data. |
| 1132 | * Returns 1 if succeeded, 0 if not. |
| 1133 | */ |
| 1134 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1135 | const uint8_t *data, size_t len) |
| 1136 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1137 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1138 | enum quic_tls_enc_level tel; |
| 1139 | struct quic_enc_level *qel; |
| 1140 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1141 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1142 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1143 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1144 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1145 | goto out; |
| 1146 | } |
| 1147 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1148 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1149 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1150 | 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] | 1151 | goto err; |
| 1152 | } |
| 1153 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1154 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1155 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1156 | 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] | 1157 | goto err; |
| 1158 | } |
| 1159 | |
| 1160 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1161 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1162 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1163 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1164 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1165 | return 1; |
| 1166 | |
| 1167 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1168 | 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] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | int ha_quic_flush_flight(SSL *ssl) |
| 1173 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1174 | 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] | 1175 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1176 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1177 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1178 | |
| 1179 | return 1; |
| 1180 | } |
| 1181 | |
| 1182 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1183 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1184 | 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] | 1185 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1186 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1187 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1188 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1189 | return 1; |
| 1190 | } |
| 1191 | |
| 1192 | /* QUIC TLS methods */ |
| 1193 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1194 | #ifdef OPENSSL_IS_BORINGSSL |
| 1195 | .set_read_secret = ha_set_rsec, |
| 1196 | .set_write_secret = ha_set_wsec, |
| 1197 | #else |
| 1198 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1199 | #endif |
| 1200 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1201 | .flush_flight = ha_quic_flush_flight, |
| 1202 | .send_alert = ha_quic_send_alert, |
| 1203 | }; |
| 1204 | |
| 1205 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1206 | * Returns an error count. |
| 1207 | */ |
| 1208 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1209 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1210 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1211 | int cfgerr = 0; |
| 1212 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1213 | long options = |
| 1214 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1215 | SSL_OP_SINGLE_ECDH_USE | |
| 1216 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1217 | SSL_CTX *ctx; |
| 1218 | |
| 1219 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1220 | bind_conf->initial_ctx = ctx; |
| 1221 | |
| 1222 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1223 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1224 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1225 | 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] | 1226 | |
| 1227 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1228 | #ifdef OPENSSL_IS_BORINGSSL |
| 1229 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1230 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1231 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1232 | if (bind_conf->ssl_conf.early_data) { |
| 1233 | 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] | 1234 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1235 | } |
| 1236 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1237 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1238 | #else |
| 1239 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1240 | #endif |
| 1241 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1242 | #endif |
| 1243 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1244 | |
| 1245 | return cfgerr; |
| 1246 | } |
| 1247 | |
| 1248 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1249 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1250 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1251 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1252 | */ |
| 1253 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1254 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1255 | { |
| 1256 | uint64_t expected_pn = largest_pn + 1; |
| 1257 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1258 | uint64_t pn_hwin = pn_win / 2; |
| 1259 | uint64_t pn_mask = pn_win - 1; |
| 1260 | uint64_t candidate_pn; |
| 1261 | |
| 1262 | |
| 1263 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1264 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1265 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1266 | candidate_pn + pn_hwin <= expected_pn) |
| 1267 | return candidate_pn + pn_win; |
| 1268 | |
| 1269 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1270 | return candidate_pn - pn_win; |
| 1271 | |
| 1272 | return candidate_pn; |
| 1273 | } |
| 1274 | |
| 1275 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1276 | * cryptographic context. |
| 1277 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1278 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1279 | * <end> points to one byte past the end of this packet. |
| 1280 | * Returns 1 if succeeded, 0 if not. |
| 1281 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1282 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1283 | 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] | 1284 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1285 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1286 | { |
| 1287 | int ret, outlen, i, pnlen; |
| 1288 | uint64_t packet_number; |
| 1289 | uint32_t truncated_pn = 0; |
| 1290 | unsigned char mask[5] = {0}; |
| 1291 | unsigned char *sample; |
| 1292 | EVP_CIPHER_CTX *cctx; |
| 1293 | unsigned char *hp_key; |
| 1294 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1295 | /* Check there is enough data in this packet. */ |
| 1296 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1297 | 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] | 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | cctx = EVP_CIPHER_CTX_new(); |
| 1302 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1303 | 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] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | ret = 0; |
| 1308 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1309 | |
| 1310 | hp_key = tls_ctx->rx.hp_key; |
| 1311 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1312 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1313 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1314 | 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] | 1315 | goto out; |
| 1316 | } |
| 1317 | |
| 1318 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1319 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1320 | for (i = 0; i < pnlen; i++) { |
| 1321 | pn[i] ^= mask[i + 1]; |
| 1322 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1323 | } |
| 1324 | |
| 1325 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1326 | /* Store remaining information for this unprotected header */ |
| 1327 | pkt->pn = packet_number; |
| 1328 | pkt->pnl = pnlen; |
| 1329 | |
| 1330 | ret = 1; |
| 1331 | |
| 1332 | out: |
| 1333 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1334 | |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
| 1338 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1339 | * address, with <payload_len> as payload length, <aad> as address of |
| 1340 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1341 | * context. |
| 1342 | * Returns 1 if succeeded, 0 if not. |
| 1343 | */ |
| 1344 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1345 | 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] | 1346 | 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] | 1347 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1348 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1349 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1350 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1351 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1352 | |
| 1353 | 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] | 1354 | 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] | 1355 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | 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] | 1359 | 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] | 1360 | 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] | 1361 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1365 | |
| 1366 | err: |
| 1367 | 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] | 1368 | 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] | 1369 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1373 | * Returns 1 if succeeded, 0 if not. |
| 1374 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1375 | 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] | 1376 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1377 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1378 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1379 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1380 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1381 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1382 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1383 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1384 | |
| 1385 | kp_changed = 0; |
| 1386 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1387 | /* The two tested bits are not at the same position, |
| 1388 | * this is why they are first both inversed. |
| 1389 | */ |
| 1390 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1391 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1392 | /* The lowest packet number of a previous key phase |
| 1393 | * cannot be null if it really stores previous key phase |
| 1394 | * secrets. |
| 1395 | */ |
| 1396 | if (!pkt->qc->ku.prv_rx.pn) |
| 1397 | return 0; |
| 1398 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1399 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1400 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1401 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1402 | } |
| 1403 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1404 | /* Next key phase */ |
| 1405 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1406 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1407 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1408 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1409 | } |
| 1410 | } |
| 1411 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1412 | |
| 1413 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1414 | return 0; |
| 1415 | |
| 1416 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1417 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1418 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1419 | if (!ret) |
| 1420 | return 0; |
| 1421 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1422 | /* Update the keys only if the packet decryption succeeded. */ |
| 1423 | if (kp_changed) { |
| 1424 | quic_tls_rotate_keys(pkt->qc); |
| 1425 | /* Toggle the Key Phase bit */ |
| 1426 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1427 | /* Store the lowest packet number received for the current key phase */ |
| 1428 | tls_ctx->rx.pn = pkt->pn; |
| 1429 | /* Prepare the next key update */ |
| 1430 | if (!quic_tls_key_update(pkt->qc)) |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1434 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1435 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1436 | |
| 1437 | return 1; |
| 1438 | } |
| 1439 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1440 | |
| 1441 | /* Remove references to <frm> frame */ |
| 1442 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1443 | { |
| 1444 | uint64_t pn; |
| 1445 | struct quic_frame *f, *tmp; |
| 1446 | |
| 1447 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1448 | pn = f->pkt->pn_node.key; |
| 1449 | f->origin = NULL; |
| 1450 | LIST_DELETE(&f->ref); |
| 1451 | TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1455 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1456 | 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] | 1457 | { |
| 1458 | uint64_t pn; |
| 1459 | struct quic_frame *origin, *f, *tmp; |
| 1460 | |
| 1461 | /* Identify this frame: a frame copy or one of its copies */ |
| 1462 | origin = frm->origin ? frm->origin : frm; |
| 1463 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1464 | * possibly a copy of <origin> |
| 1465 | */ |
| 1466 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1467 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1468 | * not release the packets (releasing the frames) at this time as |
| 1469 | * they are possibly also to be acknowledged alongside the |
| 1470 | * the current one. |
| 1471 | */ |
| 1472 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1473 | pn = f->pkt->pn_node.key; |
| 1474 | TRACE_PROTO("mark frame as acked from packet", |
| 1475 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1476 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1477 | f->origin = NULL; |
| 1478 | LIST_DELETE(&f->ref); |
| 1479 | } |
| 1480 | LIST_DELETE(&frm->list); |
| 1481 | pn = frm->pkt->pn_node.key; |
| 1482 | quic_tx_packet_refdec(frm->pkt); |
| 1483 | TRACE_PROTO("freeing frame from packet", |
| 1484 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1485 | pool_free(pool_head_quic_frame, frm); |
| 1486 | } |
| 1487 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1488 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1489 | * |
| 1490 | * 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] | 1491 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1492 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1493 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1494 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1495 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1496 | struct eb64_node *frm_node; |
| 1497 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1498 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1499 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1500 | while (frm_node) { |
| 1501 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1502 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1503 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1504 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1505 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1506 | offset = strm->offset.key; |
| 1507 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1508 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1509 | if (offset > stream->ack_offset) |
| 1510 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1511 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1512 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1513 | /* cf. next comment : frame may be freed at this stage. */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1514 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1515 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1516 | ret = 1; |
| 1517 | } |
| 1518 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1519 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1520 | * has been freed. with the stream frames tree. Nothing to do |
| 1521 | * anymore in here. |
| 1522 | */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1523 | if (!stream) |
| 1524 | return 1; |
| 1525 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1526 | frm_node = eb64_next(frm_node); |
| 1527 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1528 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1529 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1530 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1531 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1532 | |
| 1533 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1536 | /* 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] | 1537 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1538 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1539 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1540 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1541 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1542 | 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] | 1543 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1544 | switch (frm->type) { |
| 1545 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1546 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1547 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1548 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1549 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1550 | const size_t offset = strm_frm->offset.key; |
| 1551 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1552 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1553 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1554 | * 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] | 1555 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1556 | * |
| 1557 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1558 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1559 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1560 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1561 | if (!node) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1562 | 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] | 1563 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1564 | /* early return */ |
| 1565 | return; |
| 1566 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1567 | 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] | 1568 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1569 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1570 | if (offset <= stream->ack_offset) { |
| 1571 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1572 | stream_acked = 1; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1573 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1574 | qc, strm_frm, stream); |
| 1575 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1576 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1577 | if (!stream) { |
| 1578 | /* no need to continue if stream freed. */ |
| 1579 | 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] | 1580 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1581 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1582 | } |
| 1583 | |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1584 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1585 | qc, strm_frm, stream); |
| 1586 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1587 | } |
| 1588 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1589 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1590 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1591 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1592 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1593 | } |
| 1594 | break; |
| 1595 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1596 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1597 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1598 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1599 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1600 | struct qcc *qcc = qc->qcc; |
| 1601 | |
| 1602 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1603 | tasklet_wakeup(qcc->subs->tasklet); |
| 1604 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1605 | if (!qcc->subs->events) |
| 1606 | qcc->subs = NULL; |
| 1607 | } |
| 1608 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1612 | * deallocating them, and their TX frames. |
| 1613 | * Returns the last node reached to be used for the next range. |
| 1614 | * May be NULL if <largest> node could not be found. |
| 1615 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1616 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1617 | struct eb_root *pkts, |
| 1618 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1619 | struct list *newly_acked_pkts, |
| 1620 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1621 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1622 | { |
| 1623 | struct eb64_node *node; |
| 1624 | struct quic_tx_packet *pkt; |
| 1625 | |
| 1626 | if (largest_node) |
| 1627 | node = largest_node; |
| 1628 | else { |
| 1629 | node = eb64_lookup(pkts, largest); |
| 1630 | while (!node && largest > smallest) { |
| 1631 | node = eb64_lookup(pkts, --largest); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1636 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1637 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1638 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1639 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1640 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1641 | 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] | 1642 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1643 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1644 | node = eb64_prev(node); |
| 1645 | eb64_delete(&pkt->pn_node); |
| 1646 | } |
| 1647 | |
| 1648 | return node; |
| 1649 | } |
| 1650 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1651 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1652 | * 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] | 1653 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1654 | 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] | 1655 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1656 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1657 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1658 | struct quic_frame *frm, *frmbak; |
| 1659 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1660 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1661 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1662 | 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] | 1663 | /* Only for debug */ |
| 1664 | uint64_t pn; |
| 1665 | |
| 1666 | /* 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] | 1667 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1668 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1669 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1670 | /* 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] | 1671 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1672 | /* Remove any reference to this frame */ |
| 1673 | qc_frm_unref(qc, frm); |
| 1674 | switch (frm->type) { |
| 1675 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1676 | { |
| 1677 | struct quic_stream *strm_frm = &frm->stream; |
| 1678 | struct eb64_node *node = NULL; |
| 1679 | struct qc_stream_desc *stream_desc; |
| 1680 | |
| 1681 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1682 | if (!node) { |
| 1683 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 1684 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1685 | qc, frm, &pn); |
| 1686 | pool_free(pool_head_quic_frame, frm); |
| 1687 | continue; |
| 1688 | } |
| 1689 | |
| 1690 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1691 | /* Do not resend this frame if in the "already acked range" */ |
| 1692 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1693 | TRACE_PROTO("ignored frame in already acked range", |
| 1694 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1695 | continue; |
| 1696 | } |
| 1697 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1698 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1699 | TRACE_PROTO("updated partially acked frame", |
| 1700 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1701 | } |
| 1702 | |
| 1703 | break; |
| 1704 | } |
| 1705 | |
| 1706 | default: |
| 1707 | break; |
| 1708 | } |
| 1709 | |
| 1710 | /* Do not resend probing packet with old data */ |
| 1711 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1712 | TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1713 | qc, frm, &pn); |
| 1714 | if (frm->origin) |
| 1715 | LIST_DELETE(&frm->ref); |
| 1716 | pool_free(pool_head_quic_frame, frm); |
| 1717 | continue; |
| 1718 | } |
| 1719 | |
| 1720 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1721 | TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1722 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1723 | qc, frm, &pn); |
| 1724 | pool_free(pool_head_quic_frame, frm); |
| 1725 | } |
| 1726 | else { |
| 1727 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1728 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1729 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1730 | * will be performed to check the stream is not consumed or released. |
| 1731 | */ |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1732 | frm->flags = QUIC_FL_TX_FRAME_LOST; |
| 1733 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1734 | LIST_APPEND(&tmp, &frm->list); |
| 1735 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1736 | } |
| 1737 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1738 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1739 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1740 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1741 | /* Free <pkt> TX packet and its attached frames. |
| 1742 | * This is the responsability of the caller to remove this packet of |
| 1743 | * any data structure it was possibly attached to. |
| 1744 | */ |
| 1745 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1746 | { |
| 1747 | struct quic_frame *frm, *frmbak; |
| 1748 | |
| 1749 | if (!pkt) |
| 1750 | return; |
| 1751 | |
| 1752 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1753 | LIST_DELETE(&frm->list); |
| 1754 | pool_free(pool_head_quic_frame, frm); |
| 1755 | } |
| 1756 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1757 | } |
| 1758 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1759 | /* Free the TX packets of <pkts> list */ |
| 1760 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1761 | { |
| 1762 | struct quic_tx_packet *pkt, *tmp; |
| 1763 | |
| 1764 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1765 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1766 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1767 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1771 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1772 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1773 | * updating the range which contains <largest_acked_pn>. |
| 1774 | * Never fails. |
| 1775 | */ |
| 1776 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1777 | int64_t largest_acked_pn) |
| 1778 | { |
| 1779 | struct eb64_node *ar, *next_ar; |
| 1780 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1781 | |
| 1782 | ar = eb64_first(&arngs->root); |
| 1783 | while (ar) { |
| 1784 | struct quic_arng_node *ar_node; |
| 1785 | |
| 1786 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1787 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1788 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1789 | break; |
| 1790 | |
| 1791 | if (largest_acked_pn < ar_node->last) { |
| 1792 | eb64_delete(ar); |
| 1793 | ar_node->first.key = largest_acked_pn + 1; |
| 1794 | eb64_insert(&arngs->root, ar); |
| 1795 | break; |
| 1796 | } |
| 1797 | |
| 1798 | eb64_delete(ar); |
| 1799 | pool_free(pool_head_quic_arng, ar_node); |
| 1800 | arngs->sz--; |
| 1801 | ar = next_ar; |
| 1802 | } |
| 1803 | } |
| 1804 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1805 | /* Send a packet ack event nofication for each newly acked packet of |
| 1806 | * <newly_acked_pkts> list and free them. |
| 1807 | * Always succeeds. |
| 1808 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1809 | 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] | 1810 | struct list *newly_acked_pkts) |
| 1811 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1812 | struct quic_tx_packet *pkt, *tmp; |
| 1813 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1814 | |
| 1815 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1816 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1817 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1818 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1819 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1820 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1821 | /* If this packet contained an ACK frame, proceed to the |
| 1822 | * acknowledging of range of acks from the largest acknowledged |
| 1823 | * packet number which was sent in an ACK frame by this packet. |
| 1824 | */ |
| 1825 | if (pkt->largest_acked_pn != -1) |
| 1826 | 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] | 1827 | ev.ack.acked = pkt->in_flight_len; |
| 1828 | ev.ack.time_sent = pkt->time_sent; |
| 1829 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1830 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1831 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1832 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | } |
| 1836 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1837 | /* Release all the frames attached to <pktns> packet number space */ |
| 1838 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1839 | { |
| 1840 | struct quic_frame *frm, *frmbak; |
| 1841 | |
| 1842 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1843 | LIST_DELETE(&frm->list); |
| 1844 | pool_free(pool_head_quic_frame, frm); |
| 1845 | } |
| 1846 | } |
| 1847 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1848 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1849 | * their TX frames. |
| 1850 | * Send a packet loss event to the congestion controller if |
| 1851 | * in flight packet have been lost. |
| 1852 | * Also frees the packet in <pkts> list. |
| 1853 | * Never fails. |
| 1854 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1855 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1856 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1857 | struct list *pkts, |
| 1858 | uint64_t now_us) |
| 1859 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1860 | 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] | 1861 | uint64_t lost_bytes; |
| 1862 | |
| 1863 | lost_bytes = 0; |
| 1864 | oldest_lost = newest_lost = NULL; |
| 1865 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1866 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1867 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1868 | lost_bytes += pkt->in_flight_len; |
| 1869 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1870 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1871 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1872 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1873 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1874 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1875 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1876 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1877 | if (!oldest_lost) { |
| 1878 | oldest_lost = newest_lost = pkt; |
| 1879 | } |
| 1880 | else { |
| 1881 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1882 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1883 | newest_lost = pkt; |
| 1884 | } |
| 1885 | } |
| 1886 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1887 | if (newest_lost) { |
| 1888 | /* Sent a congestion event to the controller */ |
| 1889 | struct quic_cc_event ev = { |
| 1890 | .type = QUIC_CC_EVT_LOSS, |
| 1891 | .loss.time_sent = newest_lost->time_sent, |
| 1892 | }; |
| 1893 | |
| 1894 | quic_cc_event(&qc->path->cc, &ev); |
| 1895 | } |
| 1896 | |
| 1897 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1898 | * We must check if we are experiencing a persistent congestion. |
| 1899 | * If this is the case, the congestion controller must re-enter |
| 1900 | * slow start state. |
| 1901 | */ |
| 1902 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1903 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1904 | |
| 1905 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1906 | now_ms, qc->max_ack_delay)) |
| 1907 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1908 | } |
| 1909 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1910 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1911 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1912 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1913 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1917 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1918 | * 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] | 1919 | * 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] | 1920 | * acked ack-eliciting packet. |
| 1921 | * Return 1, if succeeded, 0 if not. |
| 1922 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1923 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1924 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1925 | struct quic_enc_level *qel, |
| 1926 | unsigned int *rtt_sample, |
| 1927 | const unsigned char **pos, const unsigned char *end) |
| 1928 | { |
| 1929 | struct quic_ack *ack = &frm->ack; |
| 1930 | uint64_t smallest, largest; |
| 1931 | struct eb_root *pkts; |
| 1932 | struct eb64_node *largest_node; |
| 1933 | unsigned int time_sent, pkt_flags; |
| 1934 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1935 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1936 | |
| 1937 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1938 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1939 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1940 | goto err; |
| 1941 | } |
| 1942 | |
| 1943 | if (ack->first_ack_range > ack->largest_ack) { |
| 1944 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1945 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1946 | goto err; |
| 1947 | } |
| 1948 | |
| 1949 | largest = ack->largest_ack; |
| 1950 | smallest = largest - ack->first_ack_range; |
| 1951 | pkts = &qel->pktns->tx.pkts; |
| 1952 | pkt_flags = 0; |
| 1953 | largest_node = NULL; |
| 1954 | time_sent = 0; |
| 1955 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1956 | 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] | 1957 | largest_node = eb64_lookup(pkts, largest); |
| 1958 | if (!largest_node) { |
| 1959 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1960 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1961 | } |
| 1962 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1963 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1964 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1965 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1969 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1970 | do { |
| 1971 | uint64_t gap, ack_range; |
| 1972 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1973 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1974 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1975 | if (!ack->ack_range_num--) |
| 1976 | break; |
| 1977 | |
| 1978 | if (!quic_dec_int(&gap, pos, end)) |
| 1979 | goto err; |
| 1980 | |
| 1981 | if (smallest < gap + 2) { |
| 1982 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1983 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1984 | goto err; |
| 1985 | } |
| 1986 | |
| 1987 | largest = smallest - gap - 2; |
| 1988 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1989 | goto err; |
| 1990 | |
| 1991 | if (largest < ack_range) { |
| 1992 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1993 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1994 | goto err; |
| 1995 | } |
| 1996 | |
| 1997 | /* Do not use this node anymore. */ |
| 1998 | largest_node = NULL; |
| 1999 | /* Next range */ |
| 2000 | smallest = largest - ack_range; |
| 2001 | |
| 2002 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2003 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2004 | } while (1); |
| 2005 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2006 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2007 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2008 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2012 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2013 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2014 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2015 | 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] | 2016 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2017 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2018 | if (quic_peer_validated_addr(qc)) |
| 2019 | qc->path->loss.pto_count = 0; |
| 2020 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | |
| 2024 | return 1; |
| 2025 | |
| 2026 | err: |
| 2027 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2028 | 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] | 2029 | return 0; |
| 2030 | } |
| 2031 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2032 | /* This function gives the detail of the SSL error. It is used only |
| 2033 | * if the debug mode and the verbose mode are activated. It dump all |
| 2034 | * the SSL error until the stack was empty. |
| 2035 | */ |
| 2036 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2037 | { |
| 2038 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2039 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2040 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2041 | unsigned long ret; |
| 2042 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2043 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2044 | ret = ERR_get_error(); |
| 2045 | if (!ret) |
| 2046 | return; |
| 2047 | |
| 2048 | 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] | 2049 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2054 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2055 | const char **str, int *len); |
| 2056 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2057 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2058 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2059 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2060 | * Return 1 if succeeded, 0 if not. |
| 2061 | */ |
| 2062 | 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] | 2063 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2064 | const unsigned char *data, size_t len, |
| 2065 | struct quic_rx_packet *pkt, |
| 2066 | struct quic_rx_crypto_frm *cf) |
| 2067 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2068 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2069 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2070 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2071 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2072 | qc = ctx->qc; |
| 2073 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2074 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2075 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2076 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2077 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2078 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2079 | goto err; |
| 2080 | } |
| 2081 | |
| 2082 | el->rx.crypto.offset += len; |
| 2083 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2084 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2085 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2086 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2087 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2088 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2089 | if (ssl_err != 1) { |
| 2090 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2091 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2092 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2093 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2094 | goto out; |
| 2095 | } |
| 2096 | |
| 2097 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2098 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2099 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2100 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | goto err; |
| 2102 | } |
| 2103 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2104 | 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] | 2105 | |
| 2106 | /* Check the alpn could be negotiated */ |
| 2107 | if (!qc->app_ops) { |
| 2108 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2109 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2110 | goto err; |
| 2111 | } |
| 2112 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2113 | /* I/O callback switch */ |
| 2114 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2115 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2116 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2117 | /* The connection is ready to be accepted. */ |
| 2118 | quic_accept_push_qc(qc); |
| 2119 | } |
| 2120 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2121 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2122 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2123 | } else { |
| 2124 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2125 | if (ssl_err != 1) { |
| 2126 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2127 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2128 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2129 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2130 | goto out; |
| 2131 | } |
| 2132 | |
| 2133 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2134 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2135 | goto err; |
| 2136 | } |
| 2137 | |
| 2138 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2139 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2140 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2141 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2142 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2143 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2144 | return 1; |
| 2145 | |
| 2146 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2147 | 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] | 2148 | return 0; |
| 2149 | } |
| 2150 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2151 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2152 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2153 | * If not, the STREAM frame is stored to be treated again later. |
| 2154 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2155 | * Return 1 if succeeded, 0 if not. |
| 2156 | */ |
| 2157 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2158 | struct quic_stream *strm_frm, |
| 2159 | struct quic_conn *qc) |
| 2160 | { |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2161 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2162 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2163 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2164 | strm_frm->offset.key, strm_frm->fin, |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 2165 | (char *)strm_frm->data); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2166 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 2167 | /* frame rejected - packet must not be acknowledeged */ |
| 2168 | if (ret) |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2169 | return 0; |
| 2170 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2171 | return 1; |
| 2172 | } |
| 2173 | |
| 2174 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2175 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2176 | * If not, the STREAM frame is stored to be treated again later. |
| 2177 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2178 | * Return 1 if succeeded, 0 if not. |
| 2179 | */ |
| 2180 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2181 | struct quic_stream *strm_frm, |
| 2182 | struct quic_conn *qc) |
| 2183 | { |
| 2184 | struct qcs *strm; |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2185 | enum ncb_ret ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2186 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2187 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2188 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2189 | 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] | 2190 | return 0; |
| 2191 | } |
| 2192 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2193 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2194 | size_t diff; |
| 2195 | |
| 2196 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2197 | TRACE_PROTO("Already received STREAM data", |
| 2198 | QUIC_EV_CONN_PSTRM, qc); |
| 2199 | goto out; |
| 2200 | } |
| 2201 | |
| 2202 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2203 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2204 | strm_frm->offset.key = strm->rx.offset; |
| 2205 | strm_frm->len -= diff; |
| 2206 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2207 | } |
| 2208 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2209 | qc_get_ncbuf(strm, &strm->rx.ncbuf); |
| 2210 | if (ncb_is_null(&strm->rx.ncbuf)) |
| 2211 | return 0; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2212 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2213 | ret = ncb_add(&strm->rx.ncbuf, strm_frm->offset.key - strm->rx.offset, |
| 2214 | (char *)strm_frm->data, strm_frm->len, NCB_ADD_COMPARE); |
| 2215 | if (ret != NCB_RET_OK) |
| 2216 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2217 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2218 | /* Inform the application of the arrival of this new stream */ |
| 2219 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
| 2220 | 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] | 2221 | return 0; |
| 2222 | } |
| 2223 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 2224 | qcs_notify_recv(strm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2225 | |
| 2226 | out: |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2230 | /* Returns 1 on success or 0 on error. On error, the packet containing the |
| 2231 | * frame must not be acknowledged. |
| 2232 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2233 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2234 | struct quic_stream *strm_frm, |
| 2235 | struct quic_conn *qc) |
| 2236 | { |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2237 | /* RFC9000 13.1. Packet Processing |
| 2238 | * |
| 2239 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2240 | * successfully removed and all frames contained in the packet have |
| 2241 | * been processed. For STREAM frames, this means the data has been |
| 2242 | * enqueued in preparation to be received by the application protocol, |
| 2243 | * but it does not require that data be delivered and consumed. |
| 2244 | */ |
| 2245 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2246 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2247 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2248 | else |
| 2249 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2250 | } |
| 2251 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2252 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2253 | * for <qc> QUIC connection. |
| 2254 | * This is a best effort function which never fails even if no memory could be |
| 2255 | * allocated to duplicate these frames. |
| 2256 | */ |
| 2257 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2258 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2259 | { |
| 2260 | struct quic_frame *frm, *frmbak; |
| 2261 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2262 | |
| 2263 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2264 | struct quic_frame *dup_frm, *origin; |
| 2265 | |
| 2266 | switch (frm->type) { |
| 2267 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2268 | { |
| 2269 | struct quic_stream *strm_frm = &frm->stream; |
| 2270 | struct eb64_node *node = NULL; |
| 2271 | struct qc_stream_desc *stream_desc; |
| 2272 | |
| 2273 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2274 | if (!node) { |
| 2275 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm); |
| 2276 | continue; |
| 2277 | } |
| 2278 | |
| 2279 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2280 | /* Do not resend this frame if in the "already acked range" */ |
| 2281 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2282 | TRACE_PROTO("ignored frame frame in already acked range", |
| 2283 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2284 | continue; |
| 2285 | } |
| 2286 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2287 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2288 | TRACE_PROTO("updated partially acked frame", |
| 2289 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2290 | } |
| 2291 | |
| 2292 | break; |
| 2293 | } |
| 2294 | |
| 2295 | default: |
| 2296 | break; |
| 2297 | } |
| 2298 | |
| 2299 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2300 | if (!dup_frm) { |
| 2301 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | /* If <frm> is already a copy of another frame, we must take |
| 2306 | * its original frame as source for the copy. |
| 2307 | */ |
| 2308 | origin = frm->origin ? frm->origin : frm; |
| 2309 | TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2310 | *dup_frm = *origin; |
| 2311 | LIST_INIT(&dup_frm->reflist); |
| 2312 | TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM, |
| 2313 | qc, NULL, &origin->pkt->pn_node.key); |
| 2314 | dup_frm->origin = origin; |
| 2315 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2316 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2317 | } |
| 2318 | |
| 2319 | LIST_SPLICE(out_frm_list, &tmp); |
| 2320 | } |
| 2321 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2322 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2323 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2324 | struct quic_enc_level *qel, |
| 2325 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2326 | { |
| 2327 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2328 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2329 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2330 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2331 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2332 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2333 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2334 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2335 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2336 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2337 | node = eb64_next(node); |
| 2338 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2339 | 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] | 2340 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2344 | return; |
| 2345 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2346 | /* When building a packet from another one, the field which may increase the |
| 2347 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2348 | */ |
| 2349 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2350 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2351 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2352 | return; |
| 2353 | } |
| 2354 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2355 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2356 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2357 | if (frms == frms1 && frms2) { |
| 2358 | frms = frms2; |
| 2359 | goto start; |
| 2360 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2363 | /* 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] | 2364 | * 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] | 2365 | * Initial packets send them coalescing with others (Handshake here). |
| 2366 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2367 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2368 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2369 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2370 | { |
| 2371 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2372 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2373 | |
| 2374 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2375 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2376 | struct quic_enc_level *qel = iqel; |
| 2377 | struct eb_root *pkts; |
| 2378 | struct eb64_node *node; |
| 2379 | struct quic_tx_packet *pkt; |
| 2380 | struct list *tmp = &itmp; |
| 2381 | |
| 2382 | start: |
| 2383 | pkt = NULL; |
| 2384 | pkts = &qel->pktns->tx.pkts; |
| 2385 | node = eb64_first(pkts); |
| 2386 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2387 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2388 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2389 | 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] | 2390 | break; |
| 2391 | node = eb64_next(node); |
| 2392 | } |
| 2393 | |
| 2394 | if (!pkt) |
| 2395 | goto end; |
| 2396 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2397 | /* When building a packet from another one, the field which may increase the |
| 2398 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2399 | */ |
| 2400 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2401 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2402 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2403 | goto end; |
| 2404 | } |
| 2405 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2406 | qel->pktns->tx.pto_probe += 1; |
| 2407 | requeue: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2408 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2409 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2410 | if (qel == iqel) { |
| 2411 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2412 | pkt = pkt->next; |
| 2413 | tmp = &htmp; |
| 2414 | hqel->pktns->tx.pto_probe += 1; |
| 2415 | goto requeue; |
| 2416 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2417 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2418 | |
| 2419 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2420 | LIST_SPLICE(ifrms, &itmp); |
| 2421 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2424 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2425 | * as I/O handler context and <qel> as encryption level. |
| 2426 | * Returns 1 if succeeded, 0 if failed. |
| 2427 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2428 | 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] | 2429 | struct quic_enc_level *qel) |
| 2430 | { |
| 2431 | struct quic_frame frm; |
| 2432 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2433 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2434 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2435 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2436 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2437 | /* Skip the AAD */ |
| 2438 | pos = pkt->data + pkt->aad_len; |
| 2439 | end = pkt->data + pkt->len; |
| 2440 | |
| 2441 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2442 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2443 | goto err; |
| 2444 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2445 | 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] | 2446 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2447 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2448 | break; |
| 2449 | case QUIC_FT_PING: |
| 2450 | break; |
| 2451 | case QUIC_FT_ACK: |
| 2452 | { |
| 2453 | unsigned int rtt_sample; |
| 2454 | |
| 2455 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2456 | 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] | 2457 | goto err; |
| 2458 | |
| 2459 | if (rtt_sample) { |
| 2460 | unsigned int ack_delay; |
| 2461 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2462 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2463 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2464 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2465 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2466 | 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] | 2467 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2468 | break; |
| 2469 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2470 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2471 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2472 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2473 | { |
| 2474 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2475 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2476 | 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] | 2477 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2478 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2479 | |
| 2480 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2481 | cfdebug.len = frm.crypto.len; |
| 2482 | TRACE_PROTO("CRYPTO data discarded", |
| 2483 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2484 | break; |
| 2485 | } |
| 2486 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2487 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2488 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2489 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2490 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2491 | |
| 2492 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2493 | cfdebug.len = frm.crypto.len; |
| 2494 | /* Nothing to do */ |
| 2495 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2496 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2497 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2498 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2499 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2500 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2501 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2502 | else { |
| 2503 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2504 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2505 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2506 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2507 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2508 | cfdebug.len = frm.crypto.len; |
| 2509 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2510 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2511 | frm.crypto.len -= diff; |
| 2512 | frm.crypto.data += diff; |
| 2513 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2514 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2515 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2516 | |
| 2517 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2518 | /* 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] | 2519 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2520 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2521 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2522 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2523 | if (!qc_provide_cdata(qel, ctx, |
| 2524 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2525 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2526 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2527 | |
| 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2532 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2533 | if (!cf) { |
| 2534 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2535 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2536 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2537 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2538 | |
| 2539 | cf->offset_node.key = frm.crypto.offset; |
| 2540 | cf->len = frm.crypto.len; |
| 2541 | cf->data = frm.crypto.data; |
| 2542 | cf->pkt = pkt; |
| 2543 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2544 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2545 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2546 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2547 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2548 | { |
| 2549 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2550 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2551 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2552 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2553 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2554 | goto err; |
| 2555 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2556 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2557 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2558 | /* The upper layer may not be allocated. */ |
| 2559 | if (qc->mux_state != QC_MUX_READY) { |
| 2560 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2561 | TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2562 | break; |
| 2563 | } |
| 2564 | else { |
| 2565 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc); |
| 2566 | goto err; |
| 2567 | } |
| 2568 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2569 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2570 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2571 | goto err; |
| 2572 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2573 | break; |
| 2574 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2575 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2576 | if (qc->mux_state == QC_MUX_READY) { |
| 2577 | struct quic_max_data *data = &frm.max_data; |
| 2578 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2579 | } |
| 2580 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2581 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2582 | if (qc->mux_state == QC_MUX_READY) { |
| 2583 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2584 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2585 | data->max_stream_data); |
| 2586 | } |
| 2587 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2588 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2589 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2590 | case QUIC_FT_DATA_BLOCKED: |
| 2591 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2592 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2593 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2594 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2595 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2596 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2597 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2598 | break; |
| 2599 | case QUIC_FT_CONNECTION_CLOSE: |
| 2600 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2601 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2602 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2603 | /* RFC 9000 10.2. Immediate Close: |
| 2604 | * The closing and draining connection states exist to ensure |
| 2605 | * that connections close cleanly and that delayed or reordered |
| 2606 | * packets are properly discarded. These states SHOULD persist |
| 2607 | * for at least three times the current PTO interval... |
| 2608 | * |
| 2609 | * Rearm the idle timeout only one time when entering draining |
| 2610 | * state. |
| 2611 | */ |
| 2612 | qc_idle_timer_do_rearm(qc); |
| 2613 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2614 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2615 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2616 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2617 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2618 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2619 | goto err; |
| 2620 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2621 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2622 | break; |
| 2623 | default: |
| 2624 | goto err; |
| 2625 | } |
| 2626 | } |
| 2627 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2628 | /* 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] | 2629 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2630 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2631 | if (fast_retrans) { |
| 2632 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2633 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2634 | |
| 2635 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2636 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2637 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2638 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2639 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2640 | * be discarded. |
| 2641 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2642 | 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] | 2643 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2644 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2645 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2646 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2647 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2648 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2649 | qc_set_timer(ctx->qc); |
| 2650 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2651 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2652 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2653 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2654 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2655 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2658 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2659 | return 1; |
| 2660 | |
| 2661 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2662 | 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] | 2663 | return 0; |
| 2664 | } |
| 2665 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2666 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2667 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2668 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2669 | * index after having marked the end of written data. This the responsability |
| 2670 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2671 | * data made of a uint16_t null field. |
| 2672 | * |
| 2673 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2674 | * ^ ^ |
| 2675 | * r w |
| 2676 | * |
| 2677 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2678 | * ^ ^ |
| 2679 | * r w |
| 2680 | |
| 2681 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2682 | * ^ |
| 2683 | * (r = w) |
| 2684 | * |
| 2685 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2686 | * ^ ^ |
| 2687 | * w r |
| 2688 | */ |
| 2689 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2690 | { |
| 2691 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2692 | |
| 2693 | /* We can reset the writer index only if in front of the reader index and |
| 2694 | * if the reader index is not null. Resetting the writer when the reader |
| 2695 | * index is null would empty the buffer. |
| 2696 | * XXX Note than the writer index cannot reach the reader index. |
| 2697 | * Only the reader index can reach the writer index. |
| 2698 | */ |
| 2699 | if (rd && rd <= cbuf->wr) { |
| 2700 | /* Mark the end of contiguous data for the reader */ |
| 2701 | write_u16(cb_wr(cbuf), 0); |
| 2702 | cb_add(cbuf, sizeof(uint16_t)); |
| 2703 | cb_wr_reset(cbuf); |
| 2704 | return 1; |
| 2705 | } |
| 2706 | |
| 2707 | return 0; |
| 2708 | } |
| 2709 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2710 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2711 | * 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] | 2712 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2713 | * This function must be called only after having built a correct datagram. |
| 2714 | * Always succeeds. |
| 2715 | */ |
| 2716 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2717 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2718 | { |
| 2719 | write_u16(cb_wr(cbuf), dglen); |
| 2720 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2721 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2722 | } |
| 2723 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2724 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2725 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2726 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2727 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2728 | */ |
| 2729 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2730 | struct quic_enc_level *qel, int cc, int probe) |
| 2731 | { |
| 2732 | unsigned int must_ack = |
| 2733 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2734 | |
| 2735 | /* Do not build any more packet if the TX secrets are not available or |
| 2736 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2737 | * and if there is no more packets to send upon PTO expiration |
| 2738 | * and if there is no more ack-eliciting frames to send or in flight |
| 2739 | * congestion control limit is reached for prepared data |
| 2740 | */ |
| 2741 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2742 | (!cc && !probe && !must_ack && |
| 2743 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2744 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
| 2748 | return 1; |
| 2749 | } |
| 2750 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2751 | /* 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] | 2752 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2753 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2754 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2755 | * 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] | 2756 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2757 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2758 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2759 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2760 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2761 | { |
| 2762 | struct quic_enc_level *qel; |
| 2763 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2764 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2765 | struct quic_tx_packet *pkt; |
| 2766 | size_t total; |
| 2767 | size_t dg_headlen; |
| 2768 | |
| 2769 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2770 | /* Each datagram is prepended with its length followed by the |
| 2771 | * address of the first packet in the datagram. |
| 2772 | */ |
| 2773 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2774 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2775 | total = 0; |
| 2776 | start: |
| 2777 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2778 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2779 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2780 | * to ensure there is enough room to mark the end of prepared |
| 2781 | * contiguous data with a zero length. |
| 2782 | */ |
| 2783 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2784 | 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] | 2785 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2786 | |
| 2787 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2788 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2789 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2790 | /* We do not probe if an immediate close was asked */ |
| 2791 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2792 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2793 | |
| 2794 | 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] | 2795 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2796 | |
| 2797 | /* Leave room for the datagram header */ |
| 2798 | pos += dg_headlen; |
| 2799 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2800 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2801 | } |
| 2802 | else { |
| 2803 | end = pos + qc->path->mtu; |
| 2804 | } |
| 2805 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2806 | 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] | 2807 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2808 | switch (err) { |
| 2809 | case -2: |
| 2810 | goto err; |
| 2811 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2812 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2813 | * MTU, we are here because of the congestion control window. There is |
| 2814 | * no need to try to reuse this buffer. |
| 2815 | */ |
| 2816 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2817 | default: |
| 2818 | break; |
| 2819 | } |
| 2820 | |
| 2821 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2822 | if (!pkt) |
| 2823 | goto err; |
| 2824 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2825 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2826 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2827 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2828 | total += pkt->len; |
| 2829 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2830 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2831 | } |
| 2832 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2833 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2834 | 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] | 2835 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2836 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2837 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | out: |
| 2841 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2842 | return total; |
| 2843 | |
| 2844 | err: |
| 2845 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2846 | return -1; |
| 2847 | } |
| 2848 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2849 | /* 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] | 2850 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2851 | * several packets in the same datagram. A header made of two fields is added |
| 2852 | * to each datagram: the datagram length followed by the address of the first |
| 2853 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2854 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2855 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2856 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2857 | 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] | 2858 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2859 | 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] | 2860 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2861 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2862 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2863 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2864 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2865 | /* length of datagrams */ |
| 2866 | uint16_t dglen; |
| 2867 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2868 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2869 | /* Each datagram is prepended with its length followed by the |
| 2870 | * address of the first packet in the datagram. |
| 2871 | */ |
| 2872 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2873 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2874 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2875 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2876 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2877 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2878 | qel = &qc->els[tel]; |
| 2879 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2880 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2881 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2882 | padding = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2883 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2884 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2885 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2886 | * to ensure there is enough room to mark the end of prepared |
| 2887 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2888 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2889 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2890 | first_pkt = prv_pkt = NULL; |
| 2891 | 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] | 2892 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2893 | enum quic_pkt_type pkt_type; |
| 2894 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2895 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2896 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2897 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2898 | /* We do not probe if an immediate close was asked */ |
| 2899 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2900 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2901 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2902 | 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] | 2903 | if (prv_pkt) |
| 2904 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2905 | /* Let's select the next encryption level */ |
| 2906 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2907 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2908 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2909 | qel = &qc->els[tel]; |
| 2910 | /* Build a new datagram */ |
| 2911 | prv_pkt = NULL; |
| 2912 | continue; |
| 2913 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2914 | break; |
| 2915 | } |
| 2916 | |
| 2917 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2918 | if (!prv_pkt) { |
| 2919 | /* Leave room for the datagram header */ |
| 2920 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2921 | 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] | 2922 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2923 | } |
| 2924 | else { |
| 2925 | end = pos + qc->path->mtu; |
| 2926 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2927 | } |
| 2928 | |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2929 | cur_pkt = qc_build_pkt(&pos, end, qel, frms, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2930 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2931 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | case -2: |
| 2933 | goto err; |
| 2934 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2935 | /* If there was already a correct packet present, set the |
| 2936 | * current datagram as prepared into <cbuf>. |
| 2937 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2938 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2939 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2940 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2941 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2944 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2945 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2946 | if (!cur_pkt) |
| 2947 | goto err; |
| 2948 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2949 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2950 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2951 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2952 | total += cur_pkt->len; |
| 2953 | /* keep trace of the first packet in the datagram */ |
| 2954 | if (!first_pkt) |
| 2955 | first_pkt = cur_pkt; |
| 2956 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2957 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2958 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2959 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 2960 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2961 | /* Let's say we have to build a new dgram */ |
| 2962 | prv_pkt = NULL; |
| 2963 | dglen += cur_pkt->len; |
| 2964 | /* Client: discard the Initial encryption keys as soon as |
| 2965 | * a handshake packet could be built. |
| 2966 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2967 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2968 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2969 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2970 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2971 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2972 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2973 | 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] | 2974 | 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] | 2975 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2976 | } |
| 2977 | /* If the data for the current encryption level have all been sent, |
| 2978 | * select the next level. |
| 2979 | */ |
| 2980 | 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] | 2981 | 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] | 2982 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2983 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2984 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2985 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2986 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 2987 | frms = &qc->els[tel].pktns->tx.frms; |
| 2988 | else |
| 2989 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2990 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2991 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2992 | /* If there is data for the next level, do not |
| 2993 | * consume a datagram. |
| 2994 | */ |
| 2995 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2996 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2997 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2998 | /* If we have to build a new datagram, set the current datagram as |
| 2999 | * prepared into <cbuf>. |
| 3000 | */ |
| 3001 | if (!prv_pkt) { |
| 3002 | qc_set_dg(cbuf, dglen, first_pkt); |
| 3003 | first_pkt = NULL; |
| 3004 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3005 | padding = 0; |
| 3006 | } |
| 3007 | 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] | 3008 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3009 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3010 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | stop_build: |
| 3015 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3016 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3017 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3018 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3019 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3023 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3024 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3025 | |
| 3026 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3027 | 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] | 3028 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | /* 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] | 3032 | * 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] | 3033 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3034 | 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] | 3035 | { |
| 3036 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3037 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3038 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3039 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3040 | cbuf = qr->cbuf; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3041 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3042 | while (cb_contig_data(cbuf)) { |
| 3043 | unsigned char *pos; |
| 3044 | struct buffer tmpbuf = { }; |
| 3045 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3046 | uint16_t dglen; |
| 3047 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3048 | unsigned int time_sent; |
| 3049 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3050 | pos = cb_rd(cbuf); |
| 3051 | dglen = read_u16(pos); |
| 3052 | /* End of prepared datagrams. |
| 3053 | * Reset the reader index only if in front of the writer index. |
| 3054 | */ |
| 3055 | if (!dglen) { |
| 3056 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3057 | |
| 3058 | if (wr && wr < cbuf->rd) { |
| 3059 | cb_rd_reset(cbuf); |
| 3060 | continue; |
| 3061 | } |
| 3062 | break; |
| 3063 | } |
| 3064 | |
| 3065 | pos += sizeof dglen; |
| 3066 | first_pkt = read_ptr(pos); |
| 3067 | pos += sizeof first_pkt; |
| 3068 | tmpbuf.area = (char *)pos; |
| 3069 | tmpbuf.size = tmpbuf.data = dglen; |
| 3070 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3071 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3072 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3073 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3074 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3075 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3076 | qc->tx.bytes += tmpbuf.data; |
| 3077 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3078 | |
| 3079 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3080 | pkt->time_sent = time_sent; |
| 3081 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3082 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3083 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3084 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3085 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3086 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3087 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3088 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3089 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3090 | qc_notify_close(qc); |
| 3091 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3092 | /* RFC 9000 10.2. Immediate Close: |
| 3093 | * The closing and draining connection states exist to ensure |
| 3094 | * that connections close cleanly and that delayed or reordered |
| 3095 | * packets are properly discarded. These states SHOULD persist |
| 3096 | * for at least three times the current PTO interval... |
| 3097 | * |
| 3098 | * Rearm the idle timeout only one time when entering closing |
| 3099 | * state. |
| 3100 | */ |
| 3101 | qc_idle_timer_do_rearm(qc); |
| 3102 | if (qc->timer_task) { |
| 3103 | task_destroy(qc->timer_task); |
| 3104 | qc->timer_task = NULL; |
| 3105 | } |
| 3106 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3107 | qc->path->in_flight += pkt->in_flight_len; |
| 3108 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3109 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3110 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3111 | 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] | 3112 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3113 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3114 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3115 | } |
| 3116 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3117 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3118 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3119 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3120 | return 1; |
| 3121 | } |
| 3122 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3123 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3124 | * <salt> salt input. This is the cluster secret which will be derived |
| 3125 | * as HKDF input secret to generate this token. |
| 3126 | * Return 1 if succeeded, 0 if not. |
| 3127 | */ |
| 3128 | static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len, |
| 3129 | const unsigned char *salt, size_t saltlen) |
| 3130 | { |
| 3131 | /* Input secret */ |
| 3132 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3133 | size_t keylen = strlen(global.cluster_secret); |
| 3134 | /* Info */ |
| 3135 | const unsigned char label[] = "stateless token"; |
| 3136 | size_t labellen = sizeof label - 1; |
| 3137 | |
| 3138 | return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3139 | key, keylen, salt, saltlen, label, labellen); |
| 3140 | } |
| 3141 | |
| 3142 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3143 | * Returns 1 if succeeded, 0 if not. |
| 3144 | */ |
| 3145 | static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid) |
| 3146 | { |
| 3147 | if (global.cluster_secret) { |
| 3148 | /* Output secret */ |
| 3149 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3150 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3151 | /* Salt */ |
| 3152 | const unsigned char *cid = quic_cid->cid.data; |
| 3153 | size_t cidlen = quic_cid->cid.len; |
| 3154 | |
| 3155 | return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen); |
| 3156 | } |
| 3157 | else { |
| 3158 | return RAND_bytes(quic_cid->stateless_reset_token, |
| 3159 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3160 | } |
| 3161 | } |
| 3162 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3163 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3164 | * ebtree. |
| 3165 | * |
| 3166 | * The CID is randomly generated in part with the result altered to be |
| 3167 | * associated with the current thread ID. This means this function must only |
| 3168 | * be called by the quic_conn thread. |
| 3169 | * |
| 3170 | * Returns the new CID if succeeded, NULL if not. |
| 3171 | */ |
| 3172 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3173 | struct quic_conn *qc, |
| 3174 | int seq_num) |
| 3175 | { |
| 3176 | struct quic_connection_id *cid; |
| 3177 | |
| 3178 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3179 | if (!cid) |
| 3180 | return NULL; |
| 3181 | |
| 3182 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3183 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3184 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3185 | |
| 3186 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3187 | if (quic_stateless_reset_token_init(cid) != 1) |
| 3188 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3189 | |
| 3190 | cid->qc = qc; |
| 3191 | |
| 3192 | cid->seq_num.key = seq_num; |
| 3193 | cid->retire_prior_to = 0; |
| 3194 | /* insert the allocated CID in the quic_conn tree */ |
| 3195 | eb64_insert(root, &cid->seq_num); |
| 3196 | |
| 3197 | return cid; |
| 3198 | |
| 3199 | err: |
| 3200 | pool_free(pool_head_quic_connection_id, cid); |
| 3201 | return NULL; |
| 3202 | } |
| 3203 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3204 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3205 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3206 | * a HANDSHAKE_DONE frame. |
| 3207 | * Return 1 if succeeded, 0 if not. |
| 3208 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3209 | 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] | 3210 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3211 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3212 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3213 | struct quic_frame *frm, *frmbak; |
| 3214 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3215 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3216 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3217 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3218 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3219 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3220 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3221 | if (!frm) |
| 3222 | return 0; |
| 3223 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3224 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3225 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3226 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3227 | } |
| 3228 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3229 | first = 1; |
| 3230 | max = qc->tx.params.active_connection_id_limit; |
| 3231 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3232 | struct quic_connection_id *cid; |
| 3233 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3234 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3235 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3236 | goto err; |
| 3237 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3238 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3239 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3240 | if (!cid) |
| 3241 | goto err; |
| 3242 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3243 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3244 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3245 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3246 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3247 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3248 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3249 | |
| 3250 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3251 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3252 | |
| 3253 | return 1; |
| 3254 | |
| 3255 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3256 | /* free the frames */ |
| 3257 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3258 | pool_free(pool_head_quic_frame, frm); |
| 3259 | |
| 3260 | node = eb64_first(&qc->cids); |
| 3261 | while (node) { |
| 3262 | struct quic_connection_id *cid; |
| 3263 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3264 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3265 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3266 | if (cid->seq_num.key >= max) |
| 3267 | break; |
| 3268 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3269 | if (cid->seq_num.key < first) |
| 3270 | continue; |
| 3271 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3272 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3273 | ebmb_delete(&cid->node); |
| 3274 | eb64_delete(&cid->seq_num); |
| 3275 | pool_free(pool_head_quic_connection_id, cid); |
| 3276 | } |
| 3277 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3278 | return 0; |
| 3279 | } |
| 3280 | |
| 3281 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3282 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3283 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3284 | struct eb64_node *n; |
| 3285 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3286 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3287 | n = eb64_first(&arngs->root); |
| 3288 | while (n) { |
| 3289 | struct eb64_node *next; |
| 3290 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3291 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3292 | next = eb64_next(n); |
| 3293 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3294 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3295 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3299 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3300 | * descending order. |
| 3301 | */ |
| 3302 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3303 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3304 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3305 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | |
| 3309 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3310 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3311 | * 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] | 3312 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3313 | 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] | 3314 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3315 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3317 | last = eb64_last(&arngs->root); |
| 3318 | while (last && arngs->enc_sz > limit) { |
| 3319 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3320 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3321 | prev = eb64_prev(last); |
| 3322 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3323 | return 0; |
| 3324 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3325 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3326 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3327 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3328 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3329 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3330 | --arngs->sz; |
| 3331 | eb64_delete(last); |
| 3332 | pool_free(pool_head_quic_arng, last); |
| 3333 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | return 1; |
| 3337 | } |
| 3338 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3339 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3340 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3341 | { |
| 3342 | struct eb64_node *node, *next; |
| 3343 | struct quic_arng_node *ar, *ar_next; |
| 3344 | |
| 3345 | node = eb64_last(&arngs->root); |
| 3346 | if (!node) |
| 3347 | return; |
| 3348 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3349 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3350 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3351 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3352 | |
| 3353 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3354 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3355 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3356 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3357 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3358 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3362 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3363 | * 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] | 3364 | */ |
| 3365 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3366 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3367 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3368 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3369 | struct quic_arng_node *new_ar; |
| 3370 | |
| 3371 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3372 | if (new_ar) { |
| 3373 | new_ar->first.key = ar->first; |
| 3374 | new_ar->last = ar->last; |
| 3375 | eb64_insert(&arngs->root, &new_ar->first); |
| 3376 | arngs->sz++; |
| 3377 | } |
| 3378 | |
| 3379 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /* 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] | 3383 | * 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] | 3384 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3385 | * |
| 3386 | * Descending order |
| 3387 | * -------------> |
| 3388 | * range1 range2 |
| 3389 | * ..........|--------|..............|--------| |
| 3390 | * ^ ^ ^ ^ |
| 3391 | * | | | | |
| 3392 | * last1 first1 last2 first2 |
| 3393 | * ..........+--------+--------------+--------+...... |
| 3394 | * diff1 gap12 diff2 |
| 3395 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3396 | * To encode the previous list of ranges we must encode integers as follows in |
| 3397 | * descending order: |
| 3398 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3399 | * with diff1 = last1 - first1 |
| 3400 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3401 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3402 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3403 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3404 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3405 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3406 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3407 | struct eb64_node *le; |
| 3408 | struct quic_arng_node *new_node; |
| 3409 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3410 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3411 | new = NULL; |
| 3412 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3413 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3414 | if (!new_node) |
| 3415 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3416 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3417 | goto out; |
| 3418 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3419 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3420 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3421 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3422 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3423 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3424 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3425 | |
| 3426 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3427 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3428 | else { |
| 3429 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3430 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3431 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3432 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3433 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3434 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3435 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3436 | if (le_ar->last + 1 >= ar->first) { |
| 3437 | le_ar->last = ar->last; |
| 3438 | new = le; |
| 3439 | new_node = le_ar; |
| 3440 | } |
| 3441 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3442 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3443 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3444 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3445 | |
| 3446 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3447 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3448 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3449 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3450 | /* Verify that the new inserted node does not overlap the nodes |
| 3451 | * which follow it. |
| 3452 | */ |
| 3453 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3454 | struct eb64_node *next; |
| 3455 | struct quic_arng_node *next_node; |
| 3456 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3457 | while ((next = eb64_next(new))) { |
| 3458 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3459 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3460 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3461 | break; |
| 3462 | |
| 3463 | if (next_node->last > new_node->last) |
| 3464 | new_node->last = next_node->last; |
| 3465 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3466 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3467 | /* Decrement the size of these ranges. */ |
| 3468 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3469 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | } |
| 3471 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3472 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3473 | quic_arngs_set_enc_sz(arngs); |
| 3474 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3475 | return 1; |
| 3476 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3477 | /* Remove the header protection of packets at <el> encryption level. |
| 3478 | * Always succeeds. |
| 3479 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3480 | 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] | 3481 | { |
| 3482 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3483 | struct quic_rx_packet *pqpkt; |
| 3484 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3485 | struct quic_enc_level *app_qel; |
| 3486 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3487 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3488 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3489 | /* 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] | 3490 | 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] | 3491 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3492 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3493 | goto out; |
| 3494 | } |
| 3495 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3496 | 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] | 3497 | 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] | 3498 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3499 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3500 | 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] | 3501 | /* XXX TO DO XXX */ |
| 3502 | } |
| 3503 | else { |
| 3504 | /* The AAD includes the packet number field */ |
| 3505 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3506 | /* Store the packet into the tree of packets to decrypt. */ |
| 3507 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3508 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3509 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3510 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3511 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3512 | 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] | 3513 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3514 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3515 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3519 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3523 | * Return 1 if succeeded, 0 if not. |
| 3524 | */ |
| 3525 | 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] | 3526 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3527 | { |
| 3528 | struct eb64_node *node; |
| 3529 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3530 | node = eb64_first(&el->rx.crypto.frms); |
| 3531 | while (node) { |
| 3532 | struct quic_rx_crypto_frm *cf; |
| 3533 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3534 | cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3535 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3536 | break; |
| 3537 | |
| 3538 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3539 | goto err; |
| 3540 | |
| 3541 | node = eb64_next(node); |
| 3542 | quic_rx_packet_refdec(cf->pkt); |
| 3543 | eb64_delete(&cf->offset_node); |
| 3544 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3545 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3546 | return 1; |
| 3547 | |
| 3548 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3549 | 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] | 3550 | return 0; |
| 3551 | } |
| 3552 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3553 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3554 | * 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] | 3555 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3556 | * Return 1 if succeeded, 0 if not. |
| 3557 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3558 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3559 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3560 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3561 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3562 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3563 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3564 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3565 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3566 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3567 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3568 | qel = cur_el; |
| 3569 | next_tel: |
| 3570 | if (!qel) |
| 3571 | goto out; |
| 3572 | |
| 3573 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3574 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3575 | while (node) { |
| 3576 | struct quic_rx_packet *pkt; |
| 3577 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3578 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3579 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3580 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3581 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3582 | /* Drop the packet */ |
| 3583 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3584 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3585 | } |
| 3586 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3587 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3588 | /* Drop the packet */ |
| 3589 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3590 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3591 | } |
| 3592 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3593 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3594 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3595 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3596 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3597 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3598 | qc_idle_timer_rearm(qc, 1); |
| 3599 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3600 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3601 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3602 | largest_pn_time_received = pkt->time_received; |
| 3603 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3604 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3605 | 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] | 3606 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3607 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3611 | eb64_delete(&pkt->pn_node); |
| 3612 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3613 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3614 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3615 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3616 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3617 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3618 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3619 | /* Update the largest acknowledged packet timestamps */ |
| 3620 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3621 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3622 | } |
| 3623 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3624 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3625 | goto err; |
| 3626 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3627 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3628 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3629 | qel = next_el; |
| 3630 | goto next_tel; |
| 3631 | } |
| 3632 | |
| 3633 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3634 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3635 | return 1; |
| 3636 | |
| 3637 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3638 | 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] | 3639 | return 0; |
| 3640 | } |
| 3641 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3642 | /* Check if it's possible to remove header protection for packets related to |
| 3643 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3644 | * |
| 3645 | * Return true if the operation is possible else false. |
| 3646 | */ |
| 3647 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3648 | { |
| 3649 | enum quic_tls_enc_level tel; |
| 3650 | |
| 3651 | if (!qel) |
| 3652 | return 0; |
| 3653 | |
| 3654 | tel = ssl_to_quic_enc_level(qel->level); |
| 3655 | |
| 3656 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3657 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3658 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3659 | return 0; |
| 3660 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3661 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3662 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3663 | return 0; |
| 3664 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3665 | /* 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] | 3666 | 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] | 3667 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3668 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3669 | |
| 3670 | return 1; |
| 3671 | } |
| 3672 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3673 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3674 | 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] | 3675 | { |
| 3676 | int ret; |
| 3677 | struct qring *qr; |
| 3678 | |
| 3679 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3680 | if (!qr) |
| 3681 | /* Never happens */ |
| 3682 | return 1; |
| 3683 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3684 | if (old_data) |
| 3685 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3686 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3687 | if (ret == -1) |
| 3688 | goto err; |
| 3689 | else if (ret == 0) |
| 3690 | goto out; |
| 3691 | |
| 3692 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3693 | goto err; |
| 3694 | |
| 3695 | out: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3696 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3697 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3698 | return 1; |
| 3699 | |
| 3700 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3701 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3702 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3703 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3707 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3708 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 3709 | * QUIC connection |
| 3710 | * Returns 1 if succeeded, 0 if not. |
| 3711 | */ |
| 3712 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3713 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3714 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3715 | { |
| 3716 | int ret; |
| 3717 | struct qring *qr; |
| 3718 | |
| 3719 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3720 | if (!qr) |
| 3721 | /* Never happens */ |
| 3722 | return 1; |
| 3723 | |
| 3724 | if (old_data) |
| 3725 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3726 | ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms); |
| 3727 | if (ret == -1) |
| 3728 | goto err; |
| 3729 | else if (ret == 0) |
| 3730 | goto out; |
| 3731 | |
| 3732 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3733 | goto err; |
| 3734 | |
| 3735 | out: |
| 3736 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3737 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3738 | return 1; |
| 3739 | |
| 3740 | err: |
| 3741 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3742 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3743 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3748 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3749 | { |
| 3750 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3751 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3752 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3753 | |
| 3754 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3755 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 3756 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 3757 | |
| 3758 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 3759 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 3760 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3761 | if (!LIST_ISEMPTY(&ifrms)) { |
| 3762 | iqel->pktns->tx.pto_probe = 1; |
| 3763 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3764 | hqel->pktns->tx.pto_probe = 1; |
| 3765 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 3766 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 3767 | } |
| 3768 | } |
| 3769 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3770 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 3771 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3772 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3773 | hqel->pktns->tx.pto_probe = 1; |
| 3774 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 3775 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3776 | } |
| 3777 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3778 | } |
| 3779 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3780 | } |
| 3781 | else { |
| 3782 | int i; |
| 3783 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 3784 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 3785 | |
| 3786 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3787 | hqel->pktns->tx.pto_probe = 0; |
| 3788 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 3789 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 3790 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3791 | if (!LIST_ISEMPTY(&frms1)) { |
| 3792 | hqel->pktns->tx.pto_probe = 1; |
| 3793 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 3794 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3795 | } |
| 3796 | } |
| 3797 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3798 | } |
| 3799 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3800 | aqel->pktns->tx.pto_probe = 0; |
| 3801 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 3802 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3803 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 3804 | if (!LIST_ISEMPTY(&frms1)) { |
| 3805 | aqel->pktns->tx.pto_probe = 1; |
| 3806 | qc_send_app_pkts(qc, 1, &frms1); |
| 3807 | } |
| 3808 | if (!LIST_ISEMPTY(&frms2)) { |
| 3809 | aqel->pktns->tx.pto_probe = 1; |
| 3810 | qc_send_app_pkts(qc, 1, &frms2); |
| 3811 | } |
| 3812 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3817 | /* QUIC connection packet handler task (post handshake) */ |
| 3818 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3819 | { |
| 3820 | struct ssl_sock_ctx *ctx; |
| 3821 | struct quic_conn *qc; |
| 3822 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3823 | |
| 3824 | |
| 3825 | ctx = context; |
| 3826 | qc = ctx->qc; |
| 3827 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3828 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3829 | 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] | 3830 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3831 | /* Retranmissions */ |
| 3832 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3833 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 3834 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3835 | qc_dgrams_retransmit(qc); |
| 3836 | } |
| 3837 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3838 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3839 | qc_rm_hp_pkts(qc, qel); |
| 3840 | |
| 3841 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3842 | goto err; |
| 3843 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3844 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3845 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3846 | goto out; |
| 3847 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3848 | 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] | 3849 | goto err; |
| 3850 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3851 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3852 | return t; |
| 3853 | |
| 3854 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3855 | 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] | 3856 | return t; |
| 3857 | } |
| 3858 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3859 | /* QUIC connection packet handler task. */ |
| 3860 | 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] | 3861 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3862 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3863 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3864 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3865 | enum quic_tls_enc_level tel, next_tel; |
| 3866 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3867 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3868 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3869 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3870 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3871 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3872 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3873 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3874 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3875 | 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] | 3876 | |
| 3877 | /* Retranmissions */ |
| 3878 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3879 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 3880 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3881 | qc_dgrams_retransmit(qc); |
| 3882 | } |
| 3883 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3884 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3885 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3886 | /* The I/O handler has been woken up by the dgram listener |
| 3887 | * after the anti-amplification was reached. |
| 3888 | */ |
| 3889 | qc_set_timer(qc); |
| 3890 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3891 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3892 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3893 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3894 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3895 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3896 | 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] | 3897 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3898 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3899 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3900 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3901 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3902 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3903 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3904 | } |
| 3905 | 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] | 3906 | goto err; |
| 3907 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3908 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3909 | 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] | 3910 | |
| 3911 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3912 | /* Treat packets waiting for header packet protection decryption */ |
| 3913 | 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] | 3914 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3915 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3916 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3917 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3918 | 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] | 3919 | goto err; |
| 3920 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3921 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3922 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3923 | goto out; |
| 3924 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 3925 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
| 3926 | !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
| 3927 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3928 | qel = next_qel; |
| 3929 | next_qel = NULL; |
| 3930 | goto next_level; |
| 3931 | } |
| 3932 | else { |
| 3933 | struct quic_rx_packet *pkt; |
| 3934 | struct mt_list *elt1, elt2; |
| 3935 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 3936 | |
| 3937 | /* Drop these 0-RTT packets */ |
| 3938 | TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3939 | mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) { |
| 3940 | MT_LIST_DELETE_SAFE(elt1); |
| 3941 | quic_rx_packet_refdec(pkt); |
| 3942 | } |
| 3943 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3944 | } |
| 3945 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3946 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3947 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3948 | 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] | 3949 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3950 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3951 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3952 | 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] | 3953 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3954 | /* Discard the Handshake keys. */ |
| 3955 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3956 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3957 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3958 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3959 | 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] | 3960 | 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] | 3961 | } |
| 3962 | |
| 3963 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3964 | /* There may be remaining handshake to build (acks) */ |
| 3965 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3966 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3967 | } |
| 3968 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3969 | if (!qr) |
| 3970 | 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] | 3971 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3972 | * be considered. |
| 3973 | */ |
| 3974 | 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] | 3975 | goto err; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3976 | ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms, |
| 3977 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3978 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3979 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3980 | else if (ret == 0) |
| 3981 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3982 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3983 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3984 | goto err; |
| 3985 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3986 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3987 | /* Check if there is something to do for the next level. |
| 3988 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3989 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3990 | (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] | 3991 | (!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] | 3992 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3993 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3994 | goto next_level; |
| 3995 | } |
| 3996 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3997 | out: |
| 3998 | if (qr) |
| 3999 | 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] | 4000 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4001 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4002 | |
| 4003 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4004 | if (qr) |
| 4005 | 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] | 4006 | 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] | 4007 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4008 | } |
| 4009 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4010 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4011 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 4012 | { |
| 4013 | int i; |
| 4014 | |
| 4015 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4016 | if (qel->tx.crypto.bufs[i]) { |
| 4017 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4018 | qel->tx.crypto.bufs[i] = NULL; |
| 4019 | } |
| 4020 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4021 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4025 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4026 | * Returns 1 if succeeded, 0 if not. |
| 4027 | */ |
| 4028 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4029 | enum quic_tls_enc_level level) |
| 4030 | { |
| 4031 | struct quic_enc_level *qel; |
| 4032 | |
| 4033 | qel = &qc->els[level]; |
| 4034 | qel->level = quic_to_ssl_enc_level(level); |
| 4035 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4036 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4037 | 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] | 4038 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4039 | |
| 4040 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4041 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4042 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4043 | qel->rx.crypto.offset = 0; |
| 4044 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4045 | |
| 4046 | /* Allocate only one buffer. */ |
| 4047 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4048 | if (!qel->tx.crypto.bufs) |
| 4049 | goto err; |
| 4050 | |
| 4051 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4052 | if (!qel->tx.crypto.bufs[0]) |
| 4053 | goto err; |
| 4054 | |
| 4055 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4056 | qel->tx.crypto.nb_buf = 1; |
| 4057 | |
| 4058 | qel->tx.crypto.sz = 0; |
| 4059 | qel->tx.crypto.offset = 0; |
| 4060 | |
| 4061 | return 1; |
| 4062 | |
| 4063 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4064 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4068 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4069 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4070 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4071 | * This function must only be called by the thread responsible of the quic_conn |
| 4072 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4073 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4074 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4075 | { |
| 4076 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4077 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4078 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4079 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4080 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4081 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4082 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4083 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4084 | /* free remaining stream descriptors */ |
| 4085 | node = eb64_first(&qc->streams_by_id); |
| 4086 | while (node) { |
| 4087 | struct qc_stream_desc *stream; |
| 4088 | |
| 4089 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4090 | node = eb64_next(node); |
| 4091 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4092 | /* all streams attached to the quic-conn are released, so |
| 4093 | * qc_stream_desc_free will liberate the stream instance. |
| 4094 | */ |
| 4095 | BUG_ON(!stream->release); |
| 4096 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4097 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4098 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4099 | if (qc->idle_timer_task) { |
| 4100 | task_destroy(qc->idle_timer_task); |
| 4101 | qc->idle_timer_task = NULL; |
| 4102 | } |
| 4103 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4104 | if (qc->timer_task) { |
| 4105 | task_destroy(qc->timer_task); |
| 4106 | qc->timer_task = NULL; |
| 4107 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4108 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4109 | /* remove the connection from receiver cids trees */ |
| 4110 | ebmb_delete(&qc->odcid_node); |
| 4111 | ebmb_delete(&qc->scid_node); |
| 4112 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4113 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4114 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4115 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4116 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4117 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4118 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4119 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4120 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4121 | quic_tls_ku_free(qc); |
| 4122 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4123 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4124 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4128 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4129 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4130 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4131 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 4132 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4133 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4134 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4135 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4136 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4137 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4138 | 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] | 4139 | } |
| 4140 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4141 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4142 | { |
| 4143 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4144 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4145 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4146 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4147 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4148 | /* Next application data can be dropped. */ |
| 4149 | qc->mux_state = QC_MUX_RELEASED; |
| 4150 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4151 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4152 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4153 | quic_conn_release(qc); |
| 4154 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 4155 | return; |
| 4156 | } |
| 4157 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4158 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4159 | } |
| 4160 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4161 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4162 | 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] | 4163 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4164 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4165 | struct quic_conn *qc; |
| 4166 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4167 | |
| 4168 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4169 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4170 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4171 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4172 | task->expire = TICK_ETERNITY; |
| 4173 | pktns = quic_loss_pktns(qc); |
| 4174 | if (tick_isset(pktns->tx.loss_time)) { |
| 4175 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4176 | |
| 4177 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4178 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4179 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4180 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4181 | goto out; |
| 4182 | } |
| 4183 | |
| 4184 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4185 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | dafbde6 | 2022-04-26 13:54:28 +0200 | [diff] [blame] | 4186 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4187 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4188 | struct qcc *qcc = qc->qcc; |
| 4189 | |
| 4190 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4191 | tasklet_wakeup(qcc->subs->tasklet); |
| 4192 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4193 | if (!qcc->subs->events) |
| 4194 | qcc->subs = NULL; |
| 4195 | } |
| 4196 | else { |
| 4197 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4198 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4199 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4200 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 4201 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4202 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4203 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4204 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4205 | 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] | 4206 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4207 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4208 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4209 | 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] | 4210 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4211 | 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] | 4212 | iel->pktns->tx.pto_probe = 1; |
| 4213 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4214 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4215 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4216 | qc->path->loss.pto_count++; |
| 4217 | |
| 4218 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4219 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4220 | |
| 4221 | return task; |
| 4222 | } |
| 4223 | |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4224 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4225 | * will extract the parameters stored in the token : <odcid>. |
| 4226 | * |
| 4227 | * Returns 0 on success else non-zero. |
| 4228 | */ |
| 4229 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4230 | struct quic_cid *odcid) |
| 4231 | { |
| 4232 | uint64_t odcid_len; |
| 4233 | |
| 4234 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4235 | return 1; |
| 4236 | |
| 4237 | if (odcid_len > QUIC_CID_MAXLEN) |
| 4238 | return 1; |
| 4239 | |
| 4240 | memcpy(odcid->data, token, odcid_len); |
| 4241 | odcid->len = odcid_len; |
| 4242 | |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
| 4246 | /* Initialize the transport parameters for <qc> QUIC connection attached |
| 4247 | * to <l> listener from <pkt> Initial packet information. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4248 | * Returns 1 if succeeded, 0 if not. |
| 4249 | */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4250 | static int qc_lstnr_params_init(struct quic_conn *qc, struct listener *l, |
| 4251 | const unsigned char *token, size_t token_len, |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4252 | const struct quic_connection_id *icid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4253 | const struct quic_cid *dcid) |
| 4254 | { |
| 4255 | struct quic_cid *odcid = &qc->rx.params.original_destination_connection_id; |
| 4256 | |
| 4257 | /* Copy the transport parameters. */ |
| 4258 | qc->rx.params = l->bind_conf->quic_params; |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4259 | /* Copy the stateless reset token */ |
| 4260 | memcpy(qc->rx.params.stateless_reset_token, icid->stateless_reset_token, |
| 4261 | sizeof qc->rx.params.stateless_reset_token); |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4262 | /* Copy original_destination_connection_id transport parameter. */ |
| 4263 | if (token_len) { |
| 4264 | if (parse_retry_token(token, token_len, odcid)) { |
| 4265 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4266 | return 0; |
| 4267 | } |
| 4268 | /* Copy retry_source_connection_id transport parameter. */ |
| 4269 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, dcid); |
| 4270 | } |
| 4271 | else { |
| 4272 | memcpy(odcid->data, dcid->data, dcid->len); |
| 4273 | odcid->len = dcid->len; |
| 4274 | } |
| 4275 | |
| 4276 | /* Copy the initial source connection ID. */ |
| 4277 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4278 | |
| 4279 | return 1; |
| 4280 | } |
| 4281 | |
| 4282 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4283 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4284 | * for QUIC servers (or haproxy listeners). |
| 4285 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4286 | * <token> the token found to be used for this connection with <token_len> as |
| 4287 | * length. <saddr> is the source address. |
| 4288 | * Returns the connection if succeeded, NULL if not. |
| 4289 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4290 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4291 | struct quic_cid *dcid, struct quic_cid *scid, |
| 4292 | struct sockaddr_storage *saddr, |
| 4293 | const unsigned char *token, size_t token_len, |
| 4294 | int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4295 | { |
| 4296 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4297 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4298 | /* Initial CID. */ |
| 4299 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4300 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4301 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4302 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4303 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4304 | qc = pool_zalloc(pool_head_quic_conn); |
| 4305 | if (!qc) { |
| 4306 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4307 | goto err; |
| 4308 | } |
| 4309 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4310 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4311 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4312 | 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] | 4313 | goto err; |
| 4314 | } |
| 4315 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4316 | qc->cids = EB_ROOT; |
| 4317 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4318 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4319 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4320 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4321 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4322 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4323 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4324 | qc->odcid.len = dcid->len; |
| 4325 | qc->odcid.addrlen = dcid->addrlen; |
| 4326 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4327 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4328 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4329 | if (scid->len) |
| 4330 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4331 | qc->dcid.len = scid->len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4332 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4333 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4334 | } |
| 4335 | /* QUIC Client (outgoing connection to servers) */ |
| 4336 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4337 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4338 | if (dcid->len) |
| 4339 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4340 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4341 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4342 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4343 | |
| 4344 | /* Initialize the output buffer */ |
| 4345 | qc->obuf.pos = qc->obuf.data; |
| 4346 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4347 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4348 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4349 | 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] | 4350 | goto err; |
| 4351 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4352 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4353 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4354 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4355 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4356 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4357 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4358 | qc->scid = icid->cid; |
| 4359 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4360 | /* Packet number spaces initialization. */ |
| 4361 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4362 | quic_pktns_init(&qc->pktns[i]); |
| 4363 | /* QUIC encryption level context initialization. */ |
| 4364 | 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] | 4365 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4366 | 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] | 4367 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4368 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4369 | /* Initialize the packet number space. */ |
| 4370 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4371 | } |
| 4372 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4373 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4374 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4375 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4376 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4377 | /* TX part. */ |
| 4378 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4379 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4380 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4381 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4382 | /* RX part. */ |
| 4383 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4384 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 664741e | 2022-05-02 18:46:58 +0200 | [diff] [blame] | 4385 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4386 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4387 | |
| 4388 | qc->nb_pkt_for_cc = 1; |
| 4389 | qc->nb_pkt_since_cc = 0; |
| 4390 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4391 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4392 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4393 | 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] | 4394 | goto err; |
| 4395 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4396 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | /* XXX TO DO: Only one path at this time. */ |
| 4398 | qc->path = &qc->paths[0]; |
| 4399 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4400 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4401 | /* required to use MTLIST_IN_LIST */ |
| 4402 | MT_LIST_INIT(&qc->accept_list); |
| 4403 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4404 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4405 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 4406 | qc->sendto_err = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4407 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4408 | |
Frédéric Lécaille | 806e6cf | 2022-05-09 16:22:29 +0200 | [diff] [blame] | 4409 | if (server && !qc_lstnr_params_init(qc, l, token, token_len, icid, dcid)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4410 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4411 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4412 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4413 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4414 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4415 | |
| 4416 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4417 | 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] | 4418 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4419 | if (qc) |
| 4420 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4421 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4422 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | /* Initialize the timer task of <qc> QUIC connection. |
| 4426 | * Returns 1 if succeeded, 0 if not. |
| 4427 | */ |
| 4428 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4429 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4430 | /* Attach this task to the same thread ID used for the connection */ |
| 4431 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4432 | if (!qc->timer_task) |
| 4433 | return 0; |
| 4434 | |
| 4435 | qc->timer = TICK_ETERNITY; |
| 4436 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4437 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4438 | |
| 4439 | return 1; |
| 4440 | } |
| 4441 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4442 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4443 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4444 | { |
| 4445 | unsigned int expire; |
| 4446 | |
| 4447 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4448 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4449 | } |
| 4450 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4451 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4452 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4453 | */ |
| 4454 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4455 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4456 | if (read) { |
| 4457 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4458 | } |
| 4459 | else { |
| 4460 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4461 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4462 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | /* The task handling the idle timeout */ |
| 4466 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4467 | { |
| 4468 | struct quic_conn *qc = ctx; |
| 4469 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4470 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4471 | * might free the quic-conn too early via quic_close(). |
| 4472 | */ |
| 4473 | qc_notify_close(qc); |
| 4474 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4475 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4476 | * responsible to call quic_close to release it. |
| 4477 | */ |
| 4478 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4479 | if (qc->mux_state != QC_MUX_READY) |
| 4480 | quic_conn_release(qc); |
| 4481 | |
| 4482 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4483 | * least clean some parts of it such as the tasklet. |
| 4484 | */ |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4485 | |
| 4486 | return NULL; |
| 4487 | } |
| 4488 | |
| 4489 | /* Initialize the idle timeout task for <qc>. |
| 4490 | * Returns 1 if succeeded, 0 if not. |
| 4491 | */ |
| 4492 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4493 | { |
| 4494 | qc->idle_timer_task = task_new_here(); |
| 4495 | if (!qc->idle_timer_task) |
| 4496 | return 0; |
| 4497 | |
| 4498 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4499 | qc->idle_timer_task->context = qc; |
| 4500 | qc_idle_timer_rearm(qc, 1); |
| 4501 | task_queue(qc->idle_timer_task); |
| 4502 | |
| 4503 | return 1; |
| 4504 | } |
| 4505 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4506 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4507 | * past one byte of this buffer. |
| 4508 | */ |
| 4509 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4510 | struct quic_rx_packet *pkt) |
| 4511 | { |
| 4512 | unsigned char dcid_len, scid_len; |
| 4513 | |
| 4514 | /* Version */ |
| 4515 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4516 | return 0; |
| 4517 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4518 | /* Destination Connection ID Length */ |
| 4519 | dcid_len = *(*buf)++; |
| 4520 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4521 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4522 | /* XXX MUST BE DROPPED */ |
| 4523 | return 0; |
| 4524 | |
| 4525 | if (dcid_len) { |
| 4526 | /* Check that the length of this received DCID matches the CID lengths |
| 4527 | * of our implementation for non Initials packets only. |
| 4528 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4529 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4530 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4531 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4532 | return 0; |
| 4533 | |
| 4534 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4535 | } |
| 4536 | |
| 4537 | pkt->dcid.len = dcid_len; |
| 4538 | *buf += dcid_len; |
| 4539 | |
| 4540 | /* Source Connection ID Length */ |
| 4541 | scid_len = *(*buf)++; |
| 4542 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4543 | /* XXX MUST BE DROPPED */ |
| 4544 | return 0; |
| 4545 | |
| 4546 | if (scid_len) |
| 4547 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4548 | pkt->scid.len = scid_len; |
| 4549 | *buf += scid_len; |
| 4550 | |
| 4551 | return 1; |
| 4552 | } |
| 4553 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4554 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4555 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4556 | { |
| 4557 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4558 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4559 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4560 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4561 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* 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] | 4565 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4566 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4567 | * the packet first byte. |
| 4568 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4569 | * Returns 1 if succeeded, 0 if not. |
| 4570 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4571 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4572 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4573 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4574 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4575 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4576 | { |
| 4577 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4578 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4579 | struct quic_enc_level *qel; |
| 4580 | /* Only for traces. */ |
| 4581 | struct quic_rx_packet *qpkt_trace; |
| 4582 | |
| 4583 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4584 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4585 | /* The packet number is here. This is also the start minus |
| 4586 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4587 | * protection. |
| 4588 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4589 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4590 | |
| 4591 | tel = quic_packet_type_enc_level(pkt->type); |
| 4592 | qel = &qc->els[tel]; |
| 4593 | |
| 4594 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4595 | /* Note that the following function enables us to unprotect the packet |
| 4596 | * number and its length subsequently used to decrypt the entire |
| 4597 | * packets. |
| 4598 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4599 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4600 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4601 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4602 | goto err; |
| 4603 | } |
| 4604 | |
| 4605 | /* The AAD includes the packet number field found at <pn>. */ |
| 4606 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4607 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4608 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4609 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4610 | 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] | 4611 | /* If the packet number space has been discarded, this packet |
| 4612 | * will be not parsed. |
| 4613 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4614 | 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] | 4615 | goto out; |
| 4616 | } |
| 4617 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4618 | 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] | 4619 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4620 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4621 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4624 | *el = qel; |
| 4625 | /* No reference counter incrementation here!!! */ |
| 4626 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4627 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4628 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4629 | b_add(&qc->rx.buf, pkt->len); |
| 4630 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4631 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4632 | return 1; |
| 4633 | |
| 4634 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4635 | 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] | 4636 | return 0; |
| 4637 | } |
| 4638 | |
| 4639 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4640 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4641 | */ |
| 4642 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4643 | unsigned char byte0, int *long_header) |
| 4644 | { |
| 4645 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4646 | pkt->type = |
| 4647 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4648 | *long_header = 1; |
| 4649 | } |
| 4650 | else { |
| 4651 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4652 | *long_header = 0; |
| 4653 | } |
| 4654 | } |
| 4655 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4656 | /* |
| 4657 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4658 | */ |
| 4659 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4660 | { |
| 4661 | int j = 0, version; |
| 4662 | |
| 4663 | do { |
| 4664 | version = quic_supported_version[j]; |
| 4665 | if (version == pkt->version) |
| 4666 | return 1; |
| 4667 | |
| 4668 | version = quic_supported_version[++j]; |
| 4669 | } while(version); |
| 4670 | |
| 4671 | return 0; |
| 4672 | } |
| 4673 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4674 | /* |
| 4675 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4676 | * address <addr>. |
| 4677 | * Implementation of RFC9000 6. Version Negotiation |
| 4678 | * |
| 4679 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4680 | * |
| 4681 | * Returns 0 on success else non-zero |
| 4682 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4683 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4684 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4685 | { |
| 4686 | char buf[256]; |
| 4687 | int i = 0, j, version; |
| 4688 | const socklen_t addrlen = get_addr_len(addr); |
| 4689 | |
| 4690 | /* |
| 4691 | * header form |
| 4692 | * long header, fixed bit to 0 for Version Negotiation |
| 4693 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4694 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4695 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4696 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4697 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4698 | /* null version for Version Negotiation */ |
| 4699 | buf[i++] = '\x00'; |
| 4700 | buf[i++] = '\x00'; |
| 4701 | buf[i++] = '\x00'; |
| 4702 | buf[i++] = '\x00'; |
| 4703 | |
| 4704 | /* source connection id */ |
| 4705 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4706 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4707 | i += pkt->scid.len; |
| 4708 | |
| 4709 | /* destination connection id */ |
| 4710 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4711 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4712 | i += pkt->dcid.len; |
| 4713 | |
| 4714 | /* supported version */ |
| 4715 | j = 0; |
| 4716 | do { |
| 4717 | version = htonl(quic_supported_version[j]); |
| 4718 | memcpy(&buf[i], &version, sizeof(version)); |
| 4719 | i += sizeof(version); |
| 4720 | |
| 4721 | version = quic_supported_version[++j]; |
| 4722 | } while (version); |
| 4723 | |
| 4724 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4725 | return 1; |
| 4726 | |
| 4727 | return 0; |
| 4728 | } |
| 4729 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4730 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 4731 | * from <fd> UDP socket to <dst> |
| 4732 | * Return 1 if succeeded, 0 if not. |
| 4733 | */ |
| 4734 | static int send_stateless_reset(int fd, struct sockaddr_storage *dstaddr, |
| 4735 | struct quic_rx_packet *rxpkt) |
| 4736 | { |
| 4737 | int pktlen, rndlen; |
| 4738 | unsigned char pkt[64]; |
| 4739 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 4740 | |
| 4741 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 4742 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 4743 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 4744 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 4745 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 4746 | * connection ID length that it requests the peer to include in its packets, |
| 4747 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 4748 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 4749 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 4750 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 4751 | * than the packet it responds to. |
| 4752 | */ |
| 4753 | |
| 4754 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 4755 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 4756 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 4757 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 4758 | /* Put a header of random bytes */ |
| 4759 | if (RAND_bytes(pkt, rndlen) != 1) |
| 4760 | return 0; |
| 4761 | |
| 4762 | /* Clear the most significant bit, and set the second one */ |
| 4763 | *pkt = (*pkt & ~0x80) | 0x40; |
| 4764 | if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 4765 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 4766 | return 0; |
| 4767 | |
| 4768 | if (sendto(fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 4769 | return 0; |
| 4770 | |
| 4771 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 4772 | return 1; |
| 4773 | } |
| 4774 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4775 | /* Generate the token to be used in Retry packets. The token is written to |
| 4776 | * <buf> which is expected to be <len> bytes. |
| 4777 | * |
| 4778 | * Various parameters are expected to be encoded in the token. For now, only |
| 4779 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4780 | * as this CID must be repeated by the server in the transport parameters. |
| 4781 | * |
| 4782 | * TODO add the client address to validate the token origin. |
| 4783 | * |
| 4784 | * Returns the length of the encoded token or 0 on error. |
| 4785 | */ |
| 4786 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4787 | struct quic_rx_packet *pkt) |
| 4788 | { |
| 4789 | const size_t token_len = 1 + pkt->dcid.len; |
| 4790 | unsigned char i = 0; |
| 4791 | |
| 4792 | if (token_len > len) |
| 4793 | return 0; |
| 4794 | |
| 4795 | buf[i++] = pkt->dcid.len; |
| 4796 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4797 | i += pkt->dcid.len; |
| 4798 | |
| 4799 | return i; |
| 4800 | } |
| 4801 | |
| 4802 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4803 | * the Initial <pkt> packet. |
| 4804 | * |
| 4805 | * Returns 0 on success else non-zero. |
| 4806 | */ |
| 4807 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4808 | struct quic_rx_packet *pkt) |
| 4809 | { |
| 4810 | unsigned char buf[128]; |
| 4811 | int i = 0, token_len; |
| 4812 | const socklen_t addrlen = get_addr_len(addr); |
| 4813 | struct quic_cid scid; |
| 4814 | |
| 4815 | /* long header + fixed bit + packet type 0x3 */ |
| 4816 | buf[i++] = 0xf0; |
| 4817 | /* version */ |
| 4818 | buf[i++] = 0x00; |
| 4819 | buf[i++] = 0x00; |
| 4820 | buf[i++] = 0x00; |
| 4821 | buf[i++] = 0x01; |
| 4822 | |
| 4823 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4824 | buf[i++] = pkt->scid.len; |
| 4825 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4826 | i += pkt->scid.len; |
| 4827 | |
| 4828 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4829 | scid.len = QUIC_HAP_CID_LEN; |
| 4830 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4831 | return 1; |
| 4832 | |
| 4833 | buf[i++] = scid.len; |
| 4834 | memcpy(&buf[i], scid.data, scid.len); |
| 4835 | i += scid.len; |
| 4836 | |
| 4837 | /* token */ |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4838 | if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4839 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4840 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4841 | i += token_len; |
| 4842 | |
| 4843 | /* token integrity tag */ |
| 4844 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4845 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4846 | pkt->dcid.len, buf, i)) { |
| 4847 | return 1; |
| 4848 | } |
| 4849 | |
| 4850 | i += QUIC_TLS_TAG_LEN; |
| 4851 | |
| 4852 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4853 | return 1; |
| 4854 | |
| 4855 | return 0; |
| 4856 | } |
| 4857 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4858 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4859 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4860 | * concatenated to the <pkt> DCID field. |
| 4861 | * |
| 4862 | * Returns the instance or NULL if not found. |
| 4863 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4864 | 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] | 4865 | struct listener *l, |
| 4866 | struct sockaddr_storage *saddr) |
| 4867 | { |
| 4868 | struct quic_conn *qc = NULL; |
| 4869 | struct ebmb_node *node; |
| 4870 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4871 | /* set if the quic_conn is found in the second DCID tree */ |
| 4872 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4873 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4874 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4875 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4876 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4877 | /* DCIDs of first packets coming from multiple clients may have |
| 4878 | * the same values. Let's distinguish them by concatenating the |
| 4879 | * socket addresses. |
| 4880 | */ |
| 4881 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4882 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4883 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4884 | if (node) { |
| 4885 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4886 | goto end; |
| 4887 | } |
| 4888 | } |
| 4889 | |
| 4890 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4891 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4892 | * used. |
| 4893 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4894 | 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] | 4895 | if (!node) |
| 4896 | goto end; |
| 4897 | |
| 4898 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4899 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4900 | found_in_dcid = 1; |
| 4901 | |
| 4902 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4903 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4904 | * If already done, this is a noop. |
| 4905 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4906 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4907 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4908 | |
| 4909 | return qc; |
| 4910 | } |
| 4911 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4912 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4913 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4914 | * parameters of this session. |
| 4915 | * This is the responsibility of the caller to check the validity of all the |
| 4916 | * pointers passed as parameter to this function. |
| 4917 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4918 | * CO_ER_SSL_NO_MEM. |
| 4919 | */ |
| 4920 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4921 | unsigned char *params, size_t params_len) |
| 4922 | { |
| 4923 | int retry; |
| 4924 | |
| 4925 | retry = 1; |
| 4926 | retry: |
| 4927 | *ssl = SSL_new(ssl_ctx); |
| 4928 | if (!*ssl) { |
| 4929 | if (!retry--) |
| 4930 | goto err; |
| 4931 | |
| 4932 | pool_gc(NULL); |
| 4933 | goto retry; |
| 4934 | } |
| 4935 | |
| 4936 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4937 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4938 | !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] | 4939 | SSL_free(*ssl); |
| 4940 | *ssl = NULL; |
| 4941 | if (!retry--) |
| 4942 | goto err; |
| 4943 | |
| 4944 | pool_gc(NULL); |
| 4945 | goto retry; |
| 4946 | } |
| 4947 | |
| 4948 | return 0; |
| 4949 | |
| 4950 | err: |
| 4951 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4952 | return -1; |
| 4953 | } |
| 4954 | |
| 4955 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4956 | * used to process <qc> received packets. The allocated context is stored in |
| 4957 | * <qc.xprt_ctx>. |
| 4958 | * |
| 4959 | * Returns 0 on success else non-zero. |
| 4960 | */ |
| 4961 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4962 | { |
| 4963 | struct bind_conf *bc = qc->li->bind_conf; |
| 4964 | struct ssl_sock_ctx *ctx = NULL; |
| 4965 | |
| 4966 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4967 | if (!ctx) |
| 4968 | goto err; |
| 4969 | |
| 4970 | ctx->wait_event.tasklet = tasklet_new(); |
| 4971 | if (!ctx->wait_event.tasklet) |
| 4972 | goto err; |
| 4973 | |
| 4974 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4975 | ctx->wait_event.tasklet->context = ctx; |
| 4976 | ctx->wait_event.events = 0; |
| 4977 | ctx->subs = NULL; |
| 4978 | ctx->xprt_ctx = NULL; |
| 4979 | ctx->qc = qc; |
| 4980 | |
| 4981 | /* Set tasklet tid based on the SCID selected by us for this |
| 4982 | * connection. The upper layer will also be binded on the same thread. |
| 4983 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4984 | 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] | 4985 | |
| 4986 | if (qc_is_listener(qc)) { |
| 4987 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4988 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4989 | goto err; |
| 4990 | } |
| 4991 | |
| 4992 | /* Enabling 0-RTT */ |
| 4993 | if (bc->ssl_conf.early_data) |
| 4994 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4995 | |
| 4996 | SSL_set_accept_state(ctx->ssl); |
| 4997 | } |
| 4998 | |
| 4999 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5000 | |
| 5001 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5002 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5003 | |
| 5004 | return 0; |
| 5005 | |
| 5006 | err: |
| 5007 | if (ctx && ctx->wait_event.tasklet) |
| 5008 | tasklet_free(ctx->wait_event.tasklet); |
| 5009 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 5010 | |
| 5011 | return 1; |
| 5012 | } |
| 5013 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5014 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5015 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5016 | * with the information collected from the packet. |
| 5017 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5018 | * byte to enable the caller to run this function again to continue to parse |
| 5019 | * the remaing QUIC packets carried by the datagram. |
| 5020 | * Note that this function always sets this ->len value. If a paquet could |
| 5021 | * not be correctly found, ->len value will be set to the remaining number |
| 5022 | * bytes in the datagram to entirely consume this latter. |
| 5023 | */ |
| 5024 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5025 | struct quic_rx_packet *pkt, int first_pkt, |
| 5026 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5027 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5028 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5029 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5030 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5031 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5032 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5033 | size_t b_cspace; |
| 5034 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5035 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5036 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5037 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5038 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5039 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5040 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 5041 | /* This ist only to please to traces and distinguish the |
| 5042 | * packet with parsed packet number from others. |
| 5043 | */ |
| 5044 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5045 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5046 | goto err; |
| 5047 | |
| 5048 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5049 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5050 | /* XXX TO BE DISCARDED */ |
| 5051 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5052 | goto err; |
| 5053 | } |
| 5054 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5055 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5056 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5057 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5058 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5059 | uint64_t len; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5060 | int drop_no_con = 0; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5061 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5062 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5063 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5064 | goto err; |
| 5065 | } |
| 5066 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5067 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5068 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc); |
| 5069 | drop_no_con = 1; |
| 5070 | } |
| 5071 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5072 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5073 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc); |
| 5074 | drop_no_con = 1; |
| 5075 | } |
| 5076 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5077 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5078 | * they must have the same DCID. |
| 5079 | */ |
| 5080 | if (!first_pkt && |
| 5081 | (pkt->dcid.len != dgram->dcid_len || |
| 5082 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5083 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5084 | goto err; |
| 5085 | } |
| 5086 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5087 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 5088 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 5089 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5090 | goto err; |
| 5091 | } |
| 5092 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5093 | /* RFC9000 6. Version Negotiation */ |
| 5094 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5095 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5096 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5097 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 5098 | goto err; |
| 5099 | } |
| 5100 | |
| 5101 | 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] | 5102 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5103 | } |
| 5104 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5105 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5106 | * there is no Initial connection IDs storage. |
| 5107 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5108 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5109 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5110 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5111 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5112 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5113 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5114 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5115 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5116 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 5117 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 5118 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5119 | */ |
| 5120 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5121 | |
| 5122 | /* TODO Retry should be automatically activated if |
| 5123 | * suspect network usage is detected. |
| 5124 | */ |
| 5125 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 5126 | 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] | 5127 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5128 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 5129 | goto err; |
| 5130 | } |
| 5131 | |
| 5132 | goto err; |
| 5133 | } |
| 5134 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5135 | pkt->token = buf; |
| 5136 | buf += pkt->token_len; |
| 5137 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5138 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5139 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5140 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5141 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5142 | goto err; |
| 5143 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5144 | } |
| 5145 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5146 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5147 | end - buf < len) { |
| 5148 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5149 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5150 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5151 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5152 | payload = buf; |
| 5153 | pkt->len = len + payload - beg; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5154 | if (drop_no_con) |
| 5155 | goto drop_no_con; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5156 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5157 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5158 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5159 | int ipv4; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5160 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5161 | const unsigned char *salt = initial_salt_v1; |
| 5162 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5163 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5164 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 5165 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5166 | goto err; |
| 5167 | } |
| 5168 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5169 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 5170 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 5171 | goto err; |
| 5172 | } |
| 5173 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5174 | pkt->saddr = dgram->saddr; |
| 5175 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 5176 | qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &pkt->saddr, |
| 5177 | pkt->token, pkt->token_len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5178 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5179 | goto err; |
| 5180 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5181 | qc->enc_params_len = |
| 5182 | quic_transport_params_encode(qc->enc_params, |
| 5183 | qc->enc_params + sizeof qc->enc_params, |
| 5184 | &qc->rx.params, 1); |
| 5185 | if (!qc->enc_params_len) |
| 5186 | goto err; |
| 5187 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5188 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 5189 | goto err; |
| 5190 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 5191 | if (!quic_conn_init_timer(qc)) |
| 5192 | goto err; |
| 5193 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 5194 | if (!quic_conn_init_idle_timer_task(qc)) |
| 5195 | goto err; |
| 5196 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 5197 | /* NOTE: the socket address has been concatenated to the destination ID |
| 5198 | * chosen by the client for Initial packets. |
| 5199 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5200 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 5201 | salt = initial_salt_draft_29; |
| 5202 | salt_len = sizeof initial_salt_draft_29; |
| 5203 | } |
| 5204 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5205 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5206 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 5207 | goto err; |
| 5208 | } |
| 5209 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5210 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5211 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5212 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5213 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5214 | /* If the insertion failed, it means that another |
| 5215 | * thread has already allocated a QUIC connection for |
| 5216 | * the same CID. Liberate our allocated connection. |
| 5217 | */ |
| 5218 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5219 | qc_to_purge = qc; |
| 5220 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5221 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5222 | pkt->qc = qc; |
| 5223 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5224 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5225 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5226 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5227 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5228 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5229 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5230 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5231 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5232 | } |
| 5233 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5234 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5235 | } |
| 5236 | } |
| 5237 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5238 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5239 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5240 | goto err; |
| 5241 | } |
| 5242 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5243 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5244 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5245 | |
| 5246 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5247 | * they must have the same DCID. |
| 5248 | */ |
| 5249 | if (!first_pkt && |
| 5250 | (pkt->dcid.len != dgram->dcid_len || |
| 5251 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5252 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5253 | goto err; |
| 5254 | } |
| 5255 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5256 | buf += QUIC_HAP_CID_LEN; |
| 5257 | |
| 5258 | /* A short packet is the last one of a UDP datagram. */ |
| 5259 | payload = buf; |
| 5260 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5261 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5262 | 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] | 5263 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5264 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5265 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5266 | if (global.cluster_secret && !send_stateless_reset(l->rx.fd, &dgram->saddr, pkt)) |
| 5267 | TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5268 | goto err; |
| 5269 | } |
| 5270 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5271 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5272 | } |
| 5273 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5274 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 5275 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 5276 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 5277 | qc->nb_pkt_for_cc++; |
| 5278 | qc->nb_pkt_since_cc = 0; |
| 5279 | } |
| 5280 | /* Skip the entire datagram */ |
| 5281 | pkt->len = end - beg; |
| 5282 | TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc); |
| 5283 | goto out; |
| 5284 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5285 | |
| 5286 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5287 | * they must have the same DCID. |
| 5288 | * |
| 5289 | * This check must be done after the final update to pkt.len to |
| 5290 | * properly drop the packet on failure. |
| 5291 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5292 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5293 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5294 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 5295 | QUIC_EV_CONN_LPKT, qc); |
| 5296 | /* Reset the anti-amplification bit. It will be set again |
| 5297 | * when sending the next packet if reached again. |
| 5298 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5299 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 5300 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5301 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5302 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5303 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5304 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5305 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5306 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5307 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5308 | goto out; |
| 5309 | } |
| 5310 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5311 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5312 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5313 | b_cspace = b_contig_space(&qc->rx.buf); |
| 5314 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 5315 | /* Do not consume buf if space not at the end. */ |
| 5316 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 5317 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 5318 | goto err; |
| 5319 | } |
| 5320 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5321 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5322 | if (b_cspace) { |
| 5323 | b_putchr(&qc->rx.buf, 0x00); |
| 5324 | b_cspace--; |
| 5325 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5326 | b_add(&qc->rx.buf, b_cspace); |
| 5327 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5328 | 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] | 5329 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5330 | goto err; |
| 5331 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5332 | } |
| 5333 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5334 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5335 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 5336 | goto err; |
| 5337 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5338 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5339 | 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] | 5340 | if (pkt->aad_len) |
| 5341 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5342 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 5343 | /* Wake up the connection packet handler task from here only if all |
| 5344 | * the contexts have been initialized, especially the mux context |
| 5345 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 5346 | * will start it if these contexts for the connection are not already |
| 5347 | * initialized. |
| 5348 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5349 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5350 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5351 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5352 | |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5353 | drop_no_con: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5354 | 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] | 5355 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5356 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5357 | |
| 5358 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5359 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5360 | * This cannot be done by this thread. |
| 5361 | */ |
| 5362 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5363 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5364 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5365 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5366 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5367 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5368 | if (!pkt->len) |
| 5369 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5370 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5371 | qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5372 | } |
| 5373 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5374 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5375 | * 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] | 5376 | */ |
| 5377 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5378 | int type, size_t pn_len, struct quic_conn *conn) |
| 5379 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5380 | 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] | 5381 | return 0; |
| 5382 | |
| 5383 | /* #0 byte flags */ |
| 5384 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5385 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5386 | /* Version */ |
| 5387 | quic_write_uint32(buf, end, conn->version); |
| 5388 | *(*buf)++ = conn->dcid.len; |
| 5389 | /* Destination connection ID */ |
| 5390 | if (conn->dcid.len) { |
| 5391 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5392 | *buf += conn->dcid.len; |
| 5393 | } |
| 5394 | /* Source connection ID */ |
| 5395 | *(*buf)++ = conn->scid.len; |
| 5396 | if (conn->scid.len) { |
| 5397 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5398 | *buf += conn->scid.len; |
| 5399 | } |
| 5400 | |
| 5401 | return 1; |
| 5402 | } |
| 5403 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5404 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5405 | * 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] | 5406 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5407 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5408 | size_t pn_len, struct quic_conn *conn, |
| 5409 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5410 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5411 | if (end - *buf < 1 + conn->dcid.len) |
| 5412 | return 0; |
| 5413 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5414 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5415 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5416 | ((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] | 5417 | /* Destination connection ID */ |
| 5418 | if (conn->dcid.len) { |
| 5419 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5420 | *buf += conn->dcid.len; |
| 5421 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5422 | |
| 5423 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5427 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5428 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5429 | * Returns 1 if succeeded or 0 if failed. |
| 5430 | */ |
| 5431 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5432 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5433 | { |
| 5434 | int i, ret, outlen; |
| 5435 | EVP_CIPHER_CTX *ctx; |
| 5436 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5437 | * and at most 4 bytes for the packet number |
| 5438 | */ |
| 5439 | unsigned char mask[5] = {0}; |
| 5440 | |
| 5441 | ret = 0; |
| 5442 | ctx = EVP_CIPHER_CTX_new(); |
| 5443 | if (!ctx) |
| 5444 | return 0; |
| 5445 | |
| 5446 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5447 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5448 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5449 | goto out; |
| 5450 | |
| 5451 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5452 | for (i = 0; i < pnlen; i++) |
| 5453 | pn[i] ^= mask[i + 1]; |
| 5454 | |
| 5455 | ret = 1; |
| 5456 | |
| 5457 | out: |
| 5458 | EVP_CIPHER_CTX_free(ctx); |
| 5459 | |
| 5460 | return ret; |
| 5461 | } |
| 5462 | |
| 5463 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5464 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5465 | * Return 1 if succeeded, 0 if not. |
| 5466 | */ |
| 5467 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5468 | { |
| 5469 | size_t room, ack_delay_sz; |
| 5470 | |
| 5471 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5472 | /* A frame is made of 1 byte for the frame type. */ |
| 5473 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5474 | 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] | 5475 | return 0; |
| 5476 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5477 | 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] | 5478 | } |
| 5479 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5480 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5481 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5482 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5483 | * with the number of bytes already present in this buffer which must be taken |
| 5484 | * into an account for the Length packet field value. <headlen> is the number of |
| 5485 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5486 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5487 | * 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] | 5488 | * 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] | 5489 | * 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] | 5490 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5491 | 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] | 5492 | size_t room, size_t *len, size_t headlen, |
| 5493 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5494 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5495 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5496 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5497 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5498 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5499 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5500 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5501 | return 0; |
| 5502 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5503 | /* If we are not probing we must take into an account the congestion |
| 5504 | * control window. |
| 5505 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5506 | if (!qel->pktns->tx.pto_probe) { |
| 5507 | size_t remain = quic_path_prep_data(qc->path); |
| 5508 | |
| 5509 | if (headlen > remain) |
| 5510 | return 0; |
| 5511 | |
| 5512 | room = QUIC_MIN(room, remain - headlen); |
| 5513 | } |
| 5514 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5515 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5516 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5517 | |
| 5518 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 5519 | * returned by this function only if at least one frame could be built |
| 5520 | * in the switch/case block. |
| 5521 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5522 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5523 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5524 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5525 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5526 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5527 | break; |
| 5528 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5529 | switch (cf->type) { |
| 5530 | case QUIC_FT_CRYPTO: |
| 5531 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5532 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5533 | /* Compute the length of this CRYPTO frame header */ |
| 5534 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5535 | /* Compute the data length of this CRyPTO frame. */ |
| 5536 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5537 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5538 | 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] | 5539 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5540 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5541 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5542 | /* CRYPTO frame length. */ |
| 5543 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5544 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5545 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5546 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5547 | * length and the length of this length. |
| 5548 | */ |
| 5549 | *len += flen; |
| 5550 | room -= flen; |
| 5551 | if (dlen == cf->crypto.len) { |
| 5552 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5553 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5554 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5555 | } |
| 5556 | else { |
| 5557 | struct quic_frame *new_cf; |
| 5558 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5559 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5560 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5561 | 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] | 5562 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5563 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5564 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5565 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5566 | new_cf->type = QUIC_FT_CRYPTO; |
| 5567 | new_cf->crypto.len = dlen; |
| 5568 | new_cf->crypto.offset = cf->crypto.offset; |
| 5569 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5570 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5571 | /* Consume <dlen> bytes of the current frame. */ |
| 5572 | cf->crypto.len -= dlen; |
| 5573 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5574 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5575 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5576 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5577 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5578 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 5579 | struct eb64_node *node = NULL; |
| 5580 | struct qc_stream_desc *stream_desc = NULL; |
| 5581 | struct quic_stream *strm = &cf->stream; |
| 5582 | |
| 5583 | /* As this frame has been already lost, ensure the stream is always |
| 5584 | * available or the range of this frame is not consumed before |
| 5585 | * resending it. |
| 5586 | */ |
| 5587 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 5588 | if (!node) { |
| 5589 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5590 | LIST_DELETE(&cf->list); |
| 5591 | pool_free(pool_head_quic_frame, cf); |
| 5592 | continue; |
| 5593 | } |
| 5594 | |
| 5595 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5596 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 5597 | TRACE_PROTO("ignored frame frame in already acked range", |
| 5598 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5599 | LIST_DELETE(&cf->list); |
| 5600 | pool_free(pool_head_quic_frame, cf); |
| 5601 | continue; |
| 5602 | } |
| 5603 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 5604 | strm->offset.key = stream_desc->ack_offset; |
| 5605 | TRACE_PROTO("updated partially acked frame", |
| 5606 | QUIC_EV_CONN_PRSAFRM, qc, strm); |
| 5607 | } |
| 5608 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5609 | /* Note that these frames are accepted in short packets only without |
| 5610 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5611 | * 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] | 5612 | * |
| 5613 | * 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] | 5614 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5615 | */ |
| 5616 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5617 | ((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] | 5618 | /* Compute the data length of this STREAM frame. */ |
| 5619 | avail_room = room - hlen - *len; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5620 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5621 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5622 | if ((ssize_t)avail_room <= 0) |
| 5623 | continue; |
| 5624 | |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5625 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5626 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5627 | if (dlen > cf->stream.len) { |
| 5628 | dlen = cf->stream.len; |
| 5629 | } |
| 5630 | dlen_sz = quic_int_getsize(dlen); |
| 5631 | flen = hlen + dlen_sz + dlen; |
| 5632 | } |
| 5633 | else { |
| 5634 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5635 | flen = hlen + dlen; |
| 5636 | } |
| 5637 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5638 | 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] | 5639 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5640 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5641 | /* Add the STREAM data length and its encoded length to the packet |
| 5642 | * length and the length of this length. |
| 5643 | */ |
| 5644 | *len += flen; |
| 5645 | room -= flen; |
| 5646 | if (dlen == cf->stream.len) { |
| 5647 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5648 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5649 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5650 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5651 | /* The MUX stream might be released at this |
| 5652 | * stage. This can most notably happen on |
| 5653 | * retransmission. |
| 5654 | */ |
| 5655 | if (qc->mux_state == QC_MUX_READY && |
| 5656 | !cf->stream.stream->release) { |
| 5657 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5658 | cf->stream.len, |
| 5659 | cf->stream.offset.key); |
| 5660 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5661 | } |
| 5662 | else { |
| 5663 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5664 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5665 | |
| 5666 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5667 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5668 | 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] | 5669 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5670 | } |
| 5671 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5672 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5673 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5674 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5675 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5676 | new_cf->stream.id = cf->stream.id; |
| 5677 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5678 | new_cf->stream.offset = cf->stream.offset; |
| 5679 | new_cf->stream.len = dlen; |
| 5680 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5681 | /* FIN bit reset */ |
| 5682 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5683 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5684 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5685 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5686 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5687 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5688 | b_size(cf->stream.buf), |
| 5689 | (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] | 5690 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5691 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5692 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5693 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5694 | /* The MUX stream might be released at this |
| 5695 | * stage. This can most notably happen on |
| 5696 | * retransmission. |
| 5697 | */ |
| 5698 | if (qc->mux_state == QC_MUX_READY && |
| 5699 | !cf->stream.stream->release) { |
| 5700 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5701 | new_cf->stream.len, |
| 5702 | new_cf->stream.offset.key); |
| 5703 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5704 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5705 | |
| 5706 | /* TODO the MUX is notified about the frame sending via |
| 5707 | * previous qcc_streams_sent_done call. However, the |
| 5708 | * sending can fail later, for example if the sendto |
| 5709 | * system call returns an error. As the MUX has been |
| 5710 | * notified, the transport layer is responsible to |
| 5711 | * bufferize and resent the announced data later. |
| 5712 | */ |
| 5713 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5714 | break; |
| 5715 | |
| 5716 | default: |
| 5717 | flen = qc_frm_len(cf); |
| 5718 | BUG_ON(!flen); |
| 5719 | if (flen > room) |
| 5720 | continue; |
| 5721 | |
| 5722 | *len += flen; |
| 5723 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5724 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5725 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5726 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5727 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5728 | |
| 5729 | /* 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] | 5730 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5733 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5734 | } |
| 5735 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5736 | /* 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] | 5737 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5738 | * 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] | 5739 | * filling the buffer with as much frames as possible from <frms> list of |
| 5740 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5741 | * 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] | 5742 | * 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] | 5743 | * having returned from this function. |
| 5744 | * 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] | 5745 | * number field in this packet. <pn_len> will also have the packet number |
| 5746 | * length as value. |
| 5747 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5748 | * 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] | 5749 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5750 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5751 | size_t dglen, struct quic_tx_packet *pkt, |
| 5752 | 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] | 5753 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5754 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5755 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5756 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5757 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5758 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5759 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5760 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5761 | 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] | 5762 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5763 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5764 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5765 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5766 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5767 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5768 | /* Length field value with CRYPTO frames if present. */ |
| 5769 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5770 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5771 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5772 | * buffer to respect the congestion controller window. |
| 5773 | * 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] | 5774 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5775 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5776 | size_t path_room; |
| 5777 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5778 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5779 | if (end - beg > path_room) |
| 5780 | end = beg + path_room; |
| 5781 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5782 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5783 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5784 | * length field if any. |
| 5785 | */ |
| 5786 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5787 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5788 | goto no_room; |
| 5789 | |
| 5790 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5791 | 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] | 5792 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5793 | *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] | 5794 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5795 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5796 | !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] | 5797 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5798 | !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] | 5799 | goto no_room; |
| 5800 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5801 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5802 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5803 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5804 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5805 | /* Build an ACK frame if required. */ |
| 5806 | ack_frm_len = 0; |
Frédéric Lécaille | 337108e | 2022-04-25 10:38:27 +0200 | [diff] [blame] | 5807 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 5808 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5809 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 5810 | ack_frm.tx_ack.arngs = arngs; |
| 5811 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 5812 | qel->pktns->tx.ack_delay = |
| 5813 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 5814 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 5815 | } |
| 5816 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5817 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5818 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5819 | * that from here, we do not know if we will have to send a PING frame. |
| 5820 | * This will be decided after having computed the ack-eliciting frames |
| 5821 | * to be added to this packet. |
| 5822 | */ |
| 5823 | 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] | 5824 | if (!ack_frm_len) |
| 5825 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5826 | } |
| 5827 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5828 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5829 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5830 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5831 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5832 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5833 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5834 | 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] | 5835 | /* Initialize the length of the frames built below to <len>. |
| 5836 | * If any frame could be successfully built by qc_build_frms(), |
| 5837 | * we will have len_frms > len. |
| 5838 | */ |
| 5839 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5840 | if (!qc_build_frms(&frm_list, frms, |
| 5841 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5842 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5843 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 5844 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 5845 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5846 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5847 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5848 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5849 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5850 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5851 | * for the encryption tag. It must be taken into an account for the length |
| 5852 | * of this packet. |
| 5853 | */ |
| 5854 | if (len_frms) |
| 5855 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5856 | else |
| 5857 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5858 | /* CONNECTION_CLOSE frame */ |
| 5859 | if (cc) { |
| 5860 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5861 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5862 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5863 | len += qc_frm_len(&cc_frm); |
| 5864 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5865 | add_ping_frm = 0; |
| 5866 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5867 | len_sz = quic_int_getsize(len); |
| 5868 | /* Add this packet size to <dglen> */ |
| 5869 | dglen += head_len + len_sz + len; |
| 5870 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5871 | /* This is a maximum padding size */ |
| 5872 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5873 | /* The length field value is of this packet is <len> + <padding_len> |
| 5874 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5875 | * <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] | 5876 | * sizes from <padding_len>. |
| 5877 | */ |
| 5878 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5879 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5880 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5881 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5882 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5883 | /* 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] | 5884 | add_ping_frm = 1; |
| 5885 | len += 1; |
| 5886 | } |
| 5887 | /* 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] | 5888 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5889 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5890 | } |
| 5891 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5892 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5893 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5894 | |
| 5895 | /* Packet number field address. */ |
| 5896 | *buf_pn = pos; |
| 5897 | |
| 5898 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5899 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5900 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5901 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5902 | if (ack_frm_len) { |
| 5903 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 5904 | goto no_room; |
| 5905 | |
| 5906 | 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] | 5907 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5908 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5909 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5910 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5911 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5912 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5913 | unsigned char *spos = pos; |
| 5914 | |
| 5915 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5916 | ssize_t room = end - pos; |
| 5917 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5918 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5919 | /* TODO: this should not have happened if qc_build_frms() |
| 5920 | * had correctly computed and sized the frames to be |
| 5921 | * added to this packet. Note that <cf> was added |
| 5922 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 5923 | */ |
| 5924 | LIST_DELETE(&cf->list); |
| 5925 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5926 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5927 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5928 | |
| 5929 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5930 | quic_tx_packet_refinc(pkt); |
| 5931 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | /* Build a PING frame if needed. */ |
| 5936 | if (add_ping_frm) { |
| 5937 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5938 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5939 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5940 | } |
| 5941 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5942 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5943 | if (cc) { |
| 5944 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 5945 | goto no_room; |
| 5946 | |
| 5947 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 5948 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5949 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5950 | /* Build a PADDING frame if needed. */ |
| 5951 | if (padding_len) { |
| 5952 | frm.type = QUIC_FT_PADDING; |
| 5953 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5954 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5955 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5956 | } |
| 5957 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5958 | /* If this packet is ack-eliciting and we are probing let's |
| 5959 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5960 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5961 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5962 | qel->pktns->tx.pto_probe) |
| 5963 | qel->pktns->tx.pto_probe--; |
| 5964 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5965 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5966 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5967 | 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] | 5968 | |
| 5969 | return 1; |
| 5970 | |
| 5971 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5972 | /* 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] | 5973 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | d8b798d | 2022-04-25 17:48:40 +0200 | [diff] [blame] | 5974 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5975 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5976 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5977 | } |
| 5978 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5979 | 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] | 5980 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5981 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5982 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5983 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5984 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5985 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5986 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5987 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5988 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5989 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5990 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5991 | } |
| 5992 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5993 | /* 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] | 5994 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 5995 | * of prebuilt frames. |
| 5996 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5997 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5998 | * -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] | 5999 | * XXX NOTE XXX |
| 6000 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6001 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6002 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6003 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6004 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6005 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6006 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6007 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6008 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6009 | { |
| 6010 | /* The pointer to the packet number field. */ |
| 6011 | unsigned char *buf_pn; |
| 6012 | unsigned char *beg, *end, *payload; |
| 6013 | int64_t pn; |
| 6014 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6015 | struct quic_tls_ctx *tls_ctx; |
| 6016 | struct quic_tx_packet *pkt; |
| 6017 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6018 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6019 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6020 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6021 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6022 | 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] | 6023 | *err = -2; |
| 6024 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6025 | } |
| 6026 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6027 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6028 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6029 | pn_len = 0; |
| 6030 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6031 | |
| 6032 | pn = qel->pktns->tx.next_pn + 1; |
| 6033 | 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] | 6034 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6035 | *err = -1; |
| 6036 | goto err; |
| 6037 | } |
| 6038 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6039 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6040 | payload = buf_pn + pn_len; |
| 6041 | payload_len = end - payload; |
| 6042 | aad_len = payload - beg; |
| 6043 | |
| 6044 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6045 | 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] | 6046 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6047 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6048 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6049 | |
| 6050 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6051 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6052 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 6053 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6054 | 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] | 6055 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6056 | goto err; |
| 6057 | } |
| 6058 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6059 | /* Consume a packet number */ |
| 6060 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6061 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 6062 | 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] | 6063 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6064 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6065 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6066 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6067 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6068 | /* 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] | 6069 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6070 | pkt->in_flight_len = pkt->len; |
| 6071 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6072 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6073 | /* Always reset this flags */ |
| 6074 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6075 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6076 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6077 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6078 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6079 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6080 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6081 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6082 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6083 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6084 | |
| 6085 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6086 | /* TODO: what about the frames which have been built |
| 6087 | * for this packet. |
| 6088 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6089 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6090 | 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] | 6091 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6092 | } |
| 6093 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6094 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6095 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6096 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6097 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6098 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6099 | */ |
| 6100 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6101 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6102 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6103 | |
| 6104 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6105 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6106 | |
| 6107 | es->events |= event_type; |
| 6108 | qcc->subs = es; |
| 6109 | |
| 6110 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6111 | 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] | 6112 | |
| 6113 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6114 | 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] | 6115 | |
| 6116 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6120 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6121 | * subscribe() call. It always returns zero. |
| 6122 | */ |
| 6123 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6124 | { |
| 6125 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6126 | } |
| 6127 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6128 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 6129 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6130 | */ |
| 6131 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 6132 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 6133 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6134 | |
| 6135 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 6136 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6137 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6138 | if (*xprt_ctx) |
| 6139 | goto out; |
| 6140 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6141 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6142 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6143 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 6144 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6145 | |
| 6146 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6147 | } |
| 6148 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6149 | /* Start the QUIC transport layer */ |
| 6150 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 6151 | { |
| 6152 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 6153 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6154 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6155 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6156 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 6157 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 6158 | /* prepare a CONNECTION_CLOSE frame */ |
| 6159 | qc->err_code = QC_ERR_APPLICATION_ERROR; |
| 6160 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 6161 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | /* mux-quic can now be considered ready. */ |
| 6165 | qc->mux_state = QC_MUX_READY; |
| 6166 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6167 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 6168 | return 1; |
| 6169 | } |
| 6170 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6171 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 6172 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6173 | 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] | 6174 | return NULL; |
| 6175 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6176 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6177 | } |
| 6178 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6179 | /* transport-layer operations for QUIC connections. */ |
| 6180 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 6181 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6182 | .subscribe = quic_conn_subscribe, |
| 6183 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6184 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6185 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6186 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 6187 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6188 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6189 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6190 | .name = "QUIC", |
| 6191 | }; |
| 6192 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6193 | static void __quic_conn_init(void) |
| 6194 | { |
| 6195 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 6196 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 6197 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6198 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6199 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6200 | static void __quic_conn_deinit(void) |
| 6201 | { |
| 6202 | BIO_meth_free(ha_quic_meth); |
| 6203 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6204 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6205 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6206 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 6207 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 6208 | * 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] | 6209 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6210 | 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] | 6211 | { |
| 6212 | unsigned char *pos; |
| 6213 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6214 | struct quic_dghdlr *dghdlr = ctx; |
| 6215 | struct quic_dgram *dgram; |
| 6216 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6217 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6218 | 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] | 6219 | pos = dgram->buf; |
| 6220 | end = pos + dgram->len; |
| 6221 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6222 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6223 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6224 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 6225 | if (!pkt) |
| 6226 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6227 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6228 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6229 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 6230 | qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6231 | first_pkt = 0; |
| 6232 | pos += pkt->len; |
| 6233 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6234 | } while (pos < end); |
| 6235 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6236 | /* Increasing the received bytes counter by the UDP datagram length |
| 6237 | * if this datagram could be associated to a connection. |
| 6238 | */ |
| 6239 | if (dgram->qc) |
| 6240 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 6241 | |
| 6242 | /* Mark this datagram as consumed */ |
| 6243 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6244 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6245 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6246 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6247 | |
| 6248 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6249 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6250 | } |
| 6251 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6252 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 6253 | * Returns 1 if succeeded, 0 if not. |
| 6254 | */ |
| 6255 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 6256 | unsigned char **dcid, size_t *dcid_len) |
| 6257 | { |
| 6258 | int long_header; |
| 6259 | size_t minlen, skip; |
| 6260 | |
| 6261 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 6262 | goto err; |
| 6263 | |
| 6264 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 6265 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 6266 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6267 | 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] | 6268 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6269 | goto err; |
| 6270 | |
| 6271 | buf += skip; |
| 6272 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 6273 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 6274 | goto err; |
| 6275 | |
| 6276 | *dcid = buf; |
| 6277 | |
| 6278 | return 1; |
| 6279 | |
| 6280 | err: |
| 6281 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 6282 | return 0; |
| 6283 | } |
| 6284 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 6285 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 6286 | * correct datagram handler. |
| 6287 | * Return 1 if a correct datagram could be found, 0 if not. |
| 6288 | */ |
| 6289 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 6290 | struct sockaddr_storage *saddr, |
| 6291 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6292 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6293 | struct quic_dgram *dgram; |
| 6294 | unsigned char *dcid; |
| 6295 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 6296 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6297 | |
| 6298 | 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] | 6299 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6300 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 6301 | 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] | 6302 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6303 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6304 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 6305 | cid_tid = quic_get_cid_tid(dcid); |
| 6306 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6307 | /* All the members must be initialized! */ |
| 6308 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6309 | dgram->buf = buf; |
| 6310 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6311 | dgram->dcid = dcid; |
| 6312 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6313 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6314 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6315 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 6316 | 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] | 6317 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 6318 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6319 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6320 | return 1; |
| 6321 | |
| 6322 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6323 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6324 | } |
| 6325 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 6326 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 6327 | void qc_notify_close(struct quic_conn *qc) |
| 6328 | { |
| 6329 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 6330 | return; |
| 6331 | |
| 6332 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 6333 | |
| 6334 | /* wake up the MUX */ |
| 6335 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 6336 | qc->conn->mux->wake(qc->conn); |
| 6337 | } |
| 6338 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6339 | /* Function to automatically activate QUIC traces on stdout. |
| 6340 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 6341 | * Main use for now is in the docker image for QUIC interop testing. |
| 6342 | */ |
| 6343 | static void quic_init_stdout_traces(void) |
| 6344 | { |
| 6345 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 6346 | trace_quic.sink = sink_find("stdout"); |
| 6347 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 6348 | trace_quic.state = TRACE_STATE_RUNNING; |
| 6349 | #endif |
| 6350 | } |
| 6351 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 6352 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6353 | /* |
| 6354 | * Local variables: |
| 6355 | * c-indent-level: 8 |
| 6356 | * c-basic-offset: 8 |
| 6357 | * End: |
| 6358 | */ |