Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <netinet/tcp.h> |
| 23 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 24 | #include <import/ebmbtree.h> |
| 25 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 26 | #include <haproxy/buf-t.h> |
| 27 | #include <haproxy/compat.h> |
| 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/debug.h> |
| 30 | #include <haproxy/tools.h> |
| 31 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 32 | |
| 33 | #include <haproxy/connection.h> |
| 34 | #include <haproxy/fd.h> |
| 35 | #include <haproxy/freq_ctr.h> |
| 36 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 37 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 38 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 39 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 40 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 41 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 47 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 48 | #include <haproxy/quic_stats.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 49 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 50 | #include <haproxy/quic_tp.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 51 | #include <haproxy/cbuf.h> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 52 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 53 | #include <haproxy/quic_tls.h> |
| 54 | #include <haproxy/ssl_sock.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 55 | #include <haproxy/task.h> |
| 56 | #include <haproxy/trace.h> |
| 57 | #include <haproxy/xprt_quic.h> |
| 58 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 59 | /* list of supported QUIC versions by this implementation */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 60 | const struct quic_version quic_versions[] = { |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 61 | { |
| 62 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 63 | .initial_salt = initial_salt_draft_29, |
| 64 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 65 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 66 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 67 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 68 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 69 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 70 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 71 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 72 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 73 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 74 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 75 | }, |
| 76 | { |
| 77 | .num = QUIC_PROTOCOL_VERSION_1, |
| 78 | .initial_salt = initial_salt_v1, |
| 79 | .initial_salt_len = sizeof initial_salt_v1, |
| 80 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 81 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 82 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 83 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 84 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 85 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 86 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 87 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 88 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 89 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 90 | }, |
| 91 | { |
| 92 | .num = QUIC_PROTOCOL_VERSION_2_DRAFT, |
| 93 | .initial_salt = initial_salt_v2_draft, |
| 94 | .initial_salt_len = sizeof initial_salt_v2_draft, |
| 95 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 96 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 97 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 98 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 99 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 100 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 101 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
Frédéric Lécaille | b1cb958 | 2022-06-16 17:53:46 +0200 | [diff] [blame] | 102 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 103 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2_DRAFT, |
| 104 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2_DRAFT, |
| 105 | }, |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 108 | /* The total number of supported versions */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 109 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 110 | /* Listener only preferred version */ |
| 111 | const struct quic_version *preferred_version; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 112 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 113 | /* trace source and events */ |
| 114 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 115 | const struct trace_source *src, |
| 116 | const struct ist where, const struct ist func, |
| 117 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 118 | |
| 119 | static const struct trace_event quic_trace_events[] = { |
| 120 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 121 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 122 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 123 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 124 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 125 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 126 | { .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] | 127 | { .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] | 128 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 129 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 130 | { .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] | 131 | { .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] | 132 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 133 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 134 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 135 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 136 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 137 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 138 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 139 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 140 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 141 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 142 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 143 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 144 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 145 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 146 | { .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] | 147 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 148 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 149 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 150 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 151 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 152 | { .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] | 153 | { .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] | 154 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 155 | { .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] | 156 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 157 | { .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] | 158 | { .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] | 159 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 160 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
Frédéric Lécaille | c7785b5 | 2022-05-23 09:08:54 +0200 | [diff] [blame] | 161 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | { /* end */ } |
| 163 | }; |
| 164 | |
| 165 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 166 | /* arg1 */ { /* already used by the connection */ }, |
| 167 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 168 | /* arg3 */ { }, |
| 169 | /* arg4 */ { } |
| 170 | }; |
| 171 | |
| 172 | static const struct name_desc quic_trace_decoding[] = { |
| 173 | #define QUIC_VERB_CLEAN 1 |
| 174 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 175 | { /* end */ } |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | struct trace_source trace_quic = { |
| 180 | .name = IST("quic"), |
| 181 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 182 | .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] | 183 | .default_cb = quic_trace, |
| 184 | .known_events = quic_trace_events, |
| 185 | .lockon_args = quic_trace_lockon_args, |
| 186 | .decoding = quic_trace_decoding, |
| 187 | .report_events = ~0, /* report everything by default */ |
| 188 | }; |
| 189 | |
| 190 | #define TRACE_SOURCE &trace_quic |
| 191 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 192 | |
| 193 | static BIO_METHOD *ha_quic_meth; |
| 194 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 195 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 196 | 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] | 197 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 198 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 199 | 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] | 200 | DECLARE_POOL(pool_head_quic_connection_id, |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 201 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 202 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 203 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 204 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 205 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 206 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf)); |
| 207 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 208 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 209 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 210 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 211 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 212 | struct list *frms, struct quic_conn *qc, |
| 213 | const struct quic_version *ver, size_t dglen, int pkt_type, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 214 | int force_ack, int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 215 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 216 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 217 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 218 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 219 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 220 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 221 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 222 | /* Only for debug purpose */ |
| 223 | struct enc_debug_info { |
| 224 | unsigned char *payload; |
| 225 | size_t payload_len; |
| 226 | unsigned char *aad; |
| 227 | size_t aad_len; |
| 228 | uint64_t pn; |
| 229 | }; |
| 230 | |
| 231 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 232 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 233 | unsigned char *payload, size_t payload_len, |
| 234 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 235 | { |
| 236 | edi->payload = payload; |
| 237 | edi->payload_len = payload_len; |
| 238 | edi->aad = aad; |
| 239 | edi->aad_len = aad_len; |
| 240 | edi->pn = pn; |
| 241 | } |
| 242 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 243 | /* Trace callback for QUIC. |
| 244 | * These traces always expect that arg1, if non-null, is of type connection. |
| 245 | */ |
| 246 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 247 | const struct ist where, const struct ist func, |
| 248 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 249 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 250 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 251 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 252 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 253 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 254 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 255 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 256 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 257 | chunk_appendf(&trace_buf, "\n odcid"); |
| 258 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 259 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 260 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 261 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 262 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Frédéric Lécaille | c7785b5 | 2022-05-23 09:08:54 +0200 | [diff] [blame] | 265 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 266 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 4f5777a | 2022-06-20 19:39:26 +0200 | [diff] [blame] | 267 | quic_transport_params_dump(&trace_buf, qc, p); |
Frédéric Lécaille | c7785b5 | 2022-05-23 09:08:54 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 270 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 271 | const enum ssl_encryption_level_t *level = a2; |
| 272 | const size_t *len = a3; |
| 273 | |
| 274 | if (level) { |
| 275 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 276 | |
| 277 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 278 | } |
| 279 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 280 | 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] | 281 | } |
| 282 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 283 | /* Initial read & write secrets. */ |
| 284 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 285 | const unsigned char *rx_sec = a2; |
| 286 | const unsigned char *tx_sec = a3; |
| 287 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 288 | tls_ctx = &qc->els[level].tls_ctx; |
| 289 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 290 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 291 | if (rx_sec) |
| 292 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 293 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 294 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 295 | if (tx_sec) |
| 296 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 297 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 301 | const enum ssl_encryption_level_t *level = a2; |
| 302 | const unsigned char *secret = a3; |
| 303 | const size_t *secret_len = a4; |
| 304 | |
| 305 | if (level) { |
| 306 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 307 | |
| 308 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 309 | if (secret && secret_len) |
| 310 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 311 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 312 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 313 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 318 | const enum ssl_encryption_level_t *level = a2; |
| 319 | const unsigned char *secret = a3; |
| 320 | const size_t *secret_len = a4; |
| 321 | |
| 322 | if (level) { |
| 323 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 324 | |
| 325 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 326 | if (secret && secret_len) |
| 327 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 328 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 329 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 330 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 334 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 335 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 336 | const struct list *l = a2; |
| 337 | |
| 338 | if (l) { |
| 339 | const struct quic_frame *frm; |
| 340 | list_for_each_entry(frm, l, list) |
| 341 | chunk_frm_appendf(&trace_buf, frm); |
| 342 | } |
| 343 | } |
| 344 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 345 | 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] | 346 | const struct quic_tx_packet *pkt = a2; |
| 347 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 348 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 349 | |
| 350 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 351 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 352 | 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] | 353 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 354 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 355 | (unsigned long long)qc->path->cwnd, |
| 356 | (unsigned long long)qc->path->prep_in_flight, |
| 357 | (unsigned long long)qc->path->in_flight, |
| 358 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 359 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 360 | } |
| 361 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 362 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 363 | if (pkt->pn_node.key != (uint64_t)-1) |
| 364 | 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] | 365 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 366 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 367 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 368 | (unsigned long long)qc->rx.bytes, |
| 369 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (room) { |
| 373 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 374 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 375 | (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] | 376 | } |
| 377 | } |
| 378 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 379 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 380 | const enum quic_handshake_state *state = a2; |
| 381 | const int *err = a3; |
| 382 | |
| 383 | if (state) |
| 384 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 385 | if (err) |
| 386 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 387 | } |
| 388 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 389 | 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] | 390 | const struct quic_rx_packet *pkt = a2; |
| 391 | const unsigned long *pktlen = a3; |
| 392 | const SSL *ssl = a4; |
| 393 | |
| 394 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 395 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 396 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 397 | chunk_appendf(&trace_buf, " kp=%d", |
| 398 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 399 | chunk_appendf(&trace_buf, " el=%c", |
| 400 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 401 | if (pkt->pnl) |
| 402 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 403 | (unsigned long long)pkt->pn); |
| 404 | if (pkt->token_len) |
| 405 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 406 | (unsigned long long)pkt->token_len); |
| 407 | if (pkt->aad_len) |
| 408 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 409 | (unsigned long long)pkt->aad_len); |
| 410 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 411 | pkt->flags, (unsigned long long)pkt->len); |
| 412 | } |
| 413 | if (pktlen) |
| 414 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 415 | if (ssl) { |
| 416 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 417 | chunk_appendf(&trace_buf, " el=%c", |
| 418 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 423 | const struct quic_rx_packet *pkt = a2; |
| 424 | const struct quic_rx_crypto_frm *cf = a3; |
| 425 | const SSL *ssl = a4; |
| 426 | |
| 427 | if (pkt) |
| 428 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 429 | quic_packet_type_enc_level_char(pkt->type), |
| 430 | (unsigned long long)pkt->pn); |
| 431 | if (cf) |
| 432 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 433 | (unsigned long long)cf->offset_node.key, |
| 434 | (unsigned long long)cf->len); |
| 435 | if (ssl) { |
| 436 | 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] | 437 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 438 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 439 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 440 | |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 441 | if (qc->err.code) |
| 442 | 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] | 443 | } |
| 444 | |
| 445 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 446 | const struct quic_frame *frm = a2; |
| 447 | |
| 448 | if (frm) |
| 449 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 450 | } |
| 451 | |
| 452 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 453 | const struct quic_enc_level *qel = a2; |
| 454 | |
| 455 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 456 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 457 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 458 | " 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] | 459 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 460 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 461 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 462 | (unsigned long long)qc->path->cwnd, |
| 463 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 464 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 465 | (unsigned long long)pktns->tx.in_flight, |
| 466 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 470 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 471 | const struct enc_debug_info *edi = a2; |
| 472 | |
| 473 | if (edi) |
| 474 | chunk_appendf(&trace_buf, |
| 475 | " payload=@%p payload_len=%llu" |
| 476 | " aad=@%p aad_len=%llu pn=%llu", |
| 477 | edi->payload, (unsigned long long)edi->payload_len, |
| 478 | edi->aad, (unsigned long long)edi->aad_len, |
| 479 | (unsigned long long)edi->pn); |
| 480 | } |
| 481 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 482 | if (mask & QUIC_EV_CONN_RMHP) { |
| 483 | const struct quic_rx_packet *pkt = a2; |
| 484 | |
| 485 | if (pkt) { |
| 486 | const int *ret = a3; |
| 487 | |
| 488 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 489 | if (ret && *ret) |
| 490 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 491 | pkt->pnl, (unsigned long long)pkt->pn); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 496 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 497 | const unsigned long *val1 = a3; |
| 498 | const unsigned long *val2 = a4; |
| 499 | |
| 500 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 501 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 502 | if (val1) |
| 503 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 504 | if (val2) |
| 505 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 506 | } |
| 507 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 508 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 509 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 510 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 511 | |
| 512 | if (s) |
| 513 | 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] | 514 | if (stream) |
| 515 | 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] | 516 | } |
| 517 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 518 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 519 | const unsigned int *rtt_sample = a2; |
| 520 | const unsigned int *ack_delay = a3; |
| 521 | const struct quic_loss *ql = a4; |
| 522 | |
| 523 | if (rtt_sample) |
| 524 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 525 | if (ack_delay) |
| 526 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 527 | if (ql) |
| 528 | chunk_appendf(&trace_buf, |
| 529 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 530 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 531 | } |
| 532 | if (mask & QUIC_EV_CONN_CC) { |
| 533 | const struct quic_cc_event *ev = a2; |
| 534 | const struct quic_cc *cc = a3; |
| 535 | |
| 536 | if (a2) |
| 537 | quic_cc_event_trace(&trace_buf, ev); |
| 538 | if (a3) |
| 539 | quic_cc_state_trace(&trace_buf, cc); |
| 540 | } |
| 541 | |
| 542 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 543 | const struct quic_pktns *pktns = a2; |
| 544 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 545 | |
| 546 | if (pktns) { |
| 547 | chunk_appendf(&trace_buf, " pktns=%s", |
| 548 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 549 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 550 | if (pktns->tx.loss_time) |
| 551 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 552 | 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] | 553 | } |
| 554 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 555 | struct quic_tx_packet *pkt; |
| 556 | |
| 557 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 558 | list_for_each_entry(pkt, lost_pkts, list) |
| 559 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | 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] | 564 | const struct quic_pktns *pktns = a2; |
| 565 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 566 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 567 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 568 | if (ifae_pkts) |
| 569 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 570 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 571 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 572 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 573 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 574 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 575 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 576 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 577 | if (pktns->tx.in_flight) |
| 578 | 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] | 579 | if (pktns->tx.loss_time) |
| 580 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 581 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 582 | } |
| 583 | if (mask & QUIC_EV_CONN_SPTO) { |
| 584 | if (pktns->tx.time_of_last_eliciting) |
| 585 | chunk_appendf(&trace_buf, " tole=%dms", |
| 586 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 587 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 588 | 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] | 589 | } |
| 590 | } |
| 591 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 592 | 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] | 593 | chunk_appendf(&trace_buf, |
| 594 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 599 | const struct quic_tx_packet *pkt = a2; |
| 600 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 601 | chunk_appendf(&trace_buf, " err=%u cwnd=%llu ppif=%llu pif=%llu", qc->sendto_err, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 602 | (unsigned long long)qc->path->cwnd, |
| 603 | (unsigned long long)qc->path->prep_in_flight, |
| 604 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 605 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 606 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 607 | (unsigned long)pkt->pn_node.key, |
| 608 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 609 | 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] | 610 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 611 | } |
| 612 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 613 | |
| 614 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 615 | const uint8_t *alert = a2; |
| 616 | const enum ssl_encryption_level_t *level = a3; |
| 617 | |
| 618 | if (alert) |
| 619 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 620 | if (level) |
| 621 | chunk_appendf(&trace_buf, " el=%c", |
| 622 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 623 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 624 | |
| 625 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 626 | const size_t *sz1 = a2; |
| 627 | const size_t *sz2 = a3; |
| 628 | const size_t *sz3 = a4; |
| 629 | |
| 630 | if (sz1) |
| 631 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 632 | if (sz2) |
| 633 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 634 | if (sz3) |
| 635 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 636 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 637 | |
| 638 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 639 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 640 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 641 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 642 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 643 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 644 | } |
| 645 | if (mask & QUIC_EV_CONN_LPKT) { |
| 646 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 647 | const uint64_t *len = a3; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 648 | const struct quic_version *ver = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 649 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 650 | if (pkt) { |
| 651 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 652 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 653 | if (pkt->pn_node.key != (uint64_t)-1) |
| 654 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 655 | } |
| 656 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 657 | if (len) |
| 658 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 659 | |
| 660 | if (ver) |
| 661 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 664 | if (mask & QUIC_EV_STATELESS_RST) { |
| 665 | const struct quic_cid *cid = a2; |
| 666 | |
| 667 | if (cid) |
| 668 | quic_cid_dump(&trace_buf, cid); |
| 669 | } |
| 670 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* 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] | 674 | 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] | 675 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 676 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 677 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 678 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 679 | return 1; |
| 680 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 681 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 682 | 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] | 683 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 684 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 685 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 686 | return 1; |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 692 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 693 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 694 | 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] | 695 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 696 | struct quic_pktns *pktns; |
| 697 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 698 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 699 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 700 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 701 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 702 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 703 | pktns = quic_loss_pktns(qc); |
| 704 | if (tick_isset(pktns->tx.loss_time)) { |
| 705 | qc->timer = pktns->tx.loss_time; |
| 706 | goto out; |
| 707 | } |
| 708 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 709 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 710 | * cancelled for a server which reached the anti-amplification limit. |
| 711 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 712 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 713 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 714 | 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] | 715 | qc->timer = TICK_ETERNITY; |
| 716 | goto out; |
| 717 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 718 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 719 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 720 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 721 | /* Timer cancellation. */ |
| 722 | qc->timer = TICK_ETERNITY; |
| 723 | goto out; |
| 724 | } |
| 725 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 726 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 727 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 728 | if (tick_isset(pto)) |
| 729 | qc->timer = pto; |
| 730 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 731 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 732 | if (tick_is_expired(qc->timer, now_ms)) { |
| 733 | 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] | 734 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 735 | } |
| 736 | else { |
| 737 | 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] | 738 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 739 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 740 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 741 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 744 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 745 | * connection. |
| 746 | * Return 1 if succeeded, 0 if not. |
| 747 | */ |
| 748 | static int quic_tls_key_update(struct quic_conn *qc) |
| 749 | { |
| 750 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 751 | struct quic_tls_secrets *rx, *tx; |
| 752 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 753 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 754 | const struct quic_version *ver = |
| 755 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 756 | |
| 757 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 758 | rx = &tls_ctx->rx; |
| 759 | tx = &tls_ctx->tx; |
| 760 | nxt_rx = &qc->ku.nxt_rx; |
| 761 | nxt_tx = &qc->ku.nxt_tx; |
| 762 | |
| 763 | /* Prepare new RX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 764 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 765 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 766 | 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] | 767 | return 0; |
| 768 | } |
| 769 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 770 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 771 | nxt_rx->key, nxt_rx->keylen, |
| 772 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 773 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 774 | 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] | 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /* Prepare new TX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 779 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 780 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 781 | 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] | 782 | return 0; |
| 783 | } |
| 784 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 785 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 786 | nxt_tx->key, nxt_tx->keylen, |
| 787 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 788 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 789 | 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] | 790 | return 0; |
| 791 | } |
| 792 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 793 | if (nxt_rx->ctx) { |
| 794 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 795 | nxt_rx->ctx = NULL; |
| 796 | } |
| 797 | |
| 798 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 799 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | if (nxt_tx->ctx) { |
| 804 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 805 | nxt_tx->ctx = NULL; |
| 806 | } |
| 807 | |
| 808 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 809 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 810 | return 0; |
| 811 | } |
| 812 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 813 | return 1; |
| 814 | } |
| 815 | |
| 816 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 817 | * Must be used after having updated them. |
| 818 | * Always succeeds. |
| 819 | */ |
| 820 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 821 | { |
| 822 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 823 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 824 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 825 | |
| 826 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 827 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 828 | curr_secret = tls_ctx->rx.secret; |
| 829 | curr_iv = tls_ctx->rx.iv; |
| 830 | curr_key = tls_ctx->rx.key; |
| 831 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 832 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 833 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 834 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 835 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 836 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 837 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 838 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 839 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 840 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 841 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 842 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 843 | qc->ku.prv_rx.secret = curr_secret; |
| 844 | qc->ku.prv_rx.iv = curr_iv; |
| 845 | qc->ku.prv_rx.key = curr_key; |
| 846 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 847 | |
| 848 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 849 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 850 | curr_secret = tls_ctx->tx.secret; |
| 851 | curr_iv = tls_ctx->tx.iv; |
| 852 | curr_key = tls_ctx->tx.key; |
| 853 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 854 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 855 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 856 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 857 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 858 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 859 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 860 | qc->ku.nxt_tx.secret = curr_secret; |
| 861 | qc->ku.nxt_tx.iv = curr_iv; |
| 862 | qc->ku.nxt_tx.key = curr_key; |
| 863 | } |
| 864 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 865 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 866 | const uint8_t *read_secret, |
| 867 | const uint8_t *write_secret, size_t secret_len) |
| 868 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 869 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 870 | 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] | 871 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 872 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 873 | const struct quic_version *ver = |
| 874 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 875 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 876 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 877 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 878 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 879 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 880 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 881 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 882 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 883 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 884 | 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] | 885 | goto err; |
| 886 | } |
| 887 | |
| 888 | rx = &tls_ctx->rx; |
| 889 | tx = &tls_ctx->tx; |
| 890 | |
| 891 | rx->aead = tx->aead = tls_aead(cipher); |
| 892 | rx->md = tx->md = tls_md(cipher); |
| 893 | rx->hp = tx->hp = tls_hp(cipher); |
| 894 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 895 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 896 | 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] | 897 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 898 | 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] | 899 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 902 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 903 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 904 | goto err; |
| 905 | } |
| 906 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 907 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 908 | * listener. Note that a listener derives only RX secrets for this |
| 909 | * level. |
| 910 | */ |
| 911 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 912 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 913 | |
| 914 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 915 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 916 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 917 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 918 | 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] | 919 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 920 | 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] | 921 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 924 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 925 | TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 926 | goto err; |
| 927 | } |
| 928 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 929 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 930 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 931 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 932 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 933 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 934 | /* 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] | 935 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 936 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 937 | 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] | 938 | goto err; |
| 939 | } |
| 940 | |
| 941 | memcpy(rx->secret, read_secret, secret_len); |
| 942 | rx->secretlen = secret_len; |
| 943 | memcpy(tx->secret, write_secret, secret_len); |
| 944 | tx->secretlen = secret_len; |
| 945 | /* Initialize all the secret keys lengths */ |
| 946 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 947 | /* Prepare the next key update */ |
| 948 | if (!quic_tls_key_update(qc)) |
| 949 | goto err; |
| 950 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 951 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 952 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 953 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 954 | no_secret: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 955 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 956 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 957 | |
| 958 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 959 | 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] | 960 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 961 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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 | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1041 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 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 | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1045 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1046 | frm->type = QUIC_FT_CRYPTO; |
| 1047 | frm->crypto.offset = cf_offset; |
| 1048 | frm->crypto.len = cf_len; |
| 1049 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1050 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1051 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | return len == 0; |
| 1055 | } |
| 1056 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1057 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1058 | * activity for <qc> will be interrupted. |
| 1059 | */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1060 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1061 | { |
| 1062 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1063 | return; |
| 1064 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1065 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1066 | qc->err.code = err.code; |
| 1067 | qc->err.app = err.app; |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1068 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1069 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1070 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1071 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1072 | { |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 1073 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1074 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1075 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1076 | } |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1077 | quic_set_connection_close(qc, quic_err_tls(alert)); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1078 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1079 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1082 | /* Set the application for <qc> QUIC connection. |
| 1083 | * Return 1 if succeeded, 0 if not. |
| 1084 | */ |
| 1085 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1086 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1087 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1088 | qc->app_ops = &h3_ops; |
| 1089 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1090 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1091 | else |
| 1092 | return 0; |
| 1093 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1094 | return 1; |
| 1095 | } |
| 1096 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1097 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1098 | * wants to provide the QUIC layer with CRYPTO data. |
| 1099 | * Returns 1 if succeeded, 0 if not. |
| 1100 | */ |
| 1101 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1102 | const uint8_t *data, size_t len) |
| 1103 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1104 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1105 | enum quic_tls_enc_level tel; |
| 1106 | struct quic_enc_level *qel; |
| 1107 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1108 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1109 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1110 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1111 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1112 | goto out; |
| 1113 | } |
| 1114 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1115 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1116 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1117 | 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] | 1118 | goto err; |
| 1119 | } |
| 1120 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1121 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1122 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1123 | 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] | 1124 | goto err; |
| 1125 | } |
| 1126 | |
| 1127 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1128 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1129 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1130 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1131 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1132 | return 1; |
| 1133 | |
| 1134 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1135 | 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] | 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | int ha_quic_flush_flight(SSL *ssl) |
| 1140 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1141 | 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] | 1142 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1143 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1144 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1145 | |
| 1146 | return 1; |
| 1147 | } |
| 1148 | |
| 1149 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1150 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1151 | 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] | 1152 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1153 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1154 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1155 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1156 | return 1; |
| 1157 | } |
| 1158 | |
| 1159 | /* QUIC TLS methods */ |
| 1160 | static SSL_QUIC_METHOD ha_quic_method = { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1161 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1162 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1163 | .flush_flight = ha_quic_flush_flight, |
| 1164 | .send_alert = ha_quic_send_alert, |
| 1165 | }; |
| 1166 | |
| 1167 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1168 | * Returns an error count. |
| 1169 | */ |
| 1170 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1171 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1172 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1173 | int cfgerr = 0; |
| 1174 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1175 | long options = |
| 1176 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1177 | SSL_OP_SINGLE_ECDH_USE | |
| 1178 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1179 | SSL_CTX *ctx; |
| 1180 | |
| 1181 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1182 | bind_conf->initial_ctx = ctx; |
| 1183 | |
| 1184 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1185 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1186 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1187 | 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] | 1188 | |
| 1189 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | e06f745 | 2022-06-16 15:06:44 +0200 | [diff] [blame] | 1190 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1191 | if (bind_conf->ssl_conf.early_data) { |
| 1192 | 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] | 1193 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1194 | } |
| 1195 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1196 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1197 | #else |
| 1198 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1199 | #endif |
| 1200 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1201 | #endif |
| 1202 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1203 | |
| 1204 | return cfgerr; |
| 1205 | } |
| 1206 | |
| 1207 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1208 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1209 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1210 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1211 | */ |
| 1212 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1213 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1214 | { |
| 1215 | uint64_t expected_pn = largest_pn + 1; |
| 1216 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1217 | uint64_t pn_hwin = pn_win / 2; |
| 1218 | uint64_t pn_mask = pn_win - 1; |
| 1219 | uint64_t candidate_pn; |
| 1220 | |
| 1221 | |
| 1222 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1223 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1224 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1225 | candidate_pn + pn_hwin <= expected_pn) |
| 1226 | return candidate_pn + pn_win; |
| 1227 | |
| 1228 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1229 | return candidate_pn - pn_win; |
| 1230 | |
| 1231 | return candidate_pn; |
| 1232 | } |
| 1233 | |
| 1234 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1235 | * cryptographic context. |
| 1236 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1237 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1238 | * <end> points to one byte past the end of this packet. |
| 1239 | * Returns 1 if succeeded, 0 if not. |
| 1240 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1241 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1242 | 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] | 1243 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1244 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1245 | { |
| 1246 | int ret, outlen, i, pnlen; |
| 1247 | uint64_t packet_number; |
| 1248 | uint32_t truncated_pn = 0; |
| 1249 | unsigned char mask[5] = {0}; |
| 1250 | unsigned char *sample; |
| 1251 | EVP_CIPHER_CTX *cctx; |
| 1252 | unsigned char *hp_key; |
| 1253 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1254 | /* Check there is enough data in this packet. */ |
| 1255 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1256 | 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] | 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | cctx = EVP_CIPHER_CTX_new(); |
| 1261 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1262 | 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] | 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | ret = 0; |
| 1267 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1268 | |
| 1269 | hp_key = tls_ctx->rx.hp_key; |
| 1270 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1271 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1272 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1273 | 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] | 1274 | goto out; |
| 1275 | } |
| 1276 | |
| 1277 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1278 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1279 | for (i = 0; i < pnlen; i++) { |
| 1280 | pn[i] ^= mask[i + 1]; |
| 1281 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1282 | } |
| 1283 | |
| 1284 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1285 | /* Store remaining information for this unprotected header */ |
| 1286 | pkt->pn = packet_number; |
| 1287 | pkt->pnl = pnlen; |
| 1288 | |
| 1289 | ret = 1; |
| 1290 | |
| 1291 | out: |
| 1292 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1293 | |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
| 1297 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1298 | * address, with <payload_len> as payload length, <aad> as address of |
| 1299 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1300 | * context. |
| 1301 | * Returns 1 if succeeded, 0 if not. |
| 1302 | */ |
| 1303 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1304 | 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] | 1305 | 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] | 1306 | { |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1307 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1308 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1309 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1310 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1311 | |
| 1312 | 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] | 1313 | 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] | 1314 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1318 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1319 | 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] | 1320 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1324 | |
| 1325 | err: |
| 1326 | 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] | 1327 | 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] | 1328 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1332 | * Returns 1 if succeeded, 0 if not. |
| 1333 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1334 | 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] | 1335 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1336 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1337 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1338 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1339 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1340 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1341 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1342 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1343 | |
| 1344 | kp_changed = 0; |
| 1345 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1346 | /* The two tested bits are not at the same position, |
| 1347 | * this is why they are first both inversed. |
| 1348 | */ |
| 1349 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1350 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1351 | /* The lowest packet number of a previous key phase |
| 1352 | * cannot be null if it really stores previous key phase |
| 1353 | * secrets. |
| 1354 | */ |
| 1355 | if (!pkt->qc->ku.prv_rx.pn) |
| 1356 | return 0; |
| 1357 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1358 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1359 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1360 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1361 | } |
| 1362 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1363 | /* Next key phase */ |
| 1364 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1365 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1366 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1367 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1373 | return 0; |
| 1374 | |
| 1375 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1376 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1377 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1378 | if (!ret) |
| 1379 | return 0; |
| 1380 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1381 | /* Update the keys only if the packet decryption succeeded. */ |
| 1382 | if (kp_changed) { |
| 1383 | quic_tls_rotate_keys(pkt->qc); |
| 1384 | /* Toggle the Key Phase bit */ |
| 1385 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1386 | /* Store the lowest packet number received for the current key phase */ |
| 1387 | tls_ctx->rx.pn = pkt->pn; |
| 1388 | /* Prepare the next key update */ |
| 1389 | if (!quic_tls_key_update(pkt->qc)) |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1393 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1394 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1395 | |
| 1396 | return 1; |
| 1397 | } |
| 1398 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1399 | |
| 1400 | /* Remove references to <frm> frame */ |
| 1401 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1402 | { |
| 1403 | uint64_t pn; |
| 1404 | struct quic_frame *f, *tmp; |
| 1405 | |
| 1406 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1407 | pn = f->pkt->pn_node.key; |
| 1408 | f->origin = NULL; |
| 1409 | LIST_DELETE(&f->ref); |
| 1410 | TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1411 | } |
| 1412 | } |
| 1413 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1414 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1415 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1416 | { |
| 1417 | uint64_t pn; |
| 1418 | struct quic_frame *origin, *f, *tmp; |
| 1419 | |
| 1420 | /* Identify this frame: a frame copy or one of its copies */ |
| 1421 | origin = frm->origin ? frm->origin : frm; |
| 1422 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1423 | * possibly a copy of <origin> |
| 1424 | */ |
| 1425 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1426 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1427 | * not release the packets (releasing the frames) at this time as |
| 1428 | * they are possibly also to be acknowledged alongside the |
| 1429 | * the current one. |
| 1430 | */ |
| 1431 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1432 | pn = f->pkt->pn_node.key; |
| 1433 | TRACE_PROTO("mark frame as acked from packet", |
| 1434 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1435 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1436 | f->origin = NULL; |
| 1437 | LIST_DELETE(&f->ref); |
| 1438 | } |
| 1439 | LIST_DELETE(&frm->list); |
| 1440 | pn = frm->pkt->pn_node.key; |
| 1441 | quic_tx_packet_refdec(frm->pkt); |
| 1442 | TRACE_PROTO("freeing frame from packet", |
| 1443 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1444 | pool_free(pool_head_quic_frame, frm); |
| 1445 | } |
| 1446 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1447 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1448 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1449 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1450 | * |
| 1451 | * TODO this should also be called on lost packet detection |
| 1452 | */ |
| 1453 | static void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1454 | { |
| 1455 | struct ssl_sock_ctx *ctx = qc->xprt_ctx; |
| 1456 | |
| 1457 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1458 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1459 | quic_set_connection_close(qc, qc->err); |
| 1460 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1464 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1465 | * |
| 1466 | * 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] | 1467 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1468 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1469 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1470 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1471 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1472 | struct eb64_node *frm_node; |
| 1473 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1474 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1475 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1476 | while (frm_node) { |
| 1477 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1478 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1479 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1480 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1481 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1482 | offset = strm->offset.key; |
| 1483 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1484 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1485 | if (offset > stream->ack_offset) |
| 1486 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1487 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1488 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1489 | /* cf. next comment : frame may be freed at this stage. */ |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1490 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1491 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1492 | ret = 1; |
| 1493 | } |
| 1494 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1495 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1496 | * has been freed. with the stream frames tree. Nothing to do |
| 1497 | * anymore in here. |
| 1498 | */ |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1499 | if (!stream) { |
| 1500 | qc_check_close_on_released_mux(qc); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1501 | return 1; |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1502 | } |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1503 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1504 | frm_node = eb64_next(frm_node); |
| 1505 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1506 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1507 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1508 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1509 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1510 | |
| 1511 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1514 | /* 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] | 1515 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1516 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1517 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1518 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1519 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1520 | 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] | 1521 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1522 | switch (frm->type) { |
| 1523 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1524 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1525 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1526 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1527 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1528 | const size_t offset = strm_frm->offset.key; |
| 1529 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1530 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1531 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1532 | * might be freed at this stage. Use the id to do a proper |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1533 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1534 | * |
| 1535 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1536 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1537 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1538 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1539 | if (!node) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1540 | TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1541 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1542 | /* early return */ |
| 1543 | return; |
| 1544 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1545 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1546 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1547 | TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1548 | if (offset <= stream->ack_offset) { |
| 1549 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1550 | stream_acked = 1; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1551 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1552 | qc, strm_frm, stream); |
| 1553 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1554 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1555 | if (!stream) { |
| 1556 | /* no need to continue if stream freed. */ |
| 1557 | TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1558 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1559 | qc_check_close_on_released_mux(qc); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1560 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1563 | TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1564 | qc, strm_frm, stream); |
| 1565 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1566 | } |
| 1567 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1568 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1569 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1570 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1571 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1572 | } |
| 1573 | break; |
| 1574 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1575 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1576 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1577 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1578 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1579 | struct qcc *qcc = qc->qcc; |
| 1580 | |
| 1581 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1582 | tasklet_wakeup(qcc->subs->tasklet); |
| 1583 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1584 | if (!qcc->subs->events) |
| 1585 | qcc->subs = NULL; |
| 1586 | } |
| 1587 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1591 | * deallocating them, and their TX frames. |
| 1592 | * Returns the last node reached to be used for the next range. |
| 1593 | * May be NULL if <largest> node could not be found. |
| 1594 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1595 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1596 | struct eb_root *pkts, |
| 1597 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1598 | struct list *newly_acked_pkts, |
| 1599 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1600 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1601 | { |
| 1602 | struct eb64_node *node; |
| 1603 | struct quic_tx_packet *pkt; |
| 1604 | |
Frédéric Lécaille | 843399f | 2022-07-22 16:27:44 +0200 | [diff] [blame] | 1605 | node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1606 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1607 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1608 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1609 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1610 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1611 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1612 | 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] | 1613 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1614 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1615 | node = eb64_prev(node); |
| 1616 | eb64_delete(&pkt->pn_node); |
| 1617 | } |
| 1618 | |
| 1619 | return node; |
| 1620 | } |
| 1621 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1622 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1623 | * 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] | 1624 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1625 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1626 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1627 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1628 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1629 | struct quic_frame *frm, *frmbak; |
| 1630 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1631 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1632 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1633 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1634 | /* Only for debug */ |
| 1635 | uint64_t pn; |
| 1636 | |
| 1637 | /* First remove this frame from the packet it was attached to */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1638 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1639 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1640 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1641 | /* At this time, this frame is not freed but removed from its packet */ |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1642 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1643 | /* Remove any reference to this frame */ |
| 1644 | qc_frm_unref(qc, frm); |
| 1645 | switch (frm->type) { |
| 1646 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1647 | { |
| 1648 | struct quic_stream *strm_frm = &frm->stream; |
| 1649 | struct eb64_node *node = NULL; |
| 1650 | struct qc_stream_desc *stream_desc; |
| 1651 | |
| 1652 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1653 | if (!node) { |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 1654 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1655 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1656 | qc, frm, &pn); |
| 1657 | pool_free(pool_head_quic_frame, frm); |
| 1658 | continue; |
| 1659 | } |
| 1660 | |
| 1661 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1662 | /* Do not resend this frame if in the "already acked range" */ |
| 1663 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1664 | TRACE_PROTO("ignored frame in already acked range", |
| 1665 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1666 | continue; |
| 1667 | } |
| 1668 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1669 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1670 | TRACE_PROTO("updated partially acked frame", |
| 1671 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1672 | } |
| 1673 | |
| 1674 | break; |
| 1675 | } |
| 1676 | |
| 1677 | default: |
| 1678 | break; |
| 1679 | } |
| 1680 | |
| 1681 | /* Do not resend probing packet with old data */ |
| 1682 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1683 | TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1684 | qc, frm, &pn); |
| 1685 | if (frm->origin) |
| 1686 | LIST_DELETE(&frm->ref); |
| 1687 | pool_free(pool_head_quic_frame, frm); |
| 1688 | continue; |
| 1689 | } |
| 1690 | |
| 1691 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1692 | TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1693 | TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1694 | qc, frm, &pn); |
| 1695 | pool_free(pool_head_quic_frame, frm); |
| 1696 | } |
| 1697 | else { |
| 1698 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1699 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1700 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1701 | * will be performed to check the stream is not consumed or released. |
| 1702 | */ |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1703 | frm->flags = QUIC_FL_TX_FRAME_LOST; |
| 1704 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1705 | LIST_APPEND(&tmp, &frm->list); |
| 1706 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1709 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1710 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1711 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1712 | /* Free <pkt> TX packet and its attached frames. |
| 1713 | * This is the responsability of the caller to remove this packet of |
| 1714 | * any data structure it was possibly attached to. |
| 1715 | */ |
| 1716 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 1717 | { |
| 1718 | struct quic_frame *frm, *frmbak; |
| 1719 | |
| 1720 | if (!pkt) |
| 1721 | return; |
| 1722 | |
| 1723 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1724 | LIST_DELETE(&frm->list); |
| 1725 | pool_free(pool_head_quic_frame, frm); |
| 1726 | } |
| 1727 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1728 | } |
| 1729 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1730 | /* Free the TX packets of <pkts> list */ |
| 1731 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1732 | { |
| 1733 | struct quic_tx_packet *pkt, *tmp; |
| 1734 | |
| 1735 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1736 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1737 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1738 | free_quic_tx_packet(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1742 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1743 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1744 | * updating the range which contains <largest_acked_pn>. |
| 1745 | * Never fails. |
| 1746 | */ |
| 1747 | static void qc_treat_ack_of_ack(struct quic_pktns *pktns, |
| 1748 | int64_t largest_acked_pn) |
| 1749 | { |
| 1750 | struct eb64_node *ar, *next_ar; |
| 1751 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1752 | |
| 1753 | ar = eb64_first(&arngs->root); |
| 1754 | while (ar) { |
| 1755 | struct quic_arng_node *ar_node; |
| 1756 | |
| 1757 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1758 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1759 | if ((int64_t)ar_node->first.key > largest_acked_pn) |
| 1760 | break; |
| 1761 | |
| 1762 | if (largest_acked_pn < ar_node->last) { |
| 1763 | eb64_delete(ar); |
| 1764 | ar_node->first.key = largest_acked_pn + 1; |
| 1765 | eb64_insert(&arngs->root, ar); |
| 1766 | break; |
| 1767 | } |
| 1768 | |
| 1769 | eb64_delete(ar); |
| 1770 | pool_free(pool_head_quic_arng, ar_node); |
| 1771 | arngs->sz--; |
| 1772 | ar = next_ar; |
| 1773 | } |
| 1774 | } |
| 1775 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1776 | /* Send a packet ack event nofication for each newly acked packet of |
| 1777 | * <newly_acked_pkts> list and free them. |
| 1778 | * Always succeeds. |
| 1779 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1780 | 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] | 1781 | struct list *newly_acked_pkts) |
| 1782 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1783 | struct quic_tx_packet *pkt, *tmp; |
| 1784 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1785 | |
| 1786 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1787 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1788 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1789 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1790 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1791 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1792 | /* If this packet contained an ACK frame, proceed to the |
| 1793 | * acknowledging of range of acks from the largest acknowledged |
| 1794 | * packet number which was sent in an ACK frame by this packet. |
| 1795 | */ |
| 1796 | if (pkt->largest_acked_pn != -1) |
| 1797 | 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] | 1798 | ev.ack.acked = pkt->in_flight_len; |
| 1799 | ev.ack.time_sent = pkt->time_sent; |
| 1800 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1801 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1802 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1803 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | } |
| 1807 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1808 | /* Release all the frames attached to <pktns> packet number space */ |
| 1809 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1810 | { |
| 1811 | struct quic_frame *frm, *frmbak; |
| 1812 | |
| 1813 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1814 | LIST_DELETE(&frm->list); |
| 1815 | pool_free(pool_head_quic_frame, frm); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1819 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1820 | * their TX frames. |
| 1821 | * Send a packet loss event to the congestion controller if |
| 1822 | * in flight packet have been lost. |
| 1823 | * Also frees the packet in <pkts> list. |
| 1824 | * Never fails. |
| 1825 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1826 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1827 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1828 | struct list *pkts, |
| 1829 | uint64_t now_us) |
| 1830 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1831 | 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] | 1832 | uint64_t lost_bytes; |
| 1833 | |
| 1834 | lost_bytes = 0; |
| 1835 | oldest_lost = newest_lost = NULL; |
| 1836 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1837 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1838 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1839 | lost_bytes += pkt->in_flight_len; |
| 1840 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1841 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1842 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1843 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1844 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1845 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1846 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1847 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1848 | if (!oldest_lost) { |
| 1849 | oldest_lost = newest_lost = pkt; |
| 1850 | } |
| 1851 | else { |
| 1852 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1853 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1854 | newest_lost = pkt; |
| 1855 | } |
| 1856 | } |
| 1857 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1858 | if (newest_lost) { |
| 1859 | /* Sent a congestion event to the controller */ |
Benoit DOLEZ | 69e3f05 | 2022-06-08 09:28:56 +0200 | [diff] [blame] | 1860 | struct quic_cc_event ev = { }; |
| 1861 | |
| 1862 | ev.type = QUIC_CC_EVT_LOSS; |
| 1863 | ev.loss.time_sent = newest_lost->time_sent; |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1864 | |
| 1865 | quic_cc_event(&qc->path->cc, &ev); |
| 1866 | } |
| 1867 | |
| 1868 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1869 | * We must check if we are experiencing a persistent congestion. |
| 1870 | * If this is the case, the congestion controller must re-enter |
| 1871 | * slow start state. |
| 1872 | */ |
| 1873 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1874 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1875 | |
| 1876 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1877 | now_ms, qc->max_ack_delay)) |
| 1878 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1879 | } |
| 1880 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1881 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1882 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1883 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1884 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1888 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1889 | * 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] | 1890 | * 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] | 1891 | * acked ack-eliciting packet. |
| 1892 | * Return 1, if succeeded, 0 if not. |
| 1893 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1894 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1895 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1896 | struct quic_enc_level *qel, |
| 1897 | unsigned int *rtt_sample, |
| 1898 | const unsigned char **pos, const unsigned char *end) |
| 1899 | { |
| 1900 | struct quic_ack *ack = &frm->ack; |
| 1901 | uint64_t smallest, largest; |
| 1902 | struct eb_root *pkts; |
| 1903 | struct eb64_node *largest_node; |
| 1904 | unsigned int time_sent, pkt_flags; |
| 1905 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1906 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1907 | |
| 1908 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1909 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1910 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1911 | goto err; |
| 1912 | } |
| 1913 | |
| 1914 | if (ack->first_ack_range > ack->largest_ack) { |
| 1915 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1916 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1917 | goto err; |
| 1918 | } |
| 1919 | |
| 1920 | largest = ack->largest_ack; |
| 1921 | smallest = largest - ack->first_ack_range; |
| 1922 | pkts = &qel->pktns->tx.pkts; |
| 1923 | pkt_flags = 0; |
| 1924 | largest_node = NULL; |
| 1925 | time_sent = 0; |
| 1926 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1927 | 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] | 1928 | largest_node = eb64_lookup(pkts, largest); |
| 1929 | if (!largest_node) { |
| 1930 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1931 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1932 | } |
| 1933 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1934 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1935 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1936 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1940 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1941 | do { |
| 1942 | uint64_t gap, ack_range; |
| 1943 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1944 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1945 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1946 | if (!ack->ack_range_num--) |
| 1947 | break; |
| 1948 | |
| 1949 | if (!quic_dec_int(&gap, pos, end)) |
| 1950 | goto err; |
| 1951 | |
| 1952 | if (smallest < gap + 2) { |
| 1953 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1954 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1955 | goto err; |
| 1956 | } |
| 1957 | |
| 1958 | largest = smallest - gap - 2; |
| 1959 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1960 | goto err; |
| 1961 | |
| 1962 | if (largest < ack_range) { |
| 1963 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1964 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1965 | goto err; |
| 1966 | } |
| 1967 | |
| 1968 | /* Do not use this node anymore. */ |
| 1969 | largest_node = NULL; |
| 1970 | /* Next range */ |
| 1971 | smallest = largest - ack_range; |
| 1972 | |
| 1973 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1974 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1975 | } while (1); |
| 1976 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1977 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1978 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 1979 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1983 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1984 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1985 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1986 | 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] | 1987 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1988 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1989 | if (quic_peer_validated_addr(qc)) |
| 1990 | qc->path->loss.pto_count = 0; |
| 1991 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | return 1; |
| 1996 | |
| 1997 | err: |
| 1998 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1999 | 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] | 2000 | return 0; |
| 2001 | } |
| 2002 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2003 | /* This function gives the detail of the SSL error. It is used only |
| 2004 | * if the debug mode and the verbose mode are activated. It dump all |
| 2005 | * the SSL error until the stack was empty. |
| 2006 | */ |
| 2007 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2008 | { |
| 2009 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2010 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2011 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2012 | unsigned long ret; |
| 2013 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2014 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2015 | ret = ERR_get_error(); |
| 2016 | if (!ret) |
| 2017 | return; |
| 2018 | |
| 2019 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2020 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2025 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2026 | const char **str, int *len); |
| 2027 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2028 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2029 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2030 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2031 | * Return 1 if succeeded, 0 if not. |
| 2032 | */ |
| 2033 | 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] | 2034 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2035 | const unsigned char *data, size_t len, |
| 2036 | struct quic_rx_packet *pkt, |
| 2037 | struct quic_rx_crypto_frm *cf) |
| 2038 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2039 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2040 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2041 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2042 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2043 | qc = ctx->qc; |
| 2044 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2045 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2046 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2047 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2048 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2049 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2050 | goto err; |
| 2051 | } |
| 2052 | |
| 2053 | el->rx.crypto.offset += len; |
| 2054 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2055 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2056 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2057 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2058 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2059 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2060 | if (ssl_err != 1) { |
| 2061 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2062 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2063 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2064 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2065 | goto out; |
| 2066 | } |
| 2067 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2068 | /* TODO: Should close the connection asap */ |
| 2069 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2070 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2071 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2072 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2073 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2074 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2075 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2076 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2077 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2078 | goto err; |
| 2079 | } |
| 2080 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2081 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | bc964bd | 2022-04-13 16:20:09 +0200 | [diff] [blame] | 2082 | |
| 2083 | /* Check the alpn could be negotiated */ |
| 2084 | if (!qc->app_ops) { |
| 2085 | TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2086 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2087 | goto err; |
| 2088 | } |
| 2089 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2090 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2091 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2092 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2093 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2094 | /* I/O callback switch */ |
| 2095 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2096 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2097 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2098 | /* The connection is ready to be accepted. */ |
| 2099 | quic_accept_push_qc(qc); |
| 2100 | } |
| 2101 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2102 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2103 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2104 | } else { |
| 2105 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2106 | if (ssl_err != 1) { |
| 2107 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2108 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2109 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2110 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2111 | goto out; |
| 2112 | } |
| 2113 | |
| 2114 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2115 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2116 | goto err; |
| 2117 | } |
| 2118 | |
| 2119 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2120 | QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2121 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2122 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2123 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2124 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2125 | return 1; |
| 2126 | |
| 2127 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2128 | 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] | 2129 | return 0; |
| 2130 | } |
| 2131 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2132 | /* Parse a STREAM frame <strm_frm> |
| 2133 | * |
| 2134 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2135 | * containing the frame must not be acknowledged. |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2136 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2137 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2138 | struct quic_stream *strm_frm, |
| 2139 | struct quic_conn *qc) |
| 2140 | { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2141 | int ret; |
| 2142 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2143 | /* RFC9000 13.1. Packet Processing |
| 2144 | * |
| 2145 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2146 | * successfully removed and all frames contained in the packet have |
| 2147 | * been processed. For STREAM frames, this means the data has been |
| 2148 | * enqueued in preparation to be received by the application protocol, |
| 2149 | * but it does not require that data be delivered and consumed. |
| 2150 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2151 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2152 | strm_frm->offset.key, strm_frm->fin, |
| 2153 | (char *)strm_frm->data); |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2154 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2155 | /* frame rejected - packet must not be acknowledeged */ |
| 2156 | if (ret) |
| 2157 | return 0; |
| 2158 | |
| 2159 | return 1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2160 | } |
| 2161 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2162 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2163 | * for <qc> QUIC connection. |
| 2164 | * This is a best effort function which never fails even if no memory could be |
| 2165 | * allocated to duplicate these frames. |
| 2166 | */ |
| 2167 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2168 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2169 | { |
| 2170 | struct quic_frame *frm, *frmbak; |
| 2171 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2172 | |
| 2173 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2174 | struct quic_frame *dup_frm, *origin; |
| 2175 | |
| 2176 | switch (frm->type) { |
| 2177 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2178 | { |
| 2179 | struct quic_stream *strm_frm = &frm->stream; |
| 2180 | struct eb64_node *node = NULL; |
| 2181 | struct qc_stream_desc *stream_desc; |
| 2182 | |
| 2183 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2184 | if (!node) { |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 2185 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2186 | continue; |
| 2187 | } |
| 2188 | |
| 2189 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2190 | /* Do not resend this frame if in the "already acked range" */ |
| 2191 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2192 | TRACE_PROTO("ignored frame frame in already acked range", |
| 2193 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2194 | continue; |
| 2195 | } |
| 2196 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2197 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2198 | TRACE_PROTO("updated partially acked frame", |
| 2199 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2200 | } |
| 2201 | |
| 2202 | break; |
| 2203 | } |
| 2204 | |
| 2205 | default: |
| 2206 | break; |
| 2207 | } |
| 2208 | |
| 2209 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2210 | if (!dup_frm) { |
| 2211 | TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2212 | break; |
| 2213 | } |
| 2214 | |
| 2215 | /* If <frm> is already a copy of another frame, we must take |
| 2216 | * its original frame as source for the copy. |
| 2217 | */ |
| 2218 | origin = frm->origin ? frm->origin : frm; |
| 2219 | TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2220 | *dup_frm = *origin; |
| 2221 | LIST_INIT(&dup_frm->reflist); |
| 2222 | TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM, |
| 2223 | qc, NULL, &origin->pkt->pn_node.key); |
| 2224 | dup_frm->origin = origin; |
| 2225 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2226 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2227 | } |
| 2228 | |
| 2229 | LIST_SPLICE(out_frm_list, &tmp); |
| 2230 | } |
| 2231 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2232 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2233 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2234 | struct quic_enc_level *qel, |
| 2235 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2236 | { |
| 2237 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2238 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2239 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2240 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2241 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2242 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2243 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2244 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2245 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2246 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2247 | node = eb64_next(node); |
| 2248 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2249 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2250 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2254 | return; |
| 2255 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2256 | /* When building a packet from another one, the field which may increase the |
| 2257 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2258 | */ |
| 2259 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2260 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2261 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2262 | return; |
| 2263 | } |
| 2264 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2265 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2266 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2267 | if (frms == frms1 && frms2) { |
| 2268 | frms = frms2; |
| 2269 | goto start; |
| 2270 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2273 | /* Prepare a fast retransmission during a handshake after a client |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2274 | * has resent Initial packets. According to the RFC a server may retransmit |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2275 | * Initial packets send them coalescing with others (Handshake here). |
| 2276 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2277 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2278 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2279 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2280 | { |
| 2281 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2282 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2283 | |
| 2284 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2285 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2286 | struct quic_enc_level *qel = iqel; |
| 2287 | struct eb_root *pkts; |
| 2288 | struct eb64_node *node; |
| 2289 | struct quic_tx_packet *pkt; |
| 2290 | struct list *tmp = &itmp; |
| 2291 | |
| 2292 | start: |
| 2293 | pkt = NULL; |
| 2294 | pkts = &qel->pktns->tx.pkts; |
| 2295 | node = eb64_first(pkts); |
| 2296 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2297 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2298 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2299 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2300 | break; |
| 2301 | node = eb64_next(node); |
| 2302 | } |
| 2303 | |
| 2304 | if (!pkt) |
| 2305 | goto end; |
| 2306 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2307 | /* When building a packet from another one, the field which may increase the |
| 2308 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2309 | */ |
| 2310 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2311 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2312 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2313 | goto end; |
| 2314 | } |
| 2315 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2316 | qel->pktns->tx.pto_probe += 1; |
| 2317 | requeue: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2318 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2319 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2320 | if (qel == iqel) { |
| 2321 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2322 | pkt = pkt->next; |
| 2323 | tmp = &htmp; |
| 2324 | hqel->pktns->tx.pto_probe += 1; |
| 2325 | goto requeue; |
| 2326 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2327 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2328 | |
| 2329 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2330 | LIST_SPLICE(ifrms, &itmp); |
| 2331 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2332 | } |
| 2333 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2334 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 2335 | { |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2336 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2337 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2338 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2339 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2340 | return; |
| 2341 | |
| 2342 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2343 | } |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2346 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2347 | * frame list to send. |
| 2348 | * Return 1 if succeeded, 0 if not. |
| 2349 | */ |
| 2350 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2351 | { |
| 2352 | struct quic_frame *frm; |
| 2353 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2354 | uint64_t app_error_code; |
| 2355 | |
| 2356 | /* TODO: the mux may be released, we cannot have more |
| 2357 | * information about the application error code to send |
| 2358 | * at this time. |
| 2359 | */ |
| 2360 | app_error_code = H3_REQUEST_REJECTED; |
| 2361 | |
| 2362 | frm = pool_zalloc(pool_head_quic_frame); |
| 2363 | if (!frm) |
| 2364 | return 0; |
| 2365 | |
| 2366 | frm->type = QUIC_FT_STOP_SENDING; |
| 2367 | frm->stop_sending.id = id; |
| 2368 | frm->stop_sending.app_error_code = app_error_code; |
| 2369 | LIST_INIT(&frm->reflist); |
| 2370 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 2371 | |
| 2372 | return 1; |
| 2373 | } |
| 2374 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2375 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2376 | * as I/O handler context and <qel> as encryption level. |
| 2377 | * Returns 1 if succeeded, 0 if failed. |
| 2378 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2379 | 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] | 2380 | struct quic_enc_level *qel) |
| 2381 | { |
| 2382 | struct quic_frame frm; |
| 2383 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2384 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2385 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2386 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2387 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2388 | /* Skip the AAD */ |
| 2389 | pos = pkt->data + pkt->aad_len; |
| 2390 | end = pkt->data + pkt->len; |
| 2391 | |
| 2392 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2393 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2394 | goto err; |
| 2395 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2396 | 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] | 2397 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2398 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2399 | break; |
| 2400 | case QUIC_FT_PING: |
| 2401 | break; |
| 2402 | case QUIC_FT_ACK: |
| 2403 | { |
| 2404 | unsigned int rtt_sample; |
| 2405 | |
| 2406 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2407 | 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] | 2408 | goto err; |
| 2409 | |
| 2410 | if (rtt_sample) { |
| 2411 | unsigned int ack_delay; |
| 2412 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2413 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2414 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2415 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2416 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2417 | 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] | 2418 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2419 | break; |
| 2420 | } |
Frédéric Lécaille | e06ca65 | 2022-05-29 11:48:58 +0200 | [diff] [blame] | 2421 | case QUIC_FT_RESET_STREAM: |
| 2422 | /* TODO: handle this frame at STREAM level */ |
| 2423 | break; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2424 | case QUIC_FT_STOP_SENDING: |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2425 | { |
| 2426 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2427 | if (qc->mux_state == QC_MUX_READY) { |
| 2428 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2429 | stop_sending->app_error_code)) { |
| 2430 | goto err; |
| 2431 | } |
| 2432 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2433 | break; |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2434 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2435 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2436 | { |
| 2437 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2438 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2439 | 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] | 2440 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2441 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2442 | |
| 2443 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2444 | cfdebug.len = frm.crypto.len; |
| 2445 | TRACE_PROTO("CRYPTO data discarded", |
| 2446 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2447 | break; |
| 2448 | } |
| 2449 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2450 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2451 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2452 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2453 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2454 | |
| 2455 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2456 | cfdebug.len = frm.crypto.len; |
| 2457 | /* Nothing to do */ |
| 2458 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2459 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2460 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2461 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2462 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2463 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2464 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2465 | else { |
| 2466 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2467 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2468 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2469 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2470 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2471 | cfdebug.len = frm.crypto.len; |
| 2472 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2473 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2474 | frm.crypto.len -= diff; |
| 2475 | frm.crypto.data += diff; |
| 2476 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2477 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2478 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2479 | |
| 2480 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2481 | /* 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] | 2482 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2483 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2484 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2485 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2486 | if (!qc_provide_cdata(qel, ctx, |
| 2487 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2488 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2489 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2490 | |
| 2491 | break; |
| 2492 | } |
| 2493 | |
| 2494 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2495 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2496 | if (!cf) { |
| 2497 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2498 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2499 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2500 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2501 | |
| 2502 | cf->offset_node.key = frm.crypto.offset; |
| 2503 | cf->len = frm.crypto.len; |
| 2504 | cf->data = frm.crypto.data; |
| 2505 | cf->pkt = pkt; |
| 2506 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2507 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2508 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2509 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2510 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2511 | { |
| 2512 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2513 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2514 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2515 | /* The upper layer may not be allocated. */ |
| 2516 | if (qc->mux_state != QC_MUX_READY) { |
| 2517 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2518 | TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2519 | break; |
| 2520 | } |
| 2521 | else { |
| 2522 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2523 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
| 2524 | TRACE_PROTO("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2525 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2526 | goto err; |
| 2527 | } |
| 2528 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2529 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2530 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2531 | goto err; |
| 2532 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2533 | break; |
| 2534 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2535 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2536 | if (qc->mux_state == QC_MUX_READY) { |
| 2537 | struct quic_max_data *data = &frm.max_data; |
| 2538 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2539 | } |
| 2540 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2541 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2542 | if (qc->mux_state == QC_MUX_READY) { |
| 2543 | struct quic_max_stream_data *data = &frm.max_stream_data; |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 2544 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2545 | data->max_stream_data)) { |
| 2546 | goto err; |
| 2547 | } |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2548 | } |
| 2549 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2550 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2551 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2552 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2553 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2554 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2555 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2556 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2557 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2558 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2559 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2560 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2561 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2562 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2563 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2564 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2565 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2566 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2567 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2568 | break; |
| 2569 | case QUIC_FT_CONNECTION_CLOSE: |
| 2570 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2571 | /* Increment the error counters */ |
| 2572 | qc_cc_err_count_inc(qc, &frm); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2573 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2574 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2575 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 2576 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2577 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2578 | TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2579 | /* RFC 9000 10.2. Immediate Close: |
| 2580 | * The closing and draining connection states exist to ensure |
| 2581 | * that connections close cleanly and that delayed or reordered |
| 2582 | * packets are properly discarded. These states SHOULD persist |
| 2583 | * for at least three times the current PTO interval... |
| 2584 | * |
| 2585 | * Rearm the idle timeout only one time when entering draining |
| 2586 | * state. |
| 2587 | */ |
| 2588 | qc_idle_timer_do_rearm(qc); |
| 2589 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2590 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2591 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2592 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2593 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2594 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2595 | goto err; |
| 2596 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2597 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2598 | break; |
| 2599 | default: |
| 2600 | goto err; |
| 2601 | } |
| 2602 | } |
| 2603 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2604 | /* 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] | 2605 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2606 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2607 | if (fast_retrans) { |
| 2608 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2609 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2610 | |
| 2611 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2612 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2613 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2614 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2615 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2616 | * be discarded. |
| 2617 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2618 | 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] | 2619 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2620 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2621 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2622 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2623 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2624 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2625 | qc_set_timer(ctx->qc); |
| 2626 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2627 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2628 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2629 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2630 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2631 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2634 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2635 | return 1; |
| 2636 | |
| 2637 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2638 | 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] | 2639 | return 0; |
| 2640 | } |
| 2641 | |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2642 | /* Must be called only by a <cbuf> writer (packet builder). |
| 2643 | * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and |
| 2644 | * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer |
| 2645 | * index after having marked the end of written data. This the responsability |
| 2646 | * of the caller to ensure there is enough room in <cbuf> to write the end of |
| 2647 | * data made of a uint16_t null field. |
| 2648 | * |
| 2649 | * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused) |
| 2650 | * ^ ^ |
| 2651 | * r w |
| 2652 | * |
| 2653 | * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused) |
| 2654 | * ^ ^ |
| 2655 | * r w |
| 2656 | |
| 2657 | * +--------------------------------------+ (empty buffer, can be reused) |
| 2658 | * ^ |
| 2659 | * (r = w) |
| 2660 | * |
| 2661 | * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused) |
| 2662 | * ^ ^ |
| 2663 | * w r |
| 2664 | */ |
| 2665 | static int qc_may_reuse_cbuf(struct cbuf *cbuf) |
| 2666 | { |
| 2667 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2668 | |
| 2669 | /* We can reset the writer index only if in front of the reader index and |
| 2670 | * if the reader index is not null. Resetting the writer when the reader |
| 2671 | * index is null would empty the buffer. |
| 2672 | * XXX Note than the writer index cannot reach the reader index. |
| 2673 | * Only the reader index can reach the writer index. |
| 2674 | */ |
| 2675 | if (rd && rd <= cbuf->wr) { |
| 2676 | /* Mark the end of contiguous data for the reader */ |
| 2677 | write_u16(cb_wr(cbuf), 0); |
| 2678 | cb_add(cbuf, sizeof(uint16_t)); |
| 2679 | cb_wr_reset(cbuf); |
| 2680 | return 1; |
| 2681 | } |
| 2682 | |
| 2683 | return 0; |
| 2684 | } |
| 2685 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2686 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2687 | * 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] | 2688 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2689 | * This function must be called only after having built a correct datagram. |
| 2690 | * Always succeeds. |
| 2691 | */ |
| 2692 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2693 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2694 | { |
| 2695 | write_u16(cb_wr(cbuf), dglen); |
| 2696 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2697 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2698 | } |
| 2699 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2700 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2701 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2702 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2703 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2704 | * <force_ack> may be set to 1 if you want to force an ack. |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2705 | */ |
| 2706 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2707 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2708 | { |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2709 | unsigned int must_ack = force_ack || |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2710 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2711 | |
| 2712 | /* Do not build any more packet if the TX secrets are not available or |
| 2713 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2714 | * and if there is no more packets to send upon PTO expiration |
| 2715 | * and if there is no more ack-eliciting frames to send or in flight |
| 2716 | * congestion control limit is reached for prepared data |
| 2717 | */ |
| 2718 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2719 | (!cc && !probe && !must_ack && |
| 2720 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 2721 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
| 2725 | return 1; |
| 2726 | } |
| 2727 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2728 | /* 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] | 2729 | * ring buffer for the QUIC connection with <ctx> as I/O handler context from |
| 2730 | * <frms> list of prebuilt frames. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2731 | * A header made of two fields is added to each datagram: the datagram length followed |
| 2732 | * 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] | 2733 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2734 | * or -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2735 | */ |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2736 | static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr, |
| 2737 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2738 | { |
| 2739 | struct quic_enc_level *qel; |
| 2740 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2741 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2742 | struct quic_tx_packet *pkt; |
| 2743 | size_t total; |
| 2744 | size_t dg_headlen; |
| 2745 | |
| 2746 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2747 | /* Each datagram is prepended with its length followed by the |
| 2748 | * address of the first packet in the datagram. |
| 2749 | */ |
| 2750 | dg_headlen = sizeof(uint16_t) + sizeof pkt; |
| 2751 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2752 | total = 0; |
| 2753 | start: |
| 2754 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2755 | pos = cb_wr(cbuf); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2756 | /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer |
| 2757 | * to ensure there is enough room to mark the end of prepared |
| 2758 | * contiguous data with a zero length. |
| 2759 | */ |
| 2760 | end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t); |
| 2761 | 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] | 2762 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2763 | |
| 2764 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2765 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2766 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2767 | /* We do not probe if an immediate close was asked */ |
| 2768 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2769 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2770 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2771 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2772 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* Leave room for the datagram header */ |
| 2775 | pos += dg_headlen; |
| 2776 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2777 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2778 | } |
| 2779 | else { |
| 2780 | end = pos + qc->path->mtu; |
| 2781 | } |
| 2782 | |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2783 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 2784 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2785 | switch (err) { |
| 2786 | case -2: |
| 2787 | goto err; |
| 2788 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2789 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2790 | * MTU, we are here because of the congestion control window. There is |
| 2791 | * no need to try to reuse this buffer. |
| 2792 | */ |
| 2793 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2794 | default: |
| 2795 | break; |
| 2796 | } |
| 2797 | |
| 2798 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 2799 | if (!pkt) |
| 2800 | goto err; |
| 2801 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2802 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2803 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2804 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2805 | total += pkt->len; |
| 2806 | /* Set the current datagram as prepared into <cbuf>. */ |
| 2807 | qc_set_dg(cbuf, pkt->len, pkt); |
| 2808 | } |
| 2809 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2810 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2811 | 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] | 2812 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2813 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2814 | goto start; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | out: |
| 2818 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2819 | return total; |
| 2820 | |
| 2821 | err: |
| 2822 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
| 2823 | return -1; |
| 2824 | } |
| 2825 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2826 | /* 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] | 2827 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2828 | * several packets in the same datagram. A header made of two fields is added |
| 2829 | * to each datagram: the datagram length followed by the address of the first |
| 2830 | * packet in this datagram. |
Frédéric Lécaille | 728b30d | 2022-03-10 17:42:58 +0100 | [diff] [blame] | 2831 | * Returns the number of bytes prepared in packets if succeeded (may be 0), |
| 2832 | * or -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2833 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2834 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2835 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2836 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2837 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2838 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2839 | struct cbuf *cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2840 | unsigned char *end_buf, *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2841 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2842 | /* length of datagrams */ |
| 2843 | uint16_t dglen; |
| 2844 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2845 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2846 | /* Each datagram is prepended with its length followed by the |
| 2847 | * address of the first packet in the datagram. |
| 2848 | */ |
| 2849 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2850 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2851 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2852 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2853 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2854 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2855 | qel = &qc->els[tel]; |
| 2856 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2857 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2858 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2859 | padding = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2860 | cbuf = qr->cbuf; |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 2861 | pos = cb_wr(cbuf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2862 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2863 | * to ensure there is enough room to mark the end of prepared |
| 2864 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2865 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2866 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2867 | first_pkt = prv_pkt = NULL; |
| 2868 | 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] | 2869 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2870 | enum quic_pkt_type pkt_type; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 2871 | struct quic_tls_ctx *tls_ctx; |
| 2872 | const struct quic_version *ver; |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2873 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 2874 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 2875 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2876 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2877 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2878 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2879 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2880 | /* We do not probe if an immediate close was asked */ |
| 2881 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2882 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2883 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2884 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2885 | if (prv_pkt) |
| 2886 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2887 | /* Let's select the next encryption level */ |
| 2888 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2889 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2890 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2891 | qel = &qc->els[tel]; |
| 2892 | /* Build a new datagram */ |
| 2893 | prv_pkt = NULL; |
| 2894 | continue; |
| 2895 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2896 | break; |
| 2897 | } |
| 2898 | |
| 2899 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2900 | if (!prv_pkt) { |
| 2901 | /* Leave room for the datagram header */ |
| 2902 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2903 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2904 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2905 | } |
| 2906 | else { |
| 2907 | end = pos + qc->path->mtu; |
| 2908 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 2911 | if (qc->negotiated_version) { |
| 2912 | ver = qc->negotiated_version; |
| 2913 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2914 | tls_ctx = &qc->negotiated_ictx; |
| 2915 | else |
| 2916 | tls_ctx = &qel->tls_ctx; |
| 2917 | } |
| 2918 | else { |
| 2919 | ver = qc->original_version; |
| 2920 | tls_ctx = &qel->tls_ctx; |
| 2921 | } |
| 2922 | |
| 2923 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2924 | qc, ver, dglen, pkt_type, |
| 2925 | force_ack, padding, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2926 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2927 | case -2: |
| 2928 | goto err; |
| 2929 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2930 | /* If there was already a correct packet present, set the |
| 2931 | * current datagram as prepared into <cbuf>. |
| 2932 | */ |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2933 | if (prv_pkt) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2934 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 05e30ee | 2022-02-28 16:55:32 +0100 | [diff] [blame] | 2935 | goto stop_build; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2936 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2937 | break; |
| 2938 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2939 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2940 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2941 | if (!cur_pkt) |
| 2942 | goto err; |
| 2943 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2944 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2945 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2946 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2947 | total += cur_pkt->len; |
| 2948 | /* keep trace of the first packet in the datagram */ |
| 2949 | if (!first_pkt) |
| 2950 | first_pkt = cur_pkt; |
| 2951 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2952 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2953 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2954 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 2955 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2956 | /* Let's say we have to build a new dgram */ |
| 2957 | prv_pkt = NULL; |
| 2958 | dglen += cur_pkt->len; |
| 2959 | /* Client: discard the Initial encryption keys as soon as |
| 2960 | * a handshake packet could be built. |
| 2961 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2962 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2963 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2964 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2965 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2966 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2967 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2968 | 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] | 2969 | 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] | 2970 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2971 | } |
| 2972 | /* If the data for the current encryption level have all been sent, |
| 2973 | * select the next level. |
| 2974 | */ |
| 2975 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 2976 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2977 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2978 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2979 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2980 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2981 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 2982 | frms = &qc->els[tel].pktns->tx.frms; |
| 2983 | else |
| 2984 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2985 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2986 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2987 | /* If there is data for the next level, do not |
| 2988 | * consume a datagram. |
| 2989 | */ |
| 2990 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2991 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2992 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2993 | /* If we have to build a new datagram, set the current datagram as |
| 2994 | * prepared into <cbuf>. |
| 2995 | */ |
| 2996 | if (!prv_pkt) { |
| 2997 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2998 | first_pkt = NULL; |
| 2999 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3000 | padding = 0; |
| 3001 | } |
| 3002 | 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] | 3003 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3004 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3005 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | stop_build: |
| 3010 | /* Reset <wr> writer index if in front of <rd> index */ |
| 3011 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3012 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 302c2b1 | 2022-03-14 12:21:03 +0100 | [diff] [blame] | 3013 | if (qc_may_reuse_cbuf(cbuf)) |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3014 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3018 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3019 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3020 | |
| 3021 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3022 | 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] | 3023 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | /* 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] | 3027 | * 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] | 3028 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3029 | 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] | 3030 | { |
| 3031 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3032 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3033 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3034 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3035 | cbuf = qr->cbuf; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3036 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3037 | while (cb_contig_data(cbuf)) { |
| 3038 | unsigned char *pos; |
| 3039 | struct buffer tmpbuf = { }; |
| 3040 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3041 | uint16_t dglen; |
| 3042 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | unsigned int time_sent; |
| 3044 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3045 | pos = cb_rd(cbuf); |
| 3046 | dglen = read_u16(pos); |
| 3047 | /* End of prepared datagrams. |
| 3048 | * Reset the reader index only if in front of the writer index. |
| 3049 | */ |
| 3050 | if (!dglen) { |
| 3051 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 3052 | |
| 3053 | if (wr && wr < cbuf->rd) { |
| 3054 | cb_rd_reset(cbuf); |
| 3055 | continue; |
| 3056 | } |
| 3057 | break; |
| 3058 | } |
| 3059 | |
| 3060 | pos += sizeof dglen; |
| 3061 | first_pkt = read_ptr(pos); |
| 3062 | pos += sizeof first_pkt; |
| 3063 | tmpbuf.area = (char *)pos; |
| 3064 | tmpbuf.size = tmpbuf.data = dglen; |
| 3065 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3066 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3067 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 3068 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3069 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3070 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3071 | qc->tx.bytes += tmpbuf.data; |
| 3072 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3073 | |
| 3074 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3075 | pkt->time_sent = time_sent; |
| 3076 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3077 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3078 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3079 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3080 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3081 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3082 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3083 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3084 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3085 | qc_notify_close(qc); |
| 3086 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3087 | /* RFC 9000 10.2. Immediate Close: |
| 3088 | * The closing and draining connection states exist to ensure |
| 3089 | * that connections close cleanly and that delayed or reordered |
| 3090 | * packets are properly discarded. These states SHOULD persist |
| 3091 | * for at least three times the current PTO interval... |
| 3092 | * |
| 3093 | * Rearm the idle timeout only one time when entering closing |
| 3094 | * state. |
| 3095 | */ |
| 3096 | qc_idle_timer_do_rearm(qc); |
| 3097 | if (qc->timer_task) { |
| 3098 | task_destroy(qc->timer_task); |
| 3099 | qc->timer_task = NULL; |
| 3100 | } |
| 3101 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3102 | qc->path->in_flight += pkt->in_flight_len; |
| 3103 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3104 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3105 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3106 | 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] | 3107 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3108 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3109 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3110 | } |
| 3111 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3112 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3113 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3114 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3115 | return 1; |
| 3116 | } |
| 3117 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3118 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3119 | * <salt> salt input. This is the cluster secret which will be derived |
| 3120 | * as HKDF input secret to generate this token. |
| 3121 | * Return 1 if succeeded, 0 if not. |
| 3122 | */ |
| 3123 | static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len, |
| 3124 | const unsigned char *salt, size_t saltlen) |
| 3125 | { |
| 3126 | /* Input secret */ |
| 3127 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3128 | size_t keylen = strlen(global.cluster_secret); |
| 3129 | /* Info */ |
| 3130 | const unsigned char label[] = "stateless token"; |
| 3131 | size_t labellen = sizeof label - 1; |
| 3132 | |
| 3133 | return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3134 | key, keylen, salt, saltlen, label, labellen); |
| 3135 | } |
| 3136 | |
| 3137 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3138 | * Returns 1 if succeeded, 0 if not. |
| 3139 | */ |
| 3140 | static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid) |
| 3141 | { |
| 3142 | if (global.cluster_secret) { |
| 3143 | /* Output secret */ |
| 3144 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3145 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3146 | /* Salt */ |
| 3147 | const unsigned char *cid = quic_cid->cid.data; |
| 3148 | size_t cidlen = quic_cid->cid.len; |
| 3149 | |
| 3150 | return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen); |
| 3151 | } |
| 3152 | else { |
| 3153 | return RAND_bytes(quic_cid->stateless_reset_token, |
| 3154 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3155 | } |
| 3156 | } |
| 3157 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3158 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3159 | * ebtree. |
| 3160 | * |
| 3161 | * The CID is randomly generated in part with the result altered to be |
| 3162 | * associated with the current thread ID. This means this function must only |
| 3163 | * be called by the quic_conn thread. |
| 3164 | * |
| 3165 | * Returns the new CID if succeeded, NULL if not. |
| 3166 | */ |
| 3167 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3168 | struct quic_conn *qc, |
| 3169 | int seq_num) |
| 3170 | { |
| 3171 | struct quic_connection_id *cid; |
| 3172 | |
| 3173 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3174 | if (!cid) |
| 3175 | return NULL; |
| 3176 | |
| 3177 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3178 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3179 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3180 | |
| 3181 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3182 | if (quic_stateless_reset_token_init(cid) != 1) |
| 3183 | goto err; |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3184 | |
| 3185 | cid->qc = qc; |
| 3186 | |
| 3187 | cid->seq_num.key = seq_num; |
| 3188 | cid->retire_prior_to = 0; |
| 3189 | /* insert the allocated CID in the quic_conn tree */ |
| 3190 | eb64_insert(root, &cid->seq_num); |
| 3191 | |
| 3192 | return cid; |
| 3193 | |
| 3194 | err: |
| 3195 | pool_free(pool_head_quic_connection_id, cid); |
| 3196 | return NULL; |
| 3197 | } |
| 3198 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3199 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3200 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3201 | * a HANDSHAKE_DONE frame. |
| 3202 | * Return 1 if succeeded, 0 if not. |
| 3203 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3204 | 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] | 3205 | { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3206 | int i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3207 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3208 | struct quic_frame *frm, *frmbak; |
| 3209 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3210 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3211 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3212 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3213 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3214 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3215 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3216 | if (!frm) |
| 3217 | return 0; |
| 3218 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3219 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3220 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3221 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3222 | } |
| 3223 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3224 | first = 1; |
| 3225 | max = qc->tx.params.active_connection_id_limit; |
| 3226 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3227 | struct quic_connection_id *cid; |
| 3228 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3229 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3230 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3231 | goto err; |
| 3232 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3233 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3234 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3235 | if (!cid) |
| 3236 | goto err; |
| 3237 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3238 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3239 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3240 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3241 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3242 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3243 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3244 | |
| 3245 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3246 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3247 | |
| 3248 | return 1; |
| 3249 | |
| 3250 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3251 | /* free the frames */ |
| 3252 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3253 | pool_free(pool_head_quic_frame, frm); |
| 3254 | |
| 3255 | node = eb64_first(&qc->cids); |
| 3256 | while (node) { |
| 3257 | struct quic_connection_id *cid; |
| 3258 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3259 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3260 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3261 | if (cid->seq_num.key >= max) |
| 3262 | break; |
| 3263 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3264 | if (cid->seq_num.key < first) |
| 3265 | continue; |
| 3266 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3267 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3268 | ebmb_delete(&cid->node); |
| 3269 | eb64_delete(&cid->seq_num); |
| 3270 | pool_free(pool_head_quic_connection_id, cid); |
| 3271 | } |
| 3272 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3273 | return 0; |
| 3274 | } |
| 3275 | |
| 3276 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 3277 | void quic_free_arngs(struct quic_arngs *arngs) |
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 | struct eb64_node *n; |
| 3280 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3281 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3282 | n = eb64_first(&arngs->root); |
| 3283 | while (n) { |
| 3284 | struct eb64_node *next; |
| 3285 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3286 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3287 | next = eb64_next(n); |
| 3288 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3289 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3290 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3291 | } |
| 3292 | } |
| 3293 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3294 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3295 | * descending order. |
| 3296 | */ |
| 3297 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3298 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3299 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3300 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3305 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3306 | * 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] | 3307 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3308 | 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] | 3309 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3310 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3311 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3312 | last = eb64_last(&arngs->root); |
| 3313 | while (last && arngs->enc_sz > limit) { |
| 3314 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3315 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3316 | prev = eb64_prev(last); |
| 3317 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3318 | return 0; |
| 3319 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3320 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3321 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3322 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3323 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3324 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3325 | --arngs->sz; |
| 3326 | eb64_delete(last); |
| 3327 | pool_free(pool_head_quic_arng, last); |
| 3328 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | return 1; |
| 3332 | } |
| 3333 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3334 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3335 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 3336 | { |
| 3337 | struct eb64_node *node, *next; |
| 3338 | struct quic_arng_node *ar, *ar_next; |
| 3339 | |
| 3340 | node = eb64_last(&arngs->root); |
| 3341 | if (!node) |
| 3342 | return; |
| 3343 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3344 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3345 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3346 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3347 | |
| 3348 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3349 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3350 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3351 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3352 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3353 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3357 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3358 | * 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] | 3359 | */ |
| 3360 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3361 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 3362 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3363 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3364 | struct quic_arng_node *new_ar; |
| 3365 | |
| 3366 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3367 | if (new_ar) { |
| 3368 | new_ar->first.key = ar->first; |
| 3369 | new_ar->last = ar->last; |
| 3370 | eb64_insert(&arngs->root, &new_ar->first); |
| 3371 | arngs->sz++; |
| 3372 | } |
| 3373 | |
| 3374 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | /* 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] | 3378 | * 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] | 3379 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3380 | * |
| 3381 | * Descending order |
| 3382 | * -------------> |
| 3383 | * range1 range2 |
| 3384 | * ..........|--------|..............|--------| |
| 3385 | * ^ ^ ^ ^ |
| 3386 | * | | | | |
| 3387 | * last1 first1 last2 first2 |
| 3388 | * ..........+--------+--------------+--------+...... |
| 3389 | * diff1 gap12 diff2 |
| 3390 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3391 | * To encode the previous list of ranges we must encode integers as follows in |
| 3392 | * descending order: |
| 3393 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3394 | * with diff1 = last1 - first1 |
| 3395 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3396 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3397 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3398 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3399 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 3400 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3401 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3402 | struct eb64_node *le; |
| 3403 | struct quic_arng_node *new_node; |
| 3404 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3405 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3406 | new = NULL; |
| 3407 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3408 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3409 | if (!new_node) |
| 3410 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3411 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3412 | goto out; |
| 3413 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3414 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3415 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3416 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3417 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3418 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3419 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3420 | |
| 3421 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3422 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3423 | else { |
| 3424 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3425 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3426 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3427 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 3428 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3429 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3431 | if (le_ar->last + 1 >= ar->first) { |
| 3432 | le_ar->last = ar->last; |
| 3433 | new = le; |
| 3434 | new_node = le_ar; |
| 3435 | } |
| 3436 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3437 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3438 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3439 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3440 | |
| 3441 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3442 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3443 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3444 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3445 | /* Verify that the new inserted node does not overlap the nodes |
| 3446 | * which follow it. |
| 3447 | */ |
| 3448 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3449 | struct eb64_node *next; |
| 3450 | struct quic_arng_node *next_node; |
| 3451 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3452 | while ((next = eb64_next(new))) { |
| 3453 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3454 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3455 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3456 | break; |
| 3457 | |
| 3458 | if (next_node->last > new_node->last) |
| 3459 | new_node->last = next_node->last; |
| 3460 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3461 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3462 | /* Decrement the size of these ranges. */ |
| 3463 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3464 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3465 | } |
| 3466 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3467 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3468 | quic_arngs_set_enc_sz(arngs); |
| 3469 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | return 1; |
| 3471 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3472 | /* Remove the header protection of packets at <el> encryption level. |
| 3473 | * Always succeeds. |
| 3474 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3475 | 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] | 3476 | { |
| 3477 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3478 | struct quic_rx_packet *pqpkt; |
| 3479 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3480 | struct quic_enc_level *app_qel; |
| 3481 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3482 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3483 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3484 | /* 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] | 3485 | 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] | 3486 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3487 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3488 | goto out; |
| 3489 | } |
| 3490 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3491 | 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] | 3492 | 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] | 3493 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3494 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3495 | 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] | 3496 | /* XXX TO DO XXX */ |
| 3497 | } |
| 3498 | else { |
| 3499 | /* The AAD includes the packet number field */ |
| 3500 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3501 | /* Store the packet into the tree of packets to decrypt. */ |
| 3502 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3503 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3504 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3505 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3506 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3507 | 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] | 3508 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3509 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3510 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3514 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3518 | * Return 1 if succeeded, 0 if not. |
| 3519 | */ |
| 3520 | 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] | 3521 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3522 | { |
| 3523 | struct eb64_node *node; |
| 3524 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3525 | node = eb64_first(&el->rx.crypto.frms); |
| 3526 | while (node) { |
| 3527 | struct quic_rx_crypto_frm *cf; |
| 3528 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3529 | cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3530 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3531 | break; |
| 3532 | |
| 3533 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3534 | goto err; |
| 3535 | |
| 3536 | node = eb64_next(node); |
| 3537 | quic_rx_packet_refdec(cf->pkt); |
| 3538 | eb64_delete(&cf->offset_node); |
| 3539 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3540 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3541 | return 1; |
| 3542 | |
| 3543 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3544 | 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] | 3545 | return 0; |
| 3546 | } |
| 3547 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3548 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3549 | * 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] | 3550 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3551 | * Return 1 if succeeded, 0 if not. |
| 3552 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3553 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3554 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3555 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3556 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3557 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3558 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3559 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3560 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3561 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3562 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3563 | qel = cur_el; |
| 3564 | next_tel: |
| 3565 | if (!qel) |
| 3566 | goto out; |
| 3567 | |
| 3568 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3569 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3570 | while (node) { |
| 3571 | struct quic_rx_packet *pkt; |
| 3572 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3573 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3574 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3575 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3576 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3577 | /* Drop the packet */ |
| 3578 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3579 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3580 | } |
| 3581 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3582 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3583 | /* Drop the packet */ |
| 3584 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3585 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 3586 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3587 | } |
| 3588 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3589 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3590 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3591 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3592 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3593 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3594 | qc_idle_timer_rearm(qc, 1); |
| 3595 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3596 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3597 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3598 | largest_pn_time_received = pkt->time_received; |
| 3599 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3600 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3601 | 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] | 3602 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3603 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3607 | eb64_delete(&pkt->pn_node); |
| 3608 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3609 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3610 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3611 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3612 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3613 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3614 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3615 | /* Update the largest acknowledged packet timestamps */ |
| 3616 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3617 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3618 | } |
| 3619 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3620 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3621 | goto err; |
| 3622 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3623 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3624 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3625 | qel = next_el; |
Frédéric Lécaille | 91a211f | 2022-05-24 10:54:42 +0200 | [diff] [blame] | 3626 | largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3627 | goto next_tel; |
| 3628 | } |
| 3629 | |
| 3630 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3631 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3632 | return 1; |
| 3633 | |
| 3634 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3635 | 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] | 3636 | return 0; |
| 3637 | } |
| 3638 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3639 | /* Check if it's possible to remove header protection for packets related to |
| 3640 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3641 | * |
| 3642 | * Return true if the operation is possible else false. |
| 3643 | */ |
| 3644 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3645 | { |
| 3646 | enum quic_tls_enc_level tel; |
| 3647 | |
| 3648 | if (!qel) |
| 3649 | return 0; |
| 3650 | |
| 3651 | tel = ssl_to_quic_enc_level(qel->level); |
| 3652 | |
| 3653 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3654 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3655 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3656 | return 0; |
| 3657 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3658 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3659 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3660 | return 0; |
| 3661 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3662 | /* 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] | 3663 | 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] | 3664 | qc->mux_state == QC_MUX_NULL) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3665 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3666 | |
| 3667 | return 1; |
| 3668 | } |
| 3669 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3670 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3671 | int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3672 | { |
| 3673 | int ret; |
| 3674 | struct qring *qr; |
| 3675 | |
| 3676 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3677 | if (!qr) |
| 3678 | /* Never happens */ |
| 3679 | return 1; |
| 3680 | |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3681 | /* Prepare and send packets until we could not further prepare packets. */ |
| 3682 | while (1) { |
| 3683 | if (old_data) |
| 3684 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3685 | ret = qc_prep_app_pkts(qc, qr, frms); |
| 3686 | if (ret == -1) |
| 3687 | goto err; |
| 3688 | else if (ret == 0) |
| 3689 | goto out; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3690 | |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3691 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3692 | goto err; |
| 3693 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3694 | |
| 3695 | out: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3696 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3697 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3698 | return 1; |
| 3699 | |
| 3700 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3701 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3702 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3703 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3707 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3708 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 3709 | * QUIC connection |
| 3710 | * Returns 1 if succeeded, 0 if not. |
| 3711 | */ |
| 3712 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3713 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3714 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3715 | { |
| 3716 | int ret; |
| 3717 | struct qring *qr; |
| 3718 | |
| 3719 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
| 3720 | if (!qr) |
| 3721 | /* Never happens */ |
| 3722 | return 1; |
| 3723 | |
| 3724 | if (old_data) |
| 3725 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3726 | ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms); |
| 3727 | if (ret == -1) |
| 3728 | goto err; |
| 3729 | else if (ret == 0) |
| 3730 | goto out; |
| 3731 | |
| 3732 | if (!qc_send_ppkts(qr, qc->xprt_ctx)) |
| 3733 | goto err; |
| 3734 | |
| 3735 | out: |
| 3736 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3737 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3738 | return 1; |
| 3739 | |
| 3740 | err: |
| 3741 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3742 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
| 3743 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc); |
| 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3748 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3749 | { |
| 3750 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3751 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3752 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3753 | |
| 3754 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3755 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 3756 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 3757 | |
| 3758 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 3759 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 3760 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3761 | if (!LIST_ISEMPTY(&ifrms)) { |
| 3762 | iqel->pktns->tx.pto_probe = 1; |
| 3763 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3764 | hqel->pktns->tx.pto_probe = 1; |
| 3765 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 3766 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 3767 | } |
| 3768 | } |
| 3769 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3770 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 3771 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 3772 | if (!LIST_ISEMPTY(&hfrms)) { |
| 3773 | hqel->pktns->tx.pto_probe = 1; |
| 3774 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 3775 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3776 | } |
| 3777 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3778 | } |
| 3779 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3780 | } |
| 3781 | else { |
| 3782 | int i; |
| 3783 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 3784 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 3785 | |
| 3786 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3787 | hqel->pktns->tx.pto_probe = 0; |
| 3788 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 3789 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 3790 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3791 | if (!LIST_ISEMPTY(&frms1)) { |
| 3792 | hqel->pktns->tx.pto_probe = 1; |
| 3793 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 3794 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 3795 | } |
| 3796 | } |
| 3797 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3798 | } |
| 3799 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 3800 | aqel->pktns->tx.pto_probe = 0; |
| 3801 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 3802 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 3803 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 3804 | if (!LIST_ISEMPTY(&frms1)) { |
| 3805 | aqel->pktns->tx.pto_probe = 1; |
| 3806 | qc_send_app_pkts(qc, 1, &frms1); |
| 3807 | } |
| 3808 | if (!LIST_ISEMPTY(&frms2)) { |
| 3809 | aqel->pktns->tx.pto_probe = 1; |
| 3810 | qc_send_app_pkts(qc, 1, &frms2); |
| 3811 | } |
| 3812 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3817 | /* QUIC connection packet handler task (post handshake) */ |
| 3818 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 3819 | { |
| 3820 | struct ssl_sock_ctx *ctx; |
| 3821 | struct quic_conn *qc; |
| 3822 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3823 | |
| 3824 | |
| 3825 | ctx = context; |
| 3826 | qc = ctx->qc; |
| 3827 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3828 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3829 | TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3830 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3831 | /* Retranmissions */ |
| 3832 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3833 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 3834 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3835 | qc_dgrams_retransmit(qc); |
| 3836 | } |
| 3837 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3838 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 3839 | qc_rm_hp_pkts(qc, qel); |
| 3840 | |
| 3841 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) |
| 3842 | goto err; |
| 3843 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3844 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3845 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3846 | goto out; |
| 3847 | |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3848 | if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms)) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3849 | goto err; |
| 3850 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3851 | out: |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3852 | return t; |
| 3853 | |
| 3854 | err: |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3855 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3856 | return t; |
| 3857 | } |
| 3858 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3859 | /* QUIC connection packet handler task. */ |
| 3860 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3861 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3862 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3863 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3864 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3865 | enum quic_tls_enc_level tel, next_tel; |
| 3866 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3867 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3868 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3869 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3870 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3871 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3872 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3873 | qr = NULL; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3874 | st = qc->state; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3875 | TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 3876 | |
| 3877 | /* Retranmissions */ |
| 3878 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 3879 | TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 3880 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 3881 | qc_dgrams_retransmit(qc); |
| 3882 | } |
| 3883 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3884 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3885 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3886 | /* The I/O handler has been woken up by the dgram listener |
| 3887 | * after the anti-amplification was reached. |
| 3888 | */ |
| 3889 | qc_set_timer(qc); |
| 3890 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3891 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3892 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3893 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3894 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3895 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3896 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA])); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3897 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3898 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3899 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3900 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3901 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3902 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3903 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3904 | } |
| 3905 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3906 | goto err; |
| 3907 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3908 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3909 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3910 | |
| 3911 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3912 | /* Treat packets waiting for header packet protection decryption */ |
| 3913 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3914 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3915 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3916 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3917 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3918 | if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3919 | goto err; |
| 3920 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3921 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 3922 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 3923 | goto out; |
| 3924 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 3925 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
| 3926 | !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
| 3927 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3928 | qel = next_qel; |
| 3929 | next_qel = NULL; |
| 3930 | goto next_level; |
| 3931 | } |
| 3932 | else { |
| 3933 | struct quic_rx_packet *pkt; |
| 3934 | struct mt_list *elt1, elt2; |
| 3935 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 3936 | |
| 3937 | /* Drop these 0-RTT packets */ |
| 3938 | TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3939 | mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) { |
| 3940 | MT_LIST_DELETE_SAFE(elt1); |
| 3941 | quic_rx_packet_refdec(pkt); |
| 3942 | } |
| 3943 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3944 | } |
| 3945 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3946 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3947 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3948 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3949 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3950 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3951 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3952 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3953 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3954 | /* Discard the Handshake keys. */ |
| 3955 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3956 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3957 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3958 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3959 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 3960 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3964 | /* There may be remaining handshake to build (acks) */ |
| 3965 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3966 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3967 | } |
| 3968 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3969 | if (!qr) |
| 3970 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 3971 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3972 | * be considered. |
| 3973 | */ |
| 3974 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0)) |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 3975 | goto err; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3976 | ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms, |
| 3977 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3978 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3979 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3980 | else if (ret == 0) |
| 3981 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3982 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3983 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3984 | goto err; |
| 3985 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3986 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3987 | /* Check if there is something to do for the next level. |
| 3988 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3989 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3990 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 3991 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3992 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3993 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3994 | goto next_level; |
| 3995 | } |
| 3996 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 3997 | out: |
| 3998 | if (qr) |
| 3999 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4000 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4001 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4002 | |
| 4003 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4004 | if (qr) |
| 4005 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4006 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4007 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4008 | } |
| 4009 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4010 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4011 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 4012 | { |
| 4013 | int i; |
| 4014 | |
| 4015 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4016 | if (qel->tx.crypto.bufs[i]) { |
| 4017 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4018 | qel->tx.crypto.bufs[i] = NULL; |
| 4019 | } |
| 4020 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4021 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4025 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4026 | * Returns 1 if succeeded, 0 if not. |
| 4027 | */ |
| 4028 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4029 | enum quic_tls_enc_level level) |
| 4030 | { |
| 4031 | struct quic_enc_level *qel; |
| 4032 | |
| 4033 | qel = &qc->els[level]; |
| 4034 | qel->level = quic_to_ssl_enc_level(level); |
| 4035 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4036 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4037 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 4038 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4039 | |
| 4040 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4041 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4042 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4043 | qel->rx.crypto.offset = 0; |
| 4044 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4045 | |
| 4046 | /* Allocate only one buffer. */ |
| 4047 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4048 | if (!qel->tx.crypto.bufs) |
| 4049 | goto err; |
| 4050 | |
| 4051 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4052 | if (!qel->tx.crypto.bufs[0]) |
| 4053 | goto err; |
| 4054 | |
| 4055 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4056 | qel->tx.crypto.nb_buf = 1; |
| 4057 | |
| 4058 | qel->tx.crypto.sz = 0; |
| 4059 | qel->tx.crypto.offset = 0; |
| 4060 | |
| 4061 | return 1; |
| 4062 | |
| 4063 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4064 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4068 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4069 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4070 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4071 | * This function must only be called by the thread responsible of the quic_conn |
| 4072 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4073 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4074 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4075 | { |
| 4076 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4077 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4078 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4079 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4080 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4081 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4082 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4083 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4084 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4085 | /* free remaining stream descriptors */ |
| 4086 | node = eb64_first(&qc->streams_by_id); |
| 4087 | while (node) { |
| 4088 | struct qc_stream_desc *stream; |
| 4089 | |
| 4090 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4091 | node = eb64_next(node); |
| 4092 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4093 | /* all streams attached to the quic-conn are released, so |
| 4094 | * qc_stream_desc_free will liberate the stream instance. |
| 4095 | */ |
| 4096 | BUG_ON(!stream->release); |
| 4097 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4098 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4099 | |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4100 | /* Purge Rx packet list. */ |
| 4101 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 4102 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 4103 | pool_free(pool_head_quic_rx_packet, pkt); |
| 4104 | } |
| 4105 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4106 | if (qc->idle_timer_task) { |
| 4107 | task_destroy(qc->idle_timer_task); |
| 4108 | qc->idle_timer_task = NULL; |
| 4109 | } |
| 4110 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4111 | if (qc->timer_task) { |
| 4112 | task_destroy(qc->timer_task); |
| 4113 | qc->timer_task = NULL; |
| 4114 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4115 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4116 | /* remove the connection from receiver cids trees */ |
| 4117 | ebmb_delete(&qc->odcid_node); |
| 4118 | ebmb_delete(&qc->scid_node); |
| 4119 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4120 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4121 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4122 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4123 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4124 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4125 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4126 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4127 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4128 | quic_tls_ku_free(qc); |
| 4129 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4130 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4131 | quic_conn_enc_level_uninit(&qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4132 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4133 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4134 | |
| 4135 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4136 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4137 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4138 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4139 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
Frédéric Lécaille | 8ddde4f | 2022-08-01 14:07:50 +0200 | [diff] [blame^] | 4140 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4141 | quic_free_arngs(&qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4142 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4143 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4144 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4145 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4146 | 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] | 4147 | } |
| 4148 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4149 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4150 | { |
| 4151 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4152 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4153 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4154 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4155 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4156 | /* Next application data can be dropped. */ |
| 4157 | qc->mux_state = QC_MUX_RELEASED; |
| 4158 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4159 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4160 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4161 | quic_conn_release(qc); |
| 4162 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE); |
| 4163 | return; |
| 4164 | } |
| 4165 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 4166 | qc_check_close_on_released_mux(qc); |
| 4167 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4168 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4169 | } |
| 4170 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4171 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4172 | 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] | 4173 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4174 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4175 | struct quic_conn *qc; |
| 4176 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4177 | |
| 4178 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4179 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4180 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4181 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4182 | task->expire = TICK_ETERNITY; |
| 4183 | pktns = quic_loss_pktns(qc); |
| 4184 | if (tick_isset(pktns->tx.loss_time)) { |
| 4185 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4186 | |
| 4187 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4188 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4189 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4190 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4191 | goto out; |
| 4192 | } |
| 4193 | |
| 4194 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4195 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | dafbde6 | 2022-04-26 13:54:28 +0200 | [diff] [blame] | 4196 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4197 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4198 | struct qcc *qcc = qc->qcc; |
| 4199 | |
| 4200 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4201 | tasklet_wakeup(qcc->subs->tasklet); |
| 4202 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4203 | if (!qcc->subs->events) |
| 4204 | qcc->subs = NULL; |
| 4205 | } |
| 4206 | else { |
| 4207 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4208 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4209 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4210 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 4211 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4212 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4213 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4214 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4215 | 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] | 4216 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4217 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4218 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4219 | 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] | 4220 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4221 | 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] | 4222 | iel->pktns->tx.pto_probe = 1; |
| 4223 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4224 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4225 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4226 | qc->path->loss.pto_count++; |
| 4227 | |
| 4228 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4229 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4230 | |
| 4231 | return task; |
| 4232 | } |
| 4233 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4234 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4235 | * one byte past the end of this buffer. This will extract the ODCID |
| 4236 | * which will be stored into <odcid> |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4237 | * |
| 4238 | * Returns 0 on success else non-zero. |
| 4239 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4240 | static int parse_retry_token(const unsigned char *token, const unsigned char *end, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4241 | struct quic_cid *odcid) |
| 4242 | { |
| 4243 | uint64_t odcid_len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4244 | uint32_t timestamp; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4245 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4246 | if (!quic_dec_int(&odcid_len, &token, end)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4247 | return 1; |
| 4248 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4249 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4250 | * When an Initial packet is sent by a client that has not previously |
| 4251 | * received an Initial or Retry packet from the server, the client |
| 4252 | * populates the Destination Connection ID field with an unpredictable |
| 4253 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4254 | */ |
| 4255 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) |
| 4256 | return 1; |
| 4257 | |
| 4258 | if (end - token < odcid_len + sizeof timestamp) |
| 4259 | return 1; |
| 4260 | |
| 4261 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4262 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4263 | return 1; |
| 4264 | |
| 4265 | memcpy(odcid->data, token, odcid_len); |
| 4266 | odcid->len = odcid_len; |
| 4267 | |
| 4268 | return 0; |
| 4269 | } |
| 4270 | |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4271 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4272 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4273 | * for QUIC servers (or haproxy listeners). |
| 4274 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4275 | * <token> the token found to be used for this connection with <token_len> as |
| 4276 | * length. <saddr> is the source address. |
| 4277 | * Returns the connection if succeeded, NULL if not. |
| 4278 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4279 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4280 | struct quic_cid *dcid, struct quic_cid *scid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4281 | const struct quic_cid *odcid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4282 | struct sockaddr_storage *saddr, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4283 | int server, int token, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4284 | { |
| 4285 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4286 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4287 | /* Initial CID. */ |
| 4288 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4289 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4290 | struct listener *l = NULL; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4291 | struct quic_cc_algo *cc_algo = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4292 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4293 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4294 | qc = pool_zalloc(pool_head_quic_conn); |
| 4295 | if (!qc) { |
| 4296 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4297 | goto err; |
| 4298 | } |
| 4299 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4300 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4301 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4302 | 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] | 4303 | goto err; |
| 4304 | } |
| 4305 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4306 | qc->cids = EB_ROOT; |
| 4307 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4308 | if (server) { |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4309 | struct proxy *prx; |
| 4310 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4311 | l = owner; |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4312 | prx = l->bind_conf->frontend; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4313 | cc_algo = l->bind_conf->quic_cc_algo; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4314 | |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4315 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4316 | &quic_stats_module); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4317 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4318 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4319 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4320 | qc->odcid.len = dcid->len; |
| 4321 | qc->odcid.addrlen = dcid->addrlen; |
| 4322 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4323 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4324 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4325 | if (scid->len) |
| 4326 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4327 | qc->dcid.len = scid->len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 4328 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4329 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4330 | } |
| 4331 | /* QUIC Client (outgoing connection to servers) */ |
| 4332 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4333 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4334 | if (dcid->len) |
| 4335 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4336 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4337 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4338 | qc->mux_state = QC_MUX_NULL; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 4339 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4340 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4341 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4342 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4343 | 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] | 4344 | goto err; |
| 4345 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4346 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4347 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4348 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4349 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4350 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4351 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4352 | qc->scid = icid->cid; |
| 4353 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4354 | /* Packet number spaces initialization. */ |
| 4355 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4356 | quic_pktns_init(&qc->pktns[i]); |
| 4357 | /* QUIC encryption level context initialization. */ |
| 4358 | 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] | 4359 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4360 | 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] | 4361 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4362 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4363 | /* Initialize the packet number space. */ |
| 4364 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4365 | } |
| 4366 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4367 | qc->original_version = qv; |
| 4368 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 4369 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4370 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4371 | /* TX part. */ |
| 4372 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4373 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4374 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4375 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4376 | /* RX part. */ |
| 4377 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4378 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 664741e | 2022-05-02 18:46:58 +0200 | [diff] [blame] | 4379 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4380 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4381 | |
| 4382 | qc->nb_pkt_for_cc = 1; |
| 4383 | qc->nb_pkt_since_cc = 0; |
| 4384 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4385 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4386 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4387 | 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] | 4388 | goto err; |
| 4389 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4390 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4391 | /* XXX TO DO: Only one path at this time. */ |
| 4392 | qc->path = &qc->paths[0]; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4393 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4394 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4395 | /* required to use MTLIST_IN_LIST */ |
| 4396 | MT_LIST_INIT(&qc->accept_list); |
| 4397 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4398 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4399 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 4400 | qc->sendto_err = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4401 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4402 | |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4403 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 4404 | icid->stateless_reset_token, |
| 4405 | dcid->data, dcid->len, |
| 4406 | qc->scid.data, qc->scid.len, |
| 4407 | odcid->data, odcid->len, token)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4408 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4409 | |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4410 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4411 | !quic_conn_init_timer(qc) || |
| 4412 | !quic_conn_init_idle_timer_task(qc)) |
| 4413 | goto err; |
| 4414 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4415 | if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx, |
| 4416 | qc->original_version, dcid->data, dcid->len, 1)) |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4417 | goto err; |
| 4418 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4419 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4420 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4421 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4422 | |
| 4423 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4424 | 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] | 4425 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4426 | if (qc) |
| 4427 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4428 | quic_conn_release(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4429 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | /* Initialize the timer task of <qc> QUIC connection. |
| 4433 | * Returns 1 if succeeded, 0 if not. |
| 4434 | */ |
| 4435 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4436 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4437 | /* Attach this task to the same thread ID used for the connection */ |
Willy Tarreau | 8716875 | 2022-06-13 16:31:53 +0200 | [diff] [blame] | 4438 | qc->timer_task = task_new_on(qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4439 | if (!qc->timer_task) |
| 4440 | return 0; |
| 4441 | |
| 4442 | qc->timer = TICK_ETERNITY; |
| 4443 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4444 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4445 | |
| 4446 | return 1; |
| 4447 | } |
| 4448 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4449 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4450 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4451 | { |
| 4452 | unsigned int expire; |
| 4453 | |
| 4454 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4455 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4456 | } |
| 4457 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4458 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4459 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4460 | */ |
| 4461 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4462 | { |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4463 | if (read) { |
| 4464 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4465 | } |
| 4466 | else { |
| 4467 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4468 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4469 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | /* The task handling the idle timeout */ |
| 4473 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4474 | { |
| 4475 | struct quic_conn *qc = ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4476 | struct quic_counters *prx_counters = qc->prx_counters; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4477 | unsigned int qc_flags = qc->flags; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4478 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4479 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4480 | * might free the quic-conn too early via quic_close(). |
| 4481 | */ |
| 4482 | qc_notify_close(qc); |
| 4483 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4484 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4485 | * responsible to call quic_close to release it. |
| 4486 | */ |
| 4487 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4488 | if (qc->mux_state != QC_MUX_READY) |
| 4489 | quic_conn_release(qc); |
| 4490 | |
| 4491 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4492 | * least clean some parts of it such as the tasklet. |
| 4493 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4494 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4495 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 4496 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4497 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4498 | } |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4499 | |
| 4500 | return NULL; |
| 4501 | } |
| 4502 | |
| 4503 | /* Initialize the idle timeout task for <qc>. |
| 4504 | * Returns 1 if succeeded, 0 if not. |
| 4505 | */ |
| 4506 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4507 | { |
| 4508 | qc->idle_timer_task = task_new_here(); |
| 4509 | if (!qc->idle_timer_task) |
| 4510 | return 0; |
| 4511 | |
| 4512 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4513 | qc->idle_timer_task->context = qc; |
| 4514 | qc_idle_timer_rearm(qc, 1); |
| 4515 | task_queue(qc->idle_timer_task); |
| 4516 | |
| 4517 | return 1; |
| 4518 | } |
| 4519 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4520 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4521 | * past one byte of this buffer. |
| 4522 | */ |
| 4523 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4524 | struct quic_rx_packet *pkt) |
| 4525 | { |
| 4526 | unsigned char dcid_len, scid_len; |
| 4527 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4528 | if (end == *buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4529 | return 0; |
| 4530 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4531 | /* Destination Connection ID Length */ |
| 4532 | dcid_len = *(*buf)++; |
| 4533 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 4534 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 4535 | /* XXX MUST BE DROPPED */ |
| 4536 | return 0; |
| 4537 | |
| 4538 | if (dcid_len) { |
| 4539 | /* Check that the length of this received DCID matches the CID lengths |
| 4540 | * of our implementation for non Initials packets only. |
| 4541 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4542 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4543 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4544 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4545 | return 0; |
| 4546 | |
| 4547 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4548 | } |
| 4549 | |
| 4550 | pkt->dcid.len = dcid_len; |
| 4551 | *buf += dcid_len; |
| 4552 | |
| 4553 | /* Source Connection ID Length */ |
| 4554 | scid_len = *(*buf)++; |
| 4555 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 4556 | /* XXX MUST BE DROPPED */ |
| 4557 | return 0; |
| 4558 | |
| 4559 | if (scid_len) |
| 4560 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4561 | pkt->scid.len = scid_len; |
| 4562 | *buf += scid_len; |
| 4563 | |
| 4564 | return 1; |
| 4565 | } |
| 4566 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4567 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 4568 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 4569 | { |
| 4570 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4571 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4572 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4573 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4574 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | /* 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] | 4578 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4579 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4580 | * the packet first byte. |
| 4581 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4582 | * Returns 1 if succeeded, 0 if not. |
| 4583 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4584 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4585 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4586 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4587 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4588 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4589 | { |
| 4590 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4591 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4592 | struct quic_enc_level *qel; |
| 4593 | /* Only for traces. */ |
| 4594 | struct quic_rx_packet *qpkt_trace; |
| 4595 | |
| 4596 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4597 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4598 | /* The packet number is here. This is also the start minus |
| 4599 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4600 | * protection. |
| 4601 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4602 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4603 | |
| 4604 | tel = quic_packet_type_enc_level(pkt->type); |
| 4605 | qel = &qc->els[tel]; |
| 4606 | |
| 4607 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4608 | /* Note that the following function enables us to unprotect the packet |
| 4609 | * number and its length subsequently used to decrypt the entire |
| 4610 | * packets. |
| 4611 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4612 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 4613 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 4614 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4615 | goto err; |
| 4616 | } |
| 4617 | |
| 4618 | /* The AAD includes the packet number field found at <pn>. */ |
| 4619 | pkt->aad_len = pn - beg + pkt->pnl; |
| 4620 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4621 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4622 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4623 | 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] | 4624 | /* If the packet number space has been discarded, this packet |
| 4625 | * will be not parsed. |
| 4626 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4627 | 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] | 4628 | goto out; |
| 4629 | } |
| 4630 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4631 | 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] | 4632 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 4633 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 4634 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4635 | } |
| 4636 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4637 | *el = qel; |
| 4638 | /* No reference counter incrementation here!!! */ |
| 4639 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 4640 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 4641 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 4642 | b_add(&qc->rx.buf, pkt->len); |
| 4643 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4644 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4645 | return 1; |
| 4646 | |
| 4647 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4648 | 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] | 4649 | return 0; |
| 4650 | } |
| 4651 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4652 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4653 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 4654 | * of this packet into <*version>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4655 | */ |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4656 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 4657 | unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4658 | int *long_header, uint32_t *version) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4659 | { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4660 | const unsigned char byte0 = **buf; |
| 4661 | |
| 4662 | (*buf)++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4663 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4664 | unsigned char type = |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4665 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4666 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4667 | *long_header = 1; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4668 | /* Version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4669 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4670 | return 0; |
| 4671 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4672 | if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4673 | pkt->type = type; |
| 4674 | } |
| 4675 | else { |
| 4676 | switch (type) { |
| 4677 | case 0: |
| 4678 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 4679 | break; |
| 4680 | case 1: |
| 4681 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 4682 | break; |
| 4683 | case 2: |
| 4684 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 4685 | break; |
| 4686 | case 3: |
| 4687 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 4688 | break; |
| 4689 | } |
| 4690 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4691 | } |
| 4692 | else { |
| 4693 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 4694 | *long_header = 0; |
| 4695 | } |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 4696 | |
| 4697 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4698 | } |
| 4699 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4700 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 4701 | * if supported or NULL if not. |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4702 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4703 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4704 | { |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4705 | int i; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4706 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4707 | for (i = 0; i < quic_versions_nb; i++) |
| 4708 | if (quic_versions[i].num == version) |
| 4709 | return &quic_versions[i]; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4710 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4711 | return NULL; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4712 | } |
| 4713 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4714 | /* |
| 4715 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4716 | * address <addr>. |
| 4717 | * Implementation of RFC9000 6. Version Negotiation |
| 4718 | * |
| 4719 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4720 | * |
| 4721 | * Returns 0 on success else non-zero |
| 4722 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4723 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4724 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4725 | { |
| 4726 | char buf[256]; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4727 | int i = 0, j; |
| 4728 | uint32_t version; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4729 | const socklen_t addrlen = get_addr_len(addr); |
| 4730 | |
| 4731 | /* |
| 4732 | * header form |
| 4733 | * long header, fixed bit to 0 for Version Negotiation |
| 4734 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4735 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4736 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4737 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4738 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4739 | /* null version for Version Negotiation */ |
| 4740 | buf[i++] = '\x00'; |
| 4741 | buf[i++] = '\x00'; |
| 4742 | buf[i++] = '\x00'; |
| 4743 | buf[i++] = '\x00'; |
| 4744 | |
| 4745 | /* source connection id */ |
| 4746 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4747 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4748 | i += pkt->scid.len; |
| 4749 | |
| 4750 | /* destination connection id */ |
| 4751 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4752 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4753 | i += pkt->dcid.len; |
| 4754 | |
| 4755 | /* supported version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4756 | for (j = 0; j < quic_versions_nb; j++) { |
| 4757 | version = htonl(quic_versions[j].num); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4758 | memcpy(&buf[i], &version, sizeof(version)); |
| 4759 | i += sizeof(version); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4760 | } |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4761 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4762 | |
| 4763 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4764 | return 1; |
| 4765 | |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4769 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 4770 | * from <fd> UDP socket to <dst> |
| 4771 | * Return 1 if succeeded, 0 if not. |
| 4772 | */ |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4773 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4774 | struct quic_rx_packet *rxpkt) |
| 4775 | { |
| 4776 | int pktlen, rndlen; |
| 4777 | unsigned char pkt[64]; |
| 4778 | const socklen_t addrlen = get_addr_len(dstaddr); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4779 | struct proxy *prx; |
| 4780 | struct quic_counters *prx_counters; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4781 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4782 | prx = l->bind_conf->frontend; |
| 4783 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4784 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 4785 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 4786 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 4787 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 4788 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 4789 | * connection ID length that it requests the peer to include in its packets, |
| 4790 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 4791 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 4792 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 4793 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 4794 | * than the packet it responds to. |
| 4795 | */ |
| 4796 | |
| 4797 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 4798 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 4799 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 4800 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 4801 | /* Put a header of random bytes */ |
| 4802 | if (RAND_bytes(pkt, rndlen) != 1) |
| 4803 | return 0; |
| 4804 | |
| 4805 | /* Clear the most significant bit, and set the second one */ |
| 4806 | *pkt = (*pkt & ~0x80) | 0x40; |
| 4807 | if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 4808 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 4809 | return 0; |
| 4810 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4811 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4812 | return 0; |
| 4813 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4814 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 4815 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 4816 | return 1; |
| 4817 | } |
| 4818 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4819 | /* QUIC server only function. |
| 4820 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 4821 | * This is the responsability of the caller to check <aad> size is big enough |
| 4822 | * to contain these data. |
| 4823 | * Return the number of bytes copied to <aad>. |
| 4824 | */ |
| 4825 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 4826 | uint32_t version, |
| 4827 | const struct quic_cid *cid, |
| 4828 | const struct sockaddr_storage *addr) |
| 4829 | { |
| 4830 | unsigned char *p; |
| 4831 | |
| 4832 | p = aad; |
| 4833 | memcpy(p, &version, sizeof version); |
| 4834 | p += sizeof version; |
| 4835 | p += quic_saddr_cpy(p, addr); |
| 4836 | memcpy(p, cid->data, cid->len); |
| 4837 | p += cid->len; |
| 4838 | |
| 4839 | return p - aad; |
| 4840 | } |
| 4841 | |
| 4842 | /* QUIC server only function. |
| 4843 | * Generate the token to be used in Retry packets. The token is written to |
| 4844 | * <buf> whith <len> as length. <odcid> is the original destination connection |
| 4845 | * ID and <dcid> is our side destination connection ID (or client source |
| 4846 | * connection ID). |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4847 | * Returns the length of the encoded token or 0 on error. |
| 4848 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4849 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 4850 | const uint32_t version, |
| 4851 | const struct quic_cid *odcid, |
| 4852 | const struct quic_cid *dcid, |
| 4853 | struct sockaddr_storage *addr) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4854 | { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4855 | unsigned char *p; |
| 4856 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4857 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4858 | size_t aadlen; |
| 4859 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 4860 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4861 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4862 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4863 | size_t seclen = strlen(global.cluster_secret); |
| 4864 | EVP_CIPHER_CTX *ctx = NULL; |
| 4865 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4866 | uint32_t timestamp = now_ms; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4867 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4868 | /* We copy the odcid into the token, prefixed by its one byte |
| 4869 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 4870 | * the random bytes used to derive the secret to encrypt the token. |
| 4871 | */ |
| 4872 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4873 | return 0; |
| 4874 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4875 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 4876 | if (RAND_bytes(salt, sizeof salt) != 1) |
| 4877 | goto err; |
| 4878 | |
| 4879 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 4880 | salt, sizeof salt, sec, seclen)) |
| 4881 | goto err; |
| 4882 | |
| 4883 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) |
| 4884 | goto err; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4885 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4886 | /* Token build */ |
| 4887 | p = buf; |
| 4888 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 4889 | *p++ = odcid->len; |
| 4890 | memcpy(p, odcid->data, odcid->len); |
| 4891 | p += odcid->len; |
| 4892 | write_u32(p, htonl(timestamp)); |
| 4893 | p += sizeof timestamp; |
| 4894 | |
| 4895 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4896 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) |
| 4897 | goto err; |
| 4898 | |
| 4899 | p += QUIC_TLS_TAG_LEN; |
| 4900 | memcpy(p, salt, sizeof salt); |
| 4901 | p += sizeof salt; |
| 4902 | EVP_CIPHER_CTX_free(ctx); |
| 4903 | |
| 4904 | return p - buf; |
| 4905 | |
| 4906 | err: |
| 4907 | if (ctx) |
| 4908 | EVP_CIPHER_CTX_free(ctx); |
| 4909 | return 0; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4910 | } |
| 4911 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4912 | /* QUIC server only function. |
| 4913 | * Check the validity of the Retry token from <token> buffer with <tokenlen> |
| 4914 | * as length. If valid, the ODCID of <qc> QUIC connection will be put |
| 4915 | * into <odcid> connection ID. <dcid> is our side destination connection ID |
| 4916 | * of client source connection ID. |
| 4917 | * Return 1 if succeeded, 0 if not. |
| 4918 | */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4919 | static int quic_retry_token_check(const unsigned char *token, size_t tokenlen, |
| 4920 | const struct quic_version *qv, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4921 | struct quic_cid *odcid, |
| 4922 | const struct quic_cid *dcid, |
| 4923 | struct quic_conn *qc, |
| 4924 | struct sockaddr_storage *addr) |
| 4925 | { |
| 4926 | unsigned char buf[128]; |
| 4927 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 4928 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 4929 | size_t aadlen; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4930 | const unsigned char *salt; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4931 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 4932 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 4933 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 4934 | size_t seclen = strlen(global.cluster_secret); |
| 4935 | EVP_CIPHER_CTX *ctx = NULL; |
| 4936 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 4937 | |
| 4938 | if (sizeof buf < tokenlen) |
| 4939 | return 0; |
| 4940 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4941 | aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4942 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 4943 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 4944 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 4945 | TRACE_PROTO("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4946 | return 0; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 4947 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4948 | |
| 4949 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) |
| 4950 | goto err; |
| 4951 | |
| 4952 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 4953 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 4954 | ctx, aead, key, iv)) { |
| 4955 | TRACE_PROTO("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4956 | goto err; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 4957 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4958 | |
| 4959 | if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 4960 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4961 | goto err; |
| 4962 | } |
| 4963 | |
| 4964 | EVP_CIPHER_CTX_free(ctx); |
| 4965 | return 1; |
| 4966 | |
| 4967 | err: |
| 4968 | if (ctx) |
| 4969 | EVP_CIPHER_CTX_free(ctx); |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4973 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4974 | * the Initial <pkt> packet. |
| 4975 | * |
| 4976 | * Returns 0 on success else non-zero. |
| 4977 | */ |
| 4978 | static int send_retry(int fd, struct sockaddr_storage *addr, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4979 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4980 | { |
| 4981 | unsigned char buf[128]; |
| 4982 | int i = 0, token_len; |
| 4983 | const socklen_t addrlen = get_addr_len(addr); |
| 4984 | struct quic_cid scid; |
| 4985 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4986 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 4987 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 4988 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4989 | /* version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4990 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 4991 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 4992 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 4993 | buf[i++] = *(unsigned char *)&qv->num; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4994 | |
| 4995 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4996 | buf[i++] = pkt->scid.len; |
| 4997 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4998 | i += pkt->scid.len; |
| 4999 | |
| 5000 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5001 | scid.len = QUIC_HAP_CID_LEN; |
| 5002 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 5003 | return 1; |
| 5004 | |
| 5005 | buf[i++] = scid.len; |
| 5006 | memcpy(&buf[i], scid.data, scid.len); |
| 5007 | i += scid.len; |
| 5008 | |
| 5009 | /* token */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5010 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5011 | &pkt->dcid, &pkt->scid, addr))) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5012 | return 1; |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 5013 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5014 | i += token_len; |
| 5015 | |
| 5016 | /* token integrity tag */ |
| 5017 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5018 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5019 | pkt->dcid.len, buf, i, qv)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5020 | return 1; |
| 5021 | } |
| 5022 | |
| 5023 | i += QUIC_TLS_TAG_LEN; |
| 5024 | |
| 5025 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5026 | return 1; |
| 5027 | |
| 5028 | return 0; |
| 5029 | } |
| 5030 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5031 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5032 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5033 | * concatenated to the <pkt> DCID field. |
| 5034 | * |
| 5035 | * Returns the instance or NULL if not found. |
| 5036 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5037 | 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] | 5038 | struct listener *l, |
| 5039 | struct sockaddr_storage *saddr) |
| 5040 | { |
| 5041 | struct quic_conn *qc = NULL; |
| 5042 | struct ebmb_node *node; |
| 5043 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5044 | /* set if the quic_conn is found in the second DCID tree */ |
| 5045 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5046 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5047 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5048 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5049 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5050 | /* DCIDs of first packets coming from multiple clients may have |
| 5051 | * the same values. Let's distinguish them by concatenating the |
| 5052 | * socket addresses. |
| 5053 | */ |
| 5054 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5055 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5056 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5057 | if (node) { |
| 5058 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5059 | goto end; |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5064 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5065 | * used. |
| 5066 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5067 | 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] | 5068 | if (!node) |
| 5069 | goto end; |
| 5070 | |
| 5071 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5072 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5073 | found_in_dcid = 1; |
| 5074 | |
| 5075 | end: |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5076 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5077 | * If already done, this is a noop. |
| 5078 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 5079 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5080 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5081 | |
| 5082 | return qc; |
| 5083 | } |
| 5084 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5085 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5086 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5087 | * parameters of this session. |
| 5088 | * This is the responsibility of the caller to check the validity of all the |
| 5089 | * pointers passed as parameter to this function. |
| 5090 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5091 | * CO_ER_SSL_NO_MEM. |
| 5092 | */ |
| 5093 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5094 | unsigned char *params, size_t params_len) |
| 5095 | { |
| 5096 | int retry; |
| 5097 | |
| 5098 | retry = 1; |
| 5099 | retry: |
| 5100 | *ssl = SSL_new(ssl_ctx); |
| 5101 | if (!*ssl) { |
| 5102 | if (!retry--) |
| 5103 | goto err; |
| 5104 | |
| 5105 | pool_gc(NULL); |
| 5106 | goto retry; |
| 5107 | } |
| 5108 | |
| 5109 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5110 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5111 | SSL_free(*ssl); |
| 5112 | *ssl = NULL; |
| 5113 | if (!retry--) |
| 5114 | goto err; |
| 5115 | |
| 5116 | pool_gc(NULL); |
| 5117 | goto retry; |
| 5118 | } |
| 5119 | |
| 5120 | return 0; |
| 5121 | |
| 5122 | err: |
| 5123 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5124 | return -1; |
| 5125 | } |
| 5126 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5127 | /* Finalize <qc> QUIC connection: |
| 5128 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 5129 | * - derive the secrets for this context, |
| 5130 | * - encode the transport parameters to be sent, |
| 5131 | * - set them into the TLS stack, |
| 5132 | * - initialize ->max_ack_delay and max_idle_timeout, |
| 5133 | * |
| 5134 | * MUST be called after having received the remote transport parameters. |
| 5135 | * Return 1 if succeeded, 0 if not. |
| 5136 | */ |
| 5137 | int qc_conn_finalize(struct quic_conn *qc, int server) |
| 5138 | { |
| 5139 | struct quic_transport_params *tx_tp = &qc->tx.params; |
| 5140 | struct quic_transport_params *rx_tp = &qc->rx.params; |
| 5141 | const struct quic_version *ver; |
| 5142 | |
| 5143 | if (tx_tp->version_information.negotiated_version && |
| 5144 | tx_tp->version_information.negotiated_version != qc->original_version) { |
| 5145 | qc->negotiated_version = |
| 5146 | qc->tx.params.version_information.negotiated_version; |
| 5147 | if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 5148 | qc->odcid.data, qc->odcid.len, !server)) |
| 5149 | return 0; |
| 5150 | |
| 5151 | ver = qc->negotiated_version; |
| 5152 | } |
| 5153 | else { |
| 5154 | ver = qc->original_version; |
| 5155 | } |
| 5156 | |
| 5157 | qc->enc_params_len = |
| 5158 | quic_transport_params_encode(qc->enc_params, |
| 5159 | qc->enc_params + sizeof qc->enc_params, |
| 5160 | &qc->rx.params, ver, 1); |
| 5161 | if (!qc->enc_params_len) |
| 5162 | return 0; |
| 5163 | |
| 5164 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) |
| 5165 | return 0; |
| 5166 | |
| 5167 | if (tx_tp->max_ack_delay) |
| 5168 | qc->max_ack_delay = tx_tp->max_ack_delay; |
| 5169 | |
| 5170 | if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout) |
| 5171 | qc->max_idle_timeout = |
| 5172 | QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5173 | else |
| 5174 | qc->max_idle_timeout = |
| 5175 | QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5176 | |
| 5177 | TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp); |
| 5178 | |
| 5179 | return 1; |
| 5180 | } |
| 5181 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5182 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5183 | * used to process <qc> received packets. The allocated context is stored in |
| 5184 | * <qc.xprt_ctx>. |
| 5185 | * |
| 5186 | * Returns 0 on success else non-zero. |
| 5187 | */ |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 5188 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5189 | { |
| 5190 | struct bind_conf *bc = qc->li->bind_conf; |
| 5191 | struct ssl_sock_ctx *ctx = NULL; |
| 5192 | |
| 5193 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 5194 | if (!ctx) |
| 5195 | goto err; |
| 5196 | |
| 5197 | ctx->wait_event.tasklet = tasklet_new(); |
| 5198 | if (!ctx->wait_event.tasklet) |
| 5199 | goto err; |
| 5200 | |
| 5201 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5202 | ctx->wait_event.tasklet->context = ctx; |
| 5203 | ctx->wait_event.events = 0; |
| 5204 | ctx->subs = NULL; |
| 5205 | ctx->xprt_ctx = NULL; |
| 5206 | ctx->qc = qc; |
| 5207 | |
| 5208 | /* Set tasklet tid based on the SCID selected by us for this |
| 5209 | * connection. The upper layer will also be binded on the same thread. |
| 5210 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 5211 | 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] | 5212 | |
| 5213 | if (qc_is_listener(qc)) { |
| 5214 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5215 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5216 | goto err; |
| 5217 | } |
| 5218 | |
| 5219 | /* Enabling 0-RTT */ |
| 5220 | if (bc->ssl_conf.early_data) |
| 5221 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5222 | |
| 5223 | SSL_set_accept_state(ctx->ssl); |
| 5224 | } |
| 5225 | |
| 5226 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5227 | |
| 5228 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5229 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5230 | |
| 5231 | return 0; |
| 5232 | |
| 5233 | err: |
| 5234 | if (ctx && ctx->wait_event.tasklet) |
| 5235 | tasklet_free(ctx->wait_event.tasklet); |
| 5236 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 5237 | |
| 5238 | return 1; |
| 5239 | } |
| 5240 | |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5241 | /* Check that all the bytes between <buf> included and <end> address |
| 5242 | * excluded are null. This is the responsability of the caller to |
| 5243 | * check that there is at least one byte between <buf> end <end>. |
| 5244 | * Return 1 if this all the bytes are null, 0 if not. |
| 5245 | */ |
| 5246 | static inline int quic_padding_check(const unsigned char *buf, |
| 5247 | const unsigned char *end) |
| 5248 | { |
| 5249 | while (buf < end && !*buf) |
| 5250 | buf++; |
| 5251 | |
| 5252 | return buf == end; |
| 5253 | } |
| 5254 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5255 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5256 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5257 | * with the information collected from the packet. |
| 5258 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5259 | * byte to enable the caller to run this function again to continue to parse |
| 5260 | * the remaing QUIC packets carried by the datagram. |
| 5261 | * Note that this function always sets this ->len value. If a paquet could |
| 5262 | * not be correctly found, ->len value will be set to the remaining number |
| 5263 | * bytes in the datagram to entirely consume this latter. |
| 5264 | */ |
| 5265 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5266 | struct quic_rx_packet *pkt, int first_pkt, |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 5267 | struct quic_dgram *dgram, struct list **tasklist_head) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5268 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5269 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5270 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5271 | struct listener *l; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5272 | struct proxy *prx; |
| 5273 | struct quic_counters *prx_counters; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5274 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5275 | int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5276 | size_t b_cspace; |
| 5277 | struct quic_enc_level *qel; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5278 | uint32_t version; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5279 | const struct quic_version *qv = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5280 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5281 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5282 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5283 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5284 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5285 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5286 | l = dgram->owner; |
| 5287 | prx = l->bind_conf->frontend; |
| 5288 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 5289 | /* This ist only to please to traces and distinguish the |
| 5290 | * packet with parsed packet number from others. |
| 5291 | */ |
| 5292 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5293 | if (end <= buf) { |
| 5294 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5295 | goto drop; |
| 5296 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5297 | |
| 5298 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5299 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5300 | if (!first_pkt && quic_padding_check(buf, end)) { |
| 5301 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 5302 | * That is what we called add padding out of QUIC packets. Such |
| 5303 | * datagrams must be considered as valid. But we can only consume |
| 5304 | * the remaining space. |
| 5305 | */ |
| 5306 | pkt->len = end - buf; |
| 5307 | goto drop_no_conn; |
| 5308 | } |
| 5309 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5310 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5311 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5314 | /* Header form */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5315 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5316 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5317 | goto drop; |
| 5318 | } |
| 5319 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5320 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5321 | uint64_t len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5322 | struct quic_cid odcid; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5323 | int check_token = 0; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5324 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5325 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5326 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5327 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5328 | } |
| 5329 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5330 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5331 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5332 | drop_no_conn = 1; |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5333 | } |
| 5334 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5335 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5336 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 5337 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5338 | } |
| 5339 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5340 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5341 | * they must have the same DCID. |
| 5342 | */ |
| 5343 | if (!first_pkt && |
| 5344 | (pkt->dcid.len != dgram->dcid_len || |
| 5345 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5346 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5347 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5348 | } |
| 5349 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5350 | /* Retry of Version Negotiation packets are only sent by servers */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5351 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5352 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5353 | goto drop; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5354 | } |
| 5355 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5356 | /* RFC9000 6. Version Negotiation */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5357 | qv = qc_supported_version(version); |
| 5358 | if (!qv) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5359 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5360 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5361 | TRACE_PROTO("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5362 | goto err; |
| 5363 | } |
| 5364 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5365 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5366 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5367 | } |
| 5368 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5369 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5370 | * there is no Initial connection IDs storage. |
| 5371 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5372 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5373 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5374 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5375 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5376 | end - buf < token_len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5377 | TRACE_PROTO("Packet dropped", |
| 5378 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5379 | goto drop; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5380 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5381 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5382 | /* TODO Retry should be automatically activated if |
| 5383 | * suspect network usage is detected. |
| 5384 | */ |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5385 | if (global.cluster_secret) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5386 | if (!token_len) { |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5387 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5388 | TRACE_PROTO("Initial without token, sending retry", |
| 5389 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5390 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5391 | TRACE_PROTO("Error during Retry generation", |
| 5392 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5393 | goto err; |
| 5394 | } |
| 5395 | |
| 5396 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5397 | goto err; |
| 5398 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5399 | } |
| 5400 | else { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5401 | check_token = 1; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5402 | } |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5403 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5404 | |
| 5405 | pkt->token = buf; |
| 5406 | pkt->token_len = token_len; |
| 5407 | buf += pkt->token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5408 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5409 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5410 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5411 | TRACE_PROTO("Packet dropped", |
| 5412 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5413 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5414 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5415 | } |
| 5416 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5417 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5418 | end - buf < len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5419 | TRACE_PROTO("Packet dropped", |
| 5420 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5421 | goto drop; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5422 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5423 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5424 | payload = buf; |
| 5425 | pkt->len = len + payload - beg; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5426 | if (drop_no_conn) |
| 5427 | goto drop_no_conn; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5428 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5429 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5430 | if (check_token && pkt->token) { |
| 5431 | if (*pkt->token == QUIC_TOKEN_FMT_RETRY) { |
| 5432 | const struct quic_version *ver = qc ? qc->original_version : qv; |
| 5433 | if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid, |
| 5434 | &pkt->scid, qc, &dgram->saddr)) { |
| 5435 | HA_ATOMIC_INC(&prx_counters->retry_error); |
| 5436 | TRACE_PROTO("Wrong retry token", |
| 5437 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5438 | /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection |
| 5439 | * with an INVALID_TOKEN error. |
| 5440 | */ |
| 5441 | goto drop; |
| 5442 | } |
| 5443 | |
| 5444 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5445 | } |
| 5446 | else { |
| 5447 | /* TODO: New token check */ |
| 5448 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5449 | goto drop; |
| 5450 | } |
| 5451 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5452 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5453 | int ipv4; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5454 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5455 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5456 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5457 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5458 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5459 | } |
| 5460 | |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5461 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5462 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5463 | TRACE_PROTO("Initial without token, sending retry", |
| 5464 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5465 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5466 | TRACE_PROTO("Error during Retry generation", |
| 5467 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5468 | goto err; |
| 5469 | } |
| 5470 | |
| 5471 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5472 | goto err; |
| 5473 | } |
| 5474 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5475 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5476 | * When an Initial packet is sent by a client that has not previously |
| 5477 | * received an Initial or Retry packet from the server, the client |
| 5478 | * populates the Destination Connection ID field with an unpredictable |
| 5479 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5480 | */ |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5481 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5482 | TRACE_PROTO("dropped packet", |
| 5483 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5484 | goto drop; |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5485 | } |
| 5486 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5487 | pkt->saddr = dgram->saddr; |
| 5488 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5489 | qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, &odcid, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 5490 | &pkt->saddr, 1, !!pkt->token_len, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5491 | if (qc == NULL) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5492 | goto drop; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5493 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5494 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5495 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5496 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5497 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5498 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5499 | /* If the insertion failed, it means that another |
| 5500 | * thread has already allocated a QUIC connection for |
| 5501 | * the same CID. Liberate our allocated connection. |
| 5502 | */ |
| 5503 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5504 | qc_to_purge = qc; |
| 5505 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5506 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5507 | pkt->qc = qc; |
| 5508 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5509 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5510 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5511 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5512 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5513 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5514 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5515 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5516 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5517 | } |
| 5518 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5519 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5523 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5524 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5525 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5526 | } |
| 5527 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5528 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5529 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5530 | |
| 5531 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5532 | * they must have the same DCID. |
| 5533 | */ |
| 5534 | if (!first_pkt && |
| 5535 | (pkt->dcid.len != dgram->dcid_len || |
| 5536 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5537 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5538 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5539 | } |
| 5540 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5541 | buf += QUIC_HAP_CID_LEN; |
| 5542 | |
| 5543 | /* A short packet is the last one of a UDP datagram. */ |
| 5544 | payload = buf; |
| 5545 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5546 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5547 | 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] | 5548 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5549 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5550 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5551 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5552 | TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5553 | goto drop; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 5554 | } |
| 5555 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5556 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5557 | } |
| 5558 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5559 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 5560 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 5561 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 5562 | qc->nb_pkt_for_cc++; |
| 5563 | qc->nb_pkt_since_cc = 0; |
| 5564 | } |
| 5565 | /* Skip the entire datagram */ |
| 5566 | pkt->len = end - beg; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5567 | TRACE_PROTO("Closing state connection", |
| 5568 | QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5569 | goto drop; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 5570 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5571 | |
| 5572 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5573 | * they must have the same DCID. |
| 5574 | * |
| 5575 | * This check must be done after the final update to pkt.len to |
| 5576 | * properly drop the packet on failure. |
| 5577 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5578 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5579 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5580 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5581 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5582 | /* Reset the anti-amplification bit. It will be set again |
| 5583 | * when sending the next packet if reached again. |
| 5584 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5585 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 5586 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5587 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5588 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5589 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5590 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5591 | |
Amaury Denoyelle | 72d8650 | 2022-07-13 15:08:23 +0200 | [diff] [blame] | 5592 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5593 | TRACE_PROTO("Connection error", |
| 5594 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5595 | goto out; |
| 5596 | } |
| 5597 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5598 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5599 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5600 | b_cspace = b_contig_space(&qc->rx.buf); |
| 5601 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 5602 | /* Do not consume buf if space not at the end. */ |
| 5603 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5604 | TRACE_PROTO("Packet dropped", |
| 5605 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 5606 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 5607 | goto drop_no_conn; |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 5608 | } |
| 5609 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5610 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 5611 | if (b_cspace) { |
| 5612 | b_putchr(&qc->rx.buf, 0x00); |
| 5613 | b_cspace--; |
| 5614 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5615 | b_add(&qc->rx.buf, b_cspace); |
| 5616 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5617 | TRACE_PROTO("Too big packet", |
| 5618 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 5619 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 5620 | goto drop_no_conn; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5621 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5622 | } |
| 5623 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5624 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5625 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5626 | goto drop; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 5627 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5628 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5629 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5630 | if (pkt->aad_len) |
| 5631 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5632 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 5633 | /* Wake up the connection packet handler task from here only if all |
| 5634 | * the contexts have been initialized, especially the mux context |
| 5635 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 5636 | * will start it if these contexts for the connection are not already |
| 5637 | * initialized. |
| 5638 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5639 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5640 | if (conn_ctx) |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 5641 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5642 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5643 | drop_no_conn: |
| 5644 | if (drop_no_conn) |
| 5645 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5646 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5647 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5648 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5649 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5650 | drop: |
| 5651 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5652 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5653 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 5654 | * This cannot be done by this thread. |
| 5655 | */ |
| 5656 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5657 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 5658 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 5659 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 5660 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 5661 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5662 | if (!pkt->len) |
| 5663 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5664 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5665 | qc ? qc : NULL, pkt, NULL, qv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5666 | } |
| 5667 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5668 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 5669 | * 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] | 5670 | */ |
| 5671 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5672 | int type, size_t pn_len, |
| 5673 | struct quic_conn *conn, const struct quic_version *ver) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5674 | { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5675 | if (end - *buf < sizeof ver->num + conn->dcid.len + conn->scid.len + 3) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5676 | return 0; |
| 5677 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5678 | type = quic_pkt_type(type, ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5679 | /* #0 byte flags */ |
| 5680 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 5681 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 5682 | /* Version */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5683 | quic_write_uint32(buf, end, ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5684 | *(*buf)++ = conn->dcid.len; |
| 5685 | /* Destination connection ID */ |
| 5686 | if (conn->dcid.len) { |
| 5687 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5688 | *buf += conn->dcid.len; |
| 5689 | } |
| 5690 | /* Source connection ID */ |
| 5691 | *(*buf)++ = conn->scid.len; |
| 5692 | if (conn->scid.len) { |
| 5693 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 5694 | *buf += conn->scid.len; |
| 5695 | } |
| 5696 | |
| 5697 | return 1; |
| 5698 | } |
| 5699 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5700 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 5701 | * 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] | 5702 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5703 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 5704 | size_t pn_len, struct quic_conn *conn, |
| 5705 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5706 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5707 | if (end - *buf < 1 + conn->dcid.len) |
| 5708 | return 0; |
| 5709 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5710 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 5711 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 5712 | ((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] | 5713 | /* Destination connection ID */ |
| 5714 | if (conn->dcid.len) { |
| 5715 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 5716 | *buf += conn->dcid.len; |
| 5717 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5718 | |
| 5719 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 5723 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 5724 | * with <aead> as AEAD cipher and <key> as secret key. |
| 5725 | * Returns 1 if succeeded or 0 if failed. |
| 5726 | */ |
| 5727 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 5728 | const EVP_CIPHER *aead, const unsigned char *key) |
| 5729 | { |
| 5730 | int i, ret, outlen; |
| 5731 | EVP_CIPHER_CTX *ctx; |
| 5732 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 5733 | * and at most 4 bytes for the packet number |
| 5734 | */ |
| 5735 | unsigned char mask[5] = {0}; |
| 5736 | |
| 5737 | ret = 0; |
| 5738 | ctx = EVP_CIPHER_CTX_new(); |
| 5739 | if (!ctx) |
| 5740 | return 0; |
| 5741 | |
| 5742 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 5743 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 5744 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 5745 | goto out; |
| 5746 | |
| 5747 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 5748 | for (i = 0; i < pnlen; i++) |
| 5749 | pn[i] ^= mask[i + 1]; |
| 5750 | |
| 5751 | ret = 1; |
| 5752 | |
| 5753 | out: |
| 5754 | EVP_CIPHER_CTX_free(ctx); |
| 5755 | |
| 5756 | return ret; |
| 5757 | } |
| 5758 | |
| 5759 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 5760 | * ACK ranges if needed to a value below <limit> in bytes. |
| 5761 | * Return 1 if succeeded, 0 if not. |
| 5762 | */ |
| 5763 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 5764 | { |
| 5765 | size_t room, ack_delay_sz; |
| 5766 | |
| 5767 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 5768 | /* A frame is made of 1 byte for the frame type. */ |
| 5769 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5770 | 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] | 5771 | return 0; |
| 5772 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5773 | 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] | 5774 | } |
| 5775 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5776 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 5777 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 5778 | * with <room> as available room, and <*len> the packet Length field initialized |
| 5779 | * with the number of bytes already present in this buffer which must be taken |
| 5780 | * into an account for the Length packet field value. <headlen> is the number of |
| 5781 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5782 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5783 | * 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] | 5784 | * by this function. Also attach these frames to <l> frame list. |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5785 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5786 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5787 | 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] | 5788 | size_t room, size_t *len, size_t headlen, |
| 5789 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5790 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5791 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5792 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5793 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5794 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5795 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5796 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5797 | return 0; |
| 5798 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5799 | /* If we are not probing we must take into an account the congestion |
| 5800 | * control window. |
| 5801 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 5802 | if (!qel->pktns->tx.pto_probe) { |
| 5803 | size_t remain = quic_path_prep_data(qc->path); |
| 5804 | |
| 5805 | if (headlen > remain) |
| 5806 | return 0; |
| 5807 | |
| 5808 | room = QUIC_MIN(room, remain - headlen); |
| 5809 | } |
| 5810 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5811 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5812 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5813 | |
| 5814 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 5815 | * returned by this function only if at least one frame could be built |
| 5816 | * in the switch/case block. |
| 5817 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5818 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5819 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5820 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5821 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5822 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5823 | break; |
| 5824 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5825 | switch (cf->type) { |
| 5826 | case QUIC_FT_CRYPTO: |
| 5827 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5828 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5829 | /* Compute the length of this CRYPTO frame header */ |
| 5830 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 5831 | /* Compute the data length of this CRyPTO frame. */ |
| 5832 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 5833 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5834 | 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] | 5835 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5836 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5837 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5838 | /* CRYPTO frame length. */ |
| 5839 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 5840 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5841 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5842 | /* Add the CRYPTO data length and its encoded length to the packet |
| 5843 | * length and the length of this length. |
| 5844 | */ |
| 5845 | *len += flen; |
| 5846 | room -= flen; |
| 5847 | if (dlen == cf->crypto.len) { |
| 5848 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5849 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5850 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5851 | } |
| 5852 | else { |
| 5853 | struct quic_frame *new_cf; |
| 5854 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5855 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5856 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5857 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5858 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5859 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5860 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5861 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5862 | new_cf->type = QUIC_FT_CRYPTO; |
| 5863 | new_cf->crypto.len = dlen; |
| 5864 | new_cf->crypto.offset = cf->crypto.offset; |
| 5865 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5866 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5867 | /* Consume <dlen> bytes of the current frame. */ |
| 5868 | cf->crypto.len -= dlen; |
| 5869 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5870 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5871 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5872 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 5873 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5874 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 5875 | struct eb64_node *node = NULL; |
| 5876 | struct qc_stream_desc *stream_desc = NULL; |
| 5877 | struct quic_stream *strm = &cf->stream; |
| 5878 | |
| 5879 | /* As this frame has been already lost, ensure the stream is always |
| 5880 | * available or the range of this frame is not consumed before |
| 5881 | * resending it. |
| 5882 | */ |
| 5883 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 5884 | if (!node) { |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 5885 | TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5886 | LIST_DELETE(&cf->list); |
| 5887 | pool_free(pool_head_quic_frame, cf); |
| 5888 | continue; |
| 5889 | } |
| 5890 | |
| 5891 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5892 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 5893 | TRACE_PROTO("ignored frame frame in already acked range", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 5894 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5895 | LIST_DELETE(&cf->list); |
| 5896 | pool_free(pool_head_quic_frame, cf); |
| 5897 | continue; |
| 5898 | } |
| 5899 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 5900 | strm->offset.key = stream_desc->ack_offset; |
| 5901 | TRACE_PROTO("updated partially acked frame", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 5902 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 5903 | } |
| 5904 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5905 | /* Note that these frames are accepted in short packets only without |
| 5906 | * "Length" packet field. Here, <*len> is used only to compute the |
| 5907 | * 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] | 5908 | * |
| 5909 | * 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] | 5910 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 5911 | */ |
| 5912 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5913 | ((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] | 5914 | /* Compute the data length of this STREAM frame. */ |
| 5915 | avail_room = room - hlen - *len; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 5916 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5917 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5918 | if ((ssize_t)avail_room <= 0) |
| 5919 | continue; |
| 5920 | |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5921 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 5922 | dlen = max_available_room(avail_room, &dlen_sz); |
| 5923 | if (dlen > cf->stream.len) { |
| 5924 | dlen = cf->stream.len; |
| 5925 | } |
| 5926 | dlen_sz = quic_int_getsize(dlen); |
| 5927 | flen = hlen + dlen_sz + dlen; |
| 5928 | } |
| 5929 | else { |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 5930 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5931 | flen = hlen + dlen; |
| 5932 | } |
| 5933 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5934 | 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] | 5935 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5936 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5937 | /* Add the STREAM data length and its encoded length to the packet |
| 5938 | * length and the length of this length. |
| 5939 | */ |
| 5940 | *len += flen; |
| 5941 | room -= flen; |
| 5942 | if (dlen == cf->stream.len) { |
| 5943 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5944 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5945 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5946 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5947 | /* The MUX stream might be released at this |
| 5948 | * stage. This can most notably happen on |
| 5949 | * retransmission. |
| 5950 | */ |
| 5951 | if (qc->mux_state == QC_MUX_READY && |
| 5952 | !cf->stream.stream->release) { |
| 5953 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 5954 | cf->stream.len, |
| 5955 | cf->stream.offset.key); |
| 5956 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5957 | } |
| 5958 | else { |
| 5959 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5960 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5961 | |
| 5962 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 5963 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5964 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 5965 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5966 | } |
| 5967 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 5968 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5969 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5970 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5971 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5972 | new_cf->stream.id = cf->stream.id; |
| 5973 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 5974 | new_cf->stream.offset = cf->stream.offset; |
| 5975 | new_cf->stream.len = dlen; |
| 5976 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 5977 | /* FIN bit reset */ |
| 5978 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 5979 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 5980 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 5981 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 5982 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5983 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 5984 | b_size(cf->stream.buf), |
| 5985 | (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] | 5986 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 5987 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 5988 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 5989 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 5990 | /* The MUX stream might be released at this |
| 5991 | * stage. This can most notably happen on |
| 5992 | * retransmission. |
| 5993 | */ |
| 5994 | if (qc->mux_state == QC_MUX_READY && |
| 5995 | !cf->stream.stream->release) { |
| 5996 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 5997 | new_cf->stream.len, |
| 5998 | new_cf->stream.offset.key); |
| 5999 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6000 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6001 | |
| 6002 | /* TODO the MUX is notified about the frame sending via |
| 6003 | * previous qcc_streams_sent_done call. However, the |
| 6004 | * sending can fail later, for example if the sendto |
| 6005 | * system call returns an error. As the MUX has been |
| 6006 | * notified, the transport layer is responsible to |
| 6007 | * bufferize and resent the announced data later. |
| 6008 | */ |
| 6009 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6010 | break; |
| 6011 | |
| 6012 | default: |
| 6013 | flen = qc_frm_len(cf); |
| 6014 | BUG_ON(!flen); |
| 6015 | if (flen > room) |
| 6016 | continue; |
| 6017 | |
| 6018 | *len += flen; |
| 6019 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6020 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6021 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6022 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6023 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6024 | |
| 6025 | /* Successful status as soon as a frame could be built */ |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6026 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6027 | } |
| 6028 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6029 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6030 | } |
| 6031 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6032 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6033 | * is used as return parameter and should be zero'ed by the caller. |
| 6034 | */ |
| 6035 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6036 | struct quic_frame *out) |
| 6037 | { |
| 6038 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6039 | * |
| 6040 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6041 | * different encryption levels depending on the client context. This is |
| 6042 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6043 | * more details on how to implement it. |
| 6044 | */ |
| 6045 | |
| 6046 | if (qc->err.app) { |
| 6047 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6048 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6049 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6050 | * |
| 6051 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6052 | * packet could expose application state or be used to alter application |
| 6053 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 6054 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 6055 | * Handshake packets. Otherwise, information about the application |
| 6056 | * state might be revealed. Endpoints MUST clear the value of the |
| 6057 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 6058 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 6059 | */ |
| 6060 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6061 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 6062 | out->connection_close.reason_phrase_len = 0; |
| 6063 | } |
| 6064 | else { |
| 6065 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 6066 | out->connection_close.error_code = qc->err.code; |
| 6067 | } |
| 6068 | } |
| 6069 | else { |
| 6070 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6071 | out->connection_close.error_code = qc->err.code; |
| 6072 | } |
| 6073 | } |
| 6074 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6075 | /* 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] | 6076 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 6077 | * 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] | 6078 | * filling the buffer with as much frames as possible from <frms> list of |
| 6079 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6080 | * 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] | 6081 | * 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] | 6082 | * having returned from this function. |
| 6083 | * 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] | 6084 | * number field in this packet. <pn_len> will also have the packet number |
| 6085 | * length as value. |
| 6086 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6087 | * 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] | 6088 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6089 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 6090 | size_t dglen, struct quic_tx_packet *pkt, |
| 6091 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6092 | int force_ack, int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6093 | struct quic_enc_level *qel, struct quic_conn *qc, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6094 | const struct quic_version *ver, struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6095 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6096 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6097 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6098 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 6099 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 6100 | struct quic_frame cc_frm = { }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6101 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6102 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6103 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6104 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6105 | struct quic_frame *cf; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6106 | int must_ack; |
| 6107 | int nb_aepkts_since_last_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6108 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6109 | /* Length field value with CRYPTO frames if present. */ |
| 6110 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6111 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6112 | /* When not probing, and no immediate close is required, reduce the size of this |
| 6113 | * buffer to respect the congestion controller window. |
| 6114 | * 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] | 6115 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6116 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6117 | size_t path_room; |
| 6118 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6119 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6120 | if (end - beg > path_room) |
| 6121 | end = beg + path_room; |
| 6122 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6123 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6124 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6125 | * length field if any. |
| 6126 | */ |
| 6127 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6128 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6129 | goto no_room; |
| 6130 | |
| 6131 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6132 | 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] | 6133 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6134 | *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] | 6135 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6136 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6137 | !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] | 6138 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6139 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6140 | goto no_room; |
| 6141 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6142 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 6143 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6144 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6145 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6146 | /* Build an ACK frame if required. */ |
| 6147 | ack_frm_len = 0; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6148 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 6149 | must_ack = !qel->pktns->tx.pto_probe && |
| 6150 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 6151 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 6152 | if (force_ack || must_ack) { |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6153 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6154 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6155 | ack_frm.tx_ack.arngs = arngs; |
| 6156 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 6157 | qel->pktns->tx.ack_delay = |
| 6158 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 6159 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 6160 | } |
| 6161 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6162 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 6163 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 6164 | * that from here, we do not know if we will have to send a PING frame. |
| 6165 | * This will be decided after having computed the ack-eliciting frames |
| 6166 | * to be added to this packet. |
| 6167 | */ |
| 6168 | 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] | 6169 | if (!ack_frm_len) |
| 6170 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6171 | } |
| 6172 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6173 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6174 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 6175 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6176 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6177 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6178 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6179 | 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] | 6180 | /* Initialize the length of the frames built below to <len>. |
| 6181 | * If any frame could be successfully built by qc_build_frms(), |
| 6182 | * we will have len_frms > len. |
| 6183 | */ |
| 6184 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6185 | if (!qc_build_frms(&frm_list, frms, |
| 6186 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6187 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6188 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 6189 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 6190 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6191 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6192 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6193 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6194 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 6195 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 6196 | * for the encryption tag. It must be taken into an account for the length |
| 6197 | * of this packet. |
| 6198 | */ |
| 6199 | if (len_frms) |
| 6200 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 6201 | else |
| 6202 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6203 | /* CONNECTION_CLOSE frame */ |
| 6204 | if (cc) { |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6205 | qc_build_cc_frm(qc, qel, &cc_frm); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6206 | len += qc_frm_len(&cc_frm); |
| 6207 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6208 | add_ping_frm = 0; |
| 6209 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6210 | len_sz = quic_int_getsize(len); |
| 6211 | /* Add this packet size to <dglen> */ |
| 6212 | dglen += head_len + len_sz + len; |
| 6213 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 6214 | /* This is a maximum padding size */ |
| 6215 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 6216 | /* The length field value is of this packet is <len> + <padding_len> |
| 6217 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 6218 | * <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] | 6219 | * sizes from <padding_len>. |
| 6220 | */ |
| 6221 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 6222 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6223 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6224 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6225 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6226 | /* 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] | 6227 | add_ping_frm = 1; |
| 6228 | len += 1; |
| 6229 | } |
| 6230 | /* 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] | 6231 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6232 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 6233 | } |
| 6234 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6235 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 6236 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6237 | |
| 6238 | /* Packet number field address. */ |
| 6239 | *buf_pn = pos; |
| 6240 | |
| 6241 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6242 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 6243 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6244 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6245 | if (ack_frm_len) { |
| 6246 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 6247 | goto no_room; |
| 6248 | |
| 6249 | 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] | 6250 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6251 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6252 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6253 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6254 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6255 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6256 | unsigned char *spos = pos; |
| 6257 | |
| 6258 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6259 | ssize_t room = end - pos; |
| 6260 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6261 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6262 | /* TODO: this should not have happened if qc_build_frms() |
| 6263 | * had correctly computed and sized the frames to be |
| 6264 | * added to this packet. Note that <cf> was added |
| 6265 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 6266 | */ |
| 6267 | LIST_DELETE(&cf->list); |
| 6268 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6269 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6270 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6271 | |
| 6272 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6273 | quic_tx_packet_refinc(pkt); |
| 6274 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | /* Build a PING frame if needed. */ |
| 6279 | if (add_ping_frm) { |
| 6280 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6281 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6282 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6283 | } |
| 6284 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6285 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6286 | if (cc) { |
| 6287 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 6288 | goto no_room; |
| 6289 | |
| 6290 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 6291 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6292 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6293 | /* Build a PADDING frame if needed. */ |
| 6294 | if (padding_len) { |
| 6295 | frm.type = QUIC_FT_PADDING; |
| 6296 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6297 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6298 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6299 | } |
| 6300 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6301 | /* If this packet is ack-eliciting and we are probing let's |
| 6302 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6303 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6304 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 6305 | qel->pktns->tx.pto_probe) |
| 6306 | qel->pktns->tx.pto_probe--; |
| 6307 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6308 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6309 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6310 | 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] | 6311 | |
| 6312 | return 1; |
| 6313 | |
| 6314 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6315 | /* 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] | 6316 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | d8b798d | 2022-04-25 17:48:40 +0200 | [diff] [blame] | 6317 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 6318 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6319 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6320 | } |
| 6321 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6322 | 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] | 6323 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6324 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6325 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6326 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 6327 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6328 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6329 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6330 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6331 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6332 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6333 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6334 | } |
| 6335 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6336 | /* 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] | 6337 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 6338 | * of prebuilt frames. |
| 6339 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6340 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 6341 | * -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] | 6342 | * XXX NOTE XXX |
| 6343 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6344 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6345 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6346 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6347 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6348 | const unsigned char *buf_end, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6349 | struct quic_enc_level *qel, |
| 6350 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 6351 | struct quic_conn *qc, const struct quic_version *ver, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6352 | size_t dglen, int pkt_type, int force_ack, |
| 6353 | int padding, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6354 | { |
| 6355 | /* The pointer to the packet number field. */ |
| 6356 | unsigned char *buf_pn; |
| 6357 | unsigned char *beg, *end, *payload; |
| 6358 | int64_t pn; |
| 6359 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6360 | struct quic_tx_packet *pkt; |
| 6361 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6362 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6363 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6364 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6365 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6366 | 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] | 6367 | *err = -2; |
| 6368 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6369 | } |
| 6370 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6371 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6372 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6373 | pn_len = 0; |
| 6374 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6375 | |
| 6376 | pn = qel->pktns->tx.next_pn + 1; |
| 6377 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6378 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6379 | *err = -1; |
| 6380 | goto err; |
| 6381 | } |
| 6382 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6383 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6384 | payload = buf_pn + pn_len; |
| 6385 | payload_len = end - payload; |
| 6386 | aad_len = payload - beg; |
| 6387 | |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6388 | 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] | 6389 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6390 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6391 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6392 | |
| 6393 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6394 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6395 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 6396 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6397 | 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] | 6398 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6399 | goto err; |
| 6400 | } |
| 6401 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6402 | /* Consume a packet number */ |
| 6403 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6404 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 676b849 | 2022-03-10 10:38:20 +0100 | [diff] [blame] | 6405 | 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] | 6406 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6407 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6408 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6409 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6410 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6411 | /* 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] | 6412 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6413 | pkt->in_flight_len = pkt->len; |
| 6414 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6415 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6416 | /* Always reset this flags */ |
| 6417 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6418 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6419 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6420 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6421 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6422 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6423 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6424 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6425 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6426 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6427 | |
| 6428 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6429 | /* TODO: what about the frames which have been built |
| 6430 | * for this packet. |
| 6431 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6432 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6433 | 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] | 6434 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6435 | } |
| 6436 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6437 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6438 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6439 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6440 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6441 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6442 | */ |
| 6443 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6444 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6445 | struct qcc *qcc = conn->handle.qc->qcc; |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6446 | |
| 6447 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6448 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6449 | |
| 6450 | es->events |= event_type; |
| 6451 | qcc->subs = es; |
| 6452 | |
| 6453 | if (event_type & SUB_RETRY_RECV) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6454 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6455 | |
| 6456 | if (event_type & SUB_RETRY_SEND) |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6457 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6458 | |
| 6459 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6460 | } |
| 6461 | |
| 6462 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6463 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6464 | * subscribe() call. It always returns zero. |
| 6465 | */ |
| 6466 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6467 | { |
| 6468 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6469 | } |
| 6470 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6471 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 6472 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6473 | */ |
| 6474 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 6475 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 6476 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6477 | |
| 6478 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 6479 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6480 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6481 | if (*xprt_ctx) |
| 6482 | goto out; |
| 6483 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6484 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6485 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6486 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 6487 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6488 | |
| 6489 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6490 | } |
| 6491 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6492 | /* Start the QUIC transport layer */ |
| 6493 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 6494 | { |
| 6495 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 6496 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6497 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6498 | qc = conn->handle.qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6499 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 6500 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 6501 | /* prepare a CONNECTION_CLOSE frame */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 6502 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR)); |
Amaury Denoyelle | 05d4ae6 | 2022-04-13 17:40:26 +0200 | [diff] [blame] | 6503 | return -1; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6504 | } |
| 6505 | |
| 6506 | /* mux-quic can now be considered ready. */ |
| 6507 | qc->mux_state = QC_MUX_READY; |
| 6508 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6509 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 6510 | return 1; |
| 6511 | } |
| 6512 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6513 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 6514 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6515 | if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx) |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6516 | return NULL; |
| 6517 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 6518 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6519 | } |
| 6520 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6521 | /* transport-layer operations for QUIC connections. */ |
| 6522 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 6523 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6524 | .subscribe = quic_conn_subscribe, |
| 6525 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6526 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6527 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6528 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 6529 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6530 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 6531 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6532 | .name = "QUIC", |
| 6533 | }; |
| 6534 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6535 | static void __quic_conn_init(void) |
| 6536 | { |
| 6537 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 6538 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 6539 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6540 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6541 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6542 | static void __quic_conn_deinit(void) |
| 6543 | { |
| 6544 | BIO_meth_free(ha_quic_meth); |
| 6545 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 6546 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6547 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6548 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 6549 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 6550 | * 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] | 6551 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6552 | 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] | 6553 | { |
| 6554 | unsigned char *pos; |
| 6555 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6556 | struct quic_dghdlr *dghdlr = ctx; |
| 6557 | struct quic_dgram *dgram; |
| 6558 | int first_pkt = 1; |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 6559 | struct list *tasklist_head = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6560 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6561 | 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] | 6562 | pos = dgram->buf; |
| 6563 | end = pos + dgram->len; |
| 6564 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6565 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6566 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6567 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 6568 | if (!pkt) |
| 6569 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6570 | |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 6571 | LIST_INIT(&pkt->qc_rx_pkt_list); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6572 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6573 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 6574 | qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6575 | first_pkt = 0; |
| 6576 | pos += pkt->len; |
| 6577 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 6578 | |
| 6579 | /* Free rejected packets */ |
| 6580 | if (!pkt->refcnt) { |
| 6581 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 6582 | pool_free(pool_head_quic_rx_packet, pkt); |
| 6583 | } |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6584 | } while (pos < end); |
| 6585 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6586 | /* Increasing the received bytes counter by the UDP datagram length |
| 6587 | * if this datagram could be associated to a connection. |
| 6588 | */ |
| 6589 | if (dgram->qc) |
| 6590 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 6591 | |
| 6592 | /* Mark this datagram as consumed */ |
| 6593 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 6594 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6595 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6596 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6597 | |
| 6598 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6599 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6600 | } |
| 6601 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6602 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 6603 | * Returns 1 if succeeded, 0 if not. |
| 6604 | */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 6605 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 6606 | unsigned char **dcid, size_t *dcid_len) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6607 | { |
| 6608 | int long_header; |
| 6609 | size_t minlen, skip; |
| 6610 | |
| 6611 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 6612 | goto err; |
| 6613 | |
| 6614 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 6615 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 6616 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6617 | 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] | 6618 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 6619 | goto err; |
| 6620 | |
| 6621 | buf += skip; |
| 6622 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 6623 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 6624 | goto err; |
| 6625 | |
| 6626 | *dcid = buf; |
| 6627 | |
| 6628 | return 1; |
| 6629 | |
| 6630 | err: |
| 6631 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 6632 | return 0; |
| 6633 | } |
| 6634 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 6635 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 6636 | void qc_notify_close(struct quic_conn *qc) |
| 6637 | { |
| 6638 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 6639 | return; |
| 6640 | |
| 6641 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 6642 | |
| 6643 | /* wake up the MUX */ |
| 6644 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) |
| 6645 | qc->conn->mux->wake(qc->conn); |
| 6646 | } |
| 6647 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6648 | /* |
| 6649 | * Local variables: |
| 6650 | * c-indent-level: 8 |
| 6651 | * c-basic-offset: 8 |
| 6652 | * End: |
| 6653 | */ |