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 | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 369 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 370 | (unsigned long long)qc->rx.bytes, |
| 371 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (room) { |
| 375 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 376 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 377 | (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] | 378 | } |
| 379 | } |
| 380 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 381 | if (mask & QUIC_EV_CONN_IO_CB) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 382 | const enum quic_handshake_state *state = a2; |
| 383 | const int *err = a3; |
| 384 | |
| 385 | if (state) |
| 386 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 387 | if (err) |
| 388 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 389 | } |
| 390 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 391 | 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] | 392 | const struct quic_rx_packet *pkt = a2; |
| 393 | const unsigned long *pktlen = a3; |
| 394 | const SSL *ssl = a4; |
| 395 | |
| 396 | if (pkt) { |
Frédéric Lécaille | 3dfd4c4 | 2022-04-05 15:29:14 +0200 | [diff] [blame] | 397 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 398 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 399 | chunk_appendf(&trace_buf, " kp=%d", |
| 400 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 401 | chunk_appendf(&trace_buf, " el=%c", |
| 402 | quic_packet_type_enc_level_char(pkt->type)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 403 | if (pkt->pnl) |
| 404 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 405 | (unsigned long long)pkt->pn); |
| 406 | if (pkt->token_len) |
| 407 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 408 | (unsigned long long)pkt->token_len); |
| 409 | if (pkt->aad_len) |
| 410 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 411 | (unsigned long long)pkt->aad_len); |
| 412 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 413 | pkt->flags, (unsigned long long)pkt->len); |
| 414 | } |
| 415 | if (pktlen) |
| 416 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 417 | if (ssl) { |
| 418 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 419 | chunk_appendf(&trace_buf, " el=%c", |
| 420 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 421 | } |
| 422 | } |
| 423 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 424 | 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] | 425 | const struct quic_rx_packet *pkt = a2; |
| 426 | const struct quic_rx_crypto_frm *cf = a3; |
| 427 | const SSL *ssl = a4; |
| 428 | |
| 429 | if (pkt) |
| 430 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 431 | quic_packet_type_enc_level_char(pkt->type), |
| 432 | (unsigned long long)pkt->pn); |
| 433 | if (cf) |
| 434 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 435 | (unsigned long long)cf->offset_node.key, |
| 436 | (unsigned long long)cf->len); |
| 437 | if (ssl) { |
| 438 | 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] | 439 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 440 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 441 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 442 | |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 443 | if (qc->err.code) |
| 444 | 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] | 445 | } |
| 446 | |
| 447 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 448 | const struct quic_frame *frm = a2; |
| 449 | |
| 450 | if (frm) |
| 451 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 452 | } |
| 453 | |
| 454 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 455 | const struct quic_enc_level *qel = a2; |
| 456 | |
| 457 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 458 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 459 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 460 | " 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] | 461 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 462 | quic_hdshk_state_str(qc->state), |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 463 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 464 | (unsigned long long)qc->path->cwnd, |
| 465 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 466 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 467 | (unsigned long long)pktns->tx.in_flight, |
| 468 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 472 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 473 | const struct enc_debug_info *edi = a2; |
| 474 | |
| 475 | if (edi) |
| 476 | chunk_appendf(&trace_buf, |
| 477 | " payload=@%p payload_len=%llu" |
| 478 | " aad=@%p aad_len=%llu pn=%llu", |
| 479 | edi->payload, (unsigned long long)edi->payload_len, |
| 480 | edi->aad, (unsigned long long)edi->aad_len, |
| 481 | (unsigned long long)edi->pn); |
| 482 | } |
| 483 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 484 | if (mask & QUIC_EV_CONN_RMHP) { |
| 485 | const struct quic_rx_packet *pkt = a2; |
| 486 | |
| 487 | if (pkt) { |
| 488 | const int *ret = a3; |
| 489 | |
| 490 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 491 | if (ret && *ret) |
| 492 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 493 | pkt->pnl, (unsigned long long)pkt->pn); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 498 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 499 | const unsigned long *val1 = a3; |
| 500 | const unsigned long *val2 = a4; |
| 501 | |
| 502 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 503 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 504 | if (val1) |
| 505 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 506 | if (val2) |
| 507 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 508 | } |
| 509 | |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 510 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 511 | const struct quic_stream *s = a2; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 512 | const struct qc_stream_desc *stream = a3; |
Frédéric Lécaille | ce69cbc | 2022-03-22 12:45:33 +0100 | [diff] [blame] | 513 | |
| 514 | if (s) |
| 515 | 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] | 516 | if (stream) |
| 517 | 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] | 518 | } |
| 519 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 520 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 521 | const unsigned int *rtt_sample = a2; |
| 522 | const unsigned int *ack_delay = a3; |
| 523 | const struct quic_loss *ql = a4; |
| 524 | |
| 525 | if (rtt_sample) |
| 526 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 527 | if (ack_delay) |
| 528 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 529 | if (ql) |
| 530 | chunk_appendf(&trace_buf, |
| 531 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 532 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 533 | } |
| 534 | if (mask & QUIC_EV_CONN_CC) { |
| 535 | const struct quic_cc_event *ev = a2; |
| 536 | const struct quic_cc *cc = a3; |
| 537 | |
| 538 | if (a2) |
| 539 | quic_cc_event_trace(&trace_buf, ev); |
| 540 | if (a3) |
| 541 | quic_cc_state_trace(&trace_buf, cc); |
| 542 | } |
| 543 | |
| 544 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 545 | const struct quic_pktns *pktns = a2; |
| 546 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 547 | |
| 548 | if (pktns) { |
| 549 | chunk_appendf(&trace_buf, " pktns=%s", |
| 550 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 551 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 552 | if (pktns->tx.loss_time) |
| 553 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 554 | 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] | 555 | } |
| 556 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 557 | struct quic_tx_packet *pkt; |
| 558 | |
| 559 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 560 | list_for_each_entry(pkt, lost_pkts, list) |
| 561 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | 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] | 566 | const struct quic_pktns *pktns = a2; |
| 567 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 568 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 569 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 570 | if (ifae_pkts) |
| 571 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 572 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 573 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 574 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 575 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 576 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 577 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 578 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 579 | if (pktns->tx.in_flight) |
| 580 | 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] | 581 | if (pktns->tx.loss_time) |
| 582 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 583 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 584 | } |
| 585 | if (mask & QUIC_EV_CONN_SPTO) { |
| 586 | if (pktns->tx.time_of_last_eliciting) |
| 587 | chunk_appendf(&trace_buf, " tole=%dms", |
| 588 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 589 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 590 | 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] | 591 | } |
| 592 | } |
| 593 | |
Frédéric Lécaille | 03235d7 | 2022-03-30 14:36:40 +0200 | [diff] [blame] | 594 | 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] | 595 | chunk_appendf(&trace_buf, |
| 596 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 601 | const struct quic_tx_packet *pkt = a2; |
| 602 | |
Frédéric Lécaille | 8ecb736 | 2022-08-04 12:00:00 +0200 | [diff] [blame] | 603 | 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] | 604 | (unsigned long long)qc->path->cwnd, |
| 605 | (unsigned long long)qc->path->prep_in_flight, |
| 606 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 607 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 608 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 609 | (unsigned long)pkt->pn_node.key, |
| 610 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 611 | 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] | 612 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 613 | } |
| 614 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 615 | |
| 616 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 617 | const uint8_t *alert = a2; |
| 618 | const enum ssl_encryption_level_t *level = a3; |
| 619 | |
| 620 | if (alert) |
| 621 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 622 | if (level) |
| 623 | chunk_appendf(&trace_buf, " el=%c", |
| 624 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 625 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 626 | |
| 627 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 628 | const size_t *sz1 = a2; |
| 629 | const size_t *sz2 = a3; |
| 630 | const size_t *sz3 = a4; |
| 631 | |
| 632 | if (sz1) |
| 633 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 634 | if (sz2) |
| 635 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 636 | if (sz3) |
| 637 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 638 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 639 | |
| 640 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 641 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 642 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 643 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 644 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 645 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 646 | } |
| 647 | if (mask & QUIC_EV_CONN_LPKT) { |
| 648 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 649 | const uint64_t *len = a3; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 650 | const struct quic_version *ver = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 651 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 652 | if (pkt) { |
| 653 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 654 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 655 | if (pkt->pn_node.key != (uint64_t)-1) |
| 656 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 657 | } |
| 658 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 659 | if (len) |
| 660 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 661 | |
| 662 | if (ver) |
| 663 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 666 | if (mask & QUIC_EV_STATELESS_RST) { |
| 667 | const struct quic_cid *cid = a2; |
| 668 | |
| 669 | if (cid) |
| 670 | quic_cid_dump(&trace_buf, cid); |
| 671 | } |
| 672 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* 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] | 676 | 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] | 677 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 678 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 679 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 680 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 681 | return 1; |
| 682 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 683 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 684 | 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] | 685 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 686 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 687 | qc->state >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 688 | return 1; |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 694 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 695 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 696 | 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] | 697 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 698 | struct quic_pktns *pktns; |
| 699 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 700 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 701 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 702 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 703 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 704 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 705 | pktns = quic_loss_pktns(qc); |
| 706 | if (tick_isset(pktns->tx.loss_time)) { |
| 707 | qc->timer = pktns->tx.loss_time; |
| 708 | goto out; |
| 709 | } |
| 710 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 711 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 712 | * cancelled for a server which reached the anti-amplification limit. |
| 713 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 714 | if (!quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 715 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 716 | 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] | 717 | qc->timer = TICK_ETERNITY; |
| 718 | goto out; |
| 719 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 720 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 721 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 722 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 723 | /* Timer cancellation. */ |
| 724 | qc->timer = TICK_ETERNITY; |
| 725 | goto out; |
| 726 | } |
| 727 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 728 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 729 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 730 | if (tick_isset(pto)) |
| 731 | qc->timer = pto; |
| 732 | out: |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 733 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 734 | if (tick_is_expired(qc->timer, now_ms)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 735 | 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] | 736 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 737 | } |
| 738 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 739 | 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] | 740 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | aaf1f19 | 2022-03-22 15:37:41 +0100 | [diff] [blame] | 741 | } |
Frédéric Lécaille | 5757b4a | 2022-02-16 14:46:17 +0100 | [diff] [blame] | 742 | } |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 743 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 746 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 747 | * connection. |
| 748 | * Return 1 if succeeded, 0 if not. |
| 749 | */ |
| 750 | static int quic_tls_key_update(struct quic_conn *qc) |
| 751 | { |
| 752 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 753 | struct quic_tls_secrets *rx, *tx; |
| 754 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 755 | 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] | 756 | const struct quic_version *ver = |
| 757 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 758 | int ret = 0; |
| 759 | |
| 760 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 761 | |
| 762 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 763 | rx = &tls_ctx->rx; |
| 764 | tx = &tls_ctx->tx; |
| 765 | nxt_rx = &qc->ku.nxt_rx; |
| 766 | nxt_tx = &qc->ku.nxt_tx; |
| 767 | |
| 768 | /* Prepare new RX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 769 | 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] | 770 | rx->secret, rx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 771 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 772 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 775 | 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] | 776 | nxt_rx->key, nxt_rx->keylen, |
| 777 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 778 | nxt_rx->secret, nxt_rx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 779 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 780 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* Prepare new TX secrets */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 784 | 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] | 785 | tx->secret, tx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 786 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 787 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 788 | } |
| 789 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 790 | 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] | 791 | nxt_tx->key, nxt_tx->keylen, |
| 792 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 793 | nxt_tx->secret, nxt_tx->secretlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 794 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 795 | goto leave; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 798 | if (nxt_rx->ctx) { |
| 799 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 800 | nxt_rx->ctx = NULL; |
| 801 | } |
| 802 | |
| 803 | 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^] | 804 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 805 | goto leave; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (nxt_tx->ctx) { |
| 809 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 810 | nxt_tx->ctx = NULL; |
| 811 | } |
| 812 | |
| 813 | 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^] | 814 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 815 | goto leave; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 818 | ret = 1; |
| 819 | leave: |
| 820 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc); |
| 821 | return ret; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 825 | * Must be used after having updated them. |
| 826 | * Always succeeds. |
| 827 | */ |
| 828 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 829 | { |
| 830 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 831 | unsigned char *curr_secret, *curr_iv, *curr_key; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 832 | EVP_CIPHER_CTX *curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 833 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 834 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 835 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 836 | /* Rotate the RX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 837 | curr_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 838 | curr_secret = tls_ctx->rx.secret; |
| 839 | curr_iv = tls_ctx->rx.iv; |
| 840 | curr_key = tls_ctx->rx.key; |
| 841 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 842 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 843 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 844 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 845 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 846 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 847 | 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] | 848 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 849 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 850 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 851 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 852 | qc->ku.prv_rx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 853 | qc->ku.prv_rx.secret = curr_secret; |
| 854 | qc->ku.prv_rx.iv = curr_iv; |
| 855 | qc->ku.prv_rx.key = curr_key; |
| 856 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 857 | |
| 858 | /* Update the TX secrets */ |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 859 | curr_ctx = tls_ctx->tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 860 | curr_secret = tls_ctx->tx.secret; |
| 861 | curr_iv = tls_ctx->tx.iv; |
| 862 | curr_key = tls_ctx->tx.key; |
| 863 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 864 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 865 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 866 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 867 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 868 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 869 | qc->ku.nxt_tx.ctx = curr_ctx; |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 870 | qc->ku.nxt_tx.secret = curr_secret; |
| 871 | qc->ku.nxt_tx.iv = curr_iv; |
| 872 | qc->ku.nxt_tx.key = curr_key; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 873 | |
| 874 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 877 | /* returns 0 on error, 1 on success */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 878 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 879 | const uint8_t *read_secret, |
| 880 | const uint8_t *write_secret, size_t secret_len) |
| 881 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 882 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 883 | 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] | 884 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 885 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 886 | const struct quic_version *ver = |
| 887 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 888 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 889 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 890 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 891 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 892 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 893 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 894 | goto no_secret; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 895 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 896 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 897 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 898 | TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 899 | goto leave; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | rx = &tls_ctx->rx; |
| 903 | tx = &tls_ctx->tx; |
| 904 | |
| 905 | rx->aead = tx->aead = tls_aead(cipher); |
| 906 | rx->md = tx->md = tls_md(cipher); |
| 907 | rx->hp = tx->hp = tls_hp(cipher); |
| 908 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 909 | 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] | 910 | 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] | 911 | read_secret, secret_len)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 912 | TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 913 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 916 | 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^] | 917 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 918 | goto leave; |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 919 | } |
| 920 | |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 921 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 922 | * listener. Note that a listener derives only RX secrets for this |
| 923 | * level. |
| 924 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 925 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 926 | 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] | 927 | quic_accept_push_qc(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 928 | } |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 929 | |
| 930 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 931 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 932 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 933 | 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] | 934 | 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] | 935 | write_secret, secret_len)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 936 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 937 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 940 | 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^] | 941 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 942 | goto leave; |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 945 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 946 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 947 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 948 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 949 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 950 | /* 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] | 951 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 952 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 953 | TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 954 | goto leave; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | memcpy(rx->secret, read_secret, secret_len); |
| 958 | rx->secretlen = secret_len; |
| 959 | memcpy(tx->secret, write_secret, secret_len); |
| 960 | tx->secretlen = secret_len; |
| 961 | /* Initialize all the secret keys lengths */ |
| 962 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 963 | /* Prepare the next key update */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 964 | if (!quic_tls_key_update(qc)) { |
| 965 | // trace already emitted by function above |
| 966 | goto leave; |
| 967 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 968 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 969 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 970 | out: |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 971 | tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | f44d19e | 2022-03-26 12:22:41 +0100 | [diff] [blame] | 972 | no_secret: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 973 | ret = 1; |
| 974 | leave: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 975 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 976 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 977 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 978 | |
| 979 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 980 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 981 | * 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^] | 982 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 983 | * buffers to store all the data. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 984 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 985 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 986 | 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] | 987 | const unsigned char *data, size_t len) |
| 988 | { |
| 989 | struct quic_crypto_buf **qcb; |
| 990 | /* The remaining byte to store in CRYPTO buffers. */ |
| 991 | size_t cf_offset, cf_len, *nb_buf; |
| 992 | unsigned char *pos; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 993 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 994 | |
| 995 | nb_buf = &qel->tx.crypto.nb_buf; |
| 996 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 997 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 998 | cf_len = len; |
| 999 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1000 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1001 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1002 | while (len) { |
| 1003 | size_t to_copy, room; |
| 1004 | |
| 1005 | pos = (*qcb)->data + (*qcb)->sz; |
| 1006 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1007 | to_copy = len > room ? room : len; |
| 1008 | if (to_copy) { |
| 1009 | memcpy(pos, data, to_copy); |
| 1010 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1011 | qel->tx.crypto.sz += to_copy; |
| 1012 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1013 | len -= to_copy; |
| 1014 | data += to_copy; |
| 1015 | } |
| 1016 | else { |
| 1017 | struct quic_crypto_buf **tmp; |
| 1018 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1019 | // FIXME: realloc! |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1020 | tmp = realloc(qel->tx.crypto.bufs, |
| 1021 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1022 | if (tmp) { |
| 1023 | qel->tx.crypto.bufs = tmp; |
| 1024 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1025 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1026 | if (!*qcb) { |
| 1027 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1028 | goto leave; |
| 1029 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1030 | |
| 1031 | (*qcb)->sz = 0; |
| 1032 | ++*nb_buf; |
| 1033 | } |
| 1034 | else { |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1041 | * have been buffered. |
| 1042 | */ |
| 1043 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1044 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1045 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1046 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1047 | /* There is at most one CRYPTO frame in this packet number |
| 1048 | * space. Let's look for it. |
| 1049 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1050 | 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] | 1051 | if (frm->type != QUIC_FT_CRYPTO) |
| 1052 | continue; |
| 1053 | |
| 1054 | /* Found */ |
| 1055 | found = frm; |
| 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | if (found) { |
| 1060 | found->crypto.len += cf_len; |
| 1061 | } |
| 1062 | else { |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1063 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1064 | if (!frm) { |
| 1065 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1066 | goto leave; |
| 1067 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1068 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1069 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1070 | frm->type = QUIC_FT_CRYPTO; |
| 1071 | frm->crypto.offset = cf_offset; |
| 1072 | frm->crypto.len = cf_len; |
| 1073 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1074 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1075 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1076 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1077 | ret = len == 0; |
| 1078 | leave: |
| 1079 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1080 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1083 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1084 | * activity for <qc> will be interrupted. |
| 1085 | */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1086 | 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] | 1087 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1088 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1089 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1090 | goto leave; |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1091 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1092 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1093 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1094 | qc->err.code = err.code; |
| 1095 | qc->err.app = err.app; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1096 | leave: |
| 1097 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1098 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1099 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1100 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1101 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1102 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1103 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1104 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 1105 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1106 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1107 | 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] | 1108 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1109 | } |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 1110 | quic_set_connection_close(qc, quic_err_tls(alert)); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1111 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1112 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1113 | |
| 1114 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1117 | /* Set the application for <qc> QUIC connection. |
| 1118 | * Return 1 if succeeded, 0 if not. |
| 1119 | */ |
| 1120 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1121 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1122 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1123 | qc->app_ops = &h3_ops; |
| 1124 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1125 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1126 | else |
| 1127 | return 0; |
| 1128 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1129 | return 1; |
| 1130 | } |
| 1131 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1132 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1133 | * wants to provide the QUIC layer with CRYPTO data. |
| 1134 | * Returns 1 if succeeded, 0 if not. |
| 1135 | */ |
| 1136 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1137 | const uint8_t *data, size_t len) |
| 1138 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1139 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1140 | enum quic_tls_enc_level tel; |
| 1141 | struct quic_enc_level *qel; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1142 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1143 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1144 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1145 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1146 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 1147 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1148 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1149 | goto out; |
| 1150 | } |
| 1151 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1152 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1153 | if (tel == -1) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1154 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1155 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1158 | qel = &qc->els[tel]; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1159 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1160 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1161 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1164 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1165 | qc, &level, &len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1166 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1167 | ret = 1; |
| 1168 | leave: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1169 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1170 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | int ha_quic_flush_flight(SSL *ssl) |
| 1174 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1175 | 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] | 1176 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1177 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1178 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1179 | |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1184 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1185 | 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] | 1186 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1187 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1188 | |
| 1189 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1190 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1191 | quic_set_tls_alert(qc, alert); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1192 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1193 | return 1; |
| 1194 | } |
| 1195 | |
| 1196 | /* QUIC TLS methods */ |
| 1197 | static SSL_QUIC_METHOD ha_quic_method = { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1198 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1199 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1200 | .flush_flight = ha_quic_flush_flight, |
| 1201 | .send_alert = ha_quic_send_alert, |
| 1202 | }; |
| 1203 | |
| 1204 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1205 | * Returns an error count. |
| 1206 | */ |
| 1207 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1208 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1209 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1210 | int cfgerr = 0; |
| 1211 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1212 | long options = |
| 1213 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1214 | SSL_OP_SINGLE_ECDH_USE | |
| 1215 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1216 | SSL_CTX *ctx; |
| 1217 | |
| 1218 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1219 | bind_conf->initial_ctx = ctx; |
| 1220 | |
| 1221 | SSL_CTX_set_options(ctx, options); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1222 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1223 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1224 | 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] | 1225 | |
| 1226 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | e06f745 | 2022-06-16 15:06:44 +0200 | [diff] [blame] | 1227 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1228 | if (bind_conf->ssl_conf.early_data) { |
| 1229 | 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] | 1230 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1231 | } |
| 1232 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1233 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1234 | #else |
| 1235 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1236 | #endif |
| 1237 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1238 | #endif |
| 1239 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1240 | |
| 1241 | return cfgerr; |
| 1242 | } |
| 1243 | |
| 1244 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1245 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1246 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1247 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1248 | */ |
| 1249 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1250 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1251 | { |
| 1252 | uint64_t expected_pn = largest_pn + 1; |
| 1253 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1254 | uint64_t pn_hwin = pn_win / 2; |
| 1255 | uint64_t pn_mask = pn_win - 1; |
| 1256 | uint64_t candidate_pn; |
| 1257 | |
| 1258 | |
| 1259 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1260 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1261 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1262 | candidate_pn + pn_hwin <= expected_pn) |
| 1263 | return candidate_pn + pn_win; |
| 1264 | |
| 1265 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1266 | return candidate_pn - pn_win; |
| 1267 | |
| 1268 | return candidate_pn; |
| 1269 | } |
| 1270 | |
| 1271 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1272 | * cryptographic context. |
| 1273 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1274 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1275 | * <end> points to one byte past the end of this packet. |
| 1276 | * Returns 1 if succeeded, 0 if not. |
| 1277 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1278 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1279 | 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] | 1280 | 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] | 1281 | { |
| 1282 | int ret, outlen, i, pnlen; |
| 1283 | uint64_t packet_number; |
| 1284 | uint32_t truncated_pn = 0; |
| 1285 | unsigned char mask[5] = {0}; |
| 1286 | unsigned char *sample; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1287 | EVP_CIPHER_CTX *cctx = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1288 | unsigned char *hp_key; |
| 1289 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1290 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1291 | |
| 1292 | ret = 0; |
| 1293 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1294 | /* Check there is enough data in this packet. */ |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 1295 | 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^] | 1296 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1297 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | cctx = EVP_CIPHER_CTX_new(); |
| 1301 | if (!cctx) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1302 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1303 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1306 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1307 | |
| 1308 | hp_key = tls_ctx->rx.hp_key; |
| 1309 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1310 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1311 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1312 | TRACE_ERROR("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1313 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1317 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1318 | for (i = 0; i < pnlen; i++) { |
| 1319 | pn[i] ^= mask[i + 1]; |
| 1320 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1321 | } |
| 1322 | |
| 1323 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1324 | /* Store remaining information for this unprotected header */ |
| 1325 | pkt->pn = packet_number; |
| 1326 | pkt->pnl = pnlen; |
| 1327 | |
| 1328 | ret = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1329 | leave: |
| 1330 | if (cctx) |
| 1331 | EVP_CIPHER_CTX_free(cctx); |
| 1332 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1333 | return ret; |
| 1334 | } |
| 1335 | |
| 1336 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1337 | * address, with <payload_len> as payload length, <aad> as address of |
| 1338 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1339 | * context. |
| 1340 | * Returns 1 if succeeded, 0 if not. |
| 1341 | */ |
| 1342 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1343 | 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] | 1344 | 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] | 1345 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1346 | int ret = 0; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1347 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1348 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1349 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1350 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1351 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1352 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1353 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1354 | 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^] | 1355 | 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] | 1356 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | 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] | 1360 | 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^] | 1361 | 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] | 1362 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1365 | ret = 1; |
| 1366 | leave: |
| 1367 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1368 | return ret; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1369 | |
| 1370 | err: |
| 1371 | 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^] | 1372 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1376 | * Returns 1 if succeeded, 0 if not. |
| 1377 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1378 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel, |
| 1379 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1380 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1381 | int ret, kp_changed; |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 1382 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1383 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1384 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1385 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1386 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1387 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1388 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1389 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1390 | |
| 1391 | ret = 0; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1392 | kp_changed = 0; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1393 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1394 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1395 | /* The two tested bits are not at the same position, |
| 1396 | * this is why they are first both inversed. |
| 1397 | */ |
| 1398 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1399 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1400 | /* The lowest packet number of a previous key phase |
| 1401 | * cannot be null if it really stores previous key phase |
| 1402 | * secrets. |
| 1403 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1404 | // TODO: check if BUG_ON() more suitable |
| 1405 | if (!pkt->qc->ku.prv_rx.pn) { |
| 1406 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1407 | goto leave; |
| 1408 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1409 | |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1410 | rx_ctx = pkt->qc->ku.prv_rx.ctx; |
| 1411 | rx_iv = pkt->qc->ku.prv_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1412 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1413 | } |
| 1414 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1415 | /* Next key phase */ |
| 1416 | kp_changed = 1; |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1417 | rx_ctx = pkt->qc->ku.nxt_rx.ctx; |
| 1418 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1419 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1423 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1424 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1425 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1426 | goto leave; |
| 1427 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1428 | |
| 1429 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1430 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | 8c7927c | 2022-04-05 16:28:38 +0200 | [diff] [blame] | 1431 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1432 | if (!ret) { |
| 1433 | TRACE_ERROR("quic_tls_decrypt() 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 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1437 | /* Update the keys only if the packet decryption succeeded. */ |
| 1438 | if (kp_changed) { |
| 1439 | quic_tls_rotate_keys(pkt->qc); |
| 1440 | /* Toggle the Key Phase bit */ |
| 1441 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1442 | /* Store the lowest packet number received for the current key phase */ |
| 1443 | tls_ctx->rx.pn = pkt->pn; |
| 1444 | /* Prepare the next key update */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1445 | if (!quic_tls_key_update(pkt->qc)) { |
| 1446 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1447 | goto leave; |
| 1448 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1451 | /* Update the packet length (required to parse the frames). */ |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 1452 | pkt->len -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1453 | ret = 1; |
| 1454 | leave: |
| 1455 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1456 | return 1; |
| 1457 | } |
| 1458 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1459 | |
| 1460 | /* Remove references to <frm> frame */ |
| 1461 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1462 | { |
| 1463 | uint64_t pn; |
| 1464 | struct quic_frame *f, *tmp; |
| 1465 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1466 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1467 | |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1468 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1469 | pn = f->pkt->pn_node.key; |
| 1470 | f->origin = NULL; |
| 1471 | LIST_DELETE(&f->ref); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1472 | TRACE_DEVEL("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1473 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1474 | |
| 1475 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1478 | /* Release <frm> frame and mark its copies as acknowledged */ |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1479 | 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] | 1480 | { |
| 1481 | uint64_t pn; |
| 1482 | struct quic_frame *origin, *f, *tmp; |
| 1483 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1484 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1485 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1486 | /* Identify this frame: a frame copy or one of its copies */ |
| 1487 | origin = frm->origin ? frm->origin : frm; |
| 1488 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1489 | * possibly a copy of <origin> |
| 1490 | */ |
| 1491 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1492 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1493 | * not release the packets (releasing the frames) at this time as |
| 1494 | * they are possibly also to be acknowledged alongside the |
| 1495 | * the current one. |
| 1496 | */ |
| 1497 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1498 | pn = f->pkt->pn_node.key; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1499 | TRACE_DEVEL("mark frame as acked from packet", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1500 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1501 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1502 | f->origin = NULL; |
| 1503 | LIST_DELETE(&f->ref); |
| 1504 | } |
| 1505 | LIST_DELETE(&frm->list); |
| 1506 | pn = frm->pkt->pn_node.key; |
| 1507 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1508 | TRACE_DEVEL("freeing frame from packet", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1509 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1510 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1511 | |
| 1512 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 1513 | } |
| 1514 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1515 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1516 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1517 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1518 | * |
| 1519 | * TODO this should also be called on lost packet detection |
| 1520 | */ |
| 1521 | static void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1522 | { |
| 1523 | struct ssl_sock_ctx *ctx = qc->xprt_ctx; |
| 1524 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1525 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1526 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1527 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1528 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1529 | quic_set_connection_close(qc, qc->err); |
| 1530 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 1531 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1532 | |
| 1533 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1536 | /* Remove from <stream> the acknowledged frames. |
Amaury Denoyelle | 95e50fb | 2022-03-29 14:50:25 +0200 | [diff] [blame] | 1537 | * |
| 1538 | * 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] | 1539 | */ |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1540 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1541 | struct qc_stream_desc *stream) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1542 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1543 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1544 | struct eb64_node *frm_node; |
| 1545 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1546 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1547 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1548 | ret = 0; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1549 | frm_node = eb64_first(&stream->acked_frms); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1550 | while (frm_node) { |
| 1551 | struct quic_stream *strm; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1552 | struct quic_frame *frm; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1553 | size_t offset, len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1554 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1555 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1556 | offset = strm->offset.key; |
| 1557 | len = strm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1558 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1559 | if (offset > stream->ack_offset) |
| 1560 | break; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1561 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1562 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1563 | /* 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^] | 1564 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1565 | qc, stream ? strm : NULL, stream); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1566 | ret = 1; |
| 1567 | } |
| 1568 | |
Amaury Denoyelle | 7586bef | 2022-04-25 14:26:54 +0200 | [diff] [blame] | 1569 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1570 | * has been freed. with the stream frames tree. Nothing to do |
| 1571 | * anymore in here. |
| 1572 | */ |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1573 | if (!stream) { |
| 1574 | qc_check_close_on_released_mux(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1575 | ret = 1; |
| 1576 | goto leave; |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1577 | } |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1578 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1579 | frm_node = eb64_next(frm_node); |
| 1580 | eb64_delete(&strm->offset); |
Amaury Denoyelle | 7b4c9d6 | 2022-02-24 10:50:58 +0100 | [diff] [blame] | 1581 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1582 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1583 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1584 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1585 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1586 | leave: |
| 1587 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1588 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1591 | /* 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] | 1592 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1593 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1594 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1595 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1596 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1597 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1598 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1599 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1600 | switch (frm->type) { |
| 1601 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1602 | { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1603 | struct quic_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1604 | struct eb64_node *node = NULL; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1605 | struct qc_stream_desc *stream = NULL; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1606 | const size_t offset = strm_frm->offset.key; |
| 1607 | const size_t len = strm_frm->len; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1608 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1609 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1610 | * 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] | 1611 | * lookup. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1612 | * |
| 1613 | * TODO if lookup operation impact on the perf is noticeable, |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1614 | * implement a refcount on qc_stream_desc instances. |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1615 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1616 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1617 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1618 | 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] | 1619 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1620 | /* early return */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1621 | goto leave; |
Amaury Denoyelle | 8d5def0 | 2022-03-29 11:51:17 +0200 | [diff] [blame] | 1622 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1623 | 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] | 1624 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1625 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1626 | if (offset <= stream->ack_offset) { |
| 1627 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1628 | stream_acked = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1629 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1630 | qc, strm_frm, stream); |
| 1631 | } |
Amaury Denoyelle | 0c7679d | 2022-02-24 10:56:33 +0100 | [diff] [blame] | 1632 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1633 | if (!stream) { |
| 1634 | /* no need to continue if stream freed. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1635 | 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] | 1636 | qc_release_frm(qc, frm); |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 1637 | qc_check_close_on_released_mux(qc); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 1638 | break; |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1639 | } |
| 1640 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1641 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1642 | qc, strm_frm, stream); |
| 1643 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1644 | } |
| 1645 | else { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1646 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1647 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1648 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1649 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1650 | } |
| 1651 | break; |
| 1652 | default: |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 1653 | qc_release_frm(qc, frm); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1654 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1655 | |
Frédéric Lécaille | 89a2ceb | 2022-04-20 15:59:07 +0200 | [diff] [blame] | 1656 | if (stream_acked && qc->mux_state == QC_MUX_READY) { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1657 | struct qcc *qcc = qc->qcc; |
| 1658 | |
| 1659 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1660 | tasklet_wakeup(qcc->subs->tasklet); |
| 1661 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1662 | if (!qcc->subs->events) |
| 1663 | qcc->subs = NULL; |
| 1664 | } |
| 1665 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1666 | leave: |
| 1667 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1671 | * deallocating them, and their TX frames. |
| 1672 | * Returns the last node reached to be used for the next range. |
| 1673 | * May be NULL if <largest> node could not be found. |
| 1674 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1675 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1676 | struct eb_root *pkts, |
| 1677 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1678 | struct list *newly_acked_pkts, |
| 1679 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1680 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1681 | { |
| 1682 | struct eb64_node *node; |
| 1683 | struct quic_tx_packet *pkt; |
| 1684 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1685 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1686 | |
Frédéric Lécaille | 843399f | 2022-07-22 16:27:44 +0200 | [diff] [blame] | 1687 | 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] | 1688 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1689 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1690 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1691 | 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] | 1692 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1693 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1694 | 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] | 1695 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1696 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1697 | node = eb64_prev(node); |
| 1698 | eb64_delete(&pkt->pn_node); |
| 1699 | } |
| 1700 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1701 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1702 | return node; |
| 1703 | } |
| 1704 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1705 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1706 | * 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] | 1707 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1708 | 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] | 1709 | struct quic_tx_packet *pkt, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1710 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1711 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1712 | struct quic_frame *frm, *frmbak; |
| 1713 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1714 | struct list *pkt_frm_list = &pkt->frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1715 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1716 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1717 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1718 | 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] | 1719 | /* Only for debug */ |
| 1720 | uint64_t pn; |
| 1721 | |
| 1722 | /* 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] | 1723 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1724 | pn = frm->pkt->pn_node.key; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1725 | quic_tx_packet_refdec(frm->pkt); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1726 | /* 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] | 1727 | frm->pkt = NULL; |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1728 | /* Remove any reference to this frame */ |
| 1729 | qc_frm_unref(qc, frm); |
| 1730 | switch (frm->type) { |
| 1731 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1732 | { |
| 1733 | struct quic_stream *strm_frm = &frm->stream; |
| 1734 | struct eb64_node *node = NULL; |
| 1735 | struct qc_stream_desc *stream_desc; |
| 1736 | |
| 1737 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1738 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1739 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1740 | 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] | 1741 | qc, frm, &pn); |
| 1742 | pool_free(pool_head_quic_frame, frm); |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
| 1746 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1747 | /* Do not resend this frame if in the "already acked range" */ |
| 1748 | 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^] | 1749 | TRACE_DEVEL("ignored frame in already acked range", |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1750 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1751 | continue; |
| 1752 | } |
| 1753 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1754 | strm_frm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1755 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1756 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1757 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | |
| 1761 | default: |
| 1762 | break; |
| 1763 | } |
| 1764 | |
| 1765 | /* Do not resend probing packet with old data */ |
| 1766 | 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^] | 1767 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1768 | qc, frm, &pn); |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1769 | if (frm->origin) |
| 1770 | LIST_DELETE(&frm->ref); |
| 1771 | pool_free(pool_head_quic_frame, frm); |
| 1772 | continue; |
| 1773 | } |
| 1774 | |
| 1775 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1776 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1777 | 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] | 1778 | qc, frm, &pn); |
| 1779 | pool_free(pool_head_quic_frame, frm); |
| 1780 | } |
| 1781 | else { |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1782 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1783 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1784 | * will be performed to check the stream is not consumed or released. |
| 1785 | */ |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 1786 | frm->flags = QUIC_FL_TX_FRAME_LOST; |
| 1787 | } |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1788 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1789 | 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] | 1790 | } |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1791 | } |
| 1792 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1793 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1794 | |
| 1795 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1796 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1797 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1798 | /* Free <pkt> TX packet and its attached frames. |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1799 | * 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] | 1800 | * any data structure it was possibly attached to. |
| 1801 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1802 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1803 | struct quic_tx_packet *pkt) |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1804 | { |
| 1805 | struct quic_frame *frm, *frmbak; |
| 1806 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1807 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1808 | |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1809 | if (!pkt) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1810 | goto leave; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1811 | |
| 1812 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1813 | LIST_DELETE(&frm->list); |
| 1814 | pool_free(pool_head_quic_frame, frm); |
| 1815 | } |
| 1816 | pool_free(pool_head_quic_tx_packet, pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1817 | |
| 1818 | leave: |
| 1819 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1822 | /* Free the TX packets of <pkts> list */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1823 | 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] | 1824 | { |
| 1825 | struct quic_tx_packet *pkt, *tmp; |
| 1826 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1827 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1828 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1829 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1830 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1831 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1832 | free_quic_tx_packet(qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1833 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1834 | |
| 1835 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1838 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1839 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1840 | * updating the range which contains <largest_acked_pn>. |
| 1841 | * Never fails. |
| 1842 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1843 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1844 | struct quic_pktns *pktns, |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1845 | int64_t largest_acked_pn) |
| 1846 | { |
| 1847 | struct eb64_node *ar, *next_ar; |
| 1848 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1849 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1850 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1851 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1852 | ar = eb64_first(&arngs->root); |
| 1853 | while (ar) { |
| 1854 | struct quic_arng_node *ar_node; |
| 1855 | |
| 1856 | next_ar = eb64_next(ar); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 1857 | 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^] | 1858 | |
| 1859 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 1860 | 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] | 1861 | break; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1862 | } |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1863 | |
| 1864 | if (largest_acked_pn < ar_node->last) { |
| 1865 | eb64_delete(ar); |
| 1866 | ar_node->first.key = largest_acked_pn + 1; |
| 1867 | eb64_insert(&arngs->root, ar); |
| 1868 | break; |
| 1869 | } |
| 1870 | |
| 1871 | eb64_delete(ar); |
| 1872 | pool_free(pool_head_quic_arng, ar_node); |
| 1873 | arngs->sz--; |
| 1874 | ar = next_ar; |
| 1875 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1876 | |
| 1877 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1878 | } |
| 1879 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1880 | /* Send a packet ack event nofication for each newly acked packet of |
| 1881 | * <newly_acked_pkts> list and free them. |
| 1882 | * Always succeeds. |
| 1883 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1884 | 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] | 1885 | struct list *newly_acked_pkts) |
| 1886 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1887 | struct quic_tx_packet *pkt, *tmp; |
| 1888 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1889 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1890 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1891 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1892 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1893 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1894 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1895 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1896 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1897 | qc->path->ifae_pkts--; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 1898 | /* If this packet contained an ACK frame, proceed to the |
| 1899 | * acknowledging of range of acks from the largest acknowledged |
| 1900 | * packet number which was sent in an ACK frame by this packet. |
| 1901 | */ |
| 1902 | if (pkt->largest_acked_pn != -1) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1903 | 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] | 1904 | ev.ack.acked = pkt->in_flight_len; |
| 1905 | ev.ack.time_sent = pkt->time_sent; |
| 1906 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1907 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1908 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1909 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1912 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1913 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1916 | /* 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^] | 1917 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 1918 | struct quic_pktns *pktns) |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1919 | { |
| 1920 | struct quic_frame *frm, *frmbak; |
| 1921 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1922 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 1923 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1924 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1925 | LIST_DELETE(&frm->list); |
| 1926 | pool_free(pool_head_quic_frame, frm); |
| 1927 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1928 | |
| 1929 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1932 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1933 | * their TX frames. |
| 1934 | * Send a packet loss event to the congestion controller if |
| 1935 | * in flight packet have been lost. |
| 1936 | * Also frees the packet in <pkts> list. |
| 1937 | * Never fails. |
| 1938 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1939 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1940 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1941 | struct list *pkts, |
| 1942 | uint64_t now_us) |
| 1943 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1944 | 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] | 1945 | uint64_t lost_bytes; |
| 1946 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 1947 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1948 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1949 | lost_bytes = 0; |
| 1950 | oldest_lost = newest_lost = NULL; |
| 1951 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1952 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1953 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1954 | lost_bytes += pkt->in_flight_len; |
| 1955 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1956 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | ba9db40 | 2022-03-01 17:06:50 +0100 | [diff] [blame] | 1957 | qc->path->in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1958 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1959 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1960 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 9636715 | 2022-04-25 09:40:19 +0200 | [diff] [blame] | 1961 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1962 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1963 | if (!oldest_lost) { |
| 1964 | oldest_lost = newest_lost = pkt; |
| 1965 | } |
| 1966 | else { |
| 1967 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1968 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1969 | newest_lost = pkt; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1973 | if (newest_lost) { |
| 1974 | /* Sent a congestion event to the controller */ |
Benoit DOLEZ | 69e3f05 | 2022-06-08 09:28:56 +0200 | [diff] [blame] | 1975 | struct quic_cc_event ev = { }; |
| 1976 | |
| 1977 | ev.type = QUIC_CC_EVT_LOSS; |
| 1978 | ev.loss.time_sent = newest_lost->time_sent; |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 1979 | |
| 1980 | quic_cc_event(&qc->path->cc, &ev); |
| 1981 | } |
| 1982 | |
| 1983 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 1984 | * We must check if we are experiencing a persistent congestion. |
| 1985 | * If this is the case, the congestion controller must re-enter |
| 1986 | * slow start state. |
| 1987 | */ |
| 1988 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 1989 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 1990 | |
| 1991 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 1992 | now_ms, qc->max_ack_delay)) |
| 1993 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 1994 | } |
| 1995 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1996 | if (lost_bytes) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1997 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1998 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1999 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2000 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2001 | |
| 2002 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2003 | } |
| 2004 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2005 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2006 | * 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] | 2007 | * 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] | 2008 | * acked ack-eliciting packet. |
| 2009 | * Return 1, if succeeded, 0 if not. |
| 2010 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2011 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2012 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2013 | struct quic_enc_level *qel, |
| 2014 | unsigned int *rtt_sample, |
| 2015 | const unsigned char **pos, const unsigned char *end) |
| 2016 | { |
| 2017 | struct quic_ack *ack = &frm->ack; |
| 2018 | uint64_t smallest, largest; |
| 2019 | struct eb_root *pkts; |
| 2020 | struct eb64_node *largest_node; |
| 2021 | unsigned int time_sent, pkt_flags; |
| 2022 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2023 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2024 | int ret = 0; |
| 2025 | |
| 2026 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2027 | |
| 2028 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2029 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2030 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2031 | goto err; |
| 2032 | } |
| 2033 | |
| 2034 | if (ack->first_ack_range > ack->largest_ack) { |
| 2035 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2036 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2037 | goto err; |
| 2038 | } |
| 2039 | |
| 2040 | largest = ack->largest_ack; |
| 2041 | smallest = largest - ack->first_ack_range; |
| 2042 | pkts = &qel->pktns->tx.pkts; |
| 2043 | pkt_flags = 0; |
| 2044 | largest_node = NULL; |
| 2045 | time_sent = 0; |
| 2046 | |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2047 | 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] | 2048 | largest_node = eb64_lookup(pkts, largest); |
| 2049 | if (!largest_node) { |
| 2050 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2051 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 2052 | } |
| 2053 | else { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2054 | time_sent = eb64_entry(largest_node, |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 2055 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2056 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2059 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2060 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2061 | do { |
| 2062 | uint64_t gap, ack_range; |
| 2063 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2064 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2065 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2066 | if (!ack->ack_range_num--) |
| 2067 | break; |
| 2068 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2069 | if (!quic_dec_int(&gap, pos, end)) { |
| 2070 | 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] | 2071 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2072 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2073 | |
| 2074 | if (smallest < gap + 2) { |
| 2075 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2076 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2077 | goto err; |
| 2078 | } |
| 2079 | |
| 2080 | largest = smallest - gap - 2; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2081 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2082 | 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] | 2083 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2084 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2085 | |
| 2086 | if (largest < ack_range) { |
| 2087 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2088 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2089 | goto err; |
| 2090 | } |
| 2091 | |
| 2092 | /* Do not use this node anymore. */ |
| 2093 | largest_node = NULL; |
| 2094 | /* Next range */ |
| 2095 | smallest = largest - ack_range; |
| 2096 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2097 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2098 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2099 | } while (1); |
| 2100 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2101 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2102 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 2103 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2107 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2108 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2109 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2110 | 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] | 2111 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2112 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2113 | if (quic_peer_validated_addr(qc)) |
| 2114 | qc->path->loss.pto_count = 0; |
| 2115 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2118 | ret = 1; |
| 2119 | leave: |
| 2120 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2121 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2122 | |
| 2123 | err: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2124 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2125 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2128 | /* This function gives the detail of the SSL error. It is used only |
| 2129 | * if the debug mode and the verbose mode are activated. It dump all |
| 2130 | * the SSL error until the stack was empty. |
| 2131 | */ |
| 2132 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2133 | { |
| 2134 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2135 | while (1) { |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2136 | const char *func = NULL; |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2137 | unsigned long ret; |
| 2138 | |
Willy Tarreau | 325fc63 | 2022-04-11 18:47:38 +0200 | [diff] [blame] | 2139 | ERR_peek_error_func(&func); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2140 | ret = ERR_get_error(); |
| 2141 | if (!ret) |
| 2142 | return; |
| 2143 | |
| 2144 | 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] | 2145 | func, ERR_reason_error_string(ret)); |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 2150 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2151 | const char **str, int *len); |
| 2152 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2153 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2154 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2155 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2156 | * Return 1 if succeeded, 0 if not. |
| 2157 | */ |
| 2158 | 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] | 2159 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2160 | const unsigned char *data, size_t len, |
| 2161 | struct quic_rx_packet *pkt, |
| 2162 | struct quic_rx_crypto_frm *cf) |
| 2163 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2164 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2165 | struct quic_conn *qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2166 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2167 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2168 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2169 | qc = ctx->qc; |
| 2170 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2171 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2172 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2173 | 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^] | 2174 | TRACE_ERROR("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2175 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2176 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | el->rx.crypto.offset += len; |
| 2180 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 2181 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2182 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2183 | state = qc->state; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2184 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2185 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2186 | if (ssl_err != 1) { |
| 2187 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2188 | 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^] | 2189 | TRACE_PROTO("SSL handshake in progress", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2190 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2191 | goto out; |
| 2192 | } |
| 2193 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2194 | /* TODO: Should close the connection asap */ |
| 2195 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2196 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2197 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2198 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2199 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2200 | 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] | 2201 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2202 | ERR_clear_error(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2203 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2206 | 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] | 2207 | |
| 2208 | /* Check the alpn could be negotiated */ |
| 2209 | if (!qc->app_ops) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2210 | 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] | 2211 | 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^] | 2212 | goto leave; |
Frédéric Lécaille | bc964bd | 2022-04-13 16:20:09 +0200 | [diff] [blame] | 2213 | } |
| 2214 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2215 | 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^] | 2216 | 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] | 2217 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2218 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2219 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2220 | /* I/O callback switch */ |
| 2221 | ctx->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2222 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2223 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2224 | /* The connection is ready to be accepted. */ |
| 2225 | quic_accept_push_qc(qc); |
| 2226 | } |
| 2227 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2228 | qc->state = QUIC_HS_ST_COMPLETE; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 2229 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2230 | } else { |
| 2231 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2232 | if (ssl_err != 1) { |
| 2233 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2234 | 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^] | 2235 | TRACE_PROTO("SSL post handshake in progress", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2236 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2237 | goto out; |
| 2238 | } |
| 2239 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2240 | TRACE_ERROR("SSL post handshake error", |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 2241 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2242 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2245 | 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] | 2246 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 2247 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2248 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2249 | ret = 1; |
| 2250 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2251 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2252 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2255 | /* Parse a STREAM frame <strm_frm> |
| 2256 | * |
| 2257 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2258 | * containing the frame must not be acknowledged. |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2259 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2260 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2261 | struct quic_stream *strm_frm, |
| 2262 | struct quic_conn *qc) |
| 2263 | { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2264 | int ret; |
| 2265 | |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2266 | /* RFC9000 13.1. Packet Processing |
| 2267 | * |
| 2268 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2269 | * successfully removed and all frames contained in the packet have |
| 2270 | * been processed. For STREAM frames, this means the data has been |
| 2271 | * enqueued in preparation to be received by the application protocol, |
| 2272 | * but it does not require that data be delivered and consumed. |
| 2273 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2274 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2275 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2276 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2277 | strm_frm->offset.key, strm_frm->fin, |
| 2278 | (char *)strm_frm->data); |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 2279 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 2280 | /* frame rejected - packet must not be acknowledeged */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2281 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2282 | return !ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2285 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2286 | * for <qc> QUIC connection. |
| 2287 | * This is a best effort function which never fails even if no memory could be |
| 2288 | * allocated to duplicate these frames. |
| 2289 | */ |
| 2290 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2291 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2292 | { |
| 2293 | struct quic_frame *frm, *frmbak; |
| 2294 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2295 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2296 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2297 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2298 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2299 | struct quic_frame *dup_frm, *origin; |
| 2300 | |
| 2301 | switch (frm->type) { |
| 2302 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2303 | { |
| 2304 | struct quic_stream *strm_frm = &frm->stream; |
| 2305 | struct eb64_node *node = NULL; |
| 2306 | struct qc_stream_desc *stream_desc; |
| 2307 | |
| 2308 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2309 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2310 | 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] | 2311 | continue; |
| 2312 | } |
| 2313 | |
| 2314 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2315 | /* Do not resend this frame if in the "already acked range" */ |
| 2316 | 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^] | 2317 | TRACE_DEVEL("ignored frame in already acked range", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2318 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2319 | continue; |
| 2320 | } |
| 2321 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2322 | strm_frm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2323 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2324 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2325 | } |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2326 | break; |
| 2327 | } |
| 2328 | |
| 2329 | default: |
| 2330 | break; |
| 2331 | } |
| 2332 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2333 | // FIXME: zalloc |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2334 | dup_frm = pool_zalloc(pool_head_quic_frame); |
| 2335 | if (!dup_frm) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2336 | 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] | 2337 | break; |
| 2338 | } |
| 2339 | |
| 2340 | /* If <frm> is already a copy of another frame, we must take |
| 2341 | * its original frame as source for the copy. |
| 2342 | */ |
| 2343 | origin = frm->origin ? frm->origin : frm; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2344 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2345 | *dup_frm = *origin; |
| 2346 | LIST_INIT(&dup_frm->reflist); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2347 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2348 | qc, NULL, &origin->pkt->pn_node.key); |
| 2349 | dup_frm->origin = origin; |
| 2350 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2351 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2352 | } |
| 2353 | |
| 2354 | LIST_SPLICE(out_frm_list, &tmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2355 | |
| 2356 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2359 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2360 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2361 | struct quic_enc_level *qel, |
| 2362 | struct list *frms1, struct list *frms2) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2363 | { |
| 2364 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2365 | struct list *frms = frms1; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2366 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2367 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2368 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2369 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2370 | |
| 2371 | BUG_ON(frms1 == frms2); |
| 2372 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2373 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2374 | node = eb64_first(pkts); |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2375 | start: |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2376 | while (node) { |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2377 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2378 | node = eb64_next(node); |
| 2379 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 2380 | 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] | 2381 | break; |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | if (!pkt) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2385 | goto leave; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2386 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2387 | /* When building a packet from another one, the field which may increase the |
| 2388 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2389 | */ |
| 2390 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2391 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2392 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2393 | goto leave; |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2396 | 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] | 2397 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2398 | if (frms == frms1 && frms2) { |
| 2399 | frms = frms2; |
| 2400 | goto start; |
| 2401 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2402 | leave: |
| 2403 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2404 | } |
| 2405 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2406 | /* 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] | 2407 | * 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] | 2408 | * Initial packets send them coalescing with others (Handshake here). |
| 2409 | * (Listener only function). |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2410 | */ |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2411 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2412 | struct list *ifrms, struct list *hfrms) |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2413 | { |
| 2414 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2415 | struct list htmp = LIST_HEAD_INIT(htmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2416 | |
| 2417 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2418 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2419 | struct quic_enc_level *qel = iqel; |
| 2420 | struct eb_root *pkts; |
| 2421 | struct eb64_node *node; |
| 2422 | struct quic_tx_packet *pkt; |
| 2423 | struct list *tmp = &itmp; |
| 2424 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2425 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2426 | start: |
| 2427 | pkt = NULL; |
| 2428 | pkts = &qel->pktns->tx.pkts; |
| 2429 | node = eb64_first(pkts); |
| 2430 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2431 | while (node) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 2432 | 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] | 2433 | 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] | 2434 | break; |
| 2435 | node = eb64_next(node); |
| 2436 | } |
| 2437 | |
| 2438 | if (!pkt) |
| 2439 | goto end; |
| 2440 | |
Frédéric Lécaille | 12fd259 | 2022-03-31 08:42:06 +0200 | [diff] [blame] | 2441 | /* When building a packet from another one, the field which may increase the |
| 2442 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2443 | */ |
| 2444 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2445 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2446 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2447 | goto end; |
| 2448 | } |
| 2449 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2450 | qel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2451 | |
| 2452 | /* 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] | 2453 | requeue: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2454 | 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] | 2455 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2456 | if (qel == iqel) { |
| 2457 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2458 | pkt = pkt->next; |
| 2459 | tmp = &htmp; |
| 2460 | hqel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2461 | 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] | 2462 | goto requeue; |
| 2463 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2464 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2465 | |
| 2466 | end: |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2467 | LIST_SPLICE(ifrms, &itmp); |
| 2468 | LIST_SPLICE(hfrms, &htmp); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2469 | |
| 2470 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2471 | } |
| 2472 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2473 | 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] | 2474 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2475 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2476 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2477 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2478 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2479 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2480 | 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^] | 2481 | goto out; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2482 | |
| 2483 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2484 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2485 | |
| 2486 | out: |
| 2487 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | 3ccea6d | 2022-05-23 22:54:54 +0200 | [diff] [blame] | 2488 | } |
| 2489 | |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2490 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2491 | * frame list to send. |
| 2492 | * Return 1 if succeeded, 0 if not. |
| 2493 | */ |
| 2494 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2495 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2496 | int ret = 0; |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2497 | struct quic_frame *frm; |
| 2498 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2499 | uint64_t app_error_code; |
| 2500 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2501 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2502 | |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2503 | /* TODO: the mux may be released, we cannot have more |
| 2504 | * information about the application error code to send |
| 2505 | * at this time. |
| 2506 | */ |
| 2507 | app_error_code = H3_REQUEST_REJECTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2508 | // fixme: zalloc |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2509 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2510 | if (!frm) { |
| 2511 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2512 | goto out; |
| 2513 | } |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2514 | |
| 2515 | frm->type = QUIC_FT_STOP_SENDING; |
| 2516 | frm->stop_sending.id = id; |
| 2517 | frm->stop_sending.app_error_code = app_error_code; |
| 2518 | LIST_INIT(&frm->reflist); |
| 2519 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2520 | ret = 1; |
| 2521 | out: |
| 2522 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2523 | return ret; |
Frédéric Lécaille | 4df2fe9 | 2022-05-29 11:36:03 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2526 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2527 | * as I/O handler context and <qel> as encryption level. |
| 2528 | * Returns 1 if succeeded, 0 if failed. |
| 2529 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2530 | 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] | 2531 | struct quic_enc_level *qel) |
| 2532 | { |
| 2533 | struct quic_frame frm; |
| 2534 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2535 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2536 | int fast_retrans = 0, ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2537 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2538 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2539 | /* Skip the AAD */ |
| 2540 | pos = pkt->data + pkt->aad_len; |
| 2541 | end = pkt->data + pkt->len; |
| 2542 | |
| 2543 | while (pos < end) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2544 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2545 | // trace already emitted by function above |
| 2546 | goto leave; |
| 2547 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2548 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2549 | 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] | 2550 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2551 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2552 | break; |
| 2553 | case QUIC_FT_PING: |
| 2554 | break; |
| 2555 | case QUIC_FT_ACK: |
| 2556 | { |
| 2557 | unsigned int rtt_sample; |
| 2558 | |
| 2559 | rtt_sample = 0; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2560 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2561 | // trace already emitted by function above |
| 2562 | goto leave; |
| 2563 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2564 | |
| 2565 | if (rtt_sample) { |
| 2566 | unsigned int ack_delay; |
| 2567 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2568 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2569 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2570 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2571 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2572 | 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] | 2573 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2574 | break; |
| 2575 | } |
Frédéric Lécaille | e06ca65 | 2022-05-29 11:48:58 +0200 | [diff] [blame] | 2576 | case QUIC_FT_RESET_STREAM: |
| 2577 | /* TODO: handle this frame at STREAM level */ |
| 2578 | break; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2579 | case QUIC_FT_STOP_SENDING: |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2580 | { |
| 2581 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2582 | if (qc->mux_state == QC_MUX_READY) { |
| 2583 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2584 | stop_sending->app_error_code)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2585 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2586 | goto leave; |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2587 | } |
| 2588 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2589 | break; |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 2590 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2591 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2592 | { |
| 2593 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2594 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 2595 | 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] | 2596 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2597 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2598 | |
| 2599 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2600 | cfdebug.len = frm.crypto.len; |
| 2601 | TRACE_PROTO("CRYPTO data discarded", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2602 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2603 | break; |
| 2604 | } |
| 2605 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2606 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2607 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2608 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2609 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2610 | |
| 2611 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2612 | cfdebug.len = frm.crypto.len; |
| 2613 | /* Nothing to do */ |
| 2614 | TRACE_PROTO("Already received CRYPTO data", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2615 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2616 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2617 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2618 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2619 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2620 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2621 | else { |
| 2622 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2623 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2624 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2625 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2626 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2627 | cfdebug.len = frm.crypto.len; |
| 2628 | TRACE_PROTO("Partially already received CRYPTO data", |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2629 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2630 | frm.crypto.len -= diff; |
| 2631 | frm.crypto.data += diff; |
| 2632 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2633 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2634 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2635 | |
| 2636 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2637 | /* 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] | 2638 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2639 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2640 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2641 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2642 | if (!qc_provide_cdata(qel, ctx, |
| 2643 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2644 | pkt, &cfdebug)) { |
| 2645 | // trace already emitted by function above |
| 2646 | goto leave; |
| 2647 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2648 | |
| 2649 | break; |
| 2650 | } |
| 2651 | |
| 2652 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2653 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2654 | if (!cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2655 | TRACE_ERROR("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2656 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2657 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2658 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2659 | |
| 2660 | cf->offset_node.key = frm.crypto.offset; |
| 2661 | cf->len = frm.crypto.len; |
| 2662 | cf->data = frm.crypto.data; |
| 2663 | cf->pkt = pkt; |
| 2664 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2665 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2666 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2667 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2668 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2669 | { |
| 2670 | struct quic_stream *stream = &frm.stream; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2671 | 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] | 2672 | |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2673 | /* The upper layer may not be allocated. */ |
| 2674 | if (qc->mux_state != QC_MUX_READY) { |
| 2675 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2676 | 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] | 2677 | break; |
| 2678 | } |
| 2679 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2680 | 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] | 2681 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2682 | TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2683 | /* This packet will not be acknowledged */ |
| 2684 | goto leave; |
Frédéric Lécaille | d62240c | 2022-05-02 18:52:58 +0200 | [diff] [blame] | 2685 | } |
| 2686 | } |
Frédéric Lécaille | 12aa26b | 2022-03-21 11:37:13 +0100 | [diff] [blame] | 2687 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2688 | if (!qc_handle_strm_frm(pkt, stream, qc)) { |
| 2689 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2690 | goto leave; |
| 2691 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2692 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2693 | break; |
| 2694 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2695 | case QUIC_FT_MAX_DATA: |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 2696 | if (qc->mux_state == QC_MUX_READY) { |
| 2697 | struct quic_max_data *data = &frm.max_data; |
| 2698 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2699 | } |
| 2700 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2701 | case QUIC_FT_MAX_STREAM_DATA: |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2702 | if (qc->mux_state == QC_MUX_READY) { |
| 2703 | struct quic_max_stream_data *data = &frm.max_stream_data; |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 2704 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2705 | data->max_stream_data)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2706 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2707 | goto leave; |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 2708 | } |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 2709 | } |
| 2710 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2711 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2712 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2713 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2714 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2715 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2716 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2717 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2718 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2719 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2720 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2721 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2722 | break; |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2723 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 2724 | 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] | 2725 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2726 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2727 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2728 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2729 | break; |
| 2730 | case QUIC_FT_CONNECTION_CLOSE: |
| 2731 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2732 | /* Increment the error counters */ |
| 2733 | qc_cc_err_count_inc(qc, &frm); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2734 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2735 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2736 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 2737 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 2738 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2739 | 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] | 2740 | /* RFC 9000 10.2. Immediate Close: |
| 2741 | * The closing and draining connection states exist to ensure |
| 2742 | * that connections close cleanly and that delayed or reordered |
| 2743 | * packets are properly discarded. These states SHOULD persist |
| 2744 | * for at least three times the current PTO interval... |
| 2745 | * |
| 2746 | * Rearm the idle timeout only one time when entering draining |
| 2747 | * state. |
| 2748 | */ |
| 2749 | qc_idle_timer_do_rearm(qc); |
| 2750 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2751 | qc_notify_close(qc); |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 2752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2753 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2754 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2755 | if (qc_is_listener(ctx->qc)) { |
| 2756 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 2757 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2758 | goto leave; |
| 2759 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2760 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2761 | qc->state = QUIC_HS_ST_CONFIRMED; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2762 | break; |
| 2763 | default: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2764 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 2765 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2769 | /* 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] | 2770 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
Frédéric Lécaille | 44ae752 | 2022-03-21 12:18:00 +0100 | [diff] [blame] | 2771 | |
Frédéric Lécaille | e248e37 | 2022-04-25 09:25:56 +0200 | [diff] [blame] | 2772 | if (fast_retrans) { |
| 2773 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2774 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2775 | |
| 2776 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2777 | } |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2778 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2779 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2780 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2781 | * be discarded. |
| 2782 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2783 | 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] | 2784 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
Frédéric Lécaille | 05bd92b | 2022-03-29 19:09:46 +0200 | [diff] [blame] | 2785 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2786 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2787 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2788 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2789 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2790 | qc_set_timer(ctx->qc); |
| 2791 | 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^] | 2792 | 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] | 2793 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2794 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2795 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2796 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2799 | ret = 1; |
| 2800 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2801 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2802 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2803 | } |
| 2804 | |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 2805 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 2806 | * |
| 2807 | * Returns allocated buffer or NULL on error. |
| 2808 | */ |
| 2809 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 2810 | { |
| 2811 | struct buffer *buf = &qc->tx.buf; |
| 2812 | if (!b_alloc(buf)) |
| 2813 | return NULL; |
| 2814 | |
| 2815 | return buf; |
| 2816 | } |
| 2817 | |
| 2818 | /* Free Tx buffer from <qc> if it is empty. */ |
| 2819 | static void qc_txb_release(struct quic_conn *qc) |
| 2820 | { |
| 2821 | struct buffer *buf = &qc->tx.buf; |
| 2822 | |
| 2823 | /* For the moment sending function is responsible to purge the buffer |
| 2824 | * entirely. It may change in the future but this requires to be able |
| 2825 | * to reuse old data. |
| 2826 | */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2827 | BUG_ON_HOT(buf && b_data(buf)); |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 2828 | |
| 2829 | if (!b_data(buf)) { |
| 2830 | b_free(buf); |
| 2831 | offer_buffers(NULL, 1); |
| 2832 | } |
| 2833 | } |
| 2834 | |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2835 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 2836 | * must contains the address of the first packet stored in the payload. |
| 2837 | * |
| 2838 | * Caller is responsible that there is enough space in the buffer. |
| 2839 | */ |
| 2840 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 2841 | struct quic_tx_packet *first_pkt) |
| 2842 | { |
| 2843 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 2844 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 2845 | |
| 2846 | write_u16(b_tail(buf), length); |
| 2847 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 2848 | b_add(buf, hdlen + length); |
| 2849 | } |
| 2850 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2851 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 2852 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 2853 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 2854 | * <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] | 2855 | * <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] | 2856 | */ |
| 2857 | 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] | 2858 | 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] | 2859 | { |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2860 | unsigned int must_ack = force_ack || |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2861 | (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] | 2862 | |
| 2863 | /* Do not build any more packet if the TX secrets are not available or |
| 2864 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 2865 | * and if there is no more packets to send upon PTO expiration |
| 2866 | * and if there is no more ack-eliciting frames to send or in flight |
| 2867 | * congestion control limit is reached for prepared data |
| 2868 | */ |
| 2869 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 2870 | (!cc && !probe && !must_ack && |
| 2871 | (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] | 2872 | return 0; |
| 2873 | } |
| 2874 | |
| 2875 | return 1; |
| 2876 | } |
| 2877 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2878 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 2879 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 2880 | * |
| 2881 | * Each datagram is prepended by a two fields header : the datagram length and |
| 2882 | * the address of the packet contained in the datagram. |
| 2883 | * |
| 2884 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 2885 | * -1 if something wrong happened. |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2886 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2887 | 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] | 2888 | struct list *frms) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2889 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2890 | int ret = -1; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2891 | struct quic_enc_level *qel; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2892 | unsigned char *end, *pos; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2893 | struct quic_tx_packet *pkt; |
| 2894 | size_t total; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2895 | /* Each datagram is prepended with its length followed by the address |
| 2896 | * of the first packet in the datagram. |
| 2897 | */ |
| 2898 | 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] | 2899 | |
| 2900 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2901 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2902 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2903 | total = 0; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2904 | pos = (unsigned char *)b_tail(buf); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2905 | 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] | 2906 | int err, probe, cc; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2907 | |
| 2908 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2909 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 2910 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2911 | /* We do not probe if an immediate close was asked */ |
| 2912 | if (!cc) |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2913 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 2914 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 2915 | 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] | 2916 | break; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2917 | |
| 2918 | /* Leave room for the datagram header */ |
| 2919 | pos += dg_headlen; |
| 2920 | 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] | 2921 | 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] | 2922 | } |
| 2923 | else { |
| 2924 | end = pos + qc->path->mtu; |
| 2925 | } |
| 2926 | |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 2927 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 2928 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2929 | switch (err) { |
| 2930 | case -2: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2931 | // trace already emitted by function above |
| 2932 | goto leave; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2933 | case -1: |
Frédéric Lécaille | e9a974a | 2022-03-13 19:19:12 +0100 | [diff] [blame] | 2934 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 2935 | * MTU, we are here because of the congestion control window. There is |
| 2936 | * no need to try to reuse this buffer. |
| 2937 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2938 | 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] | 2939 | goto out; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2940 | default: |
| 2941 | break; |
| 2942 | } |
| 2943 | |
| 2944 | /* 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^] | 2945 | BUG_ON(!pkt); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2946 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 2947 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 2948 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 2949 | |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2950 | total += pkt->len; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2951 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2952 | /* Write datagram header. */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2953 | qc_txb_store(buf, pkt->len, pkt); |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2957 | ret = total; |
| 2958 | leave: |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2959 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2960 | return ret; |
Frédéric Lécaille | 1c5968b | 2022-02-25 17:15:21 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2963 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 2964 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 2965 | * written into <buf>. |
| 2966 | * |
| 2967 | * Each datagram is prepended by a two fields header : the datagram length and |
| 2968 | * the address of first packet in the datagram. |
| 2969 | * |
| 2970 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 2971 | * -1 if something wrong happened. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2972 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2973 | 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] | 2974 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 2975 | 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] | 2976 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2977 | struct quic_enc_level *qel; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 2978 | unsigned char *end, *pos; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2979 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2980 | /* length of datagrams */ |
| 2981 | uint16_t dglen; |
| 2982 | size_t total; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 2983 | int ret = -1, padding; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2984 | /* Each datagram is prepended with its length followed by the address |
| 2985 | * of the first packet in the datagram. |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2986 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2987 | 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] | 2988 | struct list *frms; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2989 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2990 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2991 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 2992 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
| 2993 | * caller must ensure that buf is resetted. |
| 2994 | */ |
| 2995 | BUG_ON_HOT(buf->head || buf->data); |
| 2996 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2997 | total = 0; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 2998 | qel = &qc->els[tel]; |
| 2999 | frms = tel_frms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3000 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3001 | padding = 0; |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3002 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3003 | first_pkt = prv_pkt = NULL; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3004 | 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] | 3005 | int err, probe, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3006 | enum quic_pkt_type pkt_type; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 3007 | struct quic_tls_ctx *tls_ctx; |
| 3008 | const struct quic_version *ver; |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 3009 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3010 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3011 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3012 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3013 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3014 | probe = 0; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3015 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3016 | /* We do not probe if an immediate close was asked */ |
| 3017 | if (!cc) |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 3018 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3019 | |
Frédéric Lécaille | 57bddbc | 2022-06-20 17:51:24 +0200 | [diff] [blame] | 3020 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3021 | if (prv_pkt) |
| 3022 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3023 | /* Let's select the next encryption level */ |
| 3024 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3025 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3026 | frms = next_tel_frms; |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 3027 | qel = &qc->els[tel]; |
| 3028 | /* Build a new datagram */ |
| 3029 | prv_pkt = NULL; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3030 | 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] | 3031 | continue; |
| 3032 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3033 | break; |
| 3034 | } |
| 3035 | |
| 3036 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3037 | if (!prv_pkt) { |
| 3038 | /* Leave room for the datagram header */ |
| 3039 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3040 | 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] | 3041 | 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] | 3042 | } |
| 3043 | else { |
| 3044 | end = pos + qc->path->mtu; |
| 3045 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3046 | } |
| 3047 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 3048 | if (qc->negotiated_version) { |
| 3049 | ver = qc->negotiated_version; |
| 3050 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3051 | tls_ctx = &qc->negotiated_ictx; |
| 3052 | else |
| 3053 | tls_ctx = &qel->tls_ctx; |
| 3054 | } |
| 3055 | else { |
| 3056 | ver = qc->original_version; |
| 3057 | tls_ctx = &qel->tls_ctx; |
| 3058 | } |
| 3059 | |
| 3060 | 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] | 3061 | qc, ver, dglen, pkt_type, |
| 3062 | force_ack, padding, probe, cc, &err); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3063 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3064 | case -2: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3065 | // trace already emitted by function above |
| 3066 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3067 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3068 | /* If there was already a correct packet present, set the |
| 3069 | * current datagram as prepared into <cbuf>. |
| 3070 | */ |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3071 | if (prv_pkt) |
| 3072 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3073 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3074 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3075 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3076 | break; |
| 3077 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 3078 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3079 | /* 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^] | 3080 | BUG_ON(!cur_pkt); |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3081 | |
Frédéric Lécaille | 1809c33 | 2022-04-25 10:24:12 +0200 | [diff] [blame] | 3082 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3083 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3084 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3085 | total += cur_pkt->len; |
| 3086 | /* keep trace of the first packet in the datagram */ |
| 3087 | if (!first_pkt) |
| 3088 | first_pkt = cur_pkt; |
| 3089 | /* Attach the current one to the previous one */ |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3090 | if (prv_pkt) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3091 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | b44cbc6 | 2022-04-21 17:58:46 +0200 | [diff] [blame] | 3092 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3093 | } |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3094 | /* Let's say we have to build a new dgram */ |
| 3095 | prv_pkt = NULL; |
| 3096 | dglen += cur_pkt->len; |
| 3097 | /* Client: discard the Initial encryption keys as soon as |
| 3098 | * a handshake packet could be built. |
| 3099 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3100 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3101 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3102 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3103 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3104 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3105 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 3106 | 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^] | 3107 | 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] | 3108 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3109 | } |
| 3110 | /* If the data for the current encryption level have all been sent, |
| 3111 | * select the next level. |
| 3112 | */ |
| 3113 | 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] | 3114 | 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] | 3115 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3116 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3117 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3118 | tel = next_tel; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3119 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3120 | frms = &qc->els[tel].pktns->tx.frms; |
| 3121 | else |
| 3122 | frms = next_tel_frms; |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3123 | qel = &qc->els[tel]; |
Frédéric Lécaille | fc88844 | 2022-04-11 15:39:34 +0200 | [diff] [blame] | 3124 | if (!LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 3125 | /* If there is data for the next level, do not |
| 3126 | * consume a datagram. |
| 3127 | */ |
| 3128 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3129 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3130 | } |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3131 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3132 | /* If we have to build a new datagram, set the current datagram as |
| 3133 | * prepared into <cbuf>. |
| 3134 | */ |
| 3135 | if (!prv_pkt) { |
Amaury Denoyelle | 654269c | 2022-08-08 15:38:57 +0200 | [diff] [blame] | 3136 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3137 | first_pkt = NULL; |
| 3138 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3139 | padding = 0; |
| 3140 | } |
| 3141 | 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] | 3142 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 3143 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3144 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3148 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3149 | ret = total; |
| 3150 | leave: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3151 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3152 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3153 | } |
| 3154 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3155 | /* Send datagrams stored in <buf>. |
| 3156 | * |
| 3157 | * This function always returns 1 for success. Even if sendto() syscall failed, |
| 3158 | * buffer is drained and packets are considered as emitted. QUIC loss detection |
| 3159 | * 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] | 3160 | */ |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3161 | 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] | 3162 | { |
| 3163 | struct quic_conn *qc; |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3164 | char skip_sendto = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3165 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3166 | qc = ctx->qc; |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3167 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3168 | while (b_contig_data(buf, 0)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3169 | unsigned char *pos; |
| 3170 | struct buffer tmpbuf = { }; |
| 3171 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3172 | uint16_t dglen; |
| 3173 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3174 | unsigned int time_sent; |
| 3175 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3176 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3177 | dglen = read_u16(pos); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3178 | BUG_ON_HOT(!dglen); /* this should not happen */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3179 | |
| 3180 | pos += sizeof dglen; |
| 3181 | first_pkt = read_ptr(pos); |
| 3182 | pos += sizeof first_pkt; |
| 3183 | tmpbuf.area = (char *)pos; |
| 3184 | tmpbuf.size = tmpbuf.data = dglen; |
| 3185 | |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3186 | /* If sendto is on error just skip the call to it for the rest |
| 3187 | * of the loop but continue to purge the buffer. Data will be |
| 3188 | * transmitted when QUIC packets are detected as lost on our |
| 3189 | * side. |
| 3190 | * |
| 3191 | * TODO use fd-monitoring to detect when send operation can be |
| 3192 | * retry. This should improve the bandwidth without relying on |
| 3193 | * retransmission timer. However, it requires a major rework on |
| 3194 | * quic-conn fd management. |
| 3195 | */ |
Amaury Denoyelle | 906b058 | 2022-08-05 15:22:28 +0200 | [diff] [blame] | 3196 | if (!skip_sendto) { |
| 3197 | if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) { |
| 3198 | skip_sendto = 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3199 | 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] | 3200 | } |
| 3201 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3202 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3203 | b_del(buf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3204 | qc->tx.bytes += tmpbuf.data; |
| 3205 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3206 | |
| 3207 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3208 | pkt->time_sent = time_sent; |
| 3209 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3210 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3211 | qc->path->ifae_pkts++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3212 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3213 | qc_idle_timer_rearm(qc, 0); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3214 | } |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3215 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3216 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3217 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 3218 | qc_notify_close(qc); |
| 3219 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 3220 | /* RFC 9000 10.2. Immediate Close: |
| 3221 | * The closing and draining connection states exist to ensure |
| 3222 | * that connections close cleanly and that delayed or reordered |
| 3223 | * packets are properly discarded. These states SHOULD persist |
| 3224 | * for at least three times the current PTO interval... |
| 3225 | * |
| 3226 | * Rearm the idle timeout only one time when entering closing |
| 3227 | * state. |
| 3228 | */ |
| 3229 | qc_idle_timer_do_rearm(qc); |
| 3230 | if (qc->timer_task) { |
| 3231 | task_destroy(qc->timer_task); |
| 3232 | qc->timer_task = NULL; |
| 3233 | } |
| 3234 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3235 | qc->path->in_flight += pkt->in_flight_len; |
| 3236 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3237 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3238 | qc_set_timer(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3239 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3240 | next_pkt = pkt->next; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 3241 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 3242 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3243 | } |
| 3244 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3245 | |
Frédéric Lécaille | 8726d63 | 2022-05-03 10:32:21 +0200 | [diff] [blame] | 3246 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3247 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3248 | return 1; |
| 3249 | } |
| 3250 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3251 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3252 | * <salt> salt input. This is the cluster secret which will be derived |
| 3253 | * as HKDF input secret to generate this token. |
| 3254 | * Return 1 if succeeded, 0 if not. |
| 3255 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3256 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3257 | unsigned char *buf, size_t len, |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3258 | const unsigned char *salt, size_t saltlen) |
| 3259 | { |
| 3260 | /* Input secret */ |
| 3261 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3262 | size_t keylen = strlen(global.cluster_secret); |
| 3263 | /* Info */ |
| 3264 | const unsigned char label[] = "stateless token"; |
| 3265 | size_t labellen = sizeof label - 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3266 | int ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3267 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3268 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3269 | |
| 3270 | 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] | 3271 | key, keylen, salt, saltlen, label, labellen); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3272 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3273 | return ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3277 | * Returns 1 if succeeded, 0 if not. |
| 3278 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3279 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3280 | struct quic_connection_id *quic_cid) |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3281 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3282 | int ret; |
| 3283 | |
| 3284 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3285 | |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3286 | if (global.cluster_secret) { |
| 3287 | /* Output secret */ |
| 3288 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3289 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3290 | /* Salt */ |
| 3291 | const unsigned char *cid = quic_cid->cid.data; |
| 3292 | size_t cidlen = quic_cid->cid.len; |
| 3293 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3294 | 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] | 3295 | } |
| 3296 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3297 | /* TODO: RAND_bytes() should be replaced */ |
| 3298 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3299 | sizeof quic_cid->stateless_reset_token) == 1; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3300 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3301 | |
| 3302 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3303 | return ret; |
Frédéric Lécaille | 28a1795 | 2022-05-06 15:07:20 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3306 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3307 | * ebtree. |
| 3308 | * |
| 3309 | * The CID is randomly generated in part with the result altered to be |
| 3310 | * associated with the current thread ID. This means this function must only |
| 3311 | * be called by the quic_conn thread. |
| 3312 | * |
| 3313 | * Returns the new CID if succeeded, NULL if not. |
| 3314 | */ |
| 3315 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3316 | struct quic_conn *qc, |
| 3317 | int seq_num) |
| 3318 | { |
| 3319 | struct quic_connection_id *cid; |
| 3320 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3321 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3322 | |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3323 | cid = pool_alloc(pool_head_quic_connection_id); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3324 | if (!cid) { |
| 3325 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3326 | goto err; |
| 3327 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3328 | |
| 3329 | cid->cid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3330 | /* TODO: RAND_bytes() should be replaced */ |
| 3331 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3332 | 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] | 3333 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3334 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3335 | |
| 3336 | quic_pin_cid_to_tid(cid->cid.data, tid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3337 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3338 | 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] | 3339 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3340 | } |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3341 | |
| 3342 | cid->qc = qc; |
| 3343 | |
| 3344 | cid->seq_num.key = seq_num; |
| 3345 | cid->retire_prior_to = 0; |
| 3346 | /* insert the allocated CID in the quic_conn tree */ |
| 3347 | eb64_insert(root, &cid->seq_num); |
| 3348 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3349 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3350 | return cid; |
| 3351 | |
| 3352 | err: |
| 3353 | pool_free(pool_head_quic_connection_id, cid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3354 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 0226c52 | 2022-05-06 14:18:53 +0200 | [diff] [blame] | 3355 | return NULL; |
| 3356 | } |
| 3357 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3358 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3359 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3360 | * a HANDSHAKE_DONE frame. |
| 3361 | * Return 1 if succeeded, 0 if not. |
| 3362 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3363 | 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] | 3364 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3365 | int ret = 0, i, first, max; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3366 | struct quic_enc_level *qel; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3367 | struct quic_frame *frm, *frmbak; |
| 3368 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3369 | struct eb64_node *node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3370 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3371 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3372 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 3373 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3374 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3375 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3376 | frm = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 3377 | if (!frm) |
| 3378 | return 0; |
| 3379 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3380 | LIST_INIT(&frm->reflist); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3381 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3382 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3385 | first = 1; |
| 3386 | max = qc->tx.params.active_connection_id_limit; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3387 | |
| 3388 | /* TODO: check limit */ |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3389 | for (i = first; i < max; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3390 | struct quic_connection_id *cid; |
| 3391 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3392 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3393 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3394 | goto err; |
| 3395 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 3396 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3397 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3398 | if (!cid) |
| 3399 | goto err; |
| 3400 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3401 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3402 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3403 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3404 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3405 | LIST_APPEND(&frm_list, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3406 | } |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3407 | |
| 3408 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 3409 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3410 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3411 | ret = 1; |
| 3412 | leave: |
| 3413 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3414 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3415 | |
| 3416 | err: |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3417 | /* free the frames */ |
| 3418 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3419 | pool_free(pool_head_quic_frame, frm); |
| 3420 | |
| 3421 | node = eb64_first(&qc->cids); |
| 3422 | while (node) { |
| 3423 | struct quic_connection_id *cid; |
| 3424 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3425 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3426 | 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] | 3427 | if (cid->seq_num.key >= max) |
| 3428 | break; |
| 3429 | |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3430 | if (cid->seq_num.key < first) |
| 3431 | continue; |
| 3432 | |
Frédéric Lécaille | 411aa6d | 2022-03-21 12:01:22 +0100 | [diff] [blame] | 3433 | node = eb64_next(node); |
Frédéric Lécaille | d64f68f | 2022-03-18 17:45:28 +0100 | [diff] [blame] | 3434 | ebmb_delete(&cid->node); |
| 3435 | eb64_delete(&cid->seq_num); |
| 3436 | pool_free(pool_head_quic_connection_id, cid); |
| 3437 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3438 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3442 | 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] | 3443 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3444 | struct eb64_node *n; |
| 3445 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3446 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3447 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3448 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3449 | n = eb64_first(&arngs->root); |
| 3450 | while (n) { |
| 3451 | struct eb64_node *next; |
| 3452 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3453 | ar = eb64_entry(n, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3454 | next = eb64_next(n); |
| 3455 | eb64_delete(n); |
Frédéric Lécaille | 82851bd | 2022-04-04 13:43:58 +0200 | [diff] [blame] | 3456 | pool_free(pool_head_quic_arng, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3457 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3458 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3459 | |
| 3460 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3461 | } |
| 3462 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3463 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3464 | * descending order. |
| 3465 | */ |
| 3466 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3467 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3468 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3469 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | |
| 3473 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3474 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3475 | * 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] | 3476 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3477 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3478 | struct quic_arngs *arngs, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3479 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3480 | int ret = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3481 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3482 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3483 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3484 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3485 | last = eb64_last(&arngs->root); |
| 3486 | while (last && arngs->enc_sz > limit) { |
| 3487 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3488 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3489 | prev = eb64_prev(last); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3490 | if (!prev) { |
| 3491 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3492 | goto out; |
| 3493 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3494 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3495 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3496 | 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] | 3497 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3498 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3499 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3500 | --arngs->sz; |
| 3501 | eb64_delete(last); |
| 3502 | pool_free(pool_head_quic_arng, last); |
| 3503 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3504 | } |
| 3505 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3506 | ret = 1; |
| 3507 | out: |
| 3508 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3509 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3510 | } |
| 3511 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3512 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3513 | 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] | 3514 | { |
| 3515 | struct eb64_node *node, *next; |
| 3516 | struct quic_arng_node *ar, *ar_next; |
| 3517 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3518 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3519 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3520 | node = eb64_last(&arngs->root); |
| 3521 | if (!node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3522 | goto leave; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3523 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3524 | ar = eb64_entry(node, 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(ar->last) + |
| 3526 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3527 | |
| 3528 | while ((next = eb64_prev(node))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3529 | 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] | 3530 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3531 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3532 | node = next; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3533 | ar = eb64_entry(node, struct quic_arng_node, first); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3534 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3535 | |
| 3536 | leave: |
| 3537 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3540 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3541 | * 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] | 3542 | */ |
| 3543 | static inline |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3544 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3545 | struct quic_arngs *arngs, |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3546 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3547 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3548 | struct quic_arng_node *new_ar; |
| 3549 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3550 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3551 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3552 | new_ar = pool_alloc(pool_head_quic_arng); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3553 | if (!new_ar) { |
| 3554 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3555 | goto leave; |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3556 | } |
| 3557 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3558 | new_ar->first.key = ar->first; |
| 3559 | new_ar->last = ar->last; |
| 3560 | eb64_insert(&arngs->root, &new_ar->first); |
| 3561 | arngs->sz++; |
| 3562 | |
| 3563 | leave: |
| 3564 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3565 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | /* 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] | 3569 | * 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] | 3570 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3571 | * |
| 3572 | * Descending order |
| 3573 | * -------------> |
| 3574 | * range1 range2 |
| 3575 | * ..........|--------|..............|--------| |
| 3576 | * ^ ^ ^ ^ |
| 3577 | * | | | | |
| 3578 | * last1 first1 last2 first2 |
| 3579 | * ..........+--------+--------------+--------+...... |
| 3580 | * diff1 gap12 diff2 |
| 3581 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3582 | * To encode the previous list of ranges we must encode integers as follows in |
| 3583 | * descending order: |
| 3584 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3585 | * with diff1 = last1 - first1 |
| 3586 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3587 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3588 | * |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3589 | |
| 3590 | returns 0 on error |
| 3591 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3592 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3593 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 3594 | struct quic_arngs *arngs, |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3595 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3596 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3597 | int ret = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3598 | struct eb64_node *le; |
| 3599 | struct quic_arng_node *new_node; |
| 3600 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3601 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3602 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3603 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3604 | new = NULL; |
| 3605 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3606 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3607 | if (new_node) |
| 3608 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3609 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3610 | goto leave; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3611 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3612 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3613 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3614 | if (!le) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3615 | new_node = quic_insert_new_range(qc, arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3616 | if (!new_node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3617 | goto leave; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 3618 | |
| 3619 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3620 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3621 | else { |
| 3622 | struct quic_arng_node *le_ar = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3623 | eb64_entry(le, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3624 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3625 | /* Already existing range */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3626 | if (le_ar->last >= ar->last) { |
| 3627 | ret = 1; |
| 3628 | } |
| 3629 | else if (le_ar->last + 1 >= ar->first) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3630 | le_ar->last = ar->last; |
| 3631 | new = le; |
| 3632 | new_node = le_ar; |
| 3633 | } |
| 3634 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3635 | new_node = quic_insert_new_range(qc, arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3636 | if (!new_node) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3637 | goto leave; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3638 | |
| 3639 | new = &new_node->first; |
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 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3642 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3643 | /* Verify that the new inserted node does not overlap the nodes |
| 3644 | * which follow it. |
| 3645 | */ |
| 3646 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3647 | struct eb64_node *next; |
| 3648 | struct quic_arng_node *next_node; |
| 3649 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3650 | while ((next = eb64_next(new))) { |
| 3651 | next_node = |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3652 | eb64_entry(next, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3653 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3654 | break; |
| 3655 | |
| 3656 | if (next_node->last > new_node->last) |
| 3657 | new_node->last = next_node->last; |
| 3658 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3659 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3660 | /* Decrement the size of these ranges. */ |
| 3661 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3662 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3665 | ret = 1; |
| 3666 | leave: |
| 3667 | quic_arngs_set_enc_sz(qc, arngs); |
| 3668 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3669 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3670 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3671 | /* Remove the header protection of packets at <el> encryption level. |
| 3672 | * Always succeeds. |
| 3673 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3674 | 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] | 3675 | { |
| 3676 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3677 | struct quic_rx_packet *pqpkt; |
| 3678 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3679 | struct quic_enc_level *app_qel; |
| 3680 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3681 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3682 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3683 | /* 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] | 3684 | 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^] | 3685 | TRACE_DEVEL("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3686 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3687 | goto out; |
| 3688 | } |
| 3689 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3690 | mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3691 | 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] | 3692 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3693 | 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] | 3694 | } |
| 3695 | else { |
| 3696 | /* The AAD includes the packet number field */ |
| 3697 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3698 | /* Store the packet into the tree of packets to decrypt. */ |
| 3699 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3700 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3701 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3702 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3703 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3704 | 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] | 3705 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3706 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3707 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3711 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3715 | * Return 1 if succeeded, 0 if not. |
| 3716 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3717 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 3718 | struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3719 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3720 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3721 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3722 | struct eb64_node *node; |
| 3723 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3724 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 3725 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3726 | node = eb64_first(&el->rx.crypto.frms); |
| 3727 | while (node) { |
| 3728 | struct quic_rx_crypto_frm *cf; |
| 3729 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3730 | 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] | 3731 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3732 | break; |
| 3733 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3734 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) { |
| 3735 | TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP); |
| 3736 | goto leave; |
| 3737 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3738 | |
| 3739 | node = eb64_next(node); |
| 3740 | quic_rx_packet_refdec(cf->pkt); |
| 3741 | eb64_delete(&cf->offset_node); |
| 3742 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3743 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3744 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3745 | ret = 1; |
| 3746 | leave: |
| 3747 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 3748 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3749 | } |
| 3750 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3751 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3752 | * 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] | 3753 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3754 | * Return 1 if succeeded, 0 if not. |
| 3755 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3756 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3757 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3758 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3759 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3760 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3761 | int64_t largest_pn = -1; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3762 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3763 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3764 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3765 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3766 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3767 | qel = cur_el; |
| 3768 | next_tel: |
| 3769 | if (!qel) |
| 3770 | goto out; |
| 3771 | |
| 3772 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3773 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3774 | while (node) { |
| 3775 | struct quic_rx_packet *pkt; |
| 3776 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 3777 | 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^] | 3778 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3779 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3780 | if (!qc_pkt_decrypt(pkt, qel, qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3781 | /* Drop the packet */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3782 | TRACE_ERROR("packet decryption failed -> dropped", |
| 3783 | QUIC_EV_CONN_RXPKT, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3784 | } |
| 3785 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3786 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3787 | /* Drop the packet */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3788 | TRACE_ERROR("packet parsing failed -> dropped", |
| 3789 | QUIC_EV_CONN_RXPKT, ctx->qc, pkt); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 3790 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3791 | } |
| 3792 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3793 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3794 | |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 3795 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3796 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3797 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 3798 | qc_idle_timer_rearm(qc, 1); |
| 3799 | } |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3800 | if (pkt->pn > largest_pn) { |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3801 | largest_pn = pkt->pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3802 | largest_pn_time_received = pkt->time_received; |
| 3803 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3804 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3805 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 3806 | TRACE_ERROR("Could not update ack range list", |
| 3807 | QUIC_EV_CONN_RXPKT, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3811 | eb64_delete(&pkt->pn_node); |
| 3812 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3813 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3814 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3815 | |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3816 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3817 | /* Update the largest packet number. */ |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 3818 | qel->pktns->rx.largest_pn = largest_pn; |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 3819 | /* Update the largest acknowledged packet timestamps */ |
| 3820 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3821 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3822 | } |
| 3823 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3824 | if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) { |
| 3825 | // trace already emitted by function above |
| 3826 | goto leave; |
| 3827 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3828 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3829 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3830 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3831 | qel = next_el; |
Frédéric Lécaille | 91a211f | 2022-05-24 10:54:42 +0200 | [diff] [blame] | 3832 | largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3833 | goto next_tel; |
| 3834 | } |
| 3835 | |
| 3836 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3837 | ret = 1; |
| 3838 | leave: |
| 3839 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc); |
| 3840 | return ret; |
| 3841 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3842 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3843 | /* Check if it's possible to remove header protection for packets related to |
| 3844 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3845 | * |
| 3846 | * Return true if the operation is possible else false. |
| 3847 | */ |
| 3848 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3849 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3850 | int ret = 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3851 | enum quic_tls_enc_level tel; |
| 3852 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3853 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 3854 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3855 | if (!qel) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3856 | goto cant_rm_hp; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3857 | |
| 3858 | tel = ssl_to_quic_enc_level(qel->level); |
| 3859 | |
| 3860 | /* check if tls secrets are available */ |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 3861 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3862 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3863 | goto cant_rm_hp; |
Frédéric Lécaille | 51c9065 | 2022-02-22 11:39:14 +0100 | [diff] [blame] | 3864 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3865 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3866 | if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3867 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 3868 | goto cant_rm_hp; |
| 3869 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3870 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3871 | /* 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] | 3872 | 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^] | 3873 | qc->mux_state == QC_MUX_NULL) { |
| 3874 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 3875 | goto cant_rm_hp; |
| 3876 | } |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3877 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3878 | ret = 1; |
| 3879 | cant_rm_hp: |
| 3880 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 3881 | return ret; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3882 | } |
| 3883 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3884 | /* Sends application level packets from <qc> QUIC connection */ |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3885 | int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms) |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3886 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3887 | int status = 0; |
| 3888 | struct buffer *buf; |
| 3889 | |
| 3890 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3891 | |
| 3892 | buf = qc_txb_alloc(qc); |
| 3893 | if (!buf) { |
| 3894 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3895 | goto leave; |
| 3896 | } |
| 3897 | |
| 3898 | if (old_data) { |
| 3899 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 3900 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 3901 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3902 | |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3903 | /* Prepare and send packets until we could not further prepare packets. */ |
| 3904 | while (1) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3905 | int ret; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3906 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 3907 | * previous sendto() has failed it is emptied to simulate |
| 3908 | * packet emission and rely on QUIC lost detection to try to |
| 3909 | * emit it. |
| 3910 | */ |
| 3911 | BUG_ON_HOT(b_data(buf)); |
| 3912 | b_reset(buf); |
| 3913 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3914 | ret = qc_prep_app_pkts(qc, buf, frms); |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3915 | if (ret == -1) |
| 3916 | goto err; |
| 3917 | else if (ret == 0) |
| 3918 | goto out; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3919 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3920 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Frédéric Lécaille | dc07751 | 2022-07-26 09:17:19 +0200 | [diff] [blame] | 3921 | goto err; |
| 3922 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3923 | |
| 3924 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3925 | status = 1; |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3926 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3927 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3928 | leave: |
| 3929 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3930 | return status; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3931 | |
| 3932 | err: |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 3933 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3934 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3935 | goto leave; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 3936 | } |
| 3937 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3938 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 3939 | * 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^] | 3940 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 3941 | * not already acknowledged (in flight). |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3942 | * Returns 1 if succeeded, 0 if not. |
| 3943 | */ |
| 3944 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 3945 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3946 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3947 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3948 | int ret, status = 0; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3949 | struct buffer *buf = qc_txb_alloc(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3950 | |
| 3951 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3952 | |
| 3953 | if (!buf) { |
| 3954 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3955 | goto leave; |
| 3956 | } |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3957 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3958 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 3959 | * sendto() has failed it is emptied to simulate packet emission and |
| 3960 | * rely on QUIC lost detection to try to emit it. |
| 3961 | */ |
| 3962 | BUG_ON_HOT(b_data(buf)); |
| 3963 | b_reset(buf); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3964 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3965 | if (old_data) { |
| 3966 | 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] | 3967 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3968 | } |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3969 | |
| 3970 | 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] | 3971 | if (ret == -1) |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3972 | goto out; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3973 | else if (ret == 0) |
| 3974 | goto skip_send; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3975 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 3976 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3977 | goto out; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3978 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3979 | skip_send: |
| 3980 | status = 1; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3981 | out: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3982 | 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] | 3983 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 3984 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3985 | leave: |
| 3986 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3987 | return status; |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | /* Retransmit up to two datagrams depending on packet number space */ |
| 3991 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 3992 | { |
| 3993 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3994 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3995 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3996 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 3997 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3998 | |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 3999 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4000 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4001 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 4002 | |
| 4003 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4004 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4005 | 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] | 4006 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4007 | iqel->pktns->tx.pto_probe = 1; |
| 4008 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4009 | hqel->pktns->tx.pto_probe = 1; |
| 4010 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4011 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 4012 | } |
| 4013 | } |
| 4014 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4015 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4016 | 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] | 4017 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4018 | hqel->pktns->tx.pto_probe = 1; |
| 4019 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 4020 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4021 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4022 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4023 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4024 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4025 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4026 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4027 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4028 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4029 | } |
| 4030 | else { |
| 4031 | int i; |
| 4032 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4033 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4034 | |
| 4035 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4036 | hqel->pktns->tx.pto_probe = 0; |
| 4037 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4038 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4039 | 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] | 4040 | if (!LIST_ISEMPTY(&frms1)) { |
| 4041 | hqel->pktns->tx.pto_probe = 1; |
| 4042 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4043 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4044 | } |
| 4045 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4046 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4047 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4048 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4049 | } |
| 4050 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4051 | aqel->pktns->tx.pto_probe = 0; |
| 4052 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4053 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4054 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4055 | if (!LIST_ISEMPTY(&frms1)) { |
| 4056 | aqel->pktns->tx.pto_probe = 1; |
| 4057 | qc_send_app_pkts(qc, 1, &frms1); |
| 4058 | } |
| 4059 | if (!LIST_ISEMPTY(&frms2)) { |
| 4060 | aqel->pktns->tx.pto_probe = 1; |
| 4061 | qc_send_app_pkts(qc, 1, &frms2); |
| 4062 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4063 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4064 | QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4065 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4066 | } |
| 4067 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4068 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a956841 | 2022-04-25 08:58:04 +0200 | [diff] [blame] | 4069 | } |
| 4070 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4071 | /* QUIC connection packet handler task (post handshake) */ |
| 4072 | static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4073 | { |
| 4074 | struct ssl_sock_ctx *ctx; |
| 4075 | struct quic_conn *qc; |
| 4076 | struct quic_enc_level *qel; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4077 | |
| 4078 | |
| 4079 | ctx = context; |
| 4080 | qc = ctx->qc; |
| 4081 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4082 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4083 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4084 | 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] | 4085 | |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4086 | /* Retranmissions */ |
| 4087 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4088 | 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] | 4089 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4090 | qc_dgrams_retransmit(qc); |
| 4091 | } |
| 4092 | |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4093 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4094 | qc_rm_hp_pkts(qc, qel); |
| 4095 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4096 | if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) { |
| 4097 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4098 | goto out; |
| 4099 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4100 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4101 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4102 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4103 | 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] | 4104 | goto out; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4105 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4106 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4107 | /* XXX TODO: how to limit the list frames to send */ |
| 4108 | if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms)) { |
| 4109 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4110 | goto out; |
| 4111 | } |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4112 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4113 | out: |
| 4114 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4115 | return t; |
| 4116 | } |
| 4117 | |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4118 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4119 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4120 | { |
| 4121 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4122 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4123 | qel->pktns->tx.pto_probe || |
| 4124 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4125 | } |
| 4126 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4127 | /* QUIC connection packet handler task. */ |
| 4128 | 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] | 4129 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4130 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4131 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4132 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4133 | enum quic_tls_enc_level tel, next_tel; |
| 4134 | struct quic_enc_level *qel, *next_qel; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4135 | struct buffer *buf = NULL; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4136 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4137 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4138 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4139 | qc = ctx->qc; |
Frédéric Lécaille | 00e2400 | 2022-02-18 17:13:45 +0100 | [diff] [blame] | 4140 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4141 | st = qc->state; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4142 | 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] | 4143 | |
| 4144 | /* Retranmissions */ |
| 4145 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4146 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 7aef5f4 | 2022-04-25 10:33:12 +0200 | [diff] [blame] | 4147 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4148 | qc_dgrams_retransmit(qc); |
| 4149 | } |
| 4150 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4151 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 4152 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4153 | TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached", |
| 4154 | QUIC_EV_CONN_IO_CB, qc); |
| 4155 | /* 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] | 4156 | * after the anti-amplification was reached. |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4157 | * |
| 4158 | * TODO: this part should be removed. This was there because the |
| 4159 | * datagram parser was not executed by only one thread. |
| 4160 | */ |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4161 | qc_set_timer(qc); |
| 4162 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 4163 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 4164 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4165 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 4166 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 4167 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 4168 | 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] | 4169 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 4170 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4171 | 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^] | 4172 | 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] | 4173 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4174 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4175 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4176 | } |
| 4177 | 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^] | 4178 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4179 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4180 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 4181 | 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] | 4182 | |
| 4183 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4184 | /* Treat packets waiting for header packet protection decryption */ |
| 4185 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4186 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4187 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 4188 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 4189 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4190 | 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^] | 4191 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4192 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4193 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4194 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4195 | goto out; |
| 4196 | |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4197 | if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] && |
| 4198 | !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) { |
| 4199 | if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 4200 | qel = next_qel; |
| 4201 | next_qel = NULL; |
| 4202 | goto next_level; |
| 4203 | } |
| 4204 | else { |
| 4205 | struct quic_rx_packet *pkt; |
| 4206 | struct mt_list *elt1, elt2; |
| 4207 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 4208 | |
| 4209 | /* Drop these 0-RTT packets */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4210 | TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 1231d3c | 2022-04-28 15:43:46 +0200 | [diff] [blame] | 4211 | mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) { |
| 4212 | MT_LIST_DELETE_SAFE(elt1); |
| 4213 | quic_rx_packet_refdec(pkt); |
| 4214 | } |
| 4215 | } |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4218 | st = qc->state; |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4219 | if (st >= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4220 | 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] | 4221 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4222 | goto out; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 4223 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4224 | 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] | 4225 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4226 | /* Discard the Handshake keys. */ |
| 4227 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4228 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4229 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4230 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 4231 | 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^] | 4232 | 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] | 4233 | } |
| 4234 | |
| 4235 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4236 | /* There may be remaining handshake to build (acks) */ |
| 4237 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4238 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 4239 | } |
| 4240 | |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 4241 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4242 | * be considered. |
| 4243 | */ |
| 4244 | 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^] | 4245 | goto out; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4246 | |
Amaury Denoyelle | f0f92b2 | 2022-08-09 17:52:52 +0200 | [diff] [blame] | 4247 | if (!qc_need_sending(qc, qel) && |
| 4248 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4249 | goto skip_send; |
Amaury Denoyelle | f0f92b2 | 2022-08-09 17:52:52 +0200 | [diff] [blame] | 4250 | } |
Amaury Denoyelle | c09ef0c | 2022-08-08 18:15:24 +0200 | [diff] [blame] | 4251 | |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4252 | buf = qc_txb_alloc(qc); |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4253 | if (!buf) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4254 | goto out; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4255 | |
| 4256 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4257 | * sendto() has failed it is emptied to simulate packet emission and |
| 4258 | * rely on QUIC lost detection to try to emit it. |
| 4259 | */ |
| 4260 | BUG_ON_HOT(b_data(buf)); |
| 4261 | b_reset(buf); |
| 4262 | |
| 4263 | 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] | 4264 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4265 | if (ret == -1) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4266 | goto out; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4267 | else if (ret == 0) |
| 4268 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4269 | |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4270 | if (!qc_send_ppkts(buf, ctx)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4271 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4272 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4273 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4274 | /* Check if there is something to do for the next level. |
| 4275 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4276 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4277 | (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 4278 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4279 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 4280 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4281 | goto next_level; |
| 4282 | } |
| 4283 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4284 | out: |
Amaury Denoyelle | 5b68986 | 2022-08-08 16:07:30 +0200 | [diff] [blame] | 4285 | qc_txb_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4286 | 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] | 4287 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4290 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4291 | 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] | 4292 | { |
| 4293 | int i; |
| 4294 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4295 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4296 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4297 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4298 | if (qel->tx.crypto.bufs[i]) { |
| 4299 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4300 | qel->tx.crypto.bufs[i] = NULL; |
| 4301 | } |
| 4302 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4303 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4304 | |
| 4305 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4309 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4310 | * Returns 1 if succeeded, 0 if not. |
| 4311 | */ |
| 4312 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4313 | enum quic_tls_enc_level level) |
| 4314 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4315 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4316 | struct quic_enc_level *qel; |
| 4317 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4318 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4319 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4320 | qel = &qc->els[level]; |
| 4321 | qel->level = quic_to_ssl_enc_level(level); |
| 4322 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4323 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4324 | 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] | 4325 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4326 | |
| 4327 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 4328 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 4329 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 4330 | qel->rx.crypto.offset = 0; |
| 4331 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4332 | |
| 4333 | /* Allocate only one buffer. */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4334 | /* TODO: use a pool */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4335 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4336 | if (!qel->tx.crypto.bufs) |
| 4337 | goto err; |
| 4338 | |
| 4339 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4340 | if (!qel->tx.crypto.bufs[0]) |
| 4341 | goto err; |
| 4342 | |
| 4343 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4344 | qel->tx.crypto.nb_buf = 1; |
| 4345 | |
| 4346 | qel->tx.crypto.sz = 0; |
| 4347 | qel->tx.crypto.offset = 0; |
| 4348 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4349 | ret = 1; |
| 4350 | leave: |
| 4351 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4352 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4353 | |
| 4354 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4355 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4356 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4357 | } |
| 4358 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4359 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4360 | * The connection tasklet is killed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4361 | * |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4362 | * This function must only be called by the thread responsible of the quic_conn |
| 4363 | * tasklet. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4364 | */ |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4365 | static void quic_conn_release(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4366 | { |
| 4367 | int i; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4368 | struct ssl_sock_ctx *conn_ctx; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4369 | struct eb64_node *node; |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4370 | struct quic_tls_ctx *app_tls_ctx; |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4371 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4372 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4373 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4374 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4375 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4376 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4377 | |
Willy Tarreau | 54bc786 | 2022-08-10 07:26:27 +0200 | [diff] [blame] | 4378 | /* in the unlikely (but possible) case the connection was just added to |
| 4379 | * the accept_list we must delete it from there. |
| 4380 | */ |
| 4381 | MT_LIST_DELETE(&qc->accept_list); |
| 4382 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4383 | /* free remaining stream descriptors */ |
| 4384 | node = eb64_first(&qc->streams_by_id); |
| 4385 | while (node) { |
| 4386 | struct qc_stream_desc *stream; |
| 4387 | |
| 4388 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4389 | node = eb64_next(node); |
| 4390 | |
Amaury Denoyelle | c9acc31 | 2022-04-01 16:41:21 +0200 | [diff] [blame] | 4391 | /* all streams attached to the quic-conn are released, so |
| 4392 | * qc_stream_desc_free will liberate the stream instance. |
| 4393 | */ |
| 4394 | BUG_ON(!stream->release); |
| 4395 | qc_stream_desc_free(stream); |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4396 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | |
Amaury Denoyelle | 2c31e12 | 2022-06-20 10:52:55 +0200 | [diff] [blame] | 4398 | /* Purge Rx packet list. */ |
| 4399 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 4400 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 4401 | pool_free(pool_head_quic_rx_packet, pkt); |
| 4402 | } |
| 4403 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4404 | if (qc->idle_timer_task) { |
| 4405 | task_destroy(qc->idle_timer_task); |
| 4406 | qc->idle_timer_task = NULL; |
| 4407 | } |
| 4408 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4409 | if (qc->timer_task) { |
| 4410 | task_destroy(qc->timer_task); |
| 4411 | qc->timer_task = NULL; |
| 4412 | } |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4413 | |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4414 | /* remove the connection from receiver cids trees */ |
| 4415 | ebmb_delete(&qc->odcid_node); |
| 4416 | ebmb_delete(&qc->scid_node); |
| 4417 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4418 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4419 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4420 | if (conn_ctx) { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4421 | tasklet_free(conn_ctx->wait_event.tasklet); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4422 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4423 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 4424 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4425 | |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4426 | quic_tls_ku_free(qc); |
| 4427 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4428 | 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^] | 4429 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4430 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4431 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
Frédéric Lécaille | 96fd163 | 2022-04-01 11:21:47 +0200 | [diff] [blame] | 4432 | |
| 4433 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 4434 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 4435 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4436 | |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4437 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
Frédéric Lécaille | 8ddde4f | 2022-08-01 14:07:50 +0200 | [diff] [blame] | 4438 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4439 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
Frédéric Lécaille | eb2a2da | 2022-04-01 12:15:24 +0200 | [diff] [blame] | 4440 | } |
Frédéric Lécaille | 6467088 | 2022-04-01 11:57:19 +0200 | [diff] [blame] | 4441 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 4442 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 4443 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4444 | 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^] | 4445 | |
| 4446 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4447 | } |
| 4448 | |
Amaury Denoyelle | e0be573 | 2022-04-05 17:34:18 +0200 | [diff] [blame] | 4449 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4450 | { |
| 4451 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 4452 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4453 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4454 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 4455 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4456 | /* Next application data can be dropped. */ |
| 4457 | qc->mux_state = QC_MUX_RELEASED; |
| 4458 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4459 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 4460 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 4461 | quic_conn_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4462 | goto leave; |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4463 | } |
| 4464 | |
Amaury Denoyelle | 240b1b1 | 2022-07-13 15:18:16 +0200 | [diff] [blame] | 4465 | qc_check_close_on_released_mux(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4466 | leave: |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 4467 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 4468 | } |
| 4469 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4470 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 4471 | 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] | 4472 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4473 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4474 | struct quic_conn *qc; |
| 4475 | struct quic_pktns *pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4476 | |
| 4477 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 4478 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4479 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 4480 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4481 | task->expire = TICK_ETERNITY; |
| 4482 | pktns = quic_loss_pktns(qc); |
| 4483 | if (tick_isset(pktns->tx.loss_time)) { |
| 4484 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4485 | |
| 4486 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4487 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4488 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4489 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4490 | goto out; |
| 4491 | } |
| 4492 | |
| 4493 | if (qc->path->in_flight) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4494 | 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] | 4495 | if (qc->mux_state == QC_MUX_READY && qc->qcc->subs && |
| 4496 | qc->qcc->subs->events & SUB_RETRY_SEND) { |
| 4497 | struct qcc *qcc = qc->qcc; |
| 4498 | |
| 4499 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
| 4500 | tasklet_wakeup(qcc->subs->tasklet); |
| 4501 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 4502 | if (!qcc->subs->events) |
| 4503 | qcc->subs = NULL; |
| 4504 | } |
| 4505 | else { |
| 4506 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4507 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4508 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4509 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4510 | 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] | 4511 | 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^] | 4512 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4513 | } |
| 4514 | } |
| 4515 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4516 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4517 | } |
| 4518 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 4519 | 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] | 4520 | } |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 4521 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4522 | } |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4523 | 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] | 4524 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4525 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4526 | |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4527 | 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] | 4528 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4529 | 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] | 4530 | iel->pktns->tx.pto_probe = 1; |
| 4531 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 4532 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4533 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4534 | qc->path->loss.pto_count++; |
| 4535 | |
| 4536 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4537 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4538 | |
| 4539 | return task; |
| 4540 | } |
| 4541 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4542 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4543 | * one byte past the end of this buffer. This will extract the ODCID |
| 4544 | * which will be stored into <odcid> |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4545 | * |
| 4546 | * Returns 0 on success else non-zero. |
| 4547 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4548 | static int parse_retry_token(struct quic_conn *qc, |
| 4549 | const unsigned char *token, const unsigned char *end, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4550 | struct quic_cid *odcid) |
| 4551 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4552 | int ret = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4553 | uint64_t odcid_len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4554 | uint32_t timestamp; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4555 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4556 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4557 | |
| 4558 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 4559 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 4560 | goto leave; |
| 4561 | } |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4562 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4563 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4564 | * When an Initial packet is sent by a client that has not previously |
| 4565 | * received an Initial or Retry packet from the server, the client |
| 4566 | * populates the Destination Connection ID field with an unpredictable |
| 4567 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4568 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4569 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 4570 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4571 | goto leave; |
| 4572 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4573 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4574 | if (end - token < odcid_len + sizeof timestamp) { |
| 4575 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4576 | goto leave; |
| 4577 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4578 | |
| 4579 | timestamp = ntohl(read_u32(token + odcid_len)); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4580 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 4581 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 4582 | goto leave; |
| 4583 | } |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4584 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4585 | ret = 1; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4586 | memcpy(odcid->data, token, odcid_len); |
| 4587 | odcid->len = odcid_len; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4588 | leave: |
| 4589 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4590 | return !ret; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4593 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4594 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4595 | * for QUIC servers (or haproxy listeners). |
| 4596 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4597 | * <token> the token found to be used for this connection with <token_len> as |
| 4598 | * length. <saddr> is the source address. |
| 4599 | * Returns the connection if succeeded, NULL if not. |
| 4600 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 4601 | 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] | 4602 | struct quic_cid *dcid, struct quic_cid *scid, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 4603 | const struct quic_cid *odcid, |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4604 | struct sockaddr_storage *saddr, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4605 | int server, int token, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4606 | { |
| 4607 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4608 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4609 | /* Initial CID. */ |
| 4610 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4611 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4612 | struct listener *l = NULL; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4613 | struct quic_cc_algo *cc_algo = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4614 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4615 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4616 | qc = pool_zalloc(pool_head_quic_conn); |
| 4617 | if (!qc) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4618 | 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] | 4619 | goto err; |
| 4620 | } |
| 4621 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4622 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4623 | if (!buf_area) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4624 | 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] | 4625 | goto err; |
| 4626 | } |
| 4627 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4628 | qc->cids = EB_ROOT; |
| 4629 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4630 | if (server) { |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4631 | struct proxy *prx; |
| 4632 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4633 | l = owner; |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4634 | prx = l->bind_conf->frontend; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4635 | cc_algo = l->bind_conf->quic_cc_algo; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4636 | |
Frédéric Lécaille | a89659a | 2022-05-19 11:58:53 +0200 | [diff] [blame] | 4637 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4638 | &quic_stats_module); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 4639 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4640 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4641 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4642 | qc->odcid.len = dcid->len; |
| 4643 | qc->odcid.addrlen = dcid->addrlen; |
| 4644 | 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] | 4645 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 4646 | /* 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] | 4647 | if (scid->len) |
| 4648 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4649 | qc->dcid.len = scid->len; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4650 | qc->tx.buf = BUF_NULL; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 4651 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4652 | } |
| 4653 | /* QUIC Client (outgoing connection to servers) */ |
| 4654 | else { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 4655 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4656 | if (dcid->len) |
| 4657 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4658 | qc->dcid.len = dcid->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4659 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 4660 | qc->mux_state = QC_MUX_NULL; |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 4661 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4662 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 4663 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4664 | if (!icid) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4665 | 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] | 4666 | goto err; |
| 4667 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4668 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4669 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4670 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4671 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 4672 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4673 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4674 | qc->scid = icid->cid; |
| 4675 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4676 | /* Packet number spaces initialization. */ |
| 4677 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4678 | quic_pktns_init(&qc->pktns[i]); |
| 4679 | /* QUIC encryption level context initialization. */ |
| 4680 | 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] | 4681 | if (!quic_conn_enc_level_init(qc, i)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4682 | 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] | 4683 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4684 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4685 | /* Initialize the packet number space. */ |
| 4686 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4687 | } |
| 4688 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4689 | qc->original_version = qv; |
| 4690 | 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] | 4691 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4692 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4693 | /* TX part. */ |
| 4694 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4695 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4696 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4697 | qc->tx.bytes = 0; |
Amaury Denoyelle | f247605 | 2022-08-04 16:19:57 +0200 | [diff] [blame] | 4698 | qc->tx.buf = BUF_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4699 | /* RX part. */ |
| 4700 | qc->rx.bytes = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4701 | 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] | 4702 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4703 | qc->rx.strms[i].nb_streams = 0; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 4704 | |
| 4705 | qc->nb_pkt_for_cc = 1; |
| 4706 | qc->nb_pkt_since_cc = 0; |
| 4707 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4708 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 4709 | if (!quic_tls_ku_init(qc)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4710 | 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] | 4711 | goto err; |
| 4712 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4713 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4714 | /* XXX TO DO: Only one path at this time. */ |
| 4715 | qc->path = &qc->paths[0]; |
Frédéric Lécaille | 43910a9 | 2022-07-11 10:24:21 +0200 | [diff] [blame] | 4716 | 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] | 4717 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4718 | /* required to use MTLIST_IN_LIST */ |
| 4719 | MT_LIST_INIT(&qc->accept_list); |
| 4720 | |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4721 | qc->streams_by_id = EB_ROOT_UNIQUE; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 4722 | qc->stream_buf_count = 0; |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4723 | memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr); |
| 4724 | |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 4725 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 4726 | icid->stateless_reset_token, |
| 4727 | dcid->data, dcid->len, |
| 4728 | qc->scid.data, qc->scid.len, |
| 4729 | odcid->data, odcid->len, token)) |
Frédéric Lécaille | 395a64d | 2022-05-09 15:42:26 +0200 | [diff] [blame] | 4730 | goto err; |
Amaury Denoyelle | 5c3859c | 2022-03-29 14:49:35 +0200 | [diff] [blame] | 4731 | |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4732 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4733 | !quic_conn_init_timer(qc) || |
| 4734 | !quic_conn_init_idle_timer_task(qc)) |
| 4735 | goto err; |
| 4736 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 4737 | if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx, |
| 4738 | qc->original_version, dcid->data, dcid->len, 1)) |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 4739 | goto err; |
| 4740 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 4741 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4742 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4743 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4744 | |
| 4745 | err: |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 4746 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 4747 | if (qc) |
| 4748 | qc->rx.buf.area = NULL; |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 4749 | quic_conn_release(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4750 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4751 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /* Initialize the timer task of <qc> QUIC connection. |
| 4755 | * Returns 1 if succeeded, 0 if not. |
| 4756 | */ |
| 4757 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 4758 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4759 | int ret = 0; |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 4760 | /* 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^] | 4761 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 4762 | |
Willy Tarreau | 8716875 | 2022-06-13 16:31:53 +0200 | [diff] [blame] | 4763 | qc->timer_task = task_new_on(qc->tid); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4764 | if (!qc->timer_task) { |
| 4765 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 4766 | goto leave; |
| 4767 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4768 | |
| 4769 | qc->timer = TICK_ETERNITY; |
| 4770 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 4771 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4772 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4773 | ret = 1; |
| 4774 | leave: |
| 4775 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 4776 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4777 | } |
| 4778 | |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4779 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 4780 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 4781 | { |
| 4782 | unsigned int expire; |
| 4783 | |
| 4784 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 4785 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 4786 | } |
| 4787 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4788 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 4789 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 4790 | */ |
| 4791 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 4792 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4793 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 4794 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4795 | if (read) { |
| 4796 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4797 | } |
| 4798 | else { |
| 4799 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 4800 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 4801 | qc_idle_timer_do_rearm(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4802 | |
| 4803 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | /* The task handling the idle timeout */ |
| 4807 | static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 4808 | { |
| 4809 | struct quic_conn *qc = ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4810 | struct quic_counters *prx_counters = qc->prx_counters; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4811 | unsigned int qc_flags = qc->flags; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4812 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4813 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 4814 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 4815 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 4816 | * might free the quic-conn too early via quic_close(). |
| 4817 | */ |
| 4818 | qc_notify_close(qc); |
| 4819 | |
Amaury Denoyelle | db71e3b | 2022-04-06 17:22:12 +0200 | [diff] [blame] | 4820 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 4821 | * responsible to call quic_close to release it. |
| 4822 | */ |
| 4823 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 4824 | if (qc->mux_state != QC_MUX_READY) |
| 4825 | quic_conn_release(qc); |
| 4826 | |
| 4827 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 4828 | * least clean some parts of it such as the tasklet. |
| 4829 | */ |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 4830 | |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4831 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 4832 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4833 | 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] | 4834 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
Frédéric Lécaille | 2aebaa4 | 2022-06-17 15:11:32 +0200 | [diff] [blame] | 4835 | } |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4836 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4837 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4838 | return NULL; |
| 4839 | } |
| 4840 | |
| 4841 | /* Initialize the idle timeout task for <qc>. |
| 4842 | * Returns 1 if succeeded, 0 if not. |
| 4843 | */ |
| 4844 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 4845 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4846 | int ret = 0; |
| 4847 | |
| 4848 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 4849 | |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4850 | qc->idle_timer_task = task_new_here(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4851 | if (!qc->idle_timer_task) { |
| 4852 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 4853 | goto leave; |
| 4854 | } |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4855 | |
| 4856 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 4857 | qc->idle_timer_task->context = qc; |
| 4858 | qc_idle_timer_rearm(qc, 1); |
| 4859 | task_queue(qc->idle_timer_task); |
| 4860 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4861 | ret = 1; |
| 4862 | leave: |
| 4863 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 4864 | return ret; |
Frédéric Lécaille | 530601c | 2022-03-10 15:11:57 +0100 | [diff] [blame] | 4865 | } |
| 4866 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4867 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 4868 | * past one byte of this buffer. |
| 4869 | */ |
| 4870 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 4871 | struct quic_rx_packet *pkt) |
| 4872 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4873 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4874 | unsigned char dcid_len, scid_len; |
| 4875 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4876 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 4877 | |
| 4878 | if (end == *buf) { |
| 4879 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 4880 | goto leave; |
| 4881 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4882 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4883 | /* Destination Connection ID Length */ |
| 4884 | dcid_len = *(*buf)++; |
| 4885 | /* 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^] | 4886 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 4887 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 4888 | goto leave; |
| 4889 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4890 | |
| 4891 | if (dcid_len) { |
| 4892 | /* Check that the length of this received DCID matches the CID lengths |
| 4893 | * of our implementation for non Initials packets only. |
| 4894 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4895 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 4896 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4897 | dcid_len != QUIC_HAP_CID_LEN) { |
| 4898 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 4899 | goto leave; |
| 4900 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4901 | |
| 4902 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 4903 | } |
| 4904 | |
| 4905 | pkt->dcid.len = dcid_len; |
| 4906 | *buf += dcid_len; |
| 4907 | |
| 4908 | /* Source Connection ID Length */ |
| 4909 | scid_len = *(*buf)++; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4910 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 4911 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 4912 | goto leave; |
| 4913 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4914 | |
| 4915 | if (scid_len) |
| 4916 | memcpy(pkt->scid.data, *buf, scid_len); |
| 4917 | pkt->scid.len = scid_len; |
| 4918 | *buf += scid_len; |
| 4919 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4920 | ret = 1; |
| 4921 | leave: |
| 4922 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 4923 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4924 | } |
| 4925 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4926 | /* 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^] | 4927 | static void qc_pkt_insert(struct quic_conn *qc, |
| 4928 | 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] | 4929 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4930 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4931 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4932 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 4933 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4934 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 4935 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 4936 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4937 | |
| 4938 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /* 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] | 4942 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 4943 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 4944 | * the packet first byte. |
| 4945 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 4946 | * Returns 1 if succeeded, 0 if not. |
| 4947 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4948 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 4949 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4950 | unsigned char *buf, unsigned char *beg, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4951 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4952 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4953 | int ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4954 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4955 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4956 | struct quic_enc_level *qel; |
| 4957 | /* Only for traces. */ |
| 4958 | struct quic_rx_packet *qpkt_trace; |
| 4959 | |
| 4960 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4961 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4962 | /* The packet number is here. This is also the start minus |
| 4963 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 4964 | * protection. |
| 4965 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4966 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 4967 | |
| 4968 | tel = quic_packet_type_enc_level(pkt->type); |
| 4969 | qel = &qc->els[tel]; |
| 4970 | |
| 4971 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4972 | /* Note that the following function enables us to unprotect the packet |
| 4973 | * number and its length subsequently used to decrypt the entire |
| 4974 | * packets. |
| 4975 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4976 | 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] | 4977 | qel->pktns->rx.largest_pn, pn, beg)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4978 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 4979 | goto out; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /* The AAD includes the packet number field found at <pn>. */ |
| 4983 | pkt->aad_len = pn - beg + pkt->pnl; |
Frédéric Lécaille | ffde316 | 2022-08-08 18:41:16 +0200 | [diff] [blame] | 4984 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 4985 | 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^] | 4986 | goto out; |
Frédéric Lécaille | ffde316 | 2022-08-08 18:41:16 +0200 | [diff] [blame] | 4987 | } |
| 4988 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4989 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4990 | } |
Frédéric Lécaille | 7d845f1 | 2022-02-21 19:22:09 +0100 | [diff] [blame] | 4991 | else { |
Frédéric Lécaille | bd24208 | 2022-02-25 17:17:59 +0100 | [diff] [blame] | 4992 | 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] | 4993 | /* If the packet number space has been discarded, this packet |
| 4994 | * will be not parsed. |
| 4995 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4996 | 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] | 4997 | goto out; |
| 4998 | } |
| 4999 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5000 | 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] | 5001 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 5002 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5003 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5006 | *el = qel; |
| 5007 | /* No reference counter incrementation here!!! */ |
| 5008 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5009 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5010 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5011 | b_add(&qc->rx.buf, pkt->len); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5012 | |
| 5013 | ret = 1; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5014 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 5015 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5016 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5019 | /* 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] | 5020 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5021 | * of this packet into <*version>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5022 | */ |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5023 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5024 | unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5025 | int *long_header, uint32_t *version) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5026 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5027 | int ret = 0; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5028 | const unsigned char byte0 = **buf; |
| 5029 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5030 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5031 | |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5032 | (*buf)++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5033 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5034 | unsigned char type = |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5035 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5036 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5037 | *long_header = 1; |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5038 | /* Version */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5039 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5040 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5041 | goto out; |
| 5042 | } |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5043 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5044 | if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) { |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5045 | pkt->type = type; |
| 5046 | } |
| 5047 | else { |
| 5048 | switch (type) { |
| 5049 | case 0: |
| 5050 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5051 | break; |
| 5052 | case 1: |
| 5053 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5054 | break; |
| 5055 | case 2: |
| 5056 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5057 | break; |
| 5058 | case 3: |
| 5059 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5060 | break; |
| 5061 | } |
| 5062 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5063 | } |
| 5064 | else { |
| 5065 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5066 | *long_header = 0; |
| 5067 | } |
Frédéric Lécaille | ea0ec27 | 2022-06-08 13:22:17 +0200 | [diff] [blame] | 5068 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5069 | ret = 1; |
| 5070 | out: |
| 5071 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5072 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5075 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5076 | * if supported or NULL if not. |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5077 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5078 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5079 | { |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5080 | int i; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5081 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5082 | for (i = 0; i < quic_versions_nb; i++) |
| 5083 | if (quic_versions[i].num == version) |
| 5084 | return &quic_versions[i]; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5085 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5086 | return NULL; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5087 | } |
| 5088 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5089 | /* |
| 5090 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5091 | * address <addr>. |
| 5092 | * Implementation of RFC9000 6. Version Negotiation |
| 5093 | * |
| 5094 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5095 | * |
| 5096 | * Returns 0 on success else non-zero |
| 5097 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5098 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5099 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5100 | { |
| 5101 | char buf[256]; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5102 | int ret = 0, i = 0, j; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5103 | uint32_t version; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5104 | const socklen_t addrlen = get_addr_len(addr); |
| 5105 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5106 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5107 | /* |
| 5108 | * header form |
| 5109 | * long header, fixed bit to 0 for Version Negotiation |
| 5110 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5111 | /* TODO: RAND_bytes() should be replaced? */ |
| 5112 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5113 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5114 | goto out; |
| 5115 | } |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5116 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 5117 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5118 | /* null version for Version Negotiation */ |
| 5119 | buf[i++] = '\x00'; |
| 5120 | buf[i++] = '\x00'; |
| 5121 | buf[i++] = '\x00'; |
| 5122 | buf[i++] = '\x00'; |
| 5123 | |
| 5124 | /* source connection id */ |
| 5125 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 5126 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5127 | i += pkt->scid.len; |
| 5128 | |
| 5129 | /* destination connection id */ |
| 5130 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 5131 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5132 | i += pkt->dcid.len; |
| 5133 | |
| 5134 | /* supported version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5135 | for (j = 0; j < quic_versions_nb; j++) { |
| 5136 | version = htonl(quic_versions[j].num); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5137 | memcpy(&buf[i], &version, sizeof(version)); |
| 5138 | i += sizeof(version); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5139 | } |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5140 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5141 | 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^] | 5142 | goto out; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5143 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5144 | ret = 1; |
| 5145 | out: |
| 5146 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5147 | return !ret; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5148 | } |
| 5149 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5150 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5151 | * from <fd> UDP socket to <dst> |
| 5152 | * Return 1 if succeeded, 0 if not. |
| 5153 | */ |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5154 | 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] | 5155 | struct quic_rx_packet *rxpkt) |
| 5156 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5157 | int ret = 0, pktlen, rndlen; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5158 | unsigned char pkt[64]; |
| 5159 | const socklen_t addrlen = get_addr_len(dstaddr); |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5160 | struct proxy *prx; |
| 5161 | struct quic_counters *prx_counters; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5162 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5163 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5164 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5165 | prx = l->bind_conf->frontend; |
| 5166 | 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] | 5167 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5168 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5169 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5170 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5171 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5172 | * connection ID length that it requests the peer to include in its packets, |
| 5173 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5174 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5175 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5176 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5177 | * than the packet it responds to. |
| 5178 | */ |
| 5179 | |
| 5180 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5181 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5182 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5183 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5184 | |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5185 | /* Put a header of random bytes */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5186 | /* TODO: RAND_bytes() should be replaced */ |
| 5187 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5188 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5189 | goto leave; |
| 5190 | } |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5191 | |
| 5192 | /* Clear the most significant bit, and set the second one */ |
| 5193 | *pkt = (*pkt & ~0x80) | 0x40; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5194 | 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] | 5195 | rxpkt->dcid.data, rxpkt->dcid.len)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5196 | goto leave; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5197 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5198 | 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^] | 5199 | goto leave; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5200 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5201 | ret = 1; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5202 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5203 | 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^] | 5204 | leave: |
| 5205 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5206 | return ret; |
Frédéric Lécaille | e2fb1bf | 2022-05-09 16:30:55 +0200 | [diff] [blame] | 5207 | } |
| 5208 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5209 | /* QUIC server only function. |
| 5210 | * 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] | 5211 | * 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] | 5212 | * to contain these data. |
| 5213 | * Return the number of bytes copied to <aad>. |
| 5214 | */ |
| 5215 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5216 | uint32_t version, |
| 5217 | const struct quic_cid *cid, |
| 5218 | const struct sockaddr_storage *addr) |
| 5219 | { |
| 5220 | unsigned char *p; |
| 5221 | |
| 5222 | p = aad; |
| 5223 | memcpy(p, &version, sizeof version); |
| 5224 | p += sizeof version; |
| 5225 | p += quic_saddr_cpy(p, addr); |
| 5226 | memcpy(p, cid->data, cid->len); |
| 5227 | p += cid->len; |
| 5228 | |
| 5229 | return p - aad; |
| 5230 | } |
| 5231 | |
| 5232 | /* QUIC server only function. |
| 5233 | * Generate the token to be used in Retry packets. The token is written to |
| 5234 | * <buf> whith <len> as length. <odcid> is the original destination connection |
| 5235 | * ID and <dcid> is our side destination connection ID (or client source |
| 5236 | * connection ID). |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5237 | * Returns the length of the encoded token or 0 on error. |
| 5238 | */ |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5239 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5240 | const uint32_t version, |
| 5241 | const struct quic_cid *odcid, |
| 5242 | const struct quic_cid *dcid, |
| 5243 | struct sockaddr_storage *addr) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5244 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5245 | int ret = 0; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5246 | unsigned char *p; |
| 5247 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 5248 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 5249 | size_t aadlen; |
| 5250 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5251 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5252 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5253 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5254 | size_t seclen = strlen(global.cluster_secret); |
| 5255 | EVP_CIPHER_CTX *ctx = NULL; |
| 5256 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5257 | uint32_t timestamp = now_ms; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5258 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5259 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5260 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5261 | /* We copy the odcid into the token, prefixed by its one byte |
| 5262 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5263 | * the random bytes used to derive the secret to encrypt the token. |
| 5264 | */ |
| 5265 | 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^] | 5266 | goto err; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5267 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5268 | 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^] | 5269 | /* TODO: RAND_bytes() should be replaced */ |
| 5270 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5271 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5272 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5273 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5274 | |
| 5275 | 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^] | 5276 | salt, sizeof salt, sec, seclen)) { |
| 5277 | 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] | 5278 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5279 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5280 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5281 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5282 | 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] | 5283 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5284 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5285 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5286 | /* Token build */ |
| 5287 | p = buf; |
| 5288 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5289 | *p++ = odcid->len; |
| 5290 | memcpy(p, odcid->data, odcid->len); |
| 5291 | p += odcid->len; |
| 5292 | write_u32(p, htonl(timestamp)); |
| 5293 | p += sizeof timestamp; |
| 5294 | |
| 5295 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5296 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5297 | 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] | 5298 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5299 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5300 | |
| 5301 | p += QUIC_TLS_TAG_LEN; |
| 5302 | memcpy(p, salt, sizeof salt); |
| 5303 | p += sizeof salt; |
| 5304 | EVP_CIPHER_CTX_free(ctx); |
| 5305 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5306 | ret = p - buf; |
| 5307 | leave: |
| 5308 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5309 | return ret; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5310 | |
| 5311 | err: |
| 5312 | if (ctx) |
| 5313 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5314 | goto leave; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5317 | /* QUIC server only function. |
| 5318 | * Check the validity of the Retry token from <token> buffer with <tokenlen> |
| 5319 | * as length. If valid, the ODCID of <qc> QUIC connection will be put |
| 5320 | * into <odcid> connection ID. <dcid> is our side destination connection ID |
| 5321 | * of client source connection ID. |
| 5322 | * Return 1 if succeeded, 0 if not. |
| 5323 | */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5324 | static int quic_retry_token_check(const unsigned char *token, size_t tokenlen, |
| 5325 | const struct quic_version *qv, |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5326 | struct quic_cid *odcid, |
| 5327 | const struct quic_cid *dcid, |
| 5328 | struct quic_conn *qc, |
| 5329 | struct sockaddr_storage *addr) |
| 5330 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5331 | int ret = 0; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5332 | unsigned char buf[128]; |
| 5333 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 5334 | sizeof(struct in6_addr) + QUIC_HAP_CID_LEN]; |
| 5335 | size_t aadlen; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5336 | const unsigned char *salt; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5337 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5338 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5339 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5340 | size_t seclen = strlen(global.cluster_secret); |
| 5341 | EVP_CIPHER_CTX *ctx = NULL; |
| 5342 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5343 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5344 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5345 | |
| 5346 | if (sizeof buf < tokenlen) { |
| 5347 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 5348 | goto err; |
| 5349 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5350 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5351 | 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] | 5352 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 5353 | 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] | 5354 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5355 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 5356 | goto err; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 5357 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5358 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5359 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 5360 | 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] | 5361 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5362 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5363 | |
| 5364 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5365 | 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] | 5366 | ctx, aead, key, iv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5367 | 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] | 5368 | goto err; |
Frédéric Lécaille | 01d515e | 2022-06-07 11:39:00 +0200 | [diff] [blame] | 5369 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5370 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5371 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 5372 | 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] | 5373 | goto err; |
| 5374 | } |
| 5375 | |
| 5376 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5377 | |
| 5378 | ret = 1; |
| 5379 | leave: |
| 5380 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5381 | return ret; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5382 | |
| 5383 | err: |
| 5384 | if (ctx) |
| 5385 | EVP_CIPHER_CTX_free(ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5386 | goto leave; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5387 | } |
| 5388 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5389 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5390 | * the Initial <pkt> packet. |
| 5391 | * |
| 5392 | * Returns 0 on success else non-zero. |
| 5393 | */ |
| 5394 | 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] | 5395 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5396 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5397 | int ret = 0; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5398 | unsigned char buf[128]; |
| 5399 | int i = 0, token_len; |
| 5400 | const socklen_t addrlen = get_addr_len(addr); |
| 5401 | struct quic_cid scid; |
| 5402 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5403 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5404 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5405 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 5406 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 5407 | (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] | 5408 | /* version */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5409 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 5410 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 5411 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 5412 | buf[i++] = *(unsigned char *)&qv->num; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5413 | |
| 5414 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5415 | buf[i++] = pkt->scid.len; |
| 5416 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5417 | i += pkt->scid.len; |
| 5418 | |
| 5419 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5420 | scid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5421 | /* TODO: RAND_bytes() should be replaced */ |
| 5422 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 5423 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 5424 | goto out; |
| 5425 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5426 | |
| 5427 | buf[i++] = scid.len; |
| 5428 | memcpy(&buf[i], scid.data, scid.len); |
| 5429 | i += scid.len; |
| 5430 | |
| 5431 | /* token */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5432 | 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^] | 5433 | &pkt->dcid, &pkt->scid, addr))) { |
| 5434 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 5435 | goto out; |
| 5436 | } |
Frédéric Lécaille | cc2764e | 2022-03-23 14:09:09 +0100 | [diff] [blame] | 5437 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5438 | i += token_len; |
| 5439 | |
| 5440 | /* token integrity tag */ |
| 5441 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5442 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5443 | pkt->dcid.len, buf, i, qv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5444 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5445 | goto out; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | i += QUIC_TLS_TAG_LEN; |
| 5449 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5450 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 5451 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5452 | goto out; |
| 5453 | } |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5454 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5455 | ret = 1; |
| 5456 | out: |
| 5457 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5458 | return !ret; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5459 | } |
| 5460 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5461 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5462 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5463 | * concatenated to the <pkt> DCID field. |
| 5464 | * |
| 5465 | * Returns the instance or NULL if not found. |
| 5466 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 5467 | 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] | 5468 | struct listener *l, |
| 5469 | struct sockaddr_storage *saddr) |
| 5470 | { |
| 5471 | struct quic_conn *qc = NULL; |
| 5472 | struct ebmb_node *node; |
| 5473 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5474 | /* 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^] | 5475 | |
| 5476 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5477 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5478 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5479 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5480 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5481 | /* DCIDs of first packets coming from multiple clients may have |
| 5482 | * the same values. Let's distinguish them by concatenating the |
| 5483 | * socket addresses. |
| 5484 | */ |
| 5485 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5486 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5487 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5488 | if (node) { |
| 5489 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5490 | goto end; |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5495 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5496 | * used. |
| 5497 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5498 | 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] | 5499 | if (!node) |
| 5500 | goto end; |
| 5501 | |
| 5502 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5503 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5504 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5505 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5506 | * If already done, this is a noop. |
| 5507 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5508 | if (qc) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 5509 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5510 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5511 | end: |
| 5512 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5513 | return qc; |
| 5514 | } |
| 5515 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5516 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5517 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5518 | * parameters of this session. |
| 5519 | * This is the responsibility of the caller to check the validity of all the |
| 5520 | * pointers passed as parameter to this function. |
| 5521 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5522 | * CO_ER_SSL_NO_MEM. |
| 5523 | */ |
| 5524 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5525 | unsigned char *params, size_t params_len) |
| 5526 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5527 | int retry, ret = -1; |
| 5528 | |
| 5529 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5530 | |
| 5531 | retry = 1; |
| 5532 | retry: |
| 5533 | *ssl = SSL_new(ssl_ctx); |
| 5534 | if (!*ssl) { |
| 5535 | if (!retry--) |
| 5536 | goto err; |
| 5537 | |
| 5538 | pool_gc(NULL); |
| 5539 | goto retry; |
| 5540 | } |
| 5541 | |
| 5542 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5543 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5544 | SSL_free(*ssl); |
| 5545 | *ssl = NULL; |
| 5546 | if (!retry--) |
| 5547 | goto err; |
| 5548 | |
| 5549 | pool_gc(NULL); |
| 5550 | goto retry; |
| 5551 | } |
| 5552 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5553 | ret = 0; |
| 5554 | leave: |
| 5555 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5556 | return ret; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5557 | |
| 5558 | err: |
| 5559 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5560 | goto leave; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5561 | } |
| 5562 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5563 | /* Finalize <qc> QUIC connection: |
| 5564 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 5565 | * - derive the secrets for this context, |
| 5566 | * - encode the transport parameters to be sent, |
| 5567 | * - set them into the TLS stack, |
| 5568 | * - initialize ->max_ack_delay and max_idle_timeout, |
| 5569 | * |
| 5570 | * MUST be called after having received the remote transport parameters. |
| 5571 | * Return 1 if succeeded, 0 if not. |
| 5572 | */ |
| 5573 | int qc_conn_finalize(struct quic_conn *qc, int server) |
| 5574 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5575 | int ret = 0; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5576 | struct quic_transport_params *tx_tp = &qc->tx.params; |
| 5577 | struct quic_transport_params *rx_tp = &qc->rx.params; |
| 5578 | const struct quic_version *ver; |
| 5579 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5580 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5581 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5582 | if (tx_tp->version_information.negotiated_version && |
| 5583 | tx_tp->version_information.negotiated_version != qc->original_version) { |
| 5584 | qc->negotiated_version = |
| 5585 | qc->tx.params.version_information.negotiated_version; |
| 5586 | if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 5587 | qc->odcid.data, qc->odcid.len, !server)) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5588 | goto out; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5589 | |
| 5590 | ver = qc->negotiated_version; |
| 5591 | } |
| 5592 | else { |
| 5593 | ver = qc->original_version; |
| 5594 | } |
| 5595 | |
| 5596 | qc->enc_params_len = |
| 5597 | quic_transport_params_encode(qc->enc_params, |
| 5598 | qc->enc_params + sizeof qc->enc_params, |
| 5599 | &qc->rx.params, ver, 1); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5600 | if (!qc->enc_params_len) { |
| 5601 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT); |
| 5602 | goto out; |
| 5603 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5604 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5605 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 5606 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT); |
| 5607 | goto out; |
| 5608 | } |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5609 | |
| 5610 | if (tx_tp->max_ack_delay) |
| 5611 | qc->max_ack_delay = tx_tp->max_ack_delay; |
| 5612 | |
| 5613 | if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout) |
| 5614 | qc->max_idle_timeout = |
| 5615 | QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5616 | else |
| 5617 | qc->max_idle_timeout = |
| 5618 | QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5619 | |
| 5620 | TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp); |
| 5621 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5622 | ret = 1; |
| 5623 | out: |
| 5624 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5625 | return ret; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5626 | } |
| 5627 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5628 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5629 | * used to process <qc> received packets. The allocated context is stored in |
| 5630 | * <qc.xprt_ctx>. |
| 5631 | * |
| 5632 | * Returns 0 on success else non-zero. |
| 5633 | */ |
Frédéric Lécaille | 3fd92f6 | 2022-05-19 14:35:20 +0200 | [diff] [blame] | 5634 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5635 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5636 | int ret = 0; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5637 | struct bind_conf *bc = qc->li->bind_conf; |
| 5638 | struct ssl_sock_ctx *ctx = NULL; |
| 5639 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5640 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5641 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5642 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5643 | if (!ctx) { |
| 5644 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5645 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5646 | } |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5647 | |
| 5648 | ctx->wait_event.tasklet = tasklet_new(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5649 | if (!ctx->wait_event.tasklet) { |
| 5650 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5651 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5652 | } |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5653 | |
| 5654 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5655 | ctx->wait_event.tasklet->context = ctx; |
| 5656 | ctx->wait_event.events = 0; |
| 5657 | ctx->subs = NULL; |
| 5658 | ctx->xprt_ctx = NULL; |
| 5659 | ctx->qc = qc; |
| 5660 | |
| 5661 | /* Set tasklet tid based on the SCID selected by us for this |
| 5662 | * connection. The upper layer will also be binded on the same thread. |
| 5663 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 5664 | 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] | 5665 | |
| 5666 | if (qc_is_listener(qc)) { |
| 5667 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5668 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5669 | goto err; |
| 5670 | } |
| 5671 | |
| 5672 | /* Enabling 0-RTT */ |
| 5673 | if (bc->ssl_conf.early_data) |
| 5674 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5675 | |
| 5676 | SSL_set_accept_state(ctx->ssl); |
| 5677 | } |
| 5678 | |
| 5679 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5680 | |
| 5681 | /* Store the allocated context in <qc>. */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 5682 | qc->xprt_ctx = ctx; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5683 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5684 | ret = 1; |
| 5685 | leave: |
| 5686 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5687 | return !ret; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5688 | |
| 5689 | err: |
| 5690 | if (ctx && ctx->wait_event.tasklet) |
| 5691 | tasklet_free(ctx->wait_event.tasklet); |
| 5692 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5693 | goto leave; |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5694 | } |
| 5695 | |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5696 | /* Check that all the bytes between <buf> included and <end> address |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 5697 | * 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] | 5698 | * check that there is at least one byte between <buf> end <end>. |
| 5699 | * Return 1 if this all the bytes are null, 0 if not. |
| 5700 | */ |
| 5701 | static inline int quic_padding_check(const unsigned char *buf, |
| 5702 | const unsigned char *end) |
| 5703 | { |
| 5704 | while (buf < end && !*buf) |
| 5705 | buf++; |
| 5706 | |
| 5707 | return buf == end; |
| 5708 | } |
| 5709 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5710 | /* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the |
| 5711 | * end of this buffer past one byte and populate <pkt> RX packet structure |
| 5712 | * with the information collected from the packet. |
| 5713 | * This function sets ->len <pkt> field value to the end of the packet past one |
| 5714 | * 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] | 5715 | * the remaining QUIC packets carried by the datagram. |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 5716 | * Note that this function always sets this ->len value. If a paquet could |
| 5717 | * not be correctly found, ->len value will be set to the remaining number |
| 5718 | * bytes in the datagram to entirely consume this latter. |
| 5719 | */ |
| 5720 | static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
| 5721 | struct quic_rx_packet *pkt, int first_pkt, |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 5722 | struct quic_dgram *dgram, struct list **tasklist_head) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5723 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5724 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5725 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5726 | struct listener *l; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5727 | struct proxy *prx; |
| 5728 | struct quic_counters *prx_counters; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5729 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5730 | 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] | 5731 | size_t b_cspace; |
| 5732 | struct quic_enc_level *qel; |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5733 | uint32_t version; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5734 | const struct quic_version *qv = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5735 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5736 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 5737 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5738 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5739 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 5740 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 5741 | qel = NULL; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5742 | l = dgram->owner; |
| 5743 | prx = l->bind_conf->frontend; |
| 5744 | 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] | 5745 | /* This ist only to please to traces and distinguish the |
| 5746 | * packet with parsed packet number from others. |
| 5747 | */ |
| 5748 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5749 | if (end <= buf) { |
| 5750 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5751 | goto drop; |
| 5752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5753 | |
| 5754 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5755 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | f6954c5 | 2022-05-21 14:42:21 +0200 | [diff] [blame] | 5756 | if (!first_pkt && quic_padding_check(buf, end)) { |
| 5757 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 5758 | * That is what we called add padding out of QUIC packets. Such |
| 5759 | * datagrams must be considered as valid. But we can only consume |
| 5760 | * the remaining space. |
| 5761 | */ |
| 5762 | pkt->len = end - buf; |
| 5763 | goto drop_no_conn; |
| 5764 | } |
| 5765 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5766 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5767 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5768 | } |
| 5769 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5770 | /* Header form */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5771 | 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] | 5772 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 5773 | goto drop; |
| 5774 | } |
| 5775 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5776 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5777 | uint64_t len; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5778 | struct quic_cid odcid; |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5779 | int check_token = 0; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5780 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5781 | 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] | 5782 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5783 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5784 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5785 | } |
| 5786 | |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5787 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 5788 | 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] | 5789 | drop_no_conn = 1; |
Frédéric Lécaille | 3e26698 | 2022-04-27 15:37:28 +0200 | [diff] [blame] | 5790 | } |
| 5791 | else if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 5792 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5793 | 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] | 5794 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
Frédéric Lécaille | e7df68a | 2022-08-05 09:34:44 +0200 | [diff] [blame] | 5795 | goto drop; |
Frédéric Lécaille | 87373e7 | 2022-04-27 11:42:08 +0200 | [diff] [blame] | 5796 | } |
| 5797 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5798 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5799 | * they must have the same DCID. |
| 5800 | */ |
| 5801 | if (!first_pkt && |
| 5802 | (pkt->dcid.len != dgram->dcid_len || |
| 5803 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5804 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5805 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5806 | } |
| 5807 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5808 | /* 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] | 5809 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5810 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5811 | goto drop; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5812 | } |
| 5813 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5814 | /* RFC9000 6. Version Negotiation */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5815 | qv = qc_supported_version(version); |
| 5816 | if (!qv) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5817 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5818 | 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^] | 5819 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5820 | goto err; |
| 5821 | } |
| 5822 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5823 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5824 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 5825 | } |
| 5826 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5827 | /* For Initial packets, and for servers (QUIC clients connections), |
| 5828 | * there is no Initial connection IDs storage. |
| 5829 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5830 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5831 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5832 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5833 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 5834 | end - buf < token_len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5835 | TRACE_PROTO("Packet dropped", |
| 5836 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5837 | goto drop; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 5838 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5839 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 5840 | /* TODO Retry should be automatically activated if |
| 5841 | * suspect network usage is detected. |
| 5842 | */ |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5843 | if (global.cluster_secret) { |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5844 | if (!token_len) { |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5845 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5846 | TRACE_PROTO("Initial without token, sending retry", |
| 5847 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5848 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5849 | TRACE_PROTO("Error during Retry generation", |
| 5850 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5851 | goto err; |
| 5852 | } |
| 5853 | |
| 5854 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5855 | goto err; |
| 5856 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5857 | } |
| 5858 | else { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5859 | check_token = 1; |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5860 | } |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 5861 | } |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5862 | |
| 5863 | pkt->token = buf; |
| 5864 | pkt->token_len = token_len; |
| 5865 | buf += pkt->token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5866 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5867 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5868 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5869 | TRACE_PROTO("Packet dropped", |
| 5870 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5871 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5872 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5873 | } |
| 5874 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5875 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 5876 | end - buf < len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5877 | TRACE_PROTO("Packet dropped", |
| 5878 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5879 | goto drop; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5880 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5881 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5882 | payload = buf; |
| 5883 | pkt->len = len + payload - beg; |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5884 | if (drop_no_conn) |
| 5885 | goto drop_no_conn; |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 5886 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5887 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5888 | if (check_token && pkt->token) { |
| 5889 | if (*pkt->token == QUIC_TOKEN_FMT_RETRY) { |
| 5890 | const struct quic_version *ver = qc ? qc->original_version : qv; |
| 5891 | if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid, |
| 5892 | &pkt->scid, qc, &dgram->saddr)) { |
| 5893 | HA_ATOMIC_INC(&prx_counters->retry_error); |
| 5894 | TRACE_PROTO("Wrong retry token", |
| 5895 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5896 | /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection |
| 5897 | * with an INVALID_TOKEN error. |
| 5898 | */ |
| 5899 | goto drop; |
| 5900 | } |
| 5901 | |
| 5902 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5903 | } |
| 5904 | else { |
| 5905 | /* TODO: New token check */ |
| 5906 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 5907 | goto drop; |
| 5908 | } |
| 5909 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 5910 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5911 | int ipv4; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5912 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5913 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5914 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5915 | 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] | 5916 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5917 | } |
| 5918 | |
Willy Tarreau | 787e92a | 2022-05-20 16:06:01 +0200 | [diff] [blame] | 5919 | 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] | 5920 | 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] | 5921 | TRACE_PROTO("Initial without token, sending retry", |
| 5922 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5923 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5924 | TRACE_ERROR("Error during Retry generation", |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5925 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | dfd1301 | 2022-05-20 16:37:36 +0200 | [diff] [blame] | 5926 | goto err; |
| 5927 | } |
| 5928 | |
| 5929 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 5930 | goto err; |
| 5931 | } |
| 5932 | |
Frédéric Lécaille | 3f3ff47 | 2022-05-12 14:47:59 +0200 | [diff] [blame] | 5933 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5934 | * When an Initial packet is sent by a client that has not previously |
| 5935 | * received an Initial or Retry packet from the server, the client |
| 5936 | * populates the Destination Connection ID field with an unpredictable |
| 5937 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5938 | */ |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5939 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 5940 | TRACE_PROTO("dropped packet", |
| 5941 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5942 | goto drop; |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 5943 | } |
| 5944 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5945 | pkt->saddr = dgram->saddr; |
| 5946 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5947 | |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 5948 | qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, &odcid, |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 5949 | &pkt->saddr, 1, !!pkt->token_len, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5950 | if (qc == NULL) |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5951 | goto drop; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5952 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 5953 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 5954 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5955 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5956 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5957 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5958 | /* If the insertion failed, it means that another |
| 5959 | * thread has already allocated a QUIC connection for |
| 5960 | * the same CID. Liberate our allocated connection. |
| 5961 | */ |
| 5962 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5963 | qc_to_purge = qc; |
| 5964 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 5965 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 5966 | pkt->qc = qc; |
| 5967 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5968 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5969 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5970 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 5971 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5972 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5973 | else { |
Frédéric Lécaille | f293b69 | 2022-03-08 16:59:54 +0100 | [diff] [blame] | 5974 | quic_conn_release(qc_to_purge); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 5975 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5976 | } |
| 5977 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 5978 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5979 | } |
| 5980 | } |
| 5981 | else { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 5982 | 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] | 5983 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5984 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5985 | goto drop; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5986 | } |
| 5987 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5988 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5989 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5990 | |
| 5991 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 5992 | * they must have the same DCID. |
| 5993 | */ |
| 5994 | if (!first_pkt && |
| 5995 | (pkt->dcid.len != dgram->dcid_len || |
| 5996 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 5997 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 5998 | goto drop; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5999 | } |
| 6000 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6001 | buf += QUIC_HAP_CID_LEN; |
| 6002 | |
| 6003 | /* A short packet is the last one of a UDP datagram. */ |
| 6004 | payload = buf; |
| 6005 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6006 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6007 | 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] | 6008 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6009 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 6010 | 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] | 6011 | 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^] | 6012 | 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] | 6013 | goto drop; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 6014 | } |
| 6015 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 6016 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6017 | } |
| 6018 | |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6019 | if (qc->flags & QUIC_FL_CONN_CLOSING) { |
| 6020 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6021 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6022 | qc->nb_pkt_for_cc++; |
| 6023 | qc->nb_pkt_since_cc = 0; |
| 6024 | } |
| 6025 | /* Skip the entire datagram */ |
| 6026 | pkt->len = end - beg; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6027 | TRACE_STATE("Closing state connection", |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6028 | QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv); |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6029 | goto drop; |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6030 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6031 | |
| 6032 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6033 | * they must have the same DCID. |
| 6034 | * |
| 6035 | * This check must be done after the final update to pkt.len to |
| 6036 | * properly drop the packet on failure. |
| 6037 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6038 | if (first_pkt && !quic_peer_validated_addr(qc) && |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6039 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6040 | 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] | 6041 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6042 | /* Reset the anti-amplification bit. It will be set again |
| 6043 | * when sending the next packet if reached again. |
| 6044 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6045 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 6046 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6047 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6048 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 6049 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 6050 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6051 | |
Amaury Denoyelle | 72d8650 | 2022-07-13 15:08:23 +0200 | [diff] [blame] | 6052 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6053 | TRACE_PROTO("Connection error", |
| 6054 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6055 | goto out; |
| 6056 | } |
| 6057 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6058 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 6059 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6060 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6061 | if (b_cspace < pkt->len) { |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 6062 | /* Do not consume buf if space not at the end. */ |
| 6063 | 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] | 6064 | TRACE_PROTO("Packet dropped", |
| 6065 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 6066 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 6067 | goto drop_no_conn; |
Amaury Denoyelle | 80d0572 | 2022-05-16 18:13:56 +0200 | [diff] [blame] | 6068 | } |
| 6069 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6070 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 6071 | if (b_cspace) { |
| 6072 | b_putchr(&qc->rx.buf, 0x00); |
| 6073 | b_cspace--; |
| 6074 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6075 | b_add(&qc->rx.buf, b_cspace); |
| 6076 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6077 | TRACE_PROTO("Too big packet", |
| 6078 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Frédéric Lécaille | 45a1629 | 2022-06-29 12:03:34 +0200 | [diff] [blame] | 6079 | HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun); |
| 6080 | goto drop_no_conn; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 6081 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6082 | } |
| 6083 | |
Frédéric Lécaille | 99897d1 | 2022-08-08 10:28:07 +0200 | [diff] [blame] | 6084 | 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] | 6085 | 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] | 6086 | goto drop; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 6087 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6088 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6089 | 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] | 6090 | if (pkt->aad_len) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6091 | qc_pkt_insert(qc, pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6092 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 6093 | /* Wake up the connection packet handler task from here only if all |
| 6094 | * the contexts have been initialized, especially the mux context |
| 6095 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 6096 | * will start it if these contexts for the connection are not already |
| 6097 | * initialized. |
| 6098 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6099 | conn_ctx = qc->xprt_ctx; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 6100 | if (conn_ctx) |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 6101 | *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] | 6102 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6103 | drop_no_conn: |
| 6104 | if (drop_no_conn) |
| 6105 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6106 | 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] | 6107 | |
Frédéric Lécaille | 4646cf3 | 2022-04-27 15:09:53 +0200 | [diff] [blame] | 6108 | return; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6109 | |
Frédéric Lécaille | cbd59c7 | 2022-05-20 08:11:26 +0200 | [diff] [blame] | 6110 | drop: |
| 6111 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6112 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 6113 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 6114 | * This cannot be done by this thread. |
| 6115 | */ |
| 6116 | if (io_cb_wakeup) { |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 6117 | conn_ctx = qc->xprt_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 6118 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 6119 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 6120 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 6121 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 6122 | if (!pkt->len) |
| 6123 | pkt->len = end - beg; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6124 | 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] | 6125 | } |
| 6126 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6127 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6128 | * 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] | 6129 | */ |
| 6130 | 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] | 6131 | int type, size_t pn_len, |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6132 | struct quic_conn *qc, const struct quic_version *ver) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6133 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6134 | int ret = 0; |
| 6135 | |
| 6136 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6137 | |
| 6138 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6139 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6140 | goto leave; |
| 6141 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6142 | |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6143 | type = quic_pkt_type(type, ver->num); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6144 | /* #0 byte flags */ |
| 6145 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6146 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6147 | /* Version */ |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6148 | quic_write_uint32(buf, end, ver->num); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6149 | *(*buf)++ = qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6150 | /* Destination connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6151 | if (qc->dcid.len) { |
| 6152 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6153 | *buf += qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6154 | } |
| 6155 | /* Source connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6156 | *(*buf)++ = qc->scid.len; |
| 6157 | if (qc->scid.len) { |
| 6158 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6159 | *buf += qc->scid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6160 | } |
| 6161 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6162 | ret = 1; |
| 6163 | leave: |
| 6164 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6165 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6166 | } |
| 6167 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6168 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6169 | * 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] | 6170 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6171 | 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^] | 6172 | size_t pn_len, struct quic_conn *qc, |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6173 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6174 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6175 | int ret = 0; |
| 6176 | |
| 6177 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6178 | |
| 6179 | if (end - *buf < 1 + qc->dcid.len) { |
| 6180 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6181 | goto leave; |
| 6182 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6183 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6184 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 6185 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6186 | ((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] | 6187 | /* Destination connection ID */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6188 | if (qc->dcid.len) { |
| 6189 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6190 | *buf += qc->dcid.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6191 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6192 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6193 | ret = 1; |
| 6194 | leave: |
| 6195 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6196 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6200 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6201 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6202 | * Returns 1 if succeeded or 0 if failed. |
| 6203 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6204 | static int quic_apply_header_protection(struct quic_conn *qc, |
| 6205 | unsigned char *buf, unsigned char *pn, size_t pnlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6206 | const EVP_CIPHER *aead, const unsigned char *key) |
| 6207 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6208 | int i, outlen, ret = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6209 | EVP_CIPHER_CTX *ctx; |
| 6210 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6211 | * and at most 4 bytes for the packet number |
| 6212 | */ |
| 6213 | unsigned char mask[5] = {0}; |
| 6214 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6215 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6216 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6217 | ctx = EVP_CIPHER_CTX_new(); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6218 | if (!ctx) { |
| 6219 | TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 6220 | goto out; |
| 6221 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6222 | |
| 6223 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 6224 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6225 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) { |
| 6226 | TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6227 | goto out; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6228 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6229 | |
| 6230 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6231 | for (i = 0; i < pnlen; i++) |
| 6232 | pn[i] ^= mask[i + 1]; |
| 6233 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6234 | EVP_CIPHER_CTX_free(ctx); |
| 6235 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6236 | ret = 1; |
| 6237 | out: |
| 6238 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6239 | return ret; |
| 6240 | } |
| 6241 | |
| 6242 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6243 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6244 | * Return 1 if succeeded, 0 if not. |
| 6245 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6246 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6247 | struct quic_frame *ack_frm, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6248 | { |
| 6249 | size_t room, ack_delay_sz; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6250 | int ret = 0; |
| 6251 | |
| 6252 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6253 | |
| 6254 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6255 | /* A frame is made of 1 byte for the frame type. */ |
| 6256 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6257 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6258 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6259 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6260 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6261 | leave: |
| 6262 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6263 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6264 | } |
| 6265 | |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6266 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6267 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6268 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6269 | * with the number of bytes already present in this buffer which must be taken |
| 6270 | * into an account for the Length packet field value. <headlen> is the number of |
| 6271 | * bytes already present in this packet before building frames. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6272 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6273 | * 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] | 6274 | * 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] | 6275 | * 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] | 6276 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6277 | 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] | 6278 | size_t room, size_t *len, size_t headlen, |
| 6279 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6280 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6281 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6282 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6283 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6284 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6285 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 6286 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6287 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6288 | if (*len > room) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6289 | goto leave; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6290 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6291 | /* If we are not probing we must take into an account the congestion |
| 6292 | * control window. |
| 6293 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6294 | if (!qel->pktns->tx.pto_probe) { |
| 6295 | size_t remain = quic_path_prep_data(qc->path); |
| 6296 | |
| 6297 | if (headlen > remain) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6298 | goto leave; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 6299 | |
| 6300 | room = QUIC_MIN(room, remain - headlen); |
| 6301 | } |
| 6302 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6303 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6304 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6305 | |
| 6306 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 6307 | * returned by this function only if at least one frame could be built |
| 6308 | * in the switch/case block. |
| 6309 | */ |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6310 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6311 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6312 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6313 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6314 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6315 | break; |
| 6316 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6317 | switch (cf->type) { |
| 6318 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6319 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6320 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6321 | /* Compute the length of this CRYPTO frame header */ |
| 6322 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 6323 | /* Compute the data length of this CRyPTO frame. */ |
| 6324 | 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^] | 6325 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6326 | 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] | 6327 | if (!dlen) |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6328 | continue; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6329 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6330 | /* CRYPTO frame length. */ |
| 6331 | flen = hlen + quic_int_getsize(dlen) + dlen; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6332 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6333 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6334 | /* Add the CRYPTO data length and its encoded length to the packet |
| 6335 | * length and the length of this length. |
| 6336 | */ |
| 6337 | *len += flen; |
| 6338 | room -= flen; |
| 6339 | if (dlen == cf->crypto.len) { |
| 6340 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6341 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6342 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6343 | } |
| 6344 | else { |
| 6345 | struct quic_frame *new_cf; |
| 6346 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6347 | new_cf = pool_zalloc(pool_head_quic_frame); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6348 | if (!new_cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6349 | 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] | 6350 | continue; |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6351 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6352 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6353 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6354 | new_cf->type = QUIC_FT_CRYPTO; |
| 6355 | new_cf->crypto.len = dlen; |
| 6356 | new_cf->crypto.offset = cf->crypto.offset; |
| 6357 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6358 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6359 | /* Consume <dlen> bytes of the current frame. */ |
| 6360 | cf->crypto.len -= dlen; |
| 6361 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6362 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6363 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6364 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6365 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6366 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 6367 | struct eb64_node *node = NULL; |
| 6368 | struct qc_stream_desc *stream_desc = NULL; |
| 6369 | struct quic_stream *strm = &cf->stream; |
| 6370 | |
| 6371 | /* As this frame has been already lost, ensure the stream is always |
| 6372 | * available or the range of this frame is not consumed before |
| 6373 | * resending it. |
| 6374 | */ |
| 6375 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 6376 | if (!node) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6377 | 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] | 6378 | LIST_DELETE(&cf->list); |
| 6379 | pool_free(pool_head_quic_frame, cf); |
| 6380 | continue; |
| 6381 | } |
| 6382 | |
| 6383 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 6384 | 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^] | 6385 | TRACE_DEVEL("ignored frame frame in already acked range", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 6386 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6387 | LIST_DELETE(&cf->list); |
| 6388 | pool_free(pool_head_quic_frame, cf); |
| 6389 | continue; |
| 6390 | } |
| 6391 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 6392 | strm->offset.key = stream_desc->ack_offset; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6393 | TRACE_DEVEL("updated partially acked frame", |
Frédéric Lécaille | fdc1b96 | 2022-05-31 12:04:42 +0200 | [diff] [blame] | 6394 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Frédéric Lécaille | 77cb38d | 2022-04-27 07:17:56 +0200 | [diff] [blame] | 6395 | } |
| 6396 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6397 | /* Note that these frames are accepted in short packets only without |
| 6398 | * "Length" packet field. Here, <*len> is used only to compute the |
| 6399 | * 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] | 6400 | * |
| 6401 | * 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] | 6402 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 6403 | */ |
| 6404 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6405 | ((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] | 6406 | /* Compute the data length of this STREAM frame. */ |
| 6407 | avail_room = room - hlen - *len; |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6408 | if ((ssize_t)avail_room <= 0) |
| 6409 | continue; |
| 6410 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6411 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
Frédéric Lécaille | adc7641 | 2022-08-08 16:09:46 +0200 | [diff] [blame] | 6412 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6413 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 6414 | dlen = max_available_room(avail_room, &dlen_sz); |
| 6415 | if (dlen > cf->stream.len) { |
| 6416 | dlen = cf->stream.len; |
| 6417 | } |
| 6418 | dlen_sz = quic_int_getsize(dlen); |
| 6419 | flen = hlen + dlen_sz + dlen; |
| 6420 | } |
| 6421 | else { |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 6422 | 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] | 6423 | flen = hlen + dlen; |
| 6424 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6425 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6426 | 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^] | 6427 | TRACE_DEVEL(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6428 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6429 | /* Add the STREAM data length and its encoded length to the packet |
| 6430 | * length and the length of this length. |
| 6431 | */ |
| 6432 | *len += flen; |
| 6433 | room -= flen; |
| 6434 | if (dlen == cf->stream.len) { |
| 6435 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6436 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6437 | LIST_APPEND(outlist, &cf->list); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6438 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6439 | /* The MUX stream might be released at this |
| 6440 | * stage. This can most notably happen on |
| 6441 | * retransmission. |
| 6442 | */ |
| 6443 | if (qc->mux_state == QC_MUX_READY && |
| 6444 | !cf->stream.stream->release) { |
| 6445 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 6446 | cf->stream.len, |
| 6447 | cf->stream.offset.key); |
| 6448 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6449 | } |
| 6450 | else { |
| 6451 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6452 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6453 | |
| 6454 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 6455 | if (!new_cf) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6456 | 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] | 6457 | continue; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6458 | } |
| 6459 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 6460 | LIST_INIT(&new_cf->reflist); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6461 | new_cf->type = cf->type; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6462 | new_cf->stream.stream = cf->stream.stream; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6463 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6464 | new_cf->stream.id = cf->stream.id; |
| 6465 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 6466 | new_cf->stream.offset = cf->stream.offset; |
| 6467 | new_cf->stream.len = dlen; |
| 6468 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 6469 | /* FIN bit reset */ |
| 6470 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 6471 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6472 | LIST_APPEND(outlist, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6473 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 6474 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6475 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 6476 | b_size(cf->stream.buf), |
| 6477 | (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] | 6478 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 6479 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 6480 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6481 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 6482 | /* The MUX stream might be released at this |
| 6483 | * stage. This can most notably happen on |
| 6484 | * retransmission. |
| 6485 | */ |
| 6486 | if (qc->mux_state == QC_MUX_READY && |
| 6487 | !cf->stream.stream->release) { |
| 6488 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 6489 | new_cf->stream.len, |
| 6490 | new_cf->stream.offset.key); |
| 6491 | } |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 6492 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 6493 | |
| 6494 | /* TODO the MUX is notified about the frame sending via |
| 6495 | * previous qcc_streams_sent_done call. However, the |
| 6496 | * sending can fail later, for example if the sendto |
| 6497 | * system call returns an error. As the MUX has been |
| 6498 | * notified, the transport layer is responsible to |
| 6499 | * bufferize and resent the announced data later. |
| 6500 | */ |
| 6501 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6502 | break; |
| 6503 | |
| 6504 | default: |
| 6505 | flen = qc_frm_len(cf); |
| 6506 | BUG_ON(!flen); |
| 6507 | if (flen > room) |
| 6508 | continue; |
| 6509 | |
| 6510 | *len += flen; |
| 6511 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 6512 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6513 | LIST_APPEND(outlist, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 6514 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6515 | } |
Frédéric Lécaille | 573b56b | 2022-04-25 15:42:56 +0200 | [diff] [blame] | 6516 | |
| 6517 | /* 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] | 6518 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6519 | } |
| 6520 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6521 | leave: |
| 6522 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6523 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6524 | } |
| 6525 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6526 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6527 | * is used as return parameter and should be zero'ed by the caller. |
| 6528 | */ |
| 6529 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6530 | struct quic_frame *out) |
| 6531 | { |
| 6532 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6533 | * |
| 6534 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6535 | * different encryption levels depending on the client context. This is |
| 6536 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6537 | * more details on how to implement it. |
| 6538 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6539 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 6540 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6541 | |
| 6542 | if (qc->err.app) { |
| 6543 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6544 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6545 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6546 | * |
| 6547 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6548 | * packet could expose application state or be used to alter application |
| 6549 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 6550 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 6551 | * Handshake packets. Otherwise, information about the application |
| 6552 | * state might be revealed. Endpoints MUST clear the value of the |
| 6553 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 6554 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 6555 | */ |
| 6556 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6557 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 6558 | out->connection_close.reason_phrase_len = 0; |
| 6559 | } |
| 6560 | else { |
| 6561 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 6562 | out->connection_close.error_code = qc->err.code; |
| 6563 | } |
| 6564 | } |
| 6565 | else { |
| 6566 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6567 | out->connection_close.error_code = qc->err.code; |
| 6568 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6569 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 6570 | |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6571 | } |
| 6572 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6573 | /* 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] | 6574 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 6575 | * 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] | 6576 | * filling the buffer with as much frames as possible from <frms> list of |
| 6577 | * prebuilt frames. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6578 | * 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] | 6579 | * 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] | 6580 | * having returned from this function. |
| 6581 | * 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] | 6582 | * number field in this packet. <pn_len> will also have the packet number |
| 6583 | * length as value. |
| 6584 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6585 | * 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] | 6586 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6587 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 6588 | size_t dglen, struct quic_tx_packet *pkt, |
| 6589 | 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] | 6590 | int force_ack, int padding, int cc, int probe, |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6591 | struct quic_enc_level *qel, struct quic_conn *qc, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6592 | const struct quic_version *ver, struct list *frms) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6593 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6594 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6595 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6596 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 6597 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 6598 | struct quic_frame cc_frm = { }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6599 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6600 | int64_t rx_largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6601 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6602 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6603 | struct quic_frame *cf; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6604 | int must_ack, ret = 0; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6605 | int nb_aepkts_since_last_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6606 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6607 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6608 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6609 | /* Length field value with CRYPTO frames if present. */ |
| 6610 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6611 | beg = pos; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6612 | /* When not probing, and no immediate close is required, reduce the size of this |
| 6613 | * buffer to respect the congestion controller window. |
| 6614 | * 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] | 6615 | */ |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6616 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6617 | size_t path_room; |
| 6618 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6619 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6620 | if (end - beg > path_room) |
| 6621 | end = beg + path_room; |
| 6622 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6623 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6624 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6625 | * length field if any. |
| 6626 | */ |
| 6627 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6628 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6629 | goto no_room; |
| 6630 | |
| 6631 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6632 | 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] | 6633 | /* packet number length */ |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6634 | *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] | 6635 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6636 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6637 | !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] | 6638 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6639 | !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] | 6640 | goto no_room; |
| 6641 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6642 | /* Encode the token length (0) for an Initial packet. */ |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6643 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6644 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6645 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6646 | /* Build an ACK frame if required. */ |
| 6647 | ack_frm_len = 0; |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6648 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 6649 | must_ack = !qel->pktns->tx.pto_probe && |
| 6650 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 6651 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 6652 | if (force_ack || must_ack) { |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 6653 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6654 | 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] | 6655 | ack_frm.tx_ack.arngs = arngs; |
| 6656 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 6657 | qel->pktns->tx.ack_delay = |
| 6658 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 6659 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 6660 | } |
| 6661 | 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] | 6662 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 6663 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 6664 | * that from here, we do not know if we will have to send a PING frame. |
| 6665 | * This will be decided after having computed the ack-eliciting frames |
| 6666 | * to be added to this packet. |
| 6667 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6668 | 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] | 6669 | if (!ack_frm_len) |
| 6670 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6671 | } |
| 6672 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6673 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6674 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 6675 | len_frms = 0; |
Frédéric Lécaille | 28c7ea3 | 2022-02-25 17:41:39 +0100 | [diff] [blame] | 6676 | if (!cc && !LIST_ISEMPTY(frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6677 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 6678 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6679 | 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] | 6680 | /* Initialize the length of the frames built below to <len>. |
| 6681 | * If any frame could be successfully built by qc_build_frms(), |
| 6682 | * we will have len_frms > len. |
| 6683 | */ |
| 6684 | len_frms = len; |
Frédéric Lécaille | edc8146 | 2022-02-25 17:44:29 +0100 | [diff] [blame] | 6685 | if (!qc_build_frms(&frm_list, frms, |
| 6686 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6687 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6688 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 834399c | 2022-04-25 17:17:07 +0200 | [diff] [blame] | 6689 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 6690 | goto no_room; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6691 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6692 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6693 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6694 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 6695 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 6696 | * for the encryption tag. It must be taken into an account for the length |
| 6697 | * of this packet. |
| 6698 | */ |
| 6699 | if (len_frms) |
| 6700 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 6701 | else |
| 6702 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6703 | /* CONNECTION_CLOSE frame */ |
| 6704 | if (cc) { |
Amaury Denoyelle | 0933c7b | 2022-07-18 14:11:50 +0200 | [diff] [blame] | 6705 | qc_build_cc_frm(qc, qel, &cc_frm); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6706 | len += qc_frm_len(&cc_frm); |
| 6707 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6708 | add_ping_frm = 0; |
| 6709 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 6710 | len_sz = quic_int_getsize(len); |
| 6711 | /* Add this packet size to <dglen> */ |
| 6712 | dglen += head_len + len_sz + len; |
| 6713 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 6714 | /* This is a maximum padding size */ |
| 6715 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 6716 | /* The length field value is of this packet is <len> + <padding_len> |
| 6717 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 6718 | * <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] | 6719 | * sizes from <padding_len>. |
| 6720 | */ |
| 6721 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 6722 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6723 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6724 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6725 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6726 | /* 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] | 6727 | add_ping_frm = 1; |
| 6728 | len += 1; |
| 6729 | } |
| 6730 | /* 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] | 6731 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6732 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 6733 | } |
| 6734 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6735 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 6736 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6737 | |
| 6738 | /* Packet number field address. */ |
| 6739 | *buf_pn = pos; |
| 6740 | |
| 6741 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6742 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 6743 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6744 | |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6745 | if (ack_frm_len) { |
| 6746 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 6747 | goto no_room; |
| 6748 | |
| 6749 | 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] | 6750 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6751 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6752 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6753 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6754 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6755 | list_for_each_entry(cf, &frm_list, list) { |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6756 | unsigned char *spos = pos; |
| 6757 | |
| 6758 | if (!qc_build_frm(&spos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6759 | ssize_t room = end - pos; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6760 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 6761 | qc, NULL, NULL, &room); |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6762 | /* TODO: this should not have happened if qc_build_frms() |
| 6763 | * had correctly computed and sized the frames to be |
| 6764 | * added to this packet. Note that <cf> was added |
| 6765 | * from <frm_list> to <frms> list by qc_build_frms(). |
| 6766 | */ |
| 6767 | LIST_DELETE(&cf->list); |
| 6768 | LIST_INSERT(frms, &cf->list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6769 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 6770 | } |
Frédéric Lécaille | dcc74ff | 2022-03-18 17:49:29 +0100 | [diff] [blame] | 6771 | |
| 6772 | pos = spos; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6773 | quic_tx_packet_refinc(pkt); |
| 6774 | cf->pkt = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | |
| 6778 | /* Build a PING frame if needed. */ |
| 6779 | if (add_ping_frm) { |
| 6780 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6781 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6782 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6783 | } |
| 6784 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6785 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Frédéric Lécaille | 59bf255 | 2022-03-28 12:13:09 +0200 | [diff] [blame] | 6786 | if (cc) { |
| 6787 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 6788 | goto no_room; |
| 6789 | |
| 6790 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 6791 | } |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 6792 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6793 | /* Build a PADDING frame if needed. */ |
| 6794 | if (padding_len) { |
| 6795 | frm.type = QUIC_FT_PADDING; |
| 6796 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 6797 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6798 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6799 | } |
| 6800 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6801 | /* If this packet is ack-eliciting and we are probing let's |
| 6802 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6803 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 6804 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 6805 | qel->pktns->tx.pto_probe) |
| 6806 | qel->pktns->tx.pto_probe--; |
| 6807 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6808 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6809 | LIST_SPLICE(&pkt->frms, &frm_list); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6810 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6811 | ret = 1; |
| 6812 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 6813 | leave: |
| 6814 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6815 | return ret; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6816 | |
| 6817 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 6818 | /* 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] | 6819 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6820 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 6821 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6822 | } |
| 6823 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6824 | 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] | 6825 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6826 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6827 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6828 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 6829 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6830 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6831 | pkt->time_sent = TICK_ETERNITY; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6832 | pkt->next = NULL; |
Frédéric Lécaille | 141982a | 2022-03-15 18:44:20 +0100 | [diff] [blame] | 6833 | pkt->largest_acked_pn = -1; |
Frédéric Lécaille | 2899fe2 | 2022-03-21 10:43:53 +0100 | [diff] [blame] | 6834 | pkt->flags = 0; |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6835 | pkt->refcnt = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6836 | } |
| 6837 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6838 | /* 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] | 6839 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 6840 | * of prebuilt frames. |
| 6841 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6842 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 6843 | * -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] | 6844 | * XXX NOTE XXX |
| 6845 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 6846 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 6847 | * control window limitation. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6848 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 6849 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 6850 | const unsigned char *buf_end, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 6851 | struct quic_enc_level *qel, |
| 6852 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 6853 | struct quic_conn *qc, const struct quic_version *ver, |
Frédéric Lécaille | 77ac6f5 | 2022-06-21 15:14:59 +0200 | [diff] [blame] | 6854 | size_t dglen, int pkt_type, int force_ack, |
| 6855 | int padding, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6856 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6857 | struct quic_tx_packet *ret_pkt = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6858 | /* The pointer to the packet number field. */ |
| 6859 | unsigned char *buf_pn; |
| 6860 | unsigned char *beg, *end, *payload; |
| 6861 | int64_t pn; |
| 6862 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6863 | struct quic_tx_packet *pkt; |
| 6864 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6865 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6866 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6867 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 6868 | if (!pkt) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6869 | 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] | 6870 | *err = -2; |
| 6871 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6872 | } |
| 6873 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 6874 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6875 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6876 | pn_len = 0; |
| 6877 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6878 | |
| 6879 | pn = qel->pktns->tx.next_pn + 1; |
| 6880 | 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] | 6881 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6882 | // trace already emitted by function above |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 6883 | *err = -1; |
| 6884 | goto err; |
| 6885 | } |
| 6886 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6887 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6888 | payload = buf_pn + pn_len; |
| 6889 | payload_len = end - payload; |
| 6890 | aad_len = payload - beg; |
| 6891 | |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 6892 | 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^] | 6893 | // trace already emitted by function above |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6894 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6895 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6896 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6897 | |
| 6898 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6899 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6900 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6901 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6902 | // trace already emitted by function above |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6903 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6904 | goto err; |
| 6905 | } |
| 6906 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 6907 | /* Consume a packet number */ |
| 6908 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 6909 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6910 | 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] | 6911 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6912 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 6913 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6914 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 6915 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6916 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 6917 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6918 | /* 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] | 6919 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 6920 | pkt->in_flight_len = pkt->len; |
| 6921 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 6922 | } |
Frédéric Lécaille | 4775680 | 2022-03-25 09:12:16 +0100 | [diff] [blame] | 6923 | /* Always reset this flags */ |
| 6924 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Frédéric Lécaille | b002145 | 2022-03-29 11:42:03 +0200 | [diff] [blame] | 6925 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 6926 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 6927 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 6928 | } |
Frédéric Lécaille | 205e4f3 | 2022-03-28 17:38:27 +0200 | [diff] [blame] | 6929 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6930 | pkt->pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6931 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6932 | ret_pkt = pkt; |
| 6933 | leave: |
| 6934 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 6935 | return ret_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6936 | |
| 6937 | err: |
Frédéric Lécaille | e2a1c1b | 2022-03-17 11:28:10 +0100 | [diff] [blame] | 6938 | /* TODO: what about the frames which have been built |
| 6939 | * for this packet. |
| 6940 | */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6941 | free_quic_tx_packet(qc, pkt); |
| 6942 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6943 | } |
| 6944 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 6945 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6946 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6947 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6948 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6949 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 6950 | */ |
| 6951 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6952 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6953 | struct quic_conn *qc = conn->handle.qc; |
| 6954 | struct qcc *qcc = qc->qcc; |
| 6955 | |
| 6956 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6957 | |
| 6958 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6959 | BUG_ON(qcc->subs && qcc->subs != es); |
| 6960 | |
| 6961 | es->events |= event_type; |
| 6962 | qcc->subs = es; |
| 6963 | |
| 6964 | if (event_type & SUB_RETRY_RECV) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6965 | 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] | 6966 | |
| 6967 | if (event_type & SUB_RETRY_SEND) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6968 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc); |
| 6969 | |
| 6970 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 6971 | |
| 6972 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6976 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6977 | * subscribe() call. It always returns zero. |
| 6978 | */ |
| 6979 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 6980 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 6981 | int ret; |
| 6982 | struct quic_conn *qc = conn->handle.qc; |
| 6983 | struct qcc *qcc = qc->qcc; |
| 6984 | |
| 6985 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
| 6986 | |
| 6987 | if (event_type & SUB_RETRY_RECV) |
| 6988 | TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc); |
| 6989 | if (event_type & SUB_RETRY_SEND) |
| 6990 | TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc); |
| 6991 | |
| 6992 | ret = conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 6993 | |
| 6994 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
| 6995 | |
| 6996 | return ret; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 6997 | } |
| 6998 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 6999 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 7000 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7001 | */ |
| 7002 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 7003 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 7004 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7005 | |
| 7006 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 7007 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 7008 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7009 | if (*xprt_ctx) |
| 7010 | goto out; |
| 7011 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7012 | *xprt_ctx = conn->handle.qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7013 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7014 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 7015 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7016 | |
| 7017 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7018 | } |
| 7019 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7020 | /* Start the QUIC transport layer */ |
| 7021 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 7022 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7023 | int ret = 0; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7024 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 7025 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7026 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7027 | qc = conn->handle.qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7028 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 7029 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 7030 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 5d774de | 2022-04-14 14:59:35 +0200 | [diff] [blame] | 7031 | /* prepare a CONNECTION_CLOSE frame */ |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 7032 | 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^] | 7033 | goto out; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* mux-quic can now be considered ready. */ |
| 7037 | qc->mux_state = QC_MUX_READY; |
| 7038 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7039 | tasklet_wakeup(qctx->wait_event.tasklet); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7040 | |
| 7041 | ret = 1; |
| 7042 | out: |
| 7043 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 7044 | return ret; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7045 | } |
| 7046 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7047 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 7048 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7049 | 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] | 7050 | return NULL; |
| 7051 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 7052 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7053 | } |
| 7054 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7055 | /* transport-layer operations for QUIC connections. */ |
| 7056 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 7057 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 7058 | .subscribe = quic_conn_subscribe, |
| 7059 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7060 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 7061 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7062 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 7063 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 7064 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 7065 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7066 | .name = "QUIC", |
| 7067 | }; |
| 7068 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7069 | static void __quic_conn_init(void) |
| 7070 | { |
| 7071 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7072 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 7073 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7074 | INITCALL0(STG_REGISTER, __quic_conn_init); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7075 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7076 | static void __quic_conn_deinit(void) |
| 7077 | { |
| 7078 | BIO_meth_free(ha_quic_meth); |
| 7079 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7080 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7081 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 7082 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 7083 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 7084 | * 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] | 7085 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7086 | 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] | 7087 | { |
| 7088 | unsigned char *pos; |
| 7089 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7090 | struct quic_dghdlr *dghdlr = ctx; |
| 7091 | struct quic_dgram *dgram; |
| 7092 | int first_pkt = 1; |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 7093 | struct list *tasklist_head = NULL; |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7094 | int max_dgrams = global.tune.maxpollevents; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7095 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7096 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7097 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7098 | 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] | 7099 | pos = dgram->buf; |
| 7100 | end = pos + dgram->len; |
| 7101 | do { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7102 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7103 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7104 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7105 | if (!pkt) { |
| 7106 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7107 | goto leave; |
| 7108 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7109 | |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 7110 | LIST_INIT(&pkt->qc_rx_pkt_list); |
Frédéric Lécaille | 7cc8b31 | 2022-05-05 12:04:28 +0200 | [diff] [blame] | 7111 | pkt->time_received = now_ms; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7112 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 7113 | 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] | 7114 | first_pkt = 0; |
| 7115 | pos += pkt->len; |
| 7116 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 23f908c | 2022-06-20 10:58:03 +0200 | [diff] [blame] | 7117 | |
| 7118 | /* Free rejected packets */ |
| 7119 | if (!pkt->refcnt) { |
| 7120 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7121 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7122 | } |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7123 | } while (pos < end); |
| 7124 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7125 | /* Increasing the received bytes counter by the UDP datagram length |
| 7126 | * if this datagram could be associated to a connection. |
| 7127 | */ |
| 7128 | if (dgram->qc) |
| 7129 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 7130 | |
| 7131 | /* Mark this datagram as consumed */ |
| 7132 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7133 | |
| 7134 | if (--max_dgrams <= 0) |
| 7135 | goto stop_here; |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 7136 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7137 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7138 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7139 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7140 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7141 | |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 7142 | stop_here: |
| 7143 | /* too much work done at once, come back here later */ |
| 7144 | if (!MT_LIST_ISEMPTY(&dghdlr->dgrams)) |
| 7145 | tasklet_wakeup((struct tasklet *)t); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7146 | leave: |
| 7147 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 7148 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7149 | } |
| 7150 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 7151 | /* 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] | 7152 | * Returns 1 if succeeded, 0 if not. |
| 7153 | */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 7154 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7155 | unsigned char **dcid, size_t *dcid_len) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7156 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7157 | int ret = 0, long_header; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7158 | size_t minlen, skip; |
| 7159 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7160 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7161 | |
| 7162 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7163 | 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] | 7164 | goto err; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7165 | } |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7166 | |
| 7167 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
Frédéric Lécaille | 36b28ed | 2022-05-09 18:08:13 +0200 | [diff] [blame] | 7168 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7169 | 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] | 7170 | 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] | 7171 | if (end - buf < minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7172 | goto err; |
| 7173 | |
| 7174 | buf += skip; |
| 7175 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7176 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7177 | goto err; |
| 7178 | |
| 7179 | *dcid = buf; |
| 7180 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7181 | ret = 1; |
| 7182 | leave: |
| 7183 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7184 | return ret; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7185 | |
| 7186 | err: |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7187 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7188 | goto leave; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 7189 | } |
| 7190 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7191 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7192 | void qc_notify_close(struct quic_conn *qc) |
| 7193 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7194 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7195 | |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7196 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7197 | goto leave; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7198 | |
| 7199 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7200 | /* wake up the MUX */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7201 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7202 | TRACE_STATE("connection closure notidfied to mux", |
| 7203 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7204 | qc->conn->mux->wake(qc->conn); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame^] | 7205 | } |
| 7206 | else |
| 7207 | TRACE_STATE("connection closure not notidfied to mux", |
| 7208 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7209 | leave: |
| 7210 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 7211 | } |
| 7212 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7213 | /* |
| 7214 | * Local variables: |
| 7215 | * c-indent-level: 8 |
| 7216 | * c-basic-offset: 8 |
| 7217 | * End: |
| 7218 | */ |