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 | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 128 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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 | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 131 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
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" }, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 141 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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 | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 162 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 163 | { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | { /* end */ } |
| 165 | }; |
| 166 | |
| 167 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 168 | /* arg1 */ { /* already used by the connection */ }, |
| 169 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 170 | /* arg3 */ { }, |
| 171 | /* arg4 */ { } |
| 172 | }; |
| 173 | |
| 174 | static const struct name_desc quic_trace_decoding[] = { |
| 175 | #define QUIC_VERB_CLEAN 1 |
| 176 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 177 | { /* end */ } |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | struct trace_source trace_quic = { |
| 182 | .name = IST("quic"), |
| 183 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 184 | .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] | 185 | .default_cb = quic_trace, |
| 186 | .known_events = quic_trace_events, |
| 187 | .lockon_args = quic_trace_lockon_args, |
| 188 | .decoding = quic_trace_decoding, |
| 189 | .report_events = ~0, /* report everything by default */ |
| 190 | }; |
| 191 | |
| 192 | #define TRACE_SOURCE &trace_quic |
| 193 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 194 | |
| 195 | static BIO_METHOD *ha_quic_meth; |
| 196 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 197 | 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] | 198 | 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] | 199 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 200 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 201 | 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] | 202 | DECLARE_POOL(pool_head_quic_connection_id, |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 203 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 204 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 205 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 206 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 207 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 208 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf)); |
| 209 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 210 | 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] | 211 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 212 | 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] | 213 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 214 | struct list *frms, struct quic_conn *qc, |
| 215 | 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] | 216 | 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] | 217 | 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] | 218 | 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] | 219 | 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] | 220 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 221 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 222 | 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] | 223 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 224 | /* Only for debug purpose */ |
| 225 | struct enc_debug_info { |
| 226 | unsigned char *payload; |
| 227 | size_t payload_len; |
| 228 | unsigned char *aad; |
| 229 | size_t aad_len; |
| 230 | uint64_t pn; |
| 231 | }; |
| 232 | |
| 233 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 234 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 235 | unsigned char *payload, size_t payload_len, |
| 236 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 237 | { |
| 238 | edi->payload = payload; |
| 239 | edi->payload_len = payload_len; |
| 240 | edi->aad = aad; |
| 241 | edi->aad_len = aad_len; |
| 242 | edi->pn = pn; |
| 243 | } |
| 244 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 245 | /* Trace callback for QUIC. |
| 246 | * These traces always expect that arg1, if non-null, is of type connection. |
| 247 | */ |
| 248 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 249 | const struct ist where, const struct ist func, |
| 250 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 251 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 252 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 253 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 254 | if (qc) { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 255 | const struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 256 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 257 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 258 | if (mask & QUIC_EV_CONN_INIT) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 259 | chunk_appendf(&trace_buf, "\n odcid"); |
| 260 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 261 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 262 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 263 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 264 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Frédéric Lécaille | c7785b5 | 2022-05-23 09:08:54 +0200 | [diff] [blame] | 267 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 268 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 4f5777a | 2022-06-20 19:39:26 +0200 | [diff] [blame] | 269 | quic_transport_params_dump(&trace_buf, qc, p); |
Frédéric Lécaille | c7785b5 | 2022-05-23 09:08:54 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 272 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 273 | const enum ssl_encryption_level_t *level = a2; |
| 274 | const size_t *len = a3; |
| 275 | |
| 276 | if (level) { |
| 277 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 278 | |
| 279 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 280 | } |
| 281 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 282 | 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] | 283 | } |
| 284 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 285 | /* Initial read & write secrets. */ |
| 286 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 287 | const unsigned char *rx_sec = a2; |
| 288 | const unsigned char *tx_sec = a3; |
| 289 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 290 | tls_ctx = &qc->els[level].tls_ctx; |
| 291 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 292 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 293 | if (rx_sec) |
| 294 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 295 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 296 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 297 | if (tx_sec) |
| 298 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 299 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 303 | const enum ssl_encryption_level_t *level = a2; |
| 304 | const unsigned char *secret = a3; |
| 305 | const size_t *secret_len = a4; |
| 306 | |
| 307 | if (level) { |
| 308 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 309 | |
| 310 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 311 | if (secret && secret_len) |
| 312 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 313 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 314 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 315 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 320 | const enum ssl_encryption_level_t *level = a2; |
| 321 | const unsigned char *secret = a3; |
| 322 | const size_t *secret_len = a4; |
| 323 | |
| 324 | if (level) { |
| 325 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 326 | |
| 327 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 328 | if (secret && secret_len) |
| 329 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 330 | tls_ctx = &qc->els[lvl].tls_ctx; |
| 331 | if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) |
| 332 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 336 | |
Frédéric Lécaille | b823bb7 | 2022-03-31 20:26:18 +0200 | [diff] [blame] | 337 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 338 | const struct list *l = a2; |
| 339 | |
| 340 | if (l) { |
| 341 | const struct quic_frame *frm; |
| 342 | list_for_each_entry(frm, l, list) |
| 343 | chunk_frm_appendf(&trace_buf, frm); |
| 344 | } |
| 345 | } |
| 346 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 347 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 348 | const struct quic_tx_packet *pkt = a2; |
| 349 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 350 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 351 | |
| 352 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 353 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 354 | 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] | 355 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 356 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 357 | (unsigned long long)qc->path->cwnd, |
| 358 | (unsigned long long)qc->path->prep_in_flight, |
| 359 | (unsigned long long)qc->path->in_flight, |
| 360 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 361 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 362 | } |
| 363 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 364 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 365 | if (pkt->pn_node.key != (uint64_t)-1) |
| 366 | 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] | 367 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 368 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (room) { |
| 372 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 373 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 374 | (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] | 375 | } |
| 376 | } |
| 377 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 378 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 379 | const enum quic_handshake_state *state = a2; |
| 380 | const int *err = a3; |
| 381 | |
| 382 | if (state) |
| 383 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 384 | if (err) |
| 385 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 386 | } |
| 387 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 388 | 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] | 389 | const struct quic_rx_packet *pkt = a2; |
| 390 | const unsigned long *pktlen = a3; |
| 391 | const SSL *ssl = a4; |
| 392 | |
| 393 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 394 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 395 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 396 | chunk_appendf(&trace_buf, " kp=%d", |
| 397 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 398 | chunk_appendf(&trace_buf, " el=%c", |
| 399 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 400 | if (pkt->pnl) |
| 401 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 402 | (unsigned long long)pkt->pn); |
| 403 | if (pkt->token_len) |
| 404 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 405 | (unsigned long long)pkt->token_len); |
| 406 | if (pkt->aad_len) |
| 407 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 408 | (unsigned long long)pkt->aad_len); |
| 409 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 410 | pkt->flags, (unsigned long long)pkt->len); |
| 411 | } |
| 412 | if (pktlen) |
| 413 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 414 | if (ssl) { |
| 415 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 416 | chunk_appendf(&trace_buf, " el=%c", |
| 417 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 418 | } |
| 419 | } |
| 420 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 421 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 422 | const struct quic_rx_packet *pkt = a2; |
| 423 | const struct quic_rx_crypto_frm *cf = a3; |
| 424 | const SSL *ssl = a4; |
| 425 | |
| 426 | if (pkt) |
| 427 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 428 | quic_packet_type_enc_level_char(pkt->type), |
| 429 | (unsigned long long)pkt->pn); |
| 430 | if (cf) |
| 431 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 432 | (unsigned long long)cf->offset_node.key, |
| 433 | (unsigned long long)cf->len); |
| 434 | if (ssl) { |
| 435 | 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] | 436 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 437 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 438 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 439 | |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 440 | if (qc->err.code) |
| 441 | 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] | 442 | } |
| 443 | |
| 444 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 445 | const struct quic_frame *frm = a2; |
| 446 | |
| 447 | if (frm) |
| 448 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 449 | } |
| 450 | |
| 451 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 452 | const struct quic_enc_level *qel = a2; |
| 453 | |
| 454 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 455 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 456 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 457 | " 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] | 458 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 459 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 460 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 461 | (unsigned long long)qc->path->cwnd, |
| 462 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 463 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 464 | (unsigned long long)pktns->tx.in_flight, |
| 465 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 469 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 470 | const struct enc_debug_info *edi = a2; |
| 471 | |
| 472 | if (edi) |
| 473 | chunk_appendf(&trace_buf, |
| 474 | " payload=@%p payload_len=%llu" |
| 475 | " aad=@%p aad_len=%llu pn=%llu", |
| 476 | edi->payload, (unsigned long long)edi->payload_len, |
| 477 | edi->aad, (unsigned long long)edi->aad_len, |
| 478 | (unsigned long long)edi->pn); |
| 479 | } |
| 480 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 481 | if (mask & QUIC_EV_CONN_RMHP) { |
| 482 | const struct quic_rx_packet *pkt = a2; |
| 483 | |
| 484 | if (pkt) { |
| 485 | const int *ret = a3; |
| 486 | |
| 487 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 488 | if (ret && *ret) |
| 489 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 490 | pkt->pnl, (unsigned long long)pkt->pn); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 495 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 496 | const unsigned long *val1 = a3; |
| 497 | const unsigned long *val2 = a4; |
| 498 | |
Frédéric Lécaille | 2f16348 | 2022-08-19 09:23:15 +0200 | [diff] [blame] | 499 | if (frm) { |
| 500 | chunk_appendf(&trace_buf, " frm@%p", 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 | 2f16348 | 2022-08-19 09:23:15 +0200 | [diff] [blame] | 502 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 503 | if (val1) |
| 504 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 505 | if (val2) |
| 506 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 507 | } |
| 508 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 509 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 510 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 511 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 512 | |
| 513 | if (s) |
| 514 | 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] | 515 | if (stream) |
| 516 | 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] | 517 | } |
| 518 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 520 | const unsigned int *rtt_sample = a2; |
| 521 | const unsigned int *ack_delay = a3; |
| 522 | const struct quic_loss *ql = a4; |
| 523 | |
| 524 | if (rtt_sample) |
| 525 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 526 | if (ack_delay) |
| 527 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 528 | if (ql) |
| 529 | chunk_appendf(&trace_buf, |
| 530 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 531 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 532 | } |
| 533 | if (mask & QUIC_EV_CONN_CC) { |
| 534 | const struct quic_cc_event *ev = a2; |
| 535 | const struct quic_cc *cc = a3; |
| 536 | |
| 537 | if (a2) |
| 538 | quic_cc_event_trace(&trace_buf, ev); |
| 539 | if (a3) |
| 540 | quic_cc_state_trace(&trace_buf, cc); |
| 541 | } |
| 542 | |
| 543 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 544 | const struct quic_pktns *pktns = a2; |
| 545 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 546 | |
| 547 | if (pktns) { |
| 548 | chunk_appendf(&trace_buf, " pktns=%s", |
| 549 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 550 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 551 | if (pktns->tx.loss_time) |
| 552 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 553 | 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] | 554 | } |
| 555 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 556 | struct quic_tx_packet *pkt; |
| 557 | |
| 558 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 559 | list_for_each_entry(pkt, lost_pkts, list) |
| 560 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | 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] | 565 | const struct quic_pktns *pktns = a2; |
| 566 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 567 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 568 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 569 | if (ifae_pkts) |
| 570 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 571 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 572 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 573 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 574 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 575 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 576 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 577 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 578 | if (pktns->tx.in_flight) |
| 579 | 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] | 580 | if (pktns->tx.loss_time) |
| 581 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 582 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 583 | } |
| 584 | if (mask & QUIC_EV_CONN_SPTO) { |
| 585 | if (pktns->tx.time_of_last_eliciting) |
| 586 | chunk_appendf(&trace_buf, " tole=%dms", |
| 587 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 588 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 589 | 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] | 590 | } |
| 591 | } |
| 592 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 593 | 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] | 594 | chunk_appendf(&trace_buf, |
| 595 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 600 | const struct quic_tx_packet *pkt = a2; |
| 601 | |
Frédéric Lécaille | 8ecb736 | 2022-08-04 12:00:00 +0200 | [diff] [blame] | 602 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 603 | (unsigned long long)qc->path->cwnd, |
| 604 | (unsigned long long)qc->path->prep_in_flight, |
| 605 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 606 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 607 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 608 | (unsigned long)pkt->pn_node.key, |
| 609 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 610 | 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] | 611 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | 3a9b944 | 2022-08-30 15:25:47 +0200 | [diff] [blame] | 612 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 613 | (unsigned long long)qc->rx.bytes, |
| 614 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 617 | |
| 618 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 619 | const uint8_t *alert = a2; |
| 620 | const enum ssl_encryption_level_t *level = a3; |
| 621 | |
| 622 | if (alert) |
| 623 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 624 | if (level) |
| 625 | chunk_appendf(&trace_buf, " el=%c", |
| 626 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 627 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 628 | |
| 629 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 630 | const size_t *sz1 = a2; |
| 631 | const size_t *sz2 = a3; |
| 632 | const size_t *sz3 = a4; |
| 633 | |
| 634 | if (sz1) |
| 635 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 636 | if (sz2) |
| 637 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 638 | if (sz3) |
| 639 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 640 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 641 | |
| 642 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 643 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 644 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 645 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 646 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 647 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 648 | } |
| 649 | if (mask & QUIC_EV_CONN_LPKT) { |
| 650 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 651 | const uint64_t *len = a3; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 652 | const struct quic_version *ver = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 653 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 654 | if (pkt) { |
| 655 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 656 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 657 | if (pkt->pn_node.key != (uint64_t)-1) |
| 658 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 659 | } |
| 660 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 661 | if (len) |
| 662 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 663 | |
| 664 | if (ver) |
| 665 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 668 | if (mask & QUIC_EV_STATELESS_RST) { |
| 669 | const struct quic_cid *cid = a2; |
| 670 | |
| 671 | if (cid) |
| 672 | quic_cid_dump(&trace_buf, cid); |
| 673 | } |
| 674 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* 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] | 678 | 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] | 679 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 680 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 681 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 682 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 683 | return 1; |
| 684 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 685 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 686 | 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] | 687 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 688 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 689 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 690 | return 1; |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 696 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 697 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 698 | 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] | 699 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 700 | struct quic_pktns *pktns; |
| 701 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 702 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 703 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 704 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 705 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 706 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 707 | pktns = quic_loss_pktns(qc); |
| 708 | if (tick_isset(pktns->tx.loss_time)) { |
| 709 | qc->timer = pktns->tx.loss_time; |
| 710 | goto out; |
| 711 | } |
| 712 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 713 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 714 | * cancelled for a server which reached the anti-amplification limit. |
| 715 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 716 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 717 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 718 | 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] | 719 | qc->timer = TICK_ETERNITY; |
| 720 | goto out; |
| 721 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 722 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 723 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 724 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 725 | /* Timer cancellation. */ |
| 726 | qc->timer = TICK_ETERNITY; |
| 727 | goto out; |
| 728 | } |
| 729 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 730 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 731 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 732 | if (tick_isset(pto)) |
| 733 | qc->timer = pto; |
| 734 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 735 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 736 | if (tick_is_expired(qc->timer, now_ms)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 737 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 738 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 739 | } |
| 740 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 741 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 742 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 743 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 744 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 745 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 748 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 749 | * connection. |
| 750 | * Return 1 if succeeded, 0 if not. |
| 751 | */ |
| 752 | static int quic_tls_key_update(struct quic_conn *qc) |
| 753 | { |
| 754 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 755 | struct quic_tls_secrets *rx, *tx; |
| 756 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 757 | 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] | 758 | const struct quic_version *ver = |
| 759 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 760 | int ret = 0; |
| 761 | |
| 762 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 763 | |
| 764 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 765 | rx = &tls_ctx->rx; |
| 766 | tx = &tls_ctx->tx; |
| 767 | nxt_rx = &qc->ku.nxt_rx; |
| 768 | nxt_tx = &qc->ku.nxt_tx; |
| 769 | |
| 770 | /* Prepare new RX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 771 | 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] | 772 | rx->secret, rx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 773 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 774 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 777 | 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] | 778 | nxt_rx->key, nxt_rx->keylen, |
| 779 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 780 | nxt_rx->secret, nxt_rx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 781 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 782 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* Prepare new TX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 786 | 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] | 787 | tx->secret, tx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 788 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 789 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 792 | 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] | 793 | nxt_tx->key, nxt_tx->keylen, |
| 794 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 795 | nxt_tx->secret, nxt_tx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 796 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 797 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 800 | if (nxt_rx->ctx) { |
| 801 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 802 | nxt_rx->ctx = NULL; |
| 803 | } |
| 804 | |
| 805 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 806 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 807 | goto leave; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | if (nxt_tx->ctx) { |
| 811 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 812 | nxt_tx->ctx = NULL; |
| 813 | } |
| 814 | |
| 815 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 816 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 817 | goto leave; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 820 | ret = 1; |
| 821 | leave: |
| 822 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc); |
| 823 | return ret; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 827 | * Must be used after having updated them. |
| 828 | * Always succeeds. |
| 829 | */ |
| 830 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 831 | { |
| 832 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 833 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 834 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 835 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 836 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 837 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 838 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 839 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 840 | curr_secret = tls_ctx->rx.secret; |
| 841 | curr_iv = tls_ctx->rx.iv; |
| 842 | curr_key = tls_ctx->rx.key; |
| 843 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 844 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 845 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 846 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 847 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 848 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 849 | 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] | 850 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 851 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 852 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 853 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 854 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 855 | qc->ku.prv_rx.secret = curr_secret; |
| 856 | qc->ku.prv_rx.iv = curr_iv; |
| 857 | qc->ku.prv_rx.key = curr_key; |
| 858 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 859 | |
| 860 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 861 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 862 | curr_secret = tls_ctx->tx.secret; |
| 863 | curr_iv = tls_ctx->tx.iv; |
| 864 | curr_key = tls_ctx->tx.key; |
| 865 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 866 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 867 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 868 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 869 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 870 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 871 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 872 | qc->ku.nxt_tx.secret = curr_secret; |
| 873 | qc->ku.nxt_tx.iv = curr_iv; |
| 874 | qc->ku.nxt_tx.key = curr_key; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 875 | |
| 876 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 879 | /* returns 0 on error, 1 on success */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 880 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 881 | const uint8_t *read_secret, |
| 882 | const uint8_t *write_secret, size_t secret_len) |
| 883 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 884 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 885 | 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] | 886 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 887 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 888 | const struct quic_version *ver = |
| 889 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 890 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 891 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 892 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 893 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 894 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 895 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 896 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 897 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 898 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 899 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 900 | TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 901 | goto leave; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | rx = &tls_ctx->rx; |
| 905 | tx = &tls_ctx->tx; |
| 906 | |
| 907 | rx->aead = tx->aead = tls_aead(cipher); |
| 908 | rx->md = tx->md = tls_md(cipher); |
| 909 | rx->hp = tx->hp = tls_hp(cipher); |
| 910 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 911 | 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] | 912 | 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] | 913 | read_secret, secret_len)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 914 | TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 915 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 918 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 919 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 920 | goto leave; |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 923 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 924 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 925 | goto leave; |
| 926 | } |
| 927 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 928 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 929 | * listener. Note that a listener derives only RX secrets for this |
| 930 | * level. |
| 931 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 932 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 933 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 934 | quic_accept_push_qc(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 935 | } |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 936 | |
| 937 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 938 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 939 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 940 | 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] | 941 | 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] | 942 | write_secret, secret_len)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 943 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 944 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 947 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 948 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 949 | goto leave; |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 952 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 953 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 954 | goto leave; |
| 955 | } |
| 956 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 957 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 958 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 959 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 960 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 961 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 962 | /* 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] | 963 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 964 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 965 | TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 966 | goto leave; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | memcpy(rx->secret, read_secret, secret_len); |
| 970 | rx->secretlen = secret_len; |
| 971 | memcpy(tx->secret, write_secret, secret_len); |
| 972 | tx->secretlen = secret_len; |
| 973 | /* Initialize all the secret keys lengths */ |
| 974 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 975 | /* Prepare the next key update */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 976 | if (!quic_tls_key_update(qc)) { |
| 977 | // trace already emitted by function above |
| 978 | goto leave; |
| 979 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 980 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 981 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 982 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 983 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 984 | no_secret: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 985 | ret = 1; |
| 986 | leave: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 987 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 988 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 989 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 990 | |
| 991 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 992 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 993 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 994 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 995 | * buffers to store all the data. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 996 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 997 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 998 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 999 | const unsigned char *data, size_t len) |
| 1000 | { |
| 1001 | struct quic_crypto_buf **qcb; |
| 1002 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1003 | size_t cf_offset, cf_len, *nb_buf; |
| 1004 | unsigned char *pos; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1005 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1006 | |
| 1007 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1008 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1009 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1010 | cf_len = len; |
| 1011 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1012 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1013 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1014 | while (len) { |
| 1015 | size_t to_copy, room; |
| 1016 | |
| 1017 | pos = (*qcb)->data + (*qcb)->sz; |
| 1018 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1019 | to_copy = len > room ? room : len; |
| 1020 | if (to_copy) { |
| 1021 | memcpy(pos, data, to_copy); |
| 1022 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1023 | qel->tx.crypto.sz += to_copy; |
| 1024 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1025 | len -= to_copy; |
| 1026 | data += to_copy; |
| 1027 | } |
| 1028 | else { |
| 1029 | struct quic_crypto_buf **tmp; |
| 1030 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1031 | // FIXME: realloc! |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1032 | tmp = realloc(qel->tx.crypto.bufs, |
| 1033 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1034 | if (tmp) { |
| 1035 | qel->tx.crypto.bufs = tmp; |
| 1036 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1037 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1038 | if (!*qcb) { |
| 1039 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1040 | goto leave; |
| 1041 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1042 | |
| 1043 | (*qcb)->sz = 0; |
| 1044 | ++*nb_buf; |
| 1045 | } |
| 1046 | else { |
| 1047 | break; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1053 | * have been buffered. |
| 1054 | */ |
| 1055 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1056 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1057 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1058 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1059 | /* There is at most one CRYPTO frame in this packet number |
| 1060 | * space. Let's look for it. |
| 1061 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1062 | 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] | 1063 | if (frm->type != QUIC_FT_CRYPTO) |
| 1064 | continue; |
| 1065 | |
| 1066 | /* Found */ |
| 1067 | found = frm; |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | if (found) { |
| 1072 | found->crypto.len += cf_len; |
| 1073 | } |
| 1074 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1075 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1076 | if (!frm) { |
| 1077 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1078 | goto leave; |
| 1079 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1080 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1081 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1082 | frm->type = QUIC_FT_CRYPTO; |
| 1083 | frm->crypto.offset = cf_offset; |
| 1084 | frm->crypto.len = cf_len; |
| 1085 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1086 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1087 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1088 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1089 | ret = len == 0; |
| 1090 | leave: |
| 1091 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1092 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1095 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1096 | * activity for <qc> will be interrupted. |
| 1097 | */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1098 | 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] | 1099 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1100 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1101 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1102 | goto leave; |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1103 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1104 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1105 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1106 | qc->err.code = err.code; |
| 1107 | qc->err.app = err.app; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1108 | leave: |
| 1109 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1110 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1111 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1112 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1113 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1114 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1115 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1116 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 1117 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1118 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1119 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 1120 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1121 | } |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1122 | quic_set_connection_close(qc, quic_err_tls(alert)); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1123 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1124 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1125 | |
| 1126 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1129 | /* Set the application for <qc> QUIC connection. |
| 1130 | * Return 1 if succeeded, 0 if not. |
| 1131 | */ |
| 1132 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1133 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1134 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1135 | qc->app_ops = &h3_ops; |
| 1136 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1137 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1138 | else |
| 1139 | return 0; |
| 1140 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1141 | return 1; |
| 1142 | } |
| 1143 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1144 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1145 | * wants to provide the QUIC layer with CRYPTO data. |
| 1146 | * Returns 1 if succeeded, 0 if not. |
| 1147 | */ |
| 1148 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1149 | const uint8_t *data, size_t len) |
| 1150 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1151 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1152 | enum quic_tls_enc_level tel; |
| 1153 | struct quic_enc_level *qel; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1154 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1155 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1156 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1157 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1158 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1159 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1160 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1161 | goto out; |
| 1162 | } |
| 1163 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1164 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1165 | if (tel == -1) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1166 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1167 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1168 | } |
| 1169 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1170 | qel = &qc->els[tel]; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1171 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1172 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1173 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1176 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1177 | qc, &level, &len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1178 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1179 | ret = 1; |
| 1180 | leave: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1181 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1182 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | int ha_quic_flush_flight(SSL *ssl) |
| 1186 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1187 | 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] | 1188 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1189 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1190 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1191 | |
| 1192 | return 1; |
| 1193 | } |
| 1194 | |
| 1195 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1196 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1197 | 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] | 1198 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1199 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1200 | |
| 1201 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1202 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1203 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1204 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1205 | return 1; |
| 1206 | } |
| 1207 | |
| 1208 | /* QUIC TLS methods */ |
| 1209 | static SSL_QUIC_METHOD ha_quic_method = { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1210 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1211 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1212 | .flush_flight = ha_quic_flush_flight, |
| 1213 | .send_alert = ha_quic_send_alert, |
| 1214 | }; |
| 1215 | |
| 1216 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1217 | * Returns an error count. |
| 1218 | */ |
| 1219 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1220 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1221 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1222 | int cfgerr = 0; |
| 1223 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1224 | long options = |
| 1225 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1226 | SSL_OP_SINGLE_ECDH_USE | |
| 1227 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1228 | SSL_CTX *ctx; |
| 1229 | |
| 1230 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1231 | bind_conf->initial_ctx = ctx; |
| 1232 | |
| 1233 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1234 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1235 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1236 | 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] | 1237 | |
| 1238 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | e06f745 | 2022-06-16 15:06:44 +0200 | [diff] [blame] | 1239 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1240 | if (bind_conf->ssl_conf.early_data) { |
| 1241 | 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] | 1242 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1243 | } |
| 1244 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1245 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1246 | #else |
| 1247 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1248 | #endif |
| 1249 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1250 | #endif |
| 1251 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1252 | |
| 1253 | return cfgerr; |
| 1254 | } |
| 1255 | |
| 1256 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1257 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1258 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1259 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1260 | */ |
| 1261 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1262 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1263 | { |
| 1264 | uint64_t expected_pn = largest_pn + 1; |
| 1265 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1266 | uint64_t pn_hwin = pn_win / 2; |
| 1267 | uint64_t pn_mask = pn_win - 1; |
| 1268 | uint64_t candidate_pn; |
| 1269 | |
| 1270 | |
| 1271 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1272 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1273 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1274 | candidate_pn + pn_hwin <= expected_pn) |
| 1275 | return candidate_pn + pn_win; |
| 1276 | |
| 1277 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1278 | return candidate_pn - pn_win; |
| 1279 | |
| 1280 | return candidate_pn; |
| 1281 | } |
| 1282 | |
| 1283 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1284 | * cryptographic context. |
| 1285 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1286 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1287 | * <end> points to one byte past the end of this packet. |
| 1288 | * Returns 1 if succeeded, 0 if not. |
| 1289 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1290 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1291 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 1292 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1293 | { |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 1294 | int ret, i, pnlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1295 | uint64_t packet_number; |
| 1296 | uint32_t truncated_pn = 0; |
| 1297 | unsigned char mask[5] = {0}; |
| 1298 | unsigned char *sample; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1299 | EVP_CIPHER_CTX *cctx = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1300 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1301 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1302 | |
| 1303 | ret = 0; |
| 1304 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1305 | /* Check there is enough data in this packet. */ |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 1306 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1307 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1308 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | cctx = EVP_CIPHER_CTX_new(); |
| 1312 | if (!cctx) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1313 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1314 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1317 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1318 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 1319 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1320 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1321 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1325 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1326 | for (i = 0; i < pnlen; i++) { |
| 1327 | pn[i] ^= mask[i + 1]; |
| 1328 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1329 | } |
| 1330 | |
| 1331 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1332 | /* Store remaining information for this unprotected header */ |
| 1333 | pkt->pn = packet_number; |
| 1334 | pkt->pnl = pnlen; |
| 1335 | |
| 1336 | ret = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1337 | leave: |
| 1338 | if (cctx) |
| 1339 | EVP_CIPHER_CTX_free(cctx); |
| 1340 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1341 | return ret; |
| 1342 | } |
| 1343 | |
| 1344 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1345 | * address, with <payload_len> as payload length, <aad> as address of |
| 1346 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1347 | * context. |
| 1348 | * Returns 1 if succeeded, 0 if not. |
| 1349 | */ |
| 1350 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1351 | 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] | 1352 | 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] | 1353 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1354 | int ret = 0; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1355 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1356 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1357 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1358 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1359 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1360 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1361 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1362 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1363 | TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1364 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | 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] | 1368 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1369 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1370 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1373 | ret = 1; |
| 1374 | leave: |
| 1375 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1376 | return ret; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1377 | |
| 1378 | err: |
| 1379 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1380 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1384 | * Returns 1 if succeeded, 0 if not. |
| 1385 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1386 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel, |
| 1387 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1388 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1389 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1390 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1391 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1392 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1393 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1394 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1395 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1396 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1397 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1398 | |
| 1399 | ret = 0; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1400 | kp_changed = 0; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1401 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1402 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1403 | /* The two tested bits are not at the same position, |
| 1404 | * this is why they are first both inversed. |
| 1405 | */ |
| 1406 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1407 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1408 | /* The lowest packet number of a previous key phase |
| 1409 | * cannot be null if it really stores previous key phase |
| 1410 | * secrets. |
| 1411 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1412 | // TODO: check if BUG_ON() more suitable |
| 1413 | if (!pkt->qc->ku.prv_rx.pn) { |
| 1414 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1415 | goto leave; |
| 1416 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1417 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1418 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1419 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1420 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1421 | } |
| 1422 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1423 | /* Next key phase */ |
| 1424 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1425 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1426 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1427 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1431 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1432 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1433 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1434 | goto leave; |
| 1435 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1436 | |
| 1437 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1438 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1439 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1440 | if (!ret) { |
| 1441 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1442 | goto leave; |
| 1443 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1444 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1445 | /* Update the keys only if the packet decryption succeeded. */ |
| 1446 | if (kp_changed) { |
| 1447 | quic_tls_rotate_keys(pkt->qc); |
| 1448 | /* Toggle the Key Phase bit */ |
| 1449 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1450 | /* Store the lowest packet number received for the current key phase */ |
| 1451 | tls_ctx->rx.pn = pkt->pn; |
| 1452 | /* Prepare the next key update */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1453 | if (!quic_tls_key_update(pkt->qc)) { |
| 1454 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1455 | goto leave; |
| 1456 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1459 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1460 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1461 | ret = 1; |
| 1462 | leave: |
| 1463 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | 11a6f40 | 2022-08-16 14:48:59 +0200 | [diff] [blame] | 1464 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1467 | |
| 1468 | /* Remove references to <frm> frame */ |
| 1469 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1470 | { |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1471 | struct quic_frame *f, *tmp; |
| 1472 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1473 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1474 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1475 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1476 | f->origin = NULL; |
| 1477 | LIST_DELETE(&f->ref); |
Frédéric Lécaille | eba9088 | 2022-08-25 16:06:48 +0200 | [diff] [blame] | 1478 | if (f->pkt) { |
| 1479 | TRACE_DEVEL("remove frame reference", |
| 1480 | QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key); |
| 1481 | } |
| 1482 | else { |
| 1483 | TRACE_DEVEL("remove frame reference for unsent frame", |
| 1484 | QUIC_EV_CONN_PRSAFRM, qc, f); |
| 1485 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1486 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1487 | |
| 1488 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1491 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1492 | 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] | 1493 | { |
| 1494 | uint64_t pn; |
| 1495 | struct quic_frame *origin, *f, *tmp; |
| 1496 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1497 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1498 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1499 | /* Identify this frame: a frame copy or one of its copies */ |
| 1500 | origin = frm->origin ? frm->origin : frm; |
| 1501 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1502 | * possibly a copy of <origin> |
| 1503 | */ |
| 1504 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1505 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1506 | * not release the packets (releasing the frames) at this time as |
| 1507 | * they are possibly also to be acknowledged alongside the |
| 1508 | * the current one. |
| 1509 | */ |
| 1510 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
Frédéric Lécaille | eba9088 | 2022-08-25 16:06:48 +0200 | [diff] [blame] | 1511 | if (f->pkt) { |
| 1512 | pn = f->pkt->pn_node.key; |
| 1513 | TRACE_DEVEL("mark frame as acked from packet", |
| 1514 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1515 | } |
| 1516 | else { |
| 1517 | TRACE_DEVEL("mark unsend frame as acked", |
| 1518 | QUIC_EV_CONN_PRSAFRM, qc, f); |
| 1519 | } |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1520 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1521 | f->origin = NULL; |
| 1522 | LIST_DELETE(&f->ref); |
| 1523 | } |
| 1524 | LIST_DELETE(&frm->list); |
| 1525 | pn = frm->pkt->pn_node.key; |
| 1526 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1527 | TRACE_DEVEL("freeing frame from packet", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1528 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1529 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1530 | |
| 1531 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1534 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1535 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1536 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1537 | * |
| 1538 | * TODO this should also be called on lost packet detection |
| 1539 | */ |
| 1540 | static void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1541 | { |
| 1542 | struct ssl_sock_ctx *ctx = qc->xprt_ctx; |
| 1543 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1544 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1545 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1546 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1547 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1548 | quic_set_connection_close(qc, qc->err); |
| 1549 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 1550 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1551 | |
| 1552 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1555 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1556 | * |
| 1557 | * 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] | 1558 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1559 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1560 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1561 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1562 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1563 | struct eb64_node *frm_node; |
| 1564 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1565 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1566 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1567 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1568 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1569 | while (frm_node) { |
| 1570 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1571 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1572 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1573 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1574 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1575 | offset = strm->offset.key; |
| 1576 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1577 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1578 | if (offset > stream->ack_offset) |
| 1579 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1580 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1581 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1582 | /* cf. next comment : frame may be freed at this stage. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1583 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1584 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1585 | ret = 1; |
| 1586 | } |
| 1587 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1588 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1589 | * has been freed. with the stream frames tree. Nothing to do |
| 1590 | * anymore in here. |
| 1591 | */ |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1592 | if (!stream) { |
| 1593 | qc_check_close_on_released_mux(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1594 | ret = 1; |
| 1595 | goto leave; |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1596 | } |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1597 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1598 | frm_node = eb64_next(frm_node); |
| 1599 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1600 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1601 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1602 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1603 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1604 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1605 | leave: |
| 1606 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1607 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1608 | } |
| 1609 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1610 | /* 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] | 1611 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1612 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1613 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1614 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1615 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1616 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1617 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1618 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1619 | switch (frm->type) { |
| 1620 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1621 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1622 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1623 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1624 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1625 | const size_t offset = strm_frm->offset.key; |
| 1626 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1627 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1628 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1629 | * 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] | 1630 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1631 | * |
| 1632 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1633 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1634 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1635 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1636 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1637 | TRACE_DEVEL("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] | 1638 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1639 | /* early return */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1640 | goto leave; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1641 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1642 | 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] | 1643 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1644 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1645 | if (offset <= stream->ack_offset) { |
| 1646 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1647 | stream_acked = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1648 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1649 | qc, strm_frm, stream); |
| 1650 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1651 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1652 | if (!stream) { |
| 1653 | /* no need to continue if stream freed. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1654 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1655 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1656 | qc_check_close_on_released_mux(qc); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1657 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1658 | } |
| 1659 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1660 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1661 | qc, strm_frm, stream); |
| 1662 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1663 | } |
| 1664 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1665 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1666 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1667 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1668 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1669 | } |
| 1670 | break; |
| 1671 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1672 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1673 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1674 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1675 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1676 | struct qcc *qcc = qc->qcc; |
| 1677 | |
| 1678 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1679 | tasklet_wakeup(qcc->subs->tasklet); |
| 1680 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1681 | if (!qcc->subs->events) |
| 1682 | qcc->subs = NULL; |
| 1683 | } |
| 1684 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1685 | leave: |
| 1686 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1690 | * deallocating them, and their TX frames. |
| 1691 | * Returns the last node reached to be used for the next range. |
| 1692 | * May be NULL if <largest> node could not be found. |
| 1693 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1694 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1695 | struct eb_root *pkts, |
| 1696 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1697 | struct list *newly_acked_pkts, |
| 1698 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1699 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1700 | { |
| 1701 | struct eb64_node *node; |
| 1702 | struct quic_tx_packet *pkt; |
| 1703 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1704 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1705 | |
Frédéric Lécaille | 843399f | 2022-07-22 16:27:44 +0200 | [diff] [blame] | 1706 | 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] | 1707 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1708 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1709 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1710 | 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] | 1711 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1712 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1713 | TRACE_DEVEL("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] | 1714 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1715 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1716 | node = eb64_prev(node); |
| 1717 | eb64_delete(&pkt->pn_node); |
| 1718 | } |
| 1719 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1720 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1721 | return node; |
| 1722 | } |
| 1723 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1724 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1725 | * 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] | 1726 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1727 | 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] | 1728 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1729 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1730 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1731 | struct quic_frame *frm, *frmbak; |
| 1732 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1733 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | 1ba25c2 | 2022-08-18 17:21:19 +0200 | [diff] [blame] | 1734 | uint64_t pn = pkt->pn_node.key; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1735 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1736 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1737 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1738 | 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] | 1739 | /* 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] | 1740 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 1ba25c2 | 2022-08-18 17:21:19 +0200 | [diff] [blame] | 1741 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1742 | /* 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] | 1743 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1744 | /* Remove any reference to this frame */ |
| 1745 | qc_frm_unref(qc, frm); |
| 1746 | switch (frm->type) { |
| 1747 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1748 | { |
| 1749 | struct quic_stream *strm_frm = &frm->stream; |
| 1750 | struct eb64_node *node = NULL; |
| 1751 | struct qc_stream_desc *stream_desc; |
| 1752 | |
| 1753 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1754 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1755 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1756 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1757 | qc, frm, &pn); |
| 1758 | pool_free(pool_head_quic_frame, frm); |
| 1759 | continue; |
| 1760 | } |
| 1761 | |
| 1762 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1763 | /* Do not resend this frame if in the "already acked range" */ |
| 1764 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1765 | TRACE_DEVEL("ignored frame in already acked range", |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1766 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | bccbad2 | 2022-08-31 15:02:53 +0200 | [diff] [blame] | 1767 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1768 | continue; |
| 1769 | } |
| 1770 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1771 | strm_frm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1772 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1773 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1774 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1775 | break; |
| 1776 | } |
| 1777 | |
| 1778 | default: |
| 1779 | break; |
| 1780 | } |
| 1781 | |
| 1782 | /* Do not resend probing packet with old data */ |
| 1783 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1784 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1785 | qc, frm, &pn); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1786 | if (frm->origin) |
| 1787 | LIST_DELETE(&frm->ref); |
| 1788 | pool_free(pool_head_quic_frame, frm); |
| 1789 | continue; |
| 1790 | } |
| 1791 | |
| 1792 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1793 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1794 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1795 | qc, frm, &pn); |
| 1796 | pool_free(pool_head_quic_frame, frm); |
| 1797 | } |
| 1798 | else { |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1799 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1800 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1801 | * will be performed to check the stream is not consumed or released. |
| 1802 | */ |
Amaury Denoyelle | f372e74 | 2022-08-16 14:41:57 +0200 | [diff] [blame] | 1803 | frm->flags |= QUIC_FL_TX_FRAME_LOST; |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1804 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1805 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1806 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1807 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1808 | } |
| 1809 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1810 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1811 | |
| 1812 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1813 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1814 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1815 | /* Free <pkt> TX packet and its attached frames. |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1816 | * This is the responsibility of the caller to remove this packet of |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1817 | * any data structure it was possibly attached to. |
| 1818 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1819 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1820 | struct quic_tx_packet *pkt) |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1821 | { |
| 1822 | struct quic_frame *frm, *frmbak; |
| 1823 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1824 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1825 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1826 | if (!pkt) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1827 | goto leave; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1828 | |
| 1829 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1830 | LIST_DELETE(&frm->list); |
| 1831 | pool_free(pool_head_quic_frame, frm); |
| 1832 | } |
| 1833 | pool_free(pool_head_quic_tx_packet, pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1834 | |
| 1835 | leave: |
| 1836 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1839 | /* Free the TX packets of <pkts> list */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1840 | static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1841 | { |
| 1842 | struct quic_tx_packet *pkt, *tmp; |
| 1843 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1844 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1845 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1846 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
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 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1849 | free_quic_tx_packet(qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1850 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1851 | |
| 1852 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1855 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1856 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1857 | * updating the range which contains <largest_acked_pn>. |
| 1858 | * Never fails. |
| 1859 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1860 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1861 | struct quic_pktns *pktns, |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1862 | int64_t largest_acked_pn) |
| 1863 | { |
| 1864 | struct eb64_node *ar, *next_ar; |
| 1865 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1866 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1867 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1868 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1869 | ar = eb64_first(&arngs->root); |
| 1870 | while (ar) { |
| 1871 | struct quic_arng_node *ar_node; |
| 1872 | |
| 1873 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1874 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1875 | |
| 1876 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 1877 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1878 | break; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1879 | } |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1880 | |
| 1881 | if (largest_acked_pn < ar_node->last) { |
| 1882 | eb64_delete(ar); |
| 1883 | ar_node->first.key = largest_acked_pn + 1; |
| 1884 | eb64_insert(&arngs->root, ar); |
| 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | eb64_delete(ar); |
| 1889 | pool_free(pool_head_quic_arng, ar_node); |
| 1890 | arngs->sz--; |
| 1891 | ar = next_ar; |
| 1892 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1893 | |
| 1894 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1897 | /* Send a packet ack event nofication for each newly acked packet of |
| 1898 | * <newly_acked_pkts> list and free them. |
| 1899 | * Always succeeds. |
| 1900 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1901 | 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] | 1902 | struct list *newly_acked_pkts) |
| 1903 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1904 | struct quic_tx_packet *pkt, *tmp; |
| 1905 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1906 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1907 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1908 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1909 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1910 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1911 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1912 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1913 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1914 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1915 | /* If this packet contained an ACK frame, proceed to the |
| 1916 | * acknowledging of range of acks from the largest acknowledged |
| 1917 | * packet number which was sent in an ACK frame by this packet. |
| 1918 | */ |
| 1919 | if (pkt->largest_acked_pn != -1) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1920 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1921 | ev.ack.acked = pkt->in_flight_len; |
| 1922 | ev.ack.time_sent = pkt->time_sent; |
| 1923 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1924 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1925 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1926 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1929 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1930 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1933 | /* Release all the frames attached to <pktns> packet number space */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1934 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 1935 | struct quic_pktns *pktns) |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1936 | { |
| 1937 | struct quic_frame *frm, *frmbak; |
| 1938 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1939 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 1940 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1941 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1942 | LIST_DELETE(&frm->list); |
| 1943 | pool_free(pool_head_quic_frame, frm); |
| 1944 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1945 | |
| 1946 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1949 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1950 | * their TX frames. |
| 1951 | * Send a packet loss event to the congestion controller if |
| 1952 | * in flight packet have been lost. |
| 1953 | * Also frees the packet in <pkts> list. |
| 1954 | * Never fails. |
| 1955 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1956 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1957 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1958 | struct list *pkts, |
| 1959 | uint64_t now_us) |
| 1960 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1961 | 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] | 1962 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1963 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1964 | |
Frédéric Lécaille | 277c462 | 2022-08-24 17:06:04 +0200 | [diff] [blame] | 1965 | if (LIST_ISEMPTY(pkts)) |
| 1966 | goto leave; |
| 1967 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1968 | oldest_lost = newest_lost = NULL; |
| 1969 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1970 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1971 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1972 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1973 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1974 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1975 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1976 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1977 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1978 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1979 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1980 | if (!oldest_lost) { |
| 1981 | oldest_lost = newest_lost = pkt; |
| 1982 | } |
| 1983 | else { |
| 1984 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1985 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1986 | newest_lost = pkt; |
| 1987 | } |
| 1988 | } |
| 1989 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1990 | if (newest_lost) { |
| 1991 | /* Sent a congestion event to the controller */ |
Benoit DOLEZ | 69e3f05 | 2022-06-08 09:28:56 +0200 | [diff] [blame] | 1992 | struct quic_cc_event ev = { }; |
| 1993 | |
| 1994 | ev.type = QUIC_CC_EVT_LOSS; |
| 1995 | ev.loss.time_sent = newest_lost->time_sent; |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1996 | |
| 1997 | quic_cc_event(&qc->path->cc, &ev); |
| 1998 | } |
| 1999 | |
| 2000 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2001 | * We must check if we are experiencing a persistent congestion. |
| 2002 | * If this is the case, the congestion controller must re-enter |
| 2003 | * slow start state. |
| 2004 | */ |
| 2005 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2006 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 2007 | |
| 2008 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2009 | now_ms, qc->max_ack_delay)) |
| 2010 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2011 | } |
| 2012 | |
Frédéric Lécaille | 277c462 | 2022-08-24 17:06:04 +0200 | [diff] [blame] | 2013 | quic_tx_packet_refdec(oldest_lost); |
| 2014 | if (newest_lost != oldest_lost) |
| 2015 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2016 | |
Frédéric Lécaille | 277c462 | 2022-08-24 17:06:04 +0200 | [diff] [blame] | 2017 | leave: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2018 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2021 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2022 | * 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] | 2023 | * 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] | 2024 | * acked ack-eliciting packet. |
| 2025 | * Return 1, if succeeded, 0 if not. |
| 2026 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2027 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2028 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2029 | struct quic_enc_level *qel, |
| 2030 | unsigned int *rtt_sample, |
| 2031 | const unsigned char **pos, const unsigned char *end) |
| 2032 | { |
| 2033 | struct quic_ack *ack = &frm->ack; |
| 2034 | uint64_t smallest, largest; |
| 2035 | struct eb_root *pkts; |
| 2036 | struct eb64_node *largest_node; |
| 2037 | unsigned int time_sent, pkt_flags; |
| 2038 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2039 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2040 | int ret = 0; |
| 2041 | |
| 2042 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2043 | |
| 2044 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2045 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2046 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2047 | goto err; |
| 2048 | } |
| 2049 | |
| 2050 | if (ack->first_ack_range > ack->largest_ack) { |
| 2051 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2052 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2053 | goto err; |
| 2054 | } |
| 2055 | |
| 2056 | largest = ack->largest_ack; |
| 2057 | smallest = largest - ack->first_ack_range; |
| 2058 | pkts = &qel->pktns->tx.pkts; |
| 2059 | pkt_flags = 0; |
| 2060 | largest_node = NULL; |
| 2061 | time_sent = 0; |
| 2062 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2063 | 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] | 2064 | largest_node = eb64_lookup(pkts, largest); |
| 2065 | if (!largest_node) { |
| 2066 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2067 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 2068 | } |
| 2069 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2070 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 2071 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2072 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2075 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2076 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2077 | do { |
| 2078 | uint64_t gap, ack_range; |
| 2079 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2080 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2081 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2082 | if (!ack->ack_range_num--) |
| 2083 | break; |
| 2084 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2085 | if (!quic_dec_int(&gap, pos, end)) { |
| 2086 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2087 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2088 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2089 | |
| 2090 | if (smallest < gap + 2) { |
| 2091 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2092 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2093 | goto err; |
| 2094 | } |
| 2095 | |
| 2096 | largest = smallest - gap - 2; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2097 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2098 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2099 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2100 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | |
| 2102 | if (largest < ack_range) { |
| 2103 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2104 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2105 | goto err; |
| 2106 | } |
| 2107 | |
| 2108 | /* Do not use this node anymore. */ |
| 2109 | largest_node = NULL; |
| 2110 | /* Next range */ |
| 2111 | smallest = largest - ack_range; |
| 2112 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2113 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2114 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2115 | } while (1); |
| 2116 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2117 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2118 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2119 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2123 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2124 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | 277c462 | 2022-08-24 17:06:04 +0200 | [diff] [blame] | 2125 | 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] | 2126 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2127 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2128 | if (quic_peer_validated_addr(qc)) |
| 2129 | qc->path->loss.pto_count = 0; |
| 2130 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2133 | ret = 1; |
| 2134 | leave: |
| 2135 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2136 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2137 | |
| 2138 | err: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2139 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2140 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2143 | /* This function gives the detail of the SSL error. It is used only |
| 2144 | * if the debug mode and the verbose mode are activated. It dump all |
| 2145 | * the SSL error until the stack was empty. |
| 2146 | */ |
| 2147 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2148 | { |
| 2149 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2150 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2151 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2152 | unsigned long ret; |
| 2153 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2154 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2155 | ret = ERR_get_error(); |
| 2156 | if (!ret) |
| 2157 | return; |
| 2158 | |
| 2159 | 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] | 2160 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2165 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2166 | const char **str, int *len); |
| 2167 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2168 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2169 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2170 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2171 | * Return 1 if succeeded, 0 if not. |
| 2172 | */ |
| 2173 | 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] | 2174 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2175 | const unsigned char *data, size_t len, |
| 2176 | struct quic_rx_packet *pkt, |
| 2177 | struct quic_rx_crypto_frm *cf) |
| 2178 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2179 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2180 | struct quic_conn *qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2181 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2182 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2183 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2184 | qc = ctx->qc; |
| 2185 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2186 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2187 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2188 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2189 | TRACE_ERROR("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2190 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2191 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | el->rx.crypto.offset += len; |
| 2195 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2196 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2197 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2198 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2199 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2200 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2201 | if (ssl_err != 1) { |
| 2202 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2203 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2204 | TRACE_PROTO("SSL handshake in progress", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2205 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2206 | goto out; |
| 2207 | } |
| 2208 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2209 | /* TODO: Should close the connection asap */ |
| 2210 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2211 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2212 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2213 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2214 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2215 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 2216 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2217 | ERR_clear_error(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2218 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2219 | } |
| 2220 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2221 | 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] | 2222 | |
| 2223 | /* Check the alpn could be negotiated */ |
| 2224 | if (!qc->app_ops) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2225 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | bc964bd | 2022-04-13 16:20:09 +0200 | [diff] [blame] | 2226 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2227 | goto leave; |
Frédéric Lécaille | bc964bd | 2022-04-13 16:20:09 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2230 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2231 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2232 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2233 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2234 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2235 | /* I/O callback switch */ |
| 2236 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2237 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2238 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2239 | /* The connection is ready to be accepted. */ |
| 2240 | quic_accept_push_qc(qc); |
| 2241 | } |
| 2242 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2243 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2244 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2245 | } else { |
| 2246 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2247 | if (ssl_err != 1) { |
| 2248 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2249 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2250 | TRACE_PROTO("SSL post handshake in progress", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2251 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2252 | goto out; |
| 2253 | } |
| 2254 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2255 | TRACE_ERROR("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2256 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2257 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2260 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2261 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2262 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2263 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2264 | ret = 1; |
| 2265 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2266 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2267 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2270 | /* Parse a STREAM frame <strm_frm> |
| 2271 | * |
| 2272 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2273 | * containing the frame must not be acknowledged. |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2274 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2275 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2276 | struct quic_stream *strm_frm, |
| 2277 | struct quic_conn *qc) |
| 2278 | { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2279 | int ret; |
| 2280 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2281 | /* RFC9000 13.1. Packet Processing |
| 2282 | * |
| 2283 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2284 | * successfully removed and all frames contained in the packet have |
| 2285 | * been processed. For STREAM frames, this means the data has been |
| 2286 | * enqueued in preparation to be received by the application protocol, |
| 2287 | * but it does not require that data be delivered and consumed. |
| 2288 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2289 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2290 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2291 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2292 | strm_frm->offset.key, strm_frm->fin, |
| 2293 | (char *)strm_frm->data); |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2294 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2295 | /* frame rejected - packet must not be acknowledeged */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2296 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2297 | return !ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2298 | } |
| 2299 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2300 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2301 | * for <qc> QUIC connection. |
| 2302 | * This is a best effort function which never fails even if no memory could be |
| 2303 | * allocated to duplicate these frames. |
| 2304 | */ |
| 2305 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2306 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2307 | { |
| 2308 | struct quic_frame *frm, *frmbak; |
| 2309 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2310 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2311 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2312 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2313 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2314 | struct quic_frame *dup_frm, *origin; |
| 2315 | |
| 2316 | switch (frm->type) { |
| 2317 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2318 | { |
| 2319 | struct quic_stream *strm_frm = &frm->stream; |
| 2320 | struct eb64_node *node = NULL; |
| 2321 | struct qc_stream_desc *stream_desc; |
| 2322 | |
| 2323 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2324 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2325 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2326 | continue; |
| 2327 | } |
| 2328 | |
| 2329 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2330 | /* Do not resend this frame if in the "already acked range" */ |
| 2331 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2332 | TRACE_DEVEL("ignored frame in already acked range", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2333 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2334 | continue; |
| 2335 | } |
| 2336 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2337 | strm_frm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2338 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2339 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2340 | } |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | |
| 2344 | default: |
| 2345 | break; |
| 2346 | } |
| 2347 | |
Frédéric Lécaille | 4173a39 | 2022-08-18 08:20:47 +0200 | [diff] [blame] | 2348 | dup_frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2349 | if (!dup_frm) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2350 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | /* If <frm> is already a copy of another frame, we must take |
| 2355 | * its original frame as source for the copy. |
| 2356 | */ |
| 2357 | origin = frm->origin ? frm->origin : frm; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2358 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Frédéric Lécaille | e35463c | 2022-08-27 10:19:42 +0200 | [diff] [blame] | 2359 | if (origin->pkt) |
| 2360 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
| 2361 | qc, NULL, &origin->pkt->pn_node.key); |
| 2362 | else { |
| 2363 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2364 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2365 | } |
Frédéric Lécaille | 4173a39 | 2022-08-18 08:20:47 +0200 | [diff] [blame] | 2366 | *dup_frm = *origin; |
| 2367 | dup_frm->pkt = NULL; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2368 | dup_frm->origin = origin; |
Frédéric Lécaille | 4173a39 | 2022-08-18 08:20:47 +0200 | [diff] [blame] | 2369 | dup_frm->flags = 0; |
| 2370 | LIST_INIT(&dup_frm->reflist); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2371 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2372 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2373 | } |
| 2374 | |
| 2375 | LIST_SPLICE(out_frm_list, &tmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2376 | |
| 2377 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2380 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2381 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2382 | struct quic_enc_level *qel, |
| 2383 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2384 | { |
| 2385 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2386 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2387 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2388 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2389 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2390 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2391 | |
| 2392 | BUG_ON(frms1 == frms2); |
| 2393 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2394 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2395 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2396 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2397 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2398 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2399 | node = eb64_next(node); |
| 2400 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2401 | 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] | 2402 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | if (!pkt) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2406 | goto leave; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2407 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2408 | /* When building a packet from another one, the field which may increase the |
| 2409 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2410 | */ |
| 2411 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2412 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
Frédéric Lécaille | 3a9b944 | 2022-08-30 15:25:47 +0200 | [diff] [blame] | 2413 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2414 | goto leave; |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
Frédéric Lécaille | 3a9b944 | 2022-08-30 15:25:47 +0200 | [diff] [blame] | 2417 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2418 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2419 | if (frms == frms1 && frms2) { |
| 2420 | frms = frms2; |
| 2421 | goto start; |
| 2422 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2423 | leave: |
Frédéric Lécaille | 3a9b944 | 2022-08-30 15:25:47 +0200 | [diff] [blame] | 2424 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2427 | /* 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] | 2428 | * 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] | 2429 | * Initial packets send them coalescing with others (Handshake here). |
| 2430 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2431 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2432 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2433 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2434 | { |
| 2435 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2436 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2437 | |
| 2438 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2439 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2440 | struct quic_enc_level *qel = iqel; |
| 2441 | struct eb_root *pkts; |
| 2442 | struct eb64_node *node; |
| 2443 | struct quic_tx_packet *pkt; |
| 2444 | struct list *tmp = &itmp; |
| 2445 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2446 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2447 | start: |
| 2448 | pkt = NULL; |
| 2449 | pkts = &qel->pktns->tx.pkts; |
| 2450 | node = eb64_first(pkts); |
| 2451 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2452 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2453 | 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] | 2454 | 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] | 2455 | break; |
| 2456 | node = eb64_next(node); |
| 2457 | } |
| 2458 | |
| 2459 | if (!pkt) |
| 2460 | goto end; |
| 2461 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2462 | /* When building a packet from another one, the field which may increase the |
| 2463 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2464 | */ |
| 2465 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2466 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2467 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2468 | goto end; |
| 2469 | } |
| 2470 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2471 | qel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2472 | |
| 2473 | /* No risk to loop here, #packet per datagram is bounded */ |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2474 | requeue: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2475 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2476 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2477 | if (qel == iqel) { |
| 2478 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2479 | pkt = pkt->next; |
| 2480 | tmp = &htmp; |
| 2481 | hqel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2482 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2483 | goto requeue; |
| 2484 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2485 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2486 | |
| 2487 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2488 | LIST_SPLICE(ifrms, &itmp); |
| 2489 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2490 | |
| 2491 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2494 | 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] | 2495 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2496 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2497 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2498 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2499 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2500 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2501 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2502 | goto out; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2503 | |
| 2504 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2505 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2506 | |
| 2507 | out: |
| 2508 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 2509 | } |
| 2510 | |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2511 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2512 | * frame list to send. |
| 2513 | * Return 1 if succeeded, 0 if not. |
| 2514 | */ |
| 2515 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2516 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2517 | int ret = 0; |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2518 | struct quic_frame *frm; |
| 2519 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2520 | uint64_t app_error_code; |
| 2521 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2522 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2523 | |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2524 | /* TODO: the mux may be released, we cannot have more |
| 2525 | * information about the application error code to send |
| 2526 | * at this time. |
| 2527 | */ |
| 2528 | app_error_code = H3_REQUEST_REJECTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2529 | // fixme: zalloc |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2530 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2531 | if (!frm) { |
| 2532 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2533 | goto out; |
| 2534 | } |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2535 | |
| 2536 | frm->type = QUIC_FT_STOP_SENDING; |
| 2537 | frm->stop_sending.id = id; |
| 2538 | frm->stop_sending.app_error_code = app_error_code; |
| 2539 | LIST_INIT(&frm->reflist); |
| 2540 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2541 | ret = 1; |
| 2542 | out: |
| 2543 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2544 | return ret; |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2547 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2548 | * as I/O handler context and <qel> as encryption level. |
| 2549 | * Returns 1 if succeeded, 0 if failed. |
| 2550 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2551 | 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] | 2552 | struct quic_enc_level *qel) |
| 2553 | { |
| 2554 | struct quic_frame frm; |
| 2555 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2556 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2557 | int fast_retrans = 0, ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2558 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2559 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2560 | /* Skip the AAD */ |
| 2561 | pos = pkt->data + pkt->aad_len; |
| 2562 | end = pkt->data + pkt->len; |
| 2563 | |
| 2564 | while (pos < end) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2565 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2566 | // trace already emitted by function above |
| 2567 | goto leave; |
| 2568 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2569 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2570 | 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] | 2571 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2572 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2573 | break; |
| 2574 | case QUIC_FT_PING: |
| 2575 | break; |
| 2576 | case QUIC_FT_ACK: |
| 2577 | { |
| 2578 | unsigned int rtt_sample; |
| 2579 | |
| 2580 | rtt_sample = 0; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2581 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2582 | // trace already emitted by function above |
| 2583 | goto leave; |
| 2584 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2585 | |
| 2586 | if (rtt_sample) { |
| 2587 | unsigned int ack_delay; |
| 2588 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2589 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2590 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2591 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2592 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2593 | 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] | 2594 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2595 | break; |
| 2596 | } |
Frédéric Lécaille | e06ca65 | 2022-05-29 11:48:58 +0200 | [diff] [blame] | 2597 | case QUIC_FT_RESET_STREAM: |
| 2598 | /* TODO: handle this frame at STREAM level */ |
| 2599 | break; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2600 | case QUIC_FT_STOP_SENDING: |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2601 | { |
| 2602 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2603 | if (qc->mux_state == QC_MUX_READY) { |
| 2604 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2605 | stop_sending->app_error_code)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2606 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2607 | goto leave; |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2608 | } |
| 2609 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2610 | break; |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2611 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2612 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2613 | { |
| 2614 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2615 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2616 | 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] | 2617 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2618 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2619 | |
| 2620 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2621 | cfdebug.len = frm.crypto.len; |
| 2622 | TRACE_PROTO("CRYPTO data discarded", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2623 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2624 | break; |
| 2625 | } |
| 2626 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2627 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2628 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2629 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2630 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2631 | |
| 2632 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2633 | cfdebug.len = frm.crypto.len; |
| 2634 | /* Nothing to do */ |
| 2635 | TRACE_PROTO("Already received CRYPTO data", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2636 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2637 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2638 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2639 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2640 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2641 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2642 | else { |
| 2643 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2644 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2645 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2646 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2647 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2648 | cfdebug.len = frm.crypto.len; |
| 2649 | TRACE_PROTO("Partially already received CRYPTO data", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2650 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2651 | frm.crypto.len -= diff; |
| 2652 | frm.crypto.data += diff; |
| 2653 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2654 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2655 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2656 | |
| 2657 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2658 | /* 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] | 2659 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2660 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2661 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2662 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2663 | if (!qc_provide_cdata(qel, ctx, |
| 2664 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2665 | pkt, &cfdebug)) { |
| 2666 | // trace already emitted by function above |
| 2667 | goto leave; |
| 2668 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2669 | |
| 2670 | break; |
| 2671 | } |
| 2672 | |
| 2673 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2674 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2675 | if (!cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2676 | TRACE_ERROR("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2677 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2678 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2679 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2680 | |
| 2681 | cf->offset_node.key = frm.crypto.offset; |
| 2682 | cf->len = frm.crypto.len; |
| 2683 | cf->data = frm.crypto.data; |
| 2684 | cf->pkt = pkt; |
| 2685 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2686 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2687 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2688 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2689 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2690 | { |
| 2691 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2692 | 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] | 2693 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2694 | /* The upper layer may not be allocated. */ |
| 2695 | if (qc->mux_state != QC_MUX_READY) { |
| 2696 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2697 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2698 | break; |
| 2699 | } |
| 2700 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2701 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2702 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2703 | TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2704 | /* This packet will not be acknowledged */ |
| 2705 | goto leave; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2706 | } |
| 2707 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2708 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2709 | if (!qc_handle_strm_frm(pkt, stream, qc)) { |
| 2710 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2711 | goto leave; |
| 2712 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2713 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2714 | break; |
| 2715 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2716 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2717 | if (qc->mux_state == QC_MUX_READY) { |
| 2718 | struct quic_max_data *data = &frm.max_data; |
| 2719 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2720 | } |
| 2721 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2722 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2723 | if (qc->mux_state == QC_MUX_READY) { |
| 2724 | struct quic_max_stream_data *data = &frm.max_stream_data; |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 2725 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2726 | data->max_stream_data)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2727 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2728 | goto leave; |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 2729 | } |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2730 | } |
| 2731 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2732 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2733 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2734 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2735 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2736 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2737 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2738 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2739 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2740 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2741 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2742 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2743 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2744 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2745 | 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] | 2746 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2747 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2748 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2749 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2750 | break; |
| 2751 | case QUIC_FT_CONNECTION_CLOSE: |
| 2752 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2753 | /* Increment the error counters */ |
| 2754 | qc_cc_err_count_inc(qc, &frm); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2755 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2756 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2757 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 2758 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2759 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2760 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2761 | /* RFC 9000 10.2. Immediate Close: |
| 2762 | * The closing and draining connection states exist to ensure |
| 2763 | * that connections close cleanly and that delayed or reordered |
| 2764 | * packets are properly discarded. These states SHOULD persist |
| 2765 | * for at least three times the current PTO interval... |
| 2766 | * |
| 2767 | * Rearm the idle timeout only one time when entering draining |
| 2768 | * state. |
| 2769 | */ |
| 2770 | qc_idle_timer_do_rearm(qc); |
| 2771 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2772 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2773 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2774 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2775 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2776 | if (qc_is_listener(ctx->qc)) { |
| 2777 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 2778 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2779 | goto leave; |
| 2780 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2781 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2782 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2783 | break; |
| 2784 | default: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2785 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 2786 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2790 | /* 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] | 2791 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2792 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2793 | if (fast_retrans) { |
| 2794 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2795 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2796 | |
| 2797 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2798 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2799 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2800 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2801 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2802 | * be discarded. |
| 2803 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2804 | 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] | 2805 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2806 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2807 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2808 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2809 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2810 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2811 | qc_set_timer(ctx->qc); |
| 2812 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2813 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2814 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2815 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2816 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2817 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2818 | } |
| 2819 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2820 | ret = 1; |
| 2821 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2822 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2823 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 2826 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 2827 | * |
| 2828 | * Returns allocated buffer or NULL on error. |
| 2829 | */ |
| 2830 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 2831 | { |
| 2832 | struct buffer *buf = &qc->tx.buf; |
| 2833 | if (!b_alloc(buf)) |
| 2834 | return NULL; |
| 2835 | |
| 2836 | return buf; |
| 2837 | } |
| 2838 | |
| 2839 | /* Free Tx buffer from <qc> if it is empty. */ |
| 2840 | static void qc_txb_release(struct quic_conn *qc) |
| 2841 | { |
| 2842 | struct buffer *buf = &qc->tx.buf; |
| 2843 | |
| 2844 | /* For the moment sending function is responsible to purge the buffer |
| 2845 | * entirely. It may change in the future but this requires to be able |
| 2846 | * to reuse old data. |
| 2847 | */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2848 | BUG_ON_HOT(buf && b_data(buf)); |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 2849 | |
| 2850 | if (!b_data(buf)) { |
| 2851 | b_free(buf); |
| 2852 | offer_buffers(NULL, 1); |
| 2853 | } |
| 2854 | } |
| 2855 | |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2856 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 2857 | * must contains the address of the first packet stored in the payload. |
| 2858 | * |
| 2859 | * Caller is responsible that there is enough space in the buffer. |
| 2860 | */ |
| 2861 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 2862 | struct quic_tx_packet *first_pkt) |
| 2863 | { |
| 2864 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 2865 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 2866 | |
| 2867 | write_u16(b_tail(buf), length); |
| 2868 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 2869 | b_add(buf, hdlen + length); |
| 2870 | } |
| 2871 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2872 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2873 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2874 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2875 | * <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] | 2876 | * <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] | 2877 | */ |
| 2878 | 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] | 2879 | 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] | 2880 | { |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2881 | unsigned int must_ack = force_ack || |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2882 | (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] | 2883 | |
| 2884 | /* Do not build any more packet if the TX secrets are not available or |
| 2885 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2886 | * and if there is no more packets to send upon PTO expiration |
| 2887 | * and if there is no more ack-eliciting frames to send or in flight |
| 2888 | * congestion control limit is reached for prepared data |
| 2889 | */ |
| 2890 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2891 | (!cc && !probe && !must_ack && |
| 2892 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2893 | return 0; |
| 2894 | } |
| 2895 | |
| 2896 | return 1; |
| 2897 | } |
| 2898 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2899 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 2900 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 2901 | * |
| 2902 | * Each datagram is prepended by a two fields header : the datagram length and |
| 2903 | * the address of the packet contained in the datagram. |
| 2904 | * |
| 2905 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 2906 | * -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2907 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2908 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 2909 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2910 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2911 | int ret = -1; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2912 | struct quic_enc_level *qel; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2913 | unsigned char *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2914 | struct quic_tx_packet *pkt; |
| 2915 | size_t total; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2916 | /* Each datagram is prepended with its length followed by the address |
| 2917 | * of the first packet in the datagram. |
| 2918 | */ |
| 2919 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2920 | |
| 2921 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2922 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2923 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2924 | total = 0; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2925 | pos = (unsigned char *)b_tail(buf); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2926 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2927 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2928 | |
| 2929 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2930 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2931 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2932 | /* We do not probe if an immediate close was asked */ |
| 2933 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2934 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2935 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2936 | 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] | 2937 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* Leave room for the datagram header */ |
| 2940 | pos += dg_headlen; |
| 2941 | 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] | 2942 | 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] | 2943 | } |
| 2944 | else { |
| 2945 | end = pos + qc->path->mtu; |
| 2946 | } |
| 2947 | |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2948 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 2949 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2950 | switch (err) { |
| 2951 | case -2: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2952 | // trace already emitted by function above |
| 2953 | goto leave; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2954 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2955 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2956 | * MTU, we are here because of the congestion control window. There is |
| 2957 | * no need to try to reuse this buffer. |
| 2958 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2959 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2960 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2961 | default: |
| 2962 | break; |
| 2963 | } |
| 2964 | |
| 2965 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2966 | BUG_ON(!pkt); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2967 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2968 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2969 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2970 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2971 | total += pkt->len; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2972 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2973 | /* Write datagram header. */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2974 | qc_txb_store(buf, pkt->len, pkt); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2978 | ret = total; |
| 2979 | leave: |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2980 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 2981 | return ret; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2982 | } |
| 2983 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2984 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 2985 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 2986 | * written into <buf>. |
| 2987 | * |
| 2988 | * Each datagram is prepended by a two fields header : the datagram length and |
| 2989 | * the address of first packet in the datagram. |
| 2990 | * |
| 2991 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 2992 | * -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2993 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2994 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2995 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2996 | 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] | 2997 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2998 | struct quic_enc_level *qel; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2999 | unsigned char *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3000 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3001 | /* length of datagrams */ |
| 3002 | uint16_t dglen; |
| 3003 | size_t total; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3004 | int ret = -1, padding; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3005 | /* Each datagram is prepended with its length followed by the address |
| 3006 | * of the first packet in the datagram. |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3007 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3008 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3009 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3010 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3011 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3012 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3013 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
| 3014 | * caller must ensure that buf is resetted. |
| 3015 | */ |
| 3016 | BUG_ON_HOT(buf->head || buf->data); |
| 3017 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 3018 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3019 | qel = &qc->els[tel]; |
| 3020 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3021 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3022 | padding = 0; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3023 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3024 | first_pkt = prv_pkt = NULL; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3025 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3026 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3027 | enum quic_pkt_type pkt_type; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 3028 | struct quic_tls_ctx *tls_ctx; |
| 3029 | const struct quic_version *ver; |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 3030 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3031 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3032 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3033 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3034 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3035 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3036 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3037 | /* We do not probe if an immediate close was asked */ |
| 3038 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 3039 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3040 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 3041 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3042 | if (prv_pkt) |
| 3043 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3044 | /* Let's select the next encryption level */ |
| 3045 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3046 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3047 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3048 | qel = &qc->els[tel]; |
| 3049 | /* Build a new datagram */ |
| 3050 | prv_pkt = NULL; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3051 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3052 | continue; |
| 3053 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3054 | break; |
| 3055 | } |
| 3056 | |
| 3057 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3058 | if (!prv_pkt) { |
| 3059 | /* Leave room for the datagram header */ |
| 3060 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3061 | 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] | 3062 | 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] | 3063 | } |
| 3064 | else { |
| 3065 | end = pos + qc->path->mtu; |
| 3066 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3067 | } |
| 3068 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 3069 | if (qc->negotiated_version) { |
| 3070 | ver = qc->negotiated_version; |
| 3071 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3072 | tls_ctx = &qc->negotiated_ictx; |
| 3073 | else |
| 3074 | tls_ctx = &qel->tls_ctx; |
| 3075 | } |
| 3076 | else { |
| 3077 | ver = qc->original_version; |
| 3078 | tls_ctx = &qel->tls_ctx; |
| 3079 | } |
| 3080 | |
| 3081 | 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] | 3082 | qc, ver, dglen, pkt_type, |
| 3083 | force_ack, padding, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3084 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3085 | case -2: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3086 | // trace already emitted by function above |
| 3087 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3088 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3089 | /* If there was already a correct packet present, set the |
| 3090 | * current datagram as prepared into <cbuf>. |
| 3091 | */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3092 | if (prv_pkt) |
| 3093 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3094 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3095 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3096 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3097 | break; |
| 3098 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 3099 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3100 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3101 | BUG_ON(!cur_pkt); |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3102 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 3103 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3104 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3105 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3106 | total += cur_pkt->len; |
| 3107 | /* keep trace of the first packet in the datagram */ |
| 3108 | if (!first_pkt) |
| 3109 | first_pkt = cur_pkt; |
| 3110 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3111 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3112 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3113 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3114 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3115 | /* Let's say we have to build a new dgram */ |
| 3116 | prv_pkt = NULL; |
| 3117 | dglen += cur_pkt->len; |
| 3118 | /* Client: discard the Initial encryption keys as soon as |
| 3119 | * a handshake packet could be built. |
| 3120 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3121 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3122 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3123 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3124 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3125 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3126 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 3127 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3128 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3129 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3130 | } |
| 3131 | /* If the data for the current encryption level have all been sent, |
| 3132 | * select the next level. |
| 3133 | */ |
| 3134 | 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] | 3135 | 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] | 3136 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3137 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3138 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3139 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3140 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3141 | frms = &qc->els[tel].pktns->tx.frms; |
| 3142 | else |
| 3143 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3144 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3145 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3146 | /* If there is data for the next level, do not |
| 3147 | * consume a datagram. |
| 3148 | */ |
| 3149 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3150 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3151 | } |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3152 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3153 | /* If we have to build a new datagram, set the current datagram as |
| 3154 | * prepared into <cbuf>. |
| 3155 | */ |
| 3156 | if (!prv_pkt) { |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3157 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3158 | first_pkt = NULL; |
| 3159 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3160 | padding = 0; |
| 3161 | } |
| 3162 | 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] | 3163 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3164 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3165 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3169 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3170 | ret = total; |
| 3171 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3172 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3173 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3176 | /* Send datagrams stored in <buf>. |
| 3177 | * |
| 3178 | * This function always returns 1 for success. Even if sendto() syscall failed, |
| 3179 | * buffer is drained and packets are considered as emitted. QUIC loss detection |
| 3180 | * mechanism is used as a back door way to retry sending. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3181 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3182 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3183 | { |
| 3184 | struct quic_conn *qc; |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3185 | char skip_sendto = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3186 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3187 | qc = ctx->qc; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3188 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3189 | while (b_contig_data(buf, 0)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3190 | unsigned char *pos; |
| 3191 | struct buffer tmpbuf = { }; |
| 3192 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3193 | uint16_t dglen; |
| 3194 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3195 | unsigned int time_sent; |
| 3196 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3197 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3198 | dglen = read_u16(pos); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3199 | BUG_ON_HOT(!dglen); /* this should not happen */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3200 | |
| 3201 | pos += sizeof dglen; |
| 3202 | first_pkt = read_ptr(pos); |
| 3203 | pos += sizeof first_pkt; |
| 3204 | tmpbuf.area = (char *)pos; |
| 3205 | tmpbuf.size = tmpbuf.data = dglen; |
| 3206 | |
Frédéric Lécaille | 3a56137 | 2022-08-29 18:05:44 +0200 | [diff] [blame] | 3207 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3208 | /* If sendto is on error just skip the call to it for the rest |
| 3209 | * of the loop but continue to purge the buffer. Data will be |
| 3210 | * transmitted when QUIC packets are detected as lost on our |
| 3211 | * side. |
| 3212 | * |
| 3213 | * TODO use fd-monitoring to detect when send operation can be |
| 3214 | * retry. This should improve the bandwidth without relying on |
| 3215 | * retransmission timer. However, it requires a major rework on |
| 3216 | * quic-conn fd management. |
| 3217 | */ |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3218 | if (!skip_sendto) { |
| 3219 | if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) { |
| 3220 | skip_sendto = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3221 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3222 | } |
| 3223 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3224 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3225 | b_del(buf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3226 | qc->tx.bytes += tmpbuf.data; |
| 3227 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3228 | |
| 3229 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3230 | pkt->time_sent = time_sent; |
| 3231 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3232 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3233 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3234 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3235 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3236 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3237 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3238 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3239 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3240 | qc_notify_close(qc); |
| 3241 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3242 | /* RFC 9000 10.2. Immediate Close: |
| 3243 | * The closing and draining connection states exist to ensure |
| 3244 | * that connections close cleanly and that delayed or reordered |
| 3245 | * packets are properly discarded. These states SHOULD persist |
| 3246 | * for at least three times the current PTO interval... |
| 3247 | * |
| 3248 | * Rearm the idle timeout only one time when entering closing |
| 3249 | * state. |
| 3250 | */ |
| 3251 | qc_idle_timer_do_rearm(qc); |
| 3252 | if (qc->timer_task) { |
| 3253 | task_destroy(qc->timer_task); |
| 3254 | qc->timer_task = NULL; |
| 3255 | } |
| 3256 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3257 | qc->path->in_flight += pkt->in_flight_len; |
| 3258 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
Frédéric Lécaille | 5f6c25e | 2022-08-24 16:23:44 +0200 | [diff] [blame] | 3259 | if (pkt->in_flight_len) |
Frédéric Lécaille | da9c441 | 2022-08-22 18:47:51 +0200 | [diff] [blame] | 3260 | qc_set_timer(qc); |
Frédéric Lécaille | 5f6c25e | 2022-08-24 16:23:44 +0200 | [diff] [blame] | 3261 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3262 | next_pkt = pkt->next; |
| 3263 | quic_tx_packet_refinc(pkt); |
| 3264 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3265 | } |
| 3266 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3267 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3268 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3269 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3270 | return 1; |
| 3271 | } |
| 3272 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3273 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3274 | * <salt> salt input. This is the cluster secret which will be derived |
| 3275 | * as HKDF input secret to generate this token. |
| 3276 | * Return 1 if succeeded, 0 if not. |
| 3277 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3278 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3279 | unsigned char *buf, size_t len, |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3280 | const unsigned char *salt, size_t saltlen) |
| 3281 | { |
| 3282 | /* Input secret */ |
| 3283 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3284 | size_t keylen = strlen(global.cluster_secret); |
| 3285 | /* Info */ |
| 3286 | const unsigned char label[] = "stateless token"; |
| 3287 | size_t labellen = sizeof label - 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3288 | int ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3289 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3290 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3291 | |
| 3292 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3293 | key, keylen, salt, saltlen, label, labellen); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3294 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3295 | return ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3299 | * Returns 1 if succeeded, 0 if not. |
| 3300 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3301 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3302 | struct quic_connection_id *quic_cid) |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3303 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3304 | int ret; |
| 3305 | |
| 3306 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3307 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3308 | if (global.cluster_secret) { |
| 3309 | /* Output secret */ |
| 3310 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3311 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3312 | /* Salt */ |
| 3313 | const unsigned char *cid = quic_cid->cid.data; |
| 3314 | size_t cidlen = quic_cid->cid.len; |
| 3315 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3316 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3317 | } |
| 3318 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3319 | /* TODO: RAND_bytes() should be replaced */ |
| 3320 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3321 | sizeof quic_cid->stateless_reset_token) == 1; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3322 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3323 | |
| 3324 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3325 | return ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3328 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3329 | * ebtree. |
| 3330 | * |
| 3331 | * The CID is randomly generated in part with the result altered to be |
| 3332 | * associated with the current thread ID. This means this function must only |
| 3333 | * be called by the quic_conn thread. |
| 3334 | * |
| 3335 | * Returns the new CID if succeeded, NULL if not. |
| 3336 | */ |
| 3337 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3338 | struct quic_conn *qc, |
| 3339 | int seq_num) |
| 3340 | { |
| 3341 | struct quic_connection_id *cid; |
| 3342 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3343 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3344 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3345 | cid = pool_alloc(pool_head_quic_connection_id); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3346 | if (!cid) { |
| 3347 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3348 | goto err; |
| 3349 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3350 | |
| 3351 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3352 | /* TODO: RAND_bytes() should be replaced */ |
| 3353 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3354 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3355 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3356 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3357 | |
| 3358 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3359 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3360 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3361 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3362 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3363 | |
| 3364 | cid->qc = qc; |
| 3365 | |
| 3366 | cid->seq_num.key = seq_num; |
| 3367 | cid->retire_prior_to = 0; |
| 3368 | /* insert the allocated CID in the quic_conn tree */ |
| 3369 | eb64_insert(root, &cid->seq_num); |
| 3370 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3371 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3372 | return cid; |
| 3373 | |
| 3374 | err: |
| 3375 | pool_free(pool_head_quic_connection_id, cid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3376 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3377 | return NULL; |
| 3378 | } |
| 3379 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3380 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3381 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3382 | * a HANDSHAKE_DONE frame. |
| 3383 | * Return 1 if succeeded, 0 if not. |
| 3384 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3385 | 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] | 3386 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3387 | int ret = 0, i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3388 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3389 | struct quic_frame *frm, *frmbak; |
| 3390 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3391 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3392 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3393 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3394 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3395 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3396 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3397 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3398 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3399 | if (!frm) { |
| 3400 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3401 | goto leave; |
| 3402 | } |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3403 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3404 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3405 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3406 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3407 | } |
| 3408 | |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3409 | /* Initialize <max> connection IDs minus one: there is |
| 3410 | * already one connection ID used for the current connection. |
| 3411 | */ |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3412 | first = 1; |
| 3413 | max = qc->tx.params.active_connection_id_limit; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3414 | |
| 3415 | /* TODO: check limit */ |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3416 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3417 | struct quic_connection_id *cid; |
| 3418 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3419 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3420 | if (!frm) { |
| 3421 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3422 | goto err; |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3423 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3424 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3425 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3426 | cid = new_quic_cid(&qc->cids, qc, i); |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3427 | if (!cid) { |
Frédéric Lécaille | bccbad2 | 2022-08-31 15:02:53 +0200 | [diff] [blame] | 3428 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3429 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3430 | goto err; |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3431 | } |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3432 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3433 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3434 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3435 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3436 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3437 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3438 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3439 | |
| 3440 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3441 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3442 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3443 | ret = 1; |
| 3444 | leave: |
| 3445 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3446 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3447 | |
| 3448 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3449 | /* free the frames */ |
| 3450 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3451 | pool_free(pool_head_quic_frame, frm); |
| 3452 | |
Frédéric Lécaille | 96d08d3 | 2022-08-11 12:04:07 +0200 | [diff] [blame] | 3453 | node = eb64_lookup_ge(&qc->cids, first); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3454 | while (node) { |
| 3455 | struct quic_connection_id *cid; |
| 3456 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3457 | 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] | 3458 | if (cid->seq_num.key >= max) |
| 3459 | break; |
| 3460 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3461 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3462 | ebmb_delete(&cid->node); |
| 3463 | eb64_delete(&cid->seq_num); |
| 3464 | pool_free(pool_head_quic_connection_id, cid); |
| 3465 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3466 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3470 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3471 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3472 | struct eb64_node *n; |
| 3473 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3474 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3475 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3476 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3477 | n = eb64_first(&arngs->root); |
| 3478 | while (n) { |
| 3479 | struct eb64_node *next; |
| 3480 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3481 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3482 | next = eb64_next(n); |
| 3483 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3484 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3485 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3486 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3487 | |
| 3488 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3489 | } |
| 3490 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3491 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3492 | * descending order. |
| 3493 | */ |
| 3494 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3495 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3496 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3497 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | |
| 3501 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3502 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3503 | * 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] | 3504 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3505 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3506 | struct quic_arngs *arngs, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3507 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3508 | int ret = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3509 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3510 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3511 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3512 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3513 | last = eb64_last(&arngs->root); |
| 3514 | while (last && arngs->enc_sz > limit) { |
| 3515 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3516 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3517 | prev = eb64_prev(last); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3518 | if (!prev) { |
| 3519 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3520 | goto out; |
| 3521 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3522 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3523 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3524 | 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] | 3525 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3526 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3527 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3528 | --arngs->sz; |
| 3529 | eb64_delete(last); |
| 3530 | pool_free(pool_head_quic_arng, last); |
| 3531 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3534 | ret = 1; |
| 3535 | out: |
| 3536 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3537 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3540 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3541 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3542 | { |
| 3543 | struct eb64_node *node, *next; |
| 3544 | struct quic_arng_node *ar, *ar_next; |
| 3545 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3546 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3547 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3548 | node = eb64_last(&arngs->root); |
| 3549 | if (!node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3550 | goto leave; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3551 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3552 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3553 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3554 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3555 | |
| 3556 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3557 | 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] | 3558 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3559 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3560 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3561 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3562 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3563 | |
| 3564 | leave: |
| 3565 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3566 | } |
| 3567 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3568 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3569 | * 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] | 3570 | */ |
| 3571 | static inline |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3572 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3573 | struct quic_arngs *arngs, |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3574 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3575 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3576 | struct quic_arng_node *new_ar; |
| 3577 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3578 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3579 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3580 | new_ar = pool_alloc(pool_head_quic_arng); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3581 | if (!new_ar) { |
| 3582 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3583 | goto leave; |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3586 | new_ar->first.key = ar->first; |
| 3587 | new_ar->last = ar->last; |
| 3588 | eb64_insert(&arngs->root, &new_ar->first); |
| 3589 | arngs->sz++; |
| 3590 | |
| 3591 | leave: |
| 3592 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3593 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | /* 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] | 3597 | * 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] | 3598 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3599 | * |
| 3600 | * Descending order |
| 3601 | * -------------> |
| 3602 | * range1 range2 |
| 3603 | * ..........|--------|..............|--------| |
| 3604 | * ^ ^ ^ ^ |
| 3605 | * | | | | |
| 3606 | * last1 first1 last2 first2 |
| 3607 | * ..........+--------+--------------+--------+...... |
| 3608 | * diff1 gap12 diff2 |
| 3609 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3610 | * To encode the previous list of ranges we must encode integers as follows in |
| 3611 | * descending order: |
| 3612 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3613 | * with diff1 = last1 - first1 |
| 3614 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3615 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3616 | * |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3617 | |
| 3618 | returns 0 on error |
| 3619 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3620 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3621 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 3622 | struct quic_arngs *arngs, |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3623 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3624 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3625 | int ret = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3626 | struct eb64_node *le; |
| 3627 | struct quic_arng_node *new_node; |
| 3628 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3629 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3630 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3631 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3632 | new = NULL; |
| 3633 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3634 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3635 | if (new_node) |
| 3636 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3637 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3638 | goto leave; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3639 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3641 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3642 | if (!le) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3643 | new_node = quic_insert_new_range(qc, arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3644 | if (!new_node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3645 | goto leave; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3646 | |
| 3647 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3648 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3649 | else { |
| 3650 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3651 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3652 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3653 | /* Already existing range */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3654 | if (le_ar->last >= ar->last) { |
| 3655 | ret = 1; |
| 3656 | } |
| 3657 | else if (le_ar->last + 1 >= ar->first) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3658 | le_ar->last = ar->last; |
| 3659 | new = le; |
| 3660 | new_node = le_ar; |
| 3661 | } |
| 3662 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3663 | new_node = quic_insert_new_range(qc, arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3664 | if (!new_node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3665 | goto leave; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3666 | |
| 3667 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3668 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3669 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3670 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3671 | /* Verify that the new inserted node does not overlap the nodes |
| 3672 | * which follow it. |
| 3673 | */ |
| 3674 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3675 | struct eb64_node *next; |
| 3676 | struct quic_arng_node *next_node; |
| 3677 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3678 | while ((next = eb64_next(new))) { |
| 3679 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3680 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3681 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3682 | break; |
| 3683 | |
| 3684 | if (next_node->last > new_node->last) |
| 3685 | new_node->last = next_node->last; |
| 3686 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3687 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3688 | /* Decrement the size of these ranges. */ |
| 3689 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3690 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3693 | ret = 1; |
| 3694 | leave: |
| 3695 | quic_arngs_set_enc_sz(qc, arngs); |
| 3696 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3697 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3698 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3699 | /* Remove the header protection of packets at <el> encryption level. |
| 3700 | * Always succeeds. |
| 3701 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3702 | 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] | 3703 | { |
| 3704 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 3705 | struct quic_rx_packet *pqpkt, *pkttmp; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3706 | struct quic_enc_level *app_qel; |
| 3707 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3708 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3709 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3710 | /* 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] | 3711 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3712 | TRACE_DEVEL("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3713 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3714 | goto out; |
| 3715 | } |
| 3716 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 3717 | list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3718 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 3719 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3720 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3721 | } |
| 3722 | else { |
| 3723 | /* The AAD includes the packet number field */ |
| 3724 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3725 | /* Store the packet into the tree of packets to decrypt. */ |
| 3726 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3727 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3728 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3729 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3730 | } |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 3731 | LIST_DELETE(&pqpkt->list); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3732 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3736 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3740 | * Return 1 if succeeded, 0 if not. |
| 3741 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3742 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 3743 | struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3744 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3745 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3746 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3747 | struct eb64_node *node; |
| 3748 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3749 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 3750 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3751 | node = eb64_first(&el->rx.crypto.frms); |
| 3752 | while (node) { |
| 3753 | struct quic_rx_crypto_frm *cf; |
| 3754 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3755 | 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] | 3756 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3757 | break; |
| 3758 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3759 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) { |
| 3760 | TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP); |
| 3761 | goto leave; |
| 3762 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3763 | |
| 3764 | node = eb64_next(node); |
| 3765 | quic_rx_packet_refdec(cf->pkt); |
| 3766 | eb64_delete(&cf->offset_node); |
| 3767 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3768 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3769 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3770 | ret = 1; |
| 3771 | leave: |
| 3772 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 3773 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3774 | } |
| 3775 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3776 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3777 | * 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] | 3778 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3779 | * Return 1 if succeeded, 0 if not. |
| 3780 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3781 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3782 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3783 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3784 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3785 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3786 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3787 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3788 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3789 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3790 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3791 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3792 | qel = cur_el; |
| 3793 | next_tel: |
| 3794 | if (!qel) |
| 3795 | goto out; |
| 3796 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3797 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3798 | while (node) { |
| 3799 | struct quic_rx_packet *pkt; |
| 3800 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3801 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3802 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3803 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3804 | if (!qc_pkt_decrypt(pkt, qel, qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3805 | /* Drop the packet */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3806 | TRACE_ERROR("packet decryption failed -> dropped", |
| 3807 | QUIC_EV_CONN_RXPKT, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3808 | } |
| 3809 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3810 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3811 | /* Drop the packet */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3812 | TRACE_ERROR("packet parsing failed -> dropped", |
| 3813 | QUIC_EV_CONN_RXPKT, ctx->qc, pkt); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 3814 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3815 | } |
| 3816 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3817 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3818 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3819 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3820 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3821 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3822 | qc_idle_timer_rearm(qc, 1); |
| 3823 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3824 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3825 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3826 | largest_pn_time_received = pkt->time_received; |
| 3827 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3828 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3829 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 3830 | TRACE_ERROR("Could not update ack range list", |
| 3831 | QUIC_EV_CONN_RXPKT, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3832 | } |
| 3833 | } |
| 3834 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3835 | eb64_delete(&pkt->pn_node); |
| 3836 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3837 | } |
| 3838 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3839 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3840 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3841 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3842 | /* Update the largest acknowledged packet timestamps */ |
| 3843 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3844 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3845 | } |
| 3846 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3847 | if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) { |
| 3848 | // trace already emitted by function above |
| 3849 | goto leave; |
| 3850 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3851 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3852 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3853 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3854 | qel = next_el; |
Frédéric Lécaille | 91a211f | 2022-05-24 10:54:42 +0200 | [diff] [blame] | 3855 | largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3856 | goto next_tel; |
| 3857 | } |
| 3858 | |
| 3859 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3860 | ret = 1; |
| 3861 | leave: |
| 3862 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc); |
| 3863 | return ret; |
| 3864 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3865 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3866 | /* Check if it's possible to remove header protection for packets related to |
| 3867 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3868 | * |
| 3869 | * Return true if the operation is possible else false. |
| 3870 | */ |
| 3871 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3872 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3873 | int ret = 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3874 | enum quic_tls_enc_level tel; |
| 3875 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3876 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 3877 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3878 | if (!qel) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3879 | goto cant_rm_hp; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3880 | |
| 3881 | tel = ssl_to_quic_enc_level(qel->level); |
| 3882 | |
| 3883 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3884 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3885 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3886 | goto cant_rm_hp; |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3887 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3888 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3889 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3890 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 3891 | goto cant_rm_hp; |
| 3892 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3893 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3894 | /* 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] | 3895 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3896 | qc->mux_state == QC_MUX_NULL) { |
| 3897 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 3898 | goto cant_rm_hp; |
| 3899 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3900 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3901 | ret = 1; |
| 3902 | cant_rm_hp: |
| 3903 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 3904 | return ret; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3905 | } |
| 3906 | |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3907 | /* Try to send application frames from list <frms> on connection <qc>. |
| 3908 | * |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3909 | * Use qc_send_app_probing wrapper when probing with old data. |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3910 | * |
| 3911 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 3912 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 3913 | * quic-conn to retry later. |
| 3914 | * |
| 3915 | * Returns 0 on critical error. |
| 3916 | * TODO review and classify more distinctly transient from definitive errors to |
| 3917 | * allow callers to properly handle it. |
| 3918 | */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3919 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3920 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3921 | int status = 0; |
| 3922 | struct buffer *buf; |
| 3923 | |
| 3924 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3925 | |
| 3926 | buf = qc_txb_alloc(qc); |
| 3927 | if (!buf) { |
| 3928 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3929 | goto leave; |
| 3930 | } |
| 3931 | |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3932 | /* Prepare and send packets until we could not further prepare packets. */ |
| 3933 | while (1) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3934 | int ret; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3935 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 3936 | * previous sendto() has failed it is emptied to simulate |
| 3937 | * packet emission and rely on QUIC lost detection to try to |
| 3938 | * emit it. |
| 3939 | */ |
| 3940 | BUG_ON_HOT(b_data(buf)); |
| 3941 | b_reset(buf); |
| 3942 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3943 | ret = qc_prep_app_pkts(qc, buf, frms); |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3944 | if (ret == -1) |
| 3945 | goto err; |
| 3946 | else if (ret == 0) |
| 3947 | goto out; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3948 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3949 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3950 | goto err; |
| 3951 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3952 | |
| 3953 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3954 | status = 1; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3955 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3956 | leave: |
| 3957 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3958 | return status; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3959 | |
| 3960 | err: |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3961 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 3962 | goto leave; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3963 | } |
| 3964 | |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3965 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3966 | * function when probing is required. |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 3967 | * |
| 3968 | * Returns the result from qc_send_app_pkts function. |
| 3969 | */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3970 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 3971 | struct list *frms) |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 3972 | { |
| 3973 | int ret; |
| 3974 | |
| 3975 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3976 | |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3977 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 3978 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 3979 | ret = qc_send_app_pkts(qc, frms); |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3980 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 3981 | |
| 3982 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3983 | return ret; |
| 3984 | } |
| 3985 | |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3986 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 3987 | * function is provided for MUX upper layer usage only. |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3988 | * |
| 3989 | * Returns the result from qc_send_app_pkts function. |
| 3990 | */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3991 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3992 | { |
| 3993 | int ret; |
| 3994 | |
| 3995 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3996 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 3997 | |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 3998 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 3999 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 4000 | ret = qc_send_app_pkts(qc, frms); |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 4001 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 4002 | |
| 4003 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4004 | return ret; |
| 4005 | } |
| 4006 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4007 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4008 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4009 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4010 | * not already acknowledged (in flight). |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4011 | * Returns 1 if succeeded, 0 if not. |
| 4012 | */ |
| 4013 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4014 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4015 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4016 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4017 | int ret, status = 0; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4018 | struct buffer *buf = qc_txb_alloc(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4019 | |
| 4020 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4021 | |
| 4022 | if (!buf) { |
| 4023 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4024 | goto leave; |
| 4025 | } |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4026 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4027 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4028 | * sendto() has failed it is emptied to simulate packet emission and |
| 4029 | * rely on QUIC lost detection to try to emit it. |
| 4030 | */ |
| 4031 | BUG_ON_HOT(b_data(buf)); |
| 4032 | b_reset(buf); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4033 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4034 | if (old_data) { |
| 4035 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4036 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4037 | } |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4038 | |
| 4039 | ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4040 | if (ret == -1) |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4041 | goto out; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4042 | else if (ret == 0) |
| 4043 | goto skip_send; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4044 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4045 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4046 | goto out; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4047 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4048 | skip_send: |
| 4049 | status = 1; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4050 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4051 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4052 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4053 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4054 | leave: |
| 4055 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4056 | return status; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | /* Retransmit up to two datagrams depending on packet number space */ |
| 4060 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 4061 | { |
| 4062 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4063 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4064 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4065 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4066 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4067 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4068 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4069 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4070 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 4071 | |
| 4072 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4073 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4074 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4075 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4076 | iqel->pktns->tx.pto_probe = 1; |
| 4077 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4078 | hqel->pktns->tx.pto_probe = 1; |
| 4079 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4080 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 4081 | } |
| 4082 | } |
| 4083 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4084 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4085 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4086 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4087 | hqel->pktns->tx.pto_probe = 1; |
| 4088 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 4089 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4090 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4091 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4092 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4093 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4094 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4095 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4096 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4097 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4098 | } |
| 4099 | else { |
| 4100 | int i; |
| 4101 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4102 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4103 | |
| 4104 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4105 | hqel->pktns->tx.pto_probe = 0; |
| 4106 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4107 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4108 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4109 | if (!LIST_ISEMPTY(&frms1)) { |
| 4110 | hqel->pktns->tx.pto_probe = 1; |
| 4111 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4112 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4113 | } |
| 4114 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4115 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4116 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4117 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4118 | } |
| 4119 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4120 | aqel->pktns->tx.pto_probe = 0; |
| 4121 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4122 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4123 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4124 | if (!LIST_ISEMPTY(&frms1)) { |
| 4125 | aqel->pktns->tx.pto_probe = 1; |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 4126 | qc_send_app_probing(qc, &frms1); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4127 | } |
| 4128 | if (!LIST_ISEMPTY(&frms2)) { |
| 4129 | aqel->pktns->tx.pto_probe = 1; |
Amaury Denoyelle | cc13047 | 2022-08-17 10:08:16 +0200 | [diff] [blame] | 4130 | qc_send_app_probing(qc, &frms2); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4131 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4132 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4133 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4134 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4135 | } |
| 4136 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4137 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4140 | /* QUIC connection packet handler task (post handshake) */ |
| 4141 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4142 | { |
| 4143 | struct ssl_sock_ctx *ctx; |
| 4144 | struct quic_conn *qc; |
| 4145 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4146 | |
| 4147 | |
| 4148 | ctx = context; |
| 4149 | qc = ctx->qc; |
| 4150 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4151 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4152 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4153 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4154 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4155 | /* Retranmissions */ |
| 4156 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4157 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4158 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4159 | qc_dgrams_retransmit(qc); |
| 4160 | } |
| 4161 | |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4162 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4163 | qc_rm_hp_pkts(qc, qel); |
| 4164 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4165 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) { |
| 4166 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4167 | goto out; |
| 4168 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4169 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4170 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4171 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4172 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4173 | goto out; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4174 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4175 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4176 | /* XXX TODO: how to limit the list frames to send */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 4177 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4178 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4179 | goto out; |
| 4180 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4181 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4182 | out: |
| 4183 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4184 | return t; |
| 4185 | } |
| 4186 | |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4187 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4188 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4189 | { |
| 4190 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4191 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4192 | qel->pktns->tx.pto_probe || |
| 4193 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4194 | } |
| 4195 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4196 | /* QUIC connection packet handler task. */ |
| 4197 | 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] | 4198 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4199 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4200 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4201 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4202 | enum quic_tls_enc_level tel, next_tel; |
| 4203 | struct quic_enc_level *qel, *next_qel; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4204 | struct buffer *buf = NULL; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4205 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4206 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4207 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4208 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4209 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4210 | st = qc->state; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4211 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4212 | |
| 4213 | /* Retranmissions */ |
| 4214 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4215 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4216 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4217 | qc_dgrams_retransmit(qc); |
| 4218 | } |
| 4219 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4220 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 4221 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4222 | TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached", |
| 4223 | QUIC_EV_CONN_IO_CB, qc); |
| 4224 | /* The I/O handler has been woken up by the dgram parser (qc_lstnr_pkt_rcv()) |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4225 | * after the anti-amplification was reached. |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4226 | * |
| 4227 | * TODO: this part should be removed. This was there because the |
| 4228 | * datagram parser was not executed by only one thread. |
Frédéric Lécaille | 59507de | 2022-08-11 12:14:07 +0200 | [diff] [blame] | 4229 | */ |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4230 | qc_set_timer(qc); |
| 4231 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 4232 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 4233 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4234 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 4235 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4236 | (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 4237 | 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] | 4238 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 4239 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4240 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4241 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 4242 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4243 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4244 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4245 | } |
| 4246 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4247 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4248 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4249 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 4250 | 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] | 4251 | |
| 4252 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4253 | /* Treat packets waiting for header packet protection decryption */ |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4254 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4255 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4256 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 4257 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 4258 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4259 | if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4260 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4261 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4262 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4263 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4264 | goto out; |
| 4265 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4266 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4267 | !LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4268 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 4269 | qel = next_qel; |
| 4270 | next_qel = NULL; |
| 4271 | goto next_level; |
| 4272 | } |
| 4273 | else { |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4274 | struct quic_rx_packet *pkt, *pkttmp; |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4275 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 4276 | |
| 4277 | /* Drop these 0-RTT packets */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4278 | TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4279 | list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) { |
| 4280 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4281 | quic_rx_packet_refdec(pkt); |
| 4282 | } |
| 4283 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4286 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4287 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4288 | 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] | 4289 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4290 | goto out; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 4291 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4292 | 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] | 4293 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4294 | /* Discard the Handshake keys. */ |
| 4295 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4296 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4297 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4298 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4299 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4300 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4304 | /* There may be remaining handshake to build (acks) */ |
| 4305 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4306 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 4309 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4310 | * be considered. |
| 4311 | */ |
| 4312 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4313 | goto out; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4314 | |
Amaury Denoyelle | f0f92b2 | 2022-08-09 17:52:52 +0200 | [diff] [blame] | 4315 | if (!qc_need_sending(qc, qel) && |
| 4316 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4317 | goto skip_send; |
Amaury Denoyelle | f0f92b2 | 2022-08-09 17:52:52 +0200 | [diff] [blame] | 4318 | } |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4319 | |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4320 | buf = qc_txb_alloc(qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4321 | if (!buf) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4322 | goto out; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4323 | |
| 4324 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4325 | * sendto() has failed it is emptied to simulate packet emission and |
| 4326 | * rely on QUIC lost detection to try to emit it. |
| 4327 | */ |
| 4328 | BUG_ON_HOT(b_data(buf)); |
| 4329 | b_reset(buf); |
| 4330 | |
| 4331 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 4332 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4333 | if (ret == -1) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4334 | goto out; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4335 | else if (ret == 0) |
| 4336 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4337 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4338 | if (!qc_send_ppkts(buf, ctx)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4339 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4340 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4341 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4342 | /* Check if there is something to do for the next level. |
| 4343 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4344 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4345 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4346 | (!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] | 4347 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4348 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4349 | goto next_level; |
| 4350 | } |
| 4351 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4352 | out: |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4353 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4354 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4355 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4358 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4359 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4360 | { |
| 4361 | int i; |
| 4362 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4363 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4364 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4365 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4366 | if (qel->tx.crypto.bufs[i]) { |
| 4367 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4368 | qel->tx.crypto.bufs[i] = NULL; |
| 4369 | } |
| 4370 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4371 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4372 | |
| 4373 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4377 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4378 | * Returns 1 if succeeded, 0 if not. |
| 4379 | */ |
| 4380 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4381 | enum quic_tls_enc_level level) |
| 4382 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4383 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4384 | struct quic_enc_level *qel; |
| 4385 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4386 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4387 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4388 | qel = &qc->els[level]; |
| 4389 | qel->level = quic_to_ssl_enc_level(level); |
| 4390 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4391 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4392 | 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] | 4393 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4394 | |
| 4395 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 4396 | LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4397 | qel->rx.crypto.offset = 0; |
| 4398 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4399 | |
| 4400 | /* Allocate only one buffer. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4401 | /* TODO: use a pool */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4402 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4403 | if (!qel->tx.crypto.bufs) |
| 4404 | goto err; |
| 4405 | |
| 4406 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4407 | if (!qel->tx.crypto.bufs[0]) |
| 4408 | goto err; |
| 4409 | |
| 4410 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4411 | qel->tx.crypto.nb_buf = 1; |
| 4412 | |
| 4413 | qel->tx.crypto.sz = 0; |
| 4414 | qel->tx.crypto.offset = 0; |
| 4415 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4416 | ret = 1; |
| 4417 | leave: |
| 4418 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4419 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4420 | |
| 4421 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4422 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4423 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4426 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4427 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4428 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4429 | * This function must only be called by the thread responsible of the quic_conn |
| 4430 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4431 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4432 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4433 | { |
| 4434 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4435 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4436 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4437 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4438 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4439 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4440 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4441 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4442 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4443 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4444 | |
Willy Tarreau | 54bc786 | 2022-08-10 07:26:27 +0200 | [diff] [blame] | 4445 | /* in the unlikely (but possible) case the connection was just added to |
| 4446 | * the accept_list we must delete it from there. |
| 4447 | */ |
| 4448 | MT_LIST_DELETE(&qc->accept_list); |
| 4449 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4450 | /* free remaining stream descriptors */ |
| 4451 | node = eb64_first(&qc->streams_by_id); |
| 4452 | while (node) { |
| 4453 | struct qc_stream_desc *stream; |
| 4454 | |
| 4455 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4456 | node = eb64_next(node); |
| 4457 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4458 | /* all streams attached to the quic-conn are released, so |
| 4459 | * qc_stream_desc_free will liberate the stream instance. |
| 4460 | */ |
| 4461 | BUG_ON(!stream->release); |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 4462 | qc_stream_desc_free(stream, 1); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4463 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4464 | |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4465 | /* Purge Rx packet list. */ |
| 4466 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 4467 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 4468 | pool_free(pool_head_quic_rx_packet, pkt); |
| 4469 | } |
| 4470 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4471 | if (qc->idle_timer_task) { |
| 4472 | task_destroy(qc->idle_timer_task); |
| 4473 | qc->idle_timer_task = NULL; |
| 4474 | } |
| 4475 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4476 | if (qc->timer_task) { |
| 4477 | task_destroy(qc->timer_task); |
| 4478 | qc->timer_task = NULL; |
| 4479 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4480 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4481 | /* remove the connection from receiver cids trees */ |
| 4482 | ebmb_delete(&qc->odcid_node); |
| 4483 | ebmb_delete(&qc->scid_node); |
| 4484 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4485 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4486 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4487 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4488 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4489 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4490 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4491 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4492 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4493 | quic_tls_ku_free(qc); |
| 4494 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4495 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4496 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4497 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4498 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4499 | |
| 4500 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4501 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4502 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4503 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4504 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
Frédéric Lécaille | 8ddde4f | 2022-08-01 14:07:50 +0200 | [diff] [blame] | 4505 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4506 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4507 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4508 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4509 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4510 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4511 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4512 | |
| 4513 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4514 | } |
| 4515 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4516 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4517 | { |
| 4518 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4519 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4520 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4521 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4522 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4523 | /* Next application data can be dropped. */ |
| 4524 | qc->mux_state = QC_MUX_RELEASED; |
| 4525 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4526 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4527 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4528 | quic_conn_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4529 | goto leave; |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4530 | } |
| 4531 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 4532 | qc_check_close_on_released_mux(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4533 | leave: |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4534 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4535 | } |
| 4536 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4537 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4538 | 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] | 4539 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4540 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4541 | struct quic_conn *qc; |
| 4542 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4543 | |
| 4544 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4545 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4546 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4547 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4548 | task->expire = TICK_ETERNITY; |
| 4549 | pktns = quic_loss_pktns(qc); |
| 4550 | if (tick_isset(pktns->tx.loss_time)) { |
| 4551 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4552 | |
| 4553 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
Frédéric Lécaille | 15773f2 | 2022-08-24 17:57:09 +0200 | [diff] [blame] | 4554 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 4555 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | 277c462 | 2022-08-24 17:06:04 +0200 | [diff] [blame] | 4556 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4557 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4558 | goto out; |
| 4559 | } |
| 4560 | |
| 4561 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4562 | 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] | 4563 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4564 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4565 | struct qcc *qcc = qc->qcc; |
| 4566 | |
| 4567 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4568 | tasklet_wakeup(qcc->subs->tasklet); |
| 4569 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4570 | if (!qcc->subs->events) |
| 4571 | qcc->subs = NULL; |
| 4572 | } |
| 4573 | else { |
| 4574 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4575 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4576 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4577 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4578 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
Frédéric Lécaille | dafbde6 | 2022-04-26 13:54:28 +0200 | [diff] [blame] | 4579 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4580 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4581 | } |
| 4582 | } |
| 4583 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4584 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4585 | } |
| 4586 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 4587 | TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | dafbde6 | 2022-04-26 13:54:28 +0200 | [diff] [blame] | 4588 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4589 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4590 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4591 | 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] | 4592 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4593 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4594 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4595 | 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] | 4596 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4597 | 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] | 4598 | iel->pktns->tx.pto_probe = 1; |
| 4599 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4600 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4601 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4602 | qc->path->loss.pto_count++; |
| 4603 | |
| 4604 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4605 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4606 | |
| 4607 | return task; |
| 4608 | } |
| 4609 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4610 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4611 | * one byte past the end of this buffer. This will extract the ODCID |
| 4612 | * which will be stored into <odcid> |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4613 | * |
| 4614 | * Returns 0 on success else non-zero. |
| 4615 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4616 | static int parse_retry_token(struct quic_conn *qc, |
| 4617 | const unsigned char *token, const unsigned char *end, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4618 | struct quic_cid *odcid) |
| 4619 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4620 | int ret = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4621 | uint64_t odcid_len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4622 | uint32_t timestamp; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4623 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4624 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4625 | |
| 4626 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 4627 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 4628 | goto leave; |
| 4629 | } |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4630 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4631 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4632 | * When an Initial packet is sent by a client that has not previously |
| 4633 | * received an Initial or Retry packet from the server, the client |
| 4634 | * populates the Destination Connection ID field with an unpredictable |
| 4635 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4636 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4637 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 4638 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4639 | goto leave; |
| 4640 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4641 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4642 | if (end - token < odcid_len + sizeof timestamp) { |
| 4643 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4644 | goto leave; |
| 4645 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4646 | |
| 4647 | timestamp = ntohl(read_u32(token + odcid_len)); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4648 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 4649 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 4650 | goto leave; |
| 4651 | } |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4652 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4653 | ret = 1; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4654 | memcpy(odcid->data, token, odcid_len); |
| 4655 | odcid->len = odcid_len; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4656 | leave: |
| 4657 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4658 | return !ret; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4659 | } |
| 4660 | |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4661 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4662 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4663 | * for QUIC servers (or haproxy listeners). |
| 4664 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4665 | * <token> the token found to be used for this connection with <token_len> as |
| 4666 | * length. <saddr> is the source address. |
| 4667 | * Returns the connection if succeeded, NULL if not. |
| 4668 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4669 | 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] | 4670 | struct quic_cid *dcid, struct quic_cid *scid, |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 4671 | const struct quic_cid *token_odcid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4672 | struct sockaddr_storage *saddr, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4673 | int server, int token, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4674 | { |
| 4675 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4676 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4677 | /* Initial CID. */ |
| 4678 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4679 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4680 | struct listener *l = NULL; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4681 | struct quic_cc_algo *cc_algo = NULL; |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 4682 | struct quic_tls_ctx *ictx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4683 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4684 | qc = pool_zalloc(pool_head_quic_conn); |
| 4685 | if (!qc) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4686 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4687 | goto err; |
| 4688 | } |
| 4689 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4690 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4691 | if (!buf_area) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4692 | TRACE_ERROR("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] | 4693 | goto err; |
| 4694 | } |
| 4695 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4696 | qc->cids = EB_ROOT; |
| 4697 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4698 | if (server) { |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4699 | struct proxy *prx; |
| 4700 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4701 | l = owner; |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4702 | prx = l->bind_conf->frontend; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4703 | cc_algo = l->bind_conf->quic_cc_algo; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4704 | |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4705 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4706 | &quic_stats_module); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4707 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4708 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4709 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4710 | qc->odcid.len = dcid->len; |
| 4711 | qc->odcid.addrlen = dcid->addrlen; |
| 4712 | 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] | 4713 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4714 | /* 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] | 4715 | if (scid->len) |
| 4716 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4717 | qc->dcid.len = scid->len; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4718 | qc->tx.buf = BUF_NULL; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4719 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4720 | } |
| 4721 | /* QUIC Client (outgoing connection to servers) */ |
| 4722 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4723 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4724 | if (dcid->len) |
| 4725 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4726 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4727 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4728 | qc->mux_state = QC_MUX_NULL; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 4729 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4730 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4731 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4732 | if (!icid) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4733 | TRACE_ERROR("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] | 4734 | goto err; |
| 4735 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4736 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4737 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4738 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4739 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4740 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4741 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4742 | qc->scid = icid->cid; |
| 4743 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4744 | /* Packet number spaces initialization. */ |
| 4745 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4746 | quic_pktns_init(&qc->pktns[i]); |
| 4747 | /* QUIC encryption level context initialization. */ |
| 4748 | 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] | 4749 | if (!quic_conn_enc_level_init(qc, i)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4750 | TRACE_ERROR("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] | 4751 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4753 | /* Initialize the packet number space. */ |
| 4754 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4755 | } |
| 4756 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4757 | qc->original_version = qv; |
| 4758 | 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] | 4759 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4760 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4761 | /* TX part. */ |
| 4762 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4763 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4764 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4765 | qc->tx.bytes = 0; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4766 | qc->tx.buf = BUF_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4767 | /* RX part. */ |
| 4768 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4769 | 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] | 4770 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4771 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4772 | |
| 4773 | qc->nb_pkt_for_cc = 1; |
| 4774 | qc->nb_pkt_since_cc = 0; |
| 4775 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4776 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4777 | if (!quic_tls_ku_init(qc)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4778 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4779 | goto err; |
| 4780 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4781 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4782 | /* XXX TO DO: Only one path at this time. */ |
| 4783 | qc->path = &qc->paths[0]; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4784 | 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] | 4785 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4786 | /* required to use MTLIST_IN_LIST */ |
| 4787 | MT_LIST_INIT(&qc->accept_list); |
| 4788 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4789 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4790 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4791 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4792 | |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4793 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 4794 | icid->stateless_reset_token, |
| 4795 | dcid->data, dcid->len, |
Frédéric Lécaille | 7629f5d | 2022-08-11 18:54:26 +0200 | [diff] [blame] | 4796 | qc->scid.data, qc->scid.len, token_odcid)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4797 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4798 | |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4799 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4800 | !quic_conn_init_timer(qc) || |
| 4801 | !quic_conn_init_idle_timer_task(qc)) |
| 4802 | goto err; |
| 4803 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 4804 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 4805 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4806 | goto err; |
| 4807 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4808 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4809 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4810 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4811 | |
| 4812 | err: |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4813 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4814 | if (qc) |
| 4815 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4816 | quic_conn_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4817 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4818 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | /* Initialize the timer task of <qc> QUIC connection. |
| 4822 | * Returns 1 if succeeded, 0 if not. |
| 4823 | */ |
| 4824 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4825 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4826 | int ret = 0; |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4827 | /* Attach this task to the same thread ID used for the connection */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4828 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 4829 | |
Willy Tarreau | 8716875 | 2022-06-13 16:31:53 +0200 | [diff] [blame] | 4830 | qc->timer_task = task_new_on(qc->tid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4831 | if (!qc->timer_task) { |
| 4832 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 4833 | goto leave; |
| 4834 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4835 | |
| 4836 | qc->timer = TICK_ETERNITY; |
| 4837 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4838 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4839 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4840 | ret = 1; |
| 4841 | leave: |
| 4842 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 4843 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4844 | } |
| 4845 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4846 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4847 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4848 | { |
| 4849 | unsigned int expire; |
| 4850 | |
| 4851 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4852 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4853 | } |
| 4854 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4855 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4856 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4857 | */ |
| 4858 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4859 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4860 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 4861 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4862 | if (read) { |
| 4863 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4864 | } |
| 4865 | else { |
| 4866 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4867 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4868 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4869 | |
| 4870 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /* The task handling the idle timeout */ |
| 4874 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4875 | { |
| 4876 | struct quic_conn *qc = ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4877 | struct quic_counters *prx_counters = qc->prx_counters; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4878 | unsigned int qc_flags = qc->flags; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4879 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4880 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 4881 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4882 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4883 | * might free the quic-conn too early via quic_close(). |
| 4884 | */ |
| 4885 | qc_notify_close(qc); |
| 4886 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4887 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4888 | * responsible to call quic_close to release it. |
| 4889 | */ |
| 4890 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4891 | if (qc->mux_state != QC_MUX_READY) |
| 4892 | quic_conn_release(qc); |
| 4893 | |
| 4894 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4895 | * least clean some parts of it such as the tasklet. |
| 4896 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4897 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4898 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 4899 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4900 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 4901 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4902 | } |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4903 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4904 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4905 | return NULL; |
| 4906 | } |
| 4907 | |
| 4908 | /* Initialize the idle timeout task for <qc>. |
| 4909 | * Returns 1 if succeeded, 0 if not. |
| 4910 | */ |
| 4911 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4912 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4913 | int ret = 0; |
| 4914 | |
| 4915 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 4916 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4917 | qc->idle_timer_task = task_new_here(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4918 | if (!qc->idle_timer_task) { |
| 4919 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 4920 | goto leave; |
| 4921 | } |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4922 | |
| 4923 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4924 | qc->idle_timer_task->context = qc; |
| 4925 | qc_idle_timer_rearm(qc, 1); |
| 4926 | task_queue(qc->idle_timer_task); |
| 4927 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4928 | ret = 1; |
| 4929 | leave: |
| 4930 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 4931 | return ret; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4932 | } |
| 4933 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4934 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4935 | * past one byte of this buffer. |
| 4936 | */ |
| 4937 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4938 | struct quic_rx_packet *pkt) |
| 4939 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4940 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4941 | unsigned char dcid_len, scid_len; |
| 4942 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4943 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 4944 | |
| 4945 | if (end == *buf) { |
| 4946 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 4947 | goto leave; |
| 4948 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4949 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4950 | /* Destination Connection ID Length */ |
| 4951 | dcid_len = *(*buf)++; |
| 4952 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4953 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 4954 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 4955 | goto leave; |
| 4956 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4957 | |
| 4958 | if (dcid_len) { |
| 4959 | /* Check that the length of this received DCID matches the CID lengths |
| 4960 | * of our implementation for non Initials packets only. |
| 4961 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4962 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4963 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4964 | dcid_len != QUIC_HAP_CID_LEN) { |
| 4965 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 4966 | goto leave; |
| 4967 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4968 | |
| 4969 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4970 | } |
| 4971 | |
| 4972 | pkt->dcid.len = dcid_len; |
| 4973 | *buf += dcid_len; |
| 4974 | |
| 4975 | /* Source Connection ID Length */ |
| 4976 | scid_len = *(*buf)++; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4977 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 4978 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 4979 | goto leave; |
| 4980 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4981 | |
| 4982 | if (scid_len) |
| 4983 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4984 | pkt->scid.len = scid_len; |
| 4985 | *buf += scid_len; |
| 4986 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4987 | ret = 1; |
| 4988 | leave: |
| 4989 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 4990 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4991 | } |
| 4992 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4993 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4994 | static void qc_pkt_insert(struct quic_conn *qc, |
| 4995 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4996 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 4997 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4998 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4999 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 5000 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5001 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5002 | |
| 5003 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | /* 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] | 5007 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 5008 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 5009 | * the packet first byte. |
| 5010 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 5011 | * Returns 1 if succeeded, 0 if not. |
| 5012 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5013 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5014 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5015 | unsigned char *buf, unsigned char *beg, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5016 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5017 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5018 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5019 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 5020 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5021 | struct quic_enc_level *qel; |
| 5022 | /* Only for traces. */ |
| 5023 | struct quic_rx_packet *qpkt_trace; |
| 5024 | |
| 5025 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5026 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5027 | /* The packet number is here. This is also the start minus |
| 5028 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5029 | * protection. |
| 5030 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5031 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 5032 | |
| 5033 | tel = quic_packet_type_enc_level(pkt->type); |
| 5034 | qel = &qc->els[tel]; |
| 5035 | |
| 5036 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5037 | /* Note that the following function enables us to unprotect the packet |
| 5038 | * number and its length subsequently used to decrypt the entire |
| 5039 | * packets. |
| 5040 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5041 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 5042 | qel->pktns->rx.largest_pn, pn, beg)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5043 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5044 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /* The AAD includes the packet number field found at <pn>. */ |
| 5048 | pkt->aad_len = pn - beg + pkt->pnl; |
Frédéric Lécaille | ffde316 | 2022-08-08 18:41:16 +0200 | [diff] [blame] | 5049 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5050 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5051 | goto out; |
Frédéric Lécaille | ffde316 | 2022-08-08 18:41:16 +0200 | [diff] [blame] | 5052 | } |
| 5053 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5054 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5055 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 5056 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 5057 | 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] | 5058 | /* If the packet number space has been discarded, this packet |
| 5059 | * will be not parsed. |
| 5060 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5061 | 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] | 5062 | goto out; |
| 5063 | } |
| 5064 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5065 | 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] | 5066 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | a2d8ad2 | 2022-08-23 17:45:52 +0200 | [diff] [blame] | 5067 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 5068 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5069 | } |
| 5070 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5071 | *el = qel; |
| 5072 | /* No reference counter incrementation here!!! */ |
| 5073 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5074 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5075 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5076 | b_add(&qc->rx.buf, pkt->len); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5077 | |
| 5078 | ret = 1; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5079 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5080 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5081 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5084 | /* 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] | 5085 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5086 | * of this packet into <*version>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5087 | */ |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5088 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5089 | unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5090 | int *long_header, uint32_t *version) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5091 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5092 | int ret = 0; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5093 | const unsigned char byte0 = **buf; |
| 5094 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5095 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5096 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5097 | (*buf)++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5098 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5099 | unsigned char type = |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5100 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5101 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5102 | *long_header = 1; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5103 | /* Version */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5104 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5105 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5106 | goto out; |
| 5107 | } |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5108 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5109 | if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5110 | pkt->type = type; |
| 5111 | } |
| 5112 | else { |
| 5113 | switch (type) { |
| 5114 | case 0: |
| 5115 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5116 | break; |
| 5117 | case 1: |
| 5118 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5119 | break; |
| 5120 | case 2: |
| 5121 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5122 | break; |
| 5123 | case 3: |
| 5124 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5125 | break; |
| 5126 | } |
| 5127 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5128 | } |
| 5129 | else { |
| 5130 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5131 | *long_header = 0; |
| 5132 | } |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5133 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5134 | ret = 1; |
| 5135 | out: |
| 5136 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
Frédéric Lécaille | 59507de | 2022-08-11 12:14:07 +0200 | [diff] [blame] | 5137 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5138 | } |
| 5139 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5140 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5141 | * if supported or NULL if not. |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5142 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5143 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5144 | { |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5145 | int i; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5146 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5147 | for (i = 0; i < quic_versions_nb; i++) |
| 5148 | if (quic_versions[i].num == version) |
| 5149 | return &quic_versions[i]; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5150 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5151 | return NULL; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5152 | } |
| 5153 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5154 | /* |
| 5155 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5156 | * address <addr>. |
| 5157 | * Implementation of RFC9000 6. Version Negotiation |
| 5158 | * |
| 5159 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5160 | * |
| 5161 | * Returns 0 on success else non-zero |
| 5162 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5163 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5164 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5165 | { |
| 5166 | char buf[256]; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5167 | int ret = 0, i = 0, j; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5168 | uint32_t version; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5169 | const socklen_t addrlen = get_addr_len(addr); |
| 5170 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5171 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5172 | /* |
| 5173 | * header form |
| 5174 | * long header, fixed bit to 0 for Version Negotiation |
| 5175 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5176 | /* TODO: RAND_bytes() should be replaced? */ |
| 5177 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5178 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5179 | goto out; |
| 5180 | } |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5181 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 5182 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5183 | /* null version for Version Negotiation */ |
| 5184 | buf[i++] = '\x00'; |
| 5185 | buf[i++] = '\x00'; |
| 5186 | buf[i++] = '\x00'; |
| 5187 | buf[i++] = '\x00'; |
| 5188 | |
| 5189 | /* source connection id */ |
| 5190 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 5191 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5192 | i += pkt->scid.len; |
| 5193 | |
| 5194 | /* destination connection id */ |
| 5195 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 5196 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5197 | i += pkt->dcid.len; |
| 5198 | |
| 5199 | /* supported version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5200 | for (j = 0; j < quic_versions_nb; j++) { |
| 5201 | version = htonl(quic_versions[j].num); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5202 | memcpy(&buf[i], &version, sizeof(version)); |
| 5203 | i += sizeof(version); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5204 | } |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5205 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5206 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5207 | goto out; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5208 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5209 | ret = 1; |
| 5210 | out: |
| 5211 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5212 | return !ret; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5213 | } |
| 5214 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5215 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5216 | * from <fd> UDP socket to <dst> |
| 5217 | * Return 1 if succeeded, 0 if not. |
| 5218 | */ |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5219 | 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] | 5220 | struct quic_rx_packet *rxpkt) |
| 5221 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5222 | int ret = 0, pktlen, rndlen; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5223 | unsigned char pkt[64]; |
| 5224 | const socklen_t addrlen = get_addr_len(dstaddr); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5225 | struct proxy *prx; |
| 5226 | struct quic_counters *prx_counters; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5227 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5228 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5229 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5230 | prx = l->bind_conf->frontend; |
| 5231 | 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] | 5232 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5233 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5234 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5235 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5236 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5237 | * connection ID length that it requests the peer to include in its packets, |
| 5238 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5239 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5240 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5241 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5242 | * than the packet it responds to. |
| 5243 | */ |
| 5244 | |
| 5245 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5246 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5247 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5248 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5249 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5250 | /* Put a header of random bytes */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5251 | /* TODO: RAND_bytes() should be replaced */ |
| 5252 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5253 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5254 | goto leave; |
| 5255 | } |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5256 | |
| 5257 | /* Clear the most significant bit, and set the second one */ |
| 5258 | *pkt = (*pkt & ~0x80) | 0x40; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5259 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5260 | rxpkt->dcid.data, rxpkt->dcid.len)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5261 | goto leave; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5262 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5263 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5264 | goto leave; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5265 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5266 | ret = 1; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5267 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5268 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5269 | leave: |
| 5270 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5271 | return ret; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5272 | } |
| 5273 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5274 | /* QUIC server only function. |
| 5275 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 5276 | * This is the responsibility of the caller to check <aad> size is big enough |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5277 | * to contain these data. |
| 5278 | * Return the number of bytes copied to <aad>. |
| 5279 | */ |
| 5280 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5281 | uint32_t version, |
| 5282 | const struct quic_cid *cid, |
| 5283 | const struct sockaddr_storage *addr) |
| 5284 | { |
| 5285 | unsigned char *p; |
| 5286 | |
| 5287 | p = aad; |
| 5288 | memcpy(p, &version, sizeof version); |
| 5289 | p += sizeof version; |
| 5290 | p += quic_saddr_cpy(p, addr); |
| 5291 | memcpy(p, cid->data, cid->len); |
| 5292 | p += cid->len; |
| 5293 | |
| 5294 | return p - aad; |
| 5295 | } |
| 5296 | |
| 5297 | /* QUIC server only function. |
| 5298 | * Generate the token to be used in Retry packets. The token is written to |
| 5299 | * <buf> whith <len> as length. <odcid> is the original destination connection |
| 5300 | * ID and <dcid> is our side destination connection ID (or client source |
| 5301 | * connection ID). |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5302 | * Returns the length of the encoded token or 0 on error. |
| 5303 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5304 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5305 | const uint32_t version, |
| 5306 | const struct quic_cid *odcid, |
| 5307 | const struct quic_cid *dcid, |
| 5308 | struct sockaddr_storage *addr) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5309 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5310 | int ret = 0; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5311 | unsigned char *p; |
| 5312 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 5313 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 5314 | size_t aadlen; |
| 5315 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5316 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5317 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5318 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5319 | size_t seclen = strlen(global.cluster_secret); |
| 5320 | EVP_CIPHER_CTX *ctx = NULL; |
| 5321 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5322 | uint32_t timestamp = now_ms; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5323 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5324 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5325 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5326 | /* We copy the odcid into the token, prefixed by its one byte |
| 5327 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5328 | * the random bytes used to derive the secret to encrypt the token. |
| 5329 | */ |
| 5330 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5331 | goto err; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5332 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5333 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5334 | /* TODO: RAND_bytes() should be replaced */ |
| 5335 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5336 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5337 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5338 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5339 | |
| 5340 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5341 | salt, sizeof salt, sec, seclen)) { |
| 5342 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5343 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5344 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5345 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5346 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5347 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5348 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5349 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5350 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5351 | /* Token build */ |
| 5352 | p = buf; |
| 5353 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5354 | *p++ = odcid->len; |
| 5355 | memcpy(p, odcid->data, odcid->len); |
| 5356 | p += odcid->len; |
| 5357 | write_u32(p, htonl(timestamp)); |
| 5358 | p += sizeof timestamp; |
| 5359 | |
| 5360 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5361 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5362 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5363 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5364 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5365 | |
| 5366 | p += QUIC_TLS_TAG_LEN; |
| 5367 | memcpy(p, salt, sizeof salt); |
| 5368 | p += sizeof salt; |
| 5369 | EVP_CIPHER_CTX_free(ctx); |
| 5370 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5371 | ret = p - buf; |
| 5372 | leave: |
| 5373 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5374 | return ret; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5375 | |
| 5376 | err: |
| 5377 | if (ctx) |
| 5378 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5379 | goto leave; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5380 | } |
| 5381 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5382 | /* QUIC server only function. |
| 5383 | * Check the validity of the Retry token from <token> buffer with <tokenlen> |
| 5384 | * as length. If valid, the ODCID of <qc> QUIC connection will be put |
| 5385 | * into <odcid> connection ID. <dcid> is our side destination connection ID |
| 5386 | * of client source connection ID. |
| 5387 | * Return 1 if succeeded, 0 if not. |
| 5388 | */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5389 | static int quic_retry_token_check(const unsigned char *token, size_t tokenlen, |
| 5390 | const struct quic_version *qv, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5391 | struct quic_cid *odcid, |
| 5392 | const struct quic_cid *dcid, |
| 5393 | struct quic_conn *qc, |
| 5394 | struct sockaddr_storage *addr) |
| 5395 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5396 | int ret = 0; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5397 | unsigned char buf[128]; |
| 5398 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 5399 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 5400 | size_t aadlen; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5401 | const unsigned char *salt; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5402 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5403 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5404 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5405 | size_t seclen = strlen(global.cluster_secret); |
| 5406 | EVP_CIPHER_CTX *ctx = NULL; |
| 5407 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5408 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5409 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5410 | |
| 5411 | if (sizeof buf < tokenlen) { |
| 5412 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 5413 | goto err; |
| 5414 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5415 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5416 | 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] | 5417 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 5418 | 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] | 5419 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5420 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 5421 | goto err; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 5422 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5423 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5424 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 5425 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5426 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5427 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5428 | |
| 5429 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5430 | 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] | 5431 | ctx, aead, key, iv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5432 | TRACE_ERROR("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] | 5433 | goto err; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 5434 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5435 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5436 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 5437 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5438 | goto err; |
| 5439 | } |
| 5440 | |
| 5441 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5442 | |
| 5443 | ret = 1; |
| 5444 | leave: |
| 5445 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5446 | return ret; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5447 | |
| 5448 | err: |
| 5449 | if (ctx) |
| 5450 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5451 | goto leave; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5452 | } |
| 5453 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5454 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5455 | * the Initial <pkt> packet. |
| 5456 | * |
| 5457 | * Returns 0 on success else non-zero. |
| 5458 | */ |
| 5459 | 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] | 5460 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5461 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5462 | int ret = 0; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5463 | unsigned char buf[128]; |
| 5464 | int i = 0, token_len; |
| 5465 | const socklen_t addrlen = get_addr_len(addr); |
| 5466 | struct quic_cid scid; |
| 5467 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5468 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5469 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5470 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 5471 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 5472 | (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] | 5473 | /* version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5474 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 5475 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 5476 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 5477 | buf[i++] = *(unsigned char *)&qv->num; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5478 | |
| 5479 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5480 | buf[i++] = pkt->scid.len; |
| 5481 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5482 | i += pkt->scid.len; |
| 5483 | |
| 5484 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5485 | scid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5486 | /* TODO: RAND_bytes() should be replaced */ |
| 5487 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 5488 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 5489 | goto out; |
| 5490 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5491 | |
| 5492 | buf[i++] = scid.len; |
| 5493 | memcpy(&buf[i], scid.data, scid.len); |
| 5494 | i += scid.len; |
| 5495 | |
| 5496 | /* token */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5497 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5498 | &pkt->dcid, &pkt->scid, addr))) { |
| 5499 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 5500 | goto out; |
| 5501 | } |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 5502 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5503 | i += token_len; |
| 5504 | |
| 5505 | /* token integrity tag */ |
| 5506 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5507 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5508 | pkt->dcid.len, buf, i, qv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5509 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5510 | goto out; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | i += QUIC_TLS_TAG_LEN; |
| 5514 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5515 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 5516 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5517 | goto out; |
| 5518 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5519 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5520 | ret = 1; |
| 5521 | out: |
| 5522 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5523 | return !ret; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5524 | } |
| 5525 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5526 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5527 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5528 | * concatenated to the <pkt> DCID field. |
| 5529 | * |
| 5530 | * Returns the instance or NULL if not found. |
| 5531 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5532 | 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] | 5533 | struct listener *l, |
| 5534 | struct sockaddr_storage *saddr) |
| 5535 | { |
| 5536 | struct quic_conn *qc = NULL; |
| 5537 | struct ebmb_node *node; |
| 5538 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5539 | /* set if the quic_conn is found in the second DCID tree */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5540 | |
| 5541 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5542 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5543 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5544 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5545 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5546 | /* DCIDs of first packets coming from multiple clients may have |
| 5547 | * the same values. Let's distinguish them by concatenating the |
| 5548 | * socket addresses. |
| 5549 | */ |
| 5550 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5551 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5552 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5553 | if (node) { |
| 5554 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5555 | goto end; |
| 5556 | } |
| 5557 | } |
| 5558 | |
| 5559 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5560 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5561 | * used. |
| 5562 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5563 | 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] | 5564 | if (!node) |
| 5565 | goto end; |
| 5566 | |
| 5567 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5568 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5569 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5570 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5571 | * If already done, this is a noop. |
| 5572 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5573 | if (qc) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5574 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5575 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5576 | end: |
| 5577 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5578 | return qc; |
| 5579 | } |
| 5580 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5581 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5582 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5583 | * parameters of this session. |
| 5584 | * This is the responsibility of the caller to check the validity of all the |
| 5585 | * pointers passed as parameter to this function. |
| 5586 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5587 | * CO_ER_SSL_NO_MEM. |
| 5588 | */ |
| 5589 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5590 | unsigned char *params, size_t params_len) |
| 5591 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5592 | int retry, ret = -1; |
| 5593 | |
| 5594 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5595 | |
| 5596 | retry = 1; |
| 5597 | retry: |
| 5598 | *ssl = SSL_new(ssl_ctx); |
| 5599 | if (!*ssl) { |
| 5600 | if (!retry--) |
| 5601 | goto err; |
| 5602 | |
| 5603 | pool_gc(NULL); |
| 5604 | goto retry; |
| 5605 | } |
| 5606 | |
| 5607 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5608 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5609 | SSL_free(*ssl); |
| 5610 | *ssl = NULL; |
| 5611 | if (!retry--) |
| 5612 | goto err; |
| 5613 | |
| 5614 | pool_gc(NULL); |
| 5615 | goto retry; |
| 5616 | } |
| 5617 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5618 | ret = 0; |
| 5619 | leave: |
| 5620 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5621 | return ret; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5622 | |
| 5623 | err: |
| 5624 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5625 | goto leave; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5628 | /* Finalize <qc> QUIC connection: |
| 5629 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 5630 | * - derive the secrets for this context, |
| 5631 | * - encode the transport parameters to be sent, |
| 5632 | * - set them into the TLS stack, |
| 5633 | * - initialize ->max_ack_delay and max_idle_timeout, |
| 5634 | * |
| 5635 | * MUST be called after having received the remote transport parameters. |
| 5636 | * Return 1 if succeeded, 0 if not. |
| 5637 | */ |
| 5638 | int qc_conn_finalize(struct quic_conn *qc, int server) |
| 5639 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5640 | int ret = 0; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5641 | struct quic_transport_params *tx_tp = &qc->tx.params; |
| 5642 | struct quic_transport_params *rx_tp = &qc->rx.params; |
| 5643 | const struct quic_version *ver; |
| 5644 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5645 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5646 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5647 | if (tx_tp->version_information.negotiated_version && |
| 5648 | tx_tp->version_information.negotiated_version != qc->original_version) { |
| 5649 | qc->negotiated_version = |
| 5650 | qc->tx.params.version_information.negotiated_version; |
| 5651 | if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 5652 | qc->odcid.data, qc->odcid.len, !server)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5653 | goto out; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5654 | |
| 5655 | ver = qc->negotiated_version; |
| 5656 | } |
| 5657 | else { |
| 5658 | ver = qc->original_version; |
| 5659 | } |
| 5660 | |
| 5661 | qc->enc_params_len = |
| 5662 | quic_transport_params_encode(qc->enc_params, |
| 5663 | qc->enc_params + sizeof qc->enc_params, |
| 5664 | &qc->rx.params, ver, 1); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5665 | if (!qc->enc_params_len) { |
| 5666 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT); |
| 5667 | goto out; |
| 5668 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5669 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5670 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 5671 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT); |
| 5672 | goto out; |
| 5673 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5674 | |
| 5675 | if (tx_tp->max_ack_delay) |
| 5676 | qc->max_ack_delay = tx_tp->max_ack_delay; |
| 5677 | |
| 5678 | if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout) |
| 5679 | qc->max_idle_timeout = |
| 5680 | QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5681 | else |
| 5682 | qc->max_idle_timeout = |
| 5683 | QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5684 | |
| 5685 | TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp); |
| 5686 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5687 | ret = 1; |
| 5688 | out: |
| 5689 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5690 | return ret; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5691 | } |
| 5692 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5693 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5694 | * used to process <qc> received packets. The allocated context is stored in |
| 5695 | * <qc.xprt_ctx>. |
| 5696 | * |
| 5697 | * Returns 0 on success else non-zero. |
| 5698 | */ |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 5699 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5700 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5701 | int ret = 0; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5702 | struct bind_conf *bc = qc->li->bind_conf; |
| 5703 | struct ssl_sock_ctx *ctx = NULL; |
| 5704 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5705 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5706 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5707 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5708 | if (!ctx) { |
| 5709 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5710 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5711 | } |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5712 | |
| 5713 | ctx->wait_event.tasklet = tasklet_new(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5714 | if (!ctx->wait_event.tasklet) { |
| 5715 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5716 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5717 | } |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5718 | |
| 5719 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5720 | ctx->wait_event.tasklet->context = ctx; |
| 5721 | ctx->wait_event.events = 0; |
| 5722 | ctx->subs = NULL; |
| 5723 | ctx->xprt_ctx = NULL; |
| 5724 | ctx->qc = qc; |
| 5725 | |
| 5726 | /* Set tasklet tid based on the SCID selected by us for this |
| 5727 | * connection. The upper layer will also be binded on the same thread. |
| 5728 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 5729 | 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] | 5730 | |
| 5731 | if (qc_is_listener(qc)) { |
| 5732 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5733 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5734 | goto err; |
| 5735 | } |
| 5736 | |
| 5737 | /* Enabling 0-RTT */ |
| 5738 | if (bc->ssl_conf.early_data) |
| 5739 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5740 | |
| 5741 | SSL_set_accept_state(ctx->ssl); |
| 5742 | } |
| 5743 | |
| 5744 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5745 | |
| 5746 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5747 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5748 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5749 | ret = 1; |
| 5750 | leave: |
| 5751 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5752 | return !ret; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5753 | |
| 5754 | err: |
| 5755 | if (ctx && ctx->wait_event.tasklet) |
| 5756 | tasklet_free(ctx->wait_event.tasklet); |
| 5757 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5758 | goto leave; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5759 | } |
| 5760 | |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5761 | /* Check that all the bytes between <buf> included and <end> address |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 5762 | * excluded are null. This is the responsibility of the caller to |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5763 | * check that there is at least one byte between <buf> end <end>. |
| 5764 | * Return 1 if this all the bytes are null, 0 if not. |
| 5765 | */ |
| 5766 | static inline int quic_padding_check(const unsigned char *buf, |
| 5767 | const unsigned char *end) |
| 5768 | { |
| 5769 | while (buf < end && !*buf) |
| 5770 | buf++; |
| 5771 | |
| 5772 | return buf == end; |
| 5773 | } |
| 5774 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5775 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5776 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5777 | * with the information collected from the packet. |
| 5778 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5779 | * byte to enable the caller to run this function again to continue to parse |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 5780 | * the remaining QUIC packets carried by the datagram. |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5781 | * Note that this function always sets this ->len value. If a paquet could |
| 5782 | * not be correctly found, ->len value will be set to the remaining number |
| 5783 | * bytes in the datagram to entirely consume this latter. |
| 5784 | */ |
| 5785 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5786 | struct quic_rx_packet *pkt, int first_pkt, |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 5787 | struct quic_dgram *dgram, struct list **tasklist_head) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5788 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5789 | unsigned char *beg, *payload; |
Frédéric Lécaille | 560ddfa | 2022-08-24 18:59:23 +0200 | [diff] [blame] | 5790 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5791 | struct listener *l; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5792 | struct proxy *prx; |
| 5793 | struct quic_counters *prx_counters; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5794 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5795 | 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] | 5796 | size_t b_cspace; |
| 5797 | struct quic_enc_level *qel; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5798 | uint32_t version; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5799 | const struct quic_version *qv = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5800 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5801 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 5802 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5803 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5804 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5805 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5806 | qel = NULL; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5807 | l = dgram->owner; |
| 5808 | prx = l->bind_conf->frontend; |
| 5809 | 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] | 5810 | /* This ist only to please to traces and distinguish the |
| 5811 | * packet with parsed packet number from others. |
| 5812 | */ |
| 5813 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5814 | if (end <= buf) { |
| 5815 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5816 | goto drop; |
| 5817 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5818 | |
| 5819 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5820 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5821 | if (!first_pkt && quic_padding_check(buf, end)) { |
| 5822 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 5823 | * That is what we called add padding out of QUIC packets. Such |
| 5824 | * datagrams must be considered as valid. But we can only consume |
| 5825 | * the remaining space. |
| 5826 | */ |
| 5827 | pkt->len = end - buf; |
| 5828 | goto drop_no_conn; |
| 5829 | } |
| 5830 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5831 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5832 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5833 | } |
| 5834 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5835 | /* Header form */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5836 | 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] | 5837 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5838 | goto drop; |
| 5839 | } |
| 5840 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5841 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5842 | uint64_t len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5843 | struct quic_cid odcid; |
Frédéric Lécaille | 7629f5d | 2022-08-11 18:54:26 +0200 | [diff] [blame] | 5844 | const struct quic_cid *token_odcid = NULL; // ODCID received from client token |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5845 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5846 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5847 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5848 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5849 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5850 | } |
| 5851 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5852 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5853 | 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] | 5854 | drop_no_conn = 1; |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5855 | } |
| 5856 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5857 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5858 | 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] | 5859 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
Frédéric Lécaille | e7df68a | 2022-08-05 09:34:44 +0200 | [diff] [blame] | 5860 | goto drop; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5861 | } |
| 5862 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5863 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5864 | * they must have the same DCID. |
| 5865 | */ |
| 5866 | if (!first_pkt && |
| 5867 | (pkt->dcid.len != dgram->dcid_len || |
| 5868 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5869 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5870 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5871 | } |
| 5872 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5873 | /* 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] | 5874 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5875 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5876 | goto drop; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5877 | } |
| 5878 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5879 | /* RFC9000 6. Version Negotiation */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5880 | qv = qc_supported_version(version); |
| 5881 | if (!qv) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5882 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5883 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 5884 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5885 | goto err; |
| 5886 | } |
| 5887 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5888 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5889 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5890 | } |
| 5891 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5892 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5893 | * there is no Initial connection IDs storage. |
| 5894 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5895 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5896 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5897 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5898 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5899 | end - buf < token_len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5900 | TRACE_PROTO("Packet dropped", |
| 5901 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5902 | goto drop; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5903 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5904 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5905 | /* TODO Retry should be automatically activated if |
| 5906 | * suspect network usage is detected. |
| 5907 | */ |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 5908 | if (global.cluster_secret && !token_len) { |
| 5909 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 5910 | TRACE_PROTO("Initial without token, sending retry", |
| 5911 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 5912 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 5913 | TRACE_PROTO("Error during Retry generation", |
| 5914 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5915 | goto err; |
| 5916 | } |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 5917 | |
| 5918 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5919 | goto err; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5920 | } |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5921 | } |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 5922 | else if (!global.cluster_secret && token_len) { |
| 5923 | /* Impossible case: a token was received without configured |
| 5924 | * cluster secret. |
| 5925 | */ |
| 5926 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 5927 | NULL, NULL, NULL, qv); |
| 5928 | goto drop; |
| 5929 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5930 | |
| 5931 | pkt->token = buf; |
| 5932 | pkt->token_len = token_len; |
| 5933 | buf += pkt->token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5934 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5935 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5936 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5937 | TRACE_PROTO("Packet dropped", |
| 5938 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5939 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5940 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5943 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5944 | end - buf < len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5945 | TRACE_PROTO("Packet dropped", |
| 5946 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5947 | goto drop; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5948 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5949 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5950 | payload = buf; |
| 5951 | pkt->len = len + payload - beg; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5952 | if (drop_no_conn) |
| 5953 | goto drop_no_conn; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5954 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5955 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 5956 | if (global.cluster_secret && pkt->token_len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5957 | if (*pkt->token == QUIC_TOKEN_FMT_RETRY) { |
| 5958 | const struct quic_version *ver = qc ? qc->original_version : qv; |
| 5959 | if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid, |
| 5960 | &pkt->scid, qc, &dgram->saddr)) { |
| 5961 | HA_ATOMIC_INC(&prx_counters->retry_error); |
| 5962 | TRACE_PROTO("Wrong retry token", |
| 5963 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5964 | /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection |
| 5965 | * with an INVALID_TOKEN error. |
| 5966 | */ |
| 5967 | goto drop; |
| 5968 | } |
| 5969 | |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 5970 | token_odcid = &odcid; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5971 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5972 | } |
| 5973 | else { |
| 5974 | /* TODO: New token check */ |
| 5975 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5976 | goto drop; |
| 5977 | } |
| 5978 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5979 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5980 | int ipv4; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5981 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5982 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5983 | 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] | 5984 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5987 | 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] | 5988 | 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] | 5989 | TRACE_PROTO("Initial without token, sending retry", |
| 5990 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5991 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
Frédéric Lécaille | 59507de | 2022-08-11 12:14:07 +0200 | [diff] [blame] | 5992 | TRACE_ERROR("Error during Retry generation", |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5993 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5994 | goto err; |
| 5995 | } |
| 5996 | |
| 5997 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5998 | goto err; |
| 5999 | } |
| 6000 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 6001 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6002 | * When an Initial packet is sent by a client that has not previously |
| 6003 | * received an Initial or Retry packet from the server, the client |
| 6004 | * populates the Destination Connection ID field with an unpredictable |
| 6005 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6006 | */ |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 6007 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6008 | TRACE_PROTO("dropped packet", |
| 6009 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6010 | goto drop; |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 6011 | } |
| 6012 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6013 | pkt->saddr = dgram->saddr; |
| 6014 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6015 | |
Frédéric Lécaille | e9325e9 | 2022-08-11 17:24:38 +0200 | [diff] [blame] | 6016 | qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 6017 | &pkt->saddr, 1, !!pkt->token_len, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 6018 | if (qc == NULL) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6019 | goto drop; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 6020 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 6021 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 6022 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Frédéric Lécaille | 560ddfa | 2022-08-24 18:59:23 +0200 | [diff] [blame] | 6023 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6024 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6025 | } |
Frédéric Lécaille | 560ddfa | 2022-08-24 18:59:23 +0200 | [diff] [blame] | 6026 | |
| 6027 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6028 | } |
| 6029 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6030 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6031 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6032 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6033 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6034 | } |
| 6035 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6036 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6037 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6038 | |
| 6039 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6040 | * they must have the same DCID. |
| 6041 | */ |
| 6042 | if (!first_pkt && |
| 6043 | (pkt->dcid.len != dgram->dcid_len || |
| 6044 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 6045 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6046 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6047 | } |
| 6048 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6049 | buf += QUIC_HAP_CID_LEN; |
| 6050 | |
| 6051 | /* A short packet is the last one of a UDP datagram. */ |
| 6052 | payload = buf; |
| 6053 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6054 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6055 | 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] | 6056 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6057 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 6058 | 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] | 6059 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6060 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6061 | goto drop; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 6064 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6065 | } |
| 6066 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6067 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 6068 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6069 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6070 | qc->nb_pkt_for_cc++; |
| 6071 | qc->nb_pkt_since_cc = 0; |
| 6072 | } |
| 6073 | /* Skip the entire datagram */ |
| 6074 | pkt->len = end - beg; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6075 | TRACE_STATE("Closing state connection", |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6076 | QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6077 | goto drop; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6078 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6079 | |
| 6080 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6081 | * they must have the same DCID. |
| 6082 | * |
| 6083 | * This check must be done after the final update to pkt.len to |
| 6084 | * properly drop the packet on failure. |
| 6085 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6086 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6087 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6088 | 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] | 6089 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6090 | /* Reset the anti-amplification bit. It will be set again |
| 6091 | * when sending the next packet if reached again. |
| 6092 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6093 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 6094 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6095 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6096 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6097 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6098 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6099 | |
Amaury Denoyelle | 72d8650 | 2022-07-13 15:08:23 +0200 | [diff] [blame] | 6100 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6101 | TRACE_PROTO("Connection error", |
| 6102 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6103 | goto out; |
| 6104 | } |
| 6105 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6106 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 6107 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6108 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6109 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 6110 | /* Do not consume buf if space not at the end. */ |
| 6111 | 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] | 6112 | TRACE_PROTO("Packet dropped", |
| 6113 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 6114 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 6115 | goto drop_no_conn; |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 6116 | } |
| 6117 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6118 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 6119 | if (b_cspace) { |
| 6120 | b_putchr(&qc->rx.buf, 0x00); |
| 6121 | b_cspace--; |
| 6122 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6123 | b_add(&qc->rx.buf, b_cspace); |
| 6124 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6125 | TRACE_PROTO("Too big packet", |
| 6126 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 6127 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 6128 | goto drop_no_conn; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6129 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6130 | } |
| 6131 | |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 6132 | if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6133 | 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] | 6134 | goto drop; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 6135 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6136 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6137 | TRACE_DATA("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] | 6138 | if (pkt->aad_len) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6139 | qc_pkt_insert(qc, pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6140 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 6141 | /* Wake up the connection packet handler task from here only if all |
| 6142 | * the contexts have been initialized, especially the mux context |
| 6143 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 6144 | * will start it if these contexts for the connection are not already |
| 6145 | * initialized. |
| 6146 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6147 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6148 | if (conn_ctx) |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 6149 | *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] | 6150 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6151 | drop_no_conn: |
| 6152 | if (drop_no_conn) |
| 6153 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6154 | 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] | 6155 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 6156 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6157 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6158 | drop: |
| 6159 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6160 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 6161 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 6162 | * This cannot be done by this thread. |
| 6163 | */ |
| 6164 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6165 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 6166 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 6167 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 6168 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 6169 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6170 | if (!pkt->len) |
| 6171 | pkt->len = end - beg; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6172 | 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] | 6173 | } |
| 6174 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6175 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6176 | * 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] | 6177 | */ |
| 6178 | 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] | 6179 | int type, size_t pn_len, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6180 | struct quic_conn *qc, const struct quic_version *ver) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6181 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6182 | int ret = 0; |
| 6183 | |
| 6184 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6185 | |
| 6186 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6187 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6188 | goto leave; |
| 6189 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6190 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6191 | type = quic_pkt_type(type, ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6192 | /* #0 byte flags */ |
| 6193 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6194 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6195 | /* Version */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6196 | quic_write_uint32(buf, end, ver->num); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6197 | *(*buf)++ = qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6198 | /* Destination connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6199 | if (qc->dcid.len) { |
| 6200 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6201 | *buf += qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6202 | } |
| 6203 | /* Source connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6204 | *(*buf)++ = qc->scid.len; |
| 6205 | if (qc->scid.len) { |
| 6206 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6207 | *buf += qc->scid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6208 | } |
| 6209 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6210 | ret = 1; |
| 6211 | leave: |
| 6212 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6213 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6214 | } |
| 6215 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6216 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6217 | * 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] | 6218 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6219 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6220 | size_t pn_len, struct quic_conn *qc, |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6221 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6222 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6223 | int ret = 0; |
| 6224 | |
| 6225 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6226 | |
| 6227 | if (end - *buf < 1 + qc->dcid.len) { |
| 6228 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6229 | goto leave; |
| 6230 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6231 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6232 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 6233 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6234 | ((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] | 6235 | /* Destination connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6236 | if (qc->dcid.len) { |
| 6237 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6238 | *buf += qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6239 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6240 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6241 | ret = 1; |
| 6242 | leave: |
| 6243 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6244 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6248 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6249 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6250 | * Returns 1 if succeeded or 0 if failed. |
| 6251 | */ |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 6252 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6253 | unsigned char *pn, size_t pnlen, |
| 6254 | struct quic_tls_ctx *tls_ctx) |
| 6255 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6256 | { |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 6257 | int i, ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6258 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6259 | * and at most 4 bytes for the packet number |
| 6260 | */ |
| 6261 | unsigned char mask[5] = {0}; |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 6262 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6263 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6264 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6265 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 6266 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6267 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6268 | goto out; |
| 6269 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6270 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6271 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6272 | for (i = 0; i < pnlen; i++) |
| 6273 | pn[i] ^= mask[i + 1]; |
| 6274 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6275 | ret = 1; |
| 6276 | out: |
| 6277 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6278 | return ret; |
| 6279 | } |
| 6280 | |
| 6281 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6282 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6283 | * Return 1 if succeeded, 0 if not. |
| 6284 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6285 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6286 | struct quic_frame *ack_frm, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6287 | { |
| 6288 | size_t room, ack_delay_sz; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6289 | int ret = 0; |
| 6290 | |
| 6291 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6292 | |
| 6293 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6294 | /* A frame is made of 1 byte for the frame type. */ |
| 6295 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6296 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6297 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6298 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6299 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6300 | leave: |
| 6301 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6302 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6303 | } |
| 6304 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6305 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6306 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6307 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6308 | * with the number of bytes already present in this buffer which must be taken |
| 6309 | * into an account for the Length packet field value. <headlen> is the number of |
| 6310 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6311 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6312 | * 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] | 6313 | * 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] | 6314 | * 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] | 6315 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6316 | 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] | 6317 | size_t room, size_t *len, size_t headlen, |
| 6318 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6319 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6320 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6321 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6322 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6323 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6324 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 6325 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6326 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6327 | if (*len > room) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6328 | goto leave; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6329 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6330 | /* If we are not probing we must take into an account the congestion |
| 6331 | * control window. |
| 6332 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6333 | if (!qel->pktns->tx.pto_probe) { |
| 6334 | size_t remain = quic_path_prep_data(qc->path); |
| 6335 | |
| 6336 | if (headlen > remain) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6337 | goto leave; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6338 | |
| 6339 | room = QUIC_MIN(room, remain - headlen); |
| 6340 | } |
| 6341 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6342 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6343 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6344 | |
| 6345 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 6346 | * returned by this function only if at least one frame could be built |
| 6347 | * in the switch/case block. |
| 6348 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6349 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6350 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6351 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6352 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6353 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6354 | break; |
| 6355 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6356 | switch (cf->type) { |
| 6357 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6358 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6359 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6360 | /* Compute the length of this CRYPTO frame header */ |
| 6361 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 6362 | /* Compute the data length of this CRyPTO frame. */ |
| 6363 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6364 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6365 | 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] | 6366 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6367 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6368 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6369 | /* CRYPTO frame length. */ |
| 6370 | flen = hlen + quic_int_getsize(dlen) + dlen; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6371 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6372 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6373 | /* Add the CRYPTO data length and its encoded length to the packet |
| 6374 | * length and the length of this length. |
| 6375 | */ |
| 6376 | *len += flen; |
| 6377 | room -= flen; |
| 6378 | if (dlen == cf->crypto.len) { |
| 6379 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6380 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6381 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6382 | } |
| 6383 | else { |
| 6384 | struct quic_frame *new_cf; |
| 6385 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6386 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6387 | if (!new_cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6388 | TRACE_ERROR("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] | 6389 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6390 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6391 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6392 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6393 | new_cf->type = QUIC_FT_CRYPTO; |
| 6394 | new_cf->crypto.len = dlen; |
| 6395 | new_cf->crypto.offset = cf->crypto.offset; |
| 6396 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | b827840 | 2022-08-19 09:32:14 +0200 | [diff] [blame] | 6397 | TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
| 6398 | if (cf->origin) { |
| 6399 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6400 | /* This <cf> frame was duplicated */ |
| 6401 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6402 | new_cf->origin = cf->origin; |
| 6403 | } |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6404 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6405 | /* Consume <dlen> bytes of the current frame. */ |
| 6406 | cf->crypto.len -= dlen; |
| 6407 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6408 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6409 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6410 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6411 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6412 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 6413 | struct eb64_node *node = NULL; |
| 6414 | struct qc_stream_desc *stream_desc = NULL; |
| 6415 | struct quic_stream *strm = &cf->stream; |
| 6416 | |
| 6417 | /* As this frame has been already lost, ensure the stream is always |
| 6418 | * available or the range of this frame is not consumed before |
| 6419 | * resending it. |
| 6420 | */ |
| 6421 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 6422 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6423 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6424 | LIST_DELETE(&cf->list); |
| 6425 | pool_free(pool_head_quic_frame, cf); |
| 6426 | continue; |
| 6427 | } |
| 6428 | |
| 6429 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 6430 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6431 | TRACE_DEVEL("ignored frame frame in already acked range", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 6432 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6433 | LIST_DELETE(&cf->list); |
| 6434 | pool_free(pool_head_quic_frame, cf); |
| 6435 | continue; |
| 6436 | } |
| 6437 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 6438 | strm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6439 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 6440 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6441 | } |
| 6442 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6443 | /* Note that these frames are accepted in short packets only without |
| 6444 | * "Length" packet field. Here, <*len> is used only to compute the |
| 6445 | * 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] | 6446 | * |
| 6447 | * 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] | 6448 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 6449 | */ |
| 6450 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6451 | ((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] | 6452 | /* Compute the data length of this STREAM frame. */ |
| 6453 | avail_room = room - hlen - *len; |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6454 | if ((ssize_t)avail_room <= 0) |
| 6455 | continue; |
| 6456 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6457 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
Frédéric Lécaille | adc7641 | 2022-08-08 16:09:46 +0200 | [diff] [blame] | 6458 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6459 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 6460 | dlen = max_available_room(avail_room, &dlen_sz); |
| 6461 | if (dlen > cf->stream.len) { |
| 6462 | dlen = cf->stream.len; |
| 6463 | } |
| 6464 | dlen_sz = quic_int_getsize(dlen); |
| 6465 | flen = hlen + dlen_sz + dlen; |
| 6466 | } |
| 6467 | else { |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 6468 | 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] | 6469 | flen = hlen + dlen; |
| 6470 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6471 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6472 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6473 | TRACE_DEVEL(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6474 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6475 | /* Add the STREAM data length and its encoded length to the packet |
| 6476 | * length and the length of this length. |
| 6477 | */ |
| 6478 | *len += flen; |
| 6479 | room -= flen; |
| 6480 | if (dlen == cf->stream.len) { |
| 6481 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6482 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6483 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6484 | |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 6485 | /* Do not notify MUX on retransmission. */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 6486 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6487 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 6488 | cf->stream.len, |
| 6489 | cf->stream.offset.key); |
| 6490 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6491 | } |
| 6492 | else { |
| 6493 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6494 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6495 | |
| 6496 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 6497 | if (!new_cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6498 | TRACE_ERROR("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] | 6499 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6500 | } |
| 6501 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6502 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6503 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6504 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6505 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6506 | new_cf->stream.id = cf->stream.id; |
| 6507 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 6508 | new_cf->stream.offset = cf->stream.offset; |
| 6509 | new_cf->stream.len = dlen; |
| 6510 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 6511 | /* FIN bit reset */ |
| 6512 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 6513 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | b827840 | 2022-08-19 09:32:14 +0200 | [diff] [blame] | 6514 | TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
| 6515 | if (cf->origin) { |
| 6516 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6517 | /* This <cf> frame was duplicated */ |
| 6518 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6519 | new_cf->origin = cf->origin; |
| 6520 | } |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6521 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6522 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 6523 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6524 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 6525 | b_size(cf->stream.buf), |
| 6526 | (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] | 6527 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6528 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6529 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6530 | |
Amaury Denoyelle | b29a1dc | 2022-08-12 14:30:04 +0200 | [diff] [blame] | 6531 | /* Do not notify MUX on retransmission. */ |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 6532 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6533 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 6534 | new_cf->stream.len, |
| 6535 | new_cf->stream.offset.key); |
| 6536 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6537 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6538 | |
| 6539 | /* TODO the MUX is notified about the frame sending via |
| 6540 | * previous qcc_streams_sent_done call. However, the |
| 6541 | * sending can fail later, for example if the sendto |
| 6542 | * system call returns an error. As the MUX has been |
| 6543 | * notified, the transport layer is responsible to |
| 6544 | * bufferize and resent the announced data later. |
| 6545 | */ |
| 6546 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6547 | break; |
| 6548 | |
| 6549 | default: |
| 6550 | flen = qc_frm_len(cf); |
| 6551 | BUG_ON(!flen); |
| 6552 | if (flen > room) |
| 6553 | continue; |
| 6554 | |
| 6555 | *len += flen; |
| 6556 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6557 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6558 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6559 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6560 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6561 | |
| 6562 | /* 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] | 6563 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6564 | } |
| 6565 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6566 | leave: |
| 6567 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6568 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6569 | } |
| 6570 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6571 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6572 | * is used as return parameter and should be zero'ed by the caller. |
| 6573 | */ |
| 6574 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6575 | struct quic_frame *out) |
| 6576 | { |
| 6577 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6578 | * |
| 6579 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6580 | * different encryption levels depending on the client context. This is |
| 6581 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6582 | * more details on how to implement it. |
| 6583 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6584 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 6585 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6586 | |
| 6587 | if (qc->err.app) { |
| 6588 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6589 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6590 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6591 | * |
| 6592 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6593 | * packet could expose application state or be used to alter application |
| 6594 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 6595 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 6596 | * Handshake packets. Otherwise, information about the application |
| 6597 | * state might be revealed. Endpoints MUST clear the value of the |
| 6598 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 6599 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 6600 | */ |
| 6601 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6602 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 6603 | out->connection_close.reason_phrase_len = 0; |
| 6604 | } |
| 6605 | else { |
| 6606 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 6607 | out->connection_close.error_code = qc->err.code; |
| 6608 | } |
| 6609 | } |
| 6610 | else { |
| 6611 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6612 | out->connection_close.error_code = qc->err.code; |
| 6613 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6614 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 6615 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6616 | } |
| 6617 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6618 | /* 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] | 6619 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 6620 | * 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] | 6621 | * filling the buffer with as much frames as possible from <frms> list of |
| 6622 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6623 | * 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] | 6624 | * 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] | 6625 | * having returned from this function. |
| 6626 | * 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] | 6627 | * number field in this packet. <pn_len> will also have the packet number |
| 6628 | * length as value. |
| 6629 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6630 | * 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] | 6631 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6632 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 6633 | size_t dglen, struct quic_tx_packet *pkt, |
| 6634 | 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] | 6635 | int force_ack, int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6636 | struct quic_enc_level *qel, struct quic_conn *qc, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6637 | const struct quic_version *ver, struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6638 | { |
Frédéric Lécaille | ebb1070 | 2022-08-16 12:03:00 +0200 | [diff] [blame] | 6639 | unsigned char *beg, *payload; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6640 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6641 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 6642 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 6643 | struct quic_frame cc_frm = { }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6644 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6645 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6646 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6647 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6648 | struct quic_frame *cf; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6649 | int must_ack, ret = 0; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6650 | int nb_aepkts_since_last_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6651 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6652 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6653 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6654 | /* Length field value with CRYPTO frames if present. */ |
| 6655 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6656 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6657 | /* When not probing, and no immediate close is required, reduce the size of this |
| 6658 | * buffer to respect the congestion controller window. |
| 6659 | * 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] | 6660 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6661 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6662 | size_t path_room; |
| 6663 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6664 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6665 | if (end - beg > path_room) |
| 6666 | end = beg + path_room; |
| 6667 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6668 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6669 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6670 | * length field if any. |
| 6671 | */ |
| 6672 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6673 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6674 | goto no_room; |
| 6675 | |
| 6676 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6677 | 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] | 6678 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6679 | *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] | 6680 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6681 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6682 | !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] | 6683 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6684 | !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] | 6685 | goto no_room; |
| 6686 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6687 | /* Encode the token length (0) for an Initial packet. */ |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6688 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6689 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6690 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6691 | /* Build an ACK frame if required. */ |
| 6692 | ack_frm_len = 0; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6693 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 6694 | must_ack = !qel->pktns->tx.pto_probe && |
| 6695 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 6696 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
Frédéric Lécaille | b866c69 | 2022-08-30 16:24:54 +0200 | [diff] [blame^] | 6697 | if (must_ack) { |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6698 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6699 | 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] | 6700 | ack_frm.tx_ack.arngs = arngs; |
| 6701 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 6702 | qel->pktns->tx.ack_delay = |
| 6703 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 6704 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 6705 | } |
| 6706 | 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] | 6707 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 6708 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 6709 | * that from here, we do not know if we will have to send a PING frame. |
| 6710 | * This will be decided after having computed the ack-eliciting frames |
| 6711 | * to be added to this packet. |
| 6712 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6713 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6714 | if (!ack_frm_len) |
| 6715 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6716 | } |
| 6717 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6718 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6719 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 6720 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6721 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6722 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6723 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6724 | TRACE_DEVEL("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] | 6725 | /* Initialize the length of the frames built below to <len>. |
| 6726 | * If any frame could be successfully built by qc_build_frms(), |
| 6727 | * we will have len_frms > len. |
| 6728 | */ |
| 6729 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6730 | if (!qc_build_frms(&frm_list, frms, |
| 6731 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6732 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6733 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 6734 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 6735 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6736 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6737 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6738 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6739 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 6740 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 6741 | * for the encryption tag. It must be taken into an account for the length |
| 6742 | * of this packet. |
| 6743 | */ |
| 6744 | if (len_frms) |
| 6745 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 6746 | else |
| 6747 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6748 | /* CONNECTION_CLOSE frame */ |
| 6749 | if (cc) { |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6750 | qc_build_cc_frm(qc, qel, &cc_frm); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6751 | len += qc_frm_len(&cc_frm); |
| 6752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6753 | add_ping_frm = 0; |
| 6754 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6755 | len_sz = quic_int_getsize(len); |
| 6756 | /* Add this packet size to <dglen> */ |
| 6757 | dglen += head_len + len_sz + len; |
| 6758 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 6759 | /* This is a maximum padding size */ |
| 6760 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 6761 | /* The length field value is of this packet is <len> + <padding_len> |
| 6762 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 6763 | * <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] | 6764 | * sizes from <padding_len>. |
| 6765 | */ |
| 6766 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 6767 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6768 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6769 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6770 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6771 | /* 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] | 6772 | add_ping_frm = 1; |
| 6773 | len += 1; |
| 6774 | } |
| 6775 | /* 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] | 6776 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6777 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 6778 | } |
| 6779 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6780 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 6781 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6782 | |
| 6783 | /* Packet number field address. */ |
| 6784 | *buf_pn = pos; |
| 6785 | |
| 6786 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6787 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 6788 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6789 | |
Frédéric Lécaille | ebb1070 | 2022-08-16 12:03:00 +0200 | [diff] [blame] | 6790 | /* payload building (ack-eliciting or not frames) */ |
| 6791 | payload = pos; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6792 | if (ack_frm_len) { |
| 6793 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 6794 | goto no_room; |
| 6795 | |
| 6796 | 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] | 6797 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6798 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6799 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6800 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6801 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | a8a6043 | 2022-08-23 11:42:48 +0200 | [diff] [blame] | 6802 | struct quic_frame *tmp_cf; |
| 6803 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 6804 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6805 | ssize_t room = end - pos; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6806 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6807 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 149c531 | 2022-08-27 15:51:30 +0200 | [diff] [blame] | 6808 | /* Note that <cf> was added from <frms> to <frm_list> list by |
Frédéric Lécaille | ebb1070 | 2022-08-16 12:03:00 +0200 | [diff] [blame] | 6809 | * qc_build_frms(). |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6810 | */ |
| 6811 | LIST_DELETE(&cf->list); |
| 6812 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 149c531 | 2022-08-27 15:51:30 +0200 | [diff] [blame] | 6813 | continue; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6814 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6815 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6816 | quic_tx_packet_refinc(pkt); |
| 6817 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | /* Build a PING frame if needed. */ |
| 6822 | if (add_ping_frm) { |
| 6823 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6824 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6825 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6826 | } |
| 6827 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6828 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6829 | if (cc) { |
| 6830 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 6831 | goto no_room; |
| 6832 | |
| 6833 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 6834 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6835 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6836 | /* Build a PADDING frame if needed. */ |
| 6837 | if (padding_len) { |
| 6838 | frm.type = QUIC_FT_PADDING; |
| 6839 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6840 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6841 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6842 | } |
| 6843 | |
Frédéric Lécaille | ebb1070 | 2022-08-16 12:03:00 +0200 | [diff] [blame] | 6844 | if (pos == payload) { |
| 6845 | /* No payload was built because of congestion control */ |
| 6846 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 6847 | goto no_room; |
| 6848 | } |
| 6849 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6850 | /* If this packet is ack-eliciting and we are probing let's |
| 6851 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6852 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6853 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 6854 | qel->pktns->tx.pto_probe) |
| 6855 | qel->pktns->tx.pto_probe--; |
| 6856 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6857 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6858 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6859 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6860 | ret = 1; |
| 6861 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 6862 | leave: |
| 6863 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6864 | return ret; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6865 | |
| 6866 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6867 | /* 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] | 6868 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6869 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 6870 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6871 | } |
| 6872 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6873 | 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] | 6874 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6875 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6876 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6877 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 6878 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6879 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6880 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6881 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6882 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6883 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6884 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6885 | } |
| 6886 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6887 | /* 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] | 6888 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 6889 | * of prebuilt frames. |
| 6890 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6891 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 6892 | * -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] | 6893 | * XXX NOTE XXX |
| 6894 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6895 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6896 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6897 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6898 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6899 | const unsigned char *buf_end, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6900 | struct quic_enc_level *qel, |
| 6901 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 6902 | struct quic_conn *qc, const struct quic_version *ver, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6903 | size_t dglen, int pkt_type, int force_ack, |
| 6904 | int padding, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6905 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6906 | struct quic_tx_packet *ret_pkt = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6907 | /* The pointer to the packet number field. */ |
| 6908 | unsigned char *buf_pn; |
| 6909 | unsigned char *beg, *end, *payload; |
| 6910 | int64_t pn; |
| 6911 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6912 | struct quic_tx_packet *pkt; |
| 6913 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6914 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6915 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6916 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6917 | if (!pkt) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6918 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6919 | *err = -2; |
| 6920 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6923 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6924 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6925 | pn_len = 0; |
| 6926 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6927 | |
| 6928 | pn = qel->pktns->tx.next_pn + 1; |
| 6929 | 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] | 6930 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6931 | // trace already emitted by function above |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6932 | *err = -1; |
| 6933 | goto err; |
| 6934 | } |
| 6935 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6936 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6937 | payload = buf_pn + pn_len; |
| 6938 | payload_len = end - payload; |
| 6939 | aad_len = payload - beg; |
| 6940 | |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6941 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6942 | // trace already emitted by function above |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6943 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6944 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6945 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6946 | |
| 6947 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6948 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 6949 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6950 | // trace already emitted by function above |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6951 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6952 | goto err; |
| 6953 | } |
| 6954 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6955 | /* Consume a packet number */ |
| 6956 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6957 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6958 | 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] | 6959 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6960 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 6961 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6962 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6963 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6964 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6965 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6966 | /* 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] | 6967 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6968 | pkt->in_flight_len = pkt->len; |
| 6969 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6970 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6971 | /* Always reset this flags */ |
| 6972 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6973 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6974 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6975 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6976 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6977 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6978 | pkt->pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6979 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6980 | ret_pkt = pkt; |
| 6981 | leave: |
| 6982 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 6983 | return ret_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6984 | |
| 6985 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6986 | /* TODO: what about the frames which have been built |
| 6987 | * for this packet. |
| 6988 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 6989 | free_quic_tx_packet(qc, pkt); |
| 6990 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6991 | } |
| 6992 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6993 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6994 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6995 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6996 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6997 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6998 | */ |
| 6999 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 7000 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7001 | struct quic_conn *qc = conn->handle.qc; |
| 7002 | struct qcc *qcc = qc->qcc; |
| 7003 | |
| 7004 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 7005 | |
| 7006 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 7007 | BUG_ON(qcc->subs && qcc->subs != es); |
| 7008 | |
| 7009 | es->events |= event_type; |
| 7010 | qcc->subs = es; |
| 7011 | |
| 7012 | if (event_type & SUB_RETRY_RECV) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7013 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 7014 | |
| 7015 | if (event_type & SUB_RETRY_SEND) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7016 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc); |
| 7017 | |
| 7018 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 7019 | |
| 7020 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 7024 | * The <es> pointer is not allowed to differ from the one passed to the |
| 7025 | * subscribe() call. It always returns zero. |
| 7026 | */ |
| 7027 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 7028 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7029 | int ret; |
| 7030 | struct quic_conn *qc = conn->handle.qc; |
| 7031 | struct qcc *qcc = qc->qcc; |
| 7032 | |
| 7033 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
| 7034 | |
| 7035 | if (event_type & SUB_RETRY_RECV) |
| 7036 | TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc); |
| 7037 | if (event_type & SUB_RETRY_SEND) |
| 7038 | TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc); |
| 7039 | |
| 7040 | ret = conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 7041 | |
| 7042 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
| 7043 | |
| 7044 | return ret; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 7045 | } |
| 7046 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 7047 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 7048 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7049 | */ |
| 7050 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 7051 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 7052 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7053 | |
| 7054 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 7055 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 7056 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7057 | if (*xprt_ctx) |
| 7058 | goto out; |
| 7059 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7060 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7061 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7062 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 7063 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7064 | |
| 7065 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7066 | } |
| 7067 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7068 | /* Start the QUIC transport layer */ |
| 7069 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 7070 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7071 | int ret = 0; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7072 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 7073 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7074 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7075 | qc = conn->handle.qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7076 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 7077 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 7078 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 7079 | /* prepare a CONNECTION_CLOSE frame */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 7080 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR)); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7081 | goto out; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | /* mux-quic can now be considered ready. */ |
| 7085 | qc->mux_state = QC_MUX_READY; |
| 7086 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7087 | tasklet_wakeup(qctx->wait_event.tasklet); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7088 | |
| 7089 | ret = 1; |
| 7090 | out: |
| 7091 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 7092 | return ret; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7093 | } |
| 7094 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7095 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 7096 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7097 | 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] | 7098 | return NULL; |
| 7099 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7100 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7101 | } |
| 7102 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7103 | /* transport-layer operations for QUIC connections. */ |
| 7104 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 7105 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 7106 | .subscribe = quic_conn_subscribe, |
| 7107 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7108 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7109 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7110 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 7111 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 7112 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7113 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7114 | .name = "QUIC", |
| 7115 | }; |
| 7116 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7117 | static void __quic_conn_init(void) |
| 7118 | { |
| 7119 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7120 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 7121 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7122 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7123 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7124 | static void __quic_conn_deinit(void) |
| 7125 | { |
| 7126 | BIO_meth_free(ha_quic_meth); |
| 7127 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7128 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7129 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 7130 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 7131 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 7132 | * 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] | 7133 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7134 | 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] | 7135 | { |
| 7136 | unsigned char *pos; |
| 7137 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7138 | struct quic_dghdlr *dghdlr = ctx; |
| 7139 | struct quic_dgram *dgram; |
| 7140 | int first_pkt = 1; |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 7141 | struct list *tasklist_head = NULL; |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7142 | int max_dgrams = global.tune.maxpollevents; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7143 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7144 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7145 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7146 | 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] | 7147 | pos = dgram->buf; |
| 7148 | end = pos + dgram->len; |
| 7149 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7150 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7151 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7152 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7153 | if (!pkt) { |
| 7154 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7155 | goto leave; |
| 7156 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7157 | |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 7158 | LIST_INIT(&pkt->qc_rx_pkt_list); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 7159 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7160 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 7161 | 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] | 7162 | first_pkt = 0; |
| 7163 | pos += pkt->len; |
| 7164 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 7165 | |
| 7166 | /* Free rejected packets */ |
| 7167 | if (!pkt->refcnt) { |
| 7168 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7169 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7170 | } |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7171 | } while (pos < end); |
| 7172 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7173 | /* Increasing the received bytes counter by the UDP datagram length |
| 7174 | * if this datagram could be associated to a connection. |
| 7175 | */ |
| 7176 | if (dgram->qc) |
| 7177 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 7178 | |
| 7179 | /* Mark this datagram as consumed */ |
| 7180 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7181 | |
| 7182 | if (--max_dgrams <= 0) |
| 7183 | goto stop_here; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7184 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7185 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7186 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7187 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7188 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7189 | |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7190 | stop_here: |
| 7191 | /* too much work done at once, come back here later */ |
| 7192 | if (!MT_LIST_ISEMPTY(&dghdlr->dgrams)) |
| 7193 | tasklet_wakeup((struct tasklet *)t); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7194 | leave: |
| 7195 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7196 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7197 | } |
| 7198 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 7199 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7200 | * Returns 1 if succeeded, 0 if not. |
| 7201 | */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 7202 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7203 | unsigned char **dcid, size_t *dcid_len) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7204 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7205 | int ret = 0, long_header; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7206 | size_t minlen, skip; |
| 7207 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7208 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7209 | |
| 7210 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7211 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7212 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7213 | } |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7214 | |
| 7215 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 7216 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7217 | 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] | 7218 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
Willy Tarreau | af5138f | 2022-08-05 10:09:32 +0200 | [diff] [blame] | 7219 | if (end - buf < minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7220 | goto err; |
| 7221 | |
| 7222 | buf += skip; |
| 7223 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7224 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7225 | goto err; |
| 7226 | |
| 7227 | *dcid = buf; |
| 7228 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7229 | ret = 1; |
| 7230 | leave: |
| 7231 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7232 | return ret; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7233 | |
| 7234 | err: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7235 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7236 | goto leave; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7237 | } |
| 7238 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7239 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7240 | void qc_notify_close(struct quic_conn *qc) |
| 7241 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7242 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7243 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7244 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7245 | goto leave; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7246 | |
| 7247 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7248 | /* wake up the MUX */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7249 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7250 | TRACE_STATE("connection closure notidfied to mux", |
| 7251 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7252 | qc->conn->mux->wake(qc->conn); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 7253 | } |
| 7254 | else |
| 7255 | TRACE_STATE("connection closure not notidfied to mux", |
| 7256 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7257 | leave: |
| 7258 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7259 | } |
| 7260 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7261 | /* |
| 7262 | * Local variables: |
| 7263 | * c-indent-level: 8 |
| 7264 | * c-basic-offset: 8 |
| 7265 | * End: |
| 7266 | */ |