blob: 752a4f3aec2e70c216cb14baaede3c6470f4dc94 [file] [log] [blame]
Olivier Houchardfe50bfb2019-05-27 12:09:19 +02001/*
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 Tarreau7ea393d2020-06-04 18:02:10 +020013#include <haproxy/connection.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020014#include <haproxy/stream_interface.h>
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020015
16struct xprt_handshake_ctx {
17 struct connection *conn;
Willy Tarreauac6febd2020-01-10 09:08:22 +010018 struct wait_event *subs;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020019 struct wait_event wait_event;
20 const struct xprt_ops *xprt;
21 void *xprt_ctx;
22};
23
24DECLARE_STATIC_POOL(xprt_handshake_ctx_pool, "xprt_handshake_ctx_pool", sizeof(struct xprt_handshake_ctx));
25
26/* This XPRT doesn't take care of sending or receiving data, once its handshake
27 * is done, it just removes itself
28 */
29static size_t xprt_handshake_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
30{
31 return 0;
32}
33
34static size_t xprt_handshake_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags)
35{
36 return 0;
37}
38
39static struct task *xprt_handshake_io_cb(struct task *t, void *bctx, unsigned short state)
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)
74 if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY)) {
75 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
76 &ctx->wait_event);
77 goto out;
78 }
79
80out:
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 Tarreau4450b582020-01-23 15:23:13 +010085 !(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020086 int ret = 0;
87 int woke = 0;
88 int was_conn_ctx = 0;
Willy Tarreauac6febd2020-01-10 09:08:22 +010089
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020090 /* On error, wake any waiter */
Willy Tarreauac6febd2020-01-10 09:08:22 +010091 if (ctx->subs) {
92 tasklet_wakeup(ctx->subs->tasklet);
93 ctx->subs->events = 0;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020094 woke = 1;
Willy Tarreauac6febd2020-01-10 09:08:22 +010095 ctx->subs = NULL;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020096 }
Willy Tarreauac6febd2020-01-10 09:08:22 +010097
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020098 /* 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 Houchard477902b2020-01-22 18:08:48 +0100112 * 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 Houchardfe50bfb2019-05-27 12:09:19 +0200115 */
116 if (was_conn_ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +0100117 if (!ctx->conn->mux)
118 ret = conn_create_mux(ctx->conn);
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200119 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
120 ret = ctx->conn->mux->wake(ctx->conn);
121 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200122 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200123 pool_free(xprt_handshake_ctx_pool, ctx);
124 }
125 return NULL;
126}
127
128static int xprt_handshake_init(struct connection *conn, void **xprt_ctx)
129{
130 struct xprt_handshake_ctx *ctx;
131 /* already initialized */
132 if (*xprt_ctx)
133 return 0;
134 if (!conn_ctrl_ready(conn))
135 return 0;
136
137 ctx = pool_alloc(xprt_handshake_ctx_pool);
138 if (!ctx) {
139 conn->err_code = CO_ER_SSL_NO_MEM;
140 return -1;
141 }
142 ctx->conn = conn;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200143 ctx->wait_event.tasklet = tasklet_new();
144 if (!ctx->wait_event.tasklet) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200145 conn->err_code = CO_ER_SSL_NO_MEM;
146 pool_free(xprt_handshake_ctx_pool, ctx);
147 return -1;
148 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200149 ctx->wait_event.tasklet->process = xprt_handshake_io_cb;
150 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200151 ctx->wait_event.events = 0;
152 /* This XPRT expects the underlying XPRT to be provided later,
153 * with an add_xprt() call, so we start trying to do the handshake
154 * there, when we'll be provided an XPRT.
155 */
156 ctx->xprt = NULL;
157 ctx->xprt_ctx = NULL;
Willy Tarreauac6febd2020-01-10 09:08:22 +0100158 ctx->subs = NULL;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200159 *xprt_ctx = ctx;
160
161 return 0;
162}
163
164static void xprt_handshake_close(struct connection *conn, void *xprt_ctx)
165{
166 struct xprt_handshake_ctx *ctx = xprt_ctx;
167
168 if (ctx) {
169 if (ctx->wait_event.events != 0)
170 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
171 ctx->wait_event.events,
172 &ctx->wait_event);
Willy Tarreauac6febd2020-01-10 09:08:22 +0100173 if (ctx->subs) {
174 ctx->subs->events = 0;
175 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200176 }
177
178 if (ctx->xprt && ctx->xprt->close)
179 ctx->xprt->close(conn, ctx->xprt_ctx);
180 /* Remove any handshake flag, and if we were the connection
181 * xprt, get back to XPRT_RAW. If we're here because we
182 * failed an outoging connection, it will be retried using
183 * the same struct connection, and as xprt_handshake is a bit
184 * magic, because it requires a call to add_xprt(), it's better
185 * to fallback to the original XPRT to re-initiate the
186 * connection.
187 */
Willy Tarreau4450b582020-01-23 15:23:13 +0100188 conn->flags &= ~CO_FL_HANDSHAKE;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200189 if (conn->xprt == xprt_get(XPRT_HANDSHAKE))
190 conn->xprt = xprt_get(XPRT_RAW);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200191 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200192 pool_free(xprt_handshake_ctx_pool, ctx);
193 }
194}
195
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100196/* Called from the upper layer, to subscribe <es> to events <event_type>. The
197 * event subscriber <es> is not allowed to change from a previous call as long
198 * as at least one event is still subscribed. The <event_type> must only be a
199 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
200 */
201static int xprt_handshake_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200202{
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200203 struct xprt_handshake_ctx *ctx = xprt_ctx;
204
Willy Tarreauac6febd2020-01-10 09:08:22 +0100205 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100206 BUG_ON(ctx->subs && ctx->subs != es);
Willy Tarreauac6febd2020-01-10 09:08:22 +0100207
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100208 ctx->subs = es;
209 es->events |= event_type;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200210 return 0;
211
212}
213
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100214/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
215 * The <es> pointer is not allowed to differ from the one passed to the
216 * subscribe() call. It always returns zero.
217 */
218static int xprt_handshake_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200219{
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200220 struct xprt_handshake_ctx *ctx = xprt_ctx;
221
Willy Tarreauac6febd2020-01-10 09:08:22 +0100222 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100223 BUG_ON(ctx->subs && ctx->subs != es);
Willy Tarreauac6febd2020-01-10 09:08:22 +0100224
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100225 es->events &= ~event_type;
226 if (!es->events)
Willy Tarreauac6febd2020-01-10 09:08:22 +0100227 ctx->subs = NULL;
228
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200229 return 0;
230}
231
232/* Use the provided XPRT as an underlying XPRT, and provide the old one.
233 * Returns 0 on success, and non-zero on failure.
234 */
235static 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)
236{
237 struct xprt_handshake_ctx *ctx = xprt_ctx;
238
239 if (oldxprt_ops)
240 *oldxprt_ops = ctx->xprt;
241 if (oldxprt_ctx)
242 *oldxprt_ctx = ctx->xprt_ctx;
243 ctx->xprt = toadd_ops;
244 ctx->xprt_ctx = toadd_ctx;
245 /* Ok we know have an xprt, so let's try to do the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200246 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200247 return 0;
248}
249
250/* Remove the specified xprt. If if it our underlying XPRT, remove it and
251 * return 0, otherwise just call the remove_xprt method from the underlying
252 * XPRT.
253 */
254static int xprt_handshake_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
255{
256 struct xprt_handshake_ctx *ctx = xprt_ctx;
257
258 if (ctx->xprt_ctx == toremove_ctx) {
259 ctx->xprt_ctx = newctx;
260 ctx->xprt = newops;
261 return 0;
262 }
263 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
264}
265
266struct xprt_ops xprt_handshake = {
267 .snd_buf = xprt_handshake_from_buf,
268 .rcv_buf = xprt_handshake_to_buf,
269 .subscribe = xprt_handshake_subscribe,
270 .unsubscribe = xprt_handshake_unsubscribe,
271 .remove_xprt = xprt_handshake_remove_xprt,
272 .add_xprt = xprt_handshake_add_xprt,
273 .init = xprt_handshake_init,
274 .close= xprt_handshake_close,
275 .rcv_pipe = NULL,
276 .snd_pipe = NULL,
277 .shutr = NULL,
278 .shutw = NULL,
279 .name = "HS",
280};
281
282__attribute__((constructor))
283static void __xprt_handshake_init(void)
284{
285 xprt_register(XPRT_HANDSHAKE, &xprt_handshake);
286}