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 | * |
| 4 | * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
| 25 | #include <haproxy/buf-t.h> |
| 26 | #include <haproxy/compat.h> |
| 27 | #include <haproxy/api.h> |
| 28 | #include <haproxy/debug.h> |
| 29 | #include <haproxy/tools.h> |
| 30 | #include <haproxy/ticks.h> |
| 31 | #include <haproxy/time.h> |
| 32 | |
| 33 | #include <haproxy/connection.h> |
| 34 | #include <haproxy/fd.h> |
| 35 | #include <haproxy/freq_ctr.h> |
| 36 | #include <haproxy/global.h> |
| 37 | #include <haproxy/log.h> |
| 38 | #include <haproxy/pipe.h> |
| 39 | #include <haproxy/proxy.h> |
| 40 | #include <haproxy/quic_cc.h> |
| 41 | #include <haproxy/quic_frame.h> |
| 42 | #include <haproxy/quic_loss.h> |
| 43 | #include <haproxy/quic_tls.h> |
| 44 | #include <haproxy/ssl_sock.h> |
| 45 | #include <haproxy/stream_interface.h> |
| 46 | #include <haproxy/task.h> |
| 47 | #include <haproxy/trace.h> |
| 48 | #include <haproxy/xprt_quic.h> |
| 49 | |
| 50 | struct quic_transport_params quic_dflt_transport_params = { |
| 51 | .max_packet_size = QUIC_DFLT_MAX_PACKET_SIZE, |
| 52 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 53 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
| 54 | }; |
| 55 | |
| 56 | /* trace source and events */ |
| 57 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 58 | const struct trace_source *src, |
| 59 | const struct ist where, const struct ist func, |
| 60 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 61 | |
| 62 | static const struct trace_event quic_trace_events[] = { |
| 63 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 64 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 65 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 66 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 67 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 68 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 69 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 71 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 72 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 73 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 74 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 75 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 76 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 77 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 78 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 79 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 80 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 81 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 82 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 83 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 84 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 85 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 86 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 87 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 88 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 89 | { .mask = QUIC_EV_CONN_CPAPKT, .name = "phdshk_cpakt", .desc = "clear post handhshake app. packet preparation" }, |
| 90 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 91 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 92 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 93 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 94 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 95 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 96 | |
| 97 | { .mask = QUIC_EV_CONN_ENEW, .name = "new_conn_err", .desc = "error on new QUIC connection" }, |
| 98 | { .mask = QUIC_EV_CONN_EISEC, .name = "init_secs_err", .desc = "error on initial secrets derivation" }, |
| 99 | { .mask = QUIC_EV_CONN_ERSEC, .name = "read_secs_err", .desc = "error on read secrets derivation" }, |
| 100 | { .mask = QUIC_EV_CONN_EWSEC, .name = "write_secs_err", .desc = "error on write secrets derivation" }, |
| 101 | { .mask = QUIC_EV_CONN_ELPKT, .name = "lstnr_packet_err", .desc = "error on new listener received packet" }, |
| 102 | { .mask = QUIC_EV_CONN_ESPKT, .name = "srv_packet_err", .desc = "error on new server received packet" }, |
| 103 | { .mask = QUIC_EV_CONN_ECHPKT, .name = "chdshk_pkt_err", .desc = "error on clear handhshake packet building" }, |
| 104 | { .mask = QUIC_EV_CONN_EHPKT, .name = "hdshk_pkt_err", .desc = "error on handhshake packet building" }, |
| 105 | { .mask = QUIC_EV_CONN_EPAPKT, .name = "phdshk_apkt_err", .desc = "error on post handhshake application packet building" }, |
| 106 | { /* end */ } |
| 107 | }; |
| 108 | |
| 109 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 110 | /* arg1 */ { /* already used by the connection */ }, |
| 111 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 112 | /* arg3 */ { }, |
| 113 | /* arg4 */ { } |
| 114 | }; |
| 115 | |
| 116 | static const struct name_desc quic_trace_decoding[] = { |
| 117 | #define QUIC_VERB_CLEAN 1 |
| 118 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 119 | { /* end */ } |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | struct trace_source trace_quic = { |
| 124 | .name = IST("quic"), |
| 125 | .desc = "QUIC xprt", |
| 126 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 127 | .default_cb = quic_trace, |
| 128 | .known_events = quic_trace_events, |
| 129 | .lockon_args = quic_trace_lockon_args, |
| 130 | .decoding = quic_trace_decoding, |
| 131 | .report_events = ~0, /* report everything by default */ |
| 132 | }; |
| 133 | |
| 134 | #define TRACE_SOURCE &trace_quic |
| 135 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 136 | |
| 137 | static BIO_METHOD *ha_quic_meth; |
| 138 | |
| 139 | /* QUIC xprt connection context. */ |
| 140 | struct quic_conn_ctx { |
| 141 | struct connection *conn; |
| 142 | SSL *ssl; |
| 143 | BIO *bio; |
| 144 | int state; |
| 145 | const struct xprt_ops *xprt; |
| 146 | void *xprt_ctx; |
| 147 | struct wait_event wait_event; |
| 148 | struct wait_event *subs; |
| 149 | }; |
| 150 | |
| 151 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 152 | "quic_conn_ctx_pool", sizeof(struct quic_conn_ctx)); |
| 153 | |
| 154 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 155 | |
| 156 | DECLARE_POOL(pool_head_quic_connection_id, |
| 157 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
| 158 | |
| 159 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
| 160 | |
| 161 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
| 162 | |
| 163 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
| 164 | |
| 165 | DECLARE_POOL(pool_head_quic_tx_frm, "quic_tx_frm_pool", sizeof(struct quic_tx_frm)); |
| 166 | |
| 167 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
| 168 | |
| 169 | DECLARE_STATIC_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
| 170 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 171 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 172 | |
| 173 | static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type, |
| 174 | struct quic_enc_level *qel); |
| 175 | |
| 176 | int qc_prep_phdshk_pkts(struct quic_conn *qc); |
| 177 | |
| 178 | /* Add traces to <buf> depending on <frm> TX frame type. */ |
| 179 | static inline void chunk_tx_frm_appendf(struct buffer *buf, |
| 180 | const struct quic_tx_frm *frm) |
| 181 | { |
| 182 | switch (frm->type) { |
| 183 | case QUIC_FT_CRYPTO: |
| 184 | chunk_appendf(buf, " cfoff=%llu cflen=%llu", |
| 185 | (unsigned long long)frm->crypto.offset, |
| 186 | (unsigned long long)frm->crypto.len); |
| 187 | break; |
| 188 | default: |
| 189 | chunk_appendf(buf, " %s", quic_frame_type_string(frm->type)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* Trace callback for QUIC. |
| 194 | * These traces always expect that arg1, if non-null, is of type connection. |
| 195 | */ |
| 196 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 197 | const struct ist where, const struct ist func, |
| 198 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 199 | { |
| 200 | const struct connection *conn = a1; |
| 201 | |
| 202 | if (conn) { |
| 203 | struct quic_tls_secrets *secs; |
| 204 | struct quic_conn *qc; |
| 205 | |
| 206 | qc = conn->qc; |
| 207 | chunk_appendf(&trace_buf, " : conn@%p", conn); |
| 208 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 209 | chunk_appendf(&trace_buf, "\n odcid"); |
| 210 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 211 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 212 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 213 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 218 | const enum ssl_encryption_level_t *level = a2; |
| 219 | const size_t *len = a3; |
| 220 | |
| 221 | if (level) { |
| 222 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 223 | |
| 224 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 225 | } |
| 226 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 227 | 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] | 228 | } |
| 229 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 230 | /* Initial read & write secrets. */ |
| 231 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 232 | const unsigned char *rx_sec = a2; |
| 233 | const unsigned char *tx_sec = a3; |
| 234 | |
| 235 | secs = &qc->els[level].tls_ctx.rx; |
| 236 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 237 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 238 | if (rx_sec) |
| 239 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 240 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 241 | } |
| 242 | secs = &qc->els[level].tls_ctx.tx; |
| 243 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 244 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 245 | if (tx_sec) |
| 246 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 247 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 248 | } |
| 249 | } |
| 250 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 251 | const enum ssl_encryption_level_t *level = a2; |
| 252 | const unsigned char *secret = a3; |
| 253 | const size_t *secret_len = a4; |
| 254 | |
| 255 | if (level) { |
| 256 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 257 | |
| 258 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 259 | if (secret && secret_len) |
| 260 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 261 | secs = &qc->els[lvl].tls_ctx.rx; |
| 262 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 263 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 268 | const enum ssl_encryption_level_t *level = a2; |
| 269 | const unsigned char *secret = a3; |
| 270 | const size_t *secret_len = a4; |
| 271 | |
| 272 | if (level) { |
| 273 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 274 | |
| 275 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 276 | if (secret && secret_len) |
| 277 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 278 | secs = &qc->els[lvl].tls_ctx.tx; |
| 279 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 280 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 281 | } |
| 282 | |
| 283 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 284 | |
| 285 | if (mask & QUIC_EV_CONN_HPKT) { |
| 286 | const struct quic_tx_packet *pkt = a2; |
| 287 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 288 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | |
| 290 | if (qel) { |
| 291 | struct quic_pktns *pktns; |
| 292 | |
| 293 | pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 294 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 295 | "if=%llu pp=%u pdg=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 296 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 297 | (unsigned long long)qc->path->cwnd, |
| 298 | (unsigned long long)qc->path->prep_in_flight, |
| 299 | (unsigned long long)qc->path->in_flight, |
| 300 | (unsigned long long)pktns->tx.in_flight, |
| 301 | pktns->tx.pto_probe, qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 302 | } |
| 303 | if (pkt) { |
| 304 | const struct quic_tx_frm *frm; |
| 305 | chunk_appendf(&trace_buf, " pn=%llu cdlen=%u", |
| 306 | (unsigned long long)pkt->pn_node.key, pkt->cdata_len); |
| 307 | list_for_each_entry(frm, &pkt->frms, list) |
| 308 | chunk_tx_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 309 | chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes); |
| 310 | } |
| 311 | |
| 312 | if (room) { |
| 313 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 314 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 315 | (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] | 316 | } |
| 317 | } |
| 318 | |
| 319 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 320 | const enum quic_handshake_state *state = a2; |
| 321 | const int *err = a3; |
| 322 | |
| 323 | if (state) |
| 324 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 325 | if (err) |
| 326 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 327 | } |
| 328 | |
| 329 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_ESPKT|QUIC_EV_CONN_SPKT)) { |
| 330 | const struct quic_rx_packet *pkt = a2; |
| 331 | const unsigned long *pktlen = a3; |
| 332 | const SSL *ssl = a4; |
| 333 | |
| 334 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 335 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 336 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 337 | if (pkt->pnl) |
| 338 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 339 | (unsigned long long)pkt->pn); |
| 340 | if (pkt->token_len) |
| 341 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 342 | (unsigned long long)pkt->token_len); |
| 343 | if (pkt->aad_len) |
| 344 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 345 | (unsigned long long)pkt->aad_len); |
| 346 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 347 | pkt->flags, (unsigned long long)pkt->len); |
| 348 | } |
| 349 | if (pktlen) |
| 350 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 351 | if (ssl) { |
| 352 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 353 | chunk_appendf(&trace_buf, " el=%c", |
| 354 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 359 | const struct quic_rx_packet *pkt = a2; |
| 360 | const struct quic_rx_crypto_frm *cf = a3; |
| 361 | const SSL *ssl = a4; |
| 362 | |
| 363 | if (pkt) |
| 364 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 365 | quic_packet_type_enc_level_char(pkt->type), |
| 366 | (unsigned long long)pkt->pn); |
| 367 | if (cf) |
| 368 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 369 | (unsigned long long)cf->offset_node.key, |
| 370 | (unsigned long long)cf->len); |
| 371 | if (ssl) { |
| 372 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 373 | chunk_appendf(&trace_buf, " el=%c", |
| 374 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 379 | const struct quic_frame *frm = a2; |
| 380 | |
| 381 | if (frm) |
| 382 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 383 | } |
| 384 | |
| 385 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 386 | const struct quic_enc_level *qel = a2; |
| 387 | |
| 388 | if (qel) { |
| 389 | struct quic_pktns *pktns; |
| 390 | |
| 391 | pktns = qc->pktns; |
| 392 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 393 | " qel=%c ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 394 | quic_enc_level_char_from_qel(qel, qc), |
| 395 | !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 396 | (unsigned long long)qc->path->cwnd, |
| 397 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 398 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 399 | (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe, |
| 400 | (unsigned long long)qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | if (mask & QUIC_EV_CONN_RMHP) { |
| 405 | const struct quic_rx_packet *pkt = a2; |
| 406 | |
| 407 | if (pkt) { |
| 408 | const int *ret = a3; |
| 409 | |
| 410 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 411 | if (ret && *ret) |
| 412 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 413 | pkt->pnl, (unsigned long long)pkt->pn); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 418 | const struct quic_tx_frm *frm = a2; |
| 419 | const unsigned long *val1 = a3; |
| 420 | const unsigned long *val2 = a4; |
| 421 | |
| 422 | if (frm) |
| 423 | chunk_tx_frm_appendf(&trace_buf, frm); |
| 424 | if (val1) |
| 425 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 426 | if (val2) |
| 427 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 428 | } |
| 429 | |
| 430 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 431 | const unsigned int *rtt_sample = a2; |
| 432 | const unsigned int *ack_delay = a3; |
| 433 | const struct quic_loss *ql = a4; |
| 434 | |
| 435 | if (rtt_sample) |
| 436 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 437 | if (ack_delay) |
| 438 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 439 | if (ql) |
| 440 | chunk_appendf(&trace_buf, |
| 441 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 442 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 443 | } |
| 444 | if (mask & QUIC_EV_CONN_CC) { |
| 445 | const struct quic_cc_event *ev = a2; |
| 446 | const struct quic_cc *cc = a3; |
| 447 | |
| 448 | if (a2) |
| 449 | quic_cc_event_trace(&trace_buf, ev); |
| 450 | if (a3) |
| 451 | quic_cc_state_trace(&trace_buf, cc); |
| 452 | } |
| 453 | |
| 454 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 455 | const struct quic_pktns *pktns = a2; |
| 456 | const struct list *lost_pkts = a3; |
| 457 | struct quic_conn *qc = conn->qc; |
| 458 | |
| 459 | if (pktns) { |
| 460 | chunk_appendf(&trace_buf, " pktns=%s", |
| 461 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 462 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 463 | if (pktns->tx.loss_time) |
| 464 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 465 | 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] | 466 | } |
| 467 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 468 | struct quic_tx_packet *pkt; |
| 469 | |
| 470 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 471 | list_for_each_entry(pkt, lost_pkts, list) |
| 472 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 477 | struct quic_conn *qc = conn->qc; |
| 478 | const struct quic_pktns *pktns = a2; |
| 479 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 480 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 481 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 482 | if (ifae_pkts) |
| 483 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 484 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 485 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 486 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 487 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 488 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 489 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 490 | if (mask & QUIC_EV_CONN_STIMER) { |
| 491 | if (pktns->tx.loss_time) |
| 492 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 493 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 494 | } |
| 495 | if (mask & QUIC_EV_CONN_SPTO) { |
| 496 | if (pktns->tx.time_of_last_eliciting) |
| 497 | chunk_appendf(&trace_buf, " tole=%dms", |
| 498 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 499 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 500 | 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] | 501 | } |
| 502 | } |
| 503 | |
| 504 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 505 | chunk_appendf(&trace_buf, |
| 506 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 511 | const struct quic_tx_packet *pkt = a2; |
| 512 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 513 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 514 | (unsigned long long)qc->path->cwnd, |
| 515 | (unsigned long long)qc->path->prep_in_flight, |
| 516 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 517 | if (pkt) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 518 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | (unsigned long)pkt->pn_node.key, |
| 520 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 521 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 522 | (unsigned long long)pkt->in_flight_len, |
| 523 | (unsigned long long)pkt->cdata_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 526 | |
| 527 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 528 | const uint8_t *alert = a2; |
| 529 | const enum ssl_encryption_level_t *level = a3; |
| 530 | |
| 531 | if (alert) |
| 532 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 533 | if (level) |
| 534 | chunk_appendf(&trace_buf, " el=%c", |
| 535 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 536 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 537 | } |
| 538 | if (mask & QUIC_EV_CONN_LPKT) { |
| 539 | const struct quic_rx_packet *pkt = a2; |
| 540 | |
| 541 | if (conn) |
| 542 | chunk_appendf(&trace_buf, " xprt_ctx@%p", conn->xprt_ctx); |
| 543 | if (pkt) |
| 544 | chunk_appendf(&trace_buf, " type=0x%02x %s", |
| 545 | pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 546 | } |
| 547 | |
| 548 | } |
| 549 | |
| 550 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 551 | static inline int quic_peer_validated_addr(struct quic_conn_ctx *ctx) |
| 552 | { |
| 553 | struct quic_conn *qc; |
| 554 | |
| 555 | qc = ctx->conn->qc; |
| 556 | if (objt_server(qc->conn->target)) |
| 557 | return 1; |
| 558 | |
| 559 | if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 560 | (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 561 | (ctx->state & QUIC_HS_ST_COMPLETE)) |
| 562 | return 1; |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 568 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 569 | */ |
| 570 | static inline void qc_set_timer(struct quic_conn_ctx *ctx) |
| 571 | { |
| 572 | struct quic_conn *qc; |
| 573 | struct quic_pktns *pktns; |
| 574 | unsigned int pto; |
| 575 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 576 | TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn, |
| 577 | NULL, NULL, &ctx->conn->qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 578 | qc = ctx->conn->qc; |
| 579 | pktns = quic_loss_pktns(qc); |
| 580 | if (tick_isset(pktns->tx.loss_time)) { |
| 581 | qc->timer = pktns->tx.loss_time; |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | /* XXX TODO: anti-amplification: the timer must be |
| 586 | * cancelled for a server which reached the anti-amplification limit. |
| 587 | */ |
| 588 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 589 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) { |
| 590 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 591 | /* Timer cancellation. */ |
| 592 | qc->timer = TICK_ETERNITY; |
| 593 | goto out; |
| 594 | } |
| 595 | |
| 596 | pktns = quic_pto_pktns(qc, ctx->state & QUIC_HS_ST_COMPLETE, &pto); |
| 597 | if (tick_isset(pto)) |
| 598 | qc->timer = pto; |
| 599 | out: |
| 600 | task_schedule(qc->timer_task, qc->timer); |
| 601 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns); |
| 602 | } |
| 603 | |
| 604 | #ifndef OPENSSL_IS_BORINGSSL |
| 605 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 606 | const uint8_t *read_secret, |
| 607 | const uint8_t *write_secret, size_t secret_len) |
| 608 | { |
| 609 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 610 | struct quic_tls_ctx *tls_ctx = |
| 611 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 612 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
| 613 | |
| 614 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn); |
| 615 | tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher); |
| 616 | tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher); |
| 617 | tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher); |
| 618 | |
| 619 | if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md, |
| 620 | tls_ctx->rx.key, sizeof tls_ctx->rx.key, |
| 621 | tls_ctx->rx.iv, sizeof tls_ctx->rx.iv, |
| 622 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 623 | read_secret, secret_len)) { |
| 624 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
| 629 | if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md, |
| 630 | tls_ctx->tx.key, sizeof tls_ctx->tx.key, |
| 631 | tls_ctx->tx.iv, sizeof tls_ctx->tx.iv, |
| 632 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 633 | write_secret, secret_len)) { |
| 634 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
| 639 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 640 | const unsigned char *buf; |
| 641 | size_t buflen; |
| 642 | |
| 643 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 644 | if (!buflen) |
| 645 | return 0; |
| 646 | |
| 647 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 648 | return 0; |
| 649 | } |
| 650 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level); |
| 651 | |
| 652 | return 1; |
| 653 | } |
| 654 | #else |
| 655 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 656 | * level. |
| 657 | * Returns 1 if succedded, 0 if not. |
| 658 | */ |
| 659 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 660 | const SSL_CIPHER *cipher, |
| 661 | const uint8_t *secret, size_t secret_len) |
| 662 | { |
| 663 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 664 | struct quic_tls_ctx *tls_ctx = |
| 665 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 666 | |
| 667 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn); |
| 668 | tls_ctx->rx.aead = tls_aead(cipher); |
| 669 | tls_ctx->rx.md = tls_md(cipher); |
| 670 | tls_ctx->rx.hp = tls_hp(cipher); |
| 671 | |
| 672 | if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md, |
| 673 | tls_ctx->rx.key, sizeof tls_ctx->rx.key, |
| 674 | tls_ctx->rx.iv, sizeof tls_ctx->rx.iv, |
| 675 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 676 | secret, secret_len)) { |
| 677 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn); |
| 678 | goto err; |
| 679 | } |
| 680 | |
| 681 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 682 | const unsigned char *buf; |
| 683 | size_t buflen; |
| 684 | |
| 685 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 686 | if (!buflen) |
| 687 | goto err; |
| 688 | |
| 689 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 690 | goto err; |
| 691 | } |
| 692 | |
| 693 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
| 694 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len); |
| 695 | |
| 696 | return 1; |
| 697 | |
| 698 | err: |
| 699 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ERSEC, conn); |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 704 | * encryption level. |
| 705 | * Returns 1 if succedded, 0 if not. |
| 706 | */ |
| 707 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 708 | const SSL_CIPHER *cipher, |
| 709 | const uint8_t *secret, size_t secret_len) |
| 710 | { |
| 711 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 712 | struct quic_tls_ctx *tls_ctx = |
| 713 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 714 | |
| 715 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn); |
| 716 | tls_ctx->tx.aead = tls_aead(cipher); |
| 717 | tls_ctx->tx.md = tls_md(cipher); |
| 718 | tls_ctx->tx.hp = tls_hp(cipher); |
| 719 | |
| 720 | if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md, |
| 721 | tls_ctx->tx.key, sizeof tls_ctx->tx.key, |
| 722 | tls_ctx->tx.iv, sizeof tls_ctx->tx.iv, |
| 723 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 724 | secret, secret_len)) { |
| 725 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn); |
| 726 | goto err; |
| 727 | } |
| 728 | |
| 729 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
| 730 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len); |
| 731 | |
| 732 | return 1; |
| 733 | |
| 734 | err: |
| 735 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_EWSEC, conn); |
| 736 | return 0; |
| 737 | } |
| 738 | #endif |
| 739 | |
| 740 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 741 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 742 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 743 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 744 | * store all the data. |
| 745 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 746 | */ |
| 747 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 748 | const unsigned char *data, size_t len) |
| 749 | { |
| 750 | struct quic_crypto_buf **qcb; |
| 751 | /* The remaining byte to store in CRYPTO buffers. */ |
| 752 | size_t cf_offset, cf_len, *nb_buf; |
| 753 | unsigned char *pos; |
| 754 | |
| 755 | nb_buf = &qel->tx.crypto.nb_buf; |
| 756 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 757 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 758 | cf_len = len; |
| 759 | |
| 760 | while (len) { |
| 761 | size_t to_copy, room; |
| 762 | |
| 763 | pos = (*qcb)->data + (*qcb)->sz; |
| 764 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 765 | to_copy = len > room ? room : len; |
| 766 | if (to_copy) { |
| 767 | memcpy(pos, data, to_copy); |
| 768 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 769 | qel->tx.crypto.sz += to_copy; |
| 770 | (*qcb)->sz += to_copy; |
| 771 | pos += to_copy; |
| 772 | len -= to_copy; |
| 773 | data += to_copy; |
| 774 | } |
| 775 | else { |
| 776 | struct quic_crypto_buf **tmp; |
| 777 | |
| 778 | tmp = realloc(qel->tx.crypto.bufs, |
| 779 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 780 | if (tmp) { |
| 781 | qel->tx.crypto.bufs = tmp; |
| 782 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 783 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 784 | if (!*qcb) |
| 785 | return 0; |
| 786 | |
| 787 | (*qcb)->sz = 0; |
| 788 | ++*nb_buf; |
| 789 | } |
| 790 | else { |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 797 | * have been buffered. |
| 798 | */ |
| 799 | if (!len) { |
| 800 | struct quic_tx_frm *frm; |
| 801 | |
| 802 | frm = pool_alloc(pool_head_quic_tx_frm); |
| 803 | if (!frm) |
| 804 | return 0; |
| 805 | |
| 806 | frm->type = QUIC_FT_CRYPTO; |
| 807 | frm->crypto.offset = cf_offset; |
| 808 | frm->crypto.len = cf_len; |
| 809 | LIST_ADDQ(&qel->pktns->tx.frms, &frm->list); |
| 810 | } |
| 811 | |
| 812 | return len == 0; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 817 | * wants to provide the QUIC layer with CRYPTO data. |
| 818 | * Returns 1 if succeeded, 0 if not. |
| 819 | */ |
| 820 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 821 | const uint8_t *data, size_t len) |
| 822 | { |
| 823 | struct connection *conn; |
| 824 | enum quic_tls_enc_level tel; |
| 825 | struct quic_enc_level *qel; |
| 826 | |
| 827 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 828 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn); |
| 829 | tel = ssl_to_quic_enc_level(level); |
| 830 | qel = &conn->qc->els[tel]; |
| 831 | |
| 832 | if (tel == -1) { |
| 833 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn); |
| 834 | goto err; |
| 835 | } |
| 836 | |
| 837 | if (!quic_crypto_data_cpy(qel, data, len)) { |
| 838 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn); |
| 839 | goto err; |
| 840 | } |
| 841 | |
| 842 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 843 | conn, &level, &len); |
| 844 | |
| 845 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn); |
| 846 | return 1; |
| 847 | |
| 848 | err: |
| 849 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | int ha_quic_flush_flight(SSL *ssl) |
| 854 | { |
| 855 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 856 | |
| 857 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn); |
| 858 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn); |
| 859 | |
| 860 | return 1; |
| 861 | } |
| 862 | |
| 863 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 864 | { |
| 865 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 866 | |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 867 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 868 | return 1; |
| 869 | } |
| 870 | |
| 871 | /* QUIC TLS methods */ |
| 872 | static SSL_QUIC_METHOD ha_quic_method = { |
| 873 | #ifdef OPENSSL_IS_BORINGSSL |
| 874 | .set_read_secret = ha_set_rsec, |
| 875 | .set_write_secret = ha_set_wsec, |
| 876 | #else |
| 877 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 878 | #endif |
| 879 | .add_handshake_data = ha_quic_add_handshake_data, |
| 880 | .flush_flight = ha_quic_flush_flight, |
| 881 | .send_alert = ha_quic_send_alert, |
| 882 | }; |
| 883 | |
| 884 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 885 | * Returns an error count. |
| 886 | */ |
| 887 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 888 | { |
| 889 | struct proxy *curproxy = bind_conf->frontend; |
| 890 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 891 | int cfgerr = 0; |
| 892 | |
| 893 | #if 0 |
| 894 | /* XXX Did not manage to use this. */ |
| 895 | const char *ciphers = |
| 896 | "TLS_AES_128_GCM_SHA256:" |
| 897 | "TLS_AES_256_GCM_SHA384:" |
| 898 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 899 | "TLS_AES_128_CCM_SHA256"; |
| 900 | #endif |
| 901 | const char *groups = "P-256:X25519:P-384:P-521"; |
| 902 | long options = |
| 903 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 904 | SSL_OP_SINGLE_ECDH_USE | |
| 905 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 906 | SSL_CTX *ctx; |
| 907 | |
| 908 | ctx = SSL_CTX_new(TLS_server_method()); |
| 909 | bind_conf->initial_ctx = ctx; |
| 910 | |
| 911 | SSL_CTX_set_options(ctx, options); |
| 912 | #if 0 |
| 913 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 914 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 915 | "for bind '%s' at [%s:%d].\n", |
| 916 | curproxy->id, ciphers, |
| 917 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 918 | cfgerr++; |
| 919 | } |
| 920 | #endif |
| 921 | |
| 922 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 923 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 924 | "for bind '%s' at [%s:%d].\n", |
| 925 | curproxy->id, groups, |
| 926 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 927 | cfgerr++; |
| 928 | } |
| 929 | |
| 930 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 931 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 932 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 933 | SSL_CTX_set_default_verify_paths(ctx); |
| 934 | |
| 935 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 936 | #ifdef OPENSSL_IS_BORINGSSL |
| 937 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 938 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 939 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 940 | if (bind_conf->ssl_conf.early_data) { |
| 941 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 942 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 943 | } |
| 944 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 945 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 946 | #else |
| 947 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 948 | #endif |
| 949 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 950 | #endif |
| 951 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 952 | |
| 953 | return cfgerr; |
| 954 | } |
| 955 | |
| 956 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 957 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 958 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 959 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 960 | */ |
| 961 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 962 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 963 | { |
| 964 | uint64_t expected_pn = largest_pn + 1; |
| 965 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 966 | uint64_t pn_hwin = pn_win / 2; |
| 967 | uint64_t pn_mask = pn_win - 1; |
| 968 | uint64_t candidate_pn; |
| 969 | |
| 970 | |
| 971 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 972 | /* Note that <pn_win> > <pn_hwin>. */ |
| 973 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 974 | candidate_pn + pn_hwin <= expected_pn) |
| 975 | return candidate_pn + pn_win; |
| 976 | |
| 977 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 978 | return candidate_pn - pn_win; |
| 979 | |
| 980 | return candidate_pn; |
| 981 | } |
| 982 | |
| 983 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 984 | * cryptographic context. |
| 985 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 986 | * the packet number field for this packet with <byte0> address of its first byte. |
| 987 | * <end> points to one byte past the end of this packet. |
| 988 | * Returns 1 if succeeded, 0 if not. |
| 989 | */ |
| 990 | static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 991 | int64_t largest_pn, unsigned char *pn, |
| 992 | unsigned char *byte0, const unsigned char *end, |
| 993 | struct quic_conn_ctx *ctx) |
| 994 | { |
| 995 | int ret, outlen, i, pnlen; |
| 996 | uint64_t packet_number; |
| 997 | uint32_t truncated_pn = 0; |
| 998 | unsigned char mask[5] = {0}; |
| 999 | unsigned char *sample; |
| 1000 | EVP_CIPHER_CTX *cctx; |
| 1001 | unsigned char *hp_key; |
| 1002 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1003 | /* Check there is enough data in this packet. */ |
| 1004 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1005 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | cctx = EVP_CIPHER_CTX_new(); |
| 1010 | if (!cctx) { |
| 1011 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | ret = 0; |
| 1016 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1017 | |
| 1018 | hp_key = tls_ctx->rx.hp_key; |
| 1019 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1020 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1021 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
| 1022 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
| 1026 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1027 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1028 | for (i = 0; i < pnlen; i++) { |
| 1029 | pn[i] ^= mask[i + 1]; |
| 1030 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1031 | } |
| 1032 | |
| 1033 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1034 | /* Store remaining information for this unprotected header */ |
| 1035 | pkt->pn = packet_number; |
| 1036 | pkt->pnl = pnlen; |
| 1037 | |
| 1038 | ret = 1; |
| 1039 | |
| 1040 | out: |
| 1041 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1042 | |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
| 1046 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1047 | * address, with <payload_len> as payload length, <aad> as address of |
| 1048 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1049 | * context. |
| 1050 | * Returns 1 if succeeded, 0 if not. |
| 1051 | */ |
| 1052 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1053 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1054 | struct quic_tls_ctx *tls_ctx, struct connection *conn) |
| 1055 | { |
| 1056 | unsigned char iv[12]; |
| 1057 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1058 | size_t tx_iv_sz = sizeof tls_ctx->tx.iv; |
| 1059 | |
| 1060 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1061 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1066 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1067 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | return 1; |
| 1072 | } |
| 1073 | |
| 1074 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1075 | * Returns 1 if succeeded, 0 if not. |
| 1076 | */ |
| 1077 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx) |
| 1078 | { |
| 1079 | int ret; |
| 1080 | unsigned char iv[12]; |
| 1081 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1082 | size_t rx_iv_sz = sizeof tls_ctx->rx.iv; |
| 1083 | |
| 1084 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1085 | return 0; |
| 1086 | |
| 1087 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1088 | pkt->data, pkt->aad_len, |
| 1089 | tls_ctx->rx.aead, tls_ctx->rx.key, iv); |
| 1090 | if (!ret) |
| 1091 | return 0; |
| 1092 | |
| 1093 | /* Update the packet length (required to parse the frames). */ |
| 1094 | pkt->len = pkt->aad_len + ret; |
| 1095 | |
| 1096 | return 1; |
| 1097 | } |
| 1098 | |
| 1099 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1100 | static inline void qc_treat_acked_tx_frm(struct quic_tx_frm *frm, |
| 1101 | struct quic_conn_ctx *ctx) |
| 1102 | { |
| 1103 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1104 | LIST_DEL(&frm->list); |
| 1105 | pool_free(pool_head_quic_tx_frm, frm); |
| 1106 | } |
| 1107 | |
| 1108 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1109 | * deallocating them, and their TX frames. |
| 1110 | * Returns the last node reached to be used for the next range. |
| 1111 | * May be NULL if <largest> node could not be found. |
| 1112 | */ |
| 1113 | static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags, |
| 1114 | struct list *newly_acked_pkts, |
| 1115 | struct eb64_node *largest_node, |
| 1116 | uint64_t largest, uint64_t smallest, |
| 1117 | struct quic_conn_ctx *ctx) |
| 1118 | { |
| 1119 | struct eb64_node *node; |
| 1120 | struct quic_tx_packet *pkt; |
| 1121 | |
| 1122 | if (largest_node) |
| 1123 | node = largest_node; |
| 1124 | else { |
| 1125 | node = eb64_lookup(pkts, largest); |
| 1126 | while (!node && largest > smallest) { |
| 1127 | node = eb64_lookup(pkts, --largest); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | while (node && node->key >= smallest) { |
| 1132 | struct quic_tx_frm *frm, *frmbak; |
| 1133 | |
| 1134 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1135 | *pkt_flags |= pkt->flags; |
| 1136 | LIST_ADD(newly_acked_pkts, &pkt->list); |
| 1137 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key); |
| 1138 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1139 | qc_treat_acked_tx_frm(frm, ctx); |
| 1140 | node = eb64_prev(node); |
| 1141 | eb64_delete(&pkt->pn_node); |
| 1142 | } |
| 1143 | |
| 1144 | return node; |
| 1145 | } |
| 1146 | |
| 1147 | /* Treat <frm> frame whose packet it is attached to has just been detected as non |
| 1148 | * acknowledged. |
| 1149 | */ |
| 1150 | static inline void qc_treat_nacked_tx_frm(struct quic_tx_frm *frm, |
| 1151 | struct quic_pktns *pktns, |
| 1152 | struct quic_conn_ctx *ctx) |
| 1153 | { |
| 1154 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1155 | LIST_DEL(&frm->list); |
| 1156 | LIST_ADD(&pktns->tx.frms, &frm->list); |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | /* Free the TX packets of <pkts> list */ |
| 1161 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1162 | { |
| 1163 | struct quic_tx_packet *pkt, *tmp; |
| 1164 | |
| 1165 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1166 | LIST_DEL(&pkt->list); |
| 1167 | eb64_delete(&pkt->pn_node); |
| 1168 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /* Send a packet loss event nofification to the congestion controller |
| 1173 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1174 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1175 | * at <now_us> current time. |
| 1176 | * Always succeeds. |
| 1177 | */ |
| 1178 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1179 | unsigned int lost_bytes, |
| 1180 | unsigned int newest_time_sent, |
| 1181 | unsigned int period, |
| 1182 | unsigned int now_us) |
| 1183 | { |
| 1184 | struct quic_cc_event ev = { |
| 1185 | .type = QUIC_CC_EVT_LOSS, |
| 1186 | .loss.now_ms = now_ms, |
| 1187 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1188 | .loss.lost_bytes = lost_bytes, |
| 1189 | .loss.newest_time_sent = newest_time_sent, |
| 1190 | .loss.period = period, |
| 1191 | }; |
| 1192 | |
| 1193 | quic_cc_event(&qc->path->cc, &ev); |
| 1194 | } |
| 1195 | |
| 1196 | /* Send a packet ack event nofication for each newly acked packet of |
| 1197 | * <newly_acked_pkts> list and free them. |
| 1198 | * Always succeeds. |
| 1199 | */ |
| 1200 | static inline void qc_treat_newly_acked_pkts(struct quic_conn_ctx *ctx, |
| 1201 | struct list *newly_acked_pkts) |
| 1202 | { |
| 1203 | struct quic_conn *qc = ctx->conn->qc; |
| 1204 | struct quic_tx_packet *pkt, *tmp; |
| 1205 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1206 | |
| 1207 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1208 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1209 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1210 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 1211 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1212 | ev.ack.acked = pkt->in_flight_len; |
| 1213 | ev.ack.time_sent = pkt->time_sent; |
| 1214 | quic_cc_event(&qc->path->cc, &ev); |
| 1215 | LIST_DEL(&pkt->list); |
| 1216 | eb64_delete(&pkt->pn_node); |
| 1217 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1218 | } |
| 1219 | |
| 1220 | } |
| 1221 | |
| 1222 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1223 | * their TX frames. |
| 1224 | * Send a packet loss event to the congestion controller if |
| 1225 | * in flight packet have been lost. |
| 1226 | * Also frees the packet in <pkts> list. |
| 1227 | * Never fails. |
| 1228 | */ |
| 1229 | static inline void qc_release_lost_pkts(struct quic_pktns *pktns, |
| 1230 | struct quic_conn_ctx *ctx, |
| 1231 | struct list *pkts, |
| 1232 | uint64_t now_us) |
| 1233 | { |
| 1234 | struct quic_conn *qc = ctx->conn->qc; |
| 1235 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
| 1236 | struct quic_tx_frm *frm, *frmbak; |
| 1237 | uint64_t lost_bytes; |
| 1238 | |
| 1239 | lost_bytes = 0; |
| 1240 | oldest_lost = newest_lost = NULL; |
| 1241 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1242 | lost_bytes += pkt->in_flight_len; |
| 1243 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1244 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1245 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 1246 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1247 | /* Treat the frames of this lost packet. */ |
| 1248 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1249 | qc_treat_nacked_tx_frm(frm, pktns, ctx); |
| 1250 | LIST_DEL(&pkt->list); |
| 1251 | if (!oldest_lost) { |
| 1252 | oldest_lost = newest_lost = pkt; |
| 1253 | } |
| 1254 | else { |
| 1255 | if (newest_lost != oldest_lost) |
| 1256 | pool_free(pool_head_quic_tx_packet, newest_lost); |
| 1257 | newest_lost = pkt; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | if (lost_bytes) { |
| 1262 | /* Sent a packet loss event to the congestion controller. */ |
| 1263 | qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent, |
| 1264 | newest_lost->time_sent - oldest_lost->time_sent, now_us); |
| 1265 | pool_free(pool_head_quic_tx_packet, oldest_lost); |
| 1266 | if (newest_lost != oldest_lost) |
| 1267 | pool_free(pool_head_quic_tx_packet, newest_lost); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1272 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1273 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1274 | * space if any not deemed lost. |
| 1275 | * Should be called after having received an ACK frame with newly acknowledged |
| 1276 | * packets or when the the loss detection timer has expired. |
| 1277 | * Always succeeds. |
| 1278 | */ |
| 1279 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1280 | struct quic_conn *qc, |
| 1281 | struct list *lost_pkts) |
| 1282 | { |
| 1283 | struct eb_root *pkts; |
| 1284 | struct eb64_node *node; |
| 1285 | struct quic_loss *ql; |
| 1286 | unsigned int loss_delay; |
| 1287 | |
| 1288 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns); |
| 1289 | pkts = &pktns->tx.pkts; |
| 1290 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1291 | if (eb_is_empty(pkts)) |
| 1292 | goto out; |
| 1293 | |
| 1294 | ql = &qc->path->loss; |
| 1295 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1296 | loss_delay += loss_delay >> 3; |
| 1297 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1298 | |
| 1299 | node = eb64_first(pkts); |
| 1300 | while (node) { |
| 1301 | struct quic_tx_packet *pkt; |
| 1302 | int64_t largest_acked_pn; |
| 1303 | unsigned int loss_time_limit, time_sent; |
| 1304 | |
| 1305 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1306 | largest_acked_pn = pktns->tx.largest_acked_pn; |
| 1307 | node = eb64_next(node); |
| 1308 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1309 | break; |
| 1310 | |
| 1311 | time_sent = pkt->time_sent; |
| 1312 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1313 | if (tick_is_le(time_sent, now_ms) || |
| 1314 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1315 | eb64_delete(&pkt->pn_node); |
| 1316 | LIST_ADDQ(lost_pkts, &pkt->list); |
| 1317 | } |
| 1318 | else { |
| 1319 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | out: |
| 1324 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts); |
| 1325 | } |
| 1326 | |
| 1327 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1328 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 1329 | * if the largest acked packet was newly acked and if there was at leas one newly |
| 1330 | * acked ack-eliciting packet. |
| 1331 | * Return 1, if succeeded, 0 if not. |
| 1332 | */ |
| 1333 | static inline int qc_parse_ack_frm(struct quic_frame *frm, struct quic_conn_ctx *ctx, |
| 1334 | struct quic_enc_level *qel, |
| 1335 | unsigned int *rtt_sample, |
| 1336 | const unsigned char **pos, const unsigned char *end) |
| 1337 | { |
| 1338 | struct quic_ack *ack = &frm->ack; |
| 1339 | uint64_t smallest, largest; |
| 1340 | struct eb_root *pkts; |
| 1341 | struct eb64_node *largest_node; |
| 1342 | unsigned int time_sent, pkt_flags; |
| 1343 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1344 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1345 | |
| 1346 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1347 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 1348 | ctx->conn,, &ack->largest_ack); |
| 1349 | goto err; |
| 1350 | } |
| 1351 | |
| 1352 | if (ack->first_ack_range > ack->largest_ack) { |
| 1353 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 1354 | ctx->conn,, &ack->first_ack_range); |
| 1355 | goto err; |
| 1356 | } |
| 1357 | |
| 1358 | largest = ack->largest_ack; |
| 1359 | smallest = largest - ack->first_ack_range; |
| 1360 | pkts = &qel->pktns->tx.pkts; |
| 1361 | pkt_flags = 0; |
| 1362 | largest_node = NULL; |
| 1363 | time_sent = 0; |
| 1364 | |
| 1365 | if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) { |
| 1366 | largest_node = eb64_lookup(pkts, largest); |
| 1367 | if (!largest_node) { |
| 1368 | TRACE_DEVEL("Largest acked packet not found", |
| 1369 | QUIC_EV_CONN_PRSAFRM, ctx->conn); |
| 1370 | goto err; |
| 1371 | } |
| 1372 | |
| 1373 | time_sent = eb64_entry(&largest_node->node, |
| 1374 | struct quic_tx_packet, pn_node)->time_sent; |
| 1375 | } |
| 1376 | |
| 1377 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
| 1378 | ctx->conn,, &largest, &smallest); |
| 1379 | do { |
| 1380 | uint64_t gap, ack_range; |
| 1381 | |
| 1382 | qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts, |
| 1383 | largest_node, largest, smallest, ctx); |
| 1384 | if (!ack->ack_range_num--) |
| 1385 | break; |
| 1386 | |
| 1387 | if (!quic_dec_int(&gap, pos, end)) |
| 1388 | goto err; |
| 1389 | |
| 1390 | if (smallest < gap + 2) { |
| 1391 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 1392 | ctx->conn,, &gap, &smallest); |
| 1393 | goto err; |
| 1394 | } |
| 1395 | |
| 1396 | largest = smallest - gap - 2; |
| 1397 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1398 | goto err; |
| 1399 | |
| 1400 | if (largest < ack_range) { |
| 1401 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 1402 | ctx->conn,, &largest, &ack_range); |
| 1403 | goto err; |
| 1404 | } |
| 1405 | |
| 1406 | /* Do not use this node anymore. */ |
| 1407 | largest_node = NULL; |
| 1408 | /* Next range */ |
| 1409 | smallest = largest - ack_range; |
| 1410 | |
| 1411 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
| 1412 | ctx->conn,, &largest, &smallest); |
| 1413 | } while (1); |
| 1414 | |
| 1415 | /* Flag this packet number space as having received an ACK. */ |
| 1416 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED; |
| 1417 | |
| 1418 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1419 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 1420 | qel->pktns->tx.largest_acked_pn = ack->largest_ack; |
| 1421 | } |
| 1422 | |
| 1423 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1424 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 1425 | qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts); |
| 1426 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 1427 | qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms); |
| 1428 | } |
| 1429 | qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts); |
| 1430 | if (quic_peer_validated_addr(ctx)) |
| 1431 | ctx->conn->qc->path->loss.pto_count = 0; |
| 1432 | qc_set_timer(ctx); |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | return 1; |
| 1437 | |
| 1438 | err: |
| 1439 | free_quic_tx_pkts(&newly_acked_pkts); |
| 1440 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1445 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 1446 | * Remaining parameter are there for debuging purposes. |
| 1447 | * Return 1 if succeeded, 0 if not. |
| 1448 | */ |
| 1449 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 1450 | struct quic_conn_ctx *ctx, |
| 1451 | const unsigned char *data, size_t len, |
| 1452 | struct quic_rx_packet *pkt, |
| 1453 | struct quic_rx_crypto_frm *cf) |
| 1454 | { |
| 1455 | int ssl_err; |
| 1456 | |
| 1457 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1458 | ssl_err = SSL_ERROR_NONE; |
| 1459 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1460 | TRACE_PROTO("SSL_provide_quic_data() error", |
| 1461 | QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl); |
| 1462 | goto err; |
| 1463 | } |
| 1464 | |
| 1465 | el->rx.crypto.offset += len; |
| 1466 | TRACE_PROTO("in order CRYPTO data", |
| 1467 | QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl); |
| 1468 | |
| 1469 | if (ctx->state < QUIC_HS_ST_COMPLETE) { |
| 1470 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1471 | if (ssl_err != 1) { |
| 1472 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1473 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1474 | TRACE_PROTO("SSL handshake", |
| 1475 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 1476 | goto out; |
| 1477 | } |
| 1478 | |
| 1479 | TRACE_DEVEL("SSL handshake error", |
| 1480 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 1481 | goto err; |
| 1482 | } |
| 1483 | |
| 1484 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state); |
| 1485 | if (objt_listener(ctx->conn->target)) |
| 1486 | ctx->state = QUIC_HS_ST_CONFIRMED; |
| 1487 | else |
| 1488 | ctx->state = QUIC_HS_ST_COMPLETE; |
| 1489 | } else { |
| 1490 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1491 | if (ssl_err != 1) { |
| 1492 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1493 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1494 | TRACE_DEVEL("SSL post handshake", |
| 1495 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 1496 | goto out; |
| 1497 | } |
| 1498 | |
| 1499 | TRACE_DEVEL("SSL post handshake error", |
| 1500 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 1501 | goto err; |
| 1502 | } |
| 1503 | |
| 1504 | TRACE_PROTO("SSL post handshake succeeded", |
| 1505 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state); |
| 1506 | } |
| 1507 | |
| 1508 | out: |
| 1509 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1510 | return 1; |
| 1511 | |
| 1512 | err: |
| 1513 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 1518 | * as I/O handler context and <qel> as encryption level. |
| 1519 | * Returns 1 if succeeded, 0 if failed. |
| 1520 | */ |
| 1521 | static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct quic_conn_ctx *ctx, |
| 1522 | struct quic_enc_level *qel) |
| 1523 | { |
| 1524 | struct quic_frame frm; |
| 1525 | const unsigned char *pos, *end; |
| 1526 | struct quic_conn *conn = ctx->conn->qc; |
| 1527 | |
| 1528 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 1529 | /* Skip the AAD */ |
| 1530 | pos = pkt->data + pkt->aad_len; |
| 1531 | end = pkt->data + pkt->len; |
| 1532 | |
| 1533 | while (pos < end) { |
| 1534 | if (!qc_parse_frm(&frm, pkt, &pos, end, conn)) |
| 1535 | goto err; |
| 1536 | |
| 1537 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 1538 | case QUIC_FT_PADDING: |
| 1539 | if (pos != end) { |
| 1540 | TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt); |
| 1541 | goto err; |
| 1542 | } |
| 1543 | break; |
| 1544 | case QUIC_FT_PING: |
| 1545 | break; |
| 1546 | case QUIC_FT_ACK: |
| 1547 | { |
| 1548 | unsigned int rtt_sample; |
| 1549 | |
| 1550 | rtt_sample = 0; |
| 1551 | if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end)) |
| 1552 | goto err; |
| 1553 | |
| 1554 | if (rtt_sample) { |
| 1555 | unsigned int ack_delay; |
| 1556 | |
| 1557 | ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 : |
| 1558 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay)); |
| 1559 | quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn); |
| 1560 | } |
| 1561 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 1562 | break; |
| 1563 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1564 | case QUIC_FT_CRYPTO: |
| 1565 | if (frm.crypto.offset != qel->rx.crypto.offset) { |
| 1566 | struct quic_rx_crypto_frm *cf; |
| 1567 | |
| 1568 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 1569 | if (!cf) { |
| 1570 | TRACE_DEVEL("CRYPTO frame allocation failed", |
| 1571 | QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 1572 | goto err; |
| 1573 | } |
| 1574 | |
| 1575 | cf->offset_node.key = frm.crypto.offset; |
| 1576 | cf->len = frm.crypto.len; |
| 1577 | cf->data = frm.crypto.data; |
| 1578 | cf->pkt = pkt; |
| 1579 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 1580 | quic_rx_packet_refinc(pkt); |
| 1581 | } |
| 1582 | else { |
| 1583 | /* XXX TO DO: <cf> is used only for the traces. */ |
| 1584 | struct quic_rx_crypto_frm cf = { }; |
| 1585 | |
| 1586 | cf.offset_node.key = frm.crypto.offset; |
| 1587 | cf.len = frm.crypto.len; |
| 1588 | if (!qc_provide_cdata(qel, ctx, |
| 1589 | frm.crypto.data, frm.crypto.len, |
| 1590 | pkt, &cf)) |
| 1591 | goto err; |
| 1592 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1593 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 1594 | case QUIC_FT_STREAM_8: |
| 1595 | case QUIC_FT_STREAM_9: |
| 1596 | case QUIC_FT_STREAM_A: |
| 1597 | case QUIC_FT_STREAM_B: |
| 1598 | case QUIC_FT_STREAM_C: |
| 1599 | case QUIC_FT_STREAM_D: |
| 1600 | case QUIC_FT_STREAM_E: |
| 1601 | case QUIC_FT_STREAM_F: |
| 1602 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1603 | break; |
| 1604 | case QUIC_FT_CONNECTION_CLOSE: |
| 1605 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 1606 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1607 | case QUIC_FT_HANDSHAKE_DONE: |
| 1608 | if (objt_listener(ctx->conn->target)) |
| 1609 | goto err; |
| 1610 | |
| 1611 | ctx->state = QUIC_HS_ST_CONFIRMED; |
| 1612 | break; |
| 1613 | default: |
| 1614 | goto err; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 1619 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 1620 | * be discarded. |
| 1621 | */ |
| 1622 | if (ctx->state == QUIC_HS_ST_SERVER_INITIAL && |
| 1623 | pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 1624 | quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 1625 | quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn); |
| 1626 | qc_set_timer(ctx); |
| 1627 | ctx->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 1628 | } |
| 1629 | |
| 1630 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 1631 | return 1; |
| 1632 | |
| 1633 | err: |
| 1634 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /* Prepare as much as possible handshake packets for the QUIC connection |
| 1639 | * with <ctx> as I/O handler context. |
| 1640 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 1641 | */ |
| 1642 | static int qc_prep_hdshk_pkts(struct quic_conn_ctx *ctx) |
| 1643 | { |
| 1644 | struct quic_conn *qc; |
| 1645 | enum quic_tls_enc_level tel, next_tel; |
| 1646 | struct quic_enc_level *qel; |
| 1647 | struct q_buf *wbuf; |
| 1648 | /* A boolean to flag <wbuf> as reusable, even if not empty. */ |
| 1649 | int reuse_wbuf; |
| 1650 | |
| 1651 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 1652 | qc = ctx->conn->qc; |
| 1653 | if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state)) { |
| 1654 | TRACE_DEVEL("unknown enc. levels", |
| 1655 | QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 1656 | goto err; |
| 1657 | } |
| 1658 | |
| 1659 | reuse_wbuf = 0; |
| 1660 | wbuf = q_wbuf(qc); |
| 1661 | qel = &qc->els[tel]; |
| 1662 | /* When entering this function, the writter buffer must be empty. |
| 1663 | * Most of the time it points to the reader buffer. |
| 1664 | */ |
| 1665 | while ((q_buf_empty(wbuf) || reuse_wbuf)) { |
| 1666 | ssize_t ret; |
| 1667 | enum quic_pkt_type pkt_type; |
| 1668 | |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 1669 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1670 | /* Do not build any more packet f the TX secrets are not available or |
| 1671 | * f there is nothing to send, i.e. if no ACK are required |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1672 | * and if there is no more packets to send upon PTO expiration |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1673 | * and if there is no more CRYPTO data available or in flight |
| 1674 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1675 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1676 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
| 1677 | (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1678 | !qc->tx.nb_pto_dgrams && |
| 1679 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1680 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1681 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 1682 | /* Consume the buffer if we were supposed to reuse it. */ |
| 1683 | if (reuse_wbuf) |
| 1684 | wbuf = q_next_wbuf(qc); |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1689 | ret = qc_build_hdshk_pkt(wbuf, qc, pkt_type, qel); |
| 1690 | switch (ret) { |
| 1691 | case -2: |
| 1692 | goto err; |
| 1693 | case -1: |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1694 | if (!reuse_wbuf) |
| 1695 | goto out; |
| 1696 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1697 | /* Not enough room in <wbuf>. */ |
| 1698 | wbuf = q_next_wbuf(qc); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1699 | reuse_wbuf = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1700 | continue; |
| 1701 | case 0: |
| 1702 | goto out; |
| 1703 | default: |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1704 | reuse_wbuf = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1705 | /* Discard the Initial encryption keys as soon as |
| 1706 | * a handshake packet could be built. |
| 1707 | */ |
| 1708 | if (ctx->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 1709 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 1710 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 1711 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 1712 | qc_set_timer(ctx); |
| 1713 | ctx->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 1714 | } |
| 1715 | /* Special case for Initial packets: when they have all |
| 1716 | * been sent, select the next level. |
| 1717 | */ |
| 1718 | if ((LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight) && |
| 1719 | tel == QUIC_TLS_ENC_LEVEL_INITIAL) { |
| 1720 | tel = next_tel; |
| 1721 | qel = &qc->els[tel]; |
| 1722 | if (LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
| 1723 | /* If there is no more data for the next level, let's |
| 1724 | * consume a buffer. This is the case for a client |
| 1725 | * which sends only one Initial packet, then wait |
| 1726 | * for additional CRYPTO data from the server to enter the |
| 1727 | * next level. |
| 1728 | */ |
| 1729 | wbuf = q_next_wbuf(qc); |
| 1730 | } |
| 1731 | else { |
| 1732 | /* Let's try to reuse this buffer. */ |
| 1733 | reuse_wbuf = 1; |
| 1734 | } |
| 1735 | } |
| 1736 | else { |
| 1737 | wbuf = q_next_wbuf(qc); |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | out: |
| 1743 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 1744 | return 1; |
| 1745 | |
| 1746 | err: |
| 1747 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | /* Send the QUIC packets which have been prepared for QUIC connections |
| 1752 | * with <ctx> as I/O handler context. |
| 1753 | */ |
| 1754 | int qc_send_ppkts(struct quic_conn_ctx *ctx) |
| 1755 | { |
| 1756 | struct quic_conn *qc; |
| 1757 | struct buffer tmpbuf = { }; |
| 1758 | struct q_buf *rbuf; |
| 1759 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1760 | qc = ctx->conn->qc; |
| 1761 | for (rbuf = q_rbuf(qc); !q_buf_empty(rbuf) ; rbuf = q_next_rbuf(qc)) { |
| 1762 | struct quic_tx_packet *p, *q; |
| 1763 | unsigned int time_sent; |
| 1764 | |
| 1765 | tmpbuf.area = (char *)rbuf->area; |
| 1766 | tmpbuf.size = tmpbuf.data = rbuf->data; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1767 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx, |
| 1769 | &tmpbuf, tmpbuf.data, 0) <= 0) |
| 1770 | break; |
| 1771 | |
| 1772 | qc->tx.bytes += tmpbuf.data; |
| 1773 | time_sent = now_ms; |
| 1774 | /* Reset this buffer to make it available for the next packet to prepare. */ |
| 1775 | q_buf_reset(rbuf); |
| 1776 | /* Remove from <rbuf> the packets which have just been sent. */ |
| 1777 | list_for_each_entry_safe(p, q, &rbuf->pkts, list) { |
| 1778 | p->time_sent = time_sent; |
| 1779 | if (p->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 1780 | p->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 1781 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1782 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1783 | qc->path->in_flight += p->in_flight_len; |
| 1784 | p->pktns->tx.in_flight += p->in_flight_len; |
| 1785 | if (p->in_flight_len) |
| 1786 | qc_set_timer(ctx); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1787 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, p); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1788 | LIST_DEL(&p->list); |
| 1789 | } |
| 1790 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1791 | |
| 1792 | return 1; |
| 1793 | } |
| 1794 | |
| 1795 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 1796 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 1797 | * a HANDSHAKE_DONE frame. |
| 1798 | * Return 1 if succeeded, 0 if not. |
| 1799 | */ |
| 1800 | static int quic_build_post_handshake_frames(struct quic_conn *conn) |
| 1801 | { |
| 1802 | int i; |
| 1803 | struct quic_frame *frm; |
| 1804 | |
| 1805 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 1806 | if (!objt_server(conn->conn->target)) { |
| 1807 | frm = pool_alloc(pool_head_quic_frame); |
| 1808 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
| 1809 | LIST_ADDQ(&conn->tx.frms_to_send, &frm->list); |
| 1810 | } |
| 1811 | |
| 1812 | for (i = 1; i < conn->rx_tps.active_connection_id_limit; i++) { |
| 1813 | struct quic_connection_id *cid; |
| 1814 | |
| 1815 | frm = pool_alloc(pool_head_quic_frame); |
| 1816 | memset(frm, 0, sizeof *frm); |
| 1817 | cid = new_quic_cid(&conn->cids, i); |
| 1818 | if (!frm || !cid) |
| 1819 | goto err; |
| 1820 | |
| 1821 | quic_connection_id_to_frm_cpy(frm, cid); |
| 1822 | LIST_ADDQ(&conn->tx.frms_to_send, &frm->list); |
| 1823 | } |
| 1824 | |
| 1825 | return 1; |
| 1826 | |
| 1827 | err: |
| 1828 | free_quic_conn_cids(conn); |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1833 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1834 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1835 | struct eb64_node *n; |
| 1836 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1837 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1838 | n = eb64_first(&arngs->root); |
| 1839 | while (n) { |
| 1840 | struct eb64_node *next; |
| 1841 | |
| 1842 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 1843 | next = eb64_next(n); |
| 1844 | eb64_delete(n); |
| 1845 | free(ar); |
| 1846 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1850 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 1851 | * descending order. |
| 1852 | */ |
| 1853 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 1854 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1855 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1856 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 1861 | * encoded size until it goes below <limit>. |
| 1862 | * Returns 1 if succeded, 0 if not (no more element to remove). |
| 1863 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1864 | static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1865 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1866 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1867 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1868 | last = eb64_last(&arngs->root); |
| 1869 | while (last && arngs->enc_sz > limit) { |
| 1870 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1871 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1872 | prev = eb64_prev(last); |
| 1873 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1874 | return 0; |
| 1875 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1876 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 1877 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 1878 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 1879 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 1880 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 1881 | --arngs->sz; |
| 1882 | eb64_delete(last); |
| 1883 | pool_free(pool_head_quic_arng, last); |
| 1884 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | return 1; |
| 1888 | } |
| 1889 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1890 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 1891 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 1892 | { |
| 1893 | struct eb64_node *node, *next; |
| 1894 | struct quic_arng_node *ar, *ar_next; |
| 1895 | |
| 1896 | node = eb64_last(&arngs->root); |
| 1897 | if (!node) |
| 1898 | return; |
| 1899 | |
| 1900 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 1901 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 1902 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 1903 | |
| 1904 | while ((next = eb64_prev(node))) { |
| 1905 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 1906 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 1907 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 1908 | node = next; |
| 1909 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | /* Insert in <root> ebtree <node> node with <ar> as range value. |
| 1914 | * Returns the ebtree node which has been inserted. |
| 1915 | */ |
| 1916 | static inline |
| 1917 | struct eb64_node *quic_insert_new_range(struct eb_root *root, |
| 1918 | struct quic_arng_node *node, |
| 1919 | struct quic_arng *ar) |
| 1920 | { |
| 1921 | node->first.key = ar->first; |
| 1922 | node->last = ar->last; |
| 1923 | return eb64_insert(root, &node->first); |
| 1924 | } |
| 1925 | |
| 1926 | /* 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] | 1927 | * 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] | 1928 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1929 | * |
| 1930 | * Descending order |
| 1931 | * -------------> |
| 1932 | * range1 range2 |
| 1933 | * ..........|--------|..............|--------| |
| 1934 | * ^ ^ ^ ^ |
| 1935 | * | | | | |
| 1936 | * last1 first1 last2 first2 |
| 1937 | * ..........+--------+--------------+--------+...... |
| 1938 | * diff1 gap12 diff2 |
| 1939 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1940 | * To encode the previous list of ranges we must encode integers as follows in |
| 1941 | * descending order: |
| 1942 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1943 | * with diff1 = last1 - first1 |
| 1944 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1945 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1946 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1947 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1948 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 1949 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1950 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1951 | struct eb64_node *le; |
| 1952 | struct quic_arng_node *new_node; |
| 1953 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1954 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1955 | new = NULL; |
| 1956 | if (eb_is_empty(&arngs->root)) { |
| 1957 | /* First range insertion. */ |
| 1958 | new_node = pool_alloc(pool_head_quic_arng); |
| 1959 | if (!new_node) |
| 1960 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1961 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1962 | quic_insert_new_range(&arngs->root, new_node, ar); |
| 1963 | /* Increment the size of these ranges. */ |
| 1964 | arngs->sz++; |
| 1965 | goto out; |
| 1966 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1967 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1968 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 1969 | if (!le) { |
| 1970 | /* New insertion */ |
| 1971 | new_node = pool_alloc(pool_head_quic_arng); |
| 1972 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1973 | return 0; |
| 1974 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1975 | new = quic_insert_new_range(&arngs->root, new_node, ar); |
| 1976 | /* Increment the size of these ranges. */ |
| 1977 | arngs->sz++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1978 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1979 | else { |
| 1980 | struct quic_arng_node *le_ar = |
| 1981 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1982 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1983 | /* Already existing range */ |
| 1984 | if (le_ar->first.key <= ar->first && le_ar->last >= ar->last) |
| 1985 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1986 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1987 | if (le_ar->last + 1 >= ar->first) { |
| 1988 | le_ar->last = ar->last; |
| 1989 | new = le; |
| 1990 | new_node = le_ar; |
| 1991 | } |
| 1992 | else { |
| 1993 | /* New insertion */ |
| 1994 | new_node = pool_alloc(pool_head_quic_arng); |
| 1995 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1996 | return 0; |
| 1997 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 1998 | new = quic_insert_new_range(&arngs->root, new_node, ar); |
| 1999 | /* Increment the size of these ranges. */ |
| 2000 | arngs->sz++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2001 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2002 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2003 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2004 | /* Verify that the new inserted node does not overlap the nodes |
| 2005 | * which follow it. |
| 2006 | */ |
| 2007 | if (new) { |
| 2008 | uint64_t new_node_last; |
| 2009 | struct eb64_node *next; |
| 2010 | struct quic_arng_node *next_node; |
| 2011 | |
| 2012 | new_node_last = new_node->last; |
| 2013 | while ((next = eb64_next(new))) { |
| 2014 | next_node = |
| 2015 | eb64_entry(&next->node, struct quic_arng_node, first); |
| 2016 | if (new_node_last + 1 < next_node->first.key) |
| 2017 | break; |
| 2018 | |
| 2019 | if (next_node->last > new_node->last) |
| 2020 | new_node->last = next_node->last; |
| 2021 | eb64_delete(next); |
| 2022 | free(next_node); |
| 2023 | /* Decrement the size of these ranges. */ |
| 2024 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2025 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2028 | quic_arngs_set_enc_sz(arngs); |
| 2029 | |
| 2030 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2031 | return 1; |
| 2032 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2033 | /* Remove the header protection of packets at <el> encryption level. |
| 2034 | * Always succeeds. |
| 2035 | */ |
| 2036 | static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx) |
| 2037 | { |
| 2038 | struct quic_tls_ctx *tls_ctx; |
| 2039 | struct quic_rx_packet *pqpkt, *qqpkt; |
| 2040 | struct quic_enc_level *app_qel; |
| 2041 | |
| 2042 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2043 | app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2044 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 2045 | if (el == app_qel && objt_listener(ctx->conn->target) && ctx->state < QUIC_HS_ST_COMPLETE) { |
| 2046 | TRACE_PROTO("hp not removed (handshake not completed)", |
| 2047 | QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2048 | goto out; |
| 2049 | } |
| 2050 | tls_ctx = &el->tls_ctx; |
| 2051 | list_for_each_entry_safe(pqpkt, qqpkt, &el->rx.pqpkts, list) { |
| 2052 | if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 2053 | pqpkt->data + pqpkt->pn_offset, |
| 2054 | pqpkt->data, pqpkt->data + pqpkt->len, ctx)) { |
| 2055 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2056 | /* XXX TO DO XXX */ |
| 2057 | } |
| 2058 | else { |
| 2059 | /* The AAD includes the packet number field */ |
| 2060 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 2061 | /* Store the packet into the tree of packets to decrypt. */ |
| 2062 | pqpkt->pn_node.key = pqpkt->pn; |
| 2063 | quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 2064 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt); |
| 2065 | } |
| 2066 | quic_rx_packet_list_del(pqpkt); |
| 2067 | } |
| 2068 | |
| 2069 | out: |
| 2070 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2071 | } |
| 2072 | |
| 2073 | /* Process all the CRYPTO frame at <el> encryption level. |
| 2074 | * Return 1 if succeeded, 0 if not. |
| 2075 | */ |
| 2076 | static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el, |
| 2077 | struct quic_conn_ctx *ctx) |
| 2078 | { |
| 2079 | struct eb64_node *node; |
| 2080 | |
| 2081 | TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2082 | node = eb64_first(&el->rx.crypto.frms); |
| 2083 | while (node) { |
| 2084 | struct quic_rx_crypto_frm *cf; |
| 2085 | |
| 2086 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 2087 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 2088 | break; |
| 2089 | |
| 2090 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 2091 | goto err; |
| 2092 | |
| 2093 | node = eb64_next(node); |
| 2094 | quic_rx_packet_refdec(cf->pkt); |
| 2095 | eb64_delete(&cf->offset_node); |
| 2096 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 2097 | } |
| 2098 | |
| 2099 | TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2100 | return 1; |
| 2101 | |
| 2102 | err: |
| 2103 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | /* Process all the packets at <el> encryption level. |
| 2108 | * Return 1 if succeeded, 0 if not. |
| 2109 | */ |
| 2110 | int qc_treat_rx_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx) |
| 2111 | { |
| 2112 | struct quic_tls_ctx *tls_ctx; |
| 2113 | struct eb64_node *node; |
| 2114 | |
| 2115 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2116 | tls_ctx = &el->tls_ctx; |
| 2117 | node = eb64_first(&el->rx.pkts); |
| 2118 | while (node) { |
| 2119 | struct quic_rx_packet *pkt; |
| 2120 | |
| 2121 | pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node); |
| 2122 | if (!qc_pkt_decrypt(pkt, tls_ctx)) { |
| 2123 | /* Drop the packet */ |
| 2124 | TRACE_PROTO("packet decryption failed -> dropped", |
| 2125 | QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt); |
| 2126 | } |
| 2127 | else { |
| 2128 | int drop; |
| 2129 | |
| 2130 | drop = 0; |
| 2131 | if (!qc_parse_pkt_frms(pkt, ctx, el)) |
| 2132 | drop = 1; |
| 2133 | |
| 2134 | if (drop) { |
| 2135 | /* Drop the packet */ |
| 2136 | TRACE_PROTO("packet parsing failed -> dropped", |
| 2137 | QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt); |
| 2138 | } |
| 2139 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2140 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 2141 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2142 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) { |
| 2143 | el->pktns->rx.nb_ack_eliciting++; |
| 2144 | if (!(el->pktns->rx.nb_ack_eliciting & 1)) |
| 2145 | el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 2146 | } |
| 2147 | |
| 2148 | /* Update the largest packet number. */ |
| 2149 | if (pkt->pn > el->pktns->rx.largest_pn) |
| 2150 | el->pktns->rx.largest_pn = pkt->pn; |
| 2151 | |
| 2152 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2153 | if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2154 | TRACE_DEVEL("Could not update ack range list", |
| 2155 | QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2156 | node = eb64_next(node); |
| 2157 | quic_rx_packet_eb64_delete(&pkt->pn_node); |
| 2158 | free_quic_rx_packet(pkt); |
| 2159 | goto err; |
| 2160 | } |
| 2161 | |
| 2162 | } |
| 2163 | } |
| 2164 | node = eb64_next(node); |
| 2165 | quic_rx_packet_eb64_delete(&pkt->pn_node); |
| 2166 | free_quic_rx_packet(pkt); |
| 2167 | } |
| 2168 | |
| 2169 | if (!qc_treat_rx_crypto_frms(el, ctx)) |
| 2170 | goto err; |
| 2171 | |
| 2172 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2173 | return 1; |
| 2174 | |
| 2175 | err: |
| 2176 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2177 | return 0; |
| 2178 | } |
| 2179 | |
| 2180 | /* Called during handshakes to parse and build Initial and Handshake packets for QUIC |
| 2181 | * connections with <ctx> as I/O handler context. |
| 2182 | * Returns 1 if succeeded, 0 if not. |
| 2183 | */ |
| 2184 | int qc_do_hdshk(struct quic_conn_ctx *ctx) |
| 2185 | { |
| 2186 | int ssl_err; |
| 2187 | struct quic_conn *quic_conn; |
| 2188 | enum quic_tls_enc_level tel, next_tel; |
| 2189 | struct quic_enc_level *qel, *next_qel; |
| 2190 | struct quic_tls_ctx *tls_ctx; |
| 2191 | |
| 2192 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state); |
| 2193 | |
| 2194 | ssl_err = SSL_ERROR_NONE; |
| 2195 | quic_conn = ctx->conn->qc; |
| 2196 | if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state)) |
| 2197 | goto err; |
| 2198 | |
| 2199 | qel = &quic_conn->els[tel]; |
| 2200 | next_qel = &quic_conn->els[next_tel]; |
| 2201 | |
| 2202 | next_level: |
| 2203 | tls_ctx = &qel->tls_ctx; |
| 2204 | |
| 2205 | /* If the header protection key for this level has been derived, |
| 2206 | * remove the packet header protections. |
| 2207 | */ |
| 2208 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && |
| 2209 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 2210 | qc_rm_hp_pkts(qel, ctx); |
| 2211 | |
| 2212 | if (!eb_is_empty(&qel->rx.pkts) && |
| 2213 | !qc_treat_rx_pkts(qel, ctx)) |
| 2214 | goto err; |
| 2215 | |
| 2216 | if (!qc_prep_hdshk_pkts(ctx)) |
| 2217 | goto err; |
| 2218 | |
| 2219 | if (!qc_send_ppkts(ctx)) |
| 2220 | goto err; |
| 2221 | |
| 2222 | /* Check if there is something to do for the next level. |
| 2223 | */ |
| 2224 | if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
| 2225 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) { |
| 2226 | qel = next_qel; |
| 2227 | goto next_level; |
| 2228 | } |
| 2229 | |
| 2230 | /* If the handshake has not been completed -> out! */ |
| 2231 | if (ctx->state < QUIC_HS_ST_COMPLETE) |
| 2232 | goto out; |
| 2233 | |
| 2234 | /* Discard the Handshake keys. */ |
| 2235 | quic_tls_discard_keys(&quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 2236 | quic_pktns_discard(quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, quic_conn); |
| 2237 | qc_set_timer(ctx); |
| 2238 | if (!quic_build_post_handshake_frames(quic_conn) || |
| 2239 | !qc_prep_phdshk_pkts(quic_conn) || |
| 2240 | !qc_send_ppkts(ctx)) |
| 2241 | goto err; |
| 2242 | |
| 2243 | out: |
| 2244 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state); |
| 2245 | return 1; |
| 2246 | |
| 2247 | err: |
| 2248 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
| 2252 | /* Allocate a new QUIC connection and return it if succeeded, NULL if not. */ |
| 2253 | struct quic_conn *new_quic_conn(uint32_t version) |
| 2254 | { |
| 2255 | struct quic_conn *quic_conn; |
| 2256 | |
| 2257 | quic_conn = pool_alloc(pool_head_quic_conn); |
| 2258 | if (quic_conn) { |
| 2259 | memset(quic_conn, 0, sizeof *quic_conn); |
| 2260 | quic_conn->version = version; |
| 2261 | } |
| 2262 | |
| 2263 | return quic_conn; |
| 2264 | } |
| 2265 | |
| 2266 | /* Unitialize <qel> QUIC encryption level. Never fails. */ |
| 2267 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 2268 | { |
| 2269 | int i; |
| 2270 | |
| 2271 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 2272 | if (qel->tx.crypto.bufs[i]) { |
| 2273 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 2274 | qel->tx.crypto.bufs[i] = NULL; |
| 2275 | } |
| 2276 | } |
| 2277 | free(qel->tx.crypto.bufs); |
| 2278 | qel->tx.crypto.bufs = NULL; |
| 2279 | } |
| 2280 | |
| 2281 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 2282 | * connetion allocating everything needed. |
| 2283 | * Returns 1 if succeeded, 0 if not. |
| 2284 | */ |
| 2285 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 2286 | enum quic_tls_enc_level level) |
| 2287 | { |
| 2288 | struct quic_enc_level *qel; |
| 2289 | |
| 2290 | qel = &qc->els[level]; |
| 2291 | qel->level = quic_to_ssl_enc_level(level); |
| 2292 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 2293 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 2294 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 2295 | qel->tls_ctx.rx.flags = 0; |
| 2296 | qel->tls_ctx.tx.flags = 0; |
| 2297 | |
| 2298 | qel->rx.pkts = EB_ROOT; |
| 2299 | LIST_INIT(&qel->rx.pqpkts); |
| 2300 | |
| 2301 | /* Allocate only one buffer. */ |
| 2302 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 2303 | if (!qel->tx.crypto.bufs) |
| 2304 | goto err; |
| 2305 | |
| 2306 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 2307 | if (!qel->tx.crypto.bufs[0]) |
| 2308 | goto err; |
| 2309 | |
| 2310 | qel->tx.crypto.bufs[0]->sz = 0; |
| 2311 | qel->tx.crypto.nb_buf = 1; |
| 2312 | |
| 2313 | qel->tx.crypto.sz = 0; |
| 2314 | qel->tx.crypto.offset = 0; |
| 2315 | |
| 2316 | return 1; |
| 2317 | |
| 2318 | err: |
| 2319 | free(qel->tx.crypto.bufs); |
| 2320 | qel->tx.crypto.bufs = NULL; |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | /* Release the memory allocated for <buf> array of buffers, with <nb> as size. |
| 2325 | * Never fails. |
| 2326 | */ |
| 2327 | static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb) |
| 2328 | { |
| 2329 | struct q_buf **p; |
| 2330 | |
| 2331 | if (!bufs) |
| 2332 | return; |
| 2333 | |
| 2334 | p = bufs; |
| 2335 | while (--nb) { |
| 2336 | if (!*p) { |
| 2337 | p++; |
| 2338 | continue; |
| 2339 | } |
| 2340 | free((*p)->area); |
| 2341 | (*p)->area = NULL; |
| 2342 | free(*p); |
| 2343 | *p = NULL; |
| 2344 | p++; |
| 2345 | } |
| 2346 | free(bufs); |
| 2347 | } |
| 2348 | |
| 2349 | /* Allocate an array or <nb> buffers of <sz> bytes each. |
| 2350 | * Return this array if succeeded, NULL if failed. |
| 2351 | */ |
| 2352 | static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz) |
| 2353 | { |
| 2354 | int i; |
| 2355 | struct q_buf **bufs, **p; |
| 2356 | |
| 2357 | bufs = calloc(nb, sizeof *bufs); |
| 2358 | if (!bufs) |
| 2359 | return NULL; |
| 2360 | |
| 2361 | i = 0; |
| 2362 | p = bufs; |
| 2363 | while (i++ < nb) { |
| 2364 | *p = calloc(1, sizeof **p); |
| 2365 | if (!*p) |
| 2366 | goto err; |
| 2367 | |
| 2368 | (*p)->area = malloc(sz); |
| 2369 | if (!(*p)->area) |
| 2370 | goto err; |
| 2371 | |
| 2372 | (*p)->pos = (*p)->area; |
| 2373 | (*p)->end = (*p)->area + sz; |
| 2374 | (*p)->data = 0; |
| 2375 | LIST_INIT(&(*p)->pkts); |
| 2376 | p++; |
| 2377 | } |
| 2378 | |
| 2379 | return bufs; |
| 2380 | |
| 2381 | err: |
| 2382 | free_quic_conn_tx_bufs(bufs, nb); |
| 2383 | return NULL; |
| 2384 | } |
| 2385 | |
| 2386 | /* Release all the memory allocated for <conn> QUIC connection. */ |
| 2387 | static void quic_conn_free(struct quic_conn *conn) |
| 2388 | { |
| 2389 | int i; |
| 2390 | |
| 2391 | free_quic_conn_cids(conn); |
| 2392 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
| 2393 | quic_conn_enc_level_uninit(&conn->els[i]); |
| 2394 | free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf); |
| 2395 | if (conn->timer_task) |
| 2396 | task_destroy(conn->timer_task); |
| 2397 | pool_free(pool_head_quic_conn, conn); |
| 2398 | } |
| 2399 | |
| 2400 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 2401 | static struct task *process_timer(struct task *task, void *ctx, unsigned short state) |
| 2402 | { |
| 2403 | struct quic_conn_ctx *conn_ctx; |
| 2404 | struct quic_conn *qc; |
| 2405 | struct quic_pktns *pktns; |
| 2406 | |
| 2407 | |
| 2408 | conn_ctx = task->context; |
| 2409 | qc = conn_ctx->conn->qc; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame^] | 2410 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn, |
| 2411 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2412 | task->expire = TICK_ETERNITY; |
| 2413 | pktns = quic_loss_pktns(qc); |
| 2414 | if (tick_isset(pktns->tx.loss_time)) { |
| 2415 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 2416 | |
| 2417 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 2418 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 2419 | qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms); |
| 2420 | qc_set_timer(conn_ctx); |
| 2421 | goto out; |
| 2422 | } |
| 2423 | |
| 2424 | if (qc->path->in_flight) { |
| 2425 | pktns = quic_pto_pktns(qc, conn_ctx->state >= QUIC_HS_ST_COMPLETE, NULL); |
| 2426 | pktns->tx.pto_probe = 1; |
| 2427 | } |
| 2428 | else if (objt_server(qc->conn->target) && conn_ctx->state <= QUIC_HS_ST_COMPLETE) { |
| 2429 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2430 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2431 | |
| 2432 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 2433 | hel->pktns->tx.pto_probe = 1; |
| 2434 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 2435 | iel->pktns->tx.pto_probe = 1; |
| 2436 | } |
| 2437 | qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS; |
| 2438 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 2439 | qc->path->loss.pto_count++; |
| 2440 | |
| 2441 | out: |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2442 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2443 | |
| 2444 | return task; |
| 2445 | } |
| 2446 | |
| 2447 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 2448 | * connections used to identify the first Initial packets of client connecting |
| 2449 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 2450 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 2451 | * <scid> is the source connection ID with <scid_len> as length. |
| 2452 | * Returns 1 if succeeded, 0 if not. |
| 2453 | */ |
| 2454 | int qc_new_conn_init(struct quic_conn *qc, int ipv4, |
| 2455 | struct eb_root *quic_initial_clients, |
| 2456 | struct eb_root *quic_clients, |
| 2457 | unsigned char *dcid, size_t dcid_len, |
| 2458 | unsigned char *scid, size_t scid_len) |
| 2459 | { |
| 2460 | int i; |
| 2461 | /* Initial CID. */ |
| 2462 | struct quic_connection_id *icid; |
| 2463 | |
| 2464 | TRACE_ENTER(QUIC_EV_CONN_INIT, qc->conn); |
| 2465 | qc->cids = EB_ROOT; |
| 2466 | /* QUIC Server (or listener). */ |
| 2467 | if (objt_listener(qc->conn->target)) { |
| 2468 | /* Copy the initial DCID. */ |
| 2469 | qc->odcid.len = dcid_len; |
| 2470 | if (qc->odcid.len) |
| 2471 | memcpy(qc->odcid.data, dcid, dcid_len); |
| 2472 | |
| 2473 | /* Copy the SCID as our DCID for this connection. */ |
| 2474 | if (scid_len) |
| 2475 | memcpy(qc->dcid.data, scid, scid_len); |
| 2476 | qc->dcid.len = scid_len; |
| 2477 | } |
| 2478 | /* QUIC Client (outgoing connection to servers) */ |
| 2479 | else { |
| 2480 | if (dcid_len) |
| 2481 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 2482 | qc->dcid.len = dcid_len; |
| 2483 | } |
| 2484 | |
| 2485 | /* Initialize the output buffer */ |
| 2486 | qc->obuf.pos = qc->obuf.data; |
| 2487 | |
| 2488 | icid = new_quic_cid(&qc->cids, 0); |
| 2489 | if (!icid) |
| 2490 | return 0; |
| 2491 | |
| 2492 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 2493 | qc->scid = icid->cid; |
| 2494 | |
| 2495 | /* Insert the DCID the QUIC client has choosen (only for listeners) */ |
| 2496 | if (objt_listener(qc->conn->target)) |
| 2497 | ebmb_insert(quic_initial_clients, &qc->odcid_node, qc->odcid.len); |
| 2498 | |
| 2499 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 2500 | ebmb_insert(quic_clients, &qc->scid_node, qc->scid.len); |
| 2501 | |
| 2502 | /* Packet number spaces initialization. */ |
| 2503 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 2504 | quic_pktns_init(&qc->pktns[i]); |
| 2505 | /* QUIC encryption level context initialization. */ |
| 2506 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 2507 | if (!quic_conn_enc_level_init(qc, i)) |
| 2508 | goto err; |
| 2509 | /* Initialize the packet number space. */ |
| 2510 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 2511 | } |
| 2512 | |
| 2513 | /* TX part. */ |
| 2514 | LIST_INIT(&qc->tx.frms_to_send); |
| 2515 | qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ); |
| 2516 | if (!qc->tx.bufs) |
| 2517 | goto err; |
| 2518 | |
| 2519 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 2520 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 2521 | qc->tx.bytes = 0; |
| 2522 | qc->tx.nb_pto_dgrams = 0; |
| 2523 | /* RX part. */ |
| 2524 | qc->rx.bytes = 0; |
| 2525 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2526 | /* XXX TO DO: Only one path at this time. */ |
| 2527 | qc->path = &qc->paths[0]; |
| 2528 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 2529 | |
| 2530 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc->conn); |
| 2531 | |
| 2532 | return 1; |
| 2533 | |
| 2534 | err: |
| 2535 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc->conn); |
| 2536 | quic_conn_free(qc); |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
| 2540 | /* Initialize the timer task of <qc> QUIC connection. |
| 2541 | * Returns 1 if succeeded, 0 if not. |
| 2542 | */ |
| 2543 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 2544 | { |
| 2545 | qc->timer_task = task_new(MAX_THREADS_MASK); |
| 2546 | if (!qc->timer_task) |
| 2547 | return 0; |
| 2548 | |
| 2549 | qc->timer = TICK_ETERNITY; |
| 2550 | qc->timer_task->process = process_timer; |
| 2551 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 2552 | |
| 2553 | return 1; |
| 2554 | } |
| 2555 | |
| 2556 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 2557 | * past one byte of this buffer. |
| 2558 | */ |
| 2559 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 2560 | struct quic_rx_packet *pkt) |
| 2561 | { |
| 2562 | unsigned char dcid_len, scid_len; |
| 2563 | |
| 2564 | /* Version */ |
| 2565 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 2566 | return 0; |
| 2567 | |
| 2568 | if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ }; |
| 2569 | |
| 2570 | /* Destination Connection ID Length */ |
| 2571 | dcid_len = *(*buf)++; |
| 2572 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 2573 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 2574 | /* XXX MUST BE DROPPED */ |
| 2575 | return 0; |
| 2576 | |
| 2577 | if (dcid_len) { |
| 2578 | /* Check that the length of this received DCID matches the CID lengths |
| 2579 | * of our implementation for non Initials packets only. |
| 2580 | */ |
| 2581 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN) |
| 2582 | return 0; |
| 2583 | |
| 2584 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 2585 | } |
| 2586 | |
| 2587 | pkt->dcid.len = dcid_len; |
| 2588 | *buf += dcid_len; |
| 2589 | |
| 2590 | /* Source Connection ID Length */ |
| 2591 | scid_len = *(*buf)++; |
| 2592 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 2593 | /* XXX MUST BE DROPPED */ |
| 2594 | return 0; |
| 2595 | |
| 2596 | if (scid_len) |
| 2597 | memcpy(pkt->scid.data, *buf, scid_len); |
| 2598 | pkt->scid.len = scid_len; |
| 2599 | *buf += scid_len; |
| 2600 | |
| 2601 | return 1; |
| 2602 | } |
| 2603 | |
| 2604 | /* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn> |
| 2605 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 2606 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 2607 | * the packet first byte. |
| 2608 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 2609 | * Returns 1 if succeeded, 0 if not. |
| 2610 | */ |
| 2611 | static inline int qc_try_rm_hp(struct quic_rx_packet *pkt, |
| 2612 | unsigned char **buf, unsigned char *beg, |
| 2613 | const unsigned char *end, |
| 2614 | struct quic_conn_ctx *ctx) |
| 2615 | { |
| 2616 | unsigned char *pn = NULL; /* Packet number field */ |
| 2617 | enum quic_tls_enc_level tel; |
| 2618 | struct quic_enc_level *qel; |
| 2619 | /* Only for traces. */ |
| 2620 | struct quic_rx_packet *qpkt_trace; |
| 2621 | |
| 2622 | qpkt_trace = NULL; |
| 2623 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx->conn); |
| 2624 | /* The packet number is here. This is also the start minus |
| 2625 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 2626 | * protection. |
| 2627 | */ |
| 2628 | pn = *buf; |
| 2629 | tel = quic_packet_type_enc_level(pkt->type); |
| 2630 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 2631 | TRACE_DEVEL("Wrong enc. level", QUIC_EV_CONN_TRMHP, ctx->conn); |
| 2632 | goto err; |
| 2633 | } |
| 2634 | |
| 2635 | qel = &ctx->conn->qc->els[tel]; |
| 2636 | |
| 2637 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 2638 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn); |
| 2639 | goto err; |
| 2640 | } |
| 2641 | |
| 2642 | if ((qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
| 2643 | (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->state >= QUIC_HS_ST_COMPLETE)) { |
| 2644 | /* Note that the following function enables us to unprotect the packet |
| 2645 | * number and its length subsequently used to decrypt the entire |
| 2646 | * packets. |
| 2647 | */ |
| 2648 | if (!qc_do_rm_hp(pkt, &qel->tls_ctx, |
| 2649 | qel->pktns->rx.largest_pn, pn, beg, end, ctx)) { |
| 2650 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx->conn); |
| 2651 | goto err; |
| 2652 | } |
| 2653 | |
| 2654 | /* The AAD includes the packet number field found at <pn>. */ |
| 2655 | pkt->aad_len = pn - beg + pkt->pnl; |
| 2656 | qpkt_trace = pkt; |
| 2657 | /* Store the packet */ |
| 2658 | pkt->pn_node.key = pkt->pn; |
| 2659 | quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 2660 | } |
| 2661 | else { |
| 2662 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx->conn, pkt); |
| 2663 | pkt->pn_offset = pn - beg; |
| 2664 | quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt); |
| 2665 | } |
| 2666 | |
| 2667 | memcpy(pkt->data, beg, pkt->len); |
| 2668 | /* Updtate the offset of <*buf> for the next QUIC packet. */ |
| 2669 | *buf = beg + pkt->len; |
| 2670 | |
| 2671 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace); |
| 2672 | return 1; |
| 2673 | |
| 2674 | err: |
| 2675 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace); |
| 2676 | return 0; |
| 2677 | } |
| 2678 | |
| 2679 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 2680 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 2681 | */ |
| 2682 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 2683 | unsigned char byte0, int *long_header) |
| 2684 | { |
| 2685 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 2686 | pkt->type = |
| 2687 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 2688 | *long_header = 1; |
| 2689 | } |
| 2690 | else { |
| 2691 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 2692 | *long_header = 0; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 2697 | struct quic_rx_packet *pkt, |
| 2698 | struct quic_dgram_ctx *dgram_ctx, |
| 2699 | struct sockaddr_storage *saddr) |
| 2700 | { |
| 2701 | unsigned char *beg; |
| 2702 | uint64_t len; |
| 2703 | struct quic_conn *qc; |
| 2704 | struct eb_root *cids; |
| 2705 | struct ebmb_node *node; |
| 2706 | struct connection *srv_conn; |
| 2707 | struct quic_conn_ctx *conn_ctx; |
| 2708 | int long_header; |
| 2709 | |
| 2710 | qc = NULL; |
| 2711 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 2712 | if (end <= *buf) |
| 2713 | goto err; |
| 2714 | |
| 2715 | /* Fixed bit */ |
| 2716 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 2717 | /* XXX TO BE DISCARDED */ |
| 2718 | goto err; |
| 2719 | |
| 2720 | srv_conn = dgram_ctx->owner; |
| 2721 | beg = *buf; |
| 2722 | /* Header form */ |
| 2723 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 2724 | if (long_header) { |
| 2725 | size_t cid_lookup_len; |
| 2726 | |
| 2727 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 2728 | goto err; |
| 2729 | |
| 2730 | /* For Initial packets, and for servers (QUIC clients connections), |
| 2731 | * there is no Initial connection IDs storage. |
| 2732 | */ |
| 2733 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 2734 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 2735 | cid_lookup_len = pkt->dcid.len; |
| 2736 | } |
| 2737 | else { |
| 2738 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 2739 | cid_lookup_len = QUIC_CID_LEN; |
| 2740 | } |
| 2741 | |
| 2742 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 2743 | if (!node) |
| 2744 | goto err; |
| 2745 | |
| 2746 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 2747 | |
| 2748 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 2749 | qc->dcid.len = pkt->scid.len; |
| 2750 | if (pkt->scid.len) |
| 2751 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 2752 | } |
| 2753 | |
| 2754 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 2755 | uint64_t token_len; |
| 2756 | |
| 2757 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 2758 | goto err; |
| 2759 | |
| 2760 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 2761 | * A server which sends an Initial packet must not set the token. |
| 2762 | * So, a client which receives an Initial packet with a token |
| 2763 | * MUST discard the packet or generate a connection error with |
| 2764 | * PROTOCOL_VIOLATION as type. |
| 2765 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 2766 | */ |
| 2767 | pkt->token_len = token_len; |
| 2768 | } |
| 2769 | } |
| 2770 | else { |
| 2771 | /* XXX TO DO: Short header XXX */ |
| 2772 | if (end - *buf < QUIC_CID_LEN) |
| 2773 | goto err; |
| 2774 | |
| 2775 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 2776 | node = ebmb_lookup(cids, *buf, QUIC_CID_LEN); |
| 2777 | if (!node) |
| 2778 | goto err; |
| 2779 | |
| 2780 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 2781 | *buf += QUIC_CID_LEN; |
| 2782 | } |
| 2783 | /* Store the DCID used for this packet to check the packet which |
| 2784 | * come in this UDP datagram match with it. |
| 2785 | */ |
| 2786 | if (!dgram_ctx->dcid_node) |
| 2787 | dgram_ctx->dcid_node = node; |
| 2788 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 2789 | * have a length field. |
| 2790 | */ |
| 2791 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 2792 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 2793 | goto err; |
| 2794 | |
| 2795 | pkt->len = len; |
| 2796 | } |
| 2797 | else if (!long_header) { |
| 2798 | /* A short packet is the last one of an UDP datagram. */ |
| 2799 | pkt->len = end - *buf; |
| 2800 | } |
| 2801 | |
| 2802 | conn_ctx = qc->conn->xprt_ctx; |
| 2803 | |
| 2804 | /* Increase the total length of this packet by the header length. */ |
| 2805 | pkt->len += *buf - beg; |
| 2806 | /* Do not check the DCID node before the length. */ |
| 2807 | if (dgram_ctx->dcid_node != node) { |
| 2808 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn); |
| 2809 | goto err; |
| 2810 | } |
| 2811 | |
| 2812 | if (pkt->len > sizeof pkt->data) { |
| 2813 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len); |
| 2814 | goto err; |
| 2815 | } |
| 2816 | |
| 2817 | if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx)) |
| 2818 | goto err; |
| 2819 | |
| 2820 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 2821 | if (conn_ctx) |
| 2822 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 2823 | |
| 2824 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn); |
| 2825 | |
| 2826 | return pkt->len; |
| 2827 | |
| 2828 | err: |
| 2829 | TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_ESPKT, qc ? qc->conn : NULL); |
| 2830 | return -1; |
| 2831 | } |
| 2832 | |
| 2833 | static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 2834 | struct quic_rx_packet *pkt, |
| 2835 | struct quic_dgram_ctx *dgram_ctx, |
| 2836 | struct sockaddr_storage *saddr) |
| 2837 | { |
| 2838 | unsigned char *beg; |
| 2839 | uint64_t len; |
| 2840 | struct quic_conn *qc; |
| 2841 | struct eb_root *cids; |
| 2842 | struct ebmb_node *node; |
| 2843 | struct listener *l; |
| 2844 | struct quic_conn_ctx *conn_ctx; |
| 2845 | int long_header = 0; |
| 2846 | |
| 2847 | qc = NULL; |
| 2848 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 2849 | if (end <= *buf) |
| 2850 | goto err; |
| 2851 | |
| 2852 | /* Fixed bit */ |
| 2853 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) { |
| 2854 | /* XXX TO BE DISCARDED */ |
| 2855 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 2856 | goto err; |
| 2857 | } |
| 2858 | |
| 2859 | l = dgram_ctx->owner; |
| 2860 | beg = *buf; |
| 2861 | /* Header form */ |
| 2862 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 2863 | if (long_header) { |
| 2864 | unsigned char dcid_len; |
| 2865 | |
| 2866 | if (!quic_packet_read_long_header(buf, end, pkt)) { |
| 2867 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 2868 | goto err; |
| 2869 | } |
| 2870 | |
| 2871 | dcid_len = pkt->dcid.len; |
| 2872 | /* For Initial packets, and for servers (QUIC clients connections), |
| 2873 | * there is no Initial connection IDs storage. |
| 2874 | */ |
| 2875 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 2876 | /* DCIDs of first packets coming from clients may have the same values. |
| 2877 | * Let's distinguish them concatenating the socket addresses to the DCIDs. |
| 2878 | */ |
| 2879 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 2880 | cids = &l->rx.odcids; |
| 2881 | } |
| 2882 | else { |
| 2883 | if (pkt->dcid.len != QUIC_CID_LEN) { |
| 2884 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 2885 | goto err; |
| 2886 | } |
| 2887 | |
| 2888 | cids = &l->rx.cids; |
| 2889 | } |
| 2890 | |
| 2891 | node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len); |
| 2892 | if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN && |
| 2893 | cids == &l->rx.odcids) { |
| 2894 | /* Switch to the definitive tree ->cids containing the final CIDs. */ |
| 2895 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len); |
| 2896 | if (node) { |
| 2897 | /* If found, signal this with NULL as special value for <cids>. */ |
| 2898 | pkt->dcid.len = dcid_len; |
| 2899 | cids = NULL; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | if (!node) { |
| 2904 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
| 2905 | TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT); |
| 2906 | goto err; |
| 2907 | } |
| 2908 | |
| 2909 | qc = new_quic_conn(pkt->version); |
| 2910 | if (!qc) { |
| 2911 | TRACE_PROTO("Non allocated new connection", QUIC_EV_CONN_LPKT); |
| 2912 | goto err; |
| 2913 | } |
| 2914 | |
| 2915 | pkt->qc = qc; |
| 2916 | pkt->saddr = *saddr; |
| 2917 | /* Note that here, odcid_len equals to pkt->dcid.len minus the length |
| 2918 | * of <saddr>. |
| 2919 | */ |
| 2920 | pkt->odcid_len = dcid_len; |
| 2921 | /* Enqueue this packet. */ |
| 2922 | LIST_ADDQ(&l->rx.qpkts, &pkt->rx_list); |
| 2923 | /* Try to accept a new connection. */ |
| 2924 | listener_accept(l); |
| 2925 | if (!qc->conn) { |
| 2926 | TRACE_PROTO("Non accepted connection", QUIC_EV_CONN_LPKT, qc->conn); |
| 2927 | goto err; |
| 2928 | } |
| 2929 | |
| 2930 | if (!quic_conn_init_timer(qc)) { |
| 2931 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc->conn); |
| 2932 | goto err; |
| 2933 | } |
| 2934 | |
| 2935 | /* This is the DCID node sent in this packet by the client. */ |
| 2936 | node = &qc->odcid_node; |
| 2937 | conn_ctx = qc->conn->xprt_ctx; |
| 2938 | SSL_set_quic_transport_params(conn_ctx->ssl, |
| 2939 | qc->enc_params, qc->enc_params_len); |
| 2940 | } |
| 2941 | else { |
| 2942 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids) |
| 2943 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 2944 | else |
| 2945 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 2946 | } |
| 2947 | |
| 2948 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 2949 | uint64_t token_len; |
| 2950 | struct quic_tls_ctx *ctx = |
| 2951 | &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 2952 | |
| 2953 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || |
| 2954 | end - *buf < token_len) { |
| 2955 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 2956 | goto err; |
| 2957 | } |
| 2958 | |
| 2959 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 2960 | * A server which sends an Initial packet must not set the token. |
| 2961 | * So, a client which receives an Initial packet with a token |
| 2962 | * MUST discard the packet or generate a connection error with |
| 2963 | * PROTOCOL_VIOLATION as type. |
| 2964 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 2965 | */ |
| 2966 | pkt->token_len = token_len; |
| 2967 | /* NOTE: the socket address has been concatenated to the destination ID |
| 2968 | * choosen by the client for Initial packets. |
| 2969 | */ |
| 2970 | if (!ctx->rx.hp && !qc_new_isecs(qc->conn, pkt->dcid.data, |
| 2971 | pkt->odcid_len, 1)) { |
| 2972 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 2973 | goto err; |
| 2974 | } |
| 2975 | } |
| 2976 | } |
| 2977 | else { |
| 2978 | if (end - *buf < QUIC_CID_LEN) { |
| 2979 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 2980 | goto err; |
| 2981 | } |
| 2982 | |
| 2983 | cids = &l->rx.cids; |
| 2984 | node = ebmb_lookup(cids, *buf, QUIC_CID_LEN); |
| 2985 | if (!node) { |
| 2986 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 2987 | goto err; |
| 2988 | } |
| 2989 | |
| 2990 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 2991 | *buf += QUIC_CID_LEN; |
| 2992 | } |
| 2993 | |
| 2994 | /* Store the DCID used for this packet to check the packet which |
| 2995 | * come in this UDP datagram match with it. |
| 2996 | */ |
| 2997 | if (!dgram_ctx->dcid_node) { |
| 2998 | dgram_ctx->dcid_node = node; |
| 2999 | dgram_ctx->qc = qc; |
| 3000 | } |
| 3001 | |
| 3002 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3003 | * have a length field. |
| 3004 | */ |
| 3005 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3006 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || |
| 3007 | end - *buf < len) { |
| 3008 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3009 | goto err; |
| 3010 | } |
| 3011 | |
| 3012 | pkt->len = len; |
| 3013 | } |
| 3014 | else if (!long_header) { |
| 3015 | /* A short packet is the last one of an UDP datagram. */ |
| 3016 | pkt->len = end - *buf; |
| 3017 | } |
| 3018 | |
| 3019 | /* Update the state if needed. */ |
| 3020 | conn_ctx = qc->conn->xprt_ctx; |
| 3021 | |
| 3022 | /* Increase the total length of this packet by the header length. */ |
| 3023 | pkt->len += *buf - beg; |
| 3024 | /* Do not check the DCID node before the length. */ |
| 3025 | if (dgram_ctx->dcid_node != node) { |
| 3026 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3027 | goto err; |
| 3028 | } |
| 3029 | |
| 3030 | if (pkt->len > sizeof pkt->data) { |
| 3031 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len); |
| 3032 | goto err; |
| 3033 | } |
| 3034 | |
| 3035 | if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx)) { |
| 3036 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3037 | goto err; |
| 3038 | } |
| 3039 | |
| 3040 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 3041 | if (conn_ctx) |
| 3042 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3043 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt); |
| 3044 | |
| 3045 | return pkt->len; |
| 3046 | |
| 3047 | err: |
| 3048 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT|QUIC_EV_CONN_ELPKT, |
| 3049 | qc ? qc->conn : NULL, pkt); |
| 3050 | return -1; |
| 3051 | } |
| 3052 | |
| 3053 | /* This function builds into <buf> buffer a QUIC long packet header whose size may be computed |
| 3054 | * in advance. This is the reponsability of the caller to check there is enough room in this |
| 3055 | * buffer to build a long header. |
| 3056 | * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded. |
| 3057 | */ |
| 3058 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 3059 | int type, size_t pn_len, struct quic_conn *conn) |
| 3060 | { |
| 3061 | if (type > QUIC_PACKET_TYPE_RETRY) |
| 3062 | return 0; |
| 3063 | |
| 3064 | /* #0 byte flags */ |
| 3065 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 3066 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 3067 | /* Version */ |
| 3068 | quic_write_uint32(buf, end, conn->version); |
| 3069 | *(*buf)++ = conn->dcid.len; |
| 3070 | /* Destination connection ID */ |
| 3071 | if (conn->dcid.len) { |
| 3072 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 3073 | *buf += conn->dcid.len; |
| 3074 | } |
| 3075 | /* Source connection ID */ |
| 3076 | *(*buf)++ = conn->scid.len; |
| 3077 | if (conn->scid.len) { |
| 3078 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 3079 | *buf += conn->scid.len; |
| 3080 | } |
| 3081 | |
| 3082 | return 1; |
| 3083 | } |
| 3084 | |
| 3085 | /* This function builds into <buf> buffer a QUIC long packet header whose size may be computed |
| 3086 | * in advance. This is the reponsability of the caller to check there is enough room in this |
| 3087 | * buffer to build a long header. |
| 3088 | * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded. |
| 3089 | */ |
| 3090 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 3091 | size_t pn_len, struct quic_conn *conn) |
| 3092 | { |
| 3093 | /* #0 byte flags */ |
| 3094 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1); |
| 3095 | /* Destination connection ID */ |
| 3096 | if (conn->dcid.len) { |
| 3097 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 3098 | *buf += conn->dcid.len; |
| 3099 | } |
| 3100 | |
| 3101 | return 1; |
| 3102 | } |
| 3103 | |
| 3104 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 3105 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 3106 | * with <aead> as AEAD cipher and <key> as secret key. |
| 3107 | * Returns 1 if succeeded or 0 if failed. |
| 3108 | */ |
| 3109 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 3110 | const EVP_CIPHER *aead, const unsigned char *key) |
| 3111 | { |
| 3112 | int i, ret, outlen; |
| 3113 | EVP_CIPHER_CTX *ctx; |
| 3114 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 3115 | * and at most 4 bytes for the packet number |
| 3116 | */ |
| 3117 | unsigned char mask[5] = {0}; |
| 3118 | |
| 3119 | ret = 0; |
| 3120 | ctx = EVP_CIPHER_CTX_new(); |
| 3121 | if (!ctx) |
| 3122 | return 0; |
| 3123 | |
| 3124 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 3125 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 3126 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 3127 | goto out; |
| 3128 | |
| 3129 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 3130 | for (i = 0; i < pnlen; i++) |
| 3131 | pn[i] ^= mask[i + 1]; |
| 3132 | |
| 3133 | ret = 1; |
| 3134 | |
| 3135 | out: |
| 3136 | EVP_CIPHER_CTX_free(ctx); |
| 3137 | |
| 3138 | return ret; |
| 3139 | } |
| 3140 | |
| 3141 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 3142 | * ACK ranges if needed to a value below <limit> in bytes. |
| 3143 | * Return 1 if succeeded, 0 if not. |
| 3144 | */ |
| 3145 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 3146 | { |
| 3147 | size_t room, ack_delay_sz; |
| 3148 | |
| 3149 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 3150 | /* A frame is made of 1 byte for the frame type. */ |
| 3151 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3152 | if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3153 | return 0; |
| 3154 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3155 | return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | /* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel> |
| 3159 | * encryption level to be encoded in a buffer with <room> as available room, |
| 3160 | * and <*len> as number of bytes already present in this buffer. |
| 3161 | * Update consequently <*len> to reflect the size of these CRYPTO frames built |
| 3162 | * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet. |
| 3163 | * Return 1 if succeeded, 0 if not. |
| 3164 | */ |
| 3165 | static inline int qc_build_cfrms(struct quic_tx_packet *pkt, |
| 3166 | size_t room, size_t *len, |
| 3167 | struct quic_enc_level *qel, |
| 3168 | struct quic_conn *conn) |
| 3169 | { |
| 3170 | struct quic_tx_frm *cf, *cfbak; |
| 3171 | size_t max_cdata_len; |
| 3172 | |
| 3173 | if (conn->tx.nb_pto_dgrams) |
| 3174 | max_cdata_len = room; |
| 3175 | else |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3176 | max_cdata_len = quic_path_prep_data(conn->path); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3177 | list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) { |
| 3178 | /* header length, data length, frame length. */ |
| 3179 | size_t hlen, dlen, cflen; |
| 3180 | |
| 3181 | if (!max_cdata_len) |
| 3182 | break; |
| 3183 | |
| 3184 | /* Compute the length of this CRYPTO frame header */ |
| 3185 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 3186 | /* Compute the data length of this CRyPTO frame. */ |
| 3187 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 3188 | if (!dlen) |
| 3189 | break; |
| 3190 | |
| 3191 | if (dlen > max_cdata_len) |
| 3192 | dlen = max_cdata_len; |
| 3193 | max_cdata_len -= dlen; |
| 3194 | pkt->cdata_len += dlen; |
| 3195 | /* CRYPTO frame length. */ |
| 3196 | cflen = hlen + quic_int_getsize(dlen) + dlen; |
| 3197 | /* Add the CRYPTO data length and its encoded length to the packet |
| 3198 | * length and the length of this length. |
| 3199 | */ |
| 3200 | *len += cflen; |
| 3201 | if (dlen == cf->crypto.len) { |
| 3202 | /* <cf> CRYPTO data have been consumed. */ |
| 3203 | LIST_DEL(&cf->list); |
| 3204 | LIST_ADDQ(&pkt->frms, &cf->list); |
| 3205 | } |
| 3206 | else { |
| 3207 | struct quic_tx_frm *new_cf; |
| 3208 | |
| 3209 | new_cf = pool_alloc(pool_head_quic_tx_frm); |
| 3210 | if (!new_cf) { |
| 3211 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_ECHPKT, conn->conn); |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | new_cf->type = QUIC_FT_CRYPTO; |
| 3216 | new_cf->crypto.len = dlen; |
| 3217 | new_cf->crypto.offset = cf->crypto.offset; |
| 3218 | LIST_ADDQ(&pkt->frms, &new_cf->list); |
| 3219 | /* Consume <dlen> bytes of the current frame. */ |
| 3220 | cf->crypto.len -= dlen; |
| 3221 | cf->crypto.offset += dlen; |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | return 1; |
| 3226 | } |
| 3227 | |
| 3228 | /* This function builds a clear handshake packet used during a QUIC TLS handshakes |
| 3229 | * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC |
| 3230 | * TLS encryption level for outgoing packets filling it with as much as CRYPTO |
| 3231 | * data as possible from <offset> offset in the CRYPTO data stream. Note that |
| 3232 | * this offset value is updated by the length of the CRYPTO frame used to embed |
| 3233 | * the CRYPTO data if this packet and only if the packet is successfully built. |
| 3234 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 3235 | * reserved so that to be sure there is enough room to build this AEAD TAG after |
| 3236 | * having successfully returned from this function and to be sure the position |
| 3237 | * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having |
| 3238 | * returned from this funciton, <wbuf> position will point one past the last |
| 3239 | * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes |
| 3240 | * available packet to encrypt this packet. |
| 3241 | * This function also update the value of <buf_pn> pointer to point to the packet |
| 3242 | * number field in this packet. <pn_len> will also have the packet number |
| 3243 | * length as value. |
| 3244 | * |
| 3245 | * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if |
| 3246 | * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN |
| 3247 | * bytes), -2 if there are too much CRYPTO data in flight to build a packet. |
| 3248 | */ |
| 3249 | static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf, |
| 3250 | struct quic_tx_packet *pkt, int pkt_type, |
| 3251 | int64_t pn, size_t *pn_len, |
| 3252 | unsigned char **buf_pn, |
| 3253 | struct quic_enc_level *qel, |
| 3254 | struct quic_conn *conn) |
| 3255 | { |
| 3256 | unsigned char *beg, *pos; |
| 3257 | const unsigned char *end; |
| 3258 | /* This packet type. */ |
| 3259 | /* Packet number. */ |
| 3260 | /* The Length QUIC packet field value which is the length |
| 3261 | * of the remaining data after this field after encryption. |
| 3262 | */ |
| 3263 | size_t len, token_fields_len, padding_len; |
| 3264 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 3265 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 3266 | struct quic_crypto *crypto = &frm.crypto; |
| 3267 | size_t ack_frm_len; |
| 3268 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3269 | int add_ping_frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3270 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3271 | beg = pos = q_buf_getpos(wbuf); |
| 3272 | end = q_buf_end(wbuf); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3273 | /* When not probing and not acking, reduce the size of this buffer to respect |
| 3274 | * the congestion controller window. |
| 3275 | */ |
| 3276 | if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) { |
| 3277 | size_t path_room; |
| 3278 | |
| 3279 | path_room = quic_path_prep_data(conn->path); |
| 3280 | if (end - beg > path_room) |
| 3281 | end = beg + path_room; |
| 3282 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3283 | |
| 3284 | /* For a server, the token field of an Initial packet is empty. */ |
| 3285 | token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0; |
| 3286 | |
| 3287 | /* Check there is enough room to build the header followed by a token. */ |
| 3288 | if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len + |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3289 | conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) { |
| 3290 | ssize_t room = end - pos; |
| 3291 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 3292 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3293 | goto err; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3294 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* Reserve enough room at the end of the packet for the AEAD TAG. */ |
| 3297 | end -= QUIC_TLS_TAG_LEN; |
| 3298 | largest_acked_pn = qel->pktns->tx.largest_acked_pn; |
| 3299 | /* packet number length */ |
| 3300 | *pn_len = quic_packet_number_length(pn, largest_acked_pn); |
| 3301 | |
| 3302 | quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn); |
| 3303 | |
| 3304 | /* Encode the token length (0) for an Initial packet. */ |
| 3305 | if (pkt_type == QUIC_PACKET_TYPE_INITIAL) |
| 3306 | *pos++ = 0; |
| 3307 | |
| 3308 | /* Build an ACK frame if required. */ |
| 3309 | ack_frm_len = 0; |
| 3310 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3311 | !eb_is_empty(&qel->pktns->rx.arngs.root)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3312 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3313 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3314 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3315 | if (!ack_frm_len) { |
| 3316 | ssize_t room = end - pos; |
| 3317 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 3318 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3319 | goto err; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3320 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3321 | |
| 3322 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3323 | } |
| 3324 | |
| 3325 | /* Length field value without the CRYPTO frames data length. */ |
| 3326 | len = ack_frm_len + *pn_len; |
| 3327 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms) && |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3328 | !qc_build_cfrms(pkt, end - pos, &len, qel, conn)) { |
| 3329 | ssize_t room = end - pos; |
| 3330 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 3331 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3332 | goto err; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3333 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3334 | |
| 3335 | add_ping_frm = 0; |
| 3336 | padding_len = 0; |
| 3337 | if (objt_server(conn->conn->target) && |
| 3338 | pkt_type == QUIC_PACKET_TYPE_INITIAL && |
| 3339 | len < QUIC_INITIAL_PACKET_MINLEN) { |
| 3340 | len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len; |
| 3341 | } |
| 3342 | else if (LIST_ISEMPTY(&pkt->frms)) { |
| 3343 | if (qel->pktns->tx.pto_probe) { |
| 3344 | /* If we cannot send a CRYPTO frame, we send a PING frame. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3345 | add_ping_frm = 1; |
| 3346 | len += 1; |
| 3347 | } |
| 3348 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 3349 | if (!ack_frm_len) |
| 3350 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 3351 | } |
| 3352 | |
| 3353 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 3354 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 3355 | * for the encryption TAG. It must be taken into an account for the length |
| 3356 | * of this packet. |
| 3357 | */ |
| 3358 | quic_enc_int(&pos, end, len + QUIC_TLS_TAG_LEN); |
| 3359 | |
| 3360 | /* Packet number field address. */ |
| 3361 | *buf_pn = pos; |
| 3362 | |
| 3363 | /* Packet number encoding. */ |
| 3364 | quic_packet_number_encode(&pos, end, pn, *pn_len); |
| 3365 | |
| 3366 | if (ack_frm_len) |
| 3367 | qc_build_frm(&pos, end, &ack_frm, pkt, conn); |
| 3368 | |
| 3369 | /* Crypto frame */ |
| 3370 | if (!LIST_ISEMPTY(&pkt->frms)) { |
| 3371 | struct quic_tx_frm *cf; |
| 3372 | |
| 3373 | list_for_each_entry(cf, &pkt->frms, list) { |
| 3374 | crypto->offset = cf->crypto.offset; |
| 3375 | crypto->len = cf->crypto.len; |
| 3376 | crypto->qel = qel; |
| 3377 | qc_build_frm(&pos, end, &frm, pkt, conn); |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /* Build a PING frame if needed. */ |
| 3382 | if (add_ping_frm) { |
| 3383 | frm.type = QUIC_FT_PING; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3384 | if (!qc_build_frm(&pos, end, &frm, pkt, conn)) { |
| 3385 | ssize_t room = end - pos; |
| 3386 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 3387 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3388 | goto err; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3389 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | /* Build a PADDING frame if needed. */ |
| 3393 | if (padding_len) { |
| 3394 | frm.type = QUIC_FT_PADDING; |
| 3395 | frm.padding.len = padding_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3396 | if (!qc_build_frm(&pos, end, &frm, pkt, conn)) { |
| 3397 | ssize_t room = end - pos; |
| 3398 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 3399 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3400 | goto err; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3401 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3404 | /* Always reset this variable as this function has no idea |
| 3405 | * if it was set. It is handle by the loss detection timer. |
| 3406 | */ |
| 3407 | qel->pktns->tx.pto_probe = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3408 | |
| 3409 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3410 | return pos - beg; |
| 3411 | |
| 3412 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3413 | return -1; |
| 3414 | } |
| 3415 | |
| 3416 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt) |
| 3417 | { |
| 3418 | pkt->cdata_len = 0; |
| 3419 | pkt->in_flight_len = 0; |
| 3420 | LIST_INIT(&pkt->frms); |
| 3421 | } |
| 3422 | |
| 3423 | /* Free <pkt> TX packet which has not already attache to any tree. */ |
| 3424 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 3425 | { |
| 3426 | struct quic_tx_frm *frm, *frmbak; |
| 3427 | |
| 3428 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 3429 | LIST_DEL(&frm->list); |
| 3430 | pool_free(pool_head_quic_tx_frm, frm); |
| 3431 | } |
| 3432 | pool_free(pool_head_quic_tx_packet, pkt); |
| 3433 | } |
| 3434 | |
| 3435 | /* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet |
| 3436 | * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to |
| 3437 | * be encrypted at <qel> encryption level. |
| 3438 | * Return -2 if the packet could not be encrypted for any reason, -1 if there was |
| 3439 | * not enough room in <buf> to build the packet, or the size of the built packet |
| 3440 | * if succeeded (may be zero if there is too much crypto data in flight to build the packet). |
| 3441 | */ |
| 3442 | static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type, |
| 3443 | struct quic_enc_level *qel) |
| 3444 | { |
| 3445 | /* The pointer to the packet number field. */ |
| 3446 | unsigned char *buf_pn; |
| 3447 | unsigned char *beg, *end, *payload; |
| 3448 | int64_t pn; |
| 3449 | size_t pn_len, payload_len, aad_len; |
| 3450 | ssize_t pkt_len; |
| 3451 | struct quic_tls_ctx *tls_ctx; |
| 3452 | struct quic_tx_packet *pkt; |
| 3453 | |
| 3454 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn,, qel); |
| 3455 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 3456 | if (!pkt) { |
| 3457 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn); |
| 3458 | return -2; |
| 3459 | } |
| 3460 | |
| 3461 | quic_tx_packet_init(pkt); |
| 3462 | beg = q_buf_getpos(buf); |
| 3463 | pn_len = 0; |
| 3464 | buf_pn = NULL; |
| 3465 | pn = qel->pktns->tx.next_pn + 1; |
| 3466 | pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc); |
| 3467 | if (pkt_len <= 0) { |
| 3468 | free_quic_tx_packet(pkt); |
| 3469 | return pkt_len; |
| 3470 | } |
| 3471 | |
| 3472 | end = beg + pkt_len; |
| 3473 | payload = buf_pn + pn_len; |
| 3474 | payload_len = end - payload; |
| 3475 | aad_len = payload - beg; |
| 3476 | |
| 3477 | tls_ctx = &qel->tls_ctx; |
| 3478 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) |
| 3479 | goto err; |
| 3480 | |
| 3481 | end += QUIC_TLS_TAG_LEN; |
| 3482 | pkt_len += QUIC_TLS_TAG_LEN; |
| 3483 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 3484 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
| 3485 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn); |
| 3486 | goto err; |
| 3487 | } |
| 3488 | |
| 3489 | /* Now that a correct packet is built, let us set the position pointer of |
| 3490 | * <buf> buffer for the next packet. |
| 3491 | */ |
| 3492 | q_buf_setpos(buf, end); |
| 3493 | /* Consume a packet number. */ |
| 3494 | ++qel->pktns->tx.next_pn; |
| 3495 | /* Attach the built packet to its tree. */ |
| 3496 | pkt->pn_node.key = qel->pktns->tx.next_pn; |
| 3497 | /* 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] | 3498 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3499 | pkt->in_flight_len = pkt_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3500 | qc->path->prep_in_flight += pkt_len; |
| 3501 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3502 | pkt->pktns = qel->pktns; |
| 3503 | eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node); |
| 3504 | /* Increment the number of bytes in <buf> buffer by the length of this packet. */ |
| 3505 | buf->data += pkt_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3506 | /* Attach this packet to <buf>. */ |
| 3507 | LIST_ADDQ(&buf->pkts, &pkt->list); |
| 3508 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt); |
| 3509 | |
| 3510 | return pkt_len; |
| 3511 | |
| 3512 | err: |
| 3513 | free_quic_tx_packet(pkt); |
| 3514 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn); |
| 3515 | return -2; |
| 3516 | } |
| 3517 | |
| 3518 | /* Prepare a clear post handhskake packet for <conn> QUIC connnection. |
| 3519 | * Return the length of this packet if succeeded, -1 <wbuf> was full. |
| 3520 | */ |
| 3521 | static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf, |
| 3522 | struct quic_tx_packet *pkt, |
| 3523 | int64_t pn, size_t *pn_len, |
| 3524 | unsigned char **buf_pn, struct quic_enc_level *qel, |
| 3525 | struct quic_conn *conn) |
| 3526 | { |
| 3527 | const unsigned char *beg, *end; |
| 3528 | unsigned char *pos; |
| 3529 | struct quic_frame *frm, *sfrm; |
| 3530 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 3531 | size_t fake_len, ack_frm_len; |
| 3532 | int64_t largest_acked_pn; |
| 3533 | |
| 3534 | TRACE_ENTER(QUIC_EV_CONN_CPAPKT, conn->conn); |
| 3535 | beg = pos = q_buf_getpos(wbuf); |
| 3536 | end = q_buf_end(wbuf); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3537 | /* When not probing and not acking, reduce the size of this buffer to respect |
| 3538 | * the congestion controller window. |
| 3539 | */ |
| 3540 | if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) { |
| 3541 | size_t path_room; |
| 3542 | |
| 3543 | path_room = quic_path_prep_data(conn->path); |
| 3544 | if (end - beg > path_room) |
| 3545 | end = beg + path_room; |
| 3546 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3547 | largest_acked_pn = qel->pktns->tx.largest_acked_pn; |
| 3548 | /* Packet number length */ |
| 3549 | *pn_len = quic_packet_number_length(pn, largest_acked_pn); |
| 3550 | /* Check there is enough room to build this packet (without payload). */ |
| 3551 | if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) + |
| 3552 | *pn_len + QUIC_TLS_TAG_LEN) |
| 3553 | goto err; |
| 3554 | |
| 3555 | /* Reserve enough room at the end of the packet for the AEAD TAG. */ |
| 3556 | end -= QUIC_TLS_TAG_LEN; |
| 3557 | quic_build_packet_short_header(&pos, end, *pn_len, conn); |
| 3558 | /* Packet number field. */ |
| 3559 | *buf_pn = pos; |
| 3560 | /* Packet number encoding. */ |
| 3561 | quic_packet_number_encode(&pos, end, pn, *pn_len); |
| 3562 | |
| 3563 | /* Build an ACK frame if required. */ |
| 3564 | ack_frm_len = 0; |
| 3565 | if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3566 | !eb_is_empty(&qel->pktns->rx.arngs.root)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3567 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3568 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3569 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos); |
| 3570 | if (!ack_frm_len) |
| 3571 | goto err; |
| 3572 | |
| 3573 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3574 | } |
| 3575 | |
| 3576 | if (ack_frm_len) |
| 3577 | qc_build_frm(&pos, end, &ack_frm, pkt, conn); |
| 3578 | |
| 3579 | fake_len = ack_frm_len; |
| 3580 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms) && |
| 3581 | !qc_build_cfrms(pkt, end - pos, &fake_len, qel, conn)) { |
| 3582 | TRACE_DEVEL("some CRYPTO frames could not be built", |
| 3583 | QUIC_EV_CONN_CPAPKT, conn->conn); |
| 3584 | goto err; |
| 3585 | } |
| 3586 | |
| 3587 | /* Crypto frame */ |
| 3588 | if (!LIST_ISEMPTY(&pkt->frms)) { |
| 3589 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 3590 | struct quic_crypto *crypto = &frm.crypto; |
| 3591 | struct quic_tx_frm *cf; |
| 3592 | |
| 3593 | list_for_each_entry(cf, &pkt->frms, list) { |
| 3594 | crypto->offset = cf->crypto.offset; |
| 3595 | crypto->len = cf->crypto.len; |
| 3596 | crypto->qel = qel; |
| 3597 | qc_build_frm(&pos, end, &frm, pkt, conn); |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | /* Encode a maximum of frames. */ |
| 3602 | list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) { |
| 3603 | unsigned char *ppos; |
| 3604 | |
| 3605 | ppos = pos; |
| 3606 | if (!qc_build_frm(&ppos, end, frm, pkt, conn)) { |
| 3607 | TRACE_DEVEL("Frames not built", QUIC_EV_CONN_CPAPKT, conn->conn); |
| 3608 | break; |
| 3609 | } |
| 3610 | |
| 3611 | LIST_DEL(&frm->list); |
| 3612 | LIST_ADDQ(&pkt->frms, &frm->list); |
| 3613 | pos = ppos; |
| 3614 | } |
| 3615 | |
| 3616 | out: |
| 3617 | TRACE_LEAVE(QUIC_EV_CONN_CPAPKT, conn->conn, (int *)(pos - beg)); |
| 3618 | return pos - beg; |
| 3619 | |
| 3620 | err: |
| 3621 | TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_CPAPKT, conn->conn); |
| 3622 | return -1; |
| 3623 | } |
| 3624 | |
| 3625 | /* Prepare a post handhskake packet at Application encryption level for <conn> |
| 3626 | * QUIC connnection. |
| 3627 | * Return the length of this packet if succeeded, -1 if <wbuf> was full, |
| 3628 | * -2 in case of major error (encryption failure). |
| 3629 | */ |
| 3630 | static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc) |
| 3631 | { |
| 3632 | /* A pointer to the packet number fiel in <buf> */ |
| 3633 | unsigned char *buf_pn; |
| 3634 | unsigned char *beg, *end, *payload; |
| 3635 | int64_t pn; |
| 3636 | size_t pn_len, aad_len, payload_len; |
| 3637 | ssize_t pkt_len; |
| 3638 | struct quic_tls_ctx *tls_ctx; |
| 3639 | struct quic_enc_level *qel; |
| 3640 | struct quic_tx_packet *pkt; |
| 3641 | |
| 3642 | TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn); |
| 3643 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 3644 | if (!pkt) { |
| 3645 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn); |
| 3646 | return -2; |
| 3647 | } |
| 3648 | |
| 3649 | quic_tx_packet_init(pkt); |
| 3650 | beg = q_buf_getpos(wbuf); |
| 3651 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3652 | pn_len = 0; |
| 3653 | buf_pn = NULL; |
| 3654 | pn = qel->pktns->tx.next_pn + 1; |
| 3655 | pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc); |
| 3656 | if (pkt_len <= 0) { |
| 3657 | free_quic_tx_packet(pkt); |
| 3658 | return pkt_len; |
| 3659 | } |
| 3660 | |
| 3661 | end = beg + pkt_len; |
| 3662 | payload = buf_pn + pn_len; |
| 3663 | payload_len = end - payload; |
| 3664 | aad_len = payload - beg; |
| 3665 | |
| 3666 | tls_ctx = &qel->tls_ctx; |
| 3667 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) |
| 3668 | return -2; |
| 3669 | |
| 3670 | end += QUIC_TLS_TAG_LEN; |
| 3671 | pkt_len += QUIC_TLS_TAG_LEN; |
| 3672 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 3673 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) |
| 3674 | return -2; |
| 3675 | |
| 3676 | q_buf_setpos(wbuf, end); |
| 3677 | /* Consume a packet number. */ |
| 3678 | ++qel->pktns->tx.next_pn; |
| 3679 | /* Attach the built packet to its tree. */ |
| 3680 | pkt->pn_node.key = qel->pktns->tx.next_pn; |
| 3681 | eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node); |
| 3682 | /* 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] | 3683 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3684 | pkt->in_flight_len = pkt_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3685 | qc->path->prep_in_flight += pkt_len; |
| 3686 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3687 | pkt->pktns = qel->pktns; |
| 3688 | /* Increment the number of bytes in <buf> buffer by the length of this packet. */ |
| 3689 | wbuf->data += pkt_len; |
| 3690 | /* Attach this packet to <buf>. */ |
| 3691 | LIST_ADDQ(&wbuf->pkts, &pkt->list); |
| 3692 | |
| 3693 | TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn); |
| 3694 | |
| 3695 | return pkt_len; |
| 3696 | } |
| 3697 | |
| 3698 | /* Prepare a maximum of QUIC Application level packets from <ctx> QUIC |
| 3699 | * connection I/O handler context. |
| 3700 | * Returns 1 if succeeded, 0 if not. |
| 3701 | */ |
| 3702 | int qc_prep_phdshk_pkts(struct quic_conn *qc) |
| 3703 | { |
| 3704 | struct q_buf *wbuf; |
| 3705 | struct quic_enc_level *qel; |
| 3706 | |
| 3707 | TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn); |
| 3708 | wbuf = q_wbuf(qc); |
| 3709 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3710 | while (q_buf_empty(wbuf)) { |
| 3711 | ssize_t ret; |
| 3712 | |
| 3713 | if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3714 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3715 | qc->path->prep_in_flight >= qc->path->cwnd)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3716 | TRACE_DEVEL("nothing more to do", |
| 3717 | QUIC_EV_CONN_PAPKTS, qc->conn); |
| 3718 | break; |
| 3719 | } |
| 3720 | |
| 3721 | ret = qc_build_phdshk_apkt(wbuf, qc); |
| 3722 | switch (ret) { |
| 3723 | case -1: |
| 3724 | /* Not enough room left in <wbuf>. */ |
| 3725 | wbuf = q_next_wbuf(qc); |
| 3726 | continue; |
| 3727 | case -2: |
| 3728 | return 0; |
| 3729 | default: |
| 3730 | /* XXX TO CHECK: consume a buffer. */ |
| 3731 | wbuf = q_next_wbuf(qc); |
| 3732 | continue; |
| 3733 | } |
| 3734 | } |
| 3735 | TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn); |
| 3736 | |
| 3737 | return 1; |
| 3738 | } |
| 3739 | |
| 3740 | /* QUIC connection packet handler task. */ |
| 3741 | static struct task *quic_conn_io_cb(struct task *t, void *context, unsigned short state) |
| 3742 | { |
| 3743 | struct quic_conn_ctx *ctx = context; |
| 3744 | |
| 3745 | if (ctx->state < QUIC_HS_ST_COMPLETE) { |
| 3746 | qc_do_hdshk(ctx); |
| 3747 | } |
| 3748 | else { |
| 3749 | struct quic_conn *qc = ctx->conn->qc; |
| 3750 | |
| 3751 | /* XXX TO DO: may fail!!! XXX */ |
| 3752 | qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx); |
| 3753 | qc_prep_phdshk_pkts(qc); |
| 3754 | qc_send_ppkts(ctx); |
| 3755 | } |
| 3756 | |
| 3757 | return NULL; |
| 3758 | } |
| 3759 | |
| 3760 | /* Receive up to <count> bytes from connection <conn>'s socket and store them |
| 3761 | * into buffer <buf>. Only one call to recv() is performed, unless the |
| 3762 | * buffer wraps, in which case a second call may be performed. The connection's |
| 3763 | * flags are updated with whatever special event is detected (error, read0, |
| 3764 | * empty). The caller is responsible for taking care of those events and |
| 3765 | * avoiding the call if inappropriate. The function does not call the |
| 3766 | * connection's polling update function, so the caller is responsible for this. |
| 3767 | * errno is cleared before starting so that the caller knows that if it spots an |
| 3768 | * error without errno, it's pending and can be retrieved via getsockopt(SO_ERROR). |
| 3769 | */ |
| 3770 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags) |
| 3771 | { |
| 3772 | ssize_t ret; |
| 3773 | size_t try, done = 0; |
| 3774 | |
| 3775 | if (!conn_ctrl_ready(conn)) |
| 3776 | return 0; |
| 3777 | |
| 3778 | if (!fd_recv_ready(conn->handle.fd)) |
| 3779 | return 0; |
| 3780 | |
| 3781 | conn->flags &= ~CO_FL_WAIT_ROOM; |
| 3782 | errno = 0; |
| 3783 | |
| 3784 | if (unlikely(!(fdtab[conn->handle.fd].ev & FD_POLL_IN))) { |
| 3785 | /* stop here if we reached the end of data */ |
| 3786 | if ((fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_HUP) |
| 3787 | goto read0; |
| 3788 | |
| 3789 | /* report error on POLL_ERR before connection establishment */ |
| 3790 | if ((fdtab[conn->handle.fd].ev & FD_POLL_ERR) && (conn->flags & CO_FL_WAIT_L4_CONN)) { |
| 3791 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 3792 | goto leave; |
| 3793 | } |
| 3794 | } |
| 3795 | |
| 3796 | /* read the largest possible block. For this, we perform only one call |
| 3797 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 3798 | * in which case we accept to do it once again. A new attempt is made on |
| 3799 | * EINTR too. |
| 3800 | */ |
| 3801 | while (count > 0) { |
| 3802 | try = b_contig_space(buf); |
| 3803 | if (!try) |
| 3804 | break; |
| 3805 | |
| 3806 | if (try > count) |
| 3807 | try = count; |
| 3808 | |
| 3809 | ret = recvfrom(conn->handle.fd, b_tail(buf), try, 0, NULL, 0); |
| 3810 | |
| 3811 | if (ret > 0) { |
| 3812 | b_add(buf, ret); |
| 3813 | done += ret; |
| 3814 | if (ret < try) { |
| 3815 | /* unfortunately, on level-triggered events, POLL_HUP |
| 3816 | * is generally delivered AFTER the system buffer is |
| 3817 | * empty, unless the poller supports POLL_RDHUP. If |
| 3818 | * we know this is the case, we don't try to read more |
| 3819 | * as we know there's no more available. Similarly, if |
| 3820 | * there's no problem with lingering we don't even try |
| 3821 | * to read an unlikely close from the client since we'll |
| 3822 | * close first anyway. |
| 3823 | */ |
| 3824 | if (fdtab[conn->handle.fd].ev & FD_POLL_HUP) |
| 3825 | goto read0; |
| 3826 | |
| 3827 | if ((!fdtab[conn->handle.fd].linger_risk) || |
| 3828 | (cur_poller.flags & HAP_POLL_F_RDHUP)) { |
| 3829 | break; |
| 3830 | } |
| 3831 | } |
| 3832 | count -= ret; |
| 3833 | } |
| 3834 | else if (ret == 0) { |
| 3835 | goto read0; |
| 3836 | } |
| 3837 | else if (errno == EAGAIN || errno == ENOTCONN) { |
| 3838 | fd_cant_recv(conn->handle.fd); |
| 3839 | break; |
| 3840 | } |
| 3841 | else if (errno != EINTR) { |
| 3842 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 3843 | break; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 3848 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3849 | |
| 3850 | leave: |
| 3851 | return done; |
| 3852 | |
| 3853 | read0: |
| 3854 | conn_sock_read0(conn); |
| 3855 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3856 | |
| 3857 | /* Now a final check for a possible asynchronous low-level error |
| 3858 | * report. This can happen when a connection receives a reset |
| 3859 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 3860 | * we might have come from there by just checking POLL_HUP instead |
| 3861 | * of recv()'s return value 0, so we have no way to tell there was |
| 3862 | * an error without checking. |
| 3863 | */ |
| 3864 | if (unlikely(fdtab[conn->handle.fd].ev & FD_POLL_ERR)) |
| 3865 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 3866 | goto leave; |
| 3867 | } |
| 3868 | |
| 3869 | |
| 3870 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 3871 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 3872 | * other pending data for example, but this flag is ignored at the moment. |
| 3873 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 3874 | * a second call may be performed. The connection's flags are updated with |
| 3875 | * whatever special event is detected (error, empty). The caller is responsible |
| 3876 | * for taking care of those events and avoiding the call if inappropriate. The |
| 3877 | * function does not call the connection's polling update function, so the caller |
| 3878 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 3879 | * based on the return value. |
| 3880 | */ |
| 3881 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 3882 | { |
| 3883 | ssize_t ret; |
| 3884 | size_t try, done; |
| 3885 | int send_flag; |
| 3886 | |
| 3887 | if (!conn_ctrl_ready(conn)) |
| 3888 | return 0; |
| 3889 | |
| 3890 | if (!fd_send_ready(conn->handle.fd)) |
| 3891 | return 0; |
| 3892 | |
| 3893 | done = 0; |
| 3894 | /* send the largest possible block. For this we perform only one call |
| 3895 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 3896 | * in which case we accept to do it once again. |
| 3897 | */ |
| 3898 | while (count) { |
| 3899 | try = b_contig_data(buf, done); |
| 3900 | if (try > count) |
| 3901 | try = count; |
| 3902 | |
| 3903 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 3904 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 3905 | send_flag |= MSG_MORE; |
| 3906 | |
| 3907 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 3908 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 3909 | if (ret > 0) { |
| 3910 | count -= ret; |
| 3911 | done += ret; |
| 3912 | |
| 3913 | /* A send succeeded, so we can consier ourself connected */ |
| 3914 | conn->flags |= CO_FL_WAIT_L4L6; |
| 3915 | /* if the system buffer is full, don't insist */ |
| 3916 | if (ret < try) |
| 3917 | break; |
| 3918 | } |
| 3919 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 3920 | /* nothing written, we need to poll for write first */ |
| 3921 | fd_cant_send(conn->handle.fd); |
| 3922 | break; |
| 3923 | } |
| 3924 | else if (errno != EINTR) { |
| 3925 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 3926 | break; |
| 3927 | } |
| 3928 | } |
| 3929 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 3930 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3931 | |
| 3932 | if (done > 0) { |
| 3933 | /* we count the total bytes sent, and the send rate for 32-byte |
| 3934 | * blocks. The reason for the latter is that freq_ctr are |
| 3935 | * limited to 4GB and that it's not enough per second. |
| 3936 | */ |
| 3937 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 3938 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 3939 | } |
| 3940 | return done; |
| 3941 | } |
| 3942 | |
| 3943 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 3944 | * connection with <xprt_ctx> as address of the xprt context. |
| 3945 | * Returns 1 if succeeded, 0 if not. |
| 3946 | */ |
| 3947 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 3948 | { |
| 3949 | struct quic_conn_ctx *ctx; |
| 3950 | |
| 3951 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 3952 | |
| 3953 | if (*xprt_ctx) |
| 3954 | goto out; |
| 3955 | |
| 3956 | if (!conn_ctrl_ready(conn)) |
| 3957 | goto out; |
| 3958 | |
| 3959 | ctx = pool_alloc(pool_head_quic_conn_ctx); |
| 3960 | if (!ctx) { |
| 3961 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 3962 | goto err; |
| 3963 | } |
| 3964 | |
| 3965 | ctx->wait_event.tasklet = tasklet_new(); |
| 3966 | if (!ctx->wait_event.tasklet) { |
| 3967 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 3968 | goto err; |
| 3969 | } |
| 3970 | |
| 3971 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 3972 | ctx->wait_event.tasklet->context = ctx; |
| 3973 | ctx->wait_event.events = 0; |
| 3974 | ctx->conn = conn; |
| 3975 | ctx->subs = NULL; |
| 3976 | ctx->xprt_ctx = NULL; |
| 3977 | |
| 3978 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 3979 | if (objt_server(conn->target)) { |
| 3980 | /* Server */ |
| 3981 | struct server *srv = __objt_server(conn->target); |
| 3982 | unsigned char dcid[QUIC_CID_LEN]; |
| 3983 | struct quic_conn *quic_conn; |
| 3984 | int ssl_err, ipv4; |
| 3985 | |
| 3986 | ssl_err = SSL_ERROR_NONE; |
| 3987 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 3988 | goto err; |
| 3989 | |
| 3990 | conn->qc = new_quic_conn(QUIC_PROTOCOL_VERSION_DRAFT_28); |
| 3991 | if (!conn->qc) |
| 3992 | goto err; |
| 3993 | |
| 3994 | quic_conn = conn->qc; |
| 3995 | quic_conn->conn = conn; |
| 3996 | ipv4 = conn->dst->ss_family == AF_INET; |
| 3997 | if (!qc_new_conn_init(quic_conn, ipv4, NULL, &srv->cids, |
| 3998 | dcid, sizeof dcid, NULL, 0)) |
| 3999 | goto err; |
| 4000 | |
| 4001 | if (!qc_new_isecs(conn, dcid, sizeof dcid, 0)) |
| 4002 | goto err; |
| 4003 | |
| 4004 | ctx->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 4005 | if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx, |
| 4006 | &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1) |
| 4007 | goto err; |
| 4008 | |
| 4009 | quic_conn->params = srv->quic_params; |
| 4010 | /* Copy the initial source connection ID. */ |
| 4011 | quic_cid_cpy(&quic_conn->params.initial_source_connection_id, &quic_conn->scid); |
| 4012 | quic_conn->enc_params_len = |
| 4013 | quic_transport_params_encode(quic_conn->enc_params, |
| 4014 | quic_conn->enc_params + sizeof quic_conn->enc_params, |
| 4015 | &quic_conn->params, 0); |
| 4016 | if (!quic_conn->enc_params_len) |
| 4017 | goto err; |
| 4018 | |
| 4019 | SSL_set_quic_transport_params(ctx->ssl, quic_conn->enc_params, quic_conn->enc_params_len); |
| 4020 | SSL_set_connect_state(ctx->ssl); |
| 4021 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 4022 | if (ssl_err != 1) { |
| 4023 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 4024 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 4025 | TRACE_PROTO("SSL handshake", |
| 4026 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 4027 | } |
| 4028 | else { |
| 4029 | TRACE_DEVEL("SSL handshake error", |
| 4030 | QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err); |
| 4031 | goto err; |
| 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | else if (objt_listener(conn->target)) { |
| 4036 | /* Listener */ |
| 4037 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
| 4038 | |
| 4039 | ctx->state = QUIC_HS_ST_SERVER_INITIAL; |
| 4040 | |
| 4041 | if (ssl_bio_and_sess_init(conn, bc->initial_ctx, |
| 4042 | &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1) |
| 4043 | goto err; |
| 4044 | |
| 4045 | SSL_set_accept_state(ctx->ssl); |
| 4046 | } |
| 4047 | |
| 4048 | *xprt_ctx = ctx; |
| 4049 | |
| 4050 | /* Leave init state and start handshake */ |
| 4051 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
| 4052 | /* Start the handshake */ |
| 4053 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 4054 | |
| 4055 | out: |
| 4056 | TRACE_LEAVE(QUIC_EV_CONN_NEW, conn); |
| 4057 | |
| 4058 | return 0; |
| 4059 | |
| 4060 | err: |
| 4061 | if (ctx->wait_event.tasklet) |
| 4062 | tasklet_free(ctx->wait_event.tasklet); |
| 4063 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4064 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW|QUIC_EV_CONN_ENEW, conn); |
| 4065 | return -1; |
| 4066 | } |
| 4067 | |
| 4068 | /* transport-layer operations for QUIC connections. */ |
| 4069 | static struct xprt_ops ssl_quic = { |
| 4070 | .snd_buf = quic_conn_from_buf, |
| 4071 | .rcv_buf = quic_conn_to_buf, |
| 4072 | .init = qc_conn_init, |
| 4073 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 4074 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
| 4075 | .name = "QUIC", |
| 4076 | }; |
| 4077 | |
| 4078 | __attribute__((constructor)) |
| 4079 | static void __quic_conn_init(void) |
| 4080 | { |
| 4081 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 4082 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 4083 | } |
| 4084 | |
| 4085 | __attribute__((destructor)) |
| 4086 | static void __quic_conn_deinit(void) |
| 4087 | { |
| 4088 | BIO_meth_free(ha_quic_meth); |
| 4089 | } |
| 4090 | |
| 4091 | /* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP |
| 4092 | * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections, |
| 4093 | * calling <func> function; |
| 4094 | * Return the number of bytes read if succeded, -1 if not. |
| 4095 | */ |
| 4096 | static ssize_t quic_dgram_read(char *buf, size_t len, void *owner, |
| 4097 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 4098 | { |
| 4099 | unsigned char *pos; |
| 4100 | const unsigned char *end; |
| 4101 | struct quic_dgram_ctx dgram_ctx = { |
| 4102 | .dcid_node = NULL, |
| 4103 | .owner = owner, |
| 4104 | }; |
| 4105 | |
| 4106 | pos = (unsigned char *)buf; |
| 4107 | end = pos + len; |
| 4108 | |
| 4109 | do { |
| 4110 | int ret; |
| 4111 | struct quic_rx_packet *pkt; |
| 4112 | |
| 4113 | pkt = pool_alloc(pool_head_quic_rx_packet); |
| 4114 | if (!pkt) |
| 4115 | goto err; |
| 4116 | |
| 4117 | memset(pkt, 0, sizeof(*pkt)); |
| 4118 | quic_rx_packet_refinc(pkt); |
| 4119 | ret = func(&pos, end, pkt, &dgram_ctx, saddr); |
| 4120 | if (ret == -1) { |
| 4121 | size_t pkt_len; |
| 4122 | |
| 4123 | pkt_len = pkt->len; |
| 4124 | free_quic_rx_packet(pkt); |
| 4125 | /* If the packet length could not be found, we cannot continue. */ |
| 4126 | if (!pkt_len) |
| 4127 | break; |
| 4128 | } |
| 4129 | } while (pos < end); |
| 4130 | |
| 4131 | /* Increasing the received bytes counter by the UDP datagram length |
| 4132 | * if this datagram could be associated to a connection. |
| 4133 | */ |
| 4134 | if (dgram_ctx.qc) |
| 4135 | dgram_ctx.qc->rx.bytes += len; |
| 4136 | |
| 4137 | return pos - (unsigned char *)buf; |
| 4138 | |
| 4139 | err: |
| 4140 | return -1; |
| 4141 | } |
| 4142 | |
| 4143 | ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner, |
| 4144 | struct sockaddr_storage *saddr) |
| 4145 | { |
| 4146 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 4147 | } |
| 4148 | |
| 4149 | ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner, |
| 4150 | struct sockaddr_storage *saddr) |
| 4151 | { |
| 4152 | return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv); |
| 4153 | } |
| 4154 | |
| 4155 | /* QUIC I/O handler for connection to local listeners or remove servers |
| 4156 | * depending on <listener> boolean value, with <fd> as socket file |
| 4157 | * descriptor and <ctx> as context. |
| 4158 | */ |
| 4159 | static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func) |
| 4160 | { |
| 4161 | ssize_t ret; |
| 4162 | size_t done = 0; |
| 4163 | struct buffer *buf = get_trash_chunk(); |
| 4164 | /* Source address */ |
| 4165 | struct sockaddr_storage saddr = {0}; |
| 4166 | socklen_t saddrlen = sizeof saddr; |
| 4167 | |
| 4168 | if (!fd_recv_ready(fd)) |
| 4169 | return 0; |
| 4170 | |
| 4171 | do { |
| 4172 | ret = recvfrom(fd, buf->area, buf->size, 0, |
| 4173 | (struct sockaddr *)&saddr, &saddrlen); |
| 4174 | if (ret < 0) { |
| 4175 | if (errno == EINTR) |
| 4176 | continue; |
| 4177 | if (errno == EAGAIN) |
| 4178 | fd_cant_recv(fd); |
| 4179 | goto out; |
| 4180 | } |
| 4181 | } while (0); |
| 4182 | |
| 4183 | done = buf->data = ret; |
| 4184 | quic_dgram_read(buf->area, buf->data, ctx, &saddr, func); |
| 4185 | |
| 4186 | out: |
| 4187 | return done; |
| 4188 | } |
| 4189 | |
| 4190 | /* QUIC I/O handler for connections to local listeners with <fd> as socket |
| 4191 | * file descriptor. |
| 4192 | */ |
| 4193 | void quic_fd_handler(int fd) |
| 4194 | { |
| 4195 | if (fdtab[fd].ev & FD_POLL_IN) |
| 4196 | quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv); |
| 4197 | } |
| 4198 | |
| 4199 | /* QUIC I/O handler for connections to remote servers with <fd> as socket |
| 4200 | * file descriptor. |
| 4201 | */ |
| 4202 | void quic_conn_fd_handler(int fd) |
| 4203 | { |
| 4204 | if (fdtab[fd].ev & FD_POLL_IN) |
| 4205 | quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv); |
| 4206 | } |
| 4207 | |
| 4208 | /* |
| 4209 | * Local variables: |
| 4210 | * c-indent-level: 8 |
| 4211 | * c-basic-offset: 8 |
| 4212 | * End: |
| 4213 | */ |