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> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 25 | #include <import/ebmbtree.h> |
| 26 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | #include <haproxy/buf-t.h> |
| 28 | #include <haproxy/compat.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/debug.h> |
| 31 | #include <haproxy/tools.h> |
| 32 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 33 | |
| 34 | #include <haproxy/connection.h> |
| 35 | #include <haproxy/fd.h> |
| 36 | #include <haproxy/freq_ctr.h> |
| 37 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 39 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 41 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 47 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 48 | #include <haproxy/cbuf.h> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 49 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 51 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 52 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 53 | #include <haproxy/task.h> |
| 54 | #include <haproxy/trace.h> |
| 55 | #include <haproxy/xprt_quic.h> |
| 56 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 57 | /* list of supported QUIC versions by this implementation */ |
| 58 | static int quic_supported_version[] = { |
| 59 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 60 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 61 | |
| 62 | /* placeholder, do not add entry after this */ |
| 63 | 0x0 |
| 64 | }; |
| 65 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 66 | /* This is the values of some QUIC transport parameters when absent. |
| 67 | * Should be used to initialize any transport parameters (local or remote) |
| 68 | * before updating them with customized values. |
| 69 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 71 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 72 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 73 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 74 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* trace source and events */ |
| 78 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 79 | const struct trace_source *src, |
| 80 | const struct ist where, const struct ist func, |
| 81 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 82 | |
| 83 | static const struct trace_event quic_trace_events[] = { |
| 84 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 85 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 86 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 90 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 91 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 92 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 95 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 96 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 101 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 102 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 103 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 106 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 107 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 108 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 109 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 110 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 111 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 112 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 113 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 114 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 115 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 116 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 117 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 118 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 119 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 120 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 121 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 122 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 123 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 124 | { /* end */ } |
| 125 | }; |
| 126 | |
| 127 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 128 | /* arg1 */ { /* already used by the connection */ }, |
| 129 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 130 | /* arg3 */ { }, |
| 131 | /* arg4 */ { } |
| 132 | }; |
| 133 | |
| 134 | static const struct name_desc quic_trace_decoding[] = { |
| 135 | #define QUIC_VERB_CLEAN 1 |
| 136 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 137 | { /* end */ } |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | struct trace_source trace_quic = { |
| 142 | .name = IST("quic"), |
| 143 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 144 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 145 | .default_cb = quic_trace, |
| 146 | .known_events = quic_trace_events, |
| 147 | .lockon_args = quic_trace_lockon_args, |
| 148 | .decoding = quic_trace_decoding, |
| 149 | .report_events = ~0, /* report everything by default */ |
| 150 | }; |
| 151 | |
| 152 | #define TRACE_SOURCE &trace_quic |
| 153 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 154 | |
| 155 | static BIO_METHOD *ha_quic_meth; |
| 156 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 157 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 158 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 159 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 160 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 161 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | DECLARE_POOL(pool_head_quic_connection_id, |
| 163 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 164 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 165 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 166 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 167 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 168 | DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 169 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 170 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 171 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 172 | DECLARE_STATIC_POOL(pool_head_quic_conn_stream, "qc_stream_desc", sizeof(struct qc_stream_desc)); |
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 | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 556 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 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 | |
| 615 | } |
| 616 | |
| 617 | /* 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] | 618 | 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] | 619 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 620 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 621 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 622 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 623 | return 1; |
| 624 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 625 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 626 | 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] | 627 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 628 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 629 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 630 | return 1; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 636 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 637 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 638 | 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] | 639 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 640 | struct quic_pktns *pktns; |
| 641 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 642 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 643 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 644 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 645 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 646 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 647 | pktns = quic_loss_pktns(qc); |
| 648 | if (tick_isset(pktns->tx.loss_time)) { |
| 649 | qc->timer = pktns->tx.loss_time; |
| 650 | goto out; |
| 651 | } |
| 652 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 653 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 654 | * cancelled for a server which reached the anti-amplification limit. |
| 655 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 656 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 657 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 658 | 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] | 659 | qc->timer = TICK_ETERNITY; |
| 660 | goto out; |
| 661 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 662 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 663 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 664 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 665 | /* Timer cancellation. */ |
| 666 | qc->timer = TICK_ETERNITY; |
| 667 | goto out; |
| 668 | } |
| 669 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 670 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 671 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 672 | if (tick_isset(pto)) |
| 673 | qc->timer = pto; |
| 674 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 675 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 676 | if (tick_is_expired(qc->timer, now_ms)) { |
| 677 | 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] | 678 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 679 | } |
| 680 | else { |
| 681 | 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] | 682 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 683 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 684 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 685 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 688 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 689 | * connection. |
| 690 | * Return 1 if succeeded, 0 if not. |
| 691 | */ |
| 692 | static int quic_tls_key_update(struct quic_conn *qc) |
| 693 | { |
| 694 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 695 | struct quic_tls_secrets *rx, *tx; |
| 696 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 697 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 698 | |
| 699 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 700 | rx = &tls_ctx->rx; |
| 701 | tx = &tls_ctx->tx; |
| 702 | nxt_rx = &qc->ku.nxt_rx; |
| 703 | nxt_tx = &qc->ku.nxt_tx; |
| 704 | |
| 705 | /* Prepare new RX secrets */ |
| 706 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 707 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 708 | 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] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 713 | nxt_rx->key, nxt_rx->keylen, |
| 714 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 715 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 716 | 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] | 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* Prepare new TX secrets */ |
| 721 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 722 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 723 | 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] | 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 728 | nxt_tx->key, nxt_tx->keylen, |
| 729 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 730 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 731 | 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] | 732 | return 0; |
| 733 | } |
| 734 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 735 | if (nxt_rx->ctx) { |
| 736 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 737 | nxt_rx->ctx = NULL; |
| 738 | } |
| 739 | |
| 740 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 741 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | if (nxt_tx->ctx) { |
| 746 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 747 | nxt_tx->ctx = NULL; |
| 748 | } |
| 749 | |
| 750 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 751 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 752 | return 0; |
| 753 | } |
| 754 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 755 | return 1; |
| 756 | } |
| 757 | |
| 758 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 759 | * Must be used after having updated them. |
| 760 | * Always succeeds. |
| 761 | */ |
| 762 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 763 | { |
| 764 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 765 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 766 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 767 | |
| 768 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 769 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 770 | curr_secret = tls_ctx->rx.secret; |
| 771 | curr_iv = tls_ctx->rx.iv; |
| 772 | curr_key = tls_ctx->rx.key; |
| 773 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 774 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 775 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 776 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 777 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 778 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 779 | 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] | 780 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 781 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 782 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 783 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 784 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 785 | qc->ku.prv_rx.secret = curr_secret; |
| 786 | qc->ku.prv_rx.iv = curr_iv; |
| 787 | qc->ku.prv_rx.key = curr_key; |
| 788 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 789 | |
| 790 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 791 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 792 | curr_secret = tls_ctx->tx.secret; |
| 793 | curr_iv = tls_ctx->tx.iv; |
| 794 | curr_key = tls_ctx->tx.key; |
| 795 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 796 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 797 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 798 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 799 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 800 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 801 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 802 | qc->ku.nxt_tx.secret = curr_secret; |
| 803 | qc->ku.nxt_tx.iv = curr_iv; |
| 804 | qc->ku.nxt_tx.key = curr_key; |
| 805 | } |
| 806 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 807 | #ifndef OPENSSL_IS_BORINGSSL |
| 808 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 809 | const uint8_t *read_secret, |
| 810 | const uint8_t *write_secret, size_t secret_len) |
| 811 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 812 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 813 | 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] | 814 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 815 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 816 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 817 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 818 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 819 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 820 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 821 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 822 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 823 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 824 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 825 | 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] | 826 | goto err; |
| 827 | } |
| 828 | |
| 829 | rx = &tls_ctx->rx; |
| 830 | tx = &tls_ctx->tx; |
| 831 | |
| 832 | rx->aead = tx->aead = tls_aead(cipher); |
| 833 | rx->md = tx->md = tls_md(cipher); |
| 834 | rx->hp = tx->hp = tls_hp(cipher); |
| 835 | |
| 836 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 837 | 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] | 838 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 839 | 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] | 840 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 843 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 844 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 845 | goto err; |
| 846 | } |
| 847 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 848 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 849 | * listener. Note that a listener derives only RX secrets for this |
| 850 | * level. |
| 851 | */ |
| 852 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 853 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 854 | |
| 855 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 856 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 857 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 858 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 859 | 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] | 860 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 861 | 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] | 862 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 865 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 866 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 867 | goto err; |
| 868 | } |
| 869 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 870 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 871 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 872 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 873 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 874 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 875 | /* 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] | 876 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 877 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 878 | 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] | 879 | goto err; |
| 880 | } |
| 881 | |
| 882 | memcpy(rx->secret, read_secret, secret_len); |
| 883 | rx->secretlen = secret_len; |
| 884 | memcpy(tx->secret, write_secret, secret_len); |
| 885 | tx->secretlen = secret_len; |
| 886 | /* Initialize all the secret keys lengths */ |
| 887 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 888 | /* Prepare the next key update */ |
| 889 | if (!quic_tls_key_update(qc)) |
| 890 | goto err; |
| 891 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 892 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 893 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 894 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 895 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 896 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 897 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 898 | |
| 899 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 900 | 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] | 901 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 902 | } |
| 903 | #else |
| 904 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 905 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 906 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 907 | */ |
| 908 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 909 | const SSL_CIPHER *cipher, |
| 910 | const uint8_t *secret, size_t secret_len) |
| 911 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 912 | 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] | 913 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 914 | &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] | 915 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 916 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 917 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 918 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 919 | goto out; |
| 920 | } |
| 921 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 922 | tls_ctx->rx.aead = tls_aead(cipher); |
| 923 | tls_ctx->rx.md = tls_md(cipher); |
| 924 | tls_ctx->rx.hp = tls_hp(cipher); |
| 925 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 926 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 927 | goto err; |
| 928 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 929 | 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] | 930 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 931 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 932 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 933 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 934 | 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] | 935 | goto err; |
| 936 | } |
| 937 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 938 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 939 | const unsigned char *buf; |
| 940 | size_t buflen; |
| 941 | |
| 942 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 943 | if (!buflen) |
| 944 | goto err; |
| 945 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 946 | 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] | 947 | goto err; |
| 948 | } |
| 949 | |
| 950 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 951 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 952 | 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] | 953 | |
| 954 | return 1; |
| 955 | |
| 956 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 957 | 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] | 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 962 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 963 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 964 | */ |
| 965 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 966 | const SSL_CIPHER *cipher, |
| 967 | const uint8_t *secret, size_t secret_len) |
| 968 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 969 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 970 | 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] | 971 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 972 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 973 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 974 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 975 | goto out; |
| 976 | } |
| 977 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 978 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 979 | goto err; |
| 980 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 981 | tls_ctx->tx.aead = tls_aead(cipher); |
| 982 | tls_ctx->tx.md = tls_md(cipher); |
| 983 | tls_ctx->tx.hp = tls_hp(cipher); |
| 984 | |
| 985 | 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] | 986 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 987 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 988 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 989 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 990 | 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] | 991 | goto err; |
| 992 | } |
| 993 | |
| 994 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 995 | 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] | 996 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 997 | return 1; |
| 998 | |
| 999 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1000 | 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] | 1001 | return 0; |
| 1002 | } |
| 1003 | #endif |
| 1004 | |
| 1005 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1006 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1007 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1008 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 1009 | * store all the data. |
| 1010 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1011 | */ |
| 1012 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 1013 | const unsigned char *data, size_t len) |
| 1014 | { |
| 1015 | struct quic_crypto_buf **qcb; |
| 1016 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1017 | size_t cf_offset, cf_len, *nb_buf; |
| 1018 | unsigned char *pos; |
| 1019 | |
| 1020 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1021 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1022 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1023 | cf_len = len; |
| 1024 | |
| 1025 | while (len) { |
| 1026 | size_t to_copy, room; |
| 1027 | |
| 1028 | pos = (*qcb)->data + (*qcb)->sz; |
| 1029 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1030 | to_copy = len > room ? room : len; |
| 1031 | if (to_copy) { |
| 1032 | memcpy(pos, data, to_copy); |
| 1033 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1034 | qel->tx.crypto.sz += to_copy; |
| 1035 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1036 | len -= to_copy; |
| 1037 | data += to_copy; |
| 1038 | } |
| 1039 | else { |
| 1040 | struct quic_crypto_buf **tmp; |
| 1041 | |
| 1042 | tmp = realloc(qel->tx.crypto.bufs, |
| 1043 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1044 | if (tmp) { |
| 1045 | qel->tx.crypto.bufs = tmp; |
| 1046 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1047 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1048 | if (!*qcb) |
| 1049 | return 0; |
| 1050 | |
| 1051 | (*qcb)->sz = 0; |
| 1052 | ++*nb_buf; |
| 1053 | } |
| 1054 | else { |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1061 | * have been buffered. |
| 1062 | */ |
| 1063 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1064 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1065 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1066 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1067 | /* There is at most one CRYPTO frame in this packet number |
| 1068 | * space. Let's look for it. |
| 1069 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1070 | 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] | 1071 | if (frm->type != QUIC_FT_CRYPTO) |
| 1072 | continue; |
| 1073 | |
| 1074 | /* Found */ |
| 1075 | found = frm; |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | if (found) { |
| 1080 | found->crypto.len += cf_len; |
| 1081 | } |
| 1082 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1083 | frm = pool_alloc(pool_head_quic_frame); |
| 1084 | if (!frm) |
| 1085 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1086 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1087 | frm->type = QUIC_FT_CRYPTO; |
| 1088 | frm->crypto.offset = cf_offset; |
| 1089 | frm->crypto.len = cf_len; |
| 1090 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1091 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1092 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | return len == 0; |
| 1096 | } |
| 1097 | |
| 1098 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1099 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1100 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1101 | { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1102 | qc->err_code = QC_ERR_CRYPTO_ERROR | alert; |
| 1103 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1104 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1107 | /* Set the application for <qc> QUIC connection. |
| 1108 | * Return 1 if succeeded, 0 if not. |
| 1109 | */ |
| 1110 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1111 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1112 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1113 | qc->app_ops = &h3_ops; |
| 1114 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1115 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1116 | else |
| 1117 | return 0; |
| 1118 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1119 | return 1; |
| 1120 | } |
| 1121 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1122 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1123 | * wants to provide the QUIC layer with CRYPTO data. |
| 1124 | * Returns 1 if succeeded, 0 if not. |
| 1125 | */ |
| 1126 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1127 | const uint8_t *data, size_t len) |
| 1128 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1129 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1130 | enum quic_tls_enc_level tel; |
| 1131 | struct quic_enc_level *qel; |
| 1132 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1133 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1134 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1135 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1136 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1137 | goto out; |
| 1138 | } |
| 1139 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1140 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1141 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1142 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1143 | goto err; |
| 1144 | } |
| 1145 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1146 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1147 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1148 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1149 | goto err; |
| 1150 | } |
| 1151 | |
| 1152 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1153 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1154 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1155 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1156 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1157 | return 1; |
| 1158 | |
| 1159 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1160 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | int ha_quic_flush_flight(SSL *ssl) |
| 1165 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1166 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1167 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1168 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1169 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1170 | |
| 1171 | return 1; |
| 1172 | } |
| 1173 | |
| 1174 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1175 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1176 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1177 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1178 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1179 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1180 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1181 | return 1; |
| 1182 | } |
| 1183 | |
| 1184 | /* QUIC TLS methods */ |
| 1185 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1186 | #ifdef OPENSSL_IS_BORINGSSL |
| 1187 | .set_read_secret = ha_set_rsec, |
| 1188 | .set_write_secret = ha_set_wsec, |
| 1189 | #else |
| 1190 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1191 | #endif |
| 1192 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1193 | .flush_flight = ha_quic_flush_flight, |
| 1194 | .send_alert = ha_quic_send_alert, |
| 1195 | }; |
| 1196 | |
| 1197 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1198 | * Returns an error count. |
| 1199 | */ |
| 1200 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1201 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1202 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1203 | int cfgerr = 0; |
| 1204 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1205 | long options = |
| 1206 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1207 | SSL_OP_SINGLE_ECDH_USE | |
| 1208 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1209 | SSL_CTX *ctx; |
| 1210 | |
| 1211 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1212 | bind_conf->initial_ctx = ctx; |
| 1213 | |
| 1214 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1215 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1216 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1217 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1218 | |
| 1219 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1220 | #ifdef OPENSSL_IS_BORINGSSL |
| 1221 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1222 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1223 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1224 | if (bind_conf->ssl_conf.early_data) { |
| 1225 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 1226 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1227 | } |
| 1228 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1229 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1230 | #else |
| 1231 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1232 | #endif |
| 1233 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1234 | #endif |
| 1235 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1236 | |
| 1237 | return cfgerr; |
| 1238 | } |
| 1239 | |
| 1240 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1241 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1242 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1243 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1244 | */ |
| 1245 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1246 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1247 | { |
| 1248 | uint64_t expected_pn = largest_pn + 1; |
| 1249 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1250 | uint64_t pn_hwin = pn_win / 2; |
| 1251 | uint64_t pn_mask = pn_win - 1; |
| 1252 | uint64_t candidate_pn; |
| 1253 | |
| 1254 | |
| 1255 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1256 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1257 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1258 | candidate_pn + pn_hwin <= expected_pn) |
| 1259 | return candidate_pn + pn_win; |
| 1260 | |
| 1261 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1262 | return candidate_pn - pn_win; |
| 1263 | |
| 1264 | return candidate_pn; |
| 1265 | } |
| 1266 | |
| 1267 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1268 | * cryptographic context. |
| 1269 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1270 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1271 | * <end> points to one byte past the end of this packet. |
| 1272 | * Returns 1 if succeeded, 0 if not. |
| 1273 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1274 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1275 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1276 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1277 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1278 | { |
| 1279 | int ret, outlen, i, pnlen; |
| 1280 | uint64_t packet_number; |
| 1281 | uint32_t truncated_pn = 0; |
| 1282 | unsigned char mask[5] = {0}; |
| 1283 | unsigned char *sample; |
| 1284 | EVP_CIPHER_CTX *cctx; |
| 1285 | unsigned char *hp_key; |
| 1286 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1287 | /* Check there is enough data in this packet. */ |
| 1288 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1289 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | cctx = EVP_CIPHER_CTX_new(); |
| 1294 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1295 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | ret = 0; |
| 1300 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1301 | |
| 1302 | hp_key = tls_ctx->rx.hp_key; |
| 1303 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1304 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1305 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1306 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1307 | goto out; |
| 1308 | } |
| 1309 | |
| 1310 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1311 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1312 | for (i = 0; i < pnlen; i++) { |
| 1313 | pn[i] ^= mask[i + 1]; |
| 1314 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1315 | } |
| 1316 | |
| 1317 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1318 | /* Store remaining information for this unprotected header */ |
| 1319 | pkt->pn = packet_number; |
| 1320 | pkt->pnl = pnlen; |
| 1321 | |
| 1322 | ret = 1; |
| 1323 | |
| 1324 | out: |
| 1325 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1326 | |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1331 | * address, with <payload_len> as payload length, <aad> as address of |
| 1332 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1333 | * context. |
| 1334 | * Returns 1 if succeeded, 0 if not. |
| 1335 | */ |
| 1336 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1337 | unsigned char *aad, size_t aad_len, uint64_t pn, |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1338 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1339 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1340 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1341 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1342 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1343 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1344 | |
| 1345 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1346 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1347 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1351 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1352 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1353 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1357 | |
| 1358 | err: |
| 1359 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1360 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1361 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1365 | * Returns 1 if succeeded, 0 if not. |
| 1366 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1367 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1368 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1369 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1370 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1371 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1372 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1373 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1374 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1375 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1376 | |
| 1377 | kp_changed = 0; |
| 1378 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1379 | /* The two tested bits are not at the same position, |
| 1380 | * this is why they are first both inversed. |
| 1381 | */ |
| 1382 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1383 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1384 | /* The lowest packet number of a previous key phase |
| 1385 | * cannot be null if it really stores previous key phase |
| 1386 | * secrets. |
| 1387 | */ |
| 1388 | if (!pkt->qc->ku.prv_rx.pn) |
| 1389 | return 0; |
| 1390 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1391 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1392 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1393 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1394 | } |
| 1395 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1396 | /* Next key phase */ |
| 1397 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1398 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1399 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1400 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1404 | |
| 1405 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1406 | return 0; |
| 1407 | |
| 1408 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1409 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1410 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1411 | if (!ret) |
| 1412 | return 0; |
| 1413 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1414 | /* Update the keys only if the packet decryption succeeded. */ |
| 1415 | if (kp_changed) { |
| 1416 | quic_tls_rotate_keys(pkt->qc); |
| 1417 | /* Toggle the Key Phase bit */ |
| 1418 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1419 | /* Store the lowest packet number received for the current key phase */ |
| 1420 | tls_ctx->rx.pn = pkt->pn; |
| 1421 | /* Prepare the next key update */ |
| 1422 | if (!quic_tls_key_update(pkt->qc)) |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1426 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1427 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1428 | |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 1432 | /* Free the stream descriptor <stream> buffer. This function should be used |
| 1433 | * when all its data have been acknowledged. If the stream was released by the |
| 1434 | * upper layer, the stream descriptor will be freed. |
| 1435 | * |
| 1436 | * Returns 0 if the stream was not freed else non-zero. |
| 1437 | */ |
| 1438 | static int qc_stream_desc_free(struct qc_stream_desc *stream) |
| 1439 | { |
| 1440 | b_free(&stream->buf); |
| 1441 | offer_buffers(NULL, 1); |
| 1442 | |
| 1443 | if (stream->release) { |
| 1444 | eb64_delete(&stream->by_id); |
| 1445 | pool_free(pool_head_quic_conn_stream, stream); |
| 1446 | |
| 1447 | return 1; |
| 1448 | } |
| 1449 | |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1453 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1454 | * |
| 1455 | * 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] | 1456 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1457 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1458 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1459 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1460 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1461 | struct eb64_node *frm_node; |
| 1462 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1463 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1464 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1465 | while (frm_node) { |
| 1466 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1467 | struct quic_frame *frm; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1468 | |
| 1469 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1470 | if (strm->offset.key > stream->ack_offset) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1471 | break; |
| 1472 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1473 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1474 | qc, strm, stream); |
| 1475 | |
| 1476 | if (strm->offset.key + strm->len > stream->ack_offset) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1477 | const size_t diff = strm->offset.key + strm->len - |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1478 | stream->ack_offset; |
| 1479 | stream->ack_offset += diff; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1480 | b_del(strm->buf, diff); |
| 1481 | ret = 1; |
| 1482 | } |
| 1483 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1484 | frm_node = eb64_next(frm_node); |
| 1485 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1486 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1487 | frm = container_of(strm, struct quic_frame, stream); |
| 1488 | LIST_DELETE(&frm->list); |
| 1489 | quic_tx_packet_refdec(frm->pkt); |
| 1490 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1491 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1492 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1493 | if (!b_data(&stream->buf)) |
| 1494 | qc_stream_desc_free(stream); |
| 1495 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1496 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1499 | /* 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] | 1500 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1501 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1502 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1503 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1504 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1505 | 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] | 1506 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1507 | switch (frm->type) { |
| 1508 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1509 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1510 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1511 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1512 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1513 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1514 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1515 | * might be freed at this stage. Use the id to do a proper |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1516 | * lookup. First search in the MUX then in the released stream |
| 1517 | * list. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1518 | * |
| 1519 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1520 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1521 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1522 | if (qc->mux_state == QC_MUX_READY) |
| 1523 | stream = qcc_get_stream(qc->qcc, strm_frm->id); |
| 1524 | if (!stream) { |
| 1525 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1526 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1527 | } |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1528 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1529 | if (!stream) { |
| 1530 | TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1531 | LIST_DELETE(&frm->list); |
| 1532 | quic_tx_packet_refdec(frm->pkt); |
| 1533 | pool_free(pool_head_quic_frame, frm); |
| 1534 | |
| 1535 | /* early return */ |
| 1536 | return; |
| 1537 | } |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1538 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1539 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1540 | if (strm_frm->offset.key <= stream->ack_offset) { |
| 1541 | if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) { |
| 1542 | const size_t diff = strm_frm->offset.key + strm_frm->len - |
| 1543 | stream->ack_offset; |
| 1544 | stream->ack_offset += diff; |
| 1545 | b_del(strm_frm->buf, diff); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1546 | stream_acked = 1; |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1547 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1548 | if (!b_data(strm_frm->buf)) { |
| 1549 | if (qc_stream_desc_free(stream)) { |
| 1550 | /* early return */ |
| 1551 | return; |
| 1552 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1553 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1556 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1557 | qc, strm_frm, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1558 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1559 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1560 | pool_free(pool_head_quic_frame, frm); |
| 1561 | } |
| 1562 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1563 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1564 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1565 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1566 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1567 | } |
| 1568 | break; |
| 1569 | default: |
| 1570 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1571 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1572 | pool_free(pool_head_quic_frame, frm); |
| 1573 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1574 | |
| 1575 | if (stream_acked) { |
| 1576 | struct qcc *qcc = qc->qcc; |
| 1577 | |
| 1578 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1579 | tasklet_wakeup(qcc->subs->tasklet); |
| 1580 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1581 | if (!qcc->subs->events) |
| 1582 | qcc->subs = NULL; |
| 1583 | } |
| 1584 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1588 | * deallocating them, and their TX frames. |
| 1589 | * Returns the last node reached to be used for the next range. |
| 1590 | * May be NULL if <largest> node could not be found. |
| 1591 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1592 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1593 | struct eb_root *pkts, |
| 1594 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1595 | struct list *newly_acked_pkts, |
| 1596 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1597 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1598 | { |
| 1599 | struct eb64_node *node; |
| 1600 | struct quic_tx_packet *pkt; |
| 1601 | |
| 1602 | if (largest_node) |
| 1603 | node = largest_node; |
| 1604 | else { |
| 1605 | node = eb64_lookup(pkts, largest); |
| 1606 | while (!node && largest > smallest) { |
| 1607 | node = eb64_lookup(pkts, --largest); |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1612 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1613 | |
| 1614 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1615 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1616 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1617 | 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] | 1618 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1619 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1620 | node = eb64_prev(node); |
| 1621 | eb64_delete(&pkt->pn_node); |
| 1622 | } |
| 1623 | |
| 1624 | return node; |
| 1625 | } |
| 1626 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1627 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1628 | * 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] | 1629 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1630 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1631 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1632 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1633 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1634 | struct quic_frame *frm, *frmbak; |
| 1635 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1636 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1637 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1638 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1639 | quic_tx_packet_refdec(frm->pkt); |
| 1640 | /* This frame is not freed but removed from its packet */ |
| 1641 | frm->pkt = NULL; |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1642 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1643 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1644 | } |
| 1645 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1646 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1647 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1648 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1649 | /* Free <pkt> TX packet and its attached frames. |
| 1650 | * This is the responsability of the caller to remove this packet of |
| 1651 | * any data structure it was possibly attached to. |
| 1652 | */ |
| 1653 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1654 | { |
| 1655 | struct quic_frame *frm, *frmbak; |
| 1656 | |
| 1657 | if (!pkt) |
| 1658 | return; |
| 1659 | |
| 1660 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1661 | LIST_DELETE(&frm->list); |
| 1662 | pool_free(pool_head_quic_frame, frm); |
| 1663 | } |
| 1664 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1665 | } |
| 1666 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1667 | /* Free the TX packets of <pkts> list */ |
| 1668 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1669 | { |
| 1670 | struct quic_tx_packet *pkt, *tmp; |
| 1671 | |
| 1672 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1673 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1674 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1675 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1679 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1680 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1681 | * updating the range which contains <largest_acked_pn>. |
| 1682 | * Never fails. |
| 1683 | */ |
| 1684 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1685 | int64_t largest_acked_pn) |
| 1686 | { |
| 1687 | struct eb64_node *ar, *next_ar; |
| 1688 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1689 | |
| 1690 | ar = eb64_first(&arngs->root); |
| 1691 | while (ar) { |
| 1692 | struct quic_arng_node *ar_node; |
| 1693 | |
| 1694 | next_ar = eb64_next(ar); |
| 1695 | ar_node = eb64_entry(&ar->node, struct quic_arng_node, first); |
| 1696 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1697 | break; |
| 1698 | |
| 1699 | if (largest_acked_pn < ar_node->last) { |
| 1700 | eb64_delete(ar); |
| 1701 | ar_node->first.key = largest_acked_pn + 1; |
| 1702 | eb64_insert(&arngs->root, ar); |
| 1703 | break; |
| 1704 | } |
| 1705 | |
| 1706 | eb64_delete(ar); |
| 1707 | pool_free(pool_head_quic_arng, ar_node); |
| 1708 | arngs->sz--; |
| 1709 | ar = next_ar; |
| 1710 | } |
| 1711 | } |
| 1712 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1713 | /* Send a packet ack event nofication for each newly acked packet of |
| 1714 | * <newly_acked_pkts> list and free them. |
| 1715 | * Always succeeds. |
| 1716 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1717 | 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] | 1718 | struct list *newly_acked_pkts) |
| 1719 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1720 | struct quic_tx_packet *pkt, *tmp; |
| 1721 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1722 | |
| 1723 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1724 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1725 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1726 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1727 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1728 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1729 | /* If this packet contained an ACK frame, proceed to the |
| 1730 | * acknowledging of range of acks from the largest acknowledged |
| 1731 | * packet number which was sent in an ACK frame by this packet. |
| 1732 | */ |
| 1733 | if (pkt->largest_acked_pn != -1) |
| 1734 | 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] | 1735 | ev.ack.acked = pkt->in_flight_len; |
| 1736 | ev.ack.time_sent = pkt->time_sent; |
| 1737 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1738 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1739 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1740 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | } |
| 1744 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1745 | /* Release all the frames attached to <pktns> packet number space */ |
| 1746 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1747 | { |
| 1748 | struct quic_frame *frm, *frmbak; |
| 1749 | |
| 1750 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1751 | LIST_DELETE(&frm->list); |
| 1752 | pool_free(pool_head_quic_frame, frm); |
| 1753 | } |
| 1754 | } |
| 1755 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1756 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1757 | * their TX frames. |
| 1758 | * Send a packet loss event to the congestion controller if |
| 1759 | * in flight packet have been lost. |
| 1760 | * Also frees the packet in <pkts> list. |
| 1761 | * Never fails. |
| 1762 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1763 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1764 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1765 | struct list *pkts, |
| 1766 | uint64_t now_us) |
| 1767 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | 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] | 1769 | uint64_t lost_bytes; |
| 1770 | |
| 1771 | lost_bytes = 0; |
| 1772 | oldest_lost = newest_lost = NULL; |
| 1773 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1774 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1775 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1776 | lost_bytes += pkt->in_flight_len; |
| 1777 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1778 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1779 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1780 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1781 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1782 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1783 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1784 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1785 | if (!oldest_lost) { |
| 1786 | oldest_lost = newest_lost = pkt; |
| 1787 | } |
| 1788 | else { |
| 1789 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1790 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1791 | newest_lost = pkt; |
| 1792 | } |
| 1793 | } |
| 1794 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1795 | if (newest_lost) { |
| 1796 | /* Sent a congestion event to the controller */ |
| 1797 | struct quic_cc_event ev = { |
| 1798 | .type = QUIC_CC_EVT_LOSS, |
| 1799 | .loss.time_sent = newest_lost->time_sent, |
| 1800 | }; |
| 1801 | |
| 1802 | quic_cc_event(&qc->path->cc, &ev); |
| 1803 | } |
| 1804 | |
| 1805 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1806 | * We must check if we are experiencing a persistent congestion. |
| 1807 | * If this is the case, the congestion controller must re-enter |
| 1808 | * slow start state. |
| 1809 | */ |
| 1810 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1811 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1812 | |
| 1813 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1814 | now_ms, qc->max_ack_delay)) |
| 1815 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1816 | } |
| 1817 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1818 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1819 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1820 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1821 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1826 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1827 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1828 | * space if any not deemed lost. |
| 1829 | * Should be called after having received an ACK frame with newly acknowledged |
| 1830 | * packets or when the the loss detection timer has expired. |
| 1831 | * Always succeeds. |
| 1832 | */ |
| 1833 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1834 | struct quic_conn *qc, |
| 1835 | struct list *lost_pkts) |
| 1836 | { |
| 1837 | struct eb_root *pkts; |
| 1838 | struct eb64_node *node; |
| 1839 | struct quic_loss *ql; |
| 1840 | unsigned int loss_delay; |
| 1841 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1842 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1843 | pkts = &pktns->tx.pkts; |
| 1844 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1845 | if (eb_is_empty(pkts)) |
| 1846 | goto out; |
| 1847 | |
| 1848 | ql = &qc->path->loss; |
| 1849 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1850 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1851 | |
| 1852 | node = eb64_first(pkts); |
| 1853 | while (node) { |
| 1854 | struct quic_tx_packet *pkt; |
| 1855 | int64_t largest_acked_pn; |
| 1856 | unsigned int loss_time_limit, time_sent; |
| 1857 | |
| 1858 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1859 | largest_acked_pn = pktns->rx.largest_acked_pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1860 | node = eb64_next(node); |
| 1861 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1862 | break; |
| 1863 | |
| 1864 | time_sent = pkt->time_sent; |
| 1865 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1866 | if (tick_is_le(time_sent, now_ms) || |
| 1867 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1868 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1869 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1870 | } |
| 1871 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1872 | if (tick_isset(pktns->tx.loss_time)) |
| 1873 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1874 | else |
| 1875 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1880 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1884 | * 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] | 1885 | * 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] | 1886 | * acked ack-eliciting packet. |
| 1887 | * Return 1, if succeeded, 0 if not. |
| 1888 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1889 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1890 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1891 | struct quic_enc_level *qel, |
| 1892 | unsigned int *rtt_sample, |
| 1893 | const unsigned char **pos, const unsigned char *end) |
| 1894 | { |
| 1895 | struct quic_ack *ack = &frm->ack; |
| 1896 | uint64_t smallest, largest; |
| 1897 | struct eb_root *pkts; |
| 1898 | struct eb64_node *largest_node; |
| 1899 | unsigned int time_sent, pkt_flags; |
| 1900 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1901 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1902 | |
| 1903 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1904 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1905 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1906 | goto err; |
| 1907 | } |
| 1908 | |
| 1909 | if (ack->first_ack_range > ack->largest_ack) { |
| 1910 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1911 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1912 | goto err; |
| 1913 | } |
| 1914 | |
| 1915 | largest = ack->largest_ack; |
| 1916 | smallest = largest - ack->first_ack_range; |
| 1917 | pkts = &qel->pktns->tx.pkts; |
| 1918 | pkt_flags = 0; |
| 1919 | largest_node = NULL; |
| 1920 | time_sent = 0; |
| 1921 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1922 | 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] | 1923 | largest_node = eb64_lookup(pkts, largest); |
| 1924 | if (!largest_node) { |
| 1925 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1926 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1927 | } |
| 1928 | else { |
| 1929 | time_sent = eb64_entry(&largest_node->node, |
| 1930 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1931 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1935 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1936 | do { |
| 1937 | uint64_t gap, ack_range; |
| 1938 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1939 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1940 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1941 | if (!ack->ack_range_num--) |
| 1942 | break; |
| 1943 | |
| 1944 | if (!quic_dec_int(&gap, pos, end)) |
| 1945 | goto err; |
| 1946 | |
| 1947 | if (smallest < gap + 2) { |
| 1948 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1949 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1950 | goto err; |
| 1951 | } |
| 1952 | |
| 1953 | largest = smallest - gap - 2; |
| 1954 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1955 | goto err; |
| 1956 | |
| 1957 | if (largest < ack_range) { |
| 1958 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1959 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1960 | goto err; |
| 1961 | } |
| 1962 | |
| 1963 | /* Do not use this node anymore. */ |
| 1964 | largest_node = NULL; |
| 1965 | /* Next range */ |
| 1966 | smallest = largest - ack_range; |
| 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 | } while (1); |
| 1971 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1972 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1973 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1974 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1978 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1979 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1980 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1981 | 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] | 1982 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1983 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1984 | if (quic_peer_validated_addr(qc)) |
| 1985 | qc->path->loss.pto_count = 0; |
| 1986 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | return 1; |
| 1991 | |
| 1992 | err: |
| 1993 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1994 | 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] | 1995 | return 0; |
| 1996 | } |
| 1997 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1998 | /* This function gives the detail of the SSL error. It is used only |
| 1999 | * if the debug mode and the verbose mode are activated. It dump all |
| 2000 | * the SSL error until the stack was empty. |
| 2001 | */ |
| 2002 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2003 | { |
| 2004 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2005 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2006 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2007 | unsigned long ret; |
| 2008 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2009 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2010 | ret = ERR_get_error(); |
| 2011 | if (!ret) |
| 2012 | return; |
| 2013 | |
| 2014 | 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] | 2015 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2020 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2021 | const char **str, int *len); |
| 2022 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2023 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2024 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2025 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2026 | * Return 1 if succeeded, 0 if not. |
| 2027 | */ |
| 2028 | 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] | 2029 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2030 | const unsigned char *data, size_t len, |
| 2031 | struct quic_rx_packet *pkt, |
| 2032 | struct quic_rx_crypto_frm *cf) |
| 2033 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2034 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2035 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2036 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2037 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2038 | qc = ctx->qc; |
| 2039 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2040 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2041 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2042 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2043 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2044 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2045 | goto err; |
| 2046 | } |
| 2047 | |
| 2048 | el->rx.crypto.offset += len; |
| 2049 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2050 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2051 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2052 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2053 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2054 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2055 | if (ssl_err != 1) { |
| 2056 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2057 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2058 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2059 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2060 | goto out; |
| 2061 | } |
| 2062 | |
| 2063 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2064 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2065 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2066 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2067 | goto err; |
| 2068 | } |
| 2069 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2070 | 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] | 2071 | |
| 2072 | /* Check the alpn could be negotiated */ |
| 2073 | if (!qc->app_ops) { |
| 2074 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2075 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2076 | goto err; |
| 2077 | } |
| 2078 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2079 | /* I/O callback switch */ |
| 2080 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2081 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2082 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2083 | /* The connection is ready to be accepted. */ |
| 2084 | quic_accept_push_qc(qc); |
| 2085 | } |
| 2086 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2087 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2088 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2089 | } else { |
| 2090 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2091 | if (ssl_err != 1) { |
| 2092 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2093 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2094 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2095 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2096 | goto out; |
| 2097 | } |
| 2098 | |
| 2099 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2100 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | goto err; |
| 2102 | } |
| 2103 | |
| 2104 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2105 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2106 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2107 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2108 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2109 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2110 | return 1; |
| 2111 | |
| 2112 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2113 | 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] | 2114 | return 0; |
| 2115 | } |
| 2116 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2117 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 2118 | * <pkt> RX packet. |
| 2119 | * Return it if succeeded, NULL if not. |
| 2120 | */ |
| 2121 | static inline |
| 2122 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 2123 | struct quic_rx_packet *pkt) |
| 2124 | { |
| 2125 | struct quic_rx_strm_frm *frm; |
| 2126 | |
| 2127 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 2128 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2129 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2130 | frm->len = stream_frm->len; |
| 2131 | frm->data = stream_frm->data; |
| 2132 | frm->pkt = pkt; |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2133 | frm->fin = stream_frm->fin; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | return frm; |
| 2137 | } |
| 2138 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2139 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
Frédéric Lécaille | 3fe7df8 | 2021-12-15 15:32:55 +0100 | [diff] [blame] | 2140 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2141 | */ |
| 2142 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 2143 | { |
| 2144 | size_t ret; |
| 2145 | |
Amaury Denoyelle | 2d2d030 | 2022-02-28 10:00:54 +0100 | [diff] [blame] | 2146 | ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len); |
| 2147 | strm_frm->len -= ret; |
| 2148 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2149 | |
| 2150 | return ret; |
| 2151 | } |
| 2152 | |
| 2153 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2154 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2155 | * If not, the STREAM frame is stored to be treated again later. |
| 2156 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2157 | * Return 1 if succeeded, 0 if not. |
| 2158 | */ |
| 2159 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2160 | struct quic_stream *strm_frm, |
| 2161 | struct quic_conn *qc) |
| 2162 | { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2163 | struct quic_rx_strm_frm *frm; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2164 | struct eb64_node *frm_node; |
| 2165 | struct qcs *qcs = NULL; |
| 2166 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2167 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2168 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2169 | strm_frm->offset.key, strm_frm->fin, |
| 2170 | (char *)strm_frm->data, &qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2171 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2172 | /* invalid or already received frame */ |
| 2173 | if (ret == 1) |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2174 | return 1; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2175 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2176 | if (ret == 2) { |
| 2177 | /* frame cannot be parsed at the moment and should be |
| 2178 | * buffered. |
| 2179 | */ |
| 2180 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2181 | if (!frm) { |
| 2182 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2183 | QUIC_EV_CONN_PSTRM, qc); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2184 | return 0; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2187 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
| 2188 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2189 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2190 | return 1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2191 | } |
| 2192 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2193 | /* Frame correctly received by the mux. |
| 2194 | * If there is buffered frame for next offset, it may be possible to |
| 2195 | * receive them now. |
| 2196 | */ |
| 2197 | frm_node = eb64_first(&qcs->rx.frms); |
| 2198 | while (frm_node) { |
| 2199 | frm = eb64_entry(&frm_node->node, |
| 2200 | struct quic_rx_strm_frm, offset_node); |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2201 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 2202 | ret = qcc_recv(qc->qcc, qcs->id, frm->len, |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2203 | frm->offset_node.key, frm->fin, |
| 2204 | (char *)frm->data, &qcs); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2205 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2206 | /* interrupt the parsing if the frame cannot be handled for the |
| 2207 | * moment only by the MUX. |
| 2208 | */ |
| 2209 | if (ret == 2) |
| 2210 | break; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2211 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2212 | /* Remove a newly received frame or an invalid one. */ |
| 2213 | frm_node = eb64_next(frm_node); |
| 2214 | eb64_delete(&frm->offset_node); |
| 2215 | quic_rx_packet_refdec(frm->pkt); |
| 2216 | pool_free(pool_head_quic_rx_strm_frm, frm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2217 | } |
| 2218 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2219 | /* Decode the received data. */ |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2220 | qcc_decode_qcs(qc->qcc, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2221 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2222 | return 1; |
| 2223 | } |
| 2224 | |
| 2225 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2226 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2227 | * If not, the STREAM frame is stored to be treated again later. |
| 2228 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2229 | * Return 1 if succeeded, 0 if not. |
| 2230 | */ |
| 2231 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2232 | struct quic_stream *strm_frm, |
| 2233 | struct quic_conn *qc) |
| 2234 | { |
| 2235 | struct qcs *strm; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2236 | struct quic_rx_strm_frm *frm; |
| 2237 | size_t strm_frm_len; |
| 2238 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2239 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2240 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2241 | 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] | 2242 | return 0; |
| 2243 | } |
| 2244 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2245 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2246 | size_t diff; |
| 2247 | |
| 2248 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2249 | TRACE_PROTO("Already received STREAM data", |
| 2250 | QUIC_EV_CONN_PSTRM, qc); |
| 2251 | goto out; |
| 2252 | } |
| 2253 | |
| 2254 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2255 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2256 | strm_frm->offset.key = strm->rx.offset; |
| 2257 | strm_frm->len -= diff; |
| 2258 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2262 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2263 | int ret; |
| 2264 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2265 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2266 | goto store_frm; |
| 2267 | |
| 2268 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2269 | * of bytes copied. |
| 2270 | */ |
| 2271 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2272 | /* Inform the application of the arrival of this new stream */ |
| 2273 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2274 | 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] | 2275 | return 0; |
| 2276 | } |
| 2277 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2278 | if (ret) |
| 2279 | qcs_notify_recv(strm); |
| 2280 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2281 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2282 | } |
| 2283 | /* Take this frame into an account for the stream flow control */ |
| 2284 | strm->rx.offset += strm_frm_len; |
| 2285 | /* It all the data were provided to the application, there is no need to |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2286 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2287 | */ |
| 2288 | if (!strm_frm->len) |
| 2289 | goto out; |
| 2290 | |
| 2291 | store_frm: |
| 2292 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2293 | if (!frm) { |
| 2294 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2295 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2299 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2300 | quic_rx_packet_refinc(pkt); |
| 2301 | |
| 2302 | out: |
| 2303 | return 1; |
| 2304 | } |
| 2305 | |
| 2306 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2307 | struct quic_stream *strm_frm, |
| 2308 | struct quic_conn *qc) |
| 2309 | { |
| 2310 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2311 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2312 | else |
| 2313 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2314 | } |
| 2315 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2316 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2317 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2318 | struct quic_conn *qc) |
| 2319 | { |
| 2320 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2321 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2322 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2323 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2324 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2325 | pkts = &qel->pktns->tx.pkts; |
| 2326 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2327 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2328 | while (node) { |
| 2329 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2330 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2331 | break; |
| 2332 | node = eb64_next(node); |
| 2333 | } |
| 2334 | |
| 2335 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2336 | return; |
| 2337 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2338 | /* When building a packet from another one, the field which may increase the |
| 2339 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2340 | */ |
| 2341 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2342 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2343 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2344 | return; |
| 2345 | } |
| 2346 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2347 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | /* Prepare a fast retransmission during handshake after a client |
| 2351 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2352 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2353 | * and resend them coalescing with others (Handshake here). |
| 2354 | * (Listener only). |
| 2355 | */ |
| 2356 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2357 | { |
| 2358 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2359 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2360 | struct quic_frame *frm, *frmbak; |
| 2361 | |
| 2362 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2363 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2364 | struct quic_enc_level *qel = iqel; |
| 2365 | struct eb_root *pkts; |
| 2366 | struct eb64_node *node; |
| 2367 | struct quic_tx_packet *pkt; |
| 2368 | struct list *tmp = &itmp; |
| 2369 | |
Frédéric Lécaille | 5cfb4ed | 2022-03-30 14:44:49 +0200 | [diff] [blame] | 2370 | /* Do not probe from a packet number space if some probing |
| 2371 | * was already asked. |
| 2372 | */ |
| 2373 | if (qel->pktns->tx.pto_probe) { |
| 2374 | qel = hqel; |
| 2375 | if (qel->pktns->tx.pto_probe) |
| 2376 | return; |
| 2377 | } |
| 2378 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2379 | start: |
| 2380 | pkt = NULL; |
| 2381 | pkts = &qel->pktns->tx.pkts; |
| 2382 | node = eb64_first(pkts); |
| 2383 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2384 | while (node) { |
| 2385 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2386 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2387 | break; |
| 2388 | node = eb64_next(node); |
| 2389 | } |
| 2390 | |
| 2391 | if (!pkt) |
| 2392 | goto end; |
| 2393 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2394 | /* When building a packet from another one, the field which may increase the |
| 2395 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2396 | */ |
| 2397 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2398 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2399 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2400 | goto end; |
| 2401 | } |
| 2402 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2403 | qel->pktns->tx.pto_probe += 1; |
| 2404 | requeue: |
| 2405 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Frédéric Lécaille | 009016c | 2022-03-30 14:58:55 +0200 | [diff] [blame] | 2406 | struct quic_frame *dup_frm; |
| 2407 | |
| 2408 | |
| 2409 | dup_frm = pool_alloc(pool_head_quic_frame); |
| 2410 | if (!dup_frm) { |
| 2411 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2412 | break; |
| 2413 | } |
| 2414 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2415 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 009016c | 2022-03-30 14:58:55 +0200 | [diff] [blame] | 2416 | *dup_frm = *frm; |
| 2417 | LIST_APPEND(tmp, &dup_frm->list); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | if (qel == iqel) { |
| 2421 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2422 | pkt = pkt->next; |
| 2423 | tmp = &htmp; |
| 2424 | hqel->pktns->tx.pto_probe += 1; |
| 2425 | goto requeue; |
| 2426 | } |
| 2427 | |
| 2428 | qel = hqel; |
| 2429 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2430 | goto start; |
| 2431 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2432 | |
| 2433 | end: |
| 2434 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2435 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2438 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2439 | * as I/O handler context and <qel> as encryption level. |
| 2440 | * Returns 1 if succeeded, 0 if failed. |
| 2441 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2442 | 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] | 2443 | struct quic_enc_level *qel) |
| 2444 | { |
| 2445 | struct quic_frame frm; |
| 2446 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2447 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2448 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2449 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2450 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2451 | /* Skip the AAD */ |
| 2452 | pos = pkt->data + pkt->aad_len; |
| 2453 | end = pkt->data + pkt->len; |
| 2454 | |
| 2455 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2456 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2457 | goto err; |
| 2458 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2459 | 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] | 2460 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2461 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2462 | break; |
| 2463 | case QUIC_FT_PING: |
| 2464 | break; |
| 2465 | case QUIC_FT_ACK: |
| 2466 | { |
| 2467 | unsigned int rtt_sample; |
| 2468 | |
| 2469 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2470 | 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] | 2471 | goto err; |
| 2472 | |
| 2473 | if (rtt_sample) { |
| 2474 | unsigned int ack_delay; |
| 2475 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2476 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2477 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2478 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2479 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2480 | 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] | 2481 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2482 | break; |
| 2483 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2484 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2485 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2486 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2487 | { |
| 2488 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2489 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2490 | 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] | 2491 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2492 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2493 | |
| 2494 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2495 | cfdebug.len = frm.crypto.len; |
| 2496 | TRACE_PROTO("CRYPTO data discarded", |
| 2497 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2498 | break; |
| 2499 | } |
| 2500 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2501 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2502 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2503 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2504 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2505 | |
| 2506 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2507 | cfdebug.len = frm.crypto.len; |
| 2508 | /* Nothing to do */ |
| 2509 | TRACE_PROTO("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 | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2511 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2512 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2513 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2514 | break; |
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 | else { |
| 2517 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2518 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 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; |
| 2523 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2524 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2525 | frm.crypto.len -= diff; |
| 2526 | frm.crypto.data += diff; |
| 2527 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2528 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2529 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2530 | |
| 2531 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2532 | /* 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] | 2533 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2534 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2535 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2536 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2537 | if (!qc_provide_cdata(qel, ctx, |
| 2538 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2539 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2540 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2541 | |
| 2542 | break; |
| 2543 | } |
| 2544 | |
| 2545 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2546 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2547 | if (!cf) { |
| 2548 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2549 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2550 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2551 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2552 | |
| 2553 | cf->offset_node.key = frm.crypto.offset; |
| 2554 | cf->len = frm.crypto.len; |
| 2555 | cf->data = frm.crypto.data; |
| 2556 | cf->pkt = pkt; |
| 2557 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2558 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2559 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2560 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2561 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2562 | { |
| 2563 | struct quic_stream *stream = &frm.stream; |
| 2564 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2565 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2566 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2567 | goto err; |
| 2568 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2569 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2570 | |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2571 | /* At the application layer the connection may have already been closed. */ |
| 2572 | if (qc->mux_state != QC_MUX_READY) |
| 2573 | break; |
| 2574 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2575 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2576 | goto err; |
| 2577 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2578 | break; |
| 2579 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2580 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2581 | if (qc->mux_state == QC_MUX_READY) { |
| 2582 | struct quic_max_data *data = &frm.max_data; |
| 2583 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2584 | } |
| 2585 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2586 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2587 | if (qc->mux_state == QC_MUX_READY) { |
| 2588 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2589 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2590 | data->max_stream_data); |
| 2591 | } |
| 2592 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2593 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2594 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2595 | case QUIC_FT_DATA_BLOCKED: |
| 2596 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2597 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2598 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2599 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2600 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2601 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2602 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2603 | break; |
| 2604 | case QUIC_FT_CONNECTION_CLOSE: |
| 2605 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2606 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2607 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2608 | /* RFC 9000 10.2. Immediate Close: |
| 2609 | * The closing and draining connection states exist to ensure |
| 2610 | * that connections close cleanly and that delayed or reordered |
| 2611 | * packets are properly discarded. These states SHOULD persist |
| 2612 | * for at least three times the current PTO interval... |
| 2613 | * |
| 2614 | * Rearm the idle timeout only one time when entering draining |
| 2615 | * state. |
| 2616 | */ |
| 2617 | qc_idle_timer_do_rearm(qc); |
| 2618 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2619 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2620 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2621 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2622 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2623 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2624 | goto err; |
| 2625 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2626 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2627 | break; |
| 2628 | default: |
| 2629 | goto err; |
| 2630 | } |
| 2631 | } |
| 2632 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2633 | /* 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] | 2634 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2635 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2636 | if (fast_retrans) |
| 2637 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2638 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2639 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2640 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2641 | * be discarded. |
| 2642 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2643 | 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] | 2644 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2645 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2646 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2647 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2648 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2649 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2650 | qc_set_timer(ctx->qc); |
| 2651 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2652 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2653 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2654 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2655 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2656 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2659 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2660 | return 1; |
| 2661 | |
| 2662 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2663 | 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] | 2664 | return 0; |
| 2665 | } |
| 2666 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2667 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2668 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2669 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2670 | * index after having marked the end of written data. This the responsability |
| 2671 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2672 | * data made of a uint16_t null field. |
| 2673 | * |
| 2674 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2675 | * ^ ^ |
| 2676 | * r w |
| 2677 | * |
| 2678 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2679 | * ^ ^ |
| 2680 | * r w |
| 2681 | |
| 2682 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2683 | * ^ |
| 2684 | * (r = w) |
| 2685 | * |
| 2686 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2687 | * ^ ^ |
| 2688 | * w r |
| 2689 | */ |
| 2690 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2691 | { |
| 2692 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2693 | |
| 2694 | /* We can reset the writer index only if in front of the reader index and |
| 2695 | * if the reader index is not null. Resetting the writer when the reader |
| 2696 | * index is null would empty the buffer. |
| 2697 | * XXX Note than the writer index cannot reach the reader index. |
| 2698 | * Only the reader index can reach the writer index. |
| 2699 | */ |
| 2700 | if (rd && rd <= cbuf->wr) { |
| 2701 | /* Mark the end of contiguous data for the reader */ |
| 2702 | write_u16(cb_wr(cbuf), 0); |
| 2703 | cb_add(cbuf, sizeof(uint16_t)); |
| 2704 | cb_wr_reset(cbuf); |
| 2705 | return 1; |
| 2706 | } |
| 2707 | |
| 2708 | return 0; |
| 2709 | } |
| 2710 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2711 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2712 | * 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] | 2713 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2714 | * This function must be called only after having built a correct datagram. |
| 2715 | * Always succeeds. |
| 2716 | */ |
| 2717 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2718 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2719 | { |
| 2720 | write_u16(cb_wr(cbuf), dglen); |
| 2721 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2722 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2723 | } |
| 2724 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2725 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2726 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2727 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2728 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2729 | */ |
| 2730 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2731 | struct quic_enc_level *qel, int cc, int probe) |
| 2732 | { |
| 2733 | unsigned int must_ack = |
| 2734 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2735 | |
| 2736 | /* Do not build any more packet if the TX secrets are not available or |
| 2737 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2738 | * and if there is no more packets to send upon PTO expiration |
| 2739 | * and if there is no more ack-eliciting frames to send or in flight |
| 2740 | * congestion control limit is reached for prepared data |
| 2741 | */ |
| 2742 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2743 | (!cc && !probe && !must_ack && |
| 2744 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2745 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2746 | return 0; |
| 2747 | } |
| 2748 | |
| 2749 | return 1; |
| 2750 | } |
| 2751 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2752 | /* 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] | 2753 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2754 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2755 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2756 | * 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] | 2757 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2758 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2759 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2760 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2761 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2762 | { |
| 2763 | struct quic_enc_level *qel; |
| 2764 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2765 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2766 | struct quic_tx_packet *pkt; |
| 2767 | size_t total; |
| 2768 | size_t dg_headlen; |
| 2769 | |
| 2770 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2771 | /* Each datagram is prepended with its length followed by the |
| 2772 | * address of the first packet in the datagram. |
| 2773 | */ |
| 2774 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2775 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2776 | total = 0; |
| 2777 | start: |
| 2778 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2779 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2780 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2781 | * to ensure there is enough room to mark the end of prepared |
| 2782 | * contiguous data with a zero length. |
| 2783 | */ |
| 2784 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2785 | 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] | 2786 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2787 | |
| 2788 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2789 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2790 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2791 | /* We do not probe if an immediate close was asked */ |
| 2792 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2793 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2794 | |
| 2795 | 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] | 2796 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2797 | |
| 2798 | /* Leave room for the datagram header */ |
| 2799 | pos += dg_headlen; |
| 2800 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2801 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2802 | } |
| 2803 | else { |
| 2804 | end = pos + qc->path->mtu; |
| 2805 | } |
| 2806 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2807 | 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] | 2808 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2809 | switch (err) { |
| 2810 | case -2: |
| 2811 | goto err; |
| 2812 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2813 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2814 | * MTU, we are here because of the congestion control window. There is |
| 2815 | * no need to try to reuse this buffer. |
| 2816 | */ |
| 2817 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2818 | default: |
| 2819 | break; |
| 2820 | } |
| 2821 | |
| 2822 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2823 | if (!pkt) |
| 2824 | goto err; |
| 2825 | |
| 2826 | total += pkt->len; |
| 2827 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2828 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2829 | } |
| 2830 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2831 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2832 | 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] | 2833 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2834 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2835 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | out: |
| 2839 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2840 | return total; |
| 2841 | |
| 2842 | err: |
| 2843 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2844 | return -1; |
| 2845 | } |
| 2846 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2847 | /* 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] | 2848 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2849 | * several packets in the same datagram. A header made of two fields is added |
| 2850 | * to each datagram: the datagram length followed by the address of the first |
| 2851 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2852 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2853 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2854 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2855 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2856 | enum quic_tls_enc_level tel, |
| 2857 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2858 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2859 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2860 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2861 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2862 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2863 | /* length of datagrams */ |
| 2864 | uint16_t dglen; |
| 2865 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2866 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2867 | /* Each datagram is prepended with its length followed by the |
| 2868 | * address of the first packet in the datagram. |
| 2869 | */ |
| 2870 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2871 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2872 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2873 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2874 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2875 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2876 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2877 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2878 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2879 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2880 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2881 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2882 | * to ensure there is enough room to mark the end of prepared |
| 2883 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2884 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2885 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2886 | first_pkt = prv_pkt = NULL; |
| 2887 | 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] | 2888 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2889 | enum quic_pkt_type pkt_type; |
| 2890 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2891 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2892 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2893 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2894 | /* We do not probe if an immediate close was asked */ |
| 2895 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2896 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2897 | |
| 2898 | if (!qc_may_build_pkt(qc, &qel->pktns->tx.frms, qel, cc, probe)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2899 | if (prv_pkt) |
| 2900 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2901 | /* Let's select the next encryption level */ |
| 2902 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2903 | tel = next_tel; |
| 2904 | qel = &qc->els[tel]; |
| 2905 | /* Build a new datagram */ |
| 2906 | prv_pkt = NULL; |
| 2907 | continue; |
| 2908 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2909 | break; |
| 2910 | } |
| 2911 | |
| 2912 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2913 | if (!prv_pkt) { |
| 2914 | /* Leave room for the datagram header */ |
| 2915 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2916 | 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] | 2917 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2918 | } |
| 2919 | else { |
| 2920 | end = pos + qc->path->mtu; |
| 2921 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2924 | cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2925 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2926 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2927 | case -2: |
| 2928 | goto err; |
| 2929 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2930 | /* If there was already a correct packet present, set the |
| 2931 | * current datagram as prepared into <cbuf>. |
| 2932 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2933 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2934 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2935 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2936 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2937 | break; |
| 2938 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2939 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2940 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2941 | if (!cur_pkt) |
| 2942 | goto err; |
| 2943 | |
| 2944 | total += cur_pkt->len; |
| 2945 | /* keep trace of the first packet in the datagram */ |
| 2946 | if (!first_pkt) |
| 2947 | first_pkt = cur_pkt; |
| 2948 | /* Attach the current one to the previous one */ |
| 2949 | if (prv_pkt) |
| 2950 | prv_pkt->next = cur_pkt; |
| 2951 | /* Let's say we have to build a new dgram */ |
| 2952 | prv_pkt = NULL; |
| 2953 | dglen += cur_pkt->len; |
| 2954 | /* Client: discard the Initial encryption keys as soon as |
| 2955 | * a handshake packet could be built. |
| 2956 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2957 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2958 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2959 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2960 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2961 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2962 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2963 | 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] | 2964 | 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] | 2965 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2966 | } |
| 2967 | /* If the data for the current encryption level have all been sent, |
| 2968 | * select the next level. |
| 2969 | */ |
| 2970 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | d6570e1 | 2022-03-29 17:41:57 +0200 | [diff] [blame] | 2971 | (LIST_ISEMPTY(&qel->pktns->tx.frms) && !qel->pktns->tx.pto_probe)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2972 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2973 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2974 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2975 | tel = next_tel; |
| 2976 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2977 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2978 | /* If there is data for the next level, do not |
| 2979 | * consume a datagram. |
| 2980 | */ |
| 2981 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2982 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2983 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2984 | /* If we have to build a new datagram, set the current datagram as |
| 2985 | * prepared into <cbuf>. |
| 2986 | */ |
| 2987 | if (!prv_pkt) { |
| 2988 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2989 | first_pkt = NULL; |
| 2990 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2991 | padding = 0; |
| 2992 | } |
| 2993 | 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] | 2994 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2995 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2996 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | stop_build: |
| 3001 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3002 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3003 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3004 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3005 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3009 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3010 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3011 | |
| 3012 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3013 | 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] | 3014 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /* 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] | 3018 | * 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] | 3019 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3020 | 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] | 3021 | { |
| 3022 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3023 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3025 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3026 | cbuf = qr->cbuf; |
| 3027 | while (cb_contig_data(cbuf)) { |
| 3028 | unsigned char *pos; |
| 3029 | struct buffer tmpbuf = { }; |
| 3030 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3031 | uint16_t dglen; |
| 3032 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3033 | unsigned int time_sent; |
| 3034 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3035 | pos = cb_rd(cbuf); |
| 3036 | dglen = read_u16(pos); |
| 3037 | /* End of prepared datagrams. |
| 3038 | * Reset the reader index only if in front of the writer index. |
| 3039 | */ |
| 3040 | if (!dglen) { |
| 3041 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3042 | |
| 3043 | if (wr && wr < cbuf->rd) { |
| 3044 | cb_rd_reset(cbuf); |
| 3045 | continue; |
| 3046 | } |
| 3047 | break; |
| 3048 | } |
| 3049 | |
| 3050 | pos += sizeof dglen; |
| 3051 | first_pkt = read_ptr(pos); |
| 3052 | pos += sizeof first_pkt; |
| 3053 | tmpbuf.area = (char *)pos; |
| 3054 | tmpbuf.size = tmpbuf.data = dglen; |
| 3055 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3056 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3057 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3058 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3059 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3060 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3061 | qc->tx.bytes += tmpbuf.data; |
| 3062 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3063 | |
| 3064 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3065 | pkt->time_sent = time_sent; |
| 3066 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3067 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3068 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3069 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3070 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3071 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3072 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3073 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3074 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3075 | qc_notify_close(qc); |
| 3076 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3077 | /* RFC 9000 10.2. Immediate Close: |
| 3078 | * The closing and draining connection states exist to ensure |
| 3079 | * that connections close cleanly and that delayed or reordered |
| 3080 | * packets are properly discarded. These states SHOULD persist |
| 3081 | * for at least three times the current PTO interval... |
| 3082 | * |
| 3083 | * Rearm the idle timeout only one time when entering closing |
| 3084 | * state. |
| 3085 | */ |
| 3086 | qc_idle_timer_do_rearm(qc); |
| 3087 | if (qc->timer_task) { |
| 3088 | task_destroy(qc->timer_task); |
| 3089 | qc->timer_task = NULL; |
| 3090 | } |
| 3091 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3092 | qc->path->in_flight += pkt->in_flight_len; |
| 3093 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3094 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3095 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3096 | 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] | 3097 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3098 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3099 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3100 | } |
| 3101 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3102 | |
| 3103 | return 1; |
| 3104 | } |
| 3105 | |
| 3106 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3107 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3108 | * a HANDSHAKE_DONE frame. |
| 3109 | * Return 1 if succeeded, 0 if not. |
| 3110 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3111 | 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] | 3112 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3113 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3114 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3115 | struct quic_frame *frm, *frmbak; |
| 3116 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3117 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3118 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3119 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3120 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3121 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3122 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3123 | if (!frm) |
| 3124 | return 0; |
| 3125 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3126 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3127 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3128 | } |
| 3129 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3130 | first = 1; |
| 3131 | max = qc->tx.params.active_connection_id_limit; |
| 3132 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3133 | struct quic_connection_id *cid; |
| 3134 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3135 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3136 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3137 | goto err; |
| 3138 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3139 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3140 | if (!cid) |
| 3141 | goto err; |
| 3142 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3143 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3144 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3145 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3146 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3147 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3148 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3149 | |
| 3150 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3151 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3152 | |
| 3153 | return 1; |
| 3154 | |
| 3155 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3156 | /* free the frames */ |
| 3157 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3158 | pool_free(pool_head_quic_frame, frm); |
| 3159 | |
| 3160 | node = eb64_first(&qc->cids); |
| 3161 | while (node) { |
| 3162 | struct quic_connection_id *cid; |
| 3163 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3164 | |
| 3165 | cid = eb64_entry(&node->node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3166 | if (cid->seq_num.key >= max) |
| 3167 | break; |
| 3168 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3169 | if (cid->seq_num.key < first) |
| 3170 | continue; |
| 3171 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3172 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3173 | ebmb_delete(&cid->node); |
| 3174 | eb64_delete(&cid->seq_num); |
| 3175 | pool_free(pool_head_quic_connection_id, cid); |
| 3176 | } |
| 3177 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3182 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3183 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3184 | struct eb64_node *n; |
| 3185 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3186 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3187 | n = eb64_first(&arngs->root); |
| 3188 | while (n) { |
| 3189 | struct eb64_node *next; |
| 3190 | |
| 3191 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 3192 | next = eb64_next(n); |
| 3193 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3194 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3195 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3196 | } |
| 3197 | } |
| 3198 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3199 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3200 | * descending order. |
| 3201 | */ |
| 3202 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3203 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3204 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3205 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | |
| 3209 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3210 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3211 | * 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] | 3212 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3213 | 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] | 3214 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3215 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3216 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3217 | last = eb64_last(&arngs->root); |
| 3218 | while (last && arngs->enc_sz > limit) { |
| 3219 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3220 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3221 | prev = eb64_prev(last); |
| 3222 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3223 | return 0; |
| 3224 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3225 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 3226 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 3227 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3228 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3229 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3230 | --arngs->sz; |
| 3231 | eb64_delete(last); |
| 3232 | pool_free(pool_head_quic_arng, last); |
| 3233 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | return 1; |
| 3237 | } |
| 3238 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3239 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3240 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3241 | { |
| 3242 | struct eb64_node *node, *next; |
| 3243 | struct quic_arng_node *ar, *ar_next; |
| 3244 | |
| 3245 | node = eb64_last(&arngs->root); |
| 3246 | if (!node) |
| 3247 | return; |
| 3248 | |
| 3249 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3250 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3251 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3252 | |
| 3253 | while ((next = eb64_prev(node))) { |
| 3254 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 3255 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3256 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3257 | node = next; |
| 3258 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3259 | } |
| 3260 | } |
| 3261 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3262 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3263 | * 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] | 3264 | */ |
| 3265 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3266 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3267 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3268 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3269 | struct quic_arng_node *new_ar; |
| 3270 | |
| 3271 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3272 | if (new_ar) { |
| 3273 | new_ar->first.key = ar->first; |
| 3274 | new_ar->last = ar->last; |
| 3275 | eb64_insert(&arngs->root, &new_ar->first); |
| 3276 | arngs->sz++; |
| 3277 | } |
| 3278 | |
| 3279 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | /* 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] | 3283 | * 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] | 3284 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3285 | * |
| 3286 | * Descending order |
| 3287 | * -------------> |
| 3288 | * range1 range2 |
| 3289 | * ..........|--------|..............|--------| |
| 3290 | * ^ ^ ^ ^ |
| 3291 | * | | | | |
| 3292 | * last1 first1 last2 first2 |
| 3293 | * ..........+--------+--------------+--------+...... |
| 3294 | * diff1 gap12 diff2 |
| 3295 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3296 | * To encode the previous list of ranges we must encode integers as follows in |
| 3297 | * descending order: |
| 3298 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3299 | * with diff1 = last1 - first1 |
| 3300 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3301 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3302 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3303 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3304 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3305 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3306 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3307 | struct eb64_node *le; |
| 3308 | struct quic_arng_node *new_node; |
| 3309 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3310 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3311 | new = NULL; |
| 3312 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3313 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3314 | if (!new_node) |
| 3315 | return 0; |
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 | goto out; |
| 3318 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3319 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3320 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3321 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3322 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3323 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3324 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3325 | |
| 3326 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3327 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3328 | else { |
| 3329 | struct quic_arng_node *le_ar = |
| 3330 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3331 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3332 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3333 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3334 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3335 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3336 | if (le_ar->last + 1 >= ar->first) { |
| 3337 | le_ar->last = ar->last; |
| 3338 | new = le; |
| 3339 | new_node = le_ar; |
| 3340 | } |
| 3341 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3342 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3343 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3344 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3345 | |
| 3346 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3347 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3348 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3349 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3350 | /* Verify that the new inserted node does not overlap the nodes |
| 3351 | * which follow it. |
| 3352 | */ |
| 3353 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3354 | struct eb64_node *next; |
| 3355 | struct quic_arng_node *next_node; |
| 3356 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3357 | while ((next = eb64_next(new))) { |
| 3358 | next_node = |
| 3359 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3360 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3361 | break; |
| 3362 | |
| 3363 | if (next_node->last > new_node->last) |
| 3364 | new_node->last = next_node->last; |
| 3365 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3366 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3367 | /* Decrement the size of these ranges. */ |
| 3368 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3369 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3372 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3373 | quic_arngs_set_enc_sz(arngs); |
| 3374 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3375 | return 1; |
| 3376 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3377 | /* Remove the header protection of packets at <el> encryption level. |
| 3378 | * Always succeeds. |
| 3379 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3380 | 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] | 3381 | { |
| 3382 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3383 | struct quic_rx_packet *pqpkt; |
| 3384 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3385 | struct quic_enc_level *app_qel; |
| 3386 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3387 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3388 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3389 | /* 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] | 3390 | 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] | 3391 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3392 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3393 | goto out; |
| 3394 | } |
| 3395 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3396 | 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] | 3397 | 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] | 3398 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3399 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3400 | 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] | 3401 | /* XXX TO DO XXX */ |
| 3402 | } |
| 3403 | else { |
| 3404 | /* The AAD includes the packet number field */ |
| 3405 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3406 | /* Store the packet into the tree of packets to decrypt. */ |
| 3407 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3408 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3409 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3410 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3411 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3412 | 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] | 3413 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3414 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3415 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3419 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3423 | * Return 1 if succeeded, 0 if not. |
| 3424 | */ |
| 3425 | 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] | 3426 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3427 | { |
| 3428 | struct eb64_node *node; |
| 3429 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | node = eb64_first(&el->rx.crypto.frms); |
| 3431 | while (node) { |
| 3432 | struct quic_rx_crypto_frm *cf; |
| 3433 | |
| 3434 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3435 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3436 | break; |
| 3437 | |
| 3438 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3439 | goto err; |
| 3440 | |
| 3441 | node = eb64_next(node); |
| 3442 | quic_rx_packet_refdec(cf->pkt); |
| 3443 | eb64_delete(&cf->offset_node); |
| 3444 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3445 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3446 | return 1; |
| 3447 | |
| 3448 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3449 | 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] | 3450 | return 0; |
| 3451 | } |
| 3452 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3453 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3454 | * 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] | 3455 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3456 | * Return 1 if succeeded, 0 if not. |
| 3457 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3458 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3459 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3460 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3461 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3462 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3463 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3464 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3465 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3466 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3467 | qel = cur_el; |
| 3468 | next_tel: |
| 3469 | if (!qel) |
| 3470 | goto out; |
| 3471 | |
| 3472 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3473 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3474 | while (node) { |
| 3475 | struct quic_rx_packet *pkt; |
| 3476 | |
| 3477 | pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3478 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3479 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3480 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3481 | /* Drop the packet */ |
| 3482 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3483 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3484 | } |
| 3485 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3486 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3487 | /* Drop the packet */ |
| 3488 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3489 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3490 | } |
| 3491 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3492 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3493 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3494 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3495 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3496 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3497 | qc_idle_timer_rearm(qc, 1); |
| 3498 | } |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3499 | if (pkt->pn > largest_pn) |
| 3500 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3501 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3502 | 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] | 3503 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3504 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3508 | eb64_delete(&pkt->pn_node); |
| 3509 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3510 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3511 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3512 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3513 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3514 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) |
| 3515 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3516 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3517 | goto err; |
| 3518 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3519 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3520 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3521 | qel = next_el; |
| 3522 | goto next_tel; |
| 3523 | } |
| 3524 | |
| 3525 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3526 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3527 | return 1; |
| 3528 | |
| 3529 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3530 | 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] | 3531 | return 0; |
| 3532 | } |
| 3533 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3534 | /* Check if it's possible to remove header protection for packets related to |
| 3535 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3536 | * |
| 3537 | * Return true if the operation is possible else false. |
| 3538 | */ |
| 3539 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3540 | { |
| 3541 | enum quic_tls_enc_level tel; |
| 3542 | |
| 3543 | if (!qel) |
| 3544 | return 0; |
| 3545 | |
| 3546 | tel = ssl_to_quic_enc_level(qel->level); |
| 3547 | |
| 3548 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3549 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3550 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3551 | return 0; |
| 3552 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3553 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3554 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3555 | return 0; |
| 3556 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3557 | /* 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] | 3558 | 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] | 3559 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3560 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3561 | |
| 3562 | return 1; |
| 3563 | } |
| 3564 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3565 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | c2f561c | 2022-02-25 17:46:07 +0100 | [diff] [blame] | 3566 | int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3567 | { |
| 3568 | int ret; |
| 3569 | struct qring *qr; |
| 3570 | |
| 3571 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3572 | if (!qr) |
| 3573 | /* Never happens */ |
| 3574 | return 1; |
| 3575 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3576 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3577 | if (ret == -1) |
| 3578 | goto err; |
| 3579 | else if (ret == 0) |
| 3580 | goto out; |
| 3581 | |
| 3582 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3583 | goto err; |
| 3584 | |
| 3585 | out: |
| 3586 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3587 | return 1; |
| 3588 | |
| 3589 | err: |
| 3590 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3591 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3592 | return 0; |
| 3593 | } |
| 3594 | |
| 3595 | /* QUIC connection packet handler task (post handshake) */ |
| 3596 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3597 | { |
| 3598 | struct ssl_sock_ctx *ctx; |
| 3599 | struct quic_conn *qc; |
| 3600 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3601 | |
| 3602 | |
| 3603 | ctx = context; |
| 3604 | qc = ctx->qc; |
| 3605 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3606 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3607 | 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] | 3608 | |
| 3609 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3610 | qc_rm_hp_pkts(qc, qel); |
| 3611 | |
| 3612 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3613 | goto err; |
| 3614 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3615 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3616 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3617 | goto out; |
| 3618 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3619 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3620 | goto err; |
| 3621 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3622 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3623 | return t; |
| 3624 | |
| 3625 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3626 | 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] | 3627 | return t; |
| 3628 | } |
| 3629 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3630 | /* QUIC connection packet handler task. */ |
| 3631 | 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] | 3632 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3633 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3634 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3635 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3636 | enum quic_tls_enc_level tel, next_tel; |
| 3637 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3638 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3639 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3641 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3642 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3643 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3644 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3645 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3646 | TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3647 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3648 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3649 | /* The I/O handler has been woken up by the dgram listener |
| 3650 | * after the anti-amplification was reached. |
| 3651 | */ |
| 3652 | qc_set_timer(qc); |
| 3653 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3654 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3655 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3656 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3657 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3658 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3659 | 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] | 3660 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3661 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3662 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3663 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3664 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3665 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3666 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3667 | } |
| 3668 | 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] | 3669 | goto err; |
| 3670 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3671 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3672 | 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] | 3673 | |
| 3674 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3675 | /* Treat packets waiting for header packet protection decryption */ |
| 3676 | 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] | 3677 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3678 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3679 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3680 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3681 | 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] | 3682 | goto err; |
| 3683 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3684 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3685 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3686 | goto out; |
| 3687 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3688 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3689 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3690 | qel = next_qel; |
| 3691 | next_qel = NULL; |
| 3692 | goto next_level; |
| 3693 | } |
| 3694 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3695 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3696 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3697 | 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] | 3698 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3699 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3700 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3701 | 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] | 3702 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3703 | /* Discard the Handshake keys. */ |
| 3704 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3705 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3706 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3707 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3708 | 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] | 3709 | 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] | 3710 | } |
| 3711 | |
| 3712 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3713 | /* There may be remaining handshake to build (acks) */ |
| 3714 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3715 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3718 | if (!qr) |
| 3719 | 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] | 3720 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3721 | * be considered. |
| 3722 | */ |
| 3723 | 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] | 3724 | goto err; |
| 3725 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3726 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3727 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3728 | else if (ret == 0) |
| 3729 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3730 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3731 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3732 | goto err; |
| 3733 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3734 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3735 | /* Check if there is something to do for the next level. |
| 3736 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3737 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3738 | (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] | 3739 | (!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] | 3740 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3741 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3742 | goto next_level; |
| 3743 | } |
| 3744 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3745 | out: |
| 3746 | if (qr) |
| 3747 | 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] | 3748 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3749 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3750 | |
| 3751 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3752 | if (qr) |
| 3753 | 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] | 3754 | 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] | 3755 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3756 | } |
| 3757 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3758 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3759 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3760 | { |
| 3761 | int i; |
| 3762 | |
| 3763 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3764 | if (qel->tx.crypto.bufs[i]) { |
| 3765 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3766 | qel->tx.crypto.bufs[i] = NULL; |
| 3767 | } |
| 3768 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3769 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3773 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3774 | * Returns 1 if succeeded, 0 if not. |
| 3775 | */ |
| 3776 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3777 | enum quic_tls_enc_level level) |
| 3778 | { |
| 3779 | struct quic_enc_level *qel; |
| 3780 | |
| 3781 | qel = &qc->els[level]; |
| 3782 | qel->level = quic_to_ssl_enc_level(level); |
| 3783 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3784 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3785 | 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] | 3786 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3787 | |
| 3788 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3789 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3790 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3791 | qel->rx.crypto.offset = 0; |
| 3792 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3793 | |
| 3794 | /* Allocate only one buffer. */ |
| 3795 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3796 | if (!qel->tx.crypto.bufs) |
| 3797 | goto err; |
| 3798 | |
| 3799 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3800 | if (!qel->tx.crypto.bufs[0]) |
| 3801 | goto err; |
| 3802 | |
| 3803 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3804 | qel->tx.crypto.nb_buf = 1; |
| 3805 | |
| 3806 | qel->tx.crypto.sz = 0; |
| 3807 | qel->tx.crypto.offset = 0; |
| 3808 | |
| 3809 | return 1; |
| 3810 | |
| 3811 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3812 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3813 | return 0; |
| 3814 | } |
| 3815 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3816 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 3817 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3818 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3819 | * This function must only be called by the thread responsible of the quic_conn |
| 3820 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3821 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3822 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3823 | { |
| 3824 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3825 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3826 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3827 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3828 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 3829 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 3830 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 3831 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3832 | /* free remaining stream descriptors */ |
| 3833 | node = eb64_first(&qc->streams_by_id); |
| 3834 | while (node) { |
| 3835 | struct qc_stream_desc *stream; |
| 3836 | |
| 3837 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 3838 | node = eb64_next(node); |
| 3839 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 3840 | /* all streams attached to the quic-conn are released, so |
| 3841 | * qc_stream_desc_free will liberate the stream instance. |
| 3842 | */ |
| 3843 | BUG_ON(!stream->release); |
| 3844 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3845 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3846 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3847 | if (qc->idle_timer_task) { |
| 3848 | task_destroy(qc->idle_timer_task); |
| 3849 | qc->idle_timer_task = NULL; |
| 3850 | } |
| 3851 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3852 | if (qc->timer_task) { |
| 3853 | task_destroy(qc->timer_task); |
| 3854 | qc->timer_task = NULL; |
| 3855 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3856 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3857 | /* remove the connection from receiver cids trees */ |
| 3858 | ebmb_delete(&qc->odcid_node); |
| 3859 | ebmb_delete(&qc->scid_node); |
| 3860 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3861 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3862 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3863 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3864 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3865 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3866 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3867 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3868 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3869 | quic_tls_ku_free(qc); |
| 3870 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3871 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3872 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 3876 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 3877 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3878 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3879 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 3880 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3881 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3882 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3883 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3884 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3885 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3886 | 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] | 3887 | } |
| 3888 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 3889 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3890 | { |
| 3891 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3892 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3893 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3894 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3895 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3896 | /* Next application data can be dropped. */ |
| 3897 | qc->mux_state = QC_MUX_RELEASED; |
| 3898 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 3899 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 3900 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 3901 | quic_conn_release(qc); |
| 3902 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 3903 | return; |
| 3904 | } |
| 3905 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3906 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3907 | } |
| 3908 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3909 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3910 | 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] | 3911 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3912 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3913 | struct quic_conn *qc; |
| 3914 | struct quic_pktns *pktns; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3915 | int i; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3916 | |
| 3917 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3918 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3919 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3920 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3921 | task->expire = TICK_ETERNITY; |
| 3922 | pktns = quic_loss_pktns(qc); |
| 3923 | if (tick_isset(pktns->tx.loss_time)) { |
| 3924 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3925 | |
| 3926 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3927 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3928 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3929 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3930 | goto out; |
| 3931 | } |
| 3932 | |
| 3933 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3934 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 3935 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3936 | pktns->tx.pto_probe = 1; |
| 3937 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3938 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3939 | } |
| 3940 | else { |
| 3941 | pktns->tx.pto_probe = 2; |
| 3942 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3943 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3944 | 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] | 3945 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3946 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3947 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3948 | 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] | 3949 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3950 | 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] | 3951 | iel->pktns->tx.pto_probe = 1; |
| 3952 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3953 | |
| 3954 | for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3955 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3956 | continue; |
| 3957 | |
Frédéric Lécaille | 53c7d8d | 2022-02-15 12:00:55 +0100 | [diff] [blame] | 3958 | qc_prep_fast_retrans(&qc->els[i], qc); |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3959 | } |
| 3960 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3961 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3962 | qc->path->loss.pto_count++; |
| 3963 | |
| 3964 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3965 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3966 | |
| 3967 | return task; |
| 3968 | } |
| 3969 | |
| 3970 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3971 | * connections used to identify the first Initial packets of client connecting |
| 3972 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3973 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3974 | * <scid> is the source connection ID with <scid_len> as length. |
| 3975 | * Returns 1 if succeeded, 0 if not. |
| 3976 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3977 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3978 | unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3979 | unsigned char *scid, size_t scid_len, int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3980 | { |
| 3981 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3982 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3983 | /* Initial CID. */ |
| 3984 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3985 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3986 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3987 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3988 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3989 | qc = pool_zalloc(pool_head_quic_conn); |
| 3990 | if (!qc) { |
| 3991 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3992 | goto err; |
| 3993 | } |
| 3994 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3995 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3996 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3997 | 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] | 3998 | goto err; |
| 3999 | } |
| 4000 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4001 | qc->cids = EB_ROOT; |
| 4002 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4003 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4004 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4005 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4006 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4007 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4008 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4009 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4010 | qc->odcid.addrlen = dcid_addr_len; |
| 4011 | memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4012 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4013 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4014 | if (scid_len) |
| 4015 | memcpy(qc->dcid.data, scid, scid_len); |
| 4016 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4017 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4018 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4019 | } |
| 4020 | /* QUIC Client (outgoing connection to servers) */ |
| 4021 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4022 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4023 | if (dcid_len) |
| 4024 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 4025 | qc->dcid.len = dcid_len; |
| 4026 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4027 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4028 | |
| 4029 | /* Initialize the output buffer */ |
| 4030 | qc->obuf.pos = qc->obuf.data; |
| 4031 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4032 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4033 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4034 | 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] | 4035 | goto err; |
| 4036 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4037 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4038 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4039 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4040 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4041 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4042 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4043 | qc->scid = icid->cid; |
| 4044 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4045 | /* Packet number spaces initialization. */ |
| 4046 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4047 | quic_pktns_init(&qc->pktns[i]); |
| 4048 | /* QUIC encryption level context initialization. */ |
| 4049 | 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] | 4050 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4051 | 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] | 4052 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4053 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4054 | /* Initialize the packet number space. */ |
| 4055 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4056 | } |
| 4057 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4058 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4059 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4060 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4061 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4062 | /* TX part. */ |
| 4063 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4064 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4065 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4066 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4067 | /* RX part. */ |
| 4068 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4069 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4070 | |
| 4071 | qc->nb_pkt_for_cc = 1; |
| 4072 | qc->nb_pkt_since_cc = 0; |
| 4073 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4074 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4075 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4076 | 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] | 4077 | goto err; |
| 4078 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4079 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4080 | /* XXX TO DO: Only one path at this time. */ |
| 4081 | qc->path = &qc->paths[0]; |
| 4082 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4083 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4084 | /* required to use MTLIST_IN_LIST */ |
| 4085 | MT_LIST_INIT(&qc->accept_list); |
| 4086 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4087 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 4088 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4089 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4090 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4091 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4092 | |
| 4093 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4094 | 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] | 4095 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4096 | if (qc) |
| 4097 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4098 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4099 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | /* Initialize the timer task of <qc> QUIC connection. |
| 4103 | * Returns 1 if succeeded, 0 if not. |
| 4104 | */ |
| 4105 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4106 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4107 | /* Attach this task to the same thread ID used for the connection */ |
| 4108 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4109 | if (!qc->timer_task) |
| 4110 | return 0; |
| 4111 | |
| 4112 | qc->timer = TICK_ETERNITY; |
| 4113 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4114 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4115 | |
| 4116 | return 1; |
| 4117 | } |
| 4118 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4119 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4120 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4121 | { |
| 4122 | unsigned int expire; |
| 4123 | |
| 4124 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4125 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4126 | } |
| 4127 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4128 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4129 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4130 | */ |
| 4131 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4132 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4133 | if (read) { |
| 4134 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4135 | } |
| 4136 | else { |
| 4137 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4138 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4139 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* The task handling the idle timeout */ |
| 4143 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4144 | { |
| 4145 | struct quic_conn *qc = ctx; |
| 4146 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4147 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4148 | * might free the quic-conn too early via quic_close(). |
| 4149 | */ |
| 4150 | qc_notify_close(qc); |
| 4151 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4152 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4153 | * responsible to call quic_close to release it. |
| 4154 | */ |
| 4155 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4156 | if (qc->mux_state != QC_MUX_READY) |
| 4157 | quic_conn_release(qc); |
| 4158 | |
| 4159 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4160 | * least clean some parts of it such as the tasklet. |
| 4161 | */ |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4162 | |
| 4163 | return NULL; |
| 4164 | } |
| 4165 | |
| 4166 | /* Initialize the idle timeout task for <qc>. |
| 4167 | * Returns 1 if succeeded, 0 if not. |
| 4168 | */ |
| 4169 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4170 | { |
| 4171 | qc->idle_timer_task = task_new_here(); |
| 4172 | if (!qc->idle_timer_task) |
| 4173 | return 0; |
| 4174 | |
| 4175 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4176 | qc->idle_timer_task->context = qc; |
| 4177 | qc_idle_timer_rearm(qc, 1); |
| 4178 | task_queue(qc->idle_timer_task); |
| 4179 | |
| 4180 | return 1; |
| 4181 | } |
| 4182 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4183 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4184 | * past one byte of this buffer. |
| 4185 | */ |
| 4186 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4187 | struct quic_rx_packet *pkt) |
| 4188 | { |
| 4189 | unsigned char dcid_len, scid_len; |
| 4190 | |
| 4191 | /* Version */ |
| 4192 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4193 | return 0; |
| 4194 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4195 | /* Destination Connection ID Length */ |
| 4196 | dcid_len = *(*buf)++; |
| 4197 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4198 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4199 | /* XXX MUST BE DROPPED */ |
| 4200 | return 0; |
| 4201 | |
| 4202 | if (dcid_len) { |
| 4203 | /* Check that the length of this received DCID matches the CID lengths |
| 4204 | * of our implementation for non Initials packets only. |
| 4205 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4206 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4207 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4208 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4209 | return 0; |
| 4210 | |
| 4211 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4212 | } |
| 4213 | |
| 4214 | pkt->dcid.len = dcid_len; |
| 4215 | *buf += dcid_len; |
| 4216 | |
| 4217 | /* Source Connection ID Length */ |
| 4218 | scid_len = *(*buf)++; |
| 4219 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4220 | /* XXX MUST BE DROPPED */ |
| 4221 | return 0; |
| 4222 | |
| 4223 | if (scid_len) |
| 4224 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4225 | pkt->scid.len = scid_len; |
| 4226 | *buf += scid_len; |
| 4227 | |
| 4228 | return 1; |
| 4229 | } |
| 4230 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4231 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4232 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4233 | { |
| 4234 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4235 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4236 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4237 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4238 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | /* 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] | 4242 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4243 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4244 | * the packet first byte. |
| 4245 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4246 | * Returns 1 if succeeded, 0 if not. |
| 4247 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4248 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4249 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4250 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4251 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4252 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4253 | { |
| 4254 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4255 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4256 | struct quic_enc_level *qel; |
| 4257 | /* Only for traces. */ |
| 4258 | struct quic_rx_packet *qpkt_trace; |
| 4259 | |
| 4260 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4261 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4262 | /* The packet number is here. This is also the start minus |
| 4263 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4264 | * protection. |
| 4265 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4266 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4267 | |
| 4268 | tel = quic_packet_type_enc_level(pkt->type); |
| 4269 | qel = &qc->els[tel]; |
| 4270 | |
| 4271 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4272 | /* Note that the following function enables us to unprotect the packet |
| 4273 | * number and its length subsequently used to decrypt the entire |
| 4274 | * packets. |
| 4275 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4276 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4277 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4278 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4279 | goto err; |
| 4280 | } |
| 4281 | |
| 4282 | /* The AAD includes the packet number field found at <pn>. */ |
| 4283 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4284 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4285 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4286 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4287 | 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] | 4288 | /* If the packet number space has been discarded, this packet |
| 4289 | * will be not parsed. |
| 4290 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4291 | 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] | 4292 | goto out; |
| 4293 | } |
| 4294 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4295 | 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] | 4296 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4297 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4298 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4301 | *el = qel; |
| 4302 | /* No reference counter incrementation here!!! */ |
| 4303 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4304 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4305 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4306 | b_add(&qc->rx.buf, pkt->len); |
| 4307 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4308 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4309 | return 1; |
| 4310 | |
| 4311 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4312 | 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] | 4313 | return 0; |
| 4314 | } |
| 4315 | |
| 4316 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4317 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4318 | */ |
| 4319 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4320 | unsigned char byte0, int *long_header) |
| 4321 | { |
| 4322 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4323 | pkt->type = |
| 4324 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4325 | *long_header = 1; |
| 4326 | } |
| 4327 | else { |
| 4328 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4329 | *long_header = 0; |
| 4330 | } |
| 4331 | } |
| 4332 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4333 | /* |
| 4334 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4335 | */ |
| 4336 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4337 | { |
| 4338 | int j = 0, version; |
| 4339 | |
| 4340 | do { |
| 4341 | version = quic_supported_version[j]; |
| 4342 | if (version == pkt->version) |
| 4343 | return 1; |
| 4344 | |
| 4345 | version = quic_supported_version[++j]; |
| 4346 | } while(version); |
| 4347 | |
| 4348 | return 0; |
| 4349 | } |
| 4350 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4351 | /* |
| 4352 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4353 | * address <addr>. |
| 4354 | * Implementation of RFC9000 6. Version Negotiation |
| 4355 | * |
| 4356 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4357 | * |
| 4358 | * Returns 0 on success else non-zero |
| 4359 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4360 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4361 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4362 | { |
| 4363 | char buf[256]; |
| 4364 | int i = 0, j, version; |
| 4365 | const socklen_t addrlen = get_addr_len(addr); |
| 4366 | |
| 4367 | /* |
| 4368 | * header form |
| 4369 | * long header, fixed bit to 0 for Version Negotiation |
| 4370 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4371 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4372 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4373 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4374 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4375 | /* null version for Version Negotiation */ |
| 4376 | buf[i++] = '\x00'; |
| 4377 | buf[i++] = '\x00'; |
| 4378 | buf[i++] = '\x00'; |
| 4379 | buf[i++] = '\x00'; |
| 4380 | |
| 4381 | /* source connection id */ |
| 4382 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4383 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4384 | i += pkt->scid.len; |
| 4385 | |
| 4386 | /* destination connection id */ |
| 4387 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4388 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4389 | i += pkt->dcid.len; |
| 4390 | |
| 4391 | /* supported version */ |
| 4392 | j = 0; |
| 4393 | do { |
| 4394 | version = htonl(quic_supported_version[j]); |
| 4395 | memcpy(&buf[i], &version, sizeof(version)); |
| 4396 | i += sizeof(version); |
| 4397 | |
| 4398 | version = quic_supported_version[++j]; |
| 4399 | } while (version); |
| 4400 | |
| 4401 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4402 | return 1; |
| 4403 | |
| 4404 | return 0; |
| 4405 | } |
| 4406 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4407 | /* Generate the token to be used in Retry packets. The token is written to |
| 4408 | * <buf> which is expected to be <len> bytes. |
| 4409 | * |
| 4410 | * Various parameters are expected to be encoded in the token. For now, only |
| 4411 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4412 | * as this CID must be repeated by the server in the transport parameters. |
| 4413 | * |
| 4414 | * TODO add the client address to validate the token origin. |
| 4415 | * |
| 4416 | * Returns the length of the encoded token or 0 on error. |
| 4417 | */ |
| 4418 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4419 | struct quic_rx_packet *pkt) |
| 4420 | { |
| 4421 | const size_t token_len = 1 + pkt->dcid.len; |
| 4422 | unsigned char i = 0; |
| 4423 | |
| 4424 | if (token_len > len) |
| 4425 | return 0; |
| 4426 | |
| 4427 | buf[i++] = pkt->dcid.len; |
| 4428 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4429 | i += pkt->dcid.len; |
| 4430 | |
| 4431 | return i; |
| 4432 | } |
| 4433 | |
| 4434 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4435 | * the Initial <pkt> packet. |
| 4436 | * |
| 4437 | * Returns 0 on success else non-zero. |
| 4438 | */ |
| 4439 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4440 | struct quic_rx_packet *pkt) |
| 4441 | { |
| 4442 | unsigned char buf[128]; |
| 4443 | int i = 0, token_len; |
| 4444 | const socklen_t addrlen = get_addr_len(addr); |
| 4445 | struct quic_cid scid; |
| 4446 | |
| 4447 | /* long header + fixed bit + packet type 0x3 */ |
| 4448 | buf[i++] = 0xf0; |
| 4449 | /* version */ |
| 4450 | buf[i++] = 0x00; |
| 4451 | buf[i++] = 0x00; |
| 4452 | buf[i++] = 0x00; |
| 4453 | buf[i++] = 0x01; |
| 4454 | |
| 4455 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4456 | buf[i++] = pkt->scid.len; |
| 4457 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4458 | i += pkt->scid.len; |
| 4459 | |
| 4460 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4461 | scid.len = QUIC_HAP_CID_LEN; |
| 4462 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4463 | return 1; |
| 4464 | |
| 4465 | buf[i++] = scid.len; |
| 4466 | memcpy(&buf[i], scid.data, scid.len); |
| 4467 | i += scid.len; |
| 4468 | |
| 4469 | /* token */ |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4470 | if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4471 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4472 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4473 | i += token_len; |
| 4474 | |
| 4475 | /* token integrity tag */ |
| 4476 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4477 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4478 | pkt->dcid.len, buf, i)) { |
| 4479 | return 1; |
| 4480 | } |
| 4481 | |
| 4482 | i += QUIC_TLS_TAG_LEN; |
| 4483 | |
| 4484 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4485 | return 1; |
| 4486 | |
| 4487 | return 0; |
| 4488 | } |
| 4489 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4490 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4491 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4492 | * concatenated to the <pkt> DCID field. |
| 4493 | * |
| 4494 | * Returns the instance or NULL if not found. |
| 4495 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4496 | 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] | 4497 | struct listener *l, |
| 4498 | struct sockaddr_storage *saddr) |
| 4499 | { |
| 4500 | struct quic_conn *qc = NULL; |
| 4501 | struct ebmb_node *node; |
| 4502 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4503 | /* set if the quic_conn is found in the second DCID tree */ |
| 4504 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4505 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4506 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4507 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4508 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4509 | /* DCIDs of first packets coming from multiple clients may have |
| 4510 | * the same values. Let's distinguish them by concatenating the |
| 4511 | * socket addresses. |
| 4512 | */ |
| 4513 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4514 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4515 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4516 | if (node) { |
| 4517 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4518 | goto end; |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4523 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4524 | * used. |
| 4525 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4526 | 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] | 4527 | if (!node) |
| 4528 | goto end; |
| 4529 | |
| 4530 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4531 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4532 | found_in_dcid = 1; |
| 4533 | |
| 4534 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4535 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4536 | * If already done, this is a noop. |
| 4537 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4538 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4539 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4540 | |
| 4541 | return qc; |
| 4542 | } |
| 4543 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4544 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4545 | * will extract the parameters stored in the token : <odcid>. |
| 4546 | * |
| 4547 | * Returns 0 on success else non-zero. |
| 4548 | */ |
| 4549 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4550 | struct quic_cid *odcid) |
| 4551 | { |
| 4552 | uint64_t odcid_len; |
| 4553 | |
| 4554 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4555 | return 1; |
| 4556 | |
Frédéric Lécaille | f1f812b | 2022-03-17 16:22:02 +0100 | [diff] [blame] | 4557 | if (odcid_len > QUIC_CID_MAXLEN) |
| 4558 | return 1; |
| 4559 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4560 | memcpy(odcid->data, token, odcid_len); |
| 4561 | odcid->len = odcid_len; |
| 4562 | |
| 4563 | return 0; |
| 4564 | } |
| 4565 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4566 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4567 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4568 | * parameters of this session. |
| 4569 | * This is the responsibility of the caller to check the validity of all the |
| 4570 | * pointers passed as parameter to this function. |
| 4571 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4572 | * CO_ER_SSL_NO_MEM. |
| 4573 | */ |
| 4574 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4575 | unsigned char *params, size_t params_len) |
| 4576 | { |
| 4577 | int retry; |
| 4578 | |
| 4579 | retry = 1; |
| 4580 | retry: |
| 4581 | *ssl = SSL_new(ssl_ctx); |
| 4582 | if (!*ssl) { |
| 4583 | if (!retry--) |
| 4584 | goto err; |
| 4585 | |
| 4586 | pool_gc(NULL); |
| 4587 | goto retry; |
| 4588 | } |
| 4589 | |
| 4590 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4591 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4592 | !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] | 4593 | SSL_free(*ssl); |
| 4594 | *ssl = NULL; |
| 4595 | if (!retry--) |
| 4596 | goto err; |
| 4597 | |
| 4598 | pool_gc(NULL); |
| 4599 | goto retry; |
| 4600 | } |
| 4601 | |
| 4602 | return 0; |
| 4603 | |
| 4604 | err: |
| 4605 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4606 | return -1; |
| 4607 | } |
| 4608 | |
| 4609 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4610 | * used to process <qc> received packets. The allocated context is stored in |
| 4611 | * <qc.xprt_ctx>. |
| 4612 | * |
| 4613 | * Returns 0 on success else non-zero. |
| 4614 | */ |
| 4615 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4616 | { |
| 4617 | struct bind_conf *bc = qc->li->bind_conf; |
| 4618 | struct ssl_sock_ctx *ctx = NULL; |
| 4619 | |
| 4620 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4621 | if (!ctx) |
| 4622 | goto err; |
| 4623 | |
| 4624 | ctx->wait_event.tasklet = tasklet_new(); |
| 4625 | if (!ctx->wait_event.tasklet) |
| 4626 | goto err; |
| 4627 | |
| 4628 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4629 | ctx->wait_event.tasklet->context = ctx; |
| 4630 | ctx->wait_event.events = 0; |
| 4631 | ctx->subs = NULL; |
| 4632 | ctx->xprt_ctx = NULL; |
| 4633 | ctx->qc = qc; |
| 4634 | |
| 4635 | /* Set tasklet tid based on the SCID selected by us for this |
| 4636 | * connection. The upper layer will also be binded on the same thread. |
| 4637 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4638 | 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] | 4639 | |
| 4640 | if (qc_is_listener(qc)) { |
| 4641 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4642 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4643 | goto err; |
| 4644 | } |
| 4645 | |
| 4646 | /* Enabling 0-RTT */ |
| 4647 | if (bc->ssl_conf.early_data) |
| 4648 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4649 | |
| 4650 | SSL_set_accept_state(ctx->ssl); |
| 4651 | } |
| 4652 | |
| 4653 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4654 | |
| 4655 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4656 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4657 | |
| 4658 | return 0; |
| 4659 | |
| 4660 | err: |
| 4661 | if (ctx && ctx->wait_event.tasklet) |
| 4662 | tasklet_free(ctx->wait_event.tasklet); |
| 4663 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4664 | |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4668 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4669 | struct quic_rx_packet *pkt, int first_pkt, |
| 4670 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4671 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4672 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4673 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4674 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4675 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4676 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4677 | size_t b_cspace; |
| 4678 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4679 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4680 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4681 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4682 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4683 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4684 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4685 | /* This ist only to please to traces and distinguish the |
| 4686 | * packet with parsed packet number from others. |
| 4687 | */ |
| 4688 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4689 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4690 | goto err; |
| 4691 | |
| 4692 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4693 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4694 | /* XXX TO BE DISCARDED */ |
| 4695 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4696 | goto err; |
| 4697 | } |
| 4698 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4699 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4700 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4701 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4702 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4703 | uint64_t len; |
| 4704 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4705 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4706 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4707 | goto err; |
| 4708 | } |
| 4709 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4710 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4711 | * they must have the same DCID. |
| 4712 | */ |
| 4713 | if (!first_pkt && |
| 4714 | (pkt->dcid.len != dgram->dcid_len || |
| 4715 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4716 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4717 | goto err; |
| 4718 | } |
| 4719 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4720 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4721 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4722 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4723 | goto err; |
| 4724 | } |
| 4725 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4726 | /* RFC9000 6. Version Negotiation */ |
| 4727 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4728 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4729 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4730 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4731 | goto err; |
| 4732 | } |
| 4733 | |
| 4734 | 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] | 4735 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4736 | } |
| 4737 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4738 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4739 | * there is no Initial connection IDs storage. |
| 4740 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4741 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4742 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4743 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4744 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4745 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4746 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4747 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4748 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4749 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 4750 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 4751 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4752 | */ |
| 4753 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4754 | |
| 4755 | /* TODO Retry should be automatically activated if |
| 4756 | * suspect network usage is detected. |
| 4757 | */ |
| 4758 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4759 | 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] | 4760 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4761 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4762 | goto err; |
| 4763 | } |
| 4764 | |
| 4765 | goto err; |
| 4766 | } |
| 4767 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4768 | pkt->token = buf; |
| 4769 | buf += pkt->token_len; |
| 4770 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4771 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4772 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4773 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4774 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4775 | goto err; |
| 4776 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4777 | } |
| 4778 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4779 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4780 | end - buf < len) { |
| 4781 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4782 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4783 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4784 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4785 | payload = buf; |
| 4786 | pkt->len = len + payload - beg; |
| 4787 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4788 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4789 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4790 | int ipv4; |
| 4791 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4792 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4793 | const unsigned char *salt = initial_salt_v1; |
| 4794 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4795 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4796 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4797 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4798 | goto err; |
| 4799 | } |
| 4800 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 4801 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 4802 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 4803 | goto err; |
| 4804 | } |
| 4805 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4806 | pkt->saddr = dgram->saddr; |
| 4807 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4808 | qc = qc_new_conn(pkt->version, ipv4, |
| 4809 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4810 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4811 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4812 | goto err; |
| 4813 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4814 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4815 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4816 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4817 | /* Copy the transport parameters. */ |
| 4818 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4819 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4820 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4821 | if (pkt->token_len) { |
| 4822 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4823 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4824 | goto err; |
| 4825 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4826 | /* Copy retry_source_connection_id transport parameter. */ |
| 4827 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4828 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4829 | } |
| 4830 | else { |
| 4831 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4832 | odcid->len = pkt->dcid.len; |
| 4833 | } |
| 4834 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4835 | /* Copy the initial source connection ID. */ |
| 4836 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4837 | qc->enc_params_len = |
| 4838 | quic_transport_params_encode(qc->enc_params, |
| 4839 | qc->enc_params + sizeof qc->enc_params, |
| 4840 | &qc->rx.params, 1); |
| 4841 | if (!qc->enc_params_len) |
| 4842 | goto err; |
| 4843 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4844 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4845 | goto err; |
| 4846 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 4847 | if (!quic_conn_init_timer(qc)) |
| 4848 | goto err; |
| 4849 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4850 | if (!quic_conn_init_idle_timer_task(qc)) |
| 4851 | goto err; |
| 4852 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4853 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4854 | * chosen by the client for Initial packets. |
| 4855 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4856 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4857 | salt = initial_salt_draft_29; |
| 4858 | salt_len = sizeof initial_salt_draft_29; |
| 4859 | } |
| 4860 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4861 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4862 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4863 | goto err; |
| 4864 | } |
| 4865 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4866 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4867 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4868 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4869 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4870 | /* If the insertion failed, it means that another |
| 4871 | * thread has already allocated a QUIC connection for |
| 4872 | * the same CID. Liberate our allocated connection. |
| 4873 | */ |
| 4874 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4875 | qc_to_purge = qc; |
| 4876 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4877 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4878 | pkt->qc = qc; |
| 4879 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4880 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4881 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4882 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4883 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4884 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4885 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4886 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4887 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4888 | } |
| 4889 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4890 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4891 | } |
| 4892 | } |
| 4893 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4894 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4895 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4896 | goto err; |
| 4897 | } |
| 4898 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4899 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4900 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4901 | |
| 4902 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4903 | * they must have the same DCID. |
| 4904 | */ |
| 4905 | if (!first_pkt && |
| 4906 | (pkt->dcid.len != dgram->dcid_len || |
| 4907 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4908 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4909 | goto err; |
| 4910 | } |
| 4911 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4912 | buf += QUIC_HAP_CID_LEN; |
| 4913 | |
| 4914 | /* A short packet is the last one of a UDP datagram. */ |
| 4915 | payload = buf; |
| 4916 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4917 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4918 | 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] | 4919 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4920 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4921 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4922 | goto err; |
| 4923 | } |
| 4924 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4925 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4926 | } |
| 4927 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4928 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 4929 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 4930 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 4931 | qc->nb_pkt_for_cc++; |
| 4932 | qc->nb_pkt_since_cc = 0; |
| 4933 | } |
| 4934 | /* Skip the entire datagram */ |
| 4935 | pkt->len = end - beg; |
| 4936 | TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc); |
| 4937 | goto out; |
| 4938 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4939 | |
| 4940 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4941 | * they must have the same DCID. |
| 4942 | * |
| 4943 | * This check must be done after the final update to pkt.len to |
| 4944 | * properly drop the packet on failure. |
| 4945 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4946 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4947 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4948 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4949 | QUIC_EV_CONN_LPKT, qc); |
| 4950 | /* Reset the anti-amplification bit. It will be set again |
| 4951 | * when sending the next packet if reached again. |
| 4952 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4953 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 4954 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4955 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4956 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4957 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4958 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4959 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4960 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4961 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4962 | goto out; |
| 4963 | } |
| 4964 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4965 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4966 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4967 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4968 | if (b_cspace < pkt->len) { |
| 4969 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4970 | if (b_cspace) { |
| 4971 | b_putchr(&qc->rx.buf, 0x00); |
| 4972 | b_cspace--; |
| 4973 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4974 | b_add(&qc->rx.buf, b_cspace); |
| 4975 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4976 | 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] | 4977 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4978 | goto err; |
| 4979 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4982 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4983 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4984 | goto err; |
| 4985 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4986 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4987 | 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] | 4988 | if (pkt->aad_len) |
| 4989 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4990 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4991 | /* Wake up the connection packet handler task from here only if all |
| 4992 | * the contexts have been initialized, especially the mux context |
| 4993 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4994 | * will start it if these contexts for the connection are not already |
| 4995 | * initialized. |
| 4996 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4997 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4998 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4999 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5000 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5001 | 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] | 5002 | |
| 5003 | return pkt->len; |
| 5004 | |
| 5005 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5006 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5007 | * This cannot be done by this thread. |
| 5008 | */ |
| 5009 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5010 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5011 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5012 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5013 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5014 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5015 | if (!pkt->len) |
| 5016 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5017 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5018 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5019 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5020 | return -1; |
| 5021 | } |
| 5022 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5023 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5024 | * 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] | 5025 | */ |
| 5026 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5027 | int type, size_t pn_len, struct quic_conn *conn) |
| 5028 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5029 | 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] | 5030 | return 0; |
| 5031 | |
| 5032 | /* #0 byte flags */ |
| 5033 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5034 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5035 | /* Version */ |
| 5036 | quic_write_uint32(buf, end, conn->version); |
| 5037 | *(*buf)++ = conn->dcid.len; |
| 5038 | /* Destination connection ID */ |
| 5039 | if (conn->dcid.len) { |
| 5040 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5041 | *buf += conn->dcid.len; |
| 5042 | } |
| 5043 | /* Source connection ID */ |
| 5044 | *(*buf)++ = conn->scid.len; |
| 5045 | if (conn->scid.len) { |
| 5046 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5047 | *buf += conn->scid.len; |
| 5048 | } |
| 5049 | |
| 5050 | return 1; |
| 5051 | } |
| 5052 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5053 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5054 | * 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] | 5055 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5056 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5057 | size_t pn_len, struct quic_conn *conn, |
| 5058 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5059 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5060 | if (end - *buf < 1 + conn->dcid.len) |
| 5061 | return 0; |
| 5062 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5063 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5064 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5065 | ((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] | 5066 | /* Destination connection ID */ |
| 5067 | if (conn->dcid.len) { |
| 5068 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5069 | *buf += conn->dcid.len; |
| 5070 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5071 | |
| 5072 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5076 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5077 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5078 | * Returns 1 if succeeded or 0 if failed. |
| 5079 | */ |
| 5080 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5081 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5082 | { |
| 5083 | int i, ret, outlen; |
| 5084 | EVP_CIPHER_CTX *ctx; |
| 5085 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5086 | * and at most 4 bytes for the packet number |
| 5087 | */ |
| 5088 | unsigned char mask[5] = {0}; |
| 5089 | |
| 5090 | ret = 0; |
| 5091 | ctx = EVP_CIPHER_CTX_new(); |
| 5092 | if (!ctx) |
| 5093 | return 0; |
| 5094 | |
| 5095 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5096 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5097 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5098 | goto out; |
| 5099 | |
| 5100 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5101 | for (i = 0; i < pnlen; i++) |
| 5102 | pn[i] ^= mask[i + 1]; |
| 5103 | |
| 5104 | ret = 1; |
| 5105 | |
| 5106 | out: |
| 5107 | EVP_CIPHER_CTX_free(ctx); |
| 5108 | |
| 5109 | return ret; |
| 5110 | } |
| 5111 | |
| 5112 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5113 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5114 | * Return 1 if succeeded, 0 if not. |
| 5115 | */ |
| 5116 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5117 | { |
| 5118 | size_t room, ack_delay_sz; |
| 5119 | |
| 5120 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5121 | /* A frame is made of 1 byte for the frame type. */ |
| 5122 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5123 | 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] | 5124 | return 0; |
| 5125 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5126 | 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] | 5127 | } |
| 5128 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5129 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5130 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5131 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5132 | * with the number of bytes already present in this buffer which must be taken |
| 5133 | * into an account for the Length packet field value. <headlen> is the number of |
| 5134 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5135 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5136 | * 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] | 5137 | * by this function. Also attach these frames to <l> frame list. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5138 | * Return 1 if succeeded, 0 if not. |
| 5139 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5140 | 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] | 5141 | size_t room, size_t *len, size_t headlen, |
| 5142 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5143 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5144 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5145 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5146 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5147 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5148 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5149 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5150 | return 0; |
| 5151 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5152 | /* If we are not probing we must take into an account the congestion |
| 5153 | * control window. |
| 5154 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5155 | if (!qel->pktns->tx.pto_probe) { |
| 5156 | size_t remain = quic_path_prep_data(qc->path); |
| 5157 | |
| 5158 | if (headlen > remain) |
| 5159 | return 0; |
| 5160 | |
| 5161 | room = QUIC_MIN(room, remain - headlen); |
| 5162 | } |
| 5163 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5164 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5165 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5166 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5167 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5168 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5169 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5170 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5171 | break; |
| 5172 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5173 | switch (cf->type) { |
| 5174 | case QUIC_FT_CRYPTO: |
| 5175 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5176 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5177 | /* Compute the length of this CRYPTO frame header */ |
| 5178 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5179 | /* Compute the data length of this CRyPTO frame. */ |
| 5180 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5181 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5182 | 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] | 5183 | if (!dlen) |
| 5184 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5185 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5186 | /* CRYPTO frame length. */ |
| 5187 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5188 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5189 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5190 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5191 | * length and the length of this length. |
| 5192 | */ |
| 5193 | *len += flen; |
| 5194 | room -= flen; |
| 5195 | if (dlen == cf->crypto.len) { |
| 5196 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5197 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5198 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5199 | } |
| 5200 | else { |
| 5201 | struct quic_frame *new_cf; |
| 5202 | |
| 5203 | new_cf = pool_alloc(pool_head_quic_frame); |
| 5204 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5205 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5206 | return 0; |
| 5207 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5208 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5209 | new_cf->type = QUIC_FT_CRYPTO; |
| 5210 | new_cf->crypto.len = dlen; |
| 5211 | new_cf->crypto.offset = cf->crypto.offset; |
| 5212 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5213 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5214 | /* Consume <dlen> bytes of the current frame. */ |
| 5215 | cf->crypto.len -= dlen; |
| 5216 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5217 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5218 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5219 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5220 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5221 | /* Note that these frames are accepted in short packets only without |
| 5222 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5223 | * 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] | 5224 | * |
| 5225 | * 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] | 5226 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5227 | */ |
| 5228 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5229 | ((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] | 5230 | /* Compute the data length of this STREAM frame. */ |
| 5231 | avail_room = room - hlen - *len; |
| 5232 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5233 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5234 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5235 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5236 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5237 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5238 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5239 | if (dlen > cf->stream.len) { |
| 5240 | dlen = cf->stream.len; |
| 5241 | } |
| 5242 | dlen_sz = quic_int_getsize(dlen); |
| 5243 | flen = hlen + dlen_sz + dlen; |
| 5244 | } |
| 5245 | else { |
| 5246 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5247 | flen = hlen + dlen; |
| 5248 | } |
| 5249 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5250 | 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] | 5251 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5252 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5253 | /* Add the STREAM data length and its encoded length to the packet |
| 5254 | * length and the length of this length. |
| 5255 | */ |
| 5256 | *len += flen; |
| 5257 | room -= flen; |
| 5258 | if (dlen == cf->stream.len) { |
| 5259 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5260 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5261 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5262 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5263 | /* The MUX stream might be released at this |
| 5264 | * stage. This can most notably happen on |
| 5265 | * retransmission. |
| 5266 | */ |
| 5267 | if (qc->mux_state == QC_MUX_READY && |
| 5268 | !cf->stream.stream->release) { |
| 5269 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5270 | cf->stream.len, |
| 5271 | cf->stream.offset.key); |
| 5272 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5273 | } |
| 5274 | else { |
| 5275 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5276 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5277 | |
| 5278 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5279 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5280 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5281 | return 0; |
| 5282 | } |
| 5283 | |
| 5284 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5285 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5286 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5287 | new_cf->stream.id = cf->stream.id; |
| 5288 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5289 | new_cf->stream.offset = cf->stream.offset; |
| 5290 | new_cf->stream.len = dlen; |
| 5291 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5292 | /* FIN bit reset */ |
| 5293 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5294 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5295 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5296 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5297 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5298 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5299 | b_size(cf->stream.buf), |
| 5300 | (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] | 5301 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5302 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5303 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5304 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5305 | /* The MUX stream might be released at this |
| 5306 | * stage. This can most notably happen on |
| 5307 | * retransmission. |
| 5308 | */ |
| 5309 | if (qc->mux_state == QC_MUX_READY && |
| 5310 | !cf->stream.stream->release) { |
| 5311 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5312 | new_cf->stream.len, |
| 5313 | new_cf->stream.offset.key); |
| 5314 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5315 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5316 | |
| 5317 | /* TODO the MUX is notified about the frame sending via |
| 5318 | * previous qcc_streams_sent_done call. However, the |
| 5319 | * sending can fail later, for example if the sendto |
| 5320 | * system call returns an error. As the MUX has been |
| 5321 | * notified, the transport layer is responsible to |
| 5322 | * bufferize and resent the announced data later. |
| 5323 | */ |
| 5324 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5325 | break; |
| 5326 | |
| 5327 | default: |
| 5328 | flen = qc_frm_len(cf); |
| 5329 | BUG_ON(!flen); |
| 5330 | if (flen > room) |
| 5331 | continue; |
| 5332 | |
| 5333 | *len += flen; |
| 5334 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5335 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5336 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5337 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5338 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5339 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5340 | } |
| 5341 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5342 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5343 | } |
| 5344 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5345 | /* 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] | 5346 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5347 | * 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] | 5348 | * filling the buffer with as much frames as possible from <frms> list of |
| 5349 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5350 | * 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] | 5351 | * 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] | 5352 | * having returned from this function. |
| 5353 | * 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] | 5354 | * number field in this packet. <pn_len> will also have the packet number |
| 5355 | * length as value. |
| 5356 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5357 | * 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] | 5358 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5359 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5360 | size_t dglen, struct quic_tx_packet *pkt, |
| 5361 | 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] | 5362 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5363 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5364 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5365 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5366 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5367 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5368 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5369 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5370 | 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] | 5371 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5372 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5373 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5374 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5375 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5376 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5377 | /* Length field value with CRYPTO frames if present. */ |
| 5378 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5379 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5380 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5381 | * buffer to respect the congestion controller window. |
| 5382 | * 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] | 5383 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5384 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5385 | size_t path_room; |
| 5386 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5387 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5388 | if (end - beg > path_room) |
| 5389 | end = beg + path_room; |
| 5390 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5391 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5392 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5393 | * length field if any. |
| 5394 | */ |
| 5395 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5396 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5397 | goto no_room; |
| 5398 | |
| 5399 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5400 | 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] | 5401 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5402 | *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] | 5403 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5404 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5405 | !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] | 5406 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5407 | !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] | 5408 | goto no_room; |
| 5409 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5410 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5411 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5412 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5413 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5414 | /* Build an ACK frame if required. */ |
| 5415 | ack_frm_len = 0; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5416 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) { |
| 5417 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5418 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5419 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5420 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5421 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5422 | * that from here, we do not know if we will have to send a PING frame. |
| 5423 | * This will be decided after having computed the ack-eliciting frames |
| 5424 | * to be added to this packet. |
| 5425 | */ |
| 5426 | 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] | 5427 | if (!ack_frm_len) |
| 5428 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5429 | } |
| 5430 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5431 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5432 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5433 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5434 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5435 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5436 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5437 | 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] | 5438 | /* Initialize the length of the frames built below to <len>. |
| 5439 | * If any frame could be successfully built by qc_build_frms(), |
| 5440 | * we will have len_frms > len. |
| 5441 | */ |
| 5442 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5443 | if (!qc_build_frms(&frm_list, frms, |
| 5444 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5445 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5446 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5447 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5448 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5449 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5450 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5451 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5452 | * for the encryption tag. It must be taken into an account for the length |
| 5453 | * of this packet. |
| 5454 | */ |
| 5455 | if (len_frms) |
| 5456 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5457 | else |
| 5458 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5459 | /* CONNECTION_CLOSE frame */ |
| 5460 | if (cc) { |
| 5461 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5462 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5463 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5464 | len += qc_frm_len(&cc_frm); |
| 5465 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5466 | add_ping_frm = 0; |
| 5467 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5468 | len_sz = quic_int_getsize(len); |
| 5469 | /* Add this packet size to <dglen> */ |
| 5470 | dglen += head_len + len_sz + len; |
| 5471 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5472 | /* This is a maximum padding size */ |
| 5473 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5474 | /* The length field value is of this packet is <len> + <padding_len> |
| 5475 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5476 | * <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] | 5477 | * sizes from <padding_len>. |
| 5478 | */ |
| 5479 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5480 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5481 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5482 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5483 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5484 | /* 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] | 5485 | add_ping_frm = 1; |
| 5486 | len += 1; |
| 5487 | } |
| 5488 | /* 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] | 5489 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5490 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5491 | } |
| 5492 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5493 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5494 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5495 | |
| 5496 | /* Packet number field address. */ |
| 5497 | *buf_pn = pos; |
| 5498 | |
| 5499 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5500 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5501 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5502 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5503 | if (ack_frm_len) { |
| 5504 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 5505 | goto no_room; |
| 5506 | |
| 5507 | 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] | 5508 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5509 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5510 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5511 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5512 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5513 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5514 | unsigned char *spos = pos; |
| 5515 | |
| 5516 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5517 | ssize_t room = end - pos; |
| 5518 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5519 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5520 | /* TODO: this should not have happened if qc_build_frms() |
| 5521 | * had correctly computed and sized the frames to be |
| 5522 | * added to this packet. Note that <cf> was added |
| 5523 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 5524 | */ |
| 5525 | LIST_DELETE(&cf->list); |
| 5526 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5527 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5528 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5529 | |
| 5530 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5531 | quic_tx_packet_refinc(pkt); |
| 5532 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5533 | } |
| 5534 | } |
| 5535 | |
| 5536 | /* Build a PING frame if needed. */ |
| 5537 | if (add_ping_frm) { |
| 5538 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5539 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5540 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5541 | } |
| 5542 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5543 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5544 | if (cc) { |
| 5545 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 5546 | goto no_room; |
| 5547 | |
| 5548 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 5549 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5550 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5551 | /* Build a PADDING frame if needed. */ |
| 5552 | if (padding_len) { |
| 5553 | frm.type = QUIC_FT_PADDING; |
| 5554 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5555 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5556 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5557 | } |
| 5558 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5559 | /* If this packet is ack-eliciting and we are probing let's |
| 5560 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5561 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5562 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5563 | qel->pktns->tx.pto_probe) |
| 5564 | qel->pktns->tx.pto_probe--; |
| 5565 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5566 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5567 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5568 | 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] | 5569 | |
| 5570 | return 1; |
| 5571 | |
| 5572 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5573 | /* 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] | 5574 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5575 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt); |
| 5576 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5577 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5580 | 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] | 5581 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5582 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5583 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5584 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5585 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5586 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5587 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5588 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5589 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5590 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5591 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5592 | } |
| 5593 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5594 | /* 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] | 5595 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 5596 | * of prebuilt frames. |
| 5597 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5598 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5599 | * -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] | 5600 | * XXX NOTE XXX |
| 5601 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 5602 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 5603 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5604 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5605 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5606 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5607 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5608 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5609 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5610 | { |
| 5611 | /* The pointer to the packet number field. */ |
| 5612 | unsigned char *buf_pn; |
| 5613 | unsigned char *beg, *end, *payload; |
| 5614 | int64_t pn; |
| 5615 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5616 | struct quic_tls_ctx *tls_ctx; |
| 5617 | struct quic_tx_packet *pkt; |
| 5618 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5619 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5620 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5621 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5622 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5623 | 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] | 5624 | *err = -2; |
| 5625 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5628 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5629 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5630 | pn_len = 0; |
| 5631 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5632 | |
| 5633 | pn = qel->pktns->tx.next_pn + 1; |
| 5634 | 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] | 5635 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5636 | *err = -1; |
| 5637 | goto err; |
| 5638 | } |
| 5639 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5640 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5641 | payload = buf_pn + pn_len; |
| 5642 | payload_len = end - payload; |
| 5643 | aad_len = payload - beg; |
| 5644 | |
| 5645 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5646 | 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] | 5647 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5648 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5649 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5650 | |
| 5651 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5652 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5653 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5654 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5655 | 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] | 5656 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5657 | goto err; |
| 5658 | } |
| 5659 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5660 | /* Consume a packet number */ |
| 5661 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5662 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 5663 | 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] | 5664 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5665 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5666 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5667 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5668 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5669 | /* 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] | 5670 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5671 | pkt->in_flight_len = pkt->len; |
| 5672 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5673 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 5674 | /* Always reset this flags */ |
| 5675 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5676 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 5677 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 5678 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 5679 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5680 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5681 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5682 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5683 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5684 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5685 | |
| 5686 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5687 | /* TODO: what about the frames which have been built |
| 5688 | * for this packet. |
| 5689 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5690 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5691 | 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] | 5692 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5693 | } |
| 5694 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5695 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5696 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5697 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5698 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5699 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5700 | */ |
| 5701 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5702 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5703 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5704 | |
| 5705 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5706 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5707 | |
| 5708 | es->events |= event_type; |
| 5709 | qcc->subs = es; |
| 5710 | |
| 5711 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5712 | 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] | 5713 | |
| 5714 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5715 | 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] | 5716 | |
| 5717 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5721 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5722 | * subscribe() call. It always returns zero. |
| 5723 | */ |
| 5724 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5725 | { |
| 5726 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5727 | } |
| 5728 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5729 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5730 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5731 | */ |
| 5732 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5733 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5734 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5735 | |
| 5736 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5737 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5738 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5739 | if (*xprt_ctx) |
| 5740 | goto out; |
| 5741 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5742 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5743 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5744 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5745 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5746 | |
| 5747 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5748 | } |
| 5749 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5750 | /* Start the QUIC transport layer */ |
| 5751 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5752 | { |
| 5753 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5754 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5755 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5756 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5757 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5758 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame^] | 5759 | /* prepare a CONNECTION_CLOSE frame */ |
| 5760 | qc->err_code = QC_ERR_APPLICATION_ERROR; |
| 5761 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 5762 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /* mux-quic can now be considered ready. */ |
| 5766 | qc->mux_state = QC_MUX_READY; |
| 5767 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5768 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5769 | return 1; |
| 5770 | } |
| 5771 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 5772 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 5773 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5774 | 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] | 5775 | return NULL; |
| 5776 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 5777 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 5778 | } |
| 5779 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5780 | /* transport-layer operations for QUIC connections. */ |
| 5781 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5782 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5783 | .subscribe = quic_conn_subscribe, |
| 5784 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5785 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5786 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5787 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5788 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5789 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 5790 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5791 | .name = "QUIC", |
| 5792 | }; |
| 5793 | |
| 5794 | __attribute__((constructor)) |
| 5795 | static void __quic_conn_init(void) |
| 5796 | { |
| 5797 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5798 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5799 | } |
| 5800 | |
| 5801 | __attribute__((destructor)) |
| 5802 | static void __quic_conn_deinit(void) |
| 5803 | { |
| 5804 | BIO_meth_free(ha_quic_meth); |
| 5805 | } |
| 5806 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5807 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5808 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5809 | * 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] | 5810 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5811 | 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] | 5812 | { |
| 5813 | unsigned char *pos; |
| 5814 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5815 | struct quic_dghdlr *dghdlr = ctx; |
| 5816 | struct quic_dgram *dgram; |
| 5817 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5818 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5819 | 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] | 5820 | pos = dgram->buf; |
| 5821 | end = pos + dgram->len; |
| 5822 | do { |
| 5823 | int ret; |
| 5824 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5825 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5826 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 5827 | if (!pkt) |
| 5828 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5829 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5830 | quic_rx_packet_refinc(pkt); |
| 5831 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 5832 | first_pkt = 0; |
| 5833 | pos += pkt->len; |
| 5834 | quic_rx_packet_refdec(pkt); |
| 5835 | if (ret == -1) |
| 5836 | /* If the packet length could not be found, we cannot continue. */ |
| 5837 | break; |
| 5838 | } while (pos < end); |
| 5839 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5840 | /* Increasing the received bytes counter by the UDP datagram length |
| 5841 | * if this datagram could be associated to a connection. |
| 5842 | */ |
| 5843 | if (dgram->qc) |
| 5844 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 5845 | |
| 5846 | /* Mark this datagram as consumed */ |
| 5847 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5848 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5849 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5850 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5851 | |
| 5852 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5853 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5854 | } |
| 5855 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5856 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 5857 | * Returns 1 if succeeded, 0 if not. |
| 5858 | */ |
| 5859 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 5860 | unsigned char **dcid, size_t *dcid_len) |
| 5861 | { |
| 5862 | int long_header; |
| 5863 | size_t minlen, skip; |
| 5864 | |
| 5865 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 5866 | goto err; |
| 5867 | |
| 5868 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 5869 | minlen = long_header ? |
| 5870 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 5871 | 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] | 5872 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5873 | goto err; |
| 5874 | |
| 5875 | buf += skip; |
| 5876 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 5877 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 5878 | goto err; |
| 5879 | |
| 5880 | *dcid = buf; |
| 5881 | |
| 5882 | return 1; |
| 5883 | |
| 5884 | err: |
| 5885 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 5886 | return 0; |
| 5887 | } |
| 5888 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5889 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 5890 | * correct datagram handler. |
| 5891 | * Return 1 if a correct datagram could be found, 0 if not. |
| 5892 | */ |
| 5893 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 5894 | struct sockaddr_storage *saddr, |
| 5895 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5896 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5897 | struct quic_dgram *dgram; |
| 5898 | unsigned char *dcid; |
| 5899 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5900 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5901 | |
| 5902 | 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] | 5903 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5904 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5905 | 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] | 5906 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5907 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5908 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5909 | cid_tid = quic_get_cid_tid(dcid); |
| 5910 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5911 | /* All the members must be initialized! */ |
| 5912 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5913 | dgram->buf = buf; |
| 5914 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5915 | dgram->dcid = dcid; |
| 5916 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5917 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5918 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5919 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5920 | 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] | 5921 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5922 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5923 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5924 | return 1; |
| 5925 | |
| 5926 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5927 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5928 | } |
| 5929 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5930 | /* Allocate a new stream descriptor with id <id>. The caller is responsible to |
| 5931 | * store the stream in the appropriate tree. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5932 | * |
| 5933 | * Returns the newly allocated instance on success or else NULL. |
| 5934 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5935 | struct qc_stream_desc *qc_stream_desc_new(uint64_t id, void *ctx) |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5936 | { |
| 5937 | struct qc_stream_desc *stream; |
| 5938 | |
| 5939 | stream = pool_alloc(pool_head_quic_conn_stream); |
| 5940 | if (!stream) |
| 5941 | return NULL; |
| 5942 | |
| 5943 | stream->by_id.key = id; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5944 | stream->by_id.node.leaf_p = NULL; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5945 | |
| 5946 | stream->buf = BUF_NULL; |
| 5947 | stream->acked_frms = EB_ROOT; |
| 5948 | stream->ack_offset = 0; |
| 5949 | stream->release = 0; |
| 5950 | stream->ctx = ctx; |
| 5951 | |
| 5952 | return stream; |
| 5953 | } |
| 5954 | |
| 5955 | /* Mark the stream descriptor <stream> as released by the upper layer. It will |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5956 | * be freed as soon as all its buffered data are acknowledged. In the meantime, |
| 5957 | * the stream is stored in the <qc> tree : thus it must have been removed from |
| 5958 | * any other tree before calling this function. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5959 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5960 | void qc_stream_desc_release(struct qc_stream_desc *stream, |
| 5961 | struct quic_conn *qc) |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5962 | { |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5963 | BUG_ON(stream->by_id.node.leaf_p); |
| 5964 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5965 | stream->release = 1; |
| 5966 | stream->ctx = NULL; |
| 5967 | |
| 5968 | if (!b_data(&stream->buf)) |
| 5969 | qc_stream_desc_free(stream); |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5970 | else |
| 5971 | eb64_insert(&qc->streams_by_id, &stream->by_id); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5972 | } |
| 5973 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 5974 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 5975 | void qc_notify_close(struct quic_conn *qc) |
| 5976 | { |
| 5977 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 5978 | return; |
| 5979 | |
| 5980 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 5981 | |
| 5982 | /* wake up the MUX */ |
| 5983 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 5984 | qc->conn->mux->wake(qc->conn); |
| 5985 | } |
| 5986 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5987 | /* Function to automatically activate QUIC traces on stdout. |
| 5988 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5989 | * Main use for now is in the docker image for QUIC interop testing. |
| 5990 | */ |
| 5991 | static void quic_init_stdout_traces(void) |
| 5992 | { |
| 5993 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5994 | trace_quic.sink = sink_find("stdout"); |
| 5995 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5996 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5997 | #endif |
| 5998 | } |
| 5999 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 6000 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6001 | /* |
| 6002 | * Local variables: |
| 6003 | * c-indent-level: 8 |
| 6004 | * c-basic-offset: 8 |
| 6005 | * End: |
| 6006 | */ |