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 | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 180 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 181 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 182 | /* Only for debug purpose */ |
| 183 | struct enc_debug_info { |
| 184 | unsigned char *payload; |
| 185 | size_t payload_len; |
| 186 | unsigned char *aad; |
| 187 | size_t aad_len; |
| 188 | uint64_t pn; |
| 189 | }; |
| 190 | |
| 191 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 192 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 193 | unsigned char *payload, size_t payload_len, |
| 194 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 195 | { |
| 196 | edi->payload = payload; |
| 197 | edi->payload_len = payload_len; |
| 198 | edi->aad = aad; |
| 199 | edi->aad_len = aad_len; |
| 200 | edi->pn = pn; |
| 201 | } |
| 202 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 203 | /* Trace callback for QUIC. |
| 204 | * These traces always expect that arg1, if non-null, is of type connection. |
| 205 | */ |
| 206 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 207 | const struct ist where, const struct ist func, |
| 208 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 209 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 210 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 212 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 213 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 215 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 216 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 217 | chunk_appendf(&trace_buf, "\n odcid"); |
| 218 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 219 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 220 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 221 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 222 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 226 | const enum ssl_encryption_level_t *level = a2; |
| 227 | const size_t *len = a3; |
| 228 | |
| 229 | if (level) { |
| 230 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 231 | |
| 232 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 233 | } |
| 234 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 235 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 236 | } |
| 237 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 238 | /* Initial read & write secrets. */ |
| 239 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 240 | const unsigned char *rx_sec = a2; |
| 241 | const unsigned char *tx_sec = a3; |
| 242 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 243 | tls_ctx = &qc->els[level].tls_ctx; |
| 244 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 245 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 246 | if (rx_sec) |
| 247 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 248 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 249 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 250 | if (tx_sec) |
| 251 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 252 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 256 | const enum ssl_encryption_level_t *level = a2; |
| 257 | const unsigned char *secret = a3; |
| 258 | const size_t *secret_len = a4; |
| 259 | |
| 260 | if (level) { |
| 261 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 262 | |
| 263 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 264 | if (secret && secret_len) |
| 265 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 266 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 267 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 268 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 273 | const enum ssl_encryption_level_t *level = a2; |
| 274 | const unsigned char *secret = a3; |
| 275 | const size_t *secret_len = a4; |
| 276 | |
| 277 | if (level) { |
| 278 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 279 | |
| 280 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 281 | if (secret && secret_len) |
| 282 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 283 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 284 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 285 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 290 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 291 | const struct list *l = a2; |
| 292 | |
| 293 | if (l) { |
| 294 | const struct quic_frame *frm; |
| 295 | list_for_each_entry(frm, l, list) |
| 296 | chunk_frm_appendf(&trace_buf, frm); |
| 297 | } |
| 298 | } |
| 299 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 300 | if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 301 | const struct quic_tx_packet *pkt = a2; |
| 302 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 303 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 304 | |
| 305 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 306 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 307 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 308 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 309 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 310 | (unsigned long long)qc->path->cwnd, |
| 311 | (unsigned long long)qc->path->prep_in_flight, |
| 312 | (unsigned long long)qc->path->in_flight, |
| 313 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 314 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 315 | } |
| 316 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 317 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 318 | if (pkt->pn_node.key != (uint64_t)-1) |
| 319 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 320 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 321 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 322 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 323 | (unsigned long long)qc->rx.bytes, |
| 324 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (room) { |
| 328 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 329 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 330 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 334 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 335 | const enum quic_handshake_state *state = a2; |
| 336 | const int *err = a3; |
| 337 | |
| 338 | if (state) |
| 339 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 340 | if (err) |
| 341 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 342 | } |
| 343 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 344 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 345 | const struct quic_rx_packet *pkt = a2; |
| 346 | const unsigned long *pktlen = a3; |
| 347 | const SSL *ssl = a4; |
| 348 | |
| 349 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 350 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 351 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 352 | if (pkt->pnl) |
| 353 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 354 | (unsigned long long)pkt->pn); |
| 355 | if (pkt->token_len) |
| 356 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 357 | (unsigned long long)pkt->token_len); |
| 358 | if (pkt->aad_len) |
| 359 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 360 | (unsigned long long)pkt->aad_len); |
| 361 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 362 | pkt->flags, (unsigned long long)pkt->len); |
| 363 | } |
| 364 | if (pktlen) |
| 365 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 366 | if (ssl) { |
| 367 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 368 | chunk_appendf(&trace_buf, " el=%c", |
| 369 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 374 | const struct quic_rx_packet *pkt = a2; |
| 375 | const struct quic_rx_crypto_frm *cf = a3; |
| 376 | const SSL *ssl = a4; |
| 377 | |
| 378 | if (pkt) |
| 379 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 380 | quic_packet_type_enc_level_char(pkt->type), |
| 381 | (unsigned long long)pkt->pn); |
| 382 | if (cf) |
| 383 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 384 | (unsigned long long)cf->offset_node.key, |
| 385 | (unsigned long long)cf->len); |
| 386 | if (ssl) { |
| 387 | 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] | 388 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 389 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 390 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 391 | |
| 392 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 393 | 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] | 394 | } |
| 395 | |
| 396 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 397 | const struct quic_frame *frm = a2; |
| 398 | |
| 399 | if (frm) |
| 400 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 401 | } |
| 402 | |
| 403 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 404 | const struct quic_enc_level *qel = a2; |
| 405 | |
| 406 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 407 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 408 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 409 | " 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] | 410 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 411 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 412 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 413 | (unsigned long long)qc->path->cwnd, |
| 414 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 415 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 416 | (unsigned long long)pktns->tx.in_flight, |
| 417 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 421 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 422 | const struct enc_debug_info *edi = a2; |
| 423 | |
| 424 | if (edi) |
| 425 | chunk_appendf(&trace_buf, |
| 426 | " payload=@%p payload_len=%llu" |
| 427 | " aad=@%p aad_len=%llu pn=%llu", |
| 428 | edi->payload, (unsigned long long)edi->payload_len, |
| 429 | edi->aad, (unsigned long long)edi->aad_len, |
| 430 | (unsigned long long)edi->pn); |
| 431 | } |
| 432 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 433 | if (mask & QUIC_EV_CONN_RMHP) { |
| 434 | const struct quic_rx_packet *pkt = a2; |
| 435 | |
| 436 | if (pkt) { |
| 437 | const int *ret = a3; |
| 438 | |
| 439 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 440 | if (ret && *ret) |
| 441 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 442 | pkt->pnl, (unsigned long long)pkt->pn); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 447 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 448 | const unsigned long *val1 = a3; |
| 449 | const unsigned long *val2 = a4; |
| 450 | |
| 451 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 452 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 453 | if (val1) |
| 454 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 455 | if (val2) |
| 456 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 457 | } |
| 458 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 459 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 460 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 461 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 462 | |
| 463 | if (s) |
| 464 | 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] | 465 | if (stream) |
| 466 | 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] | 467 | } |
| 468 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 469 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 470 | const unsigned int *rtt_sample = a2; |
| 471 | const unsigned int *ack_delay = a3; |
| 472 | const struct quic_loss *ql = a4; |
| 473 | |
| 474 | if (rtt_sample) |
| 475 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 476 | if (ack_delay) |
| 477 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 478 | if (ql) |
| 479 | chunk_appendf(&trace_buf, |
| 480 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 481 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 482 | } |
| 483 | if (mask & QUIC_EV_CONN_CC) { |
| 484 | const struct quic_cc_event *ev = a2; |
| 485 | const struct quic_cc *cc = a3; |
| 486 | |
| 487 | if (a2) |
| 488 | quic_cc_event_trace(&trace_buf, ev); |
| 489 | if (a3) |
| 490 | quic_cc_state_trace(&trace_buf, cc); |
| 491 | } |
| 492 | |
| 493 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 494 | const struct quic_pktns *pktns = a2; |
| 495 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 496 | |
| 497 | if (pktns) { |
| 498 | chunk_appendf(&trace_buf, " pktns=%s", |
| 499 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 500 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 501 | if (pktns->tx.loss_time) |
| 502 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 503 | 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] | 504 | } |
| 505 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 506 | struct quic_tx_packet *pkt; |
| 507 | |
| 508 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 509 | list_for_each_entry(pkt, lost_pkts, list) |
| 510 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | 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] | 515 | const struct quic_pktns *pktns = a2; |
| 516 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 517 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 518 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 519 | if (ifae_pkts) |
| 520 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 521 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 522 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 523 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 524 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 525 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 526 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 527 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 528 | if (pktns->tx.in_flight) |
| 529 | 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] | 530 | if (pktns->tx.loss_time) |
| 531 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 532 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 533 | } |
| 534 | if (mask & QUIC_EV_CONN_SPTO) { |
| 535 | if (pktns->tx.time_of_last_eliciting) |
| 536 | chunk_appendf(&trace_buf, " tole=%dms", |
| 537 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 538 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 539 | 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] | 540 | } |
| 541 | } |
| 542 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 543 | 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] | 544 | chunk_appendf(&trace_buf, |
| 545 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 550 | const struct quic_tx_packet *pkt = a2; |
| 551 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 552 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 553 | (unsigned long long)qc->path->cwnd, |
| 554 | (unsigned long long)qc->path->prep_in_flight, |
| 555 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 556 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 557 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 558 | (unsigned long)pkt->pn_node.key, |
| 559 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 560 | 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] | 561 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 564 | |
| 565 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 566 | const uint8_t *alert = a2; |
| 567 | const enum ssl_encryption_level_t *level = a3; |
| 568 | |
| 569 | if (alert) |
| 570 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 571 | if (level) |
| 572 | chunk_appendf(&trace_buf, " el=%c", |
| 573 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 574 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 575 | |
| 576 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 577 | const size_t *sz1 = a2; |
| 578 | const size_t *sz2 = a3; |
| 579 | const size_t *sz3 = a4; |
| 580 | |
| 581 | if (sz1) |
| 582 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 583 | if (sz2) |
| 584 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 585 | if (sz3) |
| 586 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 587 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 588 | |
| 589 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 590 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 591 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 592 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 593 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 594 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 595 | } |
| 596 | if (mask & QUIC_EV_CONN_LPKT) { |
| 597 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 598 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 599 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 600 | if (pkt) { |
| 601 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 602 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 603 | if (pkt->pn_node.key != (uint64_t)-1) |
| 604 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 605 | } |
| 606 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 607 | if (len) |
| 608 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | } |
| 612 | |
| 613 | /* 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] | 614 | 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] | 615 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 616 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 617 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 618 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 619 | return 1; |
| 620 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 621 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 622 | 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] | 623 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 624 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 625 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 626 | return 1; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 632 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 633 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 634 | 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] | 635 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 636 | struct quic_pktns *pktns; |
| 637 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 638 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 639 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 640 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 641 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 642 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 643 | pktns = quic_loss_pktns(qc); |
| 644 | if (tick_isset(pktns->tx.loss_time)) { |
| 645 | qc->timer = pktns->tx.loss_time; |
| 646 | goto out; |
| 647 | } |
| 648 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 649 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 650 | * cancelled for a server which reached the anti-amplification limit. |
| 651 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 652 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 653 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 654 | 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] | 655 | qc->timer = TICK_ETERNITY; |
| 656 | goto out; |
| 657 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 658 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 659 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 660 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 661 | /* Timer cancellation. */ |
| 662 | qc->timer = TICK_ETERNITY; |
| 663 | goto out; |
| 664 | } |
| 665 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 666 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 667 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 668 | if (tick_isset(pto)) |
| 669 | qc->timer = pto; |
| 670 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 671 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 672 | if (tick_is_expired(qc->timer, now_ms)) { |
| 673 | 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] | 674 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 675 | } |
| 676 | else { |
| 677 | 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] | 678 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 679 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 680 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 681 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 684 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 685 | * connection. |
| 686 | * Return 1 if succeeded, 0 if not. |
| 687 | */ |
| 688 | static int quic_tls_key_update(struct quic_conn *qc) |
| 689 | { |
| 690 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 691 | struct quic_tls_secrets *rx, *tx; |
| 692 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 693 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 694 | |
| 695 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 696 | rx = &tls_ctx->rx; |
| 697 | tx = &tls_ctx->tx; |
| 698 | nxt_rx = &qc->ku.nxt_rx; |
| 699 | nxt_tx = &qc->ku.nxt_tx; |
| 700 | |
| 701 | /* Prepare new RX secrets */ |
| 702 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 703 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 704 | 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] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 709 | nxt_rx->key, nxt_rx->keylen, |
| 710 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 711 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 712 | 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] | 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* Prepare new TX secrets */ |
| 717 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 718 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 719 | 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] | 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 724 | nxt_tx->key, nxt_tx->keylen, |
| 725 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 726 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 727 | 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] | 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 735 | * Must be used after having updated them. |
| 736 | * Always succeeds. |
| 737 | */ |
| 738 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 739 | { |
| 740 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 741 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 742 | |
| 743 | /* Rotate the RX secrets */ |
| 744 | curr_secret = tls_ctx->rx.secret; |
| 745 | curr_iv = tls_ctx->rx.iv; |
| 746 | curr_key = tls_ctx->rx.key; |
| 747 | |
| 748 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 749 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 750 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 751 | |
| 752 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 753 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 754 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 755 | |
| 756 | qc->ku.prv_rx.secret = curr_secret; |
| 757 | qc->ku.prv_rx.iv = curr_iv; |
| 758 | qc->ku.prv_rx.key = curr_key; |
| 759 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 760 | |
| 761 | /* Update the TX secrets */ |
| 762 | curr_secret = tls_ctx->tx.secret; |
| 763 | curr_iv = tls_ctx->tx.iv; |
| 764 | curr_key = tls_ctx->tx.key; |
| 765 | |
| 766 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 767 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 768 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 769 | |
| 770 | qc->ku.nxt_tx.secret = curr_secret; |
| 771 | qc->ku.nxt_tx.iv = curr_iv; |
| 772 | qc->ku.nxt_tx.key = curr_key; |
| 773 | } |
| 774 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 775 | #ifndef OPENSSL_IS_BORINGSSL |
| 776 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 777 | const uint8_t *read_secret, |
| 778 | const uint8_t *write_secret, size_t secret_len) |
| 779 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 780 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 781 | 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] | 782 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 783 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 784 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 785 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 786 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 787 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 788 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 789 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 790 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 791 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 792 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 793 | 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] | 794 | goto err; |
| 795 | } |
| 796 | |
| 797 | rx = &tls_ctx->rx; |
| 798 | tx = &tls_ctx->tx; |
| 799 | |
| 800 | rx->aead = tx->aead = tls_aead(cipher); |
| 801 | rx->md = tx->md = tls_md(cipher); |
| 802 | rx->hp = tx->hp = tls_hp(cipher); |
| 803 | |
| 804 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 805 | 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] | 806 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 807 | 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] | 808 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 811 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 812 | * listener. Note that a listener derives only RX secrets for this |
| 813 | * level. |
| 814 | */ |
| 815 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 816 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 817 | |
| 818 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 819 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 820 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 821 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 822 | 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] | 823 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 824 | 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] | 825 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 828 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 829 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 830 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 831 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 832 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 833 | /* 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] | 834 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 835 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 836 | 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] | 837 | goto err; |
| 838 | } |
| 839 | |
| 840 | memcpy(rx->secret, read_secret, secret_len); |
| 841 | rx->secretlen = secret_len; |
| 842 | memcpy(tx->secret, write_secret, secret_len); |
| 843 | tx->secretlen = secret_len; |
| 844 | /* Initialize all the secret keys lengths */ |
| 845 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 846 | /* Prepare the next key update */ |
| 847 | if (!quic_tls_key_update(qc)) |
| 848 | goto err; |
| 849 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 850 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 851 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 852 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 853 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 854 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 855 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 856 | |
| 857 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 858 | 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] | 859 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 860 | } |
| 861 | #else |
| 862 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 863 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 864 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 865 | */ |
| 866 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 867 | const SSL_CIPHER *cipher, |
| 868 | const uint8_t *secret, size_t secret_len) |
| 869 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 870 | 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] | 871 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 872 | &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] | 873 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 874 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 875 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 876 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 877 | goto out; |
| 878 | } |
| 879 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 880 | tls_ctx->rx.aead = tls_aead(cipher); |
| 881 | tls_ctx->rx.md = tls_md(cipher); |
| 882 | tls_ctx->rx.hp = tls_hp(cipher); |
| 883 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 884 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 885 | goto err; |
| 886 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 887 | 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] | 888 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 889 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 890 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 891 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 892 | 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] | 893 | goto err; |
| 894 | } |
| 895 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 896 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 897 | const unsigned char *buf; |
| 898 | size_t buflen; |
| 899 | |
| 900 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 901 | if (!buflen) |
| 902 | goto err; |
| 903 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 904 | 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] | 905 | goto err; |
| 906 | } |
| 907 | |
| 908 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 909 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 910 | 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] | 911 | |
| 912 | return 1; |
| 913 | |
| 914 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 915 | 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] | 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 920 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 921 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 922 | */ |
| 923 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 924 | const SSL_CIPHER *cipher, |
| 925 | const uint8_t *secret, size_t secret_len) |
| 926 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 927 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 928 | 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] | 929 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 930 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 931 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 932 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 933 | goto out; |
| 934 | } |
| 935 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 936 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 937 | goto err; |
| 938 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 939 | tls_ctx->tx.aead = tls_aead(cipher); |
| 940 | tls_ctx->tx.md = tls_md(cipher); |
| 941 | tls_ctx->tx.hp = tls_hp(cipher); |
| 942 | |
| 943 | 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] | 944 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 945 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 946 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 947 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 948 | 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] | 949 | goto err; |
| 950 | } |
| 951 | |
| 952 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 953 | 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] | 954 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 955 | return 1; |
| 956 | |
| 957 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 958 | 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] | 959 | return 0; |
| 960 | } |
| 961 | #endif |
| 962 | |
| 963 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 964 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 965 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 966 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 967 | * store all the data. |
| 968 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 969 | */ |
| 970 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 971 | const unsigned char *data, size_t len) |
| 972 | { |
| 973 | struct quic_crypto_buf **qcb; |
| 974 | /* The remaining byte to store in CRYPTO buffers. */ |
| 975 | size_t cf_offset, cf_len, *nb_buf; |
| 976 | unsigned char *pos; |
| 977 | |
| 978 | nb_buf = &qel->tx.crypto.nb_buf; |
| 979 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 980 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 981 | cf_len = len; |
| 982 | |
| 983 | while (len) { |
| 984 | size_t to_copy, room; |
| 985 | |
| 986 | pos = (*qcb)->data + (*qcb)->sz; |
| 987 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 988 | to_copy = len > room ? room : len; |
| 989 | if (to_copy) { |
| 990 | memcpy(pos, data, to_copy); |
| 991 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 992 | qel->tx.crypto.sz += to_copy; |
| 993 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 994 | len -= to_copy; |
| 995 | data += to_copy; |
| 996 | } |
| 997 | else { |
| 998 | struct quic_crypto_buf **tmp; |
| 999 | |
| 1000 | tmp = realloc(qel->tx.crypto.bufs, |
| 1001 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1002 | if (tmp) { |
| 1003 | qel->tx.crypto.bufs = tmp; |
| 1004 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1005 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1006 | if (!*qcb) |
| 1007 | return 0; |
| 1008 | |
| 1009 | (*qcb)->sz = 0; |
| 1010 | ++*nb_buf; |
| 1011 | } |
| 1012 | else { |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1019 | * have been buffered. |
| 1020 | */ |
| 1021 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1022 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1023 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1024 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1025 | /* There is at most one CRYPTO frame in this packet number |
| 1026 | * space. Let's look for it. |
| 1027 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1028 | 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] | 1029 | if (frm->type != QUIC_FT_CRYPTO) |
| 1030 | continue; |
| 1031 | |
| 1032 | /* Found */ |
| 1033 | found = frm; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | if (found) { |
| 1038 | found->crypto.len += cf_len; |
| 1039 | } |
| 1040 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1041 | frm = pool_alloc(pool_head_quic_frame); |
| 1042 | if (!frm) |
| 1043 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1044 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1045 | frm->type = QUIC_FT_CRYPTO; |
| 1046 | frm->crypto.offset = cf_offset; |
| 1047 | frm->crypto.len = cf_len; |
| 1048 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1049 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1050 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | return len == 0; |
| 1054 | } |
| 1055 | |
| 1056 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1057 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1058 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1059 | { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1060 | qc->err_code = QC_ERR_CRYPTO_ERROR | alert; |
| 1061 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1062 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1065 | /* Set the application for <qc> QUIC connection. |
| 1066 | * Return 1 if succeeded, 0 if not. |
| 1067 | */ |
| 1068 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1069 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1070 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1071 | qc->app_ops = &h3_ops; |
| 1072 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1073 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1074 | else |
| 1075 | return 0; |
| 1076 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1077 | return 1; |
| 1078 | } |
| 1079 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1080 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1081 | * wants to provide the QUIC layer with CRYPTO data. |
| 1082 | * Returns 1 if succeeded, 0 if not. |
| 1083 | */ |
| 1084 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1085 | const uint8_t *data, size_t len) |
| 1086 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1087 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1088 | enum quic_tls_enc_level tel; |
| 1089 | struct quic_enc_level *qel; |
| 1090 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1091 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1092 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1093 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1094 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1095 | goto out; |
| 1096 | } |
| 1097 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1098 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1099 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1100 | 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] | 1101 | goto err; |
| 1102 | } |
| 1103 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1104 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1105 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1106 | 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] | 1107 | goto err; |
| 1108 | } |
| 1109 | |
| 1110 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1111 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1112 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1113 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1114 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1115 | return 1; |
| 1116 | |
| 1117 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1118 | 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] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | int ha_quic_flush_flight(SSL *ssl) |
| 1123 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1124 | 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] | 1125 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1126 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1127 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1128 | |
| 1129 | return 1; |
| 1130 | } |
| 1131 | |
| 1132 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1133 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1134 | 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] | 1135 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1136 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1137 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1138 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1139 | return 1; |
| 1140 | } |
| 1141 | |
| 1142 | /* QUIC TLS methods */ |
| 1143 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1144 | #ifdef OPENSSL_IS_BORINGSSL |
| 1145 | .set_read_secret = ha_set_rsec, |
| 1146 | .set_write_secret = ha_set_wsec, |
| 1147 | #else |
| 1148 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1149 | #endif |
| 1150 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1151 | .flush_flight = ha_quic_flush_flight, |
| 1152 | .send_alert = ha_quic_send_alert, |
| 1153 | }; |
| 1154 | |
| 1155 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1156 | * Returns an error count. |
| 1157 | */ |
| 1158 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1159 | { |
| 1160 | struct proxy *curproxy = bind_conf->frontend; |
| 1161 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1162 | int cfgerr = 0; |
| 1163 | |
| 1164 | #if 0 |
| 1165 | /* XXX Did not manage to use this. */ |
| 1166 | const char *ciphers = |
| 1167 | "TLS_AES_128_GCM_SHA256:" |
| 1168 | "TLS_AES_256_GCM_SHA384:" |
| 1169 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1170 | "TLS_AES_128_CCM_SHA256"; |
| 1171 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1172 | 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] | 1173 | long options = |
| 1174 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1175 | SSL_OP_SINGLE_ECDH_USE | |
| 1176 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1177 | SSL_CTX *ctx; |
| 1178 | |
| 1179 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1180 | bind_conf->initial_ctx = ctx; |
| 1181 | |
| 1182 | SSL_CTX_set_options(ctx, options); |
| 1183 | #if 0 |
| 1184 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1185 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1186 | "for bind '%s' at [%s:%d].\n", |
| 1187 | curproxy->id, ciphers, |
| 1188 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1189 | cfgerr++; |
| 1190 | } |
| 1191 | #endif |
| 1192 | |
| 1193 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1194 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1195 | "for bind '%s' at [%s:%d].\n", |
| 1196 | curproxy->id, groups, |
| 1197 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1198 | cfgerr++; |
| 1199 | } |
| 1200 | |
| 1201 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1202 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1203 | 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] | 1204 | |
| 1205 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1206 | #ifdef OPENSSL_IS_BORINGSSL |
| 1207 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1208 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1209 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1210 | if (bind_conf->ssl_conf.early_data) { |
| 1211 | 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] | 1212 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1213 | } |
| 1214 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1215 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1216 | #else |
| 1217 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1218 | #endif |
| 1219 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1220 | #endif |
| 1221 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1222 | |
| 1223 | return cfgerr; |
| 1224 | } |
| 1225 | |
| 1226 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1227 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1228 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1229 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1230 | */ |
| 1231 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1232 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1233 | { |
| 1234 | uint64_t expected_pn = largest_pn + 1; |
| 1235 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1236 | uint64_t pn_hwin = pn_win / 2; |
| 1237 | uint64_t pn_mask = pn_win - 1; |
| 1238 | uint64_t candidate_pn; |
| 1239 | |
| 1240 | |
| 1241 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1242 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1243 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1244 | candidate_pn + pn_hwin <= expected_pn) |
| 1245 | return candidate_pn + pn_win; |
| 1246 | |
| 1247 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1248 | return candidate_pn - pn_win; |
| 1249 | |
| 1250 | return candidate_pn; |
| 1251 | } |
| 1252 | |
| 1253 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1254 | * cryptographic context. |
| 1255 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1256 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1257 | * <end> points to one byte past the end of this packet. |
| 1258 | * Returns 1 if succeeded, 0 if not. |
| 1259 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1260 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1261 | 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] | 1262 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1263 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1264 | { |
| 1265 | int ret, outlen, i, pnlen; |
| 1266 | uint64_t packet_number; |
| 1267 | uint32_t truncated_pn = 0; |
| 1268 | unsigned char mask[5] = {0}; |
| 1269 | unsigned char *sample; |
| 1270 | EVP_CIPHER_CTX *cctx; |
| 1271 | unsigned char *hp_key; |
| 1272 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1273 | /* Check there is enough data in this packet. */ |
| 1274 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1275 | 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] | 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | cctx = EVP_CIPHER_CTX_new(); |
| 1280 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1281 | 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] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | ret = 0; |
| 1286 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1287 | |
| 1288 | hp_key = tls_ctx->rx.hp_key; |
| 1289 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1290 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1291 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1292 | 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] | 1293 | goto out; |
| 1294 | } |
| 1295 | |
| 1296 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1297 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1298 | for (i = 0; i < pnlen; i++) { |
| 1299 | pn[i] ^= mask[i + 1]; |
| 1300 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1301 | } |
| 1302 | |
| 1303 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1304 | /* Store remaining information for this unprotected header */ |
| 1305 | pkt->pn = packet_number; |
| 1306 | pkt->pnl = pnlen; |
| 1307 | |
| 1308 | ret = 1; |
| 1309 | |
| 1310 | out: |
| 1311 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1312 | |
| 1313 | return ret; |
| 1314 | } |
| 1315 | |
| 1316 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1317 | * address, with <payload_len> as payload length, <aad> as address of |
| 1318 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1319 | * context. |
| 1320 | * Returns 1 if succeeded, 0 if not. |
| 1321 | */ |
| 1322 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1323 | 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] | 1324 | 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] | 1325 | { |
| 1326 | unsigned char iv[12]; |
| 1327 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1328 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1329 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1330 | |
| 1331 | 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] | 1332 | 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] | 1333 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1337 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1338 | 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] | 1339 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1343 | |
| 1344 | err: |
| 1345 | 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] | 1346 | 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] | 1347 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1351 | * Returns 1 if succeeded, 0 if not. |
| 1352 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1353 | 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] | 1354 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1355 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1356 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1357 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1358 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1359 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1360 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1361 | |
| 1362 | kp_changed = 0; |
| 1363 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1364 | /* The two tested bits are not at the same position, |
| 1365 | * this is why they are first both inversed. |
| 1366 | */ |
| 1367 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1368 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1369 | /* The lowest packet number of a previous key phase |
| 1370 | * cannot be null if it really stores previous key phase |
| 1371 | * secrets. |
| 1372 | */ |
| 1373 | if (!pkt->qc->ku.prv_rx.pn) |
| 1374 | return 0; |
| 1375 | |
| 1376 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1377 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1378 | } |
| 1379 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1380 | /* Next key phase */ |
| 1381 | kp_changed = 1; |
| 1382 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1383 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1384 | } |
| 1385 | } |
| 1386 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1387 | |
| 1388 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1389 | return 0; |
| 1390 | |
| 1391 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1392 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1393 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1394 | if (!ret) |
| 1395 | return 0; |
| 1396 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1397 | /* Update the keys only if the packet decryption succeeded. */ |
| 1398 | if (kp_changed) { |
| 1399 | quic_tls_rotate_keys(pkt->qc); |
| 1400 | /* Toggle the Key Phase bit */ |
| 1401 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1402 | /* Store the lowest packet number received for the current key phase */ |
| 1403 | tls_ctx->rx.pn = pkt->pn; |
| 1404 | /* Prepare the next key update */ |
| 1405 | if (!quic_tls_key_update(pkt->qc)) |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1409 | /* Update the packet length (required to parse the frames). */ |
| 1410 | pkt->len = pkt->aad_len + ret; |
| 1411 | |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 1415 | /* Free the stream descriptor <stream> buffer. This function should be used |
| 1416 | * when all its data have been acknowledged. If the stream was released by the |
| 1417 | * upper layer, the stream descriptor will be freed. |
| 1418 | * |
| 1419 | * Returns 0 if the stream was not freed else non-zero. |
| 1420 | */ |
| 1421 | static int qc_stream_desc_free(struct qc_stream_desc *stream) |
| 1422 | { |
| 1423 | b_free(&stream->buf); |
| 1424 | offer_buffers(NULL, 1); |
| 1425 | |
| 1426 | if (stream->release) { |
| 1427 | eb64_delete(&stream->by_id); |
| 1428 | pool_free(pool_head_quic_conn_stream, stream); |
| 1429 | |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1436 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1437 | * |
| 1438 | * 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] | 1439 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1440 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1441 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1442 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1443 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1444 | struct eb64_node *frm_node; |
| 1445 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1446 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1447 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1448 | while (frm_node) { |
| 1449 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1450 | struct quic_frame *frm; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1451 | |
| 1452 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1453 | if (strm->offset.key > stream->ack_offset) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1454 | break; |
| 1455 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1456 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1457 | qc, strm, stream); |
| 1458 | |
| 1459 | if (strm->offset.key + strm->len > stream->ack_offset) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1460 | const size_t diff = strm->offset.key + strm->len - |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1461 | stream->ack_offset; |
| 1462 | stream->ack_offset += diff; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1463 | b_del(strm->buf, diff); |
| 1464 | ret = 1; |
| 1465 | } |
| 1466 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1467 | frm_node = eb64_next(frm_node); |
| 1468 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1469 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1470 | frm = container_of(strm, struct quic_frame, stream); |
| 1471 | LIST_DELETE(&frm->list); |
| 1472 | quic_tx_packet_refdec(frm->pkt); |
| 1473 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1474 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1475 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1476 | if (!b_data(&stream->buf)) |
| 1477 | qc_stream_desc_free(stream); |
| 1478 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1479 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1482 | /* 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] | 1483 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1484 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1485 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1486 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1487 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1488 | 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] | 1489 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1490 | switch (frm->type) { |
| 1491 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1492 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1493 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1494 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1495 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1496 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1497 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1498 | * 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] | 1499 | * lookup. First search in the MUX then in the released stream |
| 1500 | * list. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1501 | * |
| 1502 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1503 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1504 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1505 | if (qc->mux_state == QC_MUX_READY) |
| 1506 | stream = qcc_get_stream(qc->qcc, strm_frm->id); |
| 1507 | if (!stream) { |
| 1508 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1509 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1510 | } |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1511 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1512 | if (!stream) { |
| 1513 | 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] | 1514 | LIST_DELETE(&frm->list); |
| 1515 | quic_tx_packet_refdec(frm->pkt); |
| 1516 | pool_free(pool_head_quic_frame, frm); |
| 1517 | |
| 1518 | /* early return */ |
| 1519 | return; |
| 1520 | } |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1521 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1522 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1523 | if (strm_frm->offset.key <= stream->ack_offset) { |
| 1524 | if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) { |
| 1525 | const size_t diff = strm_frm->offset.key + strm_frm->len - |
| 1526 | stream->ack_offset; |
| 1527 | stream->ack_offset += diff; |
| 1528 | b_del(strm_frm->buf, diff); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1529 | stream_acked = 1; |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1530 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1531 | if (!b_data(strm_frm->buf)) { |
| 1532 | if (qc_stream_desc_free(stream)) { |
| 1533 | /* early return */ |
| 1534 | return; |
| 1535 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1536 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 1539 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1540 | qc, strm_frm, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1541 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1542 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1543 | pool_free(pool_head_quic_frame, frm); |
| 1544 | } |
| 1545 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1546 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1547 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1548 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1549 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1550 | } |
| 1551 | break; |
| 1552 | default: |
| 1553 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1554 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1555 | pool_free(pool_head_quic_frame, frm); |
| 1556 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1557 | |
| 1558 | if (stream_acked) { |
| 1559 | struct qcc *qcc = qc->qcc; |
| 1560 | |
| 1561 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1562 | tasklet_wakeup(qcc->subs->tasklet); |
| 1563 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1564 | if (!qcc->subs->events) |
| 1565 | qcc->subs = NULL; |
| 1566 | } |
| 1567 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1571 | * deallocating them, and their TX frames. |
| 1572 | * Returns the last node reached to be used for the next range. |
| 1573 | * May be NULL if <largest> node could not be found. |
| 1574 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1575 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1576 | struct eb_root *pkts, |
| 1577 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1578 | struct list *newly_acked_pkts, |
| 1579 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1580 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1581 | { |
| 1582 | struct eb64_node *node; |
| 1583 | struct quic_tx_packet *pkt; |
| 1584 | |
| 1585 | if (largest_node) |
| 1586 | node = largest_node; |
| 1587 | else { |
| 1588 | node = eb64_lookup(pkts, largest); |
| 1589 | while (!node && largest > smallest) { |
| 1590 | node = eb64_lookup(pkts, --largest); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1595 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1596 | |
| 1597 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1598 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1599 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1600 | 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] | 1601 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1602 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1603 | node = eb64_prev(node); |
| 1604 | eb64_delete(&pkt->pn_node); |
| 1605 | } |
| 1606 | |
| 1607 | return node; |
| 1608 | } |
| 1609 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1610 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1611 | * 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] | 1612 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1613 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1614 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1615 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1616 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1617 | struct quic_frame *frm, *frmbak; |
| 1618 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1619 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1620 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1621 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1622 | quic_tx_packet_refdec(frm->pkt); |
| 1623 | /* This frame is not freed but removed from its packet */ |
| 1624 | frm->pkt = NULL; |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1625 | 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] | 1626 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1629 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1630 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1631 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1632 | /* Free <pkt> TX packet and its attached frames. |
| 1633 | * This is the responsability of the caller to remove this packet of |
| 1634 | * any data structure it was possibly attached to. |
| 1635 | */ |
| 1636 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1637 | { |
| 1638 | struct quic_frame *frm, *frmbak; |
| 1639 | |
| 1640 | if (!pkt) |
| 1641 | return; |
| 1642 | |
| 1643 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1644 | LIST_DELETE(&frm->list); |
| 1645 | pool_free(pool_head_quic_frame, frm); |
| 1646 | } |
| 1647 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1648 | } |
| 1649 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1650 | /* Free the TX packets of <pkts> list */ |
| 1651 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1652 | { |
| 1653 | struct quic_tx_packet *pkt, *tmp; |
| 1654 | |
| 1655 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1656 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1657 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1658 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1662 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1663 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1664 | * updating the range which contains <largest_acked_pn>. |
| 1665 | * Never fails. |
| 1666 | */ |
| 1667 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1668 | int64_t largest_acked_pn) |
| 1669 | { |
| 1670 | struct eb64_node *ar, *next_ar; |
| 1671 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1672 | |
| 1673 | ar = eb64_first(&arngs->root); |
| 1674 | while (ar) { |
| 1675 | struct quic_arng_node *ar_node; |
| 1676 | |
| 1677 | next_ar = eb64_next(ar); |
| 1678 | ar_node = eb64_entry(&ar->node, struct quic_arng_node, first); |
| 1679 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1680 | break; |
| 1681 | |
| 1682 | if (largest_acked_pn < ar_node->last) { |
| 1683 | eb64_delete(ar); |
| 1684 | ar_node->first.key = largest_acked_pn + 1; |
| 1685 | eb64_insert(&arngs->root, ar); |
| 1686 | break; |
| 1687 | } |
| 1688 | |
| 1689 | eb64_delete(ar); |
| 1690 | pool_free(pool_head_quic_arng, ar_node); |
| 1691 | arngs->sz--; |
| 1692 | ar = next_ar; |
| 1693 | } |
| 1694 | } |
| 1695 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1696 | /* Send a packet ack event nofication for each newly acked packet of |
| 1697 | * <newly_acked_pkts> list and free them. |
| 1698 | * Always succeeds. |
| 1699 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1700 | 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] | 1701 | struct list *newly_acked_pkts) |
| 1702 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1703 | struct quic_tx_packet *pkt, *tmp; |
| 1704 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1705 | |
| 1706 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1707 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1708 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1709 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1710 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1711 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1712 | /* If this packet contained an ACK frame, proceed to the |
| 1713 | * acknowledging of range of acks from the largest acknowledged |
| 1714 | * packet number which was sent in an ACK frame by this packet. |
| 1715 | */ |
| 1716 | if (pkt->largest_acked_pn != -1) |
| 1717 | 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] | 1718 | ev.ack.acked = pkt->in_flight_len; |
| 1719 | ev.ack.time_sent = pkt->time_sent; |
| 1720 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1721 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1722 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1723 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | } |
| 1727 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1728 | /* Release all the frames attached to <pktns> packet number space */ |
| 1729 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1730 | { |
| 1731 | struct quic_frame *frm, *frmbak; |
| 1732 | |
| 1733 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1734 | LIST_DELETE(&frm->list); |
| 1735 | pool_free(pool_head_quic_frame, frm); |
| 1736 | } |
| 1737 | } |
| 1738 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1739 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1740 | * their TX frames. |
| 1741 | * Send a packet loss event to the congestion controller if |
| 1742 | * in flight packet have been lost. |
| 1743 | * Also frees the packet in <pkts> list. |
| 1744 | * Never fails. |
| 1745 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1746 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1747 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1748 | struct list *pkts, |
| 1749 | uint64_t now_us) |
| 1750 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1751 | 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] | 1752 | uint64_t lost_bytes; |
| 1753 | |
| 1754 | lost_bytes = 0; |
| 1755 | oldest_lost = newest_lost = NULL; |
| 1756 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1757 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1758 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1759 | lost_bytes += pkt->in_flight_len; |
| 1760 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1761 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1762 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1763 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1764 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1765 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1766 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1767 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | if (!oldest_lost) { |
| 1769 | oldest_lost = newest_lost = pkt; |
| 1770 | } |
| 1771 | else { |
| 1772 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1773 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1774 | newest_lost = pkt; |
| 1775 | } |
| 1776 | } |
| 1777 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1778 | if (newest_lost) { |
| 1779 | /* Sent a congestion event to the controller */ |
| 1780 | struct quic_cc_event ev = { |
| 1781 | .type = QUIC_CC_EVT_LOSS, |
| 1782 | .loss.time_sent = newest_lost->time_sent, |
| 1783 | }; |
| 1784 | |
| 1785 | quic_cc_event(&qc->path->cc, &ev); |
| 1786 | } |
| 1787 | |
| 1788 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1789 | * We must check if we are experiencing a persistent congestion. |
| 1790 | * If this is the case, the congestion controller must re-enter |
| 1791 | * slow start state. |
| 1792 | */ |
| 1793 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1794 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1795 | |
| 1796 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1797 | now_ms, qc->max_ack_delay)) |
| 1798 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1799 | } |
| 1800 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1801 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1802 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1803 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1804 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1809 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1810 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1811 | * space if any not deemed lost. |
| 1812 | * Should be called after having received an ACK frame with newly acknowledged |
| 1813 | * packets or when the the loss detection timer has expired. |
| 1814 | * Always succeeds. |
| 1815 | */ |
| 1816 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1817 | struct quic_conn *qc, |
| 1818 | struct list *lost_pkts) |
| 1819 | { |
| 1820 | struct eb_root *pkts; |
| 1821 | struct eb64_node *node; |
| 1822 | struct quic_loss *ql; |
| 1823 | unsigned int loss_delay; |
| 1824 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1825 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1826 | pkts = &pktns->tx.pkts; |
| 1827 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1828 | if (eb_is_empty(pkts)) |
| 1829 | goto out; |
| 1830 | |
| 1831 | ql = &qc->path->loss; |
| 1832 | 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] | 1833 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1834 | |
| 1835 | node = eb64_first(pkts); |
| 1836 | while (node) { |
| 1837 | struct quic_tx_packet *pkt; |
| 1838 | int64_t largest_acked_pn; |
| 1839 | unsigned int loss_time_limit, time_sent; |
| 1840 | |
| 1841 | 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] | 1842 | largest_acked_pn = pktns->rx.largest_acked_pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1843 | node = eb64_next(node); |
| 1844 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1845 | break; |
| 1846 | |
| 1847 | time_sent = pkt->time_sent; |
| 1848 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1849 | if (tick_is_le(time_sent, now_ms) || |
| 1850 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1851 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1852 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1853 | } |
| 1854 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1855 | if (tick_isset(pktns->tx.loss_time)) |
| 1856 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1857 | else |
| 1858 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1863 | 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] | 1864 | } |
| 1865 | |
| 1866 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1867 | * 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] | 1868 | * 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] | 1869 | * acked ack-eliciting packet. |
| 1870 | * Return 1, if succeeded, 0 if not. |
| 1871 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1872 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1873 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1874 | struct quic_enc_level *qel, |
| 1875 | unsigned int *rtt_sample, |
| 1876 | const unsigned char **pos, const unsigned char *end) |
| 1877 | { |
| 1878 | struct quic_ack *ack = &frm->ack; |
| 1879 | uint64_t smallest, largest; |
| 1880 | struct eb_root *pkts; |
| 1881 | struct eb64_node *largest_node; |
| 1882 | unsigned int time_sent, pkt_flags; |
| 1883 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1884 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1885 | |
| 1886 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1887 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1888 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1889 | goto err; |
| 1890 | } |
| 1891 | |
| 1892 | if (ack->first_ack_range > ack->largest_ack) { |
| 1893 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1894 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1895 | goto err; |
| 1896 | } |
| 1897 | |
| 1898 | largest = ack->largest_ack; |
| 1899 | smallest = largest - ack->first_ack_range; |
| 1900 | pkts = &qel->pktns->tx.pkts; |
| 1901 | pkt_flags = 0; |
| 1902 | largest_node = NULL; |
| 1903 | time_sent = 0; |
| 1904 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1905 | 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] | 1906 | largest_node = eb64_lookup(pkts, largest); |
| 1907 | if (!largest_node) { |
| 1908 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1909 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1910 | } |
| 1911 | else { |
| 1912 | time_sent = eb64_entry(&largest_node->node, |
| 1913 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1914 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1918 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1919 | do { |
| 1920 | uint64_t gap, ack_range; |
| 1921 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1922 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1923 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1924 | if (!ack->ack_range_num--) |
| 1925 | break; |
| 1926 | |
| 1927 | if (!quic_dec_int(&gap, pos, end)) |
| 1928 | goto err; |
| 1929 | |
| 1930 | if (smallest < gap + 2) { |
| 1931 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1932 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1933 | goto err; |
| 1934 | } |
| 1935 | |
| 1936 | largest = smallest - gap - 2; |
| 1937 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1938 | goto err; |
| 1939 | |
| 1940 | if (largest < ack_range) { |
| 1941 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1942 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1943 | goto err; |
| 1944 | } |
| 1945 | |
| 1946 | /* Do not use this node anymore. */ |
| 1947 | largest_node = NULL; |
| 1948 | /* Next range */ |
| 1949 | smallest = largest - ack_range; |
| 1950 | |
| 1951 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1952 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1953 | } while (1); |
| 1954 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1955 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1956 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1957 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1961 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1962 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1963 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1964 | 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] | 1965 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1966 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1967 | if (quic_peer_validated_addr(qc)) |
| 1968 | qc->path->loss.pto_count = 0; |
| 1969 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | return 1; |
| 1974 | |
| 1975 | err: |
| 1976 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1977 | 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] | 1978 | return 0; |
| 1979 | } |
| 1980 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1981 | /* This function gives the detail of the SSL error. It is used only |
| 1982 | * if the debug mode and the verbose mode are activated. It dump all |
| 1983 | * the SSL error until the stack was empty. |
| 1984 | */ |
| 1985 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1986 | { |
| 1987 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1988 | while (1) { |
| 1989 | unsigned long ret; |
| 1990 | |
| 1991 | ret = ERR_get_error(); |
| 1992 | if (!ret) |
| 1993 | return; |
| 1994 | |
| 1995 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1996 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2001 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2002 | const char **str, int *len); |
| 2003 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2004 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2005 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2006 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2007 | * Return 1 if succeeded, 0 if not. |
| 2008 | */ |
| 2009 | 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] | 2010 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2011 | const unsigned char *data, size_t len, |
| 2012 | struct quic_rx_packet *pkt, |
| 2013 | struct quic_rx_crypto_frm *cf) |
| 2014 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2015 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2016 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2017 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2018 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2019 | qc = ctx->qc; |
| 2020 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2021 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2022 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2023 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2024 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2025 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2026 | goto err; |
| 2027 | } |
| 2028 | |
| 2029 | el->rx.crypto.offset += len; |
| 2030 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2031 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2032 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2033 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2034 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2035 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2036 | if (ssl_err != 1) { |
| 2037 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2038 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2039 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2040 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2041 | goto out; |
| 2042 | } |
| 2043 | |
| 2044 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2045 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2046 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2047 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2048 | goto err; |
| 2049 | } |
| 2050 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2051 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2052 | /* I/O callback switch */ |
| 2053 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2054 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2055 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2056 | /* The connection is ready to be accepted. */ |
| 2057 | quic_accept_push_qc(qc); |
| 2058 | } |
| 2059 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2060 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2061 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2062 | } else { |
| 2063 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2064 | if (ssl_err != 1) { |
| 2065 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2066 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2067 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2068 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2069 | goto out; |
| 2070 | } |
| 2071 | |
| 2072 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2073 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2074 | goto err; |
| 2075 | } |
| 2076 | |
| 2077 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2078 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2079 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2080 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2081 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2082 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2083 | return 1; |
| 2084 | |
| 2085 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2086 | 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] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2090 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 2091 | * <pkt> RX packet. |
| 2092 | * Return it if succeeded, NULL if not. |
| 2093 | */ |
| 2094 | static inline |
| 2095 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 2096 | struct quic_rx_packet *pkt) |
| 2097 | { |
| 2098 | struct quic_rx_strm_frm *frm; |
| 2099 | |
| 2100 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 2101 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2102 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2103 | frm->len = stream_frm->len; |
| 2104 | frm->data = stream_frm->data; |
| 2105 | frm->pkt = pkt; |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2106 | frm->fin = stream_frm->fin; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | return frm; |
| 2110 | } |
| 2111 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2112 | /* 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] | 2113 | * 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] | 2114 | */ |
| 2115 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 2116 | { |
| 2117 | size_t ret; |
| 2118 | |
Amaury Denoyelle | 2d2d030 | 2022-02-28 10:00:54 +0100 | [diff] [blame] | 2119 | ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len); |
| 2120 | strm_frm->len -= ret; |
| 2121 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2122 | |
| 2123 | return ret; |
| 2124 | } |
| 2125 | |
| 2126 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2127 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2128 | * If not, the STREAM frame is stored to be treated again later. |
| 2129 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2130 | * Return 1 if succeeded, 0 if not. |
| 2131 | */ |
| 2132 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2133 | struct quic_stream *strm_frm, |
| 2134 | struct quic_conn *qc) |
| 2135 | { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2136 | struct quic_rx_strm_frm *frm; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2137 | struct eb64_node *frm_node; |
| 2138 | struct qcs *qcs = NULL; |
| 2139 | int ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2140 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2141 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2142 | strm_frm->offset.key, strm_frm->fin, |
| 2143 | (char *)strm_frm->data, &qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2144 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2145 | /* invalid or already received frame */ |
| 2146 | if (ret == 1) |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2147 | return 1; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2148 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2149 | if (ret == 2) { |
| 2150 | /* frame cannot be parsed at the moment and should be |
| 2151 | * buffered. |
| 2152 | */ |
| 2153 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2154 | if (!frm) { |
| 2155 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2156 | QUIC_EV_CONN_PSTRM, qc); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2157 | return 0; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2160 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
| 2161 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2162 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2163 | return 1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2166 | /* Frame correctly received by the mux. |
| 2167 | * If there is buffered frame for next offset, it may be possible to |
| 2168 | * receive them now. |
| 2169 | */ |
| 2170 | frm_node = eb64_first(&qcs->rx.frms); |
| 2171 | while (frm_node) { |
| 2172 | frm = eb64_entry(&frm_node->node, |
| 2173 | struct quic_rx_strm_frm, offset_node); |
Amaury Denoyelle | 3c43039 | 2022-02-28 11:38:36 +0100 | [diff] [blame] | 2174 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 2175 | ret = qcc_recv(qc->qcc, qcs->id, frm->len, |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2176 | frm->offset_node.key, frm->fin, |
| 2177 | (char *)frm->data, &qcs); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2178 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2179 | /* interrupt the parsing if the frame cannot be handled for the |
| 2180 | * moment only by the MUX. |
| 2181 | */ |
| 2182 | if (ret == 2) |
| 2183 | break; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2184 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2185 | /* Remove a newly received frame or an invalid one. */ |
| 2186 | frm_node = eb64_next(frm_node); |
| 2187 | eb64_delete(&frm->offset_node); |
| 2188 | quic_rx_packet_refdec(frm->pkt); |
| 2189 | pool_free(pool_head_quic_rx_strm_frm, frm); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2190 | } |
| 2191 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 2192 | /* Decode the received data. */ |
Amaury Denoyelle | 20f89ca | 2022-03-08 10:48:35 +0100 | [diff] [blame] | 2193 | qcc_decode_qcs(qc->qcc, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2194 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2195 | return 1; |
| 2196 | } |
| 2197 | |
| 2198 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2199 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2200 | * If not, the STREAM frame is stored to be treated again later. |
| 2201 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2202 | * Return 1 if succeeded, 0 if not. |
| 2203 | */ |
| 2204 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2205 | struct quic_stream *strm_frm, |
| 2206 | struct quic_conn *qc) |
| 2207 | { |
| 2208 | struct qcs *strm; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2209 | struct quic_rx_strm_frm *frm; |
| 2210 | size_t strm_frm_len; |
| 2211 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 2212 | strm = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2213 | if (!strm) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2214 | 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] | 2215 | return 0; |
| 2216 | } |
| 2217 | |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2218 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2219 | size_t diff; |
| 2220 | |
| 2221 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2222 | TRACE_PROTO("Already received STREAM data", |
| 2223 | QUIC_EV_CONN_PSTRM, qc); |
| 2224 | goto out; |
| 2225 | } |
| 2226 | |
| 2227 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2228 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2229 | strm_frm->offset.key = strm->rx.offset; |
| 2230 | strm_frm->len -= diff; |
| 2231 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2235 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2236 | int ret; |
| 2237 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2238 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2239 | goto store_frm; |
| 2240 | |
| 2241 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2242 | * of bytes copied. |
| 2243 | */ |
| 2244 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2245 | /* Inform the application of the arrival of this new stream */ |
| 2246 | 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] | 2247 | 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] | 2248 | return 0; |
| 2249 | } |
| 2250 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2251 | if (ret) |
| 2252 | qcs_notify_recv(strm); |
| 2253 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2254 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2255 | } |
| 2256 | /* Take this frame into an account for the stream flow control */ |
| 2257 | strm->rx.offset += strm_frm_len; |
| 2258 | /* 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] | 2259 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2260 | */ |
| 2261 | if (!strm_frm->len) |
| 2262 | goto out; |
| 2263 | |
| 2264 | store_frm: |
| 2265 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2266 | if (!frm) { |
| 2267 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2268 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2269 | return 0; |
| 2270 | } |
| 2271 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2272 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2273 | quic_rx_packet_refinc(pkt); |
| 2274 | |
| 2275 | out: |
| 2276 | return 1; |
| 2277 | } |
| 2278 | |
| 2279 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2280 | struct quic_stream *strm_frm, |
| 2281 | struct quic_conn *qc) |
| 2282 | { |
| 2283 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2284 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2285 | else |
| 2286 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2287 | } |
| 2288 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2289 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2290 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2291 | struct quic_conn *qc) |
| 2292 | { |
| 2293 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2294 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2295 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2296 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2297 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2298 | pkts = &qel->pktns->tx.pkts; |
| 2299 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2300 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2301 | while (node) { |
| 2302 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2303 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2304 | break; |
| 2305 | node = eb64_next(node); |
| 2306 | } |
| 2307 | |
| 2308 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2309 | return; |
| 2310 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2311 | /* When building a packet from another one, the field which may increase the |
| 2312 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2313 | */ |
| 2314 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2315 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2316 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2320 | 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] | 2321 | } |
| 2322 | |
| 2323 | /* Prepare a fast retransmission during handshake after a client |
| 2324 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2325 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2326 | * and resend them coalescing with others (Handshake here). |
| 2327 | * (Listener only). |
| 2328 | */ |
| 2329 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2330 | { |
| 2331 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2332 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2333 | struct quic_frame *frm, *frmbak; |
| 2334 | |
| 2335 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2336 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2337 | struct quic_enc_level *qel = iqel; |
| 2338 | struct eb_root *pkts; |
| 2339 | struct eb64_node *node; |
| 2340 | struct quic_tx_packet *pkt; |
| 2341 | struct list *tmp = &itmp; |
| 2342 | |
Frédéric Lécaille | 5cfb4ed | 2022-03-30 14:44:49 +0200 | [diff] [blame] | 2343 | /* Do not probe from a packet number space if some probing |
| 2344 | * was already asked. |
| 2345 | */ |
| 2346 | if (qel->pktns->tx.pto_probe) { |
| 2347 | qel = hqel; |
| 2348 | if (qel->pktns->tx.pto_probe) |
| 2349 | return; |
| 2350 | } |
| 2351 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2352 | start: |
| 2353 | pkt = NULL; |
| 2354 | pkts = &qel->pktns->tx.pkts; |
| 2355 | node = eb64_first(pkts); |
| 2356 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2357 | while (node) { |
| 2358 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2359 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2360 | break; |
| 2361 | node = eb64_next(node); |
| 2362 | } |
| 2363 | |
| 2364 | if (!pkt) |
| 2365 | goto end; |
| 2366 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2367 | /* When building a packet from another one, the field which may increase the |
| 2368 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2369 | */ |
| 2370 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2371 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2372 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2373 | goto end; |
| 2374 | } |
| 2375 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2376 | qel->pktns->tx.pto_probe += 1; |
| 2377 | requeue: |
| 2378 | 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] | 2379 | struct quic_frame *dup_frm; |
| 2380 | |
| 2381 | |
| 2382 | dup_frm = pool_alloc(pool_head_quic_frame); |
| 2383 | if (!dup_frm) { |
| 2384 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2385 | break; |
| 2386 | } |
| 2387 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2388 | 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] | 2389 | *dup_frm = *frm; |
| 2390 | LIST_APPEND(tmp, &dup_frm->list); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | if (qel == iqel) { |
| 2394 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2395 | pkt = pkt->next; |
| 2396 | tmp = &htmp; |
| 2397 | hqel->pktns->tx.pto_probe += 1; |
| 2398 | goto requeue; |
| 2399 | } |
| 2400 | |
| 2401 | qel = hqel; |
| 2402 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2403 | goto start; |
| 2404 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2405 | |
| 2406 | end: |
| 2407 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2408 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2409 | } |
| 2410 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2411 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2412 | * as I/O handler context and <qel> as encryption level. |
| 2413 | * Returns 1 if succeeded, 0 if failed. |
| 2414 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2415 | 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] | 2416 | struct quic_enc_level *qel) |
| 2417 | { |
| 2418 | struct quic_frame frm; |
| 2419 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2420 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2421 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2422 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2423 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2424 | /* Skip the AAD */ |
| 2425 | pos = pkt->data + pkt->aad_len; |
| 2426 | end = pkt->data + pkt->len; |
| 2427 | |
| 2428 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2429 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2430 | goto err; |
| 2431 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2432 | 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] | 2433 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2434 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2435 | break; |
| 2436 | case QUIC_FT_PING: |
| 2437 | break; |
| 2438 | case QUIC_FT_ACK: |
| 2439 | { |
| 2440 | unsigned int rtt_sample; |
| 2441 | |
| 2442 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2443 | 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] | 2444 | goto err; |
| 2445 | |
| 2446 | if (rtt_sample) { |
| 2447 | unsigned int ack_delay; |
| 2448 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2449 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2450 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2451 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2452 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2453 | 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] | 2454 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2455 | break; |
| 2456 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2457 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2458 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2459 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2460 | { |
| 2461 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2462 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2463 | 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] | 2464 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2465 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2466 | |
| 2467 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2468 | cfdebug.len = frm.crypto.len; |
| 2469 | TRACE_PROTO("CRYPTO data discarded", |
| 2470 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2471 | break; |
| 2472 | } |
| 2473 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2474 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2475 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2476 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2477 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2478 | |
| 2479 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2480 | cfdebug.len = frm.crypto.len; |
| 2481 | /* Nothing to do */ |
| 2482 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2483 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2484 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2485 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2486 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2487 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2488 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2489 | else { |
| 2490 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2491 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2492 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2493 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2494 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2495 | cfdebug.len = frm.crypto.len; |
| 2496 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2497 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2498 | frm.crypto.len -= diff; |
| 2499 | frm.crypto.data += diff; |
| 2500 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2501 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2502 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2503 | |
| 2504 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2505 | /* 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] | 2506 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2507 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2508 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2509 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2510 | if (!qc_provide_cdata(qel, ctx, |
| 2511 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2512 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2513 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2514 | |
| 2515 | break; |
| 2516 | } |
| 2517 | |
| 2518 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2519 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2520 | if (!cf) { |
| 2521 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2522 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2523 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2524 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2525 | |
| 2526 | cf->offset_node.key = frm.crypto.offset; |
| 2527 | cf->len = frm.crypto.len; |
| 2528 | cf->data = frm.crypto.data; |
| 2529 | cf->pkt = pkt; |
| 2530 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2531 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2532 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2533 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2534 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2535 | { |
| 2536 | struct quic_stream *stream = &frm.stream; |
| 2537 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2538 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2539 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2540 | goto err; |
| 2541 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2542 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2543 | |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2544 | /* At the application layer the connection may have already been closed. */ |
| 2545 | if (qc->mux_state != QC_MUX_READY) |
| 2546 | break; |
| 2547 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2548 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2549 | goto err; |
| 2550 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2551 | break; |
| 2552 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2553 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2554 | if (qc->mux_state == QC_MUX_READY) { |
| 2555 | struct quic_max_data *data = &frm.max_data; |
| 2556 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2557 | } |
| 2558 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2559 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2560 | if (qc->mux_state == QC_MUX_READY) { |
| 2561 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2562 | qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2563 | data->max_stream_data); |
| 2564 | } |
| 2565 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2566 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2567 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2568 | case QUIC_FT_DATA_BLOCKED: |
| 2569 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2570 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2571 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2572 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2573 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2574 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2575 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2576 | break; |
| 2577 | case QUIC_FT_CONNECTION_CLOSE: |
| 2578 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2579 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 2d0f873 | 2022-03-03 18:04:24 +0100 | [diff] [blame] | 2580 | if (qc->mux_state == QC_MUX_READY) { |
Amaury Denoyelle | 4af6595 | 2022-02-15 11:06:15 +0100 | [diff] [blame] | 2581 | qc->qcc->flags |= QC_CF_CC_RECV; |
Amaury Denoyelle | 2d0f873 | 2022-03-03 18:04:24 +0100 | [diff] [blame] | 2582 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
| 2583 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2584 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2585 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2586 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2587 | goto err; |
| 2588 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2589 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2590 | break; |
| 2591 | default: |
| 2592 | goto err; |
| 2593 | } |
| 2594 | } |
| 2595 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2596 | /* 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] | 2597 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2598 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2599 | if (fast_retrans) |
| 2600 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2601 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2602 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2603 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2604 | * be discarded. |
| 2605 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2606 | 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] | 2607 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2608 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2609 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2610 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2611 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2612 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2613 | qc_set_timer(ctx->qc); |
| 2614 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2615 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2616 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2617 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2618 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2619 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2620 | } |
| 2621 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2622 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2623 | return 1; |
| 2624 | |
| 2625 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2626 | 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] | 2627 | return 0; |
| 2628 | } |
| 2629 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2630 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2631 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2632 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2633 | * index after having marked the end of written data. This the responsability |
| 2634 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2635 | * data made of a uint16_t null field. |
| 2636 | * |
| 2637 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2638 | * ^ ^ |
| 2639 | * r w |
| 2640 | * |
| 2641 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2642 | * ^ ^ |
| 2643 | * r w |
| 2644 | |
| 2645 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2646 | * ^ |
| 2647 | * (r = w) |
| 2648 | * |
| 2649 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2650 | * ^ ^ |
| 2651 | * w r |
| 2652 | */ |
| 2653 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2654 | { |
| 2655 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2656 | |
| 2657 | /* We can reset the writer index only if in front of the reader index and |
| 2658 | * if the reader index is not null. Resetting the writer when the reader |
| 2659 | * index is null would empty the buffer. |
| 2660 | * XXX Note than the writer index cannot reach the reader index. |
| 2661 | * Only the reader index can reach the writer index. |
| 2662 | */ |
| 2663 | if (rd && rd <= cbuf->wr) { |
| 2664 | /* Mark the end of contiguous data for the reader */ |
| 2665 | write_u16(cb_wr(cbuf), 0); |
| 2666 | cb_add(cbuf, sizeof(uint16_t)); |
| 2667 | cb_wr_reset(cbuf); |
| 2668 | return 1; |
| 2669 | } |
| 2670 | |
| 2671 | return 0; |
| 2672 | } |
| 2673 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2674 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2675 | * 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] | 2676 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2677 | * This function must be called only after having built a correct datagram. |
| 2678 | * Always succeeds. |
| 2679 | */ |
| 2680 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2681 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2682 | { |
| 2683 | write_u16(cb_wr(cbuf), dglen); |
| 2684 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2685 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2686 | } |
| 2687 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2688 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2689 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2690 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2691 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 2692 | */ |
| 2693 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 2694 | struct quic_enc_level *qel, int cc, int probe) |
| 2695 | { |
| 2696 | unsigned int must_ack = |
| 2697 | qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK; |
| 2698 | |
| 2699 | /* Do not build any more packet if the TX secrets are not available or |
| 2700 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2701 | * and if there is no more packets to send upon PTO expiration |
| 2702 | * and if there is no more ack-eliciting frames to send or in flight |
| 2703 | * congestion control limit is reached for prepared data |
| 2704 | */ |
| 2705 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2706 | (!cc && !probe && !must_ack && |
| 2707 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2708 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | return 1; |
| 2713 | } |
| 2714 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2715 | /* 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] | 2716 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2717 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2718 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2719 | * 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] | 2720 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2721 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2722 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2723 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2724 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2725 | { |
| 2726 | struct quic_enc_level *qel; |
| 2727 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2728 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2729 | struct quic_tx_packet *pkt; |
| 2730 | size_t total; |
| 2731 | size_t dg_headlen; |
| 2732 | |
| 2733 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2734 | /* Each datagram is prepended with its length followed by the |
| 2735 | * address of the first packet in the datagram. |
| 2736 | */ |
| 2737 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2738 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2739 | total = 0; |
| 2740 | start: |
| 2741 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2742 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2743 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2744 | * to ensure there is enough room to mark the end of prepared |
| 2745 | * contiguous data with a zero length. |
| 2746 | */ |
| 2747 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2748 | 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] | 2749 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2750 | |
| 2751 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2752 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2753 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2754 | /* We do not probe if an immediate close was asked */ |
| 2755 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2756 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2757 | |
| 2758 | 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] | 2759 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2760 | |
| 2761 | /* Leave room for the datagram header */ |
| 2762 | pos += dg_headlen; |
| 2763 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2764 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2765 | } |
| 2766 | else { |
| 2767 | end = pos + qc->path->mtu; |
| 2768 | } |
| 2769 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2770 | 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] | 2771 | QUIC_PACKET_TYPE_SHORT, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2772 | switch (err) { |
| 2773 | case -2: |
| 2774 | goto err; |
| 2775 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2776 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2777 | * MTU, we are here because of the congestion control window. There is |
| 2778 | * no need to try to reuse this buffer. |
| 2779 | */ |
| 2780 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2781 | default: |
| 2782 | break; |
| 2783 | } |
| 2784 | |
| 2785 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2786 | if (!pkt) |
| 2787 | goto err; |
| 2788 | |
| 2789 | total += pkt->len; |
| 2790 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2791 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2792 | } |
| 2793 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2794 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2795 | 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] | 2796 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2797 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2798 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | out: |
| 2802 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2803 | return total; |
| 2804 | |
| 2805 | err: |
| 2806 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2807 | return -1; |
| 2808 | } |
| 2809 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2810 | /* 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] | 2811 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2812 | * several packets in the same datagram. A header made of two fields is added |
| 2813 | * to each datagram: the datagram length followed by the address of the first |
| 2814 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2815 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2816 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2817 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2818 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2819 | enum quic_tls_enc_level tel, |
| 2820 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2821 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2822 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2823 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2824 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2825 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2826 | /* length of datagrams */ |
| 2827 | uint16_t dglen; |
| 2828 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2829 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2830 | /* Each datagram is prepended with its length followed by the |
| 2831 | * address of the first packet in the datagram. |
| 2832 | */ |
| 2833 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2834 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2835 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2836 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2837 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2838 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2839 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2840 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2841 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2842 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2843 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2844 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2845 | * to ensure there is enough room to mark the end of prepared |
| 2846 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2847 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2848 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2849 | first_pkt = prv_pkt = NULL; |
| 2850 | 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] | 2851 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2852 | enum quic_pkt_type pkt_type; |
| 2853 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2854 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2855 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2856 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2857 | /* We do not probe if an immediate close was asked */ |
| 2858 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2859 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2860 | |
| 2861 | 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] | 2862 | if (prv_pkt) |
| 2863 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2864 | /* Let's select the next encryption level */ |
| 2865 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2866 | tel = next_tel; |
| 2867 | qel = &qc->els[tel]; |
| 2868 | /* Build a new datagram */ |
| 2869 | prv_pkt = NULL; |
| 2870 | continue; |
| 2871 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2872 | break; |
| 2873 | } |
| 2874 | |
| 2875 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2876 | if (!prv_pkt) { |
| 2877 | /* Leave room for the datagram header */ |
| 2878 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2879 | 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] | 2880 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2881 | } |
| 2882 | else { |
| 2883 | end = pos + qc->path->mtu; |
| 2884 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2885 | } |
| 2886 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2887 | 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] | 2888 | qc, dglen, padding, pkt_type, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2889 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2890 | case -2: |
| 2891 | goto err; |
| 2892 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2893 | /* If there was already a correct packet present, set the |
| 2894 | * current datagram as prepared into <cbuf>. |
| 2895 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2896 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2897 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2898 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2899 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2902 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2903 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2904 | if (!cur_pkt) |
| 2905 | goto err; |
| 2906 | |
| 2907 | total += cur_pkt->len; |
| 2908 | /* keep trace of the first packet in the datagram */ |
| 2909 | if (!first_pkt) |
| 2910 | first_pkt = cur_pkt; |
| 2911 | /* Attach the current one to the previous one */ |
| 2912 | if (prv_pkt) |
| 2913 | prv_pkt->next = cur_pkt; |
| 2914 | /* Let's say we have to build a new dgram */ |
| 2915 | prv_pkt = NULL; |
| 2916 | dglen += cur_pkt->len; |
| 2917 | /* Client: discard the Initial encryption keys as soon as |
| 2918 | * a handshake packet could be built. |
| 2919 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2920 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2921 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2922 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2923 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2924 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2925 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2926 | 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] | 2927 | 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] | 2928 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2929 | } |
| 2930 | /* If the data for the current encryption level have all been sent, |
| 2931 | * select the next level. |
| 2932 | */ |
| 2933 | 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] | 2934 | (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] | 2935 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2936 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2937 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2938 | tel = next_tel; |
| 2939 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2940 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2941 | /* If there is data for the next level, do not |
| 2942 | * consume a datagram. |
| 2943 | */ |
| 2944 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2945 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2946 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2947 | /* If we have to build a new datagram, set the current datagram as |
| 2948 | * prepared into <cbuf>. |
| 2949 | */ |
| 2950 | if (!prv_pkt) { |
| 2951 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2952 | first_pkt = NULL; |
| 2953 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2954 | padding = 0; |
| 2955 | } |
| 2956 | 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] | 2957 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2958 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2959 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | stop_build: |
| 2964 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2965 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2966 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2967 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2968 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2972 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2973 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2974 | |
| 2975 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2976 | 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] | 2977 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | /* 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] | 2981 | * 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] | 2982 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2983 | 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] | 2984 | { |
| 2985 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2986 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2987 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2988 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2989 | cbuf = qr->cbuf; |
| 2990 | while (cb_contig_data(cbuf)) { |
| 2991 | unsigned char *pos; |
| 2992 | struct buffer tmpbuf = { }; |
| 2993 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2994 | uint16_t dglen; |
| 2995 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2996 | unsigned int time_sent; |
| 2997 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2998 | pos = cb_rd(cbuf); |
| 2999 | dglen = read_u16(pos); |
| 3000 | /* End of prepared datagrams. |
| 3001 | * Reset the reader index only if in front of the writer index. |
| 3002 | */ |
| 3003 | if (!dglen) { |
| 3004 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3005 | |
| 3006 | if (wr && wr < cbuf->rd) { |
| 3007 | cb_rd_reset(cbuf); |
| 3008 | continue; |
| 3009 | } |
| 3010 | break; |
| 3011 | } |
| 3012 | |
| 3013 | pos += sizeof dglen; |
| 3014 | first_pkt = read_ptr(pos); |
| 3015 | pos += sizeof first_pkt; |
| 3016 | tmpbuf.area = (char *)pos; |
| 3017 | tmpbuf.size = tmpbuf.data = dglen; |
| 3018 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3019 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3020 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3021 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3022 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3023 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | qc->tx.bytes += tmpbuf.data; |
| 3025 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3026 | |
| 3027 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3028 | pkt->time_sent = time_sent; |
| 3029 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3030 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3031 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3032 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3033 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3034 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3035 | qc->path->in_flight += pkt->in_flight_len; |
| 3036 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3037 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3038 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3039 | 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] | 3040 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3041 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3042 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | } |
| 3044 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3045 | |
| 3046 | return 1; |
| 3047 | } |
| 3048 | |
| 3049 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3050 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3051 | * a HANDSHAKE_DONE frame. |
| 3052 | * Return 1 if succeeded, 0 if not. |
| 3053 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3054 | 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] | 3055 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3056 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3057 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3058 | struct quic_frame *frm, *frmbak; |
| 3059 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3060 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3061 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3062 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3063 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3064 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3065 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3066 | if (!frm) |
| 3067 | return 0; |
| 3068 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3069 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3070 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3071 | } |
| 3072 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3073 | first = 1; |
| 3074 | max = qc->tx.params.active_connection_id_limit; |
| 3075 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3076 | struct quic_connection_id *cid; |
| 3077 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3078 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3079 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3080 | goto err; |
| 3081 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3082 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3083 | if (!cid) |
| 3084 | goto err; |
| 3085 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3086 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3087 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3088 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3089 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3090 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3091 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3092 | |
| 3093 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3094 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3095 | |
| 3096 | return 1; |
| 3097 | |
| 3098 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3099 | /* free the frames */ |
| 3100 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3101 | pool_free(pool_head_quic_frame, frm); |
| 3102 | |
| 3103 | node = eb64_first(&qc->cids); |
| 3104 | while (node) { |
| 3105 | struct quic_connection_id *cid; |
| 3106 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3107 | |
| 3108 | 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] | 3109 | if (cid->seq_num.key >= max) |
| 3110 | break; |
| 3111 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3112 | if (cid->seq_num.key < first) |
| 3113 | continue; |
| 3114 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3115 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3116 | ebmb_delete(&cid->node); |
| 3117 | eb64_delete(&cid->seq_num); |
| 3118 | pool_free(pool_head_quic_connection_id, cid); |
| 3119 | } |
| 3120 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3121 | return 0; |
| 3122 | } |
| 3123 | |
| 3124 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3125 | void quic_free_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3126 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3127 | struct eb64_node *n; |
| 3128 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3129 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3130 | n = eb64_first(&arngs->root); |
| 3131 | while (n) { |
| 3132 | struct eb64_node *next; |
| 3133 | |
| 3134 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 3135 | next = eb64_next(n); |
| 3136 | eb64_delete(n); |
| 3137 | free(ar); |
| 3138 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3142 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3143 | * descending order. |
| 3144 | */ |
| 3145 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3146 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3148 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3153 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3154 | * 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] | 3155 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3156 | 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] | 3157 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3158 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3159 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3160 | last = eb64_last(&arngs->root); |
| 3161 | while (last && arngs->enc_sz > limit) { |
| 3162 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3163 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3164 | prev = eb64_prev(last); |
| 3165 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3166 | return 0; |
| 3167 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3168 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 3169 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 3170 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3171 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3172 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3173 | --arngs->sz; |
| 3174 | eb64_delete(last); |
| 3175 | pool_free(pool_head_quic_arng, last); |
| 3176 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | return 1; |
| 3180 | } |
| 3181 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3182 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3183 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3184 | { |
| 3185 | struct eb64_node *node, *next; |
| 3186 | struct quic_arng_node *ar, *ar_next; |
| 3187 | |
| 3188 | node = eb64_last(&arngs->root); |
| 3189 | if (!node) |
| 3190 | return; |
| 3191 | |
| 3192 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3193 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3194 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3195 | |
| 3196 | while ((next = eb64_prev(node))) { |
| 3197 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 3198 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3199 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3200 | node = next; |
| 3201 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 3202 | } |
| 3203 | } |
| 3204 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3205 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3206 | * 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] | 3207 | */ |
| 3208 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3209 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3210 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3211 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3212 | struct quic_arng_node *new_ar; |
| 3213 | |
| 3214 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3215 | if (new_ar) { |
| 3216 | new_ar->first.key = ar->first; |
| 3217 | new_ar->last = ar->last; |
| 3218 | eb64_insert(&arngs->root, &new_ar->first); |
| 3219 | arngs->sz++; |
| 3220 | } |
| 3221 | |
| 3222 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /* 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] | 3226 | * 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] | 3227 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3228 | * |
| 3229 | * Descending order |
| 3230 | * -------------> |
| 3231 | * range1 range2 |
| 3232 | * ..........|--------|..............|--------| |
| 3233 | * ^ ^ ^ ^ |
| 3234 | * | | | | |
| 3235 | * last1 first1 last2 first2 |
| 3236 | * ..........+--------+--------------+--------+...... |
| 3237 | * diff1 gap12 diff2 |
| 3238 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3239 | * To encode the previous list of ranges we must encode integers as follows in |
| 3240 | * descending order: |
| 3241 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3242 | * with diff1 = last1 - first1 |
| 3243 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3244 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3245 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3246 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3247 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3248 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3249 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3250 | struct eb64_node *le; |
| 3251 | struct quic_arng_node *new_node; |
| 3252 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3253 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3254 | new = NULL; |
| 3255 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3256 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3257 | if (!new_node) |
| 3258 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3259 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3260 | goto out; |
| 3261 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3262 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3263 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3264 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3265 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3266 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3267 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3268 | |
| 3269 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3270 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3271 | else { |
| 3272 | struct quic_arng_node *le_ar = |
| 3273 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3274 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3275 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3276 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3277 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3278 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3279 | if (le_ar->last + 1 >= ar->first) { |
| 3280 | le_ar->last = ar->last; |
| 3281 | new = le; |
| 3282 | new_node = le_ar; |
| 3283 | } |
| 3284 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3285 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3286 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3287 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3288 | |
| 3289 | new = &new_node->first; |
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 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3292 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3293 | /* Verify that the new inserted node does not overlap the nodes |
| 3294 | * which follow it. |
| 3295 | */ |
| 3296 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3297 | struct eb64_node *next; |
| 3298 | struct quic_arng_node *next_node; |
| 3299 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3300 | while ((next = eb64_next(new))) { |
| 3301 | next_node = |
| 3302 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3303 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3304 | break; |
| 3305 | |
| 3306 | if (next_node->last > new_node->last) |
| 3307 | new_node->last = next_node->last; |
| 3308 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3309 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3310 | /* Decrement the size of these ranges. */ |
| 3311 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3312 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3313 | } |
| 3314 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3315 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3316 | quic_arngs_set_enc_sz(arngs); |
| 3317 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3318 | return 1; |
| 3319 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3320 | /* Remove the header protection of packets at <el> encryption level. |
| 3321 | * Always succeeds. |
| 3322 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3323 | 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] | 3324 | { |
| 3325 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3326 | struct quic_rx_packet *pqpkt; |
| 3327 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3328 | struct quic_enc_level *app_qel; |
| 3329 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3330 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3331 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3332 | /* 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] | 3333 | 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] | 3334 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3335 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3336 | goto out; |
| 3337 | } |
| 3338 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3339 | 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] | 3340 | 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] | 3341 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3342 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3343 | 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] | 3344 | /* XXX TO DO XXX */ |
| 3345 | } |
| 3346 | else { |
| 3347 | /* The AAD includes the packet number field */ |
| 3348 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3349 | /* Store the packet into the tree of packets to decrypt. */ |
| 3350 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3351 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3352 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3353 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3354 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3355 | 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] | 3356 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3357 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3358 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3362 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3366 | * Return 1 if succeeded, 0 if not. |
| 3367 | */ |
| 3368 | 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] | 3369 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3370 | { |
| 3371 | struct eb64_node *node; |
| 3372 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3373 | node = eb64_first(&el->rx.crypto.frms); |
| 3374 | while (node) { |
| 3375 | struct quic_rx_crypto_frm *cf; |
| 3376 | |
| 3377 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3378 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3379 | break; |
| 3380 | |
| 3381 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3382 | goto err; |
| 3383 | |
| 3384 | node = eb64_next(node); |
| 3385 | quic_rx_packet_refdec(cf->pkt); |
| 3386 | eb64_delete(&cf->offset_node); |
| 3387 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3388 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3389 | return 1; |
| 3390 | |
| 3391 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3392 | 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] | 3393 | return 0; |
| 3394 | } |
| 3395 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3396 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3397 | * 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] | 3398 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3399 | * Return 1 if succeeded, 0 if not. |
| 3400 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3401 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3402 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3403 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3404 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3405 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3406 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3407 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3408 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3409 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3410 | qel = cur_el; |
| 3411 | next_tel: |
| 3412 | if (!qel) |
| 3413 | goto out; |
| 3414 | |
| 3415 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3416 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3417 | while (node) { |
| 3418 | struct quic_rx_packet *pkt; |
| 3419 | |
| 3420 | 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] | 3421 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3422 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3423 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3424 | /* Drop the packet */ |
| 3425 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3426 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3427 | } |
| 3428 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3429 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | /* Drop the packet */ |
| 3431 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3432 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3433 | } |
| 3434 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3435 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3436 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3437 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3438 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3439 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3440 | qc_idle_timer_rearm(qc, 1); |
| 3441 | } |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3442 | if (pkt->pn > largest_pn) |
| 3443 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3444 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3445 | 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] | 3446 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3447 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3451 | eb64_delete(&pkt->pn_node); |
| 3452 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3453 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3454 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3455 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3456 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3457 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) |
| 3458 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3459 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3460 | goto err; |
| 3461 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3462 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3463 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3464 | qel = next_el; |
| 3465 | goto next_tel; |
| 3466 | } |
| 3467 | |
| 3468 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3469 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | return 1; |
| 3471 | |
| 3472 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3473 | 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] | 3474 | return 0; |
| 3475 | } |
| 3476 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3477 | /* Check if it's possible to remove header protection for packets related to |
| 3478 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3479 | * |
| 3480 | * Return true if the operation is possible else false. |
| 3481 | */ |
| 3482 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3483 | { |
| 3484 | enum quic_tls_enc_level tel; |
| 3485 | |
| 3486 | if (!qel) |
| 3487 | return 0; |
| 3488 | |
| 3489 | tel = ssl_to_quic_enc_level(qel->level); |
| 3490 | |
| 3491 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3492 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3493 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3494 | return 0; |
| 3495 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3496 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3497 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3498 | return 0; |
| 3499 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3500 | /* 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] | 3501 | 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] | 3502 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3503 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3504 | |
| 3505 | return 1; |
| 3506 | } |
| 3507 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3508 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | c2f561c | 2022-02-25 17:46:07 +0100 | [diff] [blame] | 3509 | 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] | 3510 | { |
| 3511 | int ret; |
| 3512 | struct qring *qr; |
| 3513 | |
| 3514 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3515 | if (!qr) |
| 3516 | /* Never happens */ |
| 3517 | return 1; |
| 3518 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3519 | ret = qc_prep_app_pkts(qc, qr, frms); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3520 | if (ret == -1) |
| 3521 | goto err; |
| 3522 | else if (ret == 0) |
| 3523 | goto out; |
| 3524 | |
| 3525 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3526 | goto err; |
| 3527 | |
| 3528 | out: |
| 3529 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3530 | return 1; |
| 3531 | |
| 3532 | err: |
| 3533 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3534 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | /* QUIC connection packet handler task (post handshake) */ |
| 3539 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3540 | { |
| 3541 | struct ssl_sock_ctx *ctx; |
| 3542 | struct quic_conn *qc; |
| 3543 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3544 | |
| 3545 | |
| 3546 | ctx = context; |
| 3547 | qc = ctx->qc; |
| 3548 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3549 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3550 | 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] | 3551 | |
| 3552 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3553 | qc_rm_hp_pkts(qc, qel); |
| 3554 | |
| 3555 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3556 | goto err; |
| 3557 | |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 3558 | 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] | 3559 | goto err; |
| 3560 | |
| 3561 | return t; |
| 3562 | |
| 3563 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3564 | 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] | 3565 | return t; |
| 3566 | } |
| 3567 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3568 | /* QUIC connection packet handler task. */ |
| 3569 | 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] | 3570 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3571 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3572 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3573 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3574 | enum quic_tls_enc_level tel, next_tel; |
| 3575 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3576 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3577 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3578 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3579 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3580 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3581 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3582 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3583 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3584 | 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] | 3585 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3586 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3587 | /* The I/O handler has been woken up by the dgram listener |
| 3588 | * after the anti-amplification was reached. |
| 3589 | */ |
| 3590 | qc_set_timer(qc); |
| 3591 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3592 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3593 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3594 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3595 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3596 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3597 | 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] | 3598 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3599 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3600 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3601 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3602 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3603 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3604 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3605 | } |
| 3606 | 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] | 3607 | goto err; |
| 3608 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3609 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3610 | 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] | 3611 | |
| 3612 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3613 | /* Treat packets waiting for header packet protection decryption */ |
| 3614 | 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] | 3615 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3616 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3617 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3618 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3619 | 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] | 3620 | goto err; |
| 3621 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3622 | 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] | 3623 | (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] | 3624 | qel = next_qel; |
| 3625 | next_qel = NULL; |
| 3626 | goto next_level; |
| 3627 | } |
| 3628 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3629 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3630 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3631 | 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] | 3632 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3633 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3634 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3635 | 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] | 3636 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3637 | /* Discard the Handshake keys. */ |
| 3638 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3639 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3640 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3641 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3642 | 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] | 3643 | 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] | 3644 | } |
| 3645 | |
| 3646 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3647 | /* There may be remaining handshake to build (acks) */ |
| 3648 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3649 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3650 | } |
| 3651 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3652 | if (!qr) |
| 3653 | 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] | 3654 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3655 | * be considered. |
| 3656 | */ |
| 3657 | 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] | 3658 | goto err; |
| 3659 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3660 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3661 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3662 | else if (ret == 0) |
| 3663 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3664 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3665 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3666 | goto err; |
| 3667 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3668 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3669 | /* Check if there is something to do for the next level. |
| 3670 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3671 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3672 | (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] | 3673 | (!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] | 3674 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3675 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3676 | goto next_level; |
| 3677 | } |
| 3678 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3679 | 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] | 3680 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3681 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3682 | |
| 3683 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3684 | if (qr) |
| 3685 | 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] | 3686 | 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] | 3687 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3688 | } |
| 3689 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3690 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3691 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3692 | { |
| 3693 | int i; |
| 3694 | |
| 3695 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3696 | if (qel->tx.crypto.bufs[i]) { |
| 3697 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3698 | qel->tx.crypto.bufs[i] = NULL; |
| 3699 | } |
| 3700 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3701 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3705 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3706 | * Returns 1 if succeeded, 0 if not. |
| 3707 | */ |
| 3708 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3709 | enum quic_tls_enc_level level) |
| 3710 | { |
| 3711 | struct quic_enc_level *qel; |
| 3712 | |
| 3713 | qel = &qc->els[level]; |
| 3714 | qel->level = quic_to_ssl_enc_level(level); |
| 3715 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3716 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3717 | 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] | 3718 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3719 | |
| 3720 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3721 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3722 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3723 | qel->rx.crypto.offset = 0; |
| 3724 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3725 | |
| 3726 | /* Allocate only one buffer. */ |
| 3727 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3728 | if (!qel->tx.crypto.bufs) |
| 3729 | goto err; |
| 3730 | |
| 3731 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3732 | if (!qel->tx.crypto.bufs[0]) |
| 3733 | goto err; |
| 3734 | |
| 3735 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3736 | qel->tx.crypto.nb_buf = 1; |
| 3737 | |
| 3738 | qel->tx.crypto.sz = 0; |
| 3739 | qel->tx.crypto.offset = 0; |
| 3740 | |
| 3741 | return 1; |
| 3742 | |
| 3743 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3744 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3745 | return 0; |
| 3746 | } |
| 3747 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3748 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 3749 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3750 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3751 | * This function must only be called by the thread responsible of the quic_conn |
| 3752 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3753 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3754 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3755 | { |
| 3756 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3757 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3758 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3759 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 3760 | |
| 3761 | /* free remaining stream descriptors */ |
| 3762 | node = eb64_first(&qc->streams_by_id); |
| 3763 | while (node) { |
| 3764 | struct qc_stream_desc *stream; |
| 3765 | |
| 3766 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 3767 | node = eb64_next(node); |
| 3768 | |
| 3769 | eb64_delete(&stream->by_id); |
| 3770 | pool_free(pool_head_quic_conn_stream, stream); |
| 3771 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3772 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3773 | if (qc->idle_timer_task) { |
| 3774 | task_destroy(qc->idle_timer_task); |
| 3775 | qc->idle_timer_task = NULL; |
| 3776 | } |
| 3777 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3778 | if (qc->timer_task) { |
| 3779 | task_destroy(qc->timer_task); |
| 3780 | qc->timer_task = NULL; |
| 3781 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3782 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3783 | /* remove the connection from receiver cids trees */ |
| 3784 | ebmb_delete(&qc->odcid_node); |
| 3785 | ebmb_delete(&qc->scid_node); |
| 3786 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3787 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3788 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3789 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 3790 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3791 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3792 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3793 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3794 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3795 | quic_tls_ku_free(qc); |
| 3796 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3797 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3798 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 3802 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 3803 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3804 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3805 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 3806 | quic_pktns_tx_pkts_release(&qc->pktns[i]); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3807 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 3808 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3809 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3810 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3811 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3812 | 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] | 3813 | } |
| 3814 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3815 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3816 | { |
| 3817 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3818 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3819 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3820 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3821 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3822 | /* Next application data can be dropped. */ |
| 3823 | qc->mux_state = QC_MUX_RELEASED; |
| 3824 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3825 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3826 | } |
| 3827 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3828 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3829 | 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] | 3830 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3831 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3832 | struct quic_conn *qc; |
| 3833 | struct quic_pktns *pktns; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3834 | int i; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3835 | |
| 3836 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3837 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3838 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3839 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3840 | task->expire = TICK_ETERNITY; |
| 3841 | pktns = quic_loss_pktns(qc); |
| 3842 | if (tick_isset(pktns->tx.loss_time)) { |
| 3843 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3844 | |
| 3845 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3846 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3847 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3848 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3849 | goto out; |
| 3850 | } |
| 3851 | |
| 3852 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3853 | 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] | 3854 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3855 | pktns->tx.pto_probe = 1; |
| 3856 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3857 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3858 | } |
| 3859 | else { |
| 3860 | pktns->tx.pto_probe = 2; |
| 3861 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3862 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3863 | 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] | 3864 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3865 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3866 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3867 | 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] | 3868 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3869 | 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] | 3870 | iel->pktns->tx.pto_probe = 1; |
| 3871 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3872 | |
| 3873 | 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] | 3874 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3875 | continue; |
| 3876 | |
Frédéric Lécaille | 53c7d8d | 2022-02-15 12:00:55 +0100 | [diff] [blame] | 3877 | qc_prep_fast_retrans(&qc->els[i], qc); |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3878 | } |
| 3879 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3880 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3881 | qc->path->loss.pto_count++; |
| 3882 | |
| 3883 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3884 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3885 | |
| 3886 | return task; |
| 3887 | } |
| 3888 | |
| 3889 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3890 | * connections used to identify the first Initial packets of client connecting |
| 3891 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3892 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3893 | * <scid> is the source connection ID with <scid_len> as length. |
| 3894 | * Returns 1 if succeeded, 0 if not. |
| 3895 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3896 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3897 | 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] | 3898 | 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] | 3899 | { |
| 3900 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3901 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3902 | /* Initial CID. */ |
| 3903 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3904 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3905 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3906 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3907 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3908 | qc = pool_zalloc(pool_head_quic_conn); |
| 3909 | if (!qc) { |
| 3910 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3911 | goto err; |
| 3912 | } |
| 3913 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3914 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3915 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3916 | 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] | 3917 | goto err; |
| 3918 | } |
| 3919 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3920 | qc->cids = EB_ROOT; |
| 3921 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3922 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3923 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3924 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3925 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3926 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3927 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3928 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3929 | qc->odcid.addrlen = dcid_addr_len; |
| 3930 | 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] | 3931 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3932 | /* 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] | 3933 | if (scid_len) |
| 3934 | memcpy(qc->dcid.data, scid, scid_len); |
| 3935 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3936 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3937 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3938 | } |
| 3939 | /* QUIC Client (outgoing connection to servers) */ |
| 3940 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3941 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3942 | if (dcid_len) |
| 3943 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3944 | qc->dcid.len = dcid_len; |
| 3945 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3946 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3947 | |
| 3948 | /* Initialize the output buffer */ |
| 3949 | qc->obuf.pos = qc->obuf.data; |
| 3950 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3951 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3952 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3953 | 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] | 3954 | goto err; |
| 3955 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3956 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3957 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3958 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3959 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3960 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3961 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3962 | qc->scid = icid->cid; |
| 3963 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3964 | /* Packet number spaces initialization. */ |
| 3965 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3966 | quic_pktns_init(&qc->pktns[i]); |
| 3967 | /* QUIC encryption level context initialization. */ |
| 3968 | 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] | 3969 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3970 | 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] | 3971 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3972 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3973 | /* Initialize the packet number space. */ |
| 3974 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3975 | } |
| 3976 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3977 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3978 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3979 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3980 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3981 | /* TX part. */ |
| 3982 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3983 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3984 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3985 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3986 | /* RX part. */ |
| 3987 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3988 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3989 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3990 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3991 | 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] | 3992 | goto err; |
| 3993 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3994 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3995 | /* XXX TO DO: Only one path at this time. */ |
| 3996 | qc->path = &qc->paths[0]; |
| 3997 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3998 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 3999 | /* required to use MTLIST_IN_LIST */ |
| 4000 | MT_LIST_INIT(&qc->accept_list); |
| 4001 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4002 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 4003 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4004 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4005 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4006 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4007 | |
| 4008 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4009 | 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] | 4010 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4011 | if (qc) |
| 4012 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4013 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4014 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | /* Initialize the timer task of <qc> QUIC connection. |
| 4018 | * Returns 1 if succeeded, 0 if not. |
| 4019 | */ |
| 4020 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4021 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4022 | /* Attach this task to the same thread ID used for the connection */ |
| 4023 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4024 | if (!qc->timer_task) |
| 4025 | return 0; |
| 4026 | |
| 4027 | qc->timer = TICK_ETERNITY; |
| 4028 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4029 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4030 | |
| 4031 | return 1; |
| 4032 | } |
| 4033 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4034 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4035 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4036 | */ |
| 4037 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4038 | { |
| 4039 | unsigned int expire; |
| 4040 | |
| 4041 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4042 | if (read) { |
| 4043 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4044 | } |
| 4045 | else { |
| 4046 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4047 | } |
| 4048 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4049 | } |
| 4050 | |
| 4051 | /* The task handling the idle timeout */ |
| 4052 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4053 | { |
| 4054 | struct quic_conn *qc = ctx; |
| 4055 | |
| 4056 | quic_conn_release(qc); |
| 4057 | |
| 4058 | return NULL; |
| 4059 | } |
| 4060 | |
| 4061 | /* Initialize the idle timeout task for <qc>. |
| 4062 | * Returns 1 if succeeded, 0 if not. |
| 4063 | */ |
| 4064 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4065 | { |
| 4066 | qc->idle_timer_task = task_new_here(); |
| 4067 | if (!qc->idle_timer_task) |
| 4068 | return 0; |
| 4069 | |
| 4070 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4071 | qc->idle_timer_task->context = qc; |
| 4072 | qc_idle_timer_rearm(qc, 1); |
| 4073 | task_queue(qc->idle_timer_task); |
| 4074 | |
| 4075 | return 1; |
| 4076 | } |
| 4077 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4078 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4079 | * past one byte of this buffer. |
| 4080 | */ |
| 4081 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4082 | struct quic_rx_packet *pkt) |
| 4083 | { |
| 4084 | unsigned char dcid_len, scid_len; |
| 4085 | |
| 4086 | /* Version */ |
| 4087 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 4088 | return 0; |
| 4089 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4090 | /* Destination Connection ID Length */ |
| 4091 | dcid_len = *(*buf)++; |
| 4092 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4093 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4094 | /* XXX MUST BE DROPPED */ |
| 4095 | return 0; |
| 4096 | |
| 4097 | if (dcid_len) { |
| 4098 | /* Check that the length of this received DCID matches the CID lengths |
| 4099 | * of our implementation for non Initials packets only. |
| 4100 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4101 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4102 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4103 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4104 | return 0; |
| 4105 | |
| 4106 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4107 | } |
| 4108 | |
| 4109 | pkt->dcid.len = dcid_len; |
| 4110 | *buf += dcid_len; |
| 4111 | |
| 4112 | /* Source Connection ID Length */ |
| 4113 | scid_len = *(*buf)++; |
| 4114 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4115 | /* XXX MUST BE DROPPED */ |
| 4116 | return 0; |
| 4117 | |
| 4118 | if (scid_len) |
| 4119 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4120 | pkt->scid.len = scid_len; |
| 4121 | *buf += scid_len; |
| 4122 | |
| 4123 | return 1; |
| 4124 | } |
| 4125 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4126 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4127 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4128 | { |
| 4129 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4130 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4131 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4132 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4133 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | /* 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] | 4137 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4138 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4139 | * the packet first byte. |
| 4140 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4141 | * Returns 1 if succeeded, 0 if not. |
| 4142 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4143 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4144 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4145 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4146 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4147 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4148 | { |
| 4149 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4150 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4151 | struct quic_enc_level *qel; |
| 4152 | /* Only for traces. */ |
| 4153 | struct quic_rx_packet *qpkt_trace; |
| 4154 | |
| 4155 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4156 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4157 | /* The packet number is here. This is also the start minus |
| 4158 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4159 | * protection. |
| 4160 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4161 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4162 | |
| 4163 | tel = quic_packet_type_enc_level(pkt->type); |
| 4164 | qel = &qc->els[tel]; |
| 4165 | |
| 4166 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4167 | /* Note that the following function enables us to unprotect the packet |
| 4168 | * number and its length subsequently used to decrypt the entire |
| 4169 | * packets. |
| 4170 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4171 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4172 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4173 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4174 | goto err; |
| 4175 | } |
| 4176 | |
| 4177 | /* The AAD includes the packet number field found at <pn>. */ |
| 4178 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4179 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4180 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4181 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4182 | 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] | 4183 | /* If the packet number space has been discarded, this packet |
| 4184 | * will be not parsed. |
| 4185 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4186 | 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] | 4187 | goto out; |
| 4188 | } |
| 4189 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4190 | 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] | 4191 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4192 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4193 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4196 | *el = qel; |
| 4197 | /* No reference counter incrementation here!!! */ |
| 4198 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4199 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4200 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4201 | b_add(&qc->rx.buf, pkt->len); |
| 4202 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4203 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4204 | return 1; |
| 4205 | |
| 4206 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4207 | 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] | 4208 | return 0; |
| 4209 | } |
| 4210 | |
| 4211 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 4212 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 4213 | */ |
| 4214 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4215 | unsigned char byte0, int *long_header) |
| 4216 | { |
| 4217 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 4218 | pkt->type = |
| 4219 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 4220 | *long_header = 1; |
| 4221 | } |
| 4222 | else { |
| 4223 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4224 | *long_header = 0; |
| 4225 | } |
| 4226 | } |
| 4227 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4228 | /* |
| 4229 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 4230 | */ |
| 4231 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 4232 | { |
| 4233 | int j = 0, version; |
| 4234 | |
| 4235 | do { |
| 4236 | version = quic_supported_version[j]; |
| 4237 | if (version == pkt->version) |
| 4238 | return 1; |
| 4239 | |
| 4240 | version = quic_supported_version[++j]; |
| 4241 | } while(version); |
| 4242 | |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4246 | /* |
| 4247 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4248 | * address <addr>. |
| 4249 | * Implementation of RFC9000 6. Version Negotiation |
| 4250 | * |
| 4251 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4252 | * |
| 4253 | * Returns 0 on success else non-zero |
| 4254 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4255 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4256 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4257 | { |
| 4258 | char buf[256]; |
| 4259 | int i = 0, j, version; |
| 4260 | const socklen_t addrlen = get_addr_len(addr); |
| 4261 | |
| 4262 | /* |
| 4263 | * header form |
| 4264 | * long header, fixed bit to 0 for Version Negotiation |
| 4265 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4266 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4267 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4268 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4269 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4270 | /* null version for Version Negotiation */ |
| 4271 | buf[i++] = '\x00'; |
| 4272 | buf[i++] = '\x00'; |
| 4273 | buf[i++] = '\x00'; |
| 4274 | buf[i++] = '\x00'; |
| 4275 | |
| 4276 | /* source connection id */ |
| 4277 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4278 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4279 | i += pkt->scid.len; |
| 4280 | |
| 4281 | /* destination connection id */ |
| 4282 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4283 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4284 | i += pkt->dcid.len; |
| 4285 | |
| 4286 | /* supported version */ |
| 4287 | j = 0; |
| 4288 | do { |
| 4289 | version = htonl(quic_supported_version[j]); |
| 4290 | memcpy(&buf[i], &version, sizeof(version)); |
| 4291 | i += sizeof(version); |
| 4292 | |
| 4293 | version = quic_supported_version[++j]; |
| 4294 | } while (version); |
| 4295 | |
| 4296 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4297 | return 1; |
| 4298 | |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4302 | /* Generate the token to be used in Retry packets. The token is written to |
| 4303 | * <buf> which is expected to be <len> bytes. |
| 4304 | * |
| 4305 | * Various parameters are expected to be encoded in the token. For now, only |
| 4306 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4307 | * as this CID must be repeated by the server in the transport parameters. |
| 4308 | * |
| 4309 | * TODO add the client address to validate the token origin. |
| 4310 | * |
| 4311 | * Returns the length of the encoded token or 0 on error. |
| 4312 | */ |
| 4313 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4314 | struct quic_rx_packet *pkt) |
| 4315 | { |
| 4316 | const size_t token_len = 1 + pkt->dcid.len; |
| 4317 | unsigned char i = 0; |
| 4318 | |
| 4319 | if (token_len > len) |
| 4320 | return 0; |
| 4321 | |
| 4322 | buf[i++] = pkt->dcid.len; |
| 4323 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4324 | i += pkt->dcid.len; |
| 4325 | |
| 4326 | return i; |
| 4327 | } |
| 4328 | |
| 4329 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4330 | * the Initial <pkt> packet. |
| 4331 | * |
| 4332 | * Returns 0 on success else non-zero. |
| 4333 | */ |
| 4334 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4335 | struct quic_rx_packet *pkt) |
| 4336 | { |
| 4337 | unsigned char buf[128]; |
| 4338 | int i = 0, token_len; |
| 4339 | const socklen_t addrlen = get_addr_len(addr); |
| 4340 | struct quic_cid scid; |
| 4341 | |
| 4342 | /* long header + fixed bit + packet type 0x3 */ |
| 4343 | buf[i++] = 0xf0; |
| 4344 | /* version */ |
| 4345 | buf[i++] = 0x00; |
| 4346 | buf[i++] = 0x00; |
| 4347 | buf[i++] = 0x00; |
| 4348 | buf[i++] = 0x01; |
| 4349 | |
| 4350 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4351 | buf[i++] = pkt->scid.len; |
| 4352 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4353 | i += pkt->scid.len; |
| 4354 | |
| 4355 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4356 | scid.len = QUIC_HAP_CID_LEN; |
| 4357 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4358 | return 1; |
| 4359 | |
| 4360 | buf[i++] = scid.len; |
| 4361 | memcpy(&buf[i], scid.data, scid.len); |
| 4362 | i += scid.len; |
| 4363 | |
| 4364 | /* token */ |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4365 | if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4366 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 4367 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4368 | i += token_len; |
| 4369 | |
| 4370 | /* token integrity tag */ |
| 4371 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4372 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4373 | pkt->dcid.len, buf, i)) { |
| 4374 | return 1; |
| 4375 | } |
| 4376 | |
| 4377 | i += QUIC_TLS_TAG_LEN; |
| 4378 | |
| 4379 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4380 | return 1; |
| 4381 | |
| 4382 | return 0; |
| 4383 | } |
| 4384 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4385 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4386 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4387 | * concatenated to the <pkt> DCID field. |
| 4388 | * |
| 4389 | * Returns the instance or NULL if not found. |
| 4390 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4391 | 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] | 4392 | struct listener *l, |
| 4393 | struct sockaddr_storage *saddr) |
| 4394 | { |
| 4395 | struct quic_conn *qc = NULL; |
| 4396 | struct ebmb_node *node; |
| 4397 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4398 | /* set if the quic_conn is found in the second DCID tree */ |
| 4399 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4400 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4401 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4402 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4403 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4404 | /* DCIDs of first packets coming from multiple clients may have |
| 4405 | * the same values. Let's distinguish them by concatenating the |
| 4406 | * socket addresses. |
| 4407 | */ |
| 4408 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4409 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4410 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4411 | if (node) { |
| 4412 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4413 | goto end; |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4418 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4419 | * used. |
| 4420 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4421 | 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] | 4422 | if (!node) |
| 4423 | goto end; |
| 4424 | |
| 4425 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4426 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4427 | found_in_dcid = 1; |
| 4428 | |
| 4429 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4430 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4431 | * If already done, this is a noop. |
| 4432 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4433 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4434 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4435 | |
| 4436 | return qc; |
| 4437 | } |
| 4438 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4439 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4440 | * will extract the parameters stored in the token : <odcid>. |
| 4441 | * |
| 4442 | * Returns 0 on success else non-zero. |
| 4443 | */ |
| 4444 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4445 | struct quic_cid *odcid) |
| 4446 | { |
| 4447 | uint64_t odcid_len; |
| 4448 | |
| 4449 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4450 | return 1; |
| 4451 | |
Frédéric Lécaille | f1f812b | 2022-03-17 16:22:02 +0100 | [diff] [blame] | 4452 | if (odcid_len > QUIC_CID_MAXLEN) |
| 4453 | return 1; |
| 4454 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4455 | memcpy(odcid->data, token, odcid_len); |
| 4456 | odcid->len = odcid_len; |
| 4457 | |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4461 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4462 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4463 | * parameters of this session. |
| 4464 | * This is the responsibility of the caller to check the validity of all the |
| 4465 | * pointers passed as parameter to this function. |
| 4466 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4467 | * CO_ER_SSL_NO_MEM. |
| 4468 | */ |
| 4469 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4470 | unsigned char *params, size_t params_len) |
| 4471 | { |
| 4472 | int retry; |
| 4473 | |
| 4474 | retry = 1; |
| 4475 | retry: |
| 4476 | *ssl = SSL_new(ssl_ctx); |
| 4477 | if (!*ssl) { |
| 4478 | if (!retry--) |
| 4479 | goto err; |
| 4480 | |
| 4481 | pool_gc(NULL); |
| 4482 | goto retry; |
| 4483 | } |
| 4484 | |
| 4485 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4486 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4487 | !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] | 4488 | SSL_free(*ssl); |
| 4489 | *ssl = NULL; |
| 4490 | if (!retry--) |
| 4491 | goto err; |
| 4492 | |
| 4493 | pool_gc(NULL); |
| 4494 | goto retry; |
| 4495 | } |
| 4496 | |
| 4497 | return 0; |
| 4498 | |
| 4499 | err: |
| 4500 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4501 | return -1; |
| 4502 | } |
| 4503 | |
| 4504 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4505 | * used to process <qc> received packets. The allocated context is stored in |
| 4506 | * <qc.xprt_ctx>. |
| 4507 | * |
| 4508 | * Returns 0 on success else non-zero. |
| 4509 | */ |
| 4510 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4511 | { |
| 4512 | struct bind_conf *bc = qc->li->bind_conf; |
| 4513 | struct ssl_sock_ctx *ctx = NULL; |
| 4514 | |
| 4515 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4516 | if (!ctx) |
| 4517 | goto err; |
| 4518 | |
| 4519 | ctx->wait_event.tasklet = tasklet_new(); |
| 4520 | if (!ctx->wait_event.tasklet) |
| 4521 | goto err; |
| 4522 | |
| 4523 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4524 | ctx->wait_event.tasklet->context = ctx; |
| 4525 | ctx->wait_event.events = 0; |
| 4526 | ctx->subs = NULL; |
| 4527 | ctx->xprt_ctx = NULL; |
| 4528 | ctx->qc = qc; |
| 4529 | |
| 4530 | /* Set tasklet tid based on the SCID selected by us for this |
| 4531 | * connection. The upper layer will also be binded on the same thread. |
| 4532 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4533 | 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] | 4534 | |
| 4535 | if (qc_is_listener(qc)) { |
| 4536 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4537 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4538 | goto err; |
| 4539 | } |
| 4540 | |
| 4541 | /* Enabling 0-RTT */ |
| 4542 | if (bc->ssl_conf.early_data) |
| 4543 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4544 | |
| 4545 | SSL_set_accept_state(ctx->ssl); |
| 4546 | } |
| 4547 | |
| 4548 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4549 | |
| 4550 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4551 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4552 | |
| 4553 | return 0; |
| 4554 | |
| 4555 | err: |
| 4556 | if (ctx && ctx->wait_event.tasklet) |
| 4557 | tasklet_free(ctx->wait_event.tasklet); |
| 4558 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4559 | |
| 4560 | return 1; |
| 4561 | } |
| 4562 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4563 | 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] | 4564 | struct quic_rx_packet *pkt, int first_pkt, |
| 4565 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4566 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4567 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4568 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4569 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4570 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4571 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4572 | size_t b_cspace; |
| 4573 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4574 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4575 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4576 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4577 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4578 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4579 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4580 | /* This ist only to please to traces and distinguish the |
| 4581 | * packet with parsed packet number from others. |
| 4582 | */ |
| 4583 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4584 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4585 | goto err; |
| 4586 | |
| 4587 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4588 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4589 | /* XXX TO BE DISCARDED */ |
| 4590 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4591 | goto err; |
| 4592 | } |
| 4593 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4594 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4595 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4596 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4597 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4598 | uint64_t len; |
| 4599 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4600 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4601 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4602 | goto err; |
| 4603 | } |
| 4604 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4605 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4606 | * they must have the same DCID. |
| 4607 | */ |
| 4608 | if (!first_pkt && |
| 4609 | (pkt->dcid.len != dgram->dcid_len || |
| 4610 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4611 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4612 | goto err; |
| 4613 | } |
| 4614 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4615 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4616 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4617 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4618 | goto err; |
| 4619 | } |
| 4620 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4621 | /* RFC9000 6. Version Negotiation */ |
| 4622 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4623 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4624 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4625 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4626 | goto err; |
| 4627 | } |
| 4628 | |
| 4629 | 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] | 4630 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4631 | } |
| 4632 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4633 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4634 | * there is no Initial connection IDs storage. |
| 4635 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4636 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4637 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4638 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4639 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4640 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4641 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4642 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4643 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4644 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 4645 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 4646 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4647 | */ |
| 4648 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4649 | |
| 4650 | /* TODO Retry should be automatically activated if |
| 4651 | * suspect network usage is detected. |
| 4652 | */ |
| 4653 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4654 | 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] | 4655 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4656 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4657 | goto err; |
| 4658 | } |
| 4659 | |
| 4660 | goto err; |
| 4661 | } |
| 4662 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4663 | pkt->token = buf; |
| 4664 | buf += pkt->token_len; |
| 4665 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4666 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4667 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4668 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4669 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4670 | goto err; |
| 4671 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4672 | } |
| 4673 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4674 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4675 | end - buf < len) { |
| 4676 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4677 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4678 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4679 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4680 | payload = buf; |
| 4681 | pkt->len = len + payload - beg; |
| 4682 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4683 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4684 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4685 | int ipv4; |
| 4686 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4687 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4688 | const unsigned char *salt = initial_salt_v1; |
| 4689 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4690 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4691 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4692 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4693 | goto err; |
| 4694 | } |
| 4695 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 4696 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 4697 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 4698 | goto err; |
| 4699 | } |
| 4700 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4701 | pkt->saddr = dgram->saddr; |
| 4702 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4703 | qc = qc_new_conn(pkt->version, ipv4, |
| 4704 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4705 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4706 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4707 | goto err; |
| 4708 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4709 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4710 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4711 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4712 | /* Copy the transport parameters. */ |
| 4713 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4714 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4715 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4716 | if (pkt->token_len) { |
| 4717 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4718 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4719 | goto err; |
| 4720 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4721 | /* Copy retry_source_connection_id transport parameter. */ |
| 4722 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4723 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4724 | } |
| 4725 | else { |
| 4726 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4727 | odcid->len = pkt->dcid.len; |
| 4728 | } |
| 4729 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4730 | /* Copy the initial source connection ID. */ |
| 4731 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4732 | qc->enc_params_len = |
| 4733 | quic_transport_params_encode(qc->enc_params, |
| 4734 | qc->enc_params + sizeof qc->enc_params, |
| 4735 | &qc->rx.params, 1); |
| 4736 | if (!qc->enc_params_len) |
| 4737 | goto err; |
| 4738 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4739 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4740 | goto err; |
| 4741 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 4742 | if (!quic_conn_init_timer(qc)) |
| 4743 | goto err; |
| 4744 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4745 | if (!quic_conn_init_idle_timer_task(qc)) |
| 4746 | goto err; |
| 4747 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4748 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4749 | * chosen by the client for Initial packets. |
| 4750 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4751 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4752 | salt = initial_salt_draft_29; |
| 4753 | salt_len = sizeof initial_salt_draft_29; |
| 4754 | } |
| 4755 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4756 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4757 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4758 | goto err; |
| 4759 | } |
| 4760 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4761 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4762 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4763 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4764 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4765 | /* If the insertion failed, it means that another |
| 4766 | * thread has already allocated a QUIC connection for |
| 4767 | * the same CID. Liberate our allocated connection. |
| 4768 | */ |
| 4769 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4770 | qc_to_purge = qc; |
| 4771 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4772 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4773 | pkt->qc = qc; |
| 4774 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4775 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4776 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4777 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4778 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4779 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4780 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4781 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4782 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4783 | } |
| 4784 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4785 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4786 | } |
| 4787 | } |
| 4788 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4789 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4790 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4791 | goto err; |
| 4792 | } |
| 4793 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4794 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4795 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4796 | |
| 4797 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4798 | * they must have the same DCID. |
| 4799 | */ |
| 4800 | if (!first_pkt && |
| 4801 | (pkt->dcid.len != dgram->dcid_len || |
| 4802 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4803 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4804 | goto err; |
| 4805 | } |
| 4806 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4807 | buf += QUIC_HAP_CID_LEN; |
| 4808 | |
| 4809 | /* A short packet is the last one of a UDP datagram. */ |
| 4810 | payload = buf; |
| 4811 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4812 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4813 | 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] | 4814 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4815 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4816 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4817 | goto err; |
| 4818 | } |
| 4819 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4820 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4823 | |
| 4824 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4825 | * they must have the same DCID. |
| 4826 | * |
| 4827 | * This check must be done after the final update to pkt.len to |
| 4828 | * properly drop the packet on failure. |
| 4829 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4830 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4831 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4832 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4833 | QUIC_EV_CONN_LPKT, qc); |
| 4834 | /* Reset the anti-amplification bit. It will be set again |
| 4835 | * when sending the next packet if reached again. |
| 4836 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4837 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 4838 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4839 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4840 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4841 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4842 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4843 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4844 | if (qc->err_code) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4845 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4846 | goto out; |
| 4847 | } |
| 4848 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4849 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4850 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4851 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4852 | if (b_cspace < pkt->len) { |
| 4853 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4854 | if (b_cspace) { |
| 4855 | b_putchr(&qc->rx.buf, 0x00); |
| 4856 | b_cspace--; |
| 4857 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4858 | b_add(&qc->rx.buf, b_cspace); |
| 4859 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4860 | 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] | 4861 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4862 | goto err; |
| 4863 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4864 | } |
| 4865 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4866 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4867 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4868 | goto err; |
| 4869 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4870 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4871 | 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] | 4872 | if (pkt->aad_len) |
| 4873 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4874 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4875 | /* Wake up the connection packet handler task from here only if all |
| 4876 | * the contexts have been initialized, especially the mux context |
| 4877 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4878 | * will start it if these contexts for the connection are not already |
| 4879 | * initialized. |
| 4880 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4881 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4882 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4883 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4884 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4885 | 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] | 4886 | |
| 4887 | return pkt->len; |
| 4888 | |
| 4889 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4890 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4891 | * This cannot be done by this thread. |
| 4892 | */ |
| 4893 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4894 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4895 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4896 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4897 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4898 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4899 | if (!pkt->len) |
| 4900 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4901 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4902 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4903 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4904 | return -1; |
| 4905 | } |
| 4906 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4907 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4908 | * 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] | 4909 | */ |
| 4910 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4911 | int type, size_t pn_len, struct quic_conn *conn) |
| 4912 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4913 | 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] | 4914 | return 0; |
| 4915 | |
| 4916 | /* #0 byte flags */ |
| 4917 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4918 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4919 | /* Version */ |
| 4920 | quic_write_uint32(buf, end, conn->version); |
| 4921 | *(*buf)++ = conn->dcid.len; |
| 4922 | /* Destination connection ID */ |
| 4923 | if (conn->dcid.len) { |
| 4924 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4925 | *buf += conn->dcid.len; |
| 4926 | } |
| 4927 | /* Source connection ID */ |
| 4928 | *(*buf)++ = conn->scid.len; |
| 4929 | if (conn->scid.len) { |
| 4930 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4931 | *buf += conn->scid.len; |
| 4932 | } |
| 4933 | |
| 4934 | return 1; |
| 4935 | } |
| 4936 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4937 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4938 | * 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] | 4939 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4940 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4941 | size_t pn_len, struct quic_conn *conn, |
| 4942 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4943 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4944 | if (end - *buf < 1 + conn->dcid.len) |
| 4945 | return 0; |
| 4946 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4947 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4948 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4949 | ((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] | 4950 | /* Destination connection ID */ |
| 4951 | if (conn->dcid.len) { |
| 4952 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4953 | *buf += conn->dcid.len; |
| 4954 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4955 | |
| 4956 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4960 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4961 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4962 | * Returns 1 if succeeded or 0 if failed. |
| 4963 | */ |
| 4964 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4965 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4966 | { |
| 4967 | int i, ret, outlen; |
| 4968 | EVP_CIPHER_CTX *ctx; |
| 4969 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4970 | * and at most 4 bytes for the packet number |
| 4971 | */ |
| 4972 | unsigned char mask[5] = {0}; |
| 4973 | |
| 4974 | ret = 0; |
| 4975 | ctx = EVP_CIPHER_CTX_new(); |
| 4976 | if (!ctx) |
| 4977 | return 0; |
| 4978 | |
| 4979 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4980 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4981 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4982 | goto out; |
| 4983 | |
| 4984 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4985 | for (i = 0; i < pnlen; i++) |
| 4986 | pn[i] ^= mask[i + 1]; |
| 4987 | |
| 4988 | ret = 1; |
| 4989 | |
| 4990 | out: |
| 4991 | EVP_CIPHER_CTX_free(ctx); |
| 4992 | |
| 4993 | return ret; |
| 4994 | } |
| 4995 | |
| 4996 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4997 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4998 | * Return 1 if succeeded, 0 if not. |
| 4999 | */ |
| 5000 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5001 | { |
| 5002 | size_t room, ack_delay_sz; |
| 5003 | |
| 5004 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5005 | /* A frame is made of 1 byte for the frame type. */ |
| 5006 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5007 | 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] | 5008 | return 0; |
| 5009 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5010 | 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] | 5011 | } |
| 5012 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5013 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5014 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5015 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5016 | * with the number of bytes already present in this buffer which must be taken |
| 5017 | * into an account for the Length packet field value. <headlen> is the number of |
| 5018 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5019 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5020 | * 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] | 5021 | * 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] | 5022 | * Return 1 if succeeded, 0 if not. |
| 5023 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5024 | 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] | 5025 | size_t room, size_t *len, size_t headlen, |
| 5026 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5027 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5028 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5029 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5030 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5031 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5032 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5033 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5034 | return 0; |
| 5035 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5036 | /* If we are not probing we must take into an account the congestion |
| 5037 | * control window. |
| 5038 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5039 | if (!qel->pktns->tx.pto_probe) { |
| 5040 | size_t remain = quic_path_prep_data(qc->path); |
| 5041 | |
| 5042 | if (headlen > remain) |
| 5043 | return 0; |
| 5044 | |
| 5045 | room = QUIC_MIN(room, remain - headlen); |
| 5046 | } |
| 5047 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5048 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5049 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5050 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5051 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5052 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5053 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5054 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5055 | break; |
| 5056 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5057 | switch (cf->type) { |
| 5058 | case QUIC_FT_CRYPTO: |
| 5059 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5060 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5061 | /* Compute the length of this CRYPTO frame header */ |
| 5062 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5063 | /* Compute the data length of this CRyPTO frame. */ |
| 5064 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5065 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5066 | 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] | 5067 | if (!dlen) |
| 5068 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5069 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5070 | /* CRYPTO frame length. */ |
| 5071 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5072 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5073 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5074 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5075 | * length and the length of this length. |
| 5076 | */ |
| 5077 | *len += flen; |
| 5078 | room -= flen; |
| 5079 | if (dlen == cf->crypto.len) { |
| 5080 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5081 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5082 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5083 | } |
| 5084 | else { |
| 5085 | struct quic_frame *new_cf; |
| 5086 | |
| 5087 | new_cf = pool_alloc(pool_head_quic_frame); |
| 5088 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5089 | 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] | 5090 | return 0; |
| 5091 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5092 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5093 | new_cf->type = QUIC_FT_CRYPTO; |
| 5094 | new_cf->crypto.len = dlen; |
| 5095 | new_cf->crypto.offset = cf->crypto.offset; |
| 5096 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5097 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5098 | /* Consume <dlen> bytes of the current frame. */ |
| 5099 | cf->crypto.len -= dlen; |
| 5100 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5101 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5102 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5103 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5104 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5105 | /* Note that these frames are accepted in short packets only without |
| 5106 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5107 | * 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] | 5108 | * |
| 5109 | * 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] | 5110 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5111 | */ |
| 5112 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5113 | ((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] | 5114 | /* Compute the data length of this STREAM frame. */ |
| 5115 | avail_room = room - hlen - *len; |
| 5116 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5117 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5118 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5119 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5120 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5121 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5122 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5123 | if (dlen > cf->stream.len) { |
| 5124 | dlen = cf->stream.len; |
| 5125 | } |
| 5126 | dlen_sz = quic_int_getsize(dlen); |
| 5127 | flen = hlen + dlen_sz + dlen; |
| 5128 | } |
| 5129 | else { |
| 5130 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 5131 | flen = hlen + dlen; |
| 5132 | } |
| 5133 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5134 | 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] | 5135 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5136 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5137 | /* Add the STREAM data length and its encoded length to the packet |
| 5138 | * length and the length of this length. |
| 5139 | */ |
| 5140 | *len += flen; |
| 5141 | room -= flen; |
| 5142 | if (dlen == cf->stream.len) { |
| 5143 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5144 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5145 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5146 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5147 | /* The MUX stream might be released at this |
| 5148 | * stage. This can most notably happen on |
| 5149 | * retransmission. |
| 5150 | */ |
| 5151 | if (qc->mux_state == QC_MUX_READY && |
| 5152 | !cf->stream.stream->release) { |
| 5153 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5154 | cf->stream.len, |
| 5155 | cf->stream.offset.key); |
| 5156 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5157 | } |
| 5158 | else { |
| 5159 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5160 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5161 | |
| 5162 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5163 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5164 | 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] | 5165 | return 0; |
| 5166 | } |
| 5167 | |
| 5168 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5169 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5170 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5171 | new_cf->stream.id = cf->stream.id; |
| 5172 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5173 | new_cf->stream.offset = cf->stream.offset; |
| 5174 | new_cf->stream.len = dlen; |
| 5175 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5176 | /* FIN bit reset */ |
| 5177 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5178 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5179 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5180 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5181 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5182 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5183 | b_size(cf->stream.buf), |
| 5184 | (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] | 5185 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5186 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5187 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5188 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5189 | /* The MUX stream might be released at this |
| 5190 | * stage. This can most notably happen on |
| 5191 | * retransmission. |
| 5192 | */ |
| 5193 | if (qc->mux_state == QC_MUX_READY && |
| 5194 | !cf->stream.stream->release) { |
| 5195 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5196 | new_cf->stream.len, |
| 5197 | new_cf->stream.offset.key); |
| 5198 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5199 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5200 | |
| 5201 | /* TODO the MUX is notified about the frame sending via |
| 5202 | * previous qcc_streams_sent_done call. However, the |
| 5203 | * sending can fail later, for example if the sendto |
| 5204 | * system call returns an error. As the MUX has been |
| 5205 | * notified, the transport layer is responsible to |
| 5206 | * bufferize and resent the announced data later. |
| 5207 | */ |
| 5208 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5209 | break; |
| 5210 | |
| 5211 | default: |
| 5212 | flen = qc_frm_len(cf); |
| 5213 | BUG_ON(!flen); |
| 5214 | if (flen > room) |
| 5215 | continue; |
| 5216 | |
| 5217 | *len += flen; |
| 5218 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5219 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5220 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5221 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5222 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5223 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5224 | } |
| 5225 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5226 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5227 | } |
| 5228 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5229 | /* 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] | 5230 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 5231 | * 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] | 5232 | * filling the buffer with as much frames as possible from <frms> list of |
| 5233 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5234 | * 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] | 5235 | * 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] | 5236 | * having returned from this function. |
| 5237 | * 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] | 5238 | * number field in this packet. <pn_len> will also have the packet number |
| 5239 | * length as value. |
| 5240 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5241 | * 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] | 5242 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5243 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 5244 | size_t dglen, struct quic_tx_packet *pkt, |
| 5245 | 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] | 5246 | int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5247 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 5248 | struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5249 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5250 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5251 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5252 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5253 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5254 | 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] | 5255 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5256 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5257 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5258 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5259 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5260 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5261 | /* Length field value with CRYPTO frames if present. */ |
| 5262 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5263 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5264 | /* When not probing, and no immediate close is required, reduce the size of this |
| 5265 | * buffer to respect the congestion controller window. |
| 5266 | * 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] | 5267 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5268 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5269 | size_t path_room; |
| 5270 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5271 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5272 | if (end - beg > path_room) |
| 5273 | end = beg + path_room; |
| 5274 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5275 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5276 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5277 | * length field if any. |
| 5278 | */ |
| 5279 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5280 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5281 | goto no_room; |
| 5282 | |
| 5283 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5284 | 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] | 5285 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5286 | *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] | 5287 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5288 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5289 | !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] | 5290 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5291 | !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] | 5292 | goto no_room; |
| 5293 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5294 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5295 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5296 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5297 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5298 | /* Build an ACK frame if required. */ |
| 5299 | ack_frm_len = 0; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5300 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) { |
| 5301 | 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] | 5302 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5303 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5304 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5305 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5306 | * that from here, we do not know if we will have to send a PING frame. |
| 5307 | * This will be decided after having computed the ack-eliciting frames |
| 5308 | * to be added to this packet. |
| 5309 | */ |
| 5310 | 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] | 5311 | if (!ack_frm_len) |
| 5312 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5313 | } |
| 5314 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5315 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5316 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5317 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5318 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5319 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5320 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5321 | 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] | 5322 | /* Initialize the length of the frames built below to <len>. |
| 5323 | * If any frame could be successfully built by qc_build_frms(), |
| 5324 | * we will have len_frms > len. |
| 5325 | */ |
| 5326 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5327 | if (!qc_build_frms(&frm_list, frms, |
| 5328 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5329 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5330 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5331 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5332 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5333 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5334 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5335 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5336 | * for the encryption tag. It must be taken into an account for the length |
| 5337 | * of this packet. |
| 5338 | */ |
| 5339 | if (len_frms) |
| 5340 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5341 | else |
| 5342 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5343 | /* CONNECTION_CLOSE frame */ |
| 5344 | if (cc) { |
| 5345 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5346 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5347 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5348 | len += qc_frm_len(&cc_frm); |
| 5349 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5350 | add_ping_frm = 0; |
| 5351 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5352 | len_sz = quic_int_getsize(len); |
| 5353 | /* Add this packet size to <dglen> */ |
| 5354 | dglen += head_len + len_sz + len; |
| 5355 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5356 | /* This is a maximum padding size */ |
| 5357 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5358 | /* The length field value is of this packet is <len> + <padding_len> |
| 5359 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5360 | * <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] | 5361 | * sizes from <padding_len>. |
| 5362 | */ |
| 5363 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5364 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5365 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5366 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5367 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5368 | /* 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] | 5369 | add_ping_frm = 1; |
| 5370 | len += 1; |
| 5371 | } |
| 5372 | /* 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] | 5373 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5374 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5375 | } |
| 5376 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5377 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5378 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5379 | |
| 5380 | /* Packet number field address. */ |
| 5381 | *buf_pn = pos; |
| 5382 | |
| 5383 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5384 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5385 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5386 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5387 | if (ack_frm_len) { |
| 5388 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 5389 | goto no_room; |
| 5390 | |
| 5391 | 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] | 5392 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5393 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5394 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5395 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5396 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5397 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5398 | unsigned char *spos = pos; |
| 5399 | |
| 5400 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5401 | ssize_t room = end - pos; |
| 5402 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5403 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5404 | /* TODO: this should not have happened if qc_build_frms() |
| 5405 | * had correctly computed and sized the frames to be |
| 5406 | * added to this packet. Note that <cf> was added |
| 5407 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 5408 | */ |
| 5409 | LIST_DELETE(&cf->list); |
| 5410 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5411 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5412 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 5413 | |
| 5414 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5415 | quic_tx_packet_refinc(pkt); |
| 5416 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5417 | } |
| 5418 | } |
| 5419 | |
| 5420 | /* Build a PING frame if needed. */ |
| 5421 | if (add_ping_frm) { |
| 5422 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5423 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5424 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5425 | } |
| 5426 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5427 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5428 | if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5429 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5430 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5431 | /* Build a PADDING frame if needed. */ |
| 5432 | if (padding_len) { |
| 5433 | frm.type = QUIC_FT_PADDING; |
| 5434 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5435 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5436 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5437 | } |
| 5438 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5439 | /* If this packet is ack-eliciting and we are probing let's |
| 5440 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5441 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5442 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5443 | qel->pktns->tx.pto_probe) |
| 5444 | qel->pktns->tx.pto_probe--; |
| 5445 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5446 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5447 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5448 | 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] | 5449 | |
| 5450 | return 1; |
| 5451 | |
| 5452 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5453 | /* 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] | 5454 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 5455 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt); |
| 5456 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5457 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5458 | } |
| 5459 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5460 | 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] | 5461 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5462 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5463 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5464 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5465 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5466 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5467 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5468 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 5469 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 5470 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5471 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5472 | } |
| 5473 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5474 | /* 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] | 5475 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 5476 | * of prebuilt frames. |
| 5477 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5478 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5479 | * -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] | 5480 | * XXX NOTE XXX |
| 5481 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 5482 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 5483 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5484 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5485 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5486 | const unsigned char *buf_end, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 5487 | struct quic_enc_level *qel, struct list *frms, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5488 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5489 | int pkt_type, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5490 | { |
| 5491 | /* The pointer to the packet number field. */ |
| 5492 | unsigned char *buf_pn; |
| 5493 | unsigned char *beg, *end, *payload; |
| 5494 | int64_t pn; |
| 5495 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5496 | struct quic_tls_ctx *tls_ctx; |
| 5497 | struct quic_tx_packet *pkt; |
| 5498 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5499 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5500 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5501 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5502 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5503 | 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] | 5504 | *err = -2; |
| 5505 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5506 | } |
| 5507 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5508 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5509 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5510 | pn_len = 0; |
| 5511 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5512 | |
| 5513 | pn = qel->pktns->tx.next_pn + 1; |
| 5514 | 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] | 5515 | padding, cc, probe, qel, qc, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5516 | *err = -1; |
| 5517 | goto err; |
| 5518 | } |
| 5519 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5520 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5521 | payload = buf_pn + pn_len; |
| 5522 | payload_len = end - payload; |
| 5523 | aad_len = payload - beg; |
| 5524 | |
| 5525 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5526 | 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] | 5527 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5528 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5529 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5530 | |
| 5531 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5532 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5533 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5534 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5535 | 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] | 5536 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5537 | goto err; |
| 5538 | } |
| 5539 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5540 | /* Consume a packet number */ |
| 5541 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5542 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 5543 | 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] | 5544 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5545 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5546 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5547 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5548 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5549 | /* 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] | 5550 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5551 | pkt->in_flight_len = pkt->len; |
| 5552 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5553 | } |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 5554 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 5555 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 5556 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 5557 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 5558 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5559 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5560 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5561 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5562 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5563 | |
| 5564 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 5565 | /* TODO: what about the frames which have been built |
| 5566 | * for this packet. |
| 5567 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5568 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5569 | 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] | 5570 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5571 | } |
| 5572 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5573 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5574 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5575 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5576 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5577 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5578 | */ |
| 5579 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5580 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5581 | struct qcc *qcc = conn->qc->qcc; |
| 5582 | |
| 5583 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5584 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5585 | |
| 5586 | es->events |= event_type; |
| 5587 | qcc->subs = es; |
| 5588 | |
| 5589 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5590 | 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] | 5591 | |
| 5592 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5593 | 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] | 5594 | |
| 5595 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5596 | } |
| 5597 | |
| 5598 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5599 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5600 | * subscribe() call. It always returns zero. |
| 5601 | */ |
| 5602 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5603 | { |
| 5604 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5605 | } |
| 5606 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5607 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5608 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5609 | */ |
| 5610 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5611 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5612 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5613 | |
| 5614 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5615 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5616 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5617 | if (*xprt_ctx) |
| 5618 | goto out; |
| 5619 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5620 | *xprt_ctx = conn->qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5621 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5622 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5623 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5624 | |
| 5625 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5628 | /* Start the QUIC transport layer */ |
| 5629 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5630 | { |
| 5631 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5632 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5633 | |
| 5634 | qc = conn->qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5635 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5636 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
| 5637 | return 0; |
| 5638 | } |
| 5639 | |
| 5640 | /* mux-quic can now be considered ready. */ |
| 5641 | qc->mux_state = QC_MUX_READY; |
| 5642 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5643 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5644 | return 1; |
| 5645 | } |
| 5646 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5647 | /* transport-layer operations for QUIC connections. */ |
| 5648 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5649 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5650 | .subscribe = quic_conn_subscribe, |
| 5651 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5652 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5653 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5654 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5655 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5656 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5657 | .name = "QUIC", |
| 5658 | }; |
| 5659 | |
| 5660 | __attribute__((constructor)) |
| 5661 | static void __quic_conn_init(void) |
| 5662 | { |
| 5663 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5664 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5665 | } |
| 5666 | |
| 5667 | __attribute__((destructor)) |
| 5668 | static void __quic_conn_deinit(void) |
| 5669 | { |
| 5670 | BIO_meth_free(ha_quic_meth); |
| 5671 | } |
| 5672 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5673 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5674 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5675 | * 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] | 5676 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5677 | 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] | 5678 | { |
| 5679 | unsigned char *pos; |
| 5680 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5681 | struct quic_dghdlr *dghdlr = ctx; |
| 5682 | struct quic_dgram *dgram; |
| 5683 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5684 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5685 | 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] | 5686 | pos = dgram->buf; |
| 5687 | end = pos + dgram->len; |
| 5688 | do { |
| 5689 | int ret; |
| 5690 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5691 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5692 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 5693 | if (!pkt) |
| 5694 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5695 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5696 | quic_rx_packet_refinc(pkt); |
| 5697 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 5698 | first_pkt = 0; |
| 5699 | pos += pkt->len; |
| 5700 | quic_rx_packet_refdec(pkt); |
| 5701 | if (ret == -1) |
| 5702 | /* If the packet length could not be found, we cannot continue. */ |
| 5703 | break; |
| 5704 | } while (pos < end); |
| 5705 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5706 | /* Increasing the received bytes counter by the UDP datagram length |
| 5707 | * if this datagram could be associated to a connection. |
| 5708 | */ |
| 5709 | if (dgram->qc) |
| 5710 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 5711 | |
| 5712 | /* Mark this datagram as consumed */ |
| 5713 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5714 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5715 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5716 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5717 | |
| 5718 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5719 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5720 | } |
| 5721 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5722 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 5723 | * Returns 1 if succeeded, 0 if not. |
| 5724 | */ |
| 5725 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 5726 | unsigned char **dcid, size_t *dcid_len) |
| 5727 | { |
| 5728 | int long_header; |
| 5729 | size_t minlen, skip; |
| 5730 | |
| 5731 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 5732 | goto err; |
| 5733 | |
| 5734 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 5735 | minlen = long_header ? |
| 5736 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 5737 | 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] | 5738 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5739 | goto err; |
| 5740 | |
| 5741 | buf += skip; |
| 5742 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 5743 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 5744 | goto err; |
| 5745 | |
| 5746 | *dcid = buf; |
| 5747 | |
| 5748 | return 1; |
| 5749 | |
| 5750 | err: |
| 5751 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 5752 | return 0; |
| 5753 | } |
| 5754 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5755 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 5756 | * correct datagram handler. |
| 5757 | * Return 1 if a correct datagram could be found, 0 if not. |
| 5758 | */ |
| 5759 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 5760 | struct sockaddr_storage *saddr, |
| 5761 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5762 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5763 | struct quic_dgram *dgram; |
| 5764 | unsigned char *dcid; |
| 5765 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5766 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5767 | |
| 5768 | 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] | 5769 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5770 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5771 | 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] | 5772 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5773 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5774 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5775 | cid_tid = quic_get_cid_tid(dcid); |
| 5776 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5777 | /* All the members must be initialized! */ |
| 5778 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5779 | dgram->buf = buf; |
| 5780 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5781 | dgram->dcid = dcid; |
| 5782 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5783 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5784 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5785 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5786 | 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] | 5787 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5788 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5789 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5790 | return 1; |
| 5791 | |
| 5792 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5793 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5794 | } |
| 5795 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5796 | /* Allocate a new stream descriptor with id <id>. The caller is responsible to |
| 5797 | * store the stream in the appropriate tree. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5798 | * |
| 5799 | * Returns the newly allocated instance on success or else NULL. |
| 5800 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5801 | 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] | 5802 | { |
| 5803 | struct qc_stream_desc *stream; |
| 5804 | |
| 5805 | stream = pool_alloc(pool_head_quic_conn_stream); |
| 5806 | if (!stream) |
| 5807 | return NULL; |
| 5808 | |
| 5809 | stream->by_id.key = id; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5810 | stream->by_id.node.leaf_p = NULL; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5811 | |
| 5812 | stream->buf = BUF_NULL; |
| 5813 | stream->acked_frms = EB_ROOT; |
| 5814 | stream->ack_offset = 0; |
| 5815 | stream->release = 0; |
| 5816 | stream->ctx = ctx; |
| 5817 | |
| 5818 | return stream; |
| 5819 | } |
| 5820 | |
| 5821 | /* 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] | 5822 | * be freed as soon as all its buffered data are acknowledged. In the meantime, |
| 5823 | * the stream is stored in the <qc> tree : thus it must have been removed from |
| 5824 | * any other tree before calling this function. |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5825 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5826 | void qc_stream_desc_release(struct qc_stream_desc *stream, |
| 5827 | struct quic_conn *qc) |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5828 | { |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5829 | BUG_ON(stream->by_id.node.leaf_p); |
| 5830 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5831 | stream->release = 1; |
| 5832 | stream->ctx = NULL; |
| 5833 | |
| 5834 | if (!b_data(&stream->buf)) |
| 5835 | qc_stream_desc_free(stream); |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 5836 | else |
| 5837 | eb64_insert(&qc->streams_by_id, &stream->by_id); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 5838 | } |
| 5839 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5840 | /* Function to automatically activate QUIC traces on stdout. |
| 5841 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5842 | * Main use for now is in the docker image for QUIC interop testing. |
| 5843 | */ |
| 5844 | static void quic_init_stdout_traces(void) |
| 5845 | { |
| 5846 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5847 | trace_quic.sink = sink_find("stdout"); |
| 5848 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5849 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5850 | #endif |
| 5851 | } |
| 5852 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5853 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5854 | /* |
| 5855 | * Local variables: |
| 5856 | * c-indent-level: 8 |
| 5857 | * c-basic-offset: 8 |
| 5858 | * End: |
| 5859 | */ |