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> |
| 53 | #include <haproxy/stream_interface.h> |
| 54 | #include <haproxy/task.h> |
| 55 | #include <haproxy/trace.h> |
| 56 | #include <haproxy/xprt_quic.h> |
| 57 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 58 | /* list of supported QUIC versions by this implementation */ |
| 59 | static int quic_supported_version[] = { |
| 60 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 61 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 62 | |
| 63 | /* placeholder, do not add entry after this */ |
| 64 | 0x0 |
| 65 | }; |
| 66 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 67 | /* This is the values of some QUIC transport parameters when absent. |
| 68 | * Should be used to initialize any transport parameters (local or remote) |
| 69 | * before updating them with customized values. |
| 70 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 71 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 72 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 73 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 74 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 75 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* trace source and events */ |
| 79 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 80 | const struct trace_source *src, |
| 81 | const struct ist where, const struct ist func, |
| 82 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 83 | |
| 84 | static const struct trace_event quic_trace_events[] = { |
| 85 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 86 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 87 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 90 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 91 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 92 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 93 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 96 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 97 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 102 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 103 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 104 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 106 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 107 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 108 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 109 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 110 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 111 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 112 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 113 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 114 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 115 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 116 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 117 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 118 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 119 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 120 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 121 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 122 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 123 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 124 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 125 | { /* end */ } |
| 126 | }; |
| 127 | |
| 128 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 129 | /* arg1 */ { /* already used by the connection */ }, |
| 130 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 131 | /* arg3 */ { }, |
| 132 | /* arg4 */ { } |
| 133 | }; |
| 134 | |
| 135 | static const struct name_desc quic_trace_decoding[] = { |
| 136 | #define QUIC_VERB_CLEAN 1 |
| 137 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 138 | { /* end */ } |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | struct trace_source trace_quic = { |
| 143 | .name = IST("quic"), |
| 144 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 145 | .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] | 146 | .default_cb = quic_trace, |
| 147 | .known_events = quic_trace_events, |
| 148 | .lockon_args = quic_trace_lockon_args, |
| 149 | .decoding = quic_trace_decoding, |
| 150 | .report_events = ~0, /* report everything by default */ |
| 151 | }; |
| 152 | |
| 153 | #define TRACE_SOURCE &trace_quic |
| 154 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 155 | |
| 156 | static BIO_METHOD *ha_quic_meth; |
| 157 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 158 | 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] | 159 | 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] | 160 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 161 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | 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] | 163 | DECLARE_POOL(pool_head_quic_connection_id, |
| 164 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 165 | 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] | 166 | 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] | 167 | 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] | 168 | 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] | 169 | 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] | 170 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 171 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 172 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 173 | 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] | 174 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 175 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 176 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 177 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 178 | int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 179 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 180 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 181 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 182 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 183 | /* Only for debug purpose */ |
| 184 | struct enc_debug_info { |
| 185 | unsigned char *payload; |
| 186 | size_t payload_len; |
| 187 | unsigned char *aad; |
| 188 | size_t aad_len; |
| 189 | uint64_t pn; |
| 190 | }; |
| 191 | |
| 192 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 193 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 194 | unsigned char *payload, size_t payload_len, |
| 195 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 196 | { |
| 197 | edi->payload = payload; |
| 198 | edi->payload_len = payload_len; |
| 199 | edi->aad = aad; |
| 200 | edi->aad_len = aad_len; |
| 201 | edi->pn = pn; |
| 202 | } |
| 203 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 204 | /* Trace callback for QUIC. |
| 205 | * These traces always expect that arg1, if non-null, is of type connection. |
| 206 | */ |
| 207 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 208 | const struct ist where, const struct ist func, |
| 209 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 210 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 211 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 212 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 213 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 214 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 216 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 218 | chunk_appendf(&trace_buf, "\n odcid"); |
| 219 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 220 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 221 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 222 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 223 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 227 | const enum ssl_encryption_level_t *level = a2; |
| 228 | const size_t *len = a3; |
| 229 | |
| 230 | if (level) { |
| 231 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 232 | |
| 233 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 234 | } |
| 235 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 236 | 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] | 237 | } |
| 238 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 239 | /* Initial read & write secrets. */ |
| 240 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 241 | const unsigned char *rx_sec = a2; |
| 242 | const unsigned char *tx_sec = a3; |
| 243 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 244 | tls_ctx = &qc->els[level].tls_ctx; |
| 245 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 246 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 247 | if (rx_sec) |
| 248 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 249 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 250 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 251 | if (tx_sec) |
| 252 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 253 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 257 | const enum ssl_encryption_level_t *level = a2; |
| 258 | const unsigned char *secret = a3; |
| 259 | const size_t *secret_len = a4; |
| 260 | |
| 261 | if (level) { |
| 262 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 263 | |
| 264 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 265 | if (secret && secret_len) |
| 266 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 267 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 268 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 269 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 274 | const enum ssl_encryption_level_t *level = a2; |
| 275 | const unsigned char *secret = a3; |
| 276 | const size_t *secret_len = a4; |
| 277 | |
| 278 | if (level) { |
| 279 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 280 | |
| 281 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 282 | if (secret && secret_len) |
| 283 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 284 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 285 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 286 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 290 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 291 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 292 | const struct list *l = a2; |
| 293 | |
| 294 | if (l) { |
| 295 | const struct quic_frame *frm; |
| 296 | list_for_each_entry(frm, l, list) |
| 297 | chunk_frm_appendf(&trace_buf, frm); |
| 298 | } |
| 299 | } |
| 300 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 301 | 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] | 302 | const struct quic_tx_packet *pkt = a2; |
| 303 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 304 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 305 | |
| 306 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 307 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 308 | 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] | 309 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 310 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 311 | (unsigned long long)qc->path->cwnd, |
| 312 | (unsigned long long)qc->path->prep_in_flight, |
| 313 | (unsigned long long)qc->path->in_flight, |
| 314 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 315 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 316 | } |
| 317 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 318 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 319 | if (pkt->pn_node.key != (uint64_t)-1) |
| 320 | 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] | 321 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 322 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 323 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 324 | (unsigned long long)qc->rx.bytes, |
| 325 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (room) { |
| 329 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 330 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 331 | (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] | 332 | } |
| 333 | } |
| 334 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 335 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 336 | const enum quic_handshake_state *state = a2; |
| 337 | const int *err = a3; |
| 338 | |
| 339 | if (state) |
| 340 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 341 | if (err) |
| 342 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 343 | } |
| 344 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 345 | 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] | 346 | const struct quic_rx_packet *pkt = a2; |
| 347 | const unsigned long *pktlen = a3; |
| 348 | const SSL *ssl = a4; |
| 349 | |
| 350 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 351 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 352 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 353 | if (pkt->pnl) |
| 354 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 355 | (unsigned long long)pkt->pn); |
| 356 | if (pkt->token_len) |
| 357 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 358 | (unsigned long long)pkt->token_len); |
| 359 | if (pkt->aad_len) |
| 360 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 361 | (unsigned long long)pkt->aad_len); |
| 362 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 363 | pkt->flags, (unsigned long long)pkt->len); |
| 364 | } |
| 365 | if (pktlen) |
| 366 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 367 | if (ssl) { |
| 368 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 369 | chunk_appendf(&trace_buf, " el=%c", |
| 370 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 375 | const struct quic_rx_packet *pkt = a2; |
| 376 | const struct quic_rx_crypto_frm *cf = a3; |
| 377 | const SSL *ssl = a4; |
| 378 | |
| 379 | if (pkt) |
| 380 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 381 | quic_packet_type_enc_level_char(pkt->type), |
| 382 | (unsigned long long)pkt->pn); |
| 383 | if (cf) |
| 384 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 385 | (unsigned long long)cf->offset_node.key, |
| 386 | (unsigned long long)cf->len); |
| 387 | if (ssl) { |
| 388 | 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] | 389 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 390 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 391 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 392 | |
| 393 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 394 | 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] | 395 | } |
| 396 | |
| 397 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 398 | const struct quic_frame *frm = a2; |
| 399 | |
| 400 | if (frm) |
| 401 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 402 | } |
| 403 | |
| 404 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 405 | const struct quic_enc_level *qel = a2; |
| 406 | |
| 407 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 408 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 409 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 410 | " 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] | 411 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 412 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 413 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 414 | (unsigned long long)qc->path->cwnd, |
| 415 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 416 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 417 | (unsigned long long)pktns->tx.in_flight, |
| 418 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 422 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 423 | const struct enc_debug_info *edi = a2; |
| 424 | |
| 425 | if (edi) |
| 426 | chunk_appendf(&trace_buf, |
| 427 | " payload=@%p payload_len=%llu" |
| 428 | " aad=@%p aad_len=%llu pn=%llu", |
| 429 | edi->payload, (unsigned long long)edi->payload_len, |
| 430 | edi->aad, (unsigned long long)edi->aad_len, |
| 431 | (unsigned long long)edi->pn); |
| 432 | } |
| 433 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 434 | if (mask & QUIC_EV_CONN_RMHP) { |
| 435 | const struct quic_rx_packet *pkt = a2; |
| 436 | |
| 437 | if (pkt) { |
| 438 | const int *ret = a3; |
| 439 | |
| 440 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 441 | if (ret && *ret) |
| 442 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 443 | pkt->pnl, (unsigned long long)pkt->pn); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 448 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 449 | const unsigned long *val1 = a3; |
| 450 | const unsigned long *val2 = a4; |
| 451 | |
| 452 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 453 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 454 | if (val1) |
| 455 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 456 | if (val2) |
| 457 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 458 | } |
| 459 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 460 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 461 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 462 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 463 | |
| 464 | if (s) |
| 465 | 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] | 466 | if (stream) |
| 467 | 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] | 468 | } |
| 469 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 470 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 471 | const unsigned int *rtt_sample = a2; |
| 472 | const unsigned int *ack_delay = a3; |
| 473 | const struct quic_loss *ql = a4; |
| 474 | |
| 475 | if (rtt_sample) |
| 476 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 477 | if (ack_delay) |
| 478 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 479 | if (ql) |
| 480 | chunk_appendf(&trace_buf, |
| 481 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 482 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 483 | } |
| 484 | if (mask & QUIC_EV_CONN_CC) { |
| 485 | const struct quic_cc_event *ev = a2; |
| 486 | const struct quic_cc *cc = a3; |
| 487 | |
| 488 | if (a2) |
| 489 | quic_cc_event_trace(&trace_buf, ev); |
| 490 | if (a3) |
| 491 | quic_cc_state_trace(&trace_buf, cc); |
| 492 | } |
| 493 | |
| 494 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 495 | const struct quic_pktns *pktns = a2; |
| 496 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 497 | |
| 498 | if (pktns) { |
| 499 | chunk_appendf(&trace_buf, " pktns=%s", |
| 500 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 501 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 502 | if (pktns->tx.loss_time) |
| 503 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 504 | 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] | 505 | } |
| 506 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 507 | struct quic_tx_packet *pkt; |
| 508 | |
| 509 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 510 | list_for_each_entry(pkt, lost_pkts, list) |
| 511 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | 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] | 516 | const struct quic_pktns *pktns = a2; |
| 517 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 518 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 520 | if (ifae_pkts) |
| 521 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 522 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 523 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 524 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 525 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 526 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 527 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 528 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 529 | if (pktns->tx.in_flight) |
| 530 | 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] | 531 | if (pktns->tx.loss_time) |
| 532 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 533 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 534 | } |
| 535 | if (mask & QUIC_EV_CONN_SPTO) { |
| 536 | if (pktns->tx.time_of_last_eliciting) |
| 537 | chunk_appendf(&trace_buf, " tole=%dms", |
| 538 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 539 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 540 | 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] | 541 | } |
| 542 | } |
| 543 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 544 | 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] | 545 | chunk_appendf(&trace_buf, |
| 546 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 551 | const struct quic_tx_packet *pkt = a2; |
| 552 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 553 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 554 | (unsigned long long)qc->path->cwnd, |
| 555 | (unsigned long long)qc->path->prep_in_flight, |
| 556 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 557 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 558 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 559 | (unsigned long)pkt->pn_node.key, |
| 560 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 561 | 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] | 562 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 563 | } |
| 564 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 565 | |
| 566 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 567 | const uint8_t *alert = a2; |
| 568 | const enum ssl_encryption_level_t *level = a3; |
| 569 | |
| 570 | if (alert) |
| 571 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 572 | if (level) |
| 573 | chunk_appendf(&trace_buf, " el=%c", |
| 574 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 575 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 576 | |
| 577 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 578 | const size_t *sz1 = a2; |
| 579 | const size_t *sz2 = a3; |
| 580 | const size_t *sz3 = a4; |
| 581 | |
| 582 | if (sz1) |
| 583 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 584 | if (sz2) |
| 585 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 586 | if (sz3) |
| 587 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 588 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 589 | |
| 590 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 591 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 592 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 593 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 594 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 595 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 596 | } |
| 597 | if (mask & QUIC_EV_CONN_LPKT) { |
| 598 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 599 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 600 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 601 | if (pkt) { |
| 602 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 603 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 604 | if (pkt->pn_node.key != (uint64_t)-1) |
| 605 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 606 | } |
| 607 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 608 | if (len) |
| 609 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | } |
| 613 | |
| 614 | /* 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] | 615 | 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] | 616 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 617 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 618 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 619 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 620 | return 1; |
| 621 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 622 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 623 | 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] | 624 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 625 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 626 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 627 | return 1; |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 633 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 634 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 635 | 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] | 636 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 637 | struct quic_pktns *pktns; |
| 638 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 639 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 640 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 641 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 642 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 643 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 644 | pktns = quic_loss_pktns(qc); |
| 645 | if (tick_isset(pktns->tx.loss_time)) { |
| 646 | qc->timer = pktns->tx.loss_time; |
| 647 | goto out; |
| 648 | } |
| 649 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 650 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 651 | * cancelled for a server which reached the anti-amplification limit. |
| 652 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 653 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 654 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 655 | 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] | 656 | qc->timer = TICK_ETERNITY; |
| 657 | goto out; |
| 658 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 659 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 660 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 661 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 662 | /* Timer cancellation. */ |
| 663 | qc->timer = TICK_ETERNITY; |
| 664 | goto out; |
| 665 | } |
| 666 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 667 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 668 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 669 | if (tick_isset(pto)) |
| 670 | qc->timer = pto; |
| 671 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 672 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 673 | if (tick_is_expired(qc->timer, now_ms)) { |
| 674 | 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] | 675 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 676 | } |
| 677 | else { |
| 678 | 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] | 679 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 680 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 681 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 682 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 685 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 686 | * connection. |
| 687 | * Return 1 if succeeded, 0 if not. |
| 688 | */ |
| 689 | static int quic_tls_key_update(struct quic_conn *qc) |
| 690 | { |
| 691 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 692 | struct quic_tls_secrets *rx, *tx; |
| 693 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 694 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 695 | |
| 696 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 697 | rx = &tls_ctx->rx; |
| 698 | tx = &tls_ctx->tx; |
| 699 | nxt_rx = &qc->ku.nxt_rx; |
| 700 | nxt_tx = &qc->ku.nxt_tx; |
| 701 | |
| 702 | /* Prepare new RX secrets */ |
| 703 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 704 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 705 | 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] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 710 | nxt_rx->key, nxt_rx->keylen, |
| 711 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 712 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 713 | 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] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* Prepare new TX secrets */ |
| 718 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 719 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 720 | 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] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 725 | nxt_tx->key, nxt_tx->keylen, |
| 726 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 727 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 728 | 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] | 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | return 1; |
| 733 | } |
| 734 | |
| 735 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 736 | * Must be used after having updated them. |
| 737 | * Always succeeds. |
| 738 | */ |
| 739 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 740 | { |
| 741 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 742 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 743 | |
| 744 | /* Rotate the RX secrets */ |
| 745 | curr_secret = tls_ctx->rx.secret; |
| 746 | curr_iv = tls_ctx->rx.iv; |
| 747 | curr_key = tls_ctx->rx.key; |
| 748 | |
| 749 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 750 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 751 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 752 | |
| 753 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 754 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 755 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 756 | |
| 757 | qc->ku.prv_rx.secret = curr_secret; |
| 758 | qc->ku.prv_rx.iv = curr_iv; |
| 759 | qc->ku.prv_rx.key = curr_key; |
| 760 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 761 | |
| 762 | /* Update the TX secrets */ |
| 763 | curr_secret = tls_ctx->tx.secret; |
| 764 | curr_iv = tls_ctx->tx.iv; |
| 765 | curr_key = tls_ctx->tx.key; |
| 766 | |
| 767 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 768 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 769 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 770 | |
| 771 | qc->ku.nxt_tx.secret = curr_secret; |
| 772 | qc->ku.nxt_tx.iv = curr_iv; |
| 773 | qc->ku.nxt_tx.key = curr_key; |
| 774 | } |
| 775 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 776 | #ifndef OPENSSL_IS_BORINGSSL |
| 777 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 778 | const uint8_t *read_secret, |
| 779 | const uint8_t *write_secret, size_t secret_len) |
| 780 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 781 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 782 | 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] | 783 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 784 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 785 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 786 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 787 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 788 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 789 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 790 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 791 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 792 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 793 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 794 | 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] | 795 | goto err; |
| 796 | } |
| 797 | |
| 798 | rx = &tls_ctx->rx; |
| 799 | tx = &tls_ctx->tx; |
| 800 | |
| 801 | rx->aead = tx->aead = tls_aead(cipher); |
| 802 | rx->md = tx->md = tls_md(cipher); |
| 803 | rx->hp = tx->hp = tls_hp(cipher); |
| 804 | |
| 805 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 806 | 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] | 807 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 808 | 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] | 809 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 812 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 813 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 814 | goto err; |
| 815 | } |
| 816 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 817 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 818 | * listener. Note that a listener derives only RX secrets for this |
| 819 | * level. |
| 820 | */ |
| 821 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 822 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 823 | |
| 824 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 825 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 826 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 827 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 828 | 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] | 829 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 830 | 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] | 831 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 834 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 835 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 836 | goto err; |
| 837 | } |
| 838 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 839 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 840 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 841 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 842 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 843 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 844 | /* 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] | 845 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 846 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 847 | 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] | 848 | goto err; |
| 849 | } |
| 850 | |
| 851 | memcpy(rx->secret, read_secret, secret_len); |
| 852 | rx->secretlen = secret_len; |
| 853 | memcpy(tx->secret, write_secret, secret_len); |
| 854 | tx->secretlen = secret_len; |
| 855 | /* Initialize all the secret keys lengths */ |
| 856 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 857 | /* Prepare the next key update */ |
| 858 | if (!quic_tls_key_update(qc)) |
| 859 | goto err; |
| 860 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 861 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 862 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 863 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 864 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 865 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 866 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 867 | |
| 868 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 869 | 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] | 870 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 871 | } |
| 872 | #else |
| 873 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 874 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 875 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 876 | */ |
| 877 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 878 | const SSL_CIPHER *cipher, |
| 879 | const uint8_t *secret, size_t secret_len) |
| 880 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 881 | 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] | 882 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 883 | &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] | 884 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 885 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 886 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 887 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 888 | goto out; |
| 889 | } |
| 890 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 891 | tls_ctx->rx.aead = tls_aead(cipher); |
| 892 | tls_ctx->rx.md = tls_md(cipher); |
| 893 | tls_ctx->rx.hp = tls_hp(cipher); |
| 894 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 895 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 896 | goto err; |
| 897 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 898 | 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] | 899 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 900 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 901 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 902 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 903 | 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] | 904 | goto err; |
| 905 | } |
| 906 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 907 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 908 | const unsigned char *buf; |
| 909 | size_t buflen; |
| 910 | |
| 911 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 912 | if (!buflen) |
| 913 | goto err; |
| 914 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 915 | 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] | 916 | goto err; |
| 917 | } |
| 918 | |
| 919 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 920 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 921 | 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] | 922 | |
| 923 | return 1; |
| 924 | |
| 925 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 926 | 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] | 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 931 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 932 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 933 | */ |
| 934 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 935 | const SSL_CIPHER *cipher, |
| 936 | const uint8_t *secret, size_t secret_len) |
| 937 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 938 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 939 | 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] | 940 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 941 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 942 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 943 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 944 | goto out; |
| 945 | } |
| 946 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 947 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 948 | goto err; |
| 949 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 950 | tls_ctx->tx.aead = tls_aead(cipher); |
| 951 | tls_ctx->tx.md = tls_md(cipher); |
| 952 | tls_ctx->tx.hp = tls_hp(cipher); |
| 953 | |
| 954 | 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] | 955 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 956 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 957 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 958 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 959 | 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] | 960 | goto err; |
| 961 | } |
| 962 | |
| 963 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 964 | 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] | 965 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 966 | return 1; |
| 967 | |
| 968 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 969 | 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] | 970 | return 0; |
| 971 | } |
| 972 | #endif |
| 973 | |
| 974 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 975 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 976 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 977 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 978 | * store all the data. |
| 979 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 980 | */ |
| 981 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 982 | const unsigned char *data, size_t len) |
| 983 | { |
| 984 | struct quic_crypto_buf **qcb; |
| 985 | /* The remaining byte to store in CRYPTO buffers. */ |
| 986 | size_t cf_offset, cf_len, *nb_buf; |
| 987 | unsigned char *pos; |
| 988 | |
| 989 | nb_buf = &qel->tx.crypto.nb_buf; |
| 990 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 991 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 992 | cf_len = len; |
| 993 | |
| 994 | while (len) { |
| 995 | size_t to_copy, room; |
| 996 | |
| 997 | pos = (*qcb)->data + (*qcb)->sz; |
| 998 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 999 | to_copy = len > room ? room : len; |
| 1000 | if (to_copy) { |
| 1001 | memcpy(pos, data, to_copy); |
| 1002 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1003 | qel->tx.crypto.sz += to_copy; |
| 1004 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1005 | len -= to_copy; |
| 1006 | data += to_copy; |
| 1007 | } |
| 1008 | else { |
| 1009 | struct quic_crypto_buf **tmp; |
| 1010 | |
| 1011 | tmp = realloc(qel->tx.crypto.bufs, |
| 1012 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1013 | if (tmp) { |
| 1014 | qel->tx.crypto.bufs = tmp; |
| 1015 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1016 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1017 | if (!*qcb) |
| 1018 | return 0; |
| 1019 | |
| 1020 | (*qcb)->sz = 0; |
| 1021 | ++*nb_buf; |
| 1022 | } |
| 1023 | else { |
| 1024 | break; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1030 | * have been buffered. |
| 1031 | */ |
| 1032 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1033 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1034 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1035 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1036 | /* There is at most one CRYPTO frame in this packet number |
| 1037 | * space. Let's look for it. |
| 1038 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1039 | 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] | 1040 | if (frm->type != QUIC_FT_CRYPTO) |
| 1041 | continue; |
| 1042 | |
| 1043 | /* Found */ |
| 1044 | found = frm; |
| 1045 | break; |
| 1046 | } |
| 1047 | |
| 1048 | if (found) { |
| 1049 | found->crypto.len += cf_len; |
| 1050 | } |
| 1051 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1052 | frm = pool_alloc(pool_head_quic_frame); |
| 1053 | if (!frm) |
| 1054 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1055 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1056 | frm->type = QUIC_FT_CRYPTO; |
| 1057 | frm->crypto.offset = cf_offset; |
| 1058 | frm->crypto.len = cf_len; |
| 1059 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1060 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1061 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | return len == 0; |
| 1065 | } |
| 1066 | |
| 1067 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1068 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1069 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1070 | { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1071 | qc->err_code = QC_ERR_CRYPTO_ERROR | alert; |
| 1072 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1073 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1076 | /* Set the application for <qc> QUIC connection. |
| 1077 | * Return 1 if succeeded, 0 if not. |
| 1078 | */ |
| 1079 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1080 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1081 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1082 | qc->app_ops = &h3_ops; |
| 1083 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1084 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1085 | else |
| 1086 | return 0; |
| 1087 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1088 | return 1; |
| 1089 | } |
| 1090 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1091 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1092 | * wants to provide the QUIC layer with CRYPTO data. |
| 1093 | * Returns 1 if succeeded, 0 if not. |
| 1094 | */ |
| 1095 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1096 | const uint8_t *data, size_t len) |
| 1097 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1098 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1099 | enum quic_tls_enc_level tel; |
| 1100 | struct quic_enc_level *qel; |
| 1101 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1102 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1103 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1104 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1105 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1106 | goto out; |
| 1107 | } |
| 1108 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1109 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1110 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1111 | 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] | 1112 | goto err; |
| 1113 | } |
| 1114 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1115 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1116 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1117 | 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] | 1118 | goto err; |
| 1119 | } |
| 1120 | |
| 1121 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1122 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1123 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1124 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1125 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1126 | return 1; |
| 1127 | |
| 1128 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1129 | 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] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | int ha_quic_flush_flight(SSL *ssl) |
| 1134 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1135 | 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] | 1136 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1137 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1138 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1139 | |
| 1140 | return 1; |
| 1141 | } |
| 1142 | |
| 1143 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1144 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1145 | 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] | 1146 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1147 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1148 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1149 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1150 | return 1; |
| 1151 | } |
| 1152 | |
| 1153 | /* QUIC TLS methods */ |
| 1154 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1155 | #ifdef OPENSSL_IS_BORINGSSL |
| 1156 | .set_read_secret = ha_set_rsec, |
| 1157 | .set_write_secret = ha_set_wsec, |
| 1158 | #else |
| 1159 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1160 | #endif |
| 1161 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1162 | .flush_flight = ha_quic_flush_flight, |
| 1163 | .send_alert = ha_quic_send_alert, |
| 1164 | }; |
| 1165 | |
| 1166 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1167 | * Returns an error count. |
| 1168 | */ |
| 1169 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1170 | { |
| 1171 | struct proxy *curproxy = bind_conf->frontend; |
| 1172 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1173 | int cfgerr = 0; |
| 1174 | |
| 1175 | #if 0 |
| 1176 | /* XXX Did not manage to use this. */ |
| 1177 | const char *ciphers = |
| 1178 | "TLS_AES_128_GCM_SHA256:" |
| 1179 | "TLS_AES_256_GCM_SHA384:" |
| 1180 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1181 | "TLS_AES_128_CCM_SHA256"; |
| 1182 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1183 | const char *groups = "X25519:P-256:P-384:P-521"; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1184 | long options = |
| 1185 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1186 | SSL_OP_SINGLE_ECDH_USE | |
| 1187 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1188 | SSL_CTX *ctx; |
| 1189 | |
| 1190 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1191 | bind_conf->initial_ctx = ctx; |
| 1192 | |
| 1193 | SSL_CTX_set_options(ctx, options); |
| 1194 | #if 0 |
| 1195 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1196 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1197 | "for bind '%s' at [%s:%d].\n", |
| 1198 | curproxy->id, ciphers, |
| 1199 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1200 | cfgerr++; |
| 1201 | } |
| 1202 | #endif |
| 1203 | |
| 1204 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1205 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1206 | "for bind '%s' at [%s:%d].\n", |
| 1207 | curproxy->id, groups, |
| 1208 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1209 | cfgerr++; |
| 1210 | } |
| 1211 | |
| 1212 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1213 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1214 | 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] | 1215 | |
| 1216 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1217 | #ifdef OPENSSL_IS_BORINGSSL |
| 1218 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1219 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1220 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1221 | if (bind_conf->ssl_conf.early_data) { |
| 1222 | 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] | 1223 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1224 | } |
| 1225 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1226 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1227 | #else |
| 1228 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1229 | #endif |
| 1230 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1231 | #endif |
| 1232 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1233 | |
| 1234 | return cfgerr; |
| 1235 | } |
| 1236 | |
| 1237 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1238 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1239 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1240 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1241 | */ |
| 1242 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1243 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1244 | { |
| 1245 | uint64_t expected_pn = largest_pn + 1; |
| 1246 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1247 | uint64_t pn_hwin = pn_win / 2; |
| 1248 | uint64_t pn_mask = pn_win - 1; |
| 1249 | uint64_t candidate_pn; |
| 1250 | |
| 1251 | |
| 1252 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1253 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1254 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1255 | candidate_pn + pn_hwin <= expected_pn) |
| 1256 | return candidate_pn + pn_win; |
| 1257 | |
| 1258 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1259 | return candidate_pn - pn_win; |
| 1260 | |
| 1261 | return candidate_pn; |
| 1262 | } |
| 1263 | |
| 1264 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1265 | * cryptographic context. |
| 1266 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1267 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1268 | * <end> points to one byte past the end of this packet. |
| 1269 | * Returns 1 if succeeded, 0 if not. |
| 1270 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1271 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1272 | 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] | 1273 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1274 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1275 | { |
| 1276 | int ret, outlen, i, pnlen; |
| 1277 | uint64_t packet_number; |
| 1278 | uint32_t truncated_pn = 0; |
| 1279 | unsigned char mask[5] = {0}; |
| 1280 | unsigned char *sample; |
| 1281 | EVP_CIPHER_CTX *cctx; |
| 1282 | unsigned char *hp_key; |
| 1283 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1284 | /* Check there is enough data in this packet. */ |
| 1285 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1286 | 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] | 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | cctx = EVP_CIPHER_CTX_new(); |
| 1291 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1292 | 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] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | ret = 0; |
| 1297 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1298 | |
| 1299 | hp_key = tls_ctx->rx.hp_key; |
| 1300 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1301 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1302 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1303 | 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] | 1304 | goto out; |
| 1305 | } |
| 1306 | |
| 1307 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1308 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1309 | for (i = 0; i < pnlen; i++) { |
| 1310 | pn[i] ^= mask[i + 1]; |
| 1311 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1312 | } |
| 1313 | |
| 1314 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1315 | /* Store remaining information for this unprotected header */ |
| 1316 | pkt->pn = packet_number; |
| 1317 | pkt->pnl = pnlen; |
| 1318 | |
| 1319 | ret = 1; |
| 1320 | |
| 1321 | out: |
| 1322 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1323 | |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1328 | * address, with <payload_len> as payload length, <aad> as address of |
| 1329 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1330 | * context. |
| 1331 | * Returns 1 if succeeded, 0 if not. |
| 1332 | */ |
| 1333 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1334 | 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] | 1335 | 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] | 1336 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame^] | 1337 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1338 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1339 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1340 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1341 | |
| 1342 | 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] | 1343 | 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] | 1344 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | 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] | 1348 | 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] | 1349 | 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] | 1350 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1354 | |
| 1355 | err: |
| 1356 | 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] | 1357 | 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] | 1358 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1362 | * Returns 1 if succeeded, 0 if not. |
| 1363 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1364 | 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] | 1365 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1366 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame^] | 1367 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1368 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1369 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1370 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1371 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1372 | |
| 1373 | kp_changed = 0; |
| 1374 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1375 | /* The two tested bits are not at the same position, |
| 1376 | * this is why they are first both inversed. |
| 1377 | */ |
| 1378 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1379 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1380 | /* The lowest packet number of a previous key phase |
| 1381 | * cannot be null if it really stores previous key phase |
| 1382 | * secrets. |
| 1383 | */ |
| 1384 | if (!pkt->qc->ku.prv_rx.pn) |
| 1385 | return 0; |
| 1386 | |
| 1387 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1388 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1389 | } |
| 1390 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1391 | /* Next key phase */ |
| 1392 | kp_changed = 1; |
| 1393 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1394 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1395 | } |
| 1396 | } |
| 1397 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1398 | |
| 1399 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1400 | return 0; |
| 1401 | |
| 1402 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1403 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1404 | tls_ctx->rx.ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1405 | if (!ret) |
| 1406 | return 0; |
| 1407 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1408 | /* Update the keys only if the packet decryption succeeded. */ |
| 1409 | if (kp_changed) { |
| 1410 | quic_tls_rotate_keys(pkt->qc); |
| 1411 | /* Toggle the Key Phase bit */ |
| 1412 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1413 | /* Store the lowest packet number received for the current key phase */ |
| 1414 | tls_ctx->rx.pn = pkt->pn; |
| 1415 | /* Prepare the next key update */ |
| 1416 | if (!quic_tls_key_update(pkt->qc)) |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1420 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1421 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1422 | |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 1426 | /* Free the stream descriptor <stream> buffer. This function should be used |
| 1427 | * when all its data have been acknowledged. If the stream was released by the |
| 1428 | * upper layer, the stream descriptor will be freed. |
| 1429 | * |
| 1430 | * Returns 0 if the stream was not freed else non-zero. |
| 1431 | */ |
| 1432 | static int qc_stream_desc_free(struct qc_stream_desc *stream) |
| 1433 | { |
| 1434 | b_free(&stream->buf); |
| 1435 | offer_buffers(NULL, 1); |
| 1436 | |
| 1437 | if (stream->release) { |
| 1438 | eb64_delete(&stream->by_id); |
| 1439 | pool_free(pool_head_quic_conn_stream, stream); |
| 1440 | |
| 1441 | return 1; |
| 1442 | } |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1447 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1448 | * |
| 1449 | * 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] | 1450 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1451 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1452 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1453 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1454 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1455 | struct eb64_node *frm_node; |
| 1456 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1457 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1458 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1459 | while (frm_node) { |
| 1460 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1461 | struct quic_frame *frm; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1462 | |
| 1463 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1464 | if (strm->offset.key > stream->ack_offset) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1465 | break; |
| 1466 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1467 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1468 | qc, strm, stream); |
| 1469 | |
| 1470 | if (strm->offset.key + strm->len > stream->ack_offset) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1471 | const size_t diff = strm->offset.key + strm->len - |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1472 | stream->ack_offset; |
| 1473 | stream->ack_offset += diff; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1474 | b_del(strm->buf, diff); |
| 1475 | ret = 1; |
| 1476 | } |
| 1477 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1478 | frm_node = eb64_next(frm_node); |
| 1479 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1480 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1481 | frm = container_of(strm, struct quic_frame, stream); |
| 1482 | LIST_DELETE(&frm->list); |
| 1483 | quic_tx_packet_refdec(frm->pkt); |
| 1484 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1485 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1486 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1487 | if (!b_data(&stream->buf)) |
| 1488 | qc_stream_desc_free(stream); |
| 1489 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1490 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1493 | /* 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] | 1494 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1495 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1496 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1497 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1498 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1499 | 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] | 1500 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1501 | switch (frm->type) { |
| 1502 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1503 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1504 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1505 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1506 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1507 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1508 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1509 | * 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] | 1510 | * lookup. First search in the MUX then in the released stream |
| 1511 | * list. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1512 | * |
| 1513 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1514 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1515 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1516 | if (qc->mux_state == QC_MUX_READY) |
| 1517 | stream = qcc_get_stream(qc->qcc, strm_frm->id); |
| 1518 | if (!stream) { |
| 1519 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1520 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1521 | } |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1522 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1523 | if (!stream) { |
| 1524 | 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] | 1525 | LIST_DELETE(&frm->list); |
| 1526 | quic_tx_packet_refdec(frm->pkt); |
| 1527 | pool_free(pool_head_quic_frame, frm); |
| 1528 | |
| 1529 | /* early return */ |
| 1530 | return; |
| 1531 | } |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1532 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1533 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1534 | if (strm_frm->offset.key <= stream->ack_offset) { |
| 1535 | if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) { |
| 1536 | const size_t diff = strm_frm->offset.key + strm_frm->len - |
| 1537 | stream->ack_offset; |
| 1538 | stream->ack_offset += diff; |
| 1539 | b_del(strm_frm->buf, diff); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1540 | stream_acked = 1; |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1541 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1542 | if (!b_data(strm_frm->buf)) { |
| 1543 | if (qc_stream_desc_free(stream)) { |
| 1544 | /* early return */ |
| 1545 | return; |
| 1546 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1547 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1550 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1551 | qc, strm_frm, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1552 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1553 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1554 | pool_free(pool_head_quic_frame, frm); |
| 1555 | } |
| 1556 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1557 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1558 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1559 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1560 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1561 | } |
| 1562 | break; |
| 1563 | default: |
| 1564 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1565 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1566 | pool_free(pool_head_quic_frame, frm); |
| 1567 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1568 | |
| 1569 | if (stream_acked) { |
| 1570 | struct qcc *qcc = qc->qcc; |
| 1571 | |
| 1572 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1573 | tasklet_wakeup(qcc->subs->tasklet); |
| 1574 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1575 | if (!qcc->subs->events) |
| 1576 | qcc->subs = NULL; |
| 1577 | } |
| 1578 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1582 | * deallocating them, and their TX frames. |
| 1583 | * Returns the last node reached to be used for the next range. |
| 1584 | * May be NULL if <largest> node could not be found. |
| 1585 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1586 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1587 | struct eb_root *pkts, |
| 1588 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1589 | struct list *newly_acked_pkts, |
| 1590 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1591 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1592 | { |
| 1593 | struct eb64_node *node; |
| 1594 | struct quic_tx_packet *pkt; |
| 1595 | |
| 1596 | if (largest_node) |
| 1597 | node = largest_node; |
| 1598 | else { |
| 1599 | node = eb64_lookup(pkts, largest); |
| 1600 | while (!node && largest > smallest) { |
| 1601 | node = eb64_lookup(pkts, --largest); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1606 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1607 | |
| 1608 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1609 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1610 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1611 | 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] | 1612 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1613 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1614 | node = eb64_prev(node); |
| 1615 | eb64_delete(&pkt->pn_node); |
| 1616 | } |
| 1617 | |
| 1618 | return node; |
| 1619 | } |
| 1620 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1621 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1622 | * 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] | 1623 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1624 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1625 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1626 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1627 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1628 | struct quic_frame *frm, *frmbak; |
| 1629 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1630 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1631 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1632 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1633 | quic_tx_packet_refdec(frm->pkt); |
| 1634 | /* This frame is not freed but removed from its packet */ |
| 1635 | frm->pkt = NULL; |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1636 | 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] | 1637 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1640 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1641 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1642 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1643 | /* Free <pkt> TX packet and its attached frames. |
| 1644 | * This is the responsability of the caller to remove this packet of |
| 1645 | * any data structure it was possibly attached to. |
| 1646 | */ |
| 1647 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1648 | { |
| 1649 | struct quic_frame *frm, *frmbak; |
| 1650 | |
| 1651 | if (!pkt) |
| 1652 | return; |
| 1653 | |
| 1654 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1655 | LIST_DELETE(&frm->list); |
| 1656 | pool_free(pool_head_quic_frame, frm); |
| 1657 | } |
| 1658 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1659 | } |
| 1660 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1661 | /* Free the TX packets of <pkts> list */ |
| 1662 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1663 | { |
| 1664 | struct quic_tx_packet *pkt, *tmp; |
| 1665 | |
| 1666 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1667 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1668 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1669 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1673 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1674 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1675 | * updating the range which contains <largest_acked_pn>. |
| 1676 | * Never fails. |
| 1677 | */ |
| 1678 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1679 | int64_t largest_acked_pn) |
| 1680 | { |
| 1681 | struct eb64_node *ar, *next_ar; |
| 1682 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1683 | |
| 1684 | ar = eb64_first(&arngs->root); |
| 1685 | while (ar) { |
| 1686 | struct quic_arng_node *ar_node; |
| 1687 | |
| 1688 | next_ar = eb64_next(ar); |
| 1689 | ar_node = eb64_entry(&ar->node, struct quic_arng_node, first); |
| 1690 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1691 | break; |
| 1692 | |
| 1693 | if (largest_acked_pn < ar_node->last) { |
| 1694 | eb64_delete(ar); |
| 1695 | ar_node->first.key = largest_acked_pn + 1; |
| 1696 | eb64_insert(&arngs->root, ar); |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | eb64_delete(ar); |
| 1701 | pool_free(pool_head_quic_arng, ar_node); |
| 1702 | arngs->sz--; |
| 1703 | ar = next_ar; |
| 1704 | } |
| 1705 | } |
| 1706 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1707 | /* Send a packet ack event nofication for each newly acked packet of |
| 1708 | * <newly_acked_pkts> list and free them. |
| 1709 | * Always succeeds. |
| 1710 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1711 | 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] | 1712 | struct list *newly_acked_pkts) |
| 1713 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1714 | struct quic_tx_packet *pkt, *tmp; |
| 1715 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1716 | |
| 1717 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1718 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1719 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1720 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1721 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1722 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1723 | /* If this packet contained an ACK frame, proceed to the |
| 1724 | * acknowledging of range of acks from the largest acknowledged |
| 1725 | * packet number which was sent in an ACK frame by this packet. |
| 1726 | */ |
| 1727 | if (pkt->largest_acked_pn != -1) |
| 1728 | 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] | 1729 | ev.ack.acked = pkt->in_flight_len; |
| 1730 | ev.ack.time_sent = pkt->time_sent; |
| 1731 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1732 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1733 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1734 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | } |
| 1738 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1739 | /* Release all the frames attached to <pktns> packet number space */ |
| 1740 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1741 | { |
| 1742 | struct quic_frame *frm, *frmbak; |
| 1743 | |
| 1744 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1745 | LIST_DELETE(&frm->list); |
| 1746 | pool_free(pool_head_quic_frame, frm); |
| 1747 | } |
| 1748 | } |
| 1749 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1750 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1751 | * their TX frames. |
| 1752 | * Send a packet loss event to the congestion controller if |
| 1753 | * in flight packet have been lost. |
| 1754 | * Also frees the packet in <pkts> list. |
| 1755 | * Never fails. |
| 1756 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1757 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1758 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1759 | struct list *pkts, |
| 1760 | uint64_t now_us) |
| 1761 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1762 | 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] | 1763 | uint64_t lost_bytes; |
| 1764 | |
| 1765 | lost_bytes = 0; |
| 1766 | oldest_lost = newest_lost = NULL; |
| 1767 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1768 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1769 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1770 | lost_bytes += pkt->in_flight_len; |
| 1771 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1772 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1773 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1774 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1775 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1776 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1777 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1778 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1779 | if (!oldest_lost) { |
| 1780 | oldest_lost = newest_lost = pkt; |
| 1781 | } |
| 1782 | else { |
| 1783 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1784 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1785 | newest_lost = pkt; |
| 1786 | } |
| 1787 | } |
| 1788 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1789 | if (newest_lost) { |
| 1790 | /* Sent a congestion event to the controller */ |
| 1791 | struct quic_cc_event ev = { |
| 1792 | .type = QUIC_CC_EVT_LOSS, |
| 1793 | .loss.time_sent = newest_lost->time_sent, |
| 1794 | }; |
| 1795 | |
| 1796 | quic_cc_event(&qc->path->cc, &ev); |
| 1797 | } |
| 1798 | |
| 1799 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1800 | * We must check if we are experiencing a persistent congestion. |
| 1801 | * If this is the case, the congestion controller must re-enter |
| 1802 | * slow start state. |
| 1803 | */ |
| 1804 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1805 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1806 | |
| 1807 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1808 | now_ms, qc->max_ack_delay)) |
| 1809 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1810 | } |
| 1811 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1812 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1813 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1814 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1815 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1820 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1821 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1822 | * space if any not deemed lost. |
| 1823 | * Should be called after having received an ACK frame with newly acknowledged |
| 1824 | * packets or when the the loss detection timer has expired. |
| 1825 | * Always succeeds. |
| 1826 | */ |
| 1827 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1828 | struct quic_conn *qc, |
| 1829 | struct list *lost_pkts) |
| 1830 | { |
| 1831 | struct eb_root *pkts; |
| 1832 | struct eb64_node *node; |
| 1833 | struct quic_loss *ql; |
| 1834 | unsigned int loss_delay; |
| 1835 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1836 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1837 | pkts = &pktns->tx.pkts; |
| 1838 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1839 | if (eb_is_empty(pkts)) |
| 1840 | goto out; |
| 1841 | |
| 1842 | ql = &qc->path->loss; |
| 1843 | 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] | 1844 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1845 | |
| 1846 | node = eb64_first(pkts); |
| 1847 | while (node) { |
| 1848 | struct quic_tx_packet *pkt; |
| 1849 | int64_t largest_acked_pn; |
| 1850 | unsigned int loss_time_limit, time_sent; |
| 1851 | |
| 1852 | 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] | 1853 | largest_acked_pn = pktns->rx.largest_acked_pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1854 | node = eb64_next(node); |
| 1855 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1856 | break; |
| 1857 | |
| 1858 | time_sent = pkt->time_sent; |
| 1859 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1860 | if (tick_is_le(time_sent, now_ms) || |
| 1861 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1862 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1863 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1864 | } |
| 1865 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1866 | if (tick_isset(pktns->tx.loss_time)) |
| 1867 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1868 | else |
| 1869 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1874 | 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] | 1875 | } |
| 1876 | |
| 1877 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1878 | * 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] | 1879 | * 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] | 1880 | * acked ack-eliciting packet. |
| 1881 | * Return 1, if succeeded, 0 if not. |
| 1882 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1883 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1884 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1885 | struct quic_enc_level *qel, |
| 1886 | unsigned int *rtt_sample, |
| 1887 | const unsigned char **pos, const unsigned char *end) |
| 1888 | { |
| 1889 | struct quic_ack *ack = &frm->ack; |
| 1890 | uint64_t smallest, largest; |
| 1891 | struct eb_root *pkts; |
| 1892 | struct eb64_node *largest_node; |
| 1893 | unsigned int time_sent, pkt_flags; |
| 1894 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1895 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1896 | |
| 1897 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1898 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1899 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1900 | goto err; |
| 1901 | } |
| 1902 | |
| 1903 | if (ack->first_ack_range > ack->largest_ack) { |
| 1904 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1905 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1906 | goto err; |
| 1907 | } |
| 1908 | |
| 1909 | largest = ack->largest_ack; |
| 1910 | smallest = largest - ack->first_ack_range; |
| 1911 | pkts = &qel->pktns->tx.pkts; |
| 1912 | pkt_flags = 0; |
| 1913 | largest_node = NULL; |
| 1914 | time_sent = 0; |
| 1915 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1916 | 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] | 1917 | largest_node = eb64_lookup(pkts, largest); |
| 1918 | if (!largest_node) { |
| 1919 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1920 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1921 | } |
| 1922 | else { |
| 1923 | time_sent = eb64_entry(&largest_node->node, |
| 1924 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1925 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1929 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1930 | do { |
| 1931 | uint64_t gap, ack_range; |
| 1932 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1933 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1934 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1935 | if (!ack->ack_range_num--) |
| 1936 | break; |
| 1937 | |
| 1938 | if (!quic_dec_int(&gap, pos, end)) |
| 1939 | goto err; |
| 1940 | |
| 1941 | if (smallest < gap + 2) { |
| 1942 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1943 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1944 | goto err; |
| 1945 | } |
| 1946 | |
| 1947 | largest = smallest - gap - 2; |
| 1948 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1949 | goto err; |
| 1950 | |
| 1951 | if (largest < ack_range) { |
| 1952 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1953 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1954 | goto err; |
| 1955 | } |
| 1956 | |
| 1957 | /* Do not use this node anymore. */ |
| 1958 | largest_node = NULL; |
| 1959 | /* Next range */ |
| 1960 | smallest = largest - ack_range; |
| 1961 | |
| 1962 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1963 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1964 | } while (1); |
| 1965 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1966 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1967 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1968 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1972 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1973 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1974 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1975 | 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] | 1976 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1977 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1978 | if (quic_peer_validated_addr(qc)) |
| 1979 | qc->path->loss.pto_count = 0; |
| 1980 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | |
| 1984 | return 1; |
| 1985 | |
| 1986 | err: |
| 1987 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1988 | 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] | 1989 | return 0; |
| 1990 | } |
| 1991 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1992 | /* This function gives the detail of the SSL error. It is used only |
| 1993 | * if the debug mode and the verbose mode are activated. It dump all |
| 1994 | * the SSL error until the stack was empty. |
| 1995 | */ |
| 1996 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1997 | { |
| 1998 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1999 | while (1) { |
| 2000 | unsigned long ret; |
| 2001 | |
| 2002 | ret = ERR_get_error(); |
| 2003 | if (!ret) |
| 2004 | return; |
| 2005 | |
| 2006 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2007 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2012 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2013 | const char **str, int *len); |
| 2014 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2015 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2016 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2017 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2018 | * Return 1 if succeeded, 0 if not. |
| 2019 | */ |
| 2020 | 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] | 2021 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2022 | const unsigned char *data, size_t len, |
| 2023 | struct quic_rx_packet *pkt, |
| 2024 | struct quic_rx_crypto_frm *cf) |
| 2025 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2026 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2027 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2028 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2029 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2030 | qc = ctx->qc; |
| 2031 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2032 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2033 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2034 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2035 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2036 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2037 | goto err; |
| 2038 | } |
| 2039 | |
| 2040 | el->rx.crypto.offset += len; |
| 2041 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2042 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2043 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2044 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2045 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2046 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2047 | if (ssl_err != 1) { |
| 2048 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2049 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2050 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2051 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2052 | goto out; |
| 2053 | } |
| 2054 | |
| 2055 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2056 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2057 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2058 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2059 | goto err; |
| 2060 | } |
| 2061 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2062 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2063 | /* I/O callback switch */ |
| 2064 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2065 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2066 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2067 | /* The connection is ready to be accepted. */ |
| 2068 | quic_accept_push_qc(qc); |
| 2069 | } |
| 2070 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2071 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2072 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2073 | } else { |
| 2074 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2075 | if (ssl_err != 1) { |
| 2076 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2077 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2078 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2079 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2080 | goto out; |
| 2081 | } |
| 2082 | |
| 2083 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2084 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2085 | goto err; |
| 2086 | } |
| 2087 | |
| 2088 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2089 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2090 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2091 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2092 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2093 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2094 | return 1; |
| 2095 | |
| 2096 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2097 | 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] | 2098 | return 0; |
| 2099 | } |
| 2100 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2101 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 2102 | * <pkt> RX packet. |
| 2103 | * Return it if succeeded, NULL if not. |
| 2104 | */ |
| 2105 | static inline |
| 2106 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 2107 | struct quic_rx_packet *pkt) |
| 2108 | { |
| 2109 | struct quic_rx_strm_frm *frm; |
| 2110 | |
| 2111 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 2112 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2113 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2114 | frm->len = stream_frm->len; |
| 2115 | frm->data = stream_frm->data; |
| 2116 | frm->pkt = pkt; |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2117 | frm->fin = stream_frm->fin; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | return frm; |
| 2121 | } |
| 2122 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2123 | /* 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] | 2124 | * 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] | 2125 | */ |
| 2126 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 2127 | { |
| 2128 | size_t ret; |
| 2129 | |
Amaury Denoyelle | 2d2d030 | 2022-02-28 10:00:54 +0100 | [diff] [blame] | 2130 | ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len); |
| 2131 | strm_frm->len -= ret; |
| 2132 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2133 | |
| 2134 | return ret; |
| 2135 | } |
| 2136 | |
| 2137 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2138 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2139 | * If not, the STREAM frame is stored to be treated again later. |
| 2140 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2141 | * Return 1 if succeeded, 0 if not. |
| 2142 | */ |
| 2143 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2144 | struct quic_stream *strm_frm, |
| 2145 | struct quic_conn *qc) |
| 2146 | { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2147 | struct quic_rx_strm_frm *frm; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2148 | struct eb64_node *frm_node; |
| 2149 | struct qcs *qcs = NULL; |
| 2150 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2151 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2152 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2153 | strm_frm->offset.key, strm_frm->fin, |
| 2154 | (char *)strm_frm->data, &qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2155 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2156 | /* invalid or already received frame */ |
| 2157 | if (ret == 1) |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2158 | return 1; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2159 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2160 | if (ret == 2) { |
| 2161 | /* frame cannot be parsed at the moment and should be |
| 2162 | * buffered. |
| 2163 | */ |
| 2164 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2165 | if (!frm) { |
| 2166 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2167 | QUIC_EV_CONN_PSTRM, qc); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2168 | return 0; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2171 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
| 2172 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2173 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2174 | return 1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2177 | /* Frame correctly received by the mux. |
| 2178 | * If there is buffered frame for next offset, it may be possible to |
| 2179 | * receive them now. |
| 2180 | */ |
| 2181 | frm_node = eb64_first(&qcs->rx.frms); |
| 2182 | while (frm_node) { |
| 2183 | frm = eb64_entry(&frm_node->node, |
| 2184 | struct quic_rx_strm_frm, offset_node); |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2185 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 2186 | ret = qcc_recv(qc->qcc, qcs->id, frm->len, |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2187 | frm->offset_node.key, frm->fin, |
| 2188 | (char *)frm->data, &qcs); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2189 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2190 | /* interrupt the parsing if the frame cannot be handled for the |
| 2191 | * moment only by the MUX. |
| 2192 | */ |
| 2193 | if (ret == 2) |
| 2194 | break; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2195 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2196 | /* Remove a newly received frame or an invalid one. */ |
| 2197 | frm_node = eb64_next(frm_node); |
| 2198 | eb64_delete(&frm->offset_node); |
| 2199 | quic_rx_packet_refdec(frm->pkt); |
| 2200 | pool_free(pool_head_quic_rx_strm_frm, frm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2203 | /* Decode the received data. */ |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2204 | qcc_decode_qcs(qc->qcc, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2205 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2210 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2211 | * If not, the STREAM frame is stored to be treated again later. |
| 2212 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2213 | * Return 1 if succeeded, 0 if not. |
| 2214 | */ |
| 2215 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2216 | struct quic_stream *strm_frm, |
| 2217 | struct quic_conn *qc) |
| 2218 | { |
| 2219 | struct qcs *strm; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2220 | struct quic_rx_strm_frm *frm; |
| 2221 | size_t strm_frm_len; |
| 2222 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2223 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2224 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2225 | 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] | 2226 | return 0; |
| 2227 | } |
| 2228 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2229 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2230 | size_t diff; |
| 2231 | |
| 2232 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2233 | TRACE_PROTO("Already received STREAM data", |
| 2234 | QUIC_EV_CONN_PSTRM, qc); |
| 2235 | goto out; |
| 2236 | } |
| 2237 | |
| 2238 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2239 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2240 | strm_frm->offset.key = strm->rx.offset; |
| 2241 | strm_frm->len -= diff; |
| 2242 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2246 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2247 | int ret; |
| 2248 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2249 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2250 | goto store_frm; |
| 2251 | |
| 2252 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2253 | * of bytes copied. |
| 2254 | */ |
| 2255 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2256 | /* Inform the application of the arrival of this new stream */ |
| 2257 | 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] | 2258 | 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] | 2259 | return 0; |
| 2260 | } |
| 2261 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2262 | if (ret) |
| 2263 | qcs_notify_recv(strm); |
| 2264 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2265 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2266 | } |
| 2267 | /* Take this frame into an account for the stream flow control */ |
| 2268 | strm->rx.offset += strm_frm_len; |
| 2269 | /* 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] | 2270 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2271 | */ |
| 2272 | if (!strm_frm->len) |
| 2273 | goto out; |
| 2274 | |
| 2275 | store_frm: |
| 2276 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2277 | if (!frm) { |
| 2278 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2279 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2283 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2284 | quic_rx_packet_refinc(pkt); |
| 2285 | |
| 2286 | out: |
| 2287 | return 1; |
| 2288 | } |
| 2289 | |
| 2290 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2291 | struct quic_stream *strm_frm, |
| 2292 | struct quic_conn *qc) |
| 2293 | { |
| 2294 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2295 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2296 | else |
| 2297 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2298 | } |
| 2299 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2300 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2301 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2302 | struct quic_conn *qc) |
| 2303 | { |
| 2304 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2305 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2306 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2307 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2308 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2309 | pkts = &qel->pktns->tx.pkts; |
| 2310 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2311 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2312 | while (node) { |
| 2313 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2314 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2315 | break; |
| 2316 | node = eb64_next(node); |
| 2317 | } |
| 2318 | |
| 2319 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2320 | return; |
| 2321 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2322 | /* When building a packet from another one, the field which may increase the |
| 2323 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2324 | */ |
| 2325 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2326 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2327 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2328 | return; |
| 2329 | } |
| 2330 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2331 | 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] | 2332 | } |
| 2333 | |
| 2334 | /* Prepare a fast retransmission during handshake after a client |
| 2335 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2336 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2337 | * and resend them coalescing with others (Handshake here). |
| 2338 | * (Listener only). |
| 2339 | */ |
| 2340 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2341 | { |
| 2342 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2343 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2344 | struct quic_frame *frm, *frmbak; |
| 2345 | |
| 2346 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2347 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2348 | struct quic_enc_level *qel = iqel; |
| 2349 | struct eb_root *pkts; |
| 2350 | struct eb64_node *node; |
| 2351 | struct quic_tx_packet *pkt; |
| 2352 | struct list *tmp = &itmp; |
| 2353 | |
Frédéric Lécaille | 5cfb4ed | 2022-03-30 14:44:49 +0200 | [diff] [blame] | 2354 | /* Do not probe from a packet number space if some probing |
| 2355 | * was already asked. |
| 2356 | */ |
| 2357 | if (qel->pktns->tx.pto_probe) { |
| 2358 | qel = hqel; |
| 2359 | if (qel->pktns->tx.pto_probe) |
| 2360 | return; |
| 2361 | } |
| 2362 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2363 | start: |
| 2364 | pkt = NULL; |
| 2365 | pkts = &qel->pktns->tx.pkts; |
| 2366 | node = eb64_first(pkts); |
| 2367 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2368 | while (node) { |
| 2369 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2370 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2371 | break; |
| 2372 | node = eb64_next(node); |
| 2373 | } |
| 2374 | |
| 2375 | if (!pkt) |
| 2376 | goto end; |
| 2377 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2378 | /* When building a packet from another one, the field which may increase the |
| 2379 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2380 | */ |
| 2381 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2382 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2383 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2384 | goto end; |
| 2385 | } |
| 2386 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2387 | qel->pktns->tx.pto_probe += 1; |
| 2388 | requeue: |
| 2389 | 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] | 2390 | struct quic_frame *dup_frm; |
| 2391 | |
| 2392 | |
| 2393 | dup_frm = pool_alloc(pool_head_quic_frame); |
| 2394 | if (!dup_frm) { |
| 2395 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2396 | break; |
| 2397 | } |
| 2398 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2399 | 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] | 2400 | *dup_frm = *frm; |
| 2401 | LIST_APPEND(tmp, &dup_frm->list); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | if (qel == iqel) { |
| 2405 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2406 | pkt = pkt->next; |
| 2407 | tmp = &htmp; |
| 2408 | hqel->pktns->tx.pto_probe += 1; |
| 2409 | goto requeue; |
| 2410 | } |
| 2411 | |
| 2412 | qel = hqel; |
| 2413 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2414 | goto start; |
| 2415 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2416 | |
| 2417 | end: |
| 2418 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2419 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2422 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2423 | * as I/O handler context and <qel> as encryption level. |
| 2424 | * Returns 1 if succeeded, 0 if failed. |
| 2425 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2426 | 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] | 2427 | struct quic_enc_level *qel) |
| 2428 | { |
| 2429 | struct quic_frame frm; |
| 2430 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2431 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2432 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2433 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2434 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2435 | /* Skip the AAD */ |
| 2436 | pos = pkt->data + pkt->aad_len; |
| 2437 | end = pkt->data + pkt->len; |
| 2438 | |
| 2439 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2440 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2441 | goto err; |
| 2442 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2443 | 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] | 2444 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2445 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2446 | break; |
| 2447 | case QUIC_FT_PING: |
| 2448 | break; |
| 2449 | case QUIC_FT_ACK: |
| 2450 | { |
| 2451 | unsigned int rtt_sample; |
| 2452 | |
| 2453 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2454 | 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] | 2455 | goto err; |
| 2456 | |
| 2457 | if (rtt_sample) { |
| 2458 | unsigned int ack_delay; |
| 2459 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2460 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2461 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2462 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2463 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2464 | 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] | 2465 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2468 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2469 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2470 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2471 | { |
| 2472 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2473 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2474 | 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] | 2475 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2476 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2477 | |
| 2478 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2479 | cfdebug.len = frm.crypto.len; |
| 2480 | TRACE_PROTO("CRYPTO data discarded", |
| 2481 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2482 | break; |
| 2483 | } |
| 2484 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2485 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2486 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2487 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2488 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2489 | |
| 2490 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2491 | cfdebug.len = frm.crypto.len; |
| 2492 | /* Nothing to do */ |
| 2493 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2494 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2495 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2496 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2497 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2498 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2499 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2500 | else { |
| 2501 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2502 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2503 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2504 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2505 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2506 | cfdebug.len = frm.crypto.len; |
| 2507 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2508 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2509 | frm.crypto.len -= diff; |
| 2510 | frm.crypto.data += diff; |
| 2511 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2512 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2513 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2514 | |
| 2515 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2516 | /* 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] | 2517 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2518 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2519 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2520 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2521 | if (!qc_provide_cdata(qel, ctx, |
| 2522 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2523 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2524 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2525 | |
| 2526 | break; |
| 2527 | } |
| 2528 | |
| 2529 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2530 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2531 | if (!cf) { |
| 2532 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2533 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2534 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2535 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2536 | |
| 2537 | cf->offset_node.key = frm.crypto.offset; |
| 2538 | cf->len = frm.crypto.len; |
| 2539 | cf->data = frm.crypto.data; |
| 2540 | cf->pkt = pkt; |
| 2541 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2542 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2543 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2544 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2545 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2546 | { |
| 2547 | struct quic_stream *stream = &frm.stream; |
| 2548 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2549 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2550 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2551 | goto err; |
| 2552 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2553 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2554 | |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2555 | /* At the application layer the connection may have already been closed. */ |
| 2556 | if (qc->mux_state != QC_MUX_READY) |
| 2557 | break; |
| 2558 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2559 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2560 | goto err; |
| 2561 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2562 | break; |
| 2563 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2564 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2565 | if (qc->mux_state == QC_MUX_READY) { |
| 2566 | struct quic_max_data *data = &frm.max_data; |
| 2567 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2568 | } |
| 2569 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2570 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2571 | if (qc->mux_state == QC_MUX_READY) { |
| 2572 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2573 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2574 | data->max_stream_data); |
| 2575 | } |
| 2576 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2577 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2578 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2579 | case QUIC_FT_DATA_BLOCKED: |
| 2580 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2581 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2582 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2583 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2584 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2585 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2586 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2587 | break; |
| 2588 | case QUIC_FT_CONNECTION_CLOSE: |
| 2589 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2590 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2591 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2592 | /* RFC 9000 10.2. Immediate Close: |
| 2593 | * The closing and draining connection states exist to ensure |
| 2594 | * that connections close cleanly and that delayed or reordered |
| 2595 | * packets are properly discarded. These states SHOULD persist |
| 2596 | * for at least three times the current PTO interval... |
| 2597 | * |
| 2598 | * Rearm the idle timeout only one time when entering draining |
| 2599 | * state. |
| 2600 | */ |
| 2601 | qc_idle_timer_do_rearm(qc); |
| 2602 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2603 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2604 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2605 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2606 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2607 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2608 | goto err; |
| 2609 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2610 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2611 | break; |
| 2612 | default: |
| 2613 | goto err; |
| 2614 | } |
| 2615 | } |
| 2616 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2617 | /* 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] | 2618 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2619 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2620 | if (fast_retrans) |
| 2621 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2622 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2623 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2624 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2625 | * be discarded. |
| 2626 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2627 | 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] | 2628 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2629 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2630 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2631 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2632 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2633 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2634 | qc_set_timer(ctx->qc); |
| 2635 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2636 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2637 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2638 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2639 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2640 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2641 | } |
| 2642 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2643 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2644 | return 1; |
| 2645 | |
| 2646 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2647 | 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] | 2648 | return 0; |
| 2649 | } |
| 2650 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2651 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2652 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2653 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2654 | * index after having marked the end of written data. This the responsability |
| 2655 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2656 | * data made of a uint16_t null field. |
| 2657 | * |
| 2658 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2659 | * ^ ^ |
| 2660 | * r w |
| 2661 | * |
| 2662 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2663 | * ^ ^ |
| 2664 | * r w |
| 2665 | |
| 2666 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2667 | * ^ |
| 2668 | * (r = w) |
| 2669 | * |
| 2670 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2671 | * ^ ^ |
| 2672 | * w r |
| 2673 | */ |
| 2674 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2675 | { |
| 2676 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2677 | |
| 2678 | /* We can reset the writer index only if in front of the reader index and |
| 2679 | * if the reader index is not null. Resetting the writer when the reader |
| 2680 | * index is null would empty the buffer. |
| 2681 | * XXX Note than the writer index cannot reach the reader index. |
| 2682 | * Only the reader index can reach the writer index. |
| 2683 | */ |
| 2684 | if (rd && rd <= cbuf->wr) { |
| 2685 | /* Mark the end of contiguous data for the reader */ |
| 2686 | write_u16(cb_wr(cbuf), 0); |
| 2687 | cb_add(cbuf, sizeof(uint16_t)); |
| 2688 | cb_wr_reset(cbuf); |
| 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | return 0; |
| 2693 | } |
| 2694 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2695 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2696 | * 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] | 2697 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2698 | * This function must be called only after having built a correct datagram. |
| 2699 | * Always succeeds. |
| 2700 | */ |
| 2701 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2702 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2703 | { |
| 2704 | write_u16(cb_wr(cbuf), dglen); |
| 2705 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2706 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2707 | } |
| 2708 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2709 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2710 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2711 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2712 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2713 | */ |
| 2714 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2715 | struct quic_enc_level *qel, int cc, int probe) |
| 2716 | { |
| 2717 | unsigned int must_ack = |
| 2718 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2719 | |
| 2720 | /* Do not build any more packet if the TX secrets are not available or |
| 2721 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2722 | * and if there is no more packets to send upon PTO expiration |
| 2723 | * and if there is no more ack-eliciting frames to send or in flight |
| 2724 | * congestion control limit is reached for prepared data |
| 2725 | */ |
| 2726 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2727 | (!cc && !probe && !must_ack && |
| 2728 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2729 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | return 1; |
| 2734 | } |
| 2735 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2736 | /* 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] | 2737 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2738 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2739 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2740 | * 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] | 2741 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2742 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2743 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2744 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2745 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2746 | { |
| 2747 | struct quic_enc_level *qel; |
| 2748 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2749 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2750 | struct quic_tx_packet *pkt; |
| 2751 | size_t total; |
| 2752 | size_t dg_headlen; |
| 2753 | |
| 2754 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2755 | /* Each datagram is prepended with its length followed by the |
| 2756 | * address of the first packet in the datagram. |
| 2757 | */ |
| 2758 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2759 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2760 | total = 0; |
| 2761 | start: |
| 2762 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2763 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2764 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2765 | * to ensure there is enough room to mark the end of prepared |
| 2766 | * contiguous data with a zero length. |
| 2767 | */ |
| 2768 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2769 | 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] | 2770 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2771 | |
| 2772 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2773 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2774 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2775 | /* We do not probe if an immediate close was asked */ |
| 2776 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2777 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2778 | |
| 2779 | 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] | 2780 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* Leave room for the datagram header */ |
| 2783 | pos += dg_headlen; |
| 2784 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2785 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2786 | } |
| 2787 | else { |
| 2788 | end = pos + qc->path->mtu; |
| 2789 | } |
| 2790 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2791 | 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] | 2792 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2793 | switch (err) { |
| 2794 | case -2: |
| 2795 | goto err; |
| 2796 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2797 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2798 | * MTU, we are here because of the congestion control window. There is |
| 2799 | * no need to try to reuse this buffer. |
| 2800 | */ |
| 2801 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2802 | default: |
| 2803 | break; |
| 2804 | } |
| 2805 | |
| 2806 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2807 | if (!pkt) |
| 2808 | goto err; |
| 2809 | |
| 2810 | total += pkt->len; |
| 2811 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2812 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2813 | } |
| 2814 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2815 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2816 | 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] | 2817 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2818 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2819 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | out: |
| 2823 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2824 | return total; |
| 2825 | |
| 2826 | err: |
| 2827 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2831 | /* 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] | 2832 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2833 | * several packets in the same datagram. A header made of two fields is added |
| 2834 | * to each datagram: the datagram length followed by the address of the first |
| 2835 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2836 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2837 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2838 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2839 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2840 | enum quic_tls_enc_level tel, |
| 2841 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2842 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2843 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2844 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2845 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2846 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2847 | /* length of datagrams */ |
| 2848 | uint16_t dglen; |
| 2849 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2850 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2851 | /* Each datagram is prepended with its length followed by the |
| 2852 | * address of the first packet in the datagram. |
| 2853 | */ |
| 2854 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2855 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2856 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2857 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2858 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2859 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2860 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2861 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2862 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2863 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2864 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2865 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2866 | * to ensure there is enough room to mark the end of prepared |
| 2867 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2868 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2869 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2870 | first_pkt = prv_pkt = NULL; |
| 2871 | 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] | 2872 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2873 | enum quic_pkt_type pkt_type; |
| 2874 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2875 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2876 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2877 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2878 | /* We do not probe if an immediate close was asked */ |
| 2879 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2880 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2881 | |
| 2882 | 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] | 2883 | if (prv_pkt) |
| 2884 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2885 | /* Let's select the next encryption level */ |
| 2886 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2887 | tel = next_tel; |
| 2888 | qel = &qc->els[tel]; |
| 2889 | /* Build a new datagram */ |
| 2890 | prv_pkt = NULL; |
| 2891 | continue; |
| 2892 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2893 | break; |
| 2894 | } |
| 2895 | |
| 2896 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2897 | if (!prv_pkt) { |
| 2898 | /* Leave room for the datagram header */ |
| 2899 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2900 | 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] | 2901 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2902 | } |
| 2903 | else { |
| 2904 | end = pos + qc->path->mtu; |
| 2905 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2906 | } |
| 2907 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2908 | 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] | 2909 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2910 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2911 | case -2: |
| 2912 | goto err; |
| 2913 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2914 | /* If there was already a correct packet present, set the |
| 2915 | * current datagram as prepared into <cbuf>. |
| 2916 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2917 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2918 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2919 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2920 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2921 | break; |
| 2922 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2923 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2924 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2925 | if (!cur_pkt) |
| 2926 | goto err; |
| 2927 | |
| 2928 | total += cur_pkt->len; |
| 2929 | /* keep trace of the first packet in the datagram */ |
| 2930 | if (!first_pkt) |
| 2931 | first_pkt = cur_pkt; |
| 2932 | /* Attach the current one to the previous one */ |
| 2933 | if (prv_pkt) |
| 2934 | prv_pkt->next = cur_pkt; |
| 2935 | /* Let's say we have to build a new dgram */ |
| 2936 | prv_pkt = NULL; |
| 2937 | dglen += cur_pkt->len; |
| 2938 | /* Client: discard the Initial encryption keys as soon as |
| 2939 | * a handshake packet could be built. |
| 2940 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2941 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2942 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2943 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2944 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2945 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2946 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2947 | 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] | 2948 | 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] | 2949 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2950 | } |
| 2951 | /* If the data for the current encryption level have all been sent, |
| 2952 | * select the next level. |
| 2953 | */ |
| 2954 | 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] | 2955 | (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] | 2956 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2957 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2958 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2959 | tel = next_tel; |
| 2960 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2961 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2962 | /* If there is data for the next level, do not |
| 2963 | * consume a datagram. |
| 2964 | */ |
| 2965 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2966 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2967 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2968 | /* If we have to build a new datagram, set the current datagram as |
| 2969 | * prepared into <cbuf>. |
| 2970 | */ |
| 2971 | if (!prv_pkt) { |
| 2972 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2973 | first_pkt = NULL; |
| 2974 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2975 | padding = 0; |
| 2976 | } |
| 2977 | 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] | 2978 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2979 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2980 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | stop_build: |
| 2985 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2986 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2987 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2988 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2989 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2993 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2994 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2995 | |
| 2996 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2997 | 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] | 2998 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | /* 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] | 3002 | * 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] | 3003 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3004 | 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] | 3005 | { |
| 3006 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3007 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3008 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3009 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3010 | cbuf = qr->cbuf; |
| 3011 | while (cb_contig_data(cbuf)) { |
| 3012 | unsigned char *pos; |
| 3013 | struct buffer tmpbuf = { }; |
| 3014 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3015 | uint16_t dglen; |
| 3016 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3017 | unsigned int time_sent; |
| 3018 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3019 | pos = cb_rd(cbuf); |
| 3020 | dglen = read_u16(pos); |
| 3021 | /* End of prepared datagrams. |
| 3022 | * Reset the reader index only if in front of the writer index. |
| 3023 | */ |
| 3024 | if (!dglen) { |
| 3025 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3026 | |
| 3027 | if (wr && wr < cbuf->rd) { |
| 3028 | cb_rd_reset(cbuf); |
| 3029 | continue; |
| 3030 | } |
| 3031 | break; |
| 3032 | } |
| 3033 | |
| 3034 | pos += sizeof dglen; |
| 3035 | first_pkt = read_ptr(pos); |
| 3036 | pos += sizeof first_pkt; |
| 3037 | tmpbuf.area = (char *)pos; |
| 3038 | tmpbuf.size = tmpbuf.data = dglen; |
| 3039 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3040 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3041 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3042 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3044 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3045 | qc->tx.bytes += tmpbuf.data; |
| 3046 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3047 | |
| 3048 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3049 | pkt->time_sent = time_sent; |
| 3050 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3051 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3052 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3053 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3054 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3055 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3056 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3057 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3058 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3059 | qc_notify_close(qc); |
| 3060 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3061 | /* RFC 9000 10.2. Immediate Close: |
| 3062 | * The closing and draining connection states exist to ensure |
| 3063 | * that connections close cleanly and that delayed or reordered |
| 3064 | * packets are properly discarded. These states SHOULD persist |
| 3065 | * for at least three times the current PTO interval... |
| 3066 | * |
| 3067 | * Rearm the idle timeout only one time when entering closing |
| 3068 | * state. |
| 3069 | */ |
| 3070 | qc_idle_timer_do_rearm(qc); |
| 3071 | if (qc->timer_task) { |
| 3072 | task_destroy(qc->timer_task); |
| 3073 | qc->timer_task = NULL; |
| 3074 | } |
| 3075 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3076 | qc->path->in_flight += pkt->in_flight_len; |
| 3077 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3078 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3079 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3080 | 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] | 3081 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3082 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3083 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3084 | } |
| 3085 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3086 | |
| 3087 | return 1; |
| 3088 | } |
| 3089 | |
| 3090 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3091 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3092 | * a HANDSHAKE_DONE frame. |
| 3093 | * Return 1 if succeeded, 0 if not. |
| 3094 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3095 | 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] | 3096 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3097 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3098 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3099 | struct quic_frame *frm, *frmbak; |
| 3100 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3101 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3102 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3103 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3104 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3105 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3106 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3107 | if (!frm) |
| 3108 | return 0; |
| 3109 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3110 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3111 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3112 | } |
| 3113 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3114 | first = 1; |
| 3115 | max = qc->tx.params.active_connection_id_limit; |
| 3116 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3117 | struct quic_connection_id *cid; |
| 3118 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3119 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3120 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3121 | goto err; |
| 3122 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3123 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3124 | if (!cid) |
| 3125 | goto err; |
| 3126 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3127 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3128 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3129 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3130 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3131 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3132 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3133 | |
| 3134 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3135 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3136 | |
| 3137 | return 1; |
| 3138 | |
| 3139 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3140 | /* free the frames */ |
| 3141 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3142 | pool_free(pool_head_quic_frame, frm); |
| 3143 | |
| 3144 | node = eb64_first(&qc->cids); |
| 3145 | while (node) { |
| 3146 | struct quic_connection_id *cid; |
| 3147 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3148 | |
| 3149 | 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] | 3150 | if (cid->seq_num.key >= max) |
| 3151 | break; |
| 3152 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3153 | if (cid->seq_num.key < first) |
| 3154 | continue; |
| 3155 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3156 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3157 | ebmb_delete(&cid->node); |
| 3158 | eb64_delete(&cid->seq_num); |
| 3159 | pool_free(pool_head_quic_connection_id, cid); |
| 3160 | } |
| 3161 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
| 3164 | |
| 3165 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3166 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3167 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3168 | struct eb64_node *n; |
| 3169 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3170 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3171 | n = eb64_first(&arngs->root); |
| 3172 | while (n) { |
| 3173 | struct eb64_node *next; |
| 3174 | |
| 3175 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 3176 | next = eb64_next(n); |
| 3177 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3178 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3179 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3183 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3184 | * descending order. |
| 3185 | */ |
| 3186 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3187 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3188 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3189 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | |
| 3193 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3194 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3195 | * 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] | 3196 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3197 | 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] | 3198 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3199 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3200 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3201 | last = eb64_last(&arngs->root); |
| 3202 | while (last && arngs->enc_sz > limit) { |
| 3203 | struct quic_arng_node *last_node, *prev_node; |
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 | prev = eb64_prev(last); |
| 3206 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3207 | return 0; |
| 3208 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3209 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 3210 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 3211 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3212 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3213 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3214 | --arngs->sz; |
| 3215 | eb64_delete(last); |
| 3216 | pool_free(pool_head_quic_arng, last); |
| 3217 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3223 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3224 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3225 | { |
| 3226 | struct eb64_node *node, *next; |
| 3227 | struct quic_arng_node *ar, *ar_next; |
| 3228 | |
| 3229 | node = eb64_last(&arngs->root); |
| 3230 | if (!node) |
| 3231 | return; |
| 3232 | |
| 3233 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3234 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3235 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3236 | |
| 3237 | while ((next = eb64_prev(node))) { |
| 3238 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 3239 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3240 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3241 | node = next; |
| 3242 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3243 | } |
| 3244 | } |
| 3245 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3246 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3247 | * 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] | 3248 | */ |
| 3249 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3250 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3251 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3252 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3253 | struct quic_arng_node *new_ar; |
| 3254 | |
| 3255 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3256 | if (new_ar) { |
| 3257 | new_ar->first.key = ar->first; |
| 3258 | new_ar->last = ar->last; |
| 3259 | eb64_insert(&arngs->root, &new_ar->first); |
| 3260 | arngs->sz++; |
| 3261 | } |
| 3262 | |
| 3263 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /* 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] | 3267 | * 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] | 3268 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3269 | * |
| 3270 | * Descending order |
| 3271 | * -------------> |
| 3272 | * range1 range2 |
| 3273 | * ..........|--------|..............|--------| |
| 3274 | * ^ ^ ^ ^ |
| 3275 | * | | | | |
| 3276 | * last1 first1 last2 first2 |
| 3277 | * ..........+--------+--------------+--------+...... |
| 3278 | * diff1 gap12 diff2 |
| 3279 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3280 | * To encode the previous list of ranges we must encode integers as follows in |
| 3281 | * descending order: |
| 3282 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3283 | * with diff1 = last1 - first1 |
| 3284 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3285 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3286 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3287 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3288 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3289 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3290 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3291 | struct eb64_node *le; |
| 3292 | struct quic_arng_node *new_node; |
| 3293 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3294 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3295 | new = NULL; |
| 3296 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3297 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3298 | if (!new_node) |
| 3299 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3300 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3301 | goto out; |
| 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 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3305 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3306 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3307 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3308 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3309 | |
| 3310 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3311 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3312 | else { |
| 3313 | struct quic_arng_node *le_ar = |
| 3314 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3315 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3316 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3317 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3318 | return 1; |
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 | if (le_ar->last + 1 >= ar->first) { |
| 3321 | le_ar->last = ar->last; |
| 3322 | new = le; |
| 3323 | new_node = le_ar; |
| 3324 | } |
| 3325 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3326 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3327 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3328 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3329 | |
| 3330 | new = &new_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 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3333 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3334 | /* Verify that the new inserted node does not overlap the nodes |
| 3335 | * which follow it. |
| 3336 | */ |
| 3337 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3338 | struct eb64_node *next; |
| 3339 | struct quic_arng_node *next_node; |
| 3340 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3341 | while ((next = eb64_next(new))) { |
| 3342 | next_node = |
| 3343 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3344 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3345 | break; |
| 3346 | |
| 3347 | if (next_node->last > new_node->last) |
| 3348 | new_node->last = next_node->last; |
| 3349 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3350 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3351 | /* Decrement the size of these ranges. */ |
| 3352 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3353 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3356 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3357 | quic_arngs_set_enc_sz(arngs); |
| 3358 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3359 | return 1; |
| 3360 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3361 | /* Remove the header protection of packets at <el> encryption level. |
| 3362 | * Always succeeds. |
| 3363 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3364 | 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] | 3365 | { |
| 3366 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3367 | struct quic_rx_packet *pqpkt; |
| 3368 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3369 | struct quic_enc_level *app_qel; |
| 3370 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3371 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3372 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3373 | /* 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] | 3374 | 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] | 3375 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3376 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3377 | goto out; |
| 3378 | } |
| 3379 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3380 | 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] | 3381 | 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] | 3382 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3383 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3384 | 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] | 3385 | /* XXX TO DO XXX */ |
| 3386 | } |
| 3387 | else { |
| 3388 | /* The AAD includes the packet number field */ |
| 3389 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3390 | /* Store the packet into the tree of packets to decrypt. */ |
| 3391 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3392 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3393 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3394 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3395 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3396 | 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] | 3397 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3398 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3399 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3403 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3407 | * Return 1 if succeeded, 0 if not. |
| 3408 | */ |
| 3409 | 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] | 3410 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3411 | { |
| 3412 | struct eb64_node *node; |
| 3413 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3414 | node = eb64_first(&el->rx.crypto.frms); |
| 3415 | while (node) { |
| 3416 | struct quic_rx_crypto_frm *cf; |
| 3417 | |
| 3418 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3419 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3420 | break; |
| 3421 | |
| 3422 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3423 | goto err; |
| 3424 | |
| 3425 | node = eb64_next(node); |
| 3426 | quic_rx_packet_refdec(cf->pkt); |
| 3427 | eb64_delete(&cf->offset_node); |
| 3428 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3429 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | return 1; |
| 3431 | |
| 3432 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3433 | 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] | 3434 | return 0; |
| 3435 | } |
| 3436 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3437 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3438 | * 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] | 3439 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3440 | * Return 1 if succeeded, 0 if not. |
| 3441 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3442 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3443 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3444 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3445 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3446 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3447 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3448 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3449 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3450 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3451 | qel = cur_el; |
| 3452 | next_tel: |
| 3453 | if (!qel) |
| 3454 | goto out; |
| 3455 | |
| 3456 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3457 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3458 | while (node) { |
| 3459 | struct quic_rx_packet *pkt; |
| 3460 | |
| 3461 | 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] | 3462 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3463 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3464 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3465 | /* Drop the packet */ |
| 3466 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3467 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3468 | } |
| 3469 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3470 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3471 | /* Drop the packet */ |
| 3472 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3473 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3474 | } |
| 3475 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3476 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3477 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3478 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3479 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3480 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3481 | qc_idle_timer_rearm(qc, 1); |
| 3482 | } |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3483 | if (pkt->pn > largest_pn) |
| 3484 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3485 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3486 | 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] | 3487 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3488 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3489 | } |
| 3490 | } |
| 3491 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3492 | eb64_delete(&pkt->pn_node); |
| 3493 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3494 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3495 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3496 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3497 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3498 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) |
| 3499 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3500 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3501 | goto err; |
| 3502 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3503 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3504 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3505 | qel = next_el; |
| 3506 | goto next_tel; |
| 3507 | } |
| 3508 | |
| 3509 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3510 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3511 | return 1; |
| 3512 | |
| 3513 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3514 | 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] | 3515 | return 0; |
| 3516 | } |
| 3517 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3518 | /* Check if it's possible to remove header protection for packets related to |
| 3519 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3520 | * |
| 3521 | * Return true if the operation is possible else false. |
| 3522 | */ |
| 3523 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3524 | { |
| 3525 | enum quic_tls_enc_level tel; |
| 3526 | |
| 3527 | if (!qel) |
| 3528 | return 0; |
| 3529 | |
| 3530 | tel = ssl_to_quic_enc_level(qel->level); |
| 3531 | |
| 3532 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3533 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3534 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3535 | return 0; |
| 3536 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3537 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3538 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3539 | return 0; |
| 3540 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3541 | /* 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] | 3542 | 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] | 3543 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3544 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3545 | |
| 3546 | return 1; |
| 3547 | } |
| 3548 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3549 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | c2f561c | 2022-02-25 17:46:07 +0100 | [diff] [blame] | 3550 | 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] | 3551 | { |
| 3552 | int ret; |
| 3553 | struct qring *qr; |
| 3554 | |
| 3555 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3556 | if (!qr) |
| 3557 | /* Never happens */ |
| 3558 | return 1; |
| 3559 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3560 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3561 | if (ret == -1) |
| 3562 | goto err; |
| 3563 | else if (ret == 0) |
| 3564 | goto out; |
| 3565 | |
| 3566 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3567 | goto err; |
| 3568 | |
| 3569 | out: |
| 3570 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3571 | return 1; |
| 3572 | |
| 3573 | err: |
| 3574 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3575 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | /* QUIC connection packet handler task (post handshake) */ |
| 3580 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3581 | { |
| 3582 | struct ssl_sock_ctx *ctx; |
| 3583 | struct quic_conn *qc; |
| 3584 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3585 | |
| 3586 | |
| 3587 | ctx = context; |
| 3588 | qc = ctx->qc; |
| 3589 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3590 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3591 | 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] | 3592 | |
| 3593 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3594 | qc_rm_hp_pkts(qc, qel); |
| 3595 | |
| 3596 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3597 | goto err; |
| 3598 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3599 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3600 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3601 | goto out; |
| 3602 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3603 | 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] | 3604 | goto err; |
| 3605 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3606 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3607 | return t; |
| 3608 | |
| 3609 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3610 | 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] | 3611 | return t; |
| 3612 | } |
| 3613 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3614 | /* QUIC connection packet handler task. */ |
| 3615 | 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] | 3616 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3617 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3618 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3619 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3620 | enum quic_tls_enc_level tel, next_tel; |
| 3621 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3622 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3623 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3624 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3625 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3626 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3627 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3628 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3629 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3630 | 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] | 3631 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3632 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3633 | /* The I/O handler has been woken up by the dgram listener |
| 3634 | * after the anti-amplification was reached. |
| 3635 | */ |
| 3636 | qc_set_timer(qc); |
| 3637 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3638 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3639 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3641 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3642 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3643 | 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] | 3644 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3645 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3646 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3647 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3648 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3649 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3650 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3651 | } |
| 3652 | 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] | 3653 | goto err; |
| 3654 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3655 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3656 | 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] | 3657 | |
| 3658 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3659 | /* Treat packets waiting for header packet protection decryption */ |
| 3660 | 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] | 3661 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3662 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3663 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3664 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3665 | 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] | 3666 | goto err; |
| 3667 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3668 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3669 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3670 | goto out; |
| 3671 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3672 | 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] | 3673 | (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] | 3674 | qel = next_qel; |
| 3675 | next_qel = NULL; |
| 3676 | goto next_level; |
| 3677 | } |
| 3678 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3679 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3680 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3681 | 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] | 3682 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3683 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3684 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3685 | 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] | 3686 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3687 | /* Discard the Handshake keys. */ |
| 3688 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3689 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3690 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3691 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3692 | 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] | 3693 | 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] | 3694 | } |
| 3695 | |
| 3696 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3697 | /* There may be remaining handshake to build (acks) */ |
| 3698 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3699 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3702 | if (!qr) |
| 3703 | 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] | 3704 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3705 | * be considered. |
| 3706 | */ |
| 3707 | 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] | 3708 | goto err; |
| 3709 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3710 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3711 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3712 | else if (ret == 0) |
| 3713 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3714 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3715 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3716 | goto err; |
| 3717 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3718 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3719 | /* Check if there is something to do for the next level. |
| 3720 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3721 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3722 | (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] | 3723 | (!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] | 3724 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3725 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3726 | goto next_level; |
| 3727 | } |
| 3728 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3729 | out: |
| 3730 | if (qr) |
| 3731 | 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] | 3732 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3733 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3734 | |
| 3735 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3736 | if (qr) |
| 3737 | 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] | 3738 | 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] | 3739 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3740 | } |
| 3741 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3742 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3743 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3744 | { |
| 3745 | int i; |
| 3746 | |
| 3747 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3748 | if (qel->tx.crypto.bufs[i]) { |
| 3749 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3750 | qel->tx.crypto.bufs[i] = NULL; |
| 3751 | } |
| 3752 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3753 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3757 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3758 | * Returns 1 if succeeded, 0 if not. |
| 3759 | */ |
| 3760 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3761 | enum quic_tls_enc_level level) |
| 3762 | { |
| 3763 | struct quic_enc_level *qel; |
| 3764 | |
| 3765 | qel = &qc->els[level]; |
| 3766 | qel->level = quic_to_ssl_enc_level(level); |
| 3767 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3768 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3769 | 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] | 3770 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3771 | |
| 3772 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3773 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3774 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3775 | qel->rx.crypto.offset = 0; |
| 3776 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3777 | |
| 3778 | /* Allocate only one buffer. */ |
| 3779 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3780 | if (!qel->tx.crypto.bufs) |
| 3781 | goto err; |
| 3782 | |
| 3783 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3784 | if (!qel->tx.crypto.bufs[0]) |
| 3785 | goto err; |
| 3786 | |
| 3787 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3788 | qel->tx.crypto.nb_buf = 1; |
| 3789 | |
| 3790 | qel->tx.crypto.sz = 0; |
| 3791 | qel->tx.crypto.offset = 0; |
| 3792 | |
| 3793 | return 1; |
| 3794 | |
| 3795 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3796 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3797 | return 0; |
| 3798 | } |
| 3799 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3800 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 3801 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3802 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3803 | * This function must only be called by the thread responsible of the quic_conn |
| 3804 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3805 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3806 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3807 | { |
| 3808 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3809 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3810 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3811 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3812 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 3813 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 3814 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 3815 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3816 | /* free remaining stream descriptors */ |
| 3817 | node = eb64_first(&qc->streams_by_id); |
| 3818 | while (node) { |
| 3819 | struct qc_stream_desc *stream; |
| 3820 | |
| 3821 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 3822 | node = eb64_next(node); |
| 3823 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 3824 | /* all streams attached to the quic-conn are released, so |
| 3825 | * qc_stream_desc_free will liberate the stream instance. |
| 3826 | */ |
| 3827 | BUG_ON(!stream->release); |
| 3828 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3829 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3830 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3831 | if (qc->idle_timer_task) { |
| 3832 | task_destroy(qc->idle_timer_task); |
| 3833 | qc->idle_timer_task = NULL; |
| 3834 | } |
| 3835 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3836 | if (qc->timer_task) { |
| 3837 | task_destroy(qc->timer_task); |
| 3838 | qc->timer_task = NULL; |
| 3839 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3840 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3841 | /* remove the connection from receiver cids trees */ |
| 3842 | ebmb_delete(&qc->odcid_node); |
| 3843 | ebmb_delete(&qc->scid_node); |
| 3844 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3845 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3846 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3847 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3848 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3849 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3850 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3851 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3852 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3853 | quic_tls_ku_free(qc); |
| 3854 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3855 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3856 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 3860 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 3861 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3862 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3863 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 3864 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3865 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3866 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3867 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3868 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3869 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3870 | 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] | 3871 | } |
| 3872 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 3873 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3874 | { |
| 3875 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3876 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3877 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3878 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3879 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3880 | /* Next application data can be dropped. */ |
| 3881 | qc->mux_state = QC_MUX_RELEASED; |
| 3882 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 3883 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 3884 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 3885 | quic_conn_release(qc); |
| 3886 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 3887 | return; |
| 3888 | } |
| 3889 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3890 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3891 | } |
| 3892 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3893 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3894 | 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] | 3895 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3896 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3897 | struct quic_conn *qc; |
| 3898 | struct quic_pktns *pktns; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3899 | int i; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3900 | |
| 3901 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3902 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3903 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3904 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3905 | task->expire = TICK_ETERNITY; |
| 3906 | pktns = quic_loss_pktns(qc); |
| 3907 | if (tick_isset(pktns->tx.loss_time)) { |
| 3908 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3909 | |
| 3910 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3911 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3912 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3913 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3914 | goto out; |
| 3915 | } |
| 3916 | |
| 3917 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3918 | 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] | 3919 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3920 | pktns->tx.pto_probe = 1; |
| 3921 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3922 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3923 | } |
| 3924 | else { |
| 3925 | pktns->tx.pto_probe = 2; |
| 3926 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3927 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3928 | 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] | 3929 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3930 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3931 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3932 | 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] | 3933 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3934 | 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] | 3935 | iel->pktns->tx.pto_probe = 1; |
| 3936 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3937 | |
| 3938 | 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] | 3939 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3940 | continue; |
| 3941 | |
Frédéric Lécaille | 53c7d8d | 2022-02-15 12:00:55 +0100 | [diff] [blame] | 3942 | qc_prep_fast_retrans(&qc->els[i], qc); |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3945 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3946 | qc->path->loss.pto_count++; |
| 3947 | |
| 3948 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3949 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3950 | |
| 3951 | return task; |
| 3952 | } |
| 3953 | |
| 3954 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3955 | * connections used to identify the first Initial packets of client connecting |
| 3956 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3957 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3958 | * <scid> is the source connection ID with <scid_len> as length. |
| 3959 | * Returns 1 if succeeded, 0 if not. |
| 3960 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3961 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3962 | 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] | 3963 | 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] | 3964 | { |
| 3965 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3966 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3967 | /* Initial CID. */ |
| 3968 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3969 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3970 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3971 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3972 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3973 | qc = pool_zalloc(pool_head_quic_conn); |
| 3974 | if (!qc) { |
| 3975 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3976 | goto err; |
| 3977 | } |
| 3978 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3979 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3980 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3981 | 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] | 3982 | goto err; |
| 3983 | } |
| 3984 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3985 | qc->cids = EB_ROOT; |
| 3986 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3987 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3988 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3989 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3990 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3991 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3992 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3993 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3994 | qc->odcid.addrlen = dcid_addr_len; |
| 3995 | 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] | 3996 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3997 | /* 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] | 3998 | if (scid_len) |
| 3999 | memcpy(qc->dcid.data, scid, scid_len); |
| 4000 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4001 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4002 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4003 | } |
| 4004 | /* QUIC Client (outgoing connection to servers) */ |
| 4005 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4006 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4007 | if (dcid_len) |
| 4008 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 4009 | qc->dcid.len = dcid_len; |
| 4010 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4011 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4012 | |
| 4013 | /* Initialize the output buffer */ |
| 4014 | qc->obuf.pos = qc->obuf.data; |
| 4015 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4016 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4017 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4018 | 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] | 4019 | goto err; |
| 4020 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4021 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4022 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4023 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4024 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4025 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4026 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4027 | qc->scid = icid->cid; |
| 4028 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4029 | /* Packet number spaces initialization. */ |
| 4030 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4031 | quic_pktns_init(&qc->pktns[i]); |
| 4032 | /* QUIC encryption level context initialization. */ |
| 4033 | 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] | 4034 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4035 | 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] | 4036 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4037 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4038 | /* Initialize the packet number space. */ |
| 4039 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4040 | } |
| 4041 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 4042 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4043 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 4044 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4045 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4046 | /* TX part. */ |
| 4047 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4048 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4049 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4050 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4051 | /* RX part. */ |
| 4052 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4053 | 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] | 4054 | |
| 4055 | qc->nb_pkt_for_cc = 1; |
| 4056 | qc->nb_pkt_since_cc = 0; |
| 4057 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4058 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4059 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4060 | 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] | 4061 | goto err; |
| 4062 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4063 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4064 | /* XXX TO DO: Only one path at this time. */ |
| 4065 | qc->path = &qc->paths[0]; |
| 4066 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 4067 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4068 | /* required to use MTLIST_IN_LIST */ |
| 4069 | MT_LIST_INIT(&qc->accept_list); |
| 4070 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4071 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 4072 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4073 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4074 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4075 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4076 | |
| 4077 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4078 | 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] | 4079 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4080 | if (qc) |
| 4081 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4082 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4083 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* Initialize the timer task of <qc> QUIC connection. |
| 4087 | * Returns 1 if succeeded, 0 if not. |
| 4088 | */ |
| 4089 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4090 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4091 | /* Attach this task to the same thread ID used for the connection */ |
| 4092 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4093 | if (!qc->timer_task) |
| 4094 | return 0; |
| 4095 | |
| 4096 | qc->timer = TICK_ETERNITY; |
| 4097 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4098 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4099 | |
| 4100 | return 1; |
| 4101 | } |
| 4102 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4103 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4104 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4105 | { |
| 4106 | unsigned int expire; |
| 4107 | |
| 4108 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4109 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4110 | } |
| 4111 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4112 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4113 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4114 | */ |
| 4115 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4116 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4117 | if (read) { |
| 4118 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4119 | } |
| 4120 | else { |
| 4121 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4122 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4123 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | /* The task handling the idle timeout */ |
| 4127 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4128 | { |
| 4129 | struct quic_conn *qc = ctx; |
| 4130 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4131 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4132 | * might free the quic-conn too early via quic_close(). |
| 4133 | */ |
| 4134 | qc_notify_close(qc); |
| 4135 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4136 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4137 | * responsible to call quic_close to release it. |
| 4138 | */ |
| 4139 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4140 | if (qc->mux_state != QC_MUX_READY) |
| 4141 | quic_conn_release(qc); |
| 4142 | |
| 4143 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4144 | * least clean some parts of it such as the tasklet. |
| 4145 | */ |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4146 | |
| 4147 | return NULL; |
| 4148 | } |
| 4149 | |
| 4150 | /* Initialize the idle timeout task for <qc>. |
| 4151 | * Returns 1 if succeeded, 0 if not. |
| 4152 | */ |
| 4153 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4154 | { |
| 4155 | qc->idle_timer_task = task_new_here(); |
| 4156 | if (!qc->idle_timer_task) |
| 4157 | return 0; |
| 4158 | |
| 4159 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4160 | qc->idle_timer_task->context = qc; |
| 4161 | qc_idle_timer_rearm(qc, 1); |
| 4162 | task_queue(qc->idle_timer_task); |
| 4163 | |
| 4164 | return 1; |
| 4165 | } |
| 4166 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4167 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4168 | * past one byte of this buffer. |
| 4169 | */ |
| 4170 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4171 | struct quic_rx_packet *pkt) |
| 4172 | { |
| 4173 | unsigned char dcid_len, scid_len; |
| 4174 | |
| 4175 | /* Version */ |
| 4176 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4177 | return 0; |
| 4178 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4179 | /* Destination Connection ID Length */ |
| 4180 | dcid_len = *(*buf)++; |
| 4181 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4182 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4183 | /* XXX MUST BE DROPPED */ |
| 4184 | return 0; |
| 4185 | |
| 4186 | if (dcid_len) { |
| 4187 | /* Check that the length of this received DCID matches the CID lengths |
| 4188 | * of our implementation for non Initials packets only. |
| 4189 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4190 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4191 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4192 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4193 | return 0; |
| 4194 | |
| 4195 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4196 | } |
| 4197 | |
| 4198 | pkt->dcid.len = dcid_len; |
| 4199 | *buf += dcid_len; |
| 4200 | |
| 4201 | /* Source Connection ID Length */ |
| 4202 | scid_len = *(*buf)++; |
| 4203 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4204 | /* XXX MUST BE DROPPED */ |
| 4205 | return 0; |
| 4206 | |
| 4207 | if (scid_len) |
| 4208 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4209 | pkt->scid.len = scid_len; |
| 4210 | *buf += scid_len; |
| 4211 | |
| 4212 | return 1; |
| 4213 | } |
| 4214 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4215 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4216 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4217 | { |
| 4218 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4219 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4220 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4221 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4222 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* 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] | 4226 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4227 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4228 | * the packet first byte. |
| 4229 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4230 | * Returns 1 if succeeded, 0 if not. |
| 4231 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4232 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4233 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4234 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4235 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4236 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4237 | { |
| 4238 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4239 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4240 | struct quic_enc_level *qel; |
| 4241 | /* Only for traces. */ |
| 4242 | struct quic_rx_packet *qpkt_trace; |
| 4243 | |
| 4244 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4245 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4246 | /* The packet number is here. This is also the start minus |
| 4247 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4248 | * protection. |
| 4249 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4250 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4251 | |
| 4252 | tel = quic_packet_type_enc_level(pkt->type); |
| 4253 | qel = &qc->els[tel]; |
| 4254 | |
| 4255 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4256 | /* Note that the following function enables us to unprotect the packet |
| 4257 | * number and its length subsequently used to decrypt the entire |
| 4258 | * packets. |
| 4259 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4260 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4261 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4262 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4263 | goto err; |
| 4264 | } |
| 4265 | |
| 4266 | /* The AAD includes the packet number field found at <pn>. */ |
| 4267 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4268 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4269 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4270 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4271 | 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] | 4272 | /* If the packet number space has been discarded, this packet |
| 4273 | * will be not parsed. |
| 4274 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4275 | 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] | 4276 | goto out; |
| 4277 | } |
| 4278 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4279 | 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] | 4280 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4281 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4282 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4283 | } |
| 4284 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4285 | *el = qel; |
| 4286 | /* No reference counter incrementation here!!! */ |
| 4287 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4288 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4289 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4290 | b_add(&qc->rx.buf, pkt->len); |
| 4291 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4292 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4293 | return 1; |
| 4294 | |
| 4295 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4296 | 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] | 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4301 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4302 | */ |
| 4303 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4304 | unsigned char byte0, int *long_header) |
| 4305 | { |
| 4306 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4307 | pkt->type = |
| 4308 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4309 | *long_header = 1; |
| 4310 | } |
| 4311 | else { |
| 4312 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4313 | *long_header = 0; |
| 4314 | } |
| 4315 | } |
| 4316 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4317 | /* |
| 4318 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4319 | */ |
| 4320 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4321 | { |
| 4322 | int j = 0, version; |
| 4323 | |
| 4324 | do { |
| 4325 | version = quic_supported_version[j]; |
| 4326 | if (version == pkt->version) |
| 4327 | return 1; |
| 4328 | |
| 4329 | version = quic_supported_version[++j]; |
| 4330 | } while(version); |
| 4331 | |
| 4332 | return 0; |
| 4333 | } |
| 4334 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4335 | /* |
| 4336 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4337 | * address <addr>. |
| 4338 | * Implementation of RFC9000 6. Version Negotiation |
| 4339 | * |
| 4340 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4341 | * |
| 4342 | * Returns 0 on success else non-zero |
| 4343 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4344 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4345 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4346 | { |
| 4347 | char buf[256]; |
| 4348 | int i = 0, j, version; |
| 4349 | const socklen_t addrlen = get_addr_len(addr); |
| 4350 | |
| 4351 | /* |
| 4352 | * header form |
| 4353 | * long header, fixed bit to 0 for Version Negotiation |
| 4354 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4355 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4356 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4357 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4358 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4359 | /* null version for Version Negotiation */ |
| 4360 | buf[i++] = '\x00'; |
| 4361 | buf[i++] = '\x00'; |
| 4362 | buf[i++] = '\x00'; |
| 4363 | buf[i++] = '\x00'; |
| 4364 | |
| 4365 | /* source connection id */ |
| 4366 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4367 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4368 | i += pkt->scid.len; |
| 4369 | |
| 4370 | /* destination connection id */ |
| 4371 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4372 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4373 | i += pkt->dcid.len; |
| 4374 | |
| 4375 | /* supported version */ |
| 4376 | j = 0; |
| 4377 | do { |
| 4378 | version = htonl(quic_supported_version[j]); |
| 4379 | memcpy(&buf[i], &version, sizeof(version)); |
| 4380 | i += sizeof(version); |
| 4381 | |
| 4382 | version = quic_supported_version[++j]; |
| 4383 | } while (version); |
| 4384 | |
| 4385 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4386 | return 1; |
| 4387 | |
| 4388 | return 0; |
| 4389 | } |
| 4390 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4391 | /* Generate the token to be used in Retry packets. The token is written to |
| 4392 | * <buf> which is expected to be <len> bytes. |
| 4393 | * |
| 4394 | * Various parameters are expected to be encoded in the token. For now, only |
| 4395 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4396 | * as this CID must be repeated by the server in the transport parameters. |
| 4397 | * |
| 4398 | * TODO add the client address to validate the token origin. |
| 4399 | * |
| 4400 | * Returns the length of the encoded token or 0 on error. |
| 4401 | */ |
| 4402 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4403 | struct quic_rx_packet *pkt) |
| 4404 | { |
| 4405 | const size_t token_len = 1 + pkt->dcid.len; |
| 4406 | unsigned char i = 0; |
| 4407 | |
| 4408 | if (token_len > len) |
| 4409 | return 0; |
| 4410 | |
| 4411 | buf[i++] = pkt->dcid.len; |
| 4412 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4413 | i += pkt->dcid.len; |
| 4414 | |
| 4415 | return i; |
| 4416 | } |
| 4417 | |
| 4418 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4419 | * the Initial <pkt> packet. |
| 4420 | * |
| 4421 | * Returns 0 on success else non-zero. |
| 4422 | */ |
| 4423 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4424 | struct quic_rx_packet *pkt) |
| 4425 | { |
| 4426 | unsigned char buf[128]; |
| 4427 | int i = 0, token_len; |
| 4428 | const socklen_t addrlen = get_addr_len(addr); |
| 4429 | struct quic_cid scid; |
| 4430 | |
| 4431 | /* long header + fixed bit + packet type 0x3 */ |
| 4432 | buf[i++] = 0xf0; |
| 4433 | /* version */ |
| 4434 | buf[i++] = 0x00; |
| 4435 | buf[i++] = 0x00; |
| 4436 | buf[i++] = 0x00; |
| 4437 | buf[i++] = 0x01; |
| 4438 | |
| 4439 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4440 | buf[i++] = pkt->scid.len; |
| 4441 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4442 | i += pkt->scid.len; |
| 4443 | |
| 4444 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4445 | scid.len = QUIC_HAP_CID_LEN; |
| 4446 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4447 | return 1; |
| 4448 | |
| 4449 | buf[i++] = scid.len; |
| 4450 | memcpy(&buf[i], scid.data, scid.len); |
| 4451 | i += scid.len; |
| 4452 | |
| 4453 | /* token */ |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4454 | if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4455 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4456 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4457 | i += token_len; |
| 4458 | |
| 4459 | /* token integrity tag */ |
| 4460 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4461 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4462 | pkt->dcid.len, buf, i)) { |
| 4463 | return 1; |
| 4464 | } |
| 4465 | |
| 4466 | i += QUIC_TLS_TAG_LEN; |
| 4467 | |
| 4468 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4469 | return 1; |
| 4470 | |
| 4471 | return 0; |
| 4472 | } |
| 4473 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4474 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4475 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4476 | * concatenated to the <pkt> DCID field. |
| 4477 | * |
| 4478 | * Returns the instance or NULL if not found. |
| 4479 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4480 | 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] | 4481 | struct listener *l, |
| 4482 | struct sockaddr_storage *saddr) |
| 4483 | { |
| 4484 | struct quic_conn *qc = NULL; |
| 4485 | struct ebmb_node *node; |
| 4486 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4487 | /* set if the quic_conn is found in the second DCID tree */ |
| 4488 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4489 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4490 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4491 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4492 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4493 | /* DCIDs of first packets coming from multiple clients may have |
| 4494 | * the same values. Let's distinguish them by concatenating the |
| 4495 | * socket addresses. |
| 4496 | */ |
| 4497 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4498 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4499 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4500 | if (node) { |
| 4501 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4502 | goto end; |
| 4503 | } |
| 4504 | } |
| 4505 | |
| 4506 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4507 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4508 | * used. |
| 4509 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4510 | 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] | 4511 | if (!node) |
| 4512 | goto end; |
| 4513 | |
| 4514 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4515 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4516 | found_in_dcid = 1; |
| 4517 | |
| 4518 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4519 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4520 | * If already done, this is a noop. |
| 4521 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4522 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4523 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4524 | |
| 4525 | return qc; |
| 4526 | } |
| 4527 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4528 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4529 | * will extract the parameters stored in the token : <odcid>. |
| 4530 | * |
| 4531 | * Returns 0 on success else non-zero. |
| 4532 | */ |
| 4533 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4534 | struct quic_cid *odcid) |
| 4535 | { |
| 4536 | uint64_t odcid_len; |
| 4537 | |
| 4538 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4539 | return 1; |
| 4540 | |
Frédéric Lécaille | f1f812b | 2022-03-17 16:22:02 +0100 | [diff] [blame] | 4541 | if (odcid_len > QUIC_CID_MAXLEN) |
| 4542 | return 1; |
| 4543 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4544 | memcpy(odcid->data, token, odcid_len); |
| 4545 | odcid->len = odcid_len; |
| 4546 | |
| 4547 | return 0; |
| 4548 | } |
| 4549 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4550 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4551 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4552 | * parameters of this session. |
| 4553 | * This is the responsibility of the caller to check the validity of all the |
| 4554 | * pointers passed as parameter to this function. |
| 4555 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4556 | * CO_ER_SSL_NO_MEM. |
| 4557 | */ |
| 4558 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4559 | unsigned char *params, size_t params_len) |
| 4560 | { |
| 4561 | int retry; |
| 4562 | |
| 4563 | retry = 1; |
| 4564 | retry: |
| 4565 | *ssl = SSL_new(ssl_ctx); |
| 4566 | if (!*ssl) { |
| 4567 | if (!retry--) |
| 4568 | goto err; |
| 4569 | |
| 4570 | pool_gc(NULL); |
| 4571 | goto retry; |
| 4572 | } |
| 4573 | |
| 4574 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4575 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4576 | !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] | 4577 | SSL_free(*ssl); |
| 4578 | *ssl = NULL; |
| 4579 | if (!retry--) |
| 4580 | goto err; |
| 4581 | |
| 4582 | pool_gc(NULL); |
| 4583 | goto retry; |
| 4584 | } |
| 4585 | |
| 4586 | return 0; |
| 4587 | |
| 4588 | err: |
| 4589 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4590 | return -1; |
| 4591 | } |
| 4592 | |
| 4593 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4594 | * used to process <qc> received packets. The allocated context is stored in |
| 4595 | * <qc.xprt_ctx>. |
| 4596 | * |
| 4597 | * Returns 0 on success else non-zero. |
| 4598 | */ |
| 4599 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4600 | { |
| 4601 | struct bind_conf *bc = qc->li->bind_conf; |
| 4602 | struct ssl_sock_ctx *ctx = NULL; |
| 4603 | |
| 4604 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4605 | if (!ctx) |
| 4606 | goto err; |
| 4607 | |
| 4608 | ctx->wait_event.tasklet = tasklet_new(); |
| 4609 | if (!ctx->wait_event.tasklet) |
| 4610 | goto err; |
| 4611 | |
| 4612 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4613 | ctx->wait_event.tasklet->context = ctx; |
| 4614 | ctx->wait_event.events = 0; |
| 4615 | ctx->subs = NULL; |
| 4616 | ctx->xprt_ctx = NULL; |
| 4617 | ctx->qc = qc; |
| 4618 | |
| 4619 | /* Set tasklet tid based on the SCID selected by us for this |
| 4620 | * connection. The upper layer will also be binded on the same thread. |
| 4621 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4622 | 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] | 4623 | |
| 4624 | if (qc_is_listener(qc)) { |
| 4625 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4626 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4627 | goto err; |
| 4628 | } |
| 4629 | |
| 4630 | /* Enabling 0-RTT */ |
| 4631 | if (bc->ssl_conf.early_data) |
| 4632 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4633 | |
| 4634 | SSL_set_accept_state(ctx->ssl); |
| 4635 | } |
| 4636 | |
| 4637 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4638 | |
| 4639 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4640 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4641 | |
| 4642 | return 0; |
| 4643 | |
| 4644 | err: |
| 4645 | if (ctx && ctx->wait_event.tasklet) |
| 4646 | tasklet_free(ctx->wait_event.tasklet); |
| 4647 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4648 | |
| 4649 | return 1; |
| 4650 | } |
| 4651 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4652 | 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] | 4653 | struct quic_rx_packet *pkt, int first_pkt, |
| 4654 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4655 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4656 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4657 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4658 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4659 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4660 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4661 | size_t b_cspace; |
| 4662 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4663 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4664 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4665 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4666 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4667 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4668 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4669 | /* This ist only to please to traces and distinguish the |
| 4670 | * packet with parsed packet number from others. |
| 4671 | */ |
| 4672 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4673 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4674 | goto err; |
| 4675 | |
| 4676 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4677 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4678 | /* XXX TO BE DISCARDED */ |
| 4679 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4680 | goto err; |
| 4681 | } |
| 4682 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4683 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4684 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4685 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4686 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4687 | uint64_t len; |
| 4688 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4689 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4690 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4691 | goto err; |
| 4692 | } |
| 4693 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4694 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4695 | * they must have the same DCID. |
| 4696 | */ |
| 4697 | if (!first_pkt && |
| 4698 | (pkt->dcid.len != dgram->dcid_len || |
| 4699 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4700 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4701 | goto err; |
| 4702 | } |
| 4703 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4704 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4705 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4706 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4707 | goto err; |
| 4708 | } |
| 4709 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4710 | /* RFC9000 6. Version Negotiation */ |
| 4711 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4712 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4713 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4714 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4715 | goto err; |
| 4716 | } |
| 4717 | |
| 4718 | 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] | 4719 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4720 | } |
| 4721 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4722 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4723 | * there is no Initial connection IDs storage. |
| 4724 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4725 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4726 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4727 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4728 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4729 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4730 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4731 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4732 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4733 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 4734 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 4735 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4736 | */ |
| 4737 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4738 | |
| 4739 | /* TODO Retry should be automatically activated if |
| 4740 | * suspect network usage is detected. |
| 4741 | */ |
| 4742 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4743 | 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] | 4744 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4745 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4746 | goto err; |
| 4747 | } |
| 4748 | |
| 4749 | goto err; |
| 4750 | } |
| 4751 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4752 | pkt->token = buf; |
| 4753 | buf += pkt->token_len; |
| 4754 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4755 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4756 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4757 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4758 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4759 | goto err; |
| 4760 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4761 | } |
| 4762 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4763 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4764 | end - buf < len) { |
| 4765 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4766 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4767 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4768 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4769 | payload = buf; |
| 4770 | pkt->len = len + payload - beg; |
| 4771 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4772 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4773 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4774 | int ipv4; |
| 4775 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4776 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4777 | const unsigned char *salt = initial_salt_v1; |
| 4778 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4779 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4780 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4781 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4782 | goto err; |
| 4783 | } |
| 4784 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 4785 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 4786 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 4787 | goto err; |
| 4788 | } |
| 4789 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4790 | pkt->saddr = dgram->saddr; |
| 4791 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4792 | qc = qc_new_conn(pkt->version, ipv4, |
| 4793 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4794 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4795 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4796 | goto err; |
| 4797 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4798 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4799 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4800 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4801 | /* Copy the transport parameters. */ |
| 4802 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4803 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4804 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4805 | if (pkt->token_len) { |
| 4806 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4807 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4808 | goto err; |
| 4809 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4810 | /* Copy retry_source_connection_id transport parameter. */ |
| 4811 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4812 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4813 | } |
| 4814 | else { |
| 4815 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4816 | odcid->len = pkt->dcid.len; |
| 4817 | } |
| 4818 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4819 | /* Copy the initial source connection ID. */ |
| 4820 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4821 | qc->enc_params_len = |
| 4822 | quic_transport_params_encode(qc->enc_params, |
| 4823 | qc->enc_params + sizeof qc->enc_params, |
| 4824 | &qc->rx.params, 1); |
| 4825 | if (!qc->enc_params_len) |
| 4826 | goto err; |
| 4827 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4828 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4829 | goto err; |
| 4830 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 4831 | if (!quic_conn_init_timer(qc)) |
| 4832 | goto err; |
| 4833 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4834 | if (!quic_conn_init_idle_timer_task(qc)) |
| 4835 | goto err; |
| 4836 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4837 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4838 | * chosen by the client for Initial packets. |
| 4839 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4840 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4841 | salt = initial_salt_draft_29; |
| 4842 | salt_len = sizeof initial_salt_draft_29; |
| 4843 | } |
| 4844 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4845 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4846 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4847 | goto err; |
| 4848 | } |
| 4849 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4850 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4851 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4852 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4853 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4854 | /* If the insertion failed, it means that another |
| 4855 | * thread has already allocated a QUIC connection for |
| 4856 | * the same CID. Liberate our allocated connection. |
| 4857 | */ |
| 4858 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4859 | qc_to_purge = qc; |
| 4860 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4861 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4862 | pkt->qc = qc; |
| 4863 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4864 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4865 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4866 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4867 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4868 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4869 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4870 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4871 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4872 | } |
| 4873 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4874 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4875 | } |
| 4876 | } |
| 4877 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4878 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4879 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4880 | goto err; |
| 4881 | } |
| 4882 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4883 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4884 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4885 | |
| 4886 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4887 | * they must have the same DCID. |
| 4888 | */ |
| 4889 | if (!first_pkt && |
| 4890 | (pkt->dcid.len != dgram->dcid_len || |
| 4891 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4892 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4893 | goto err; |
| 4894 | } |
| 4895 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4896 | buf += QUIC_HAP_CID_LEN; |
| 4897 | |
| 4898 | /* A short packet is the last one of a UDP datagram. */ |
| 4899 | payload = buf; |
| 4900 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4901 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4902 | 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] | 4903 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4904 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4905 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4906 | goto err; |
| 4907 | } |
| 4908 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4909 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4910 | } |
| 4911 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4912 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 4913 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 4914 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 4915 | qc->nb_pkt_for_cc++; |
| 4916 | qc->nb_pkt_since_cc = 0; |
| 4917 | } |
| 4918 | /* Skip the entire datagram */ |
| 4919 | pkt->len = end - beg; |
| 4920 | TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc); |
| 4921 | goto out; |
| 4922 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4923 | |
| 4924 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4925 | * they must have the same DCID. |
| 4926 | * |
| 4927 | * This check must be done after the final update to pkt.len to |
| 4928 | * properly drop the packet on failure. |
| 4929 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4930 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4931 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4932 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4933 | QUIC_EV_CONN_LPKT, qc); |
| 4934 | /* Reset the anti-amplification bit. It will be set again |
| 4935 | * when sending the next packet if reached again. |
| 4936 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4937 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 4938 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4939 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4940 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4941 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4942 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4943 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4944 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4945 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4946 | goto out; |
| 4947 | } |
| 4948 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4949 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4950 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4951 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4952 | if (b_cspace < pkt->len) { |
| 4953 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4954 | if (b_cspace) { |
| 4955 | b_putchr(&qc->rx.buf, 0x00); |
| 4956 | b_cspace--; |
| 4957 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4958 | b_add(&qc->rx.buf, b_cspace); |
| 4959 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4960 | 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] | 4961 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4962 | goto err; |
| 4963 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4964 | } |
| 4965 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4966 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4967 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4968 | goto err; |
| 4969 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4970 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4971 | 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] | 4972 | if (pkt->aad_len) |
| 4973 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4974 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4975 | /* Wake up the connection packet handler task from here only if all |
| 4976 | * the contexts have been initialized, especially the mux context |
| 4977 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4978 | * will start it if these contexts for the connection are not already |
| 4979 | * initialized. |
| 4980 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4981 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4982 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4983 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4984 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4985 | 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] | 4986 | |
| 4987 | return pkt->len; |
| 4988 | |
| 4989 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4990 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4991 | * This cannot be done by this thread. |
| 4992 | */ |
| 4993 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4994 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4995 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4996 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4997 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4998 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4999 | if (!pkt->len) |
| 5000 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5001 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5002 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5003 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5004 | return -1; |
| 5005 | } |
| 5006 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5007 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5008 | * 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] | 5009 | */ |
| 5010 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 5011 | int type, size_t pn_len, struct quic_conn *conn) |
| 5012 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5013 | 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] | 5014 | return 0; |
| 5015 | |
| 5016 | /* #0 byte flags */ |
| 5017 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5018 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5019 | /* Version */ |
| 5020 | quic_write_uint32(buf, end, conn->version); |
| 5021 | *(*buf)++ = conn->dcid.len; |
| 5022 | /* Destination connection ID */ |
| 5023 | if (conn->dcid.len) { |
| 5024 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5025 | *buf += conn->dcid.len; |
| 5026 | } |
| 5027 | /* Source connection ID */ |
| 5028 | *(*buf)++ = conn->scid.len; |
| 5029 | if (conn->scid.len) { |
| 5030 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5031 | *buf += conn->scid.len; |
| 5032 | } |
| 5033 | |
| 5034 | return 1; |
| 5035 | } |
| 5036 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5037 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5038 | * 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] | 5039 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5040 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5041 | size_t pn_len, struct quic_conn *conn, |
| 5042 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5043 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5044 | if (end - *buf < 1 + conn->dcid.len) |
| 5045 | return 0; |
| 5046 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5047 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5048 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5049 | ((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] | 5050 | /* Destination connection ID */ |
| 5051 | if (conn->dcid.len) { |
| 5052 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5053 | *buf += conn->dcid.len; |
| 5054 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5055 | |
| 5056 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5060 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5061 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5062 | * Returns 1 if succeeded or 0 if failed. |
| 5063 | */ |
| 5064 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5065 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5066 | { |
| 5067 | int i, ret, outlen; |
| 5068 | EVP_CIPHER_CTX *ctx; |
| 5069 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5070 | * and at most 4 bytes for the packet number |
| 5071 | */ |
| 5072 | unsigned char mask[5] = {0}; |
| 5073 | |
| 5074 | ret = 0; |
| 5075 | ctx = EVP_CIPHER_CTX_new(); |
| 5076 | if (!ctx) |
| 5077 | return 0; |
| 5078 | |
| 5079 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5080 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5081 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5082 | goto out; |
| 5083 | |
| 5084 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5085 | for (i = 0; i < pnlen; i++) |
| 5086 | pn[i] ^= mask[i + 1]; |
| 5087 | |
| 5088 | ret = 1; |
| 5089 | |
| 5090 | out: |
| 5091 | EVP_CIPHER_CTX_free(ctx); |
| 5092 | |
| 5093 | return ret; |
| 5094 | } |
| 5095 | |
| 5096 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5097 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5098 | * Return 1 if succeeded, 0 if not. |
| 5099 | */ |
| 5100 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5101 | { |
| 5102 | size_t room, ack_delay_sz; |
| 5103 | |
| 5104 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5105 | /* A frame is made of 1 byte for the frame type. */ |
| 5106 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5107 | 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] | 5108 | return 0; |
| 5109 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5110 | 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] | 5111 | } |
| 5112 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5113 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5114 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5115 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5116 | * with the number of bytes already present in this buffer which must be taken |
| 5117 | * into an account for the Length packet field value. <headlen> is the number of |
| 5118 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5119 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5120 | * 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] | 5121 | * 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] | 5122 | * Return 1 if succeeded, 0 if not. |
| 5123 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5124 | 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] | 5125 | size_t room, size_t *len, size_t headlen, |
| 5126 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5127 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5128 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5129 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5130 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5131 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5132 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5133 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5134 | return 0; |
| 5135 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5136 | /* If we are not probing we must take into an account the congestion |
| 5137 | * control window. |
| 5138 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5139 | if (!qel->pktns->tx.pto_probe) { |
| 5140 | size_t remain = quic_path_prep_data(qc->path); |
| 5141 | |
| 5142 | if (headlen > remain) |
| 5143 | return 0; |
| 5144 | |
| 5145 | room = QUIC_MIN(room, remain - headlen); |
| 5146 | } |
| 5147 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5148 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5149 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5150 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5151 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5152 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5153 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5154 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5155 | break; |
| 5156 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5157 | switch (cf->type) { |
| 5158 | case QUIC_FT_CRYPTO: |
| 5159 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5160 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5161 | /* Compute the length of this CRYPTO frame header */ |
| 5162 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5163 | /* Compute the data length of this CRyPTO frame. */ |
| 5164 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5165 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5166 | 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] | 5167 | if (!dlen) |
| 5168 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5169 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5170 | /* CRYPTO frame length. */ |
| 5171 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5172 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5173 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5174 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5175 | * length and the length of this length. |
| 5176 | */ |
| 5177 | *len += flen; |
| 5178 | room -= flen; |
| 5179 | if (dlen == cf->crypto.len) { |
| 5180 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5181 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5182 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5183 | } |
| 5184 | else { |
| 5185 | struct quic_frame *new_cf; |
| 5186 | |
| 5187 | new_cf = pool_alloc(pool_head_quic_frame); |
| 5188 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5189 | 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] | 5190 | return 0; |
| 5191 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5192 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5193 | new_cf->type = QUIC_FT_CRYPTO; |
| 5194 | new_cf->crypto.len = dlen; |
| 5195 | new_cf->crypto.offset = cf->crypto.offset; |
| 5196 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5197 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5198 | /* Consume <dlen> bytes of the current frame. */ |
| 5199 | cf->crypto.len -= dlen; |
| 5200 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5201 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5202 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5203 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5204 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5205 | /* Note that these frames are accepted in short packets only without |
| 5206 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5207 | * 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] | 5208 | * |
| 5209 | * 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] | 5210 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5211 | */ |
| 5212 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5213 | ((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] | 5214 | /* Compute the data length of this STREAM frame. */ |
| 5215 | avail_room = room - hlen - *len; |
| 5216 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5217 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5218 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5219 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5220 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5221 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5222 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5223 | if (dlen > cf->stream.len) { |
| 5224 | dlen = cf->stream.len; |
| 5225 | } |
| 5226 | dlen_sz = quic_int_getsize(dlen); |
| 5227 | flen = hlen + dlen_sz + dlen; |
| 5228 | } |
| 5229 | else { |
| 5230 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5231 | flen = hlen + dlen; |
| 5232 | } |
| 5233 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5234 | 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] | 5235 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5236 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5237 | /* Add the STREAM data length and its encoded length to the packet |
| 5238 | * length and the length of this length. |
| 5239 | */ |
| 5240 | *len += flen; |
| 5241 | room -= flen; |
| 5242 | if (dlen == cf->stream.len) { |
| 5243 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5244 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5245 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5246 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5247 | /* The MUX stream might be released at this |
| 5248 | * stage. This can most notably happen on |
| 5249 | * retransmission. |
| 5250 | */ |
| 5251 | if (qc->mux_state == QC_MUX_READY && |
| 5252 | !cf->stream.stream->release) { |
| 5253 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5254 | cf->stream.len, |
| 5255 | cf->stream.offset.key); |
| 5256 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5257 | } |
| 5258 | else { |
| 5259 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5260 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5261 | |
| 5262 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5263 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5264 | 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] | 5265 | return 0; |
| 5266 | } |
| 5267 | |
| 5268 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5269 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5270 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5271 | new_cf->stream.id = cf->stream.id; |
| 5272 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5273 | new_cf->stream.offset = cf->stream.offset; |
| 5274 | new_cf->stream.len = dlen; |
| 5275 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5276 | /* FIN bit reset */ |
| 5277 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5278 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5279 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5280 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5281 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5282 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5283 | b_size(cf->stream.buf), |
| 5284 | (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] | 5285 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5286 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5287 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5288 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5289 | /* The MUX stream might be released at this |
| 5290 | * stage. This can most notably happen on |
| 5291 | * retransmission. |
| 5292 | */ |
| 5293 | if (qc->mux_state == QC_MUX_READY && |
| 5294 | !cf->stream.stream->release) { |
| 5295 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5296 | new_cf->stream.len, |
| 5297 | new_cf->stream.offset.key); |
| 5298 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5299 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5300 | |
| 5301 | /* TODO the MUX is notified about the frame sending via |
| 5302 | * previous qcc_streams_sent_done call. However, the |
| 5303 | * sending can fail later, for example if the sendto |
| 5304 | * system call returns an error. As the MUX has been |
| 5305 | * notified, the transport layer is responsible to |
| 5306 | * bufferize and resent the announced data later. |
| 5307 | */ |
| 5308 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5309 | break; |
| 5310 | |
| 5311 | default: |
| 5312 | flen = qc_frm_len(cf); |
| 5313 | BUG_ON(!flen); |
| 5314 | if (flen > room) |
| 5315 | continue; |
| 5316 | |
| 5317 | *len += flen; |
| 5318 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5319 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5320 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5321 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5322 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5323 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5326 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5327 | } |
| 5328 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5329 | /* 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] | 5330 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5331 | * 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] | 5332 | * filling the buffer with as much frames as possible from <frms> list of |
| 5333 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5334 | * 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] | 5335 | * 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] | 5336 | * having returned from this function. |
| 5337 | * 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] | 5338 | * number field in this packet. <pn_len> will also have the packet number |
| 5339 | * length as value. |
| 5340 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5341 | * 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] | 5342 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5343 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5344 | size_t dglen, struct quic_tx_packet *pkt, |
| 5345 | 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] | 5346 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5347 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5348 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5349 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5350 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5351 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5352 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5353 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5354 | 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] | 5355 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5356 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5357 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5358 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5359 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5360 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5361 | /* Length field value with CRYPTO frames if present. */ |
| 5362 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5363 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5364 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5365 | * buffer to respect the congestion controller window. |
| 5366 | * 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] | 5367 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5368 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5369 | size_t path_room; |
| 5370 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5371 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5372 | if (end - beg > path_room) |
| 5373 | end = beg + path_room; |
| 5374 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5375 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5376 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5377 | * length field if any. |
| 5378 | */ |
| 5379 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5380 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5381 | goto no_room; |
| 5382 | |
| 5383 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5384 | 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] | 5385 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5386 | *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] | 5387 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5388 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5389 | !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] | 5390 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5391 | !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] | 5392 | goto no_room; |
| 5393 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5394 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5395 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5396 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5397 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5398 | /* Build an ACK frame if required. */ |
| 5399 | ack_frm_len = 0; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5400 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) { |
| 5401 | 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] | 5402 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5403 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5404 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5405 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5406 | * that from here, we do not know if we will have to send a PING frame. |
| 5407 | * This will be decided after having computed the ack-eliciting frames |
| 5408 | * to be added to this packet. |
| 5409 | */ |
| 5410 | 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] | 5411 | if (!ack_frm_len) |
| 5412 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5413 | } |
| 5414 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5415 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5416 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5417 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5418 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5419 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5420 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5421 | 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] | 5422 | /* Initialize the length of the frames built below to <len>. |
| 5423 | * If any frame could be successfully built by qc_build_frms(), |
| 5424 | * we will have len_frms > len. |
| 5425 | */ |
| 5426 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5427 | if (!qc_build_frms(&frm_list, frms, |
| 5428 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5429 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5430 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5431 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5432 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5433 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5434 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5435 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5436 | * for the encryption tag. It must be taken into an account for the length |
| 5437 | * of this packet. |
| 5438 | */ |
| 5439 | if (len_frms) |
| 5440 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5441 | else |
| 5442 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5443 | /* CONNECTION_CLOSE frame */ |
| 5444 | if (cc) { |
| 5445 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5446 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5447 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5448 | len += qc_frm_len(&cc_frm); |
| 5449 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5450 | add_ping_frm = 0; |
| 5451 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5452 | len_sz = quic_int_getsize(len); |
| 5453 | /* Add this packet size to <dglen> */ |
| 5454 | dglen += head_len + len_sz + len; |
| 5455 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5456 | /* This is a maximum padding size */ |
| 5457 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5458 | /* The length field value is of this packet is <len> + <padding_len> |
| 5459 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5460 | * <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] | 5461 | * sizes from <padding_len>. |
| 5462 | */ |
| 5463 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5464 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5465 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5466 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5467 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5468 | /* 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] | 5469 | add_ping_frm = 1; |
| 5470 | len += 1; |
| 5471 | } |
| 5472 | /* 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] | 5473 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5474 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5475 | } |
| 5476 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5477 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5478 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5479 | |
| 5480 | /* Packet number field address. */ |
| 5481 | *buf_pn = pos; |
| 5482 | |
| 5483 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5484 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5485 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5486 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5487 | if (ack_frm_len) { |
| 5488 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 5489 | goto no_room; |
| 5490 | |
| 5491 | 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] | 5492 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5493 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5494 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5495 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5496 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5497 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5498 | unsigned char *spos = pos; |
| 5499 | |
| 5500 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5501 | ssize_t room = end - pos; |
| 5502 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5503 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5504 | /* TODO: this should not have happened if qc_build_frms() |
| 5505 | * had correctly computed and sized the frames to be |
| 5506 | * added to this packet. Note that <cf> was added |
| 5507 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 5508 | */ |
| 5509 | LIST_DELETE(&cf->list); |
| 5510 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5511 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5512 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5513 | |
| 5514 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5515 | quic_tx_packet_refinc(pkt); |
| 5516 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5517 | } |
| 5518 | } |
| 5519 | |
| 5520 | /* Build a PING frame if needed. */ |
| 5521 | if (add_ping_frm) { |
| 5522 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5523 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5524 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5525 | } |
| 5526 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5527 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5528 | if (cc) { |
| 5529 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 5530 | goto no_room; |
| 5531 | |
| 5532 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 5533 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5534 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5535 | /* Build a PADDING frame if needed. */ |
| 5536 | if (padding_len) { |
| 5537 | frm.type = QUIC_FT_PADDING; |
| 5538 | frm.padding.len = padding_len; |
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 | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5543 | /* If this packet is ack-eliciting and we are probing let's |
| 5544 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5545 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5546 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5547 | qel->pktns->tx.pto_probe) |
| 5548 | qel->pktns->tx.pto_probe--; |
| 5549 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5550 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5551 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5552 | 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] | 5553 | |
| 5554 | return 1; |
| 5555 | |
| 5556 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5557 | /* 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] | 5558 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5559 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt); |
| 5560 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5561 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5562 | } |
| 5563 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5564 | 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] | 5565 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5566 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5567 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5568 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5569 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5570 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5571 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5572 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5573 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5574 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5575 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5576 | } |
| 5577 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5578 | /* 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] | 5579 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 5580 | * of prebuilt frames. |
| 5581 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5582 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5583 | * -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] | 5584 | * XXX NOTE XXX |
| 5585 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 5586 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 5587 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5588 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5589 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5590 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5591 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5592 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5593 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5594 | { |
| 5595 | /* The pointer to the packet number field. */ |
| 5596 | unsigned char *buf_pn; |
| 5597 | unsigned char *beg, *end, *payload; |
| 5598 | int64_t pn; |
| 5599 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5600 | struct quic_tls_ctx *tls_ctx; |
| 5601 | struct quic_tx_packet *pkt; |
| 5602 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5603 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5604 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5605 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5606 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5607 | 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] | 5608 | *err = -2; |
| 5609 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5610 | } |
| 5611 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5612 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5613 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5614 | pn_len = 0; |
| 5615 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5616 | |
| 5617 | pn = qel->pktns->tx.next_pn + 1; |
| 5618 | 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] | 5619 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5620 | *err = -1; |
| 5621 | goto err; |
| 5622 | } |
| 5623 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5624 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5625 | payload = buf_pn + pn_len; |
| 5626 | payload_len = end - payload; |
| 5627 | aad_len = payload - beg; |
| 5628 | |
| 5629 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5630 | 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] | 5631 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5632 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5633 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5634 | |
| 5635 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5636 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5637 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5638 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5639 | 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] | 5640 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5641 | goto err; |
| 5642 | } |
| 5643 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5644 | /* Consume a packet number */ |
| 5645 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5646 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 5647 | 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] | 5648 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5649 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5650 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5651 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5652 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5653 | /* 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] | 5654 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5655 | pkt->in_flight_len = pkt->len; |
| 5656 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5657 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 5658 | /* Always reset this flags */ |
| 5659 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5660 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 5661 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 5662 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 5663 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5664 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5665 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5666 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5667 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5668 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5669 | |
| 5670 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5671 | /* TODO: what about the frames which have been built |
| 5672 | * for this packet. |
| 5673 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5674 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5675 | 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] | 5676 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5677 | } |
| 5678 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5679 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5680 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5681 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5682 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5683 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5684 | */ |
| 5685 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5686 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5687 | struct qcc *qcc = conn->qc->qcc; |
| 5688 | |
| 5689 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5690 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5691 | |
| 5692 | es->events |= event_type; |
| 5693 | qcc->subs = es; |
| 5694 | |
| 5695 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5696 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5697 | |
| 5698 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5699 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5700 | |
| 5701 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5705 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5706 | * subscribe() call. It always returns zero. |
| 5707 | */ |
| 5708 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5709 | { |
| 5710 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5711 | } |
| 5712 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5713 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5714 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5715 | */ |
| 5716 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5717 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5718 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5719 | |
| 5720 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5721 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5722 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5723 | if (*xprt_ctx) |
| 5724 | goto out; |
| 5725 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5726 | *xprt_ctx = conn->qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5727 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5728 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5729 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5730 | |
| 5731 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5732 | } |
| 5733 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5734 | /* Start the QUIC transport layer */ |
| 5735 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5736 | { |
| 5737 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5738 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5739 | |
| 5740 | qc = conn->qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5741 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5742 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
| 5743 | return 0; |
| 5744 | } |
| 5745 | |
| 5746 | /* mux-quic can now be considered ready. */ |
| 5747 | qc->mux_state = QC_MUX_READY; |
| 5748 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5749 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5750 | return 1; |
| 5751 | } |
| 5752 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5753 | /* transport-layer operations for QUIC connections. */ |
| 5754 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5755 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5756 | .subscribe = quic_conn_subscribe, |
| 5757 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5758 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5759 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5760 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5761 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5762 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5763 | .name = "QUIC", |
| 5764 | }; |
| 5765 | |
| 5766 | __attribute__((constructor)) |
| 5767 | static void __quic_conn_init(void) |
| 5768 | { |
| 5769 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5770 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5771 | } |
| 5772 | |
| 5773 | __attribute__((destructor)) |
| 5774 | static void __quic_conn_deinit(void) |
| 5775 | { |
| 5776 | BIO_meth_free(ha_quic_meth); |
| 5777 | } |
| 5778 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5779 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5780 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5781 | * 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] | 5782 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5783 | 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] | 5784 | { |
| 5785 | unsigned char *pos; |
| 5786 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5787 | struct quic_dghdlr *dghdlr = ctx; |
| 5788 | struct quic_dgram *dgram; |
| 5789 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5790 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5791 | 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] | 5792 | pos = dgram->buf; |
| 5793 | end = pos + dgram->len; |
| 5794 | do { |
| 5795 | int ret; |
| 5796 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5797 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5798 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 5799 | if (!pkt) |
| 5800 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5801 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5802 | quic_rx_packet_refinc(pkt); |
| 5803 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 5804 | first_pkt = 0; |
| 5805 | pos += pkt->len; |
| 5806 | quic_rx_packet_refdec(pkt); |
| 5807 | if (ret == -1) |
| 5808 | /* If the packet length could not be found, we cannot continue. */ |
| 5809 | break; |
| 5810 | } while (pos < end); |
| 5811 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5812 | /* Increasing the received bytes counter by the UDP datagram length |
| 5813 | * if this datagram could be associated to a connection. |
| 5814 | */ |
| 5815 | if (dgram->qc) |
| 5816 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 5817 | |
| 5818 | /* Mark this datagram as consumed */ |
| 5819 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5820 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5821 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5822 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5823 | |
| 5824 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5825 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5826 | } |
| 5827 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5828 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 5829 | * Returns 1 if succeeded, 0 if not. |
| 5830 | */ |
| 5831 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 5832 | unsigned char **dcid, size_t *dcid_len) |
| 5833 | { |
| 5834 | int long_header; |
| 5835 | size_t minlen, skip; |
| 5836 | |
| 5837 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 5838 | goto err; |
| 5839 | |
| 5840 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 5841 | minlen = long_header ? |
| 5842 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 5843 | 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] | 5844 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5845 | goto err; |
| 5846 | |
| 5847 | buf += skip; |
| 5848 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 5849 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 5850 | goto err; |
| 5851 | |
| 5852 | *dcid = buf; |
| 5853 | |
| 5854 | return 1; |
| 5855 | |
| 5856 | err: |
| 5857 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 5858 | return 0; |
| 5859 | } |
| 5860 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5861 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 5862 | * correct datagram handler. |
| 5863 | * Return 1 if a correct datagram could be found, 0 if not. |
| 5864 | */ |
| 5865 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 5866 | struct sockaddr_storage *saddr, |
| 5867 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5868 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5869 | struct quic_dgram *dgram; |
| 5870 | unsigned char *dcid; |
| 5871 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5872 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5873 | |
| 5874 | 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] | 5875 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5876 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5877 | 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] | 5878 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5879 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5880 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5881 | cid_tid = quic_get_cid_tid(dcid); |
| 5882 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5883 | /* All the members must be initialized! */ |
| 5884 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5885 | dgram->buf = buf; |
| 5886 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5887 | dgram->dcid = dcid; |
| 5888 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5889 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5890 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5891 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5892 | 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] | 5893 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5894 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5895 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5896 | return 1; |
| 5897 | |
| 5898 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5899 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5900 | } |
| 5901 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5902 | /* Allocate a new stream descriptor with id <id>. The caller is responsible to |
| 5903 | * store the stream in the appropriate tree. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5904 | * |
| 5905 | * Returns the newly allocated instance on success or else NULL. |
| 5906 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5907 | 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] | 5908 | { |
| 5909 | struct qc_stream_desc *stream; |
| 5910 | |
| 5911 | stream = pool_alloc(pool_head_quic_conn_stream); |
| 5912 | if (!stream) |
| 5913 | return NULL; |
| 5914 | |
| 5915 | stream->by_id.key = id; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5916 | stream->by_id.node.leaf_p = NULL; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5917 | |
| 5918 | stream->buf = BUF_NULL; |
| 5919 | stream->acked_frms = EB_ROOT; |
| 5920 | stream->ack_offset = 0; |
| 5921 | stream->release = 0; |
| 5922 | stream->ctx = ctx; |
| 5923 | |
| 5924 | return stream; |
| 5925 | } |
| 5926 | |
| 5927 | /* 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] | 5928 | * be freed as soon as all its buffered data are acknowledged. In the meantime, |
| 5929 | * the stream is stored in the <qc> tree : thus it must have been removed from |
| 5930 | * any other tree before calling this function. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5931 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5932 | void qc_stream_desc_release(struct qc_stream_desc *stream, |
| 5933 | struct quic_conn *qc) |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5934 | { |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5935 | BUG_ON(stream->by_id.node.leaf_p); |
| 5936 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5937 | stream->release = 1; |
| 5938 | stream->ctx = NULL; |
| 5939 | |
| 5940 | if (!b_data(&stream->buf)) |
| 5941 | qc_stream_desc_free(stream); |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5942 | else |
| 5943 | eb64_insert(&qc->streams_by_id, &stream->by_id); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5944 | } |
| 5945 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 5946 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 5947 | void qc_notify_close(struct quic_conn *qc) |
| 5948 | { |
| 5949 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 5950 | return; |
| 5951 | |
| 5952 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 5953 | |
| 5954 | /* wake up the MUX */ |
| 5955 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 5956 | qc->conn->mux->wake(qc->conn); |
| 5957 | } |
| 5958 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5959 | /* Function to automatically activate QUIC traces on stdout. |
| 5960 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5961 | * Main use for now is in the docker image for QUIC interop testing. |
| 5962 | */ |
| 5963 | static void quic_init_stdout_traces(void) |
| 5964 | { |
| 5965 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5966 | trace_quic.sink = sink_find("stdout"); |
| 5967 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5968 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5969 | #endif |
| 5970 | } |
| 5971 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5972 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5973 | /* |
| 5974 | * Local variables: |
| 5975 | * c-indent-level: 8 |
| 5976 | * c-basic-offset: 8 |
| 5977 | * End: |
| 5978 | */ |