Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2 | * QUIC xprt layer. Act as an abstraction between quic_conn and MUX layers. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
| 14 | #include <haproxy/connection.h> |
| 15 | #include <haproxy/quic_conn.h> |
| 16 | #include <haproxy/ssl_sock.h> |
| 17 | #include <haproxy/trace.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 18 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 19 | #define TRACE_SOURCE &trace_quic |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 20 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 21 | static void quic_close(struct connection *conn, void *xprt_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 22 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 23 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 24 | struct quic_conn *qc = conn_ctx->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 25 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 26 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 28 | /* Next application data can be dropped. */ |
| 29 | qc->mux_state = QC_MUX_RELEASED; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 30 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 31 | /* If the quic-conn timer has already expired free the quic-conn. */ |
| 32 | if (qc->flags & QUIC_FL_CONN_EXP_TIMER) { |
| 33 | quic_conn_release(qc); |
Frédéric Lécaille | eb3e517 | 2023-04-12 13:41:54 +0200 | [diff] [blame] | 34 | qc = NULL; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 35 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 38 | /* Schedule a CONNECTION_CLOSE emission. If process stopping is in |
| 39 | * progress, quic-conn idle-timer will be scheduled immediately after |
| 40 | * its emission to ensure an immediate connection closing. |
| 41 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 42 | qc_check_close_on_released_mux(qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 43 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 44 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 47 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 48 | * event subscriber <es> is not allowed to change from a previous call as long |
| 49 | * as at least one event is still subscribed. The <event_type> must only be a |
| 50 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 51 | */ |
| 52 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 53 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 54 | struct quic_conn *qc = conn->handle.qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 55 | |
| 56 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 57 | |
| 58 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 59 | BUG_ON(qc->subs && qc->subs != es); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 60 | |
| 61 | es->events |= event_type; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 62 | qc->subs = es; |
| 63 | |
| 64 | /* TODO implement a check_events to detect if subscriber should be |
| 65 | * woken up immediately ? |
| 66 | */ |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 67 | |
| 68 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 69 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 70 | |
| 71 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 72 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 73 | |
| 74 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 75 | |
| 76 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 80 | * The <es> pointer is not allowed to differ from the one passed to the |
| 81 | * subscribe() call. It always returns zero. |
| 82 | */ |
| 83 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 84 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 85 | struct quic_conn *qc = conn->handle.qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 86 | |
| 87 | TRACE_ENTER(QUIC_EV_CONN_SUB, qc); |
| 88 | |
| 89 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 90 | TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 91 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 92 | TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 93 | |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 94 | es->events &= ~event_type; |
| 95 | if (!es->events) |
| 96 | qc->subs = NULL; |
| 97 | |
| 98 | /* TODO implement ignore_events similar to conn_unsubscribe() ? */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 99 | |
| 100 | TRACE_LEAVE(QUIC_EV_CONN_SUB, qc); |
| 101 | |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 102 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 105 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 106 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 107 | */ |
| 108 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 109 | { |
Amaury Denoyelle | 8fc267b | 2023-11-20 14:56:49 +0100 | [diff] [blame] | 110 | struct quic_conn *qc = conn->handle.qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 111 | |
Frédéric Lécaille | eb3e517 | 2023-04-12 13:41:54 +0200 | [diff] [blame] | 112 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 113 | |
Amaury Denoyelle | 8fc267b | 2023-11-20 14:56:49 +0100 | [diff] [blame] | 114 | /* Ensure thread connection migration is finalized ASAP. */ |
| 115 | if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED) |
| 116 | qc_finalize_affinity_rebind(qc); |
| 117 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 118 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 119 | if (*xprt_ctx) |
| 120 | goto out; |
| 121 | |
Amaury Denoyelle | 8fc267b | 2023-11-20 14:56:49 +0100 | [diff] [blame] | 122 | *xprt_ctx = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 123 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 124 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 125 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 126 | |
| 127 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 130 | /* Start the QUIC transport layer */ |
| 131 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 132 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 133 | int ret = 0; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 134 | struct quic_conn *qc; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 135 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 136 | qc = conn->handle.qc; |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 137 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 138 | |
| 139 | /* mux-quic can now be considered ready. */ |
| 140 | qc->mux_state = QC_MUX_READY; |
| 141 | |
Amaury Denoyelle | b75338e | 2024-03-04 18:41:39 +0100 | [diff] [blame] | 142 | /* Schedule quic-conn to ensure post handshake frames are emitted. This |
| 143 | * is not done for 0-RTT as xprt->start happens before handshake |
| 144 | * completion. |
| 145 | */ |
| 146 | if (qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) |
| 147 | tasklet_wakeup(qc->wait_event.tasklet); |
| 148 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 149 | ret = 1; |
| 150 | out: |
| 151 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 152 | return ret; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 155 | static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn) |
| 156 | { |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 157 | if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx) |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 158 | return NULL; |
| 159 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 160 | return conn->handle.qc->xprt_ctx; |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 163 | /* transport-layer operations for QUIC connections. */ |
| 164 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 165 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 166 | .subscribe = quic_conn_subscribe, |
| 167 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 168 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 169 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 170 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 171 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 172 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 54a1dcb | 2022-04-11 11:57:35 +0200 | [diff] [blame] | 173 | .get_ssl_sock_ctx = qc_get_ssl_sock_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 174 | .name = "QUIC", |
| 175 | }; |
| 176 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 177 | static void __quic_conn_init(void) |
| 178 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 179 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 180 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 181 | INITCALL0(STG_REGISTER, __quic_conn_init); |