Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Pseudo-xprt to handle any handshake except the SSL handshake |
| 3 | * |
| 4 | * Copyright 2019 HAProxy Technologies, Olivier Houchard <ohouchard@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 | |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 13 | #include <haproxy/connection.h> |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 14 | |
| 15 | struct xprt_handshake_ctx { |
| 16 | struct connection *conn; |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 17 | struct wait_event *subs; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 18 | struct wait_event wait_event; |
| 19 | const struct xprt_ops *xprt; |
| 20 | void *xprt_ctx; |
| 21 | }; |
| 22 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 23 | DECLARE_STATIC_POOL(xprt_handshake_ctx_pool, "xprt_handshake_ctx", sizeof(struct xprt_handshake_ctx)); |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 24 | |
| 25 | /* This XPRT doesn't take care of sending or receiving data, once its handshake |
| 26 | * is done, it just removes itself |
| 27 | */ |
| 28 | static size_t xprt_handshake_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static size_t xprt_handshake_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 38 | /* xprt_handshake_io_cb is exported to see it resolved in "show fd" */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 39 | struct task *xprt_handshake_io_cb(struct task *t, void *bctx, unsigned int state) |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 40 | { |
| 41 | struct xprt_handshake_ctx *ctx = bctx; |
| 42 | struct connection *conn = ctx->conn; |
| 43 | |
| 44 | if (conn->flags & CO_FL_SOCKS4_SEND) |
| 45 | if (!conn_send_socks4_proxy_request(conn)) { |
| 46 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, |
| 47 | &ctx->wait_event); |
| 48 | |
| 49 | goto out; |
| 50 | } |
| 51 | |
| 52 | if (conn->flags & CO_FL_SOCKS4_RECV) |
| 53 | if (!conn_recv_socks4_proxy_response(conn)) { |
| 54 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 55 | &ctx->wait_event); |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | if (conn->flags & CO_FL_ACCEPT_CIP) |
| 60 | if (!conn_recv_netscaler_cip(conn, CO_FL_ACCEPT_CIP)) { |
| 61 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 62 | &ctx->wait_event); |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | if (conn->flags & CO_FL_ACCEPT_PROXY) |
| 67 | if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY)) { |
| 68 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 69 | &ctx->wait_event); |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | if (conn->flags & CO_FL_SEND_PROXY) |
Christopher Faulet | aa91d62 | 2022-04-01 13:22:50 +0200 | [diff] [blame] | 74 | if (!conn_send_proxy(conn, CO_FL_SEND_PROXY)) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 75 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, |
| 76 | &ctx->wait_event); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | out: |
| 81 | /* Wake the stream if we're done with the handshake, or we have a |
| 82 | * connection error |
| 83 | * */ |
| 84 | if ((conn->flags & CO_FL_ERROR) || |
Willy Tarreau | 4450b58 | 2020-01-23 15:23:13 +0100 | [diff] [blame] | 85 | !(conn->flags & CO_FL_HANDSHAKE)) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 86 | int ret = 0; |
| 87 | int woke = 0; |
| 88 | int was_conn_ctx = 0; |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 89 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 90 | /* On error, wake any waiter */ |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 91 | if (ctx->subs) { |
| 92 | tasklet_wakeup(ctx->subs->tasklet); |
| 93 | ctx->subs->events = 0; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 94 | woke = 1; |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 95 | ctx->subs = NULL; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 96 | } |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 97 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 98 | /* Remove ourself from the xprt chain */ |
| 99 | if (ctx->wait_event.events != 0) |
| 100 | ctx->xprt->unsubscribe(ctx->conn, |
| 101 | ctx->xprt_ctx, |
| 102 | ctx->wait_event.events, |
| 103 | &ctx->wait_event); |
| 104 | if (conn->xprt_ctx == ctx) { |
| 105 | conn->xprt_ctx = ctx->xprt_ctx; |
| 106 | conn->xprt = ctx->xprt; |
| 107 | was_conn_ctx = 1; |
| 108 | } else |
| 109 | conn->xprt->remove_xprt(conn, conn->xprt_ctx, ctx, |
| 110 | ctx->xprt, ctx->xprt_ctx); |
| 111 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 112 | * upper layers know. If no mux was set up yet, then call |
| 113 | * conn_create_mux, and if we have a mux, and it has a wake |
| 114 | * method, call it too. |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 115 | */ |
| 116 | if (was_conn_ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 117 | if (!ctx->conn->mux) |
| 118 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 119 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 120 | ret = ctx->conn->mux->wake(ctx->conn); |
| 121 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 122 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 123 | pool_free(xprt_handshake_ctx_pool, ctx); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 124 | t = NULL; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 125 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 126 | return t; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Olivier Houchard | d54ede7 | 2021-03-05 23:42:41 +0100 | [diff] [blame] | 129 | static int xprt_handshake_start(struct connection *conn, void *xprt_ctx) |
| 130 | { |
| 131 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 132 | |
| 133 | if (ctx->xprt->start) { |
| 134 | int ret; |
| 135 | |
| 136 | ret = ctx->xprt->start(conn, ctx->xprt_ctx); |
| 137 | if (ret < 0) |
| 138 | return ret; |
| 139 | } |
| 140 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 145 | static int xprt_handshake_init(struct connection *conn, void **xprt_ctx) |
| 146 | { |
| 147 | struct xprt_handshake_ctx *ctx; |
| 148 | /* already initialized */ |
| 149 | if (*xprt_ctx) |
| 150 | return 0; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 151 | |
| 152 | ctx = pool_alloc(xprt_handshake_ctx_pool); |
| 153 | if (!ctx) { |
| 154 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 155 | return -1; |
| 156 | } |
| 157 | ctx->conn = conn; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 158 | ctx->wait_event.tasklet = tasklet_new(); |
| 159 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 160 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 161 | pool_free(xprt_handshake_ctx_pool, ctx); |
| 162 | return -1; |
| 163 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 164 | ctx->wait_event.tasklet->process = xprt_handshake_io_cb; |
| 165 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 166 | ctx->wait_event.events = 0; |
Olivier Houchard | d54ede7 | 2021-03-05 23:42:41 +0100 | [diff] [blame] | 167 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 168 | ctx->xprt = NULL; |
| 169 | ctx->xprt_ctx = NULL; |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 170 | ctx->subs = NULL; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 171 | *xprt_ctx = ctx; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void xprt_handshake_close(struct connection *conn, void *xprt_ctx) |
| 177 | { |
| 178 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 179 | |
| 180 | if (ctx) { |
| 181 | if (ctx->wait_event.events != 0) |
| 182 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 183 | ctx->wait_event.events, |
| 184 | &ctx->wait_event); |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 185 | if (ctx->subs) { |
| 186 | ctx->subs->events = 0; |
| 187 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (ctx->xprt && ctx->xprt->close) |
| 191 | ctx->xprt->close(conn, ctx->xprt_ctx); |
| 192 | /* Remove any handshake flag, and if we were the connection |
| 193 | * xprt, get back to XPRT_RAW. If we're here because we |
| 194 | * failed an outoging connection, it will be retried using |
| 195 | * the same struct connection, and as xprt_handshake is a bit |
| 196 | * magic, because it requires a call to add_xprt(), it's better |
| 197 | * to fallback to the original XPRT to re-initiate the |
| 198 | * connection. |
| 199 | */ |
Willy Tarreau | 4450b58 | 2020-01-23 15:23:13 +0100 | [diff] [blame] | 200 | conn->flags &= ~CO_FL_HANDSHAKE; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 201 | if (conn->xprt == xprt_get(XPRT_HANDSHAKE)) |
| 202 | conn->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 203 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 204 | pool_free(xprt_handshake_ctx_pool, ctx); |
| 205 | } |
| 206 | } |
| 207 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 208 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 209 | * event subscriber <es> is not allowed to change from a previous call as long |
| 210 | * as at least one event is still subscribed. The <event_type> must only be a |
| 211 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 212 | */ |
| 213 | static int xprt_handshake_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 214 | { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 215 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 216 | |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 217 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 218 | BUG_ON(ctx->subs && ctx->subs != es); |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 219 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 220 | ctx->subs = es; |
| 221 | es->events |= event_type; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | |
| 224 | } |
| 225 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 226 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 227 | * The <es> pointer is not allowed to differ from the one passed to the |
| 228 | * subscribe() call. It always returns zero. |
| 229 | */ |
| 230 | static int xprt_handshake_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 231 | { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 232 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 233 | |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 234 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 235 | BUG_ON(ctx->subs && ctx->subs != es); |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 236 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 237 | es->events &= ~event_type; |
| 238 | if (!es->events) |
Willy Tarreau | ac6febd | 2020-01-10 09:08:22 +0100 | [diff] [blame] | 239 | ctx->subs = NULL; |
| 240 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 245 | * Returns 0 on success, and non-zero on failure. |
| 246 | */ |
| 247 | static int xprt_handshake_add_xprt(struct connection *conn, void *xprt_ctx, void *toadd_ctx, const struct xprt_ops *toadd_ops, void **oldxprt_ctx, const struct xprt_ops **oldxprt_ops) |
| 248 | { |
| 249 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 250 | |
| 251 | if (oldxprt_ops) |
| 252 | *oldxprt_ops = ctx->xprt; |
| 253 | if (oldxprt_ctx) |
| 254 | *oldxprt_ctx = ctx->xprt_ctx; |
| 255 | ctx->xprt = toadd_ops; |
| 256 | ctx->xprt_ctx = toadd_ctx; |
Olivier Houchard | d54ede7 | 2021-03-05 23:42:41 +0100 | [diff] [blame] | 257 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 262 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 263 | * XPRT. |
| 264 | */ |
| 265 | static int xprt_handshake_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 266 | { |
| 267 | struct xprt_handshake_ctx *ctx = xprt_ctx; |
| 268 | |
| 269 | if (ctx->xprt_ctx == toremove_ctx) { |
| 270 | ctx->xprt_ctx = newctx; |
| 271 | ctx->xprt = newops; |
| 272 | return 0; |
| 273 | } |
| 274 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 275 | } |
| 276 | |
| 277 | struct xprt_ops xprt_handshake = { |
| 278 | .snd_buf = xprt_handshake_from_buf, |
| 279 | .rcv_buf = xprt_handshake_to_buf, |
| 280 | .subscribe = xprt_handshake_subscribe, |
| 281 | .unsubscribe = xprt_handshake_unsubscribe, |
| 282 | .remove_xprt = xprt_handshake_remove_xprt, |
| 283 | .add_xprt = xprt_handshake_add_xprt, |
| 284 | .init = xprt_handshake_init, |
Olivier Houchard | d54ede7 | 2021-03-05 23:42:41 +0100 | [diff] [blame] | 285 | .start = xprt_handshake_start, |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 286 | .close= xprt_handshake_close, |
| 287 | .rcv_pipe = NULL, |
| 288 | .snd_pipe = NULL, |
| 289 | .shutr = NULL, |
| 290 | .shutw = NULL, |
| 291 | .name = "HS", |
| 292 | }; |
| 293 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 294 | static void __xprt_handshake_init(void) |
| 295 | { |
| 296 | xprt_register(XPRT_HANDSHAKE, &xprt_handshake); |
| 297 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 298 | |
| 299 | INITCALL0(STG_REGISTER, __xprt_handshake_init); |