blob: da0d406cb9afb8b2dc69deeeacefb20123e6b354 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * Connection management functions
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
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 Tarreaue1e4a612012-10-05 00:10:55 +020013#include <errno.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020019#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/hash.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/log-t.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020022#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020023#include <haproxy/net_helper.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020024#include <haproxy/proto_tcp.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020025#include <haproxy/sample.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020026#include <haproxy/ssl_sock.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020027#include <haproxy/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020028
Alexander Liu2a54bb72019-05-22 19:44:48 +080029
Willy Tarreau8ceae722018-11-26 11:58:30 +010030DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
31DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
Willy Tarreauff5d57b2019-07-17 18:37:02 +020032DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
Geoff Simmons7185b782019-08-27 18:31:16 +020033DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010034
Willy Tarreau4d82bf52020-06-28 00:19:17 +020035struct idle_conns idle_conns[MAX_THREADS] = { };
Willy Tarreau13e14102016-12-22 20:25:26 +010036struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020037
Christopher Faulet32f61c02018-04-10 14:33:41 +020038/* List head of all known muxes for PROTO */
39struct mux_proto_list mux_proto_list = {
40 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020041};
42
Willy Tarreau119e50e2020-05-22 13:53:29 +020043/* disables sending of proxy-protocol-v2's LOCAL command */
44static int pp2_never_send_local;
45
Olivier Houchard477902b2020-01-22 18:08:48 +010046int conn_create_mux(struct connection *conn)
47{
Olivier Houchard477902b2020-01-22 18:08:48 +010048 if (conn_is_back(conn)) {
49 struct server *srv;
50 struct conn_stream *cs = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020051 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010052
53 if (conn->flags & CO_FL_ERROR)
54 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010055
Christopher Faulet14cd3162020-04-16 14:50:06 +020056 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
57 if (conn_install_mux_chk(conn, conn->ctx, conn->owner) < 0)
58 goto fail;
59 }
60 else if (conn_install_mux_be(conn, conn->ctx, conn->owner) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010061 goto fail;
62 srv = objt_server(conn->target);
63 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
64 conn->mux->avail_streams(conn) > 0)
Olivier Houchardf0d4dff2020-03-06 18:12:03 +010065 LIST_ADDQ(&srv->available_conns[tid], mt_list_to_list(&conn->list));
Olivier Houchard477902b2020-01-22 18:08:48 +010066 return 0;
67fail:
68 /* let the upper layer know the connection failed */
69 cs->data_cb->wake(cs);
70 return -1;
71 } else
72 return conn_complete_session(conn);
73
74}
75
Willy Tarreau59f98392012-07-06 14:13:49 +020076/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020077 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020078 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020079void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020080{
Willy Tarreau80184712012-07-06 14:54:49 +020081 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020082 unsigned int flags;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +010083 int need_wake = 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020084
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010085 if (unlikely(!conn)) {
86 activity[tid].conn_dead++;
Willy Tarreau7a798e52016-04-14 11:13:20 +020087 return;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010088 }
Willy Tarreau59f98392012-07-06 14:13:49 +020089
Willy Tarreau7d281492012-12-16 19:19:13 +010090 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010091
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010092 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
93 ((fd_send_ready(fd) && fd_send_active(fd)) ||
94 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
95 /* Still waiting for a connection to establish and nothing was
96 * attempted yet to probe the connection. this will clear the
97 * CO_FL_WAIT_L4_CONN flag on success.
98 */
99 if (!conn_fd_check(conn))
100 goto leave;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100101 need_wake = 1;
Willy Tarreaub2a7ab02019-12-27 10:54:22 +0100102 }
103
Willy Tarreau8081abe2019-11-28 18:08:49 +0100104 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100105 /* force reporting of activity by clearing the previous flags :
106 * we'll have at least ERROR or CONNECTED at the end of an I/O,
107 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200108 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100109 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100110 if (conn->subs && conn->subs->events & SUB_RETRY_SEND) {
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100111 need_wake = 0; // wake will be called after this I/O
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100112 tasklet_wakeup(conn->subs->tasklet);
113 conn->subs->events &= ~SUB_RETRY_SEND;
114 if (!conn->subs->events)
115 conn->subs = NULL;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100116 }
Willy Tarreau667fefd2020-03-04 17:22:10 +0100117 fd_stop_send(fd);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200118 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200119
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200120 /* The data transfer starts here and stops on error and handshakes. Note
121 * that we must absolutely test conn->xprt at each step in case it suddenly
122 * changes due to a quick unexpected close().
123 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100124 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100125 /* force reporting of activity by clearing the previous flags :
126 * we'll have at least ERROR or CONNECTED at the end of an I/O,
127 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200128 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100129 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100130 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100131 need_wake = 0; // wake will be called after this I/O
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100132 tasklet_wakeup(conn->subs->tasklet);
133 conn->subs->events &= ~SUB_RETRY_RECV;
134 if (!conn->subs->events)
135 conn->subs = NULL;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100136 }
Willy Tarreau4cabfc12020-06-17 16:26:22 +0200137 fd_stop_recv(fd);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200138 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200139
Willy Tarreau2c6be842012-07-06 17:12:34 +0200140 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100141 /* If we don't yet have a mux, that means we were waiting for
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500142 * information to create one, typically from the ALPN. If we're
Olivier Houchard477902b2020-01-22 18:08:48 +0100143 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200144 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100145 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100146 if (conn_create_mux(conn) < 0)
147 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200148
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100149 /* The wake callback is normally used to notify the data layer about
150 * data layer activity (successful send/recv), connection establishment,
151 * shutdown and fatal errors. We need to consider the following
152 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100153 * - change among the CO_FL_NOTIFY_DONE flags :
154 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100155 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
156 * end of handshake and transition to CONNECTED
157 * - raise of CONNECTED with HANDSHAKE down
158 * - end of HANDSHAKE with CONNECTED set
159 * - regular data layer activity
160 *
161 * Note that the wake callback is allowed to release the connection and
162 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200163 */
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100164 if ((need_wake || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
Willy Tarreau911db9b2020-01-23 16:27:54 +0100165 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200166 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200167 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200168
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200169 /* commit polling changes */
170 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200171 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200172}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200173
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100174/* This is the callback which is set when a connection establishment is pending
175 * and we have nothing to send. It may update the FD polling status to indicate
176 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
177 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
178 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
179 * leaves the error code in errno.
180 */
181int conn_fd_check(struct connection *conn)
182{
183 struct sockaddr_storage *addr;
184 int fd = conn->handle.fd;
185
186 if (conn->flags & CO_FL_ERROR)
187 return 0;
188
189 if (!conn_ctrl_ready(conn))
190 return 0;
191
192 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
193 return 1; /* strange we were called while ready */
194
195 if (!fd_send_ready(fd))
196 return 0;
197
198 /* Here we have 2 cases :
199 * - modern pollers, able to report ERR/HUP. If these ones return any
200 * of these flags then it's likely a failure, otherwise it possibly
201 * is a success (i.e. there may have been data received just before
202 * the error was reported).
203 * - select, which doesn't report these and with which it's always
204 * necessary either to try connect() again or to check for SO_ERROR.
205 * In order to simplify everything, we double-check using connect() as
206 * soon as we meet either of these delicate situations. Note that
207 * SO_ERROR would clear the error after reporting it!
208 */
209 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
210 /* modern poller, able to report ERR/HUP */
211 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
212 goto done;
213 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
214 goto done;
215 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
216 goto wait;
217 /* error present, fall through common error check path */
218 }
219
220 /* Use connect() to check the state of the socket. This has the double
221 * advantage of *not* clearing the error (so that health checks can
222 * still use getsockopt(SO_ERROR)) and giving us the following info :
223 * - error
224 * - connecting (EALREADY, EINPROGRESS)
225 * - connected (EISCONN, 0)
226 */
227 addr = conn->dst;
228 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
229 addr = &objt_server(conn->target)->socks4_addr;
230
231 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
232 if (errno == EALREADY || errno == EINPROGRESS)
233 goto wait;
234
235 if (errno && errno != EISCONN)
236 goto out_error;
237 }
238
239 done:
240 /* The FD is ready now, we'll mark the connection as complete and
241 * forward the event to the transport layer which will notify the
242 * data layer.
243 */
244 conn->flags &= ~CO_FL_WAIT_L4_CONN;
245 fd_may_send(fd);
246 fd_cond_recv(fd);
247 errno = 0; // make health checks happy
248 return 1;
249
250 out_error:
251 /* Write error on the file descriptor. Report it to the connection
252 * and disable polling on this FD.
253 */
254 fdtab[fd].linger_risk = 0;
255 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau5d4d1802020-02-21 09:58:29 +0100256 conn_stop_polling(conn);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100257 return 0;
258
259 wait:
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100260 fd_cant_send(fd);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100261 fd_want_send(fd);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100262 return 0;
263}
264
Willy Tarreauff3e6482015-03-12 23:56:52 +0100265/* Send a message over an established connection. It makes use of send() and
266 * returns the same return code and errno. If the socket layer is not ready yet
267 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
268 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
269 * EMSGSIZE if called with a zero length message. The purpose is to simplify
270 * some rare attempts to directly write on the socket from above the connection
271 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
272 * It automatically retries on EINTR. Other errors cause the connection to be
273 * marked as in error state. It takes similar arguments as send() except the
274 * first one which is the connection instead of the file descriptor. Note,
275 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
276 */
277int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
278{
279 int ret;
280
281 ret = -1;
282 errno = ENOTSOCK;
283
284 if (conn->flags & CO_FL_SOCK_WR_SH)
285 goto fail;
286
287 if (!conn_ctrl_ready(conn))
288 goto fail;
289
290 errno = EMSGSIZE;
291 if (!len)
292 goto fail;
293
Willy Tarreau585744b2017-08-24 14:31:19 +0200294 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100295 goto wait;
296
297 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200298 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100299 } while (ret < 0 && errno == EINTR);
300
301
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200302 if (ret > 0) {
303 if (conn->flags & CO_FL_WAIT_L4_CONN) {
304 conn->flags &= ~CO_FL_WAIT_L4_CONN;
305 fd_may_send(conn->handle.fd);
306 fd_cond_recv(conn->handle.fd);
307 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100308 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200309 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100310
311 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
312 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200313 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100314 return 0;
315 }
316 fail:
317 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
318 return ret;
319}
320
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100321/* Called from the upper layer, to subscribe <es> to events <event_type>. The
322 * event subscriber <es> is not allowed to change from a previous call as long
323 * as at least one event is still subscribed. The <event_type> must only be a
324 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
325 */
326int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200327{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100328 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100329 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100330
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100331 es->events &= ~event_type;
332 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100333 conn->subs = NULL;
334
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100335 if (conn_ctrl_ready(conn)) {
336 if (event_type & SUB_RETRY_RECV)
337 fd_stop_recv(conn->handle.fd);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100338
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100339 if (event_type & SUB_RETRY_SEND)
340 fd_stop_send(conn->handle.fd);
341 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200342 return 0;
343}
344
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100345/* Called from the upper layer, to subscribe <es> to events <event_type>.
346 * The <es> struct is not allowed to differ from the one passed during a
347 * previous call to subscribe(). If the FD is ready, the wait_event is
348 * immediately woken up and the subcription is cancelled. It always
349 * returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100350 */
351int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200352{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100353 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100354 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100355
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100356 if (conn->subs && (conn->subs->events & event_type) == event_type)
357 return 0;
358
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100359 conn->subs = es;
360 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100361
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100362 if (conn_ctrl_ready(conn)) {
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100363 if (event_type & SUB_RETRY_RECV) {
364 if (fd_recv_ready(conn->handle.fd)) {
365 tasklet_wakeup(es->tasklet);
366 es->events &= ~SUB_RETRY_RECV;
367 if (!es->events)
368 conn->subs = NULL;
369 }
370 else
371 fd_want_recv(conn->handle.fd);
372 }
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100373
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100374 if (event_type & SUB_RETRY_SEND) {
375 if (fd_send_ready(conn->handle.fd)) {
376 tasklet_wakeup(es->tasklet);
377 es->events &= ~SUB_RETRY_SEND;
378 if (!es->events)
379 conn->subs = NULL;
380 }
381 else
382 fd_want_send(conn->handle.fd);
383 }
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100384 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200385 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200386}
387
Willy Tarreaud85c4852015-03-13 00:40:28 +0100388/* Drains possibly pending incoming data on the file descriptor attached to the
389 * connection and update the connection's flags accordingly. This is used to
390 * know whether we need to disable lingering on close. Returns non-zero if it
391 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
392 * flag may also be updated if the incoming shutdown was reported by the drain()
393 * function.
394 */
395int conn_sock_drain(struct connection *conn)
396{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200397 int turns = 2;
398 int len;
399
Willy Tarreaud85c4852015-03-13 00:40:28 +0100400 if (!conn_ctrl_ready(conn))
401 return 1;
402
403 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
404 return 1;
405
Willy Tarreaue215bba2018-08-24 14:31:53 +0200406 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
407 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100408
Willy Tarreaue215bba2018-08-24 14:31:53 +0200409 if (!fd_recv_ready(conn->handle.fd))
410 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100411
Willy Tarreaue215bba2018-08-24 14:31:53 +0200412 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200413 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100414 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200415 goto shut;
416 }
417
418 /* no drain function defined, use the generic one */
419
420 while (turns) {
421#ifdef MSG_TRUNC_CLEARS_INPUT
422 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
423 if (len == -1 && errno == EFAULT)
424#endif
425 len = recv(conn->handle.fd, trash.area, trash.size,
426 MSG_DONTWAIT | MSG_NOSIGNAL);
427
428 if (len == 0)
429 goto shut;
430
431 if (len < 0) {
432 if (errno == EAGAIN) {
433 /* connection not closed yet */
434 fd_cant_recv(conn->handle.fd);
435 break;
436 }
437 if (errno == EINTR) /* oops, try again */
438 continue;
439 /* other errors indicate a dead connection, fine. */
440 goto shut;
441 }
442 /* OK we read some data, let's try again once */
443 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100444 }
445
Willy Tarreaue215bba2018-08-24 14:31:53 +0200446 /* some data are still present, give up */
447 return 0;
448
449 shut:
450 /* we're certain the connection was shut down */
451 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100452 conn->flags |= CO_FL_SOCK_RD_SH;
453 return 1;
454}
455
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100456/*
457 * Get data length from tlv
458 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100459static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100460{
461 return (src->length_hi << 8) | src->length_lo;
462}
463
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200464/* This handshake handler waits a PROXY protocol header at the beginning of the
465 * raw data stream. The header looks like this :
466 *
467 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
468 *
469 * There must be exactly one space between each field. Fields are :
470 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
471 * - SRC3 : layer 3 (eg: IP) source address in standard text form
472 * - DST3 : layer 3 (eg: IP) destination address in standard text form
473 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
474 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
475 *
476 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
477 *
478 * The header line is small and in all cases smaller than the smallest normal
479 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
480 * can safely use MSG_PEEK and avoid buffering.
481 *
482 * Once the data is fetched, the values are set in the connection's address
483 * fields, and data are removed from the socket's buffer. The function returns
484 * zero if it needs to wait for more data or if it fails, or 1 if it completed
485 * and removed itself.
486 */
487int conn_recv_proxy(struct connection *conn, int flag)
488{
489 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200490 struct proxy_hdr_v2 *hdr_v2;
491 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100492 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100493 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200494 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200495
Willy Tarreau3c728722014-01-23 13:50:42 +0100496 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200497 goto fail;
498
Willy Tarreauca79f592019-07-17 19:04:47 +0200499 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
500 goto fail;
501
Willy Tarreau585744b2017-08-24 14:31:19 +0200502 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200503 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100504
Willy Tarreau157788c2020-02-11 10:08:05 +0100505 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200506 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
507 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200508 if (errno == EINTR)
509 continue;
510 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200511 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200512 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200513 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100514 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200515 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200516 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100517 break;
518 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200519
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100521 /* client shutdown */
522 conn->err_code = CO_ER_PRX_EMPTY;
523 goto fail;
524 }
525
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100526 conn->flags &= ~CO_FL_WAIT_L4_CONN;
527
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200528 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200529 goto missing;
530
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 line = trash.area;
532 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200533
534 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200535 if (strncmp(line, "PROXY ", 6) != 0)
536 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537
538 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200539 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200540 goto missing;
541
David CARLIER42ff05e2016-03-24 09:22:36 +0000542 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200543 u32 src3, dst3, sport, dport;
544
545 line += 5;
546
547 src3 = inetaddr_host_lim_ret(line, end, &line);
548 if (line == end)
549 goto missing;
550 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100551 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200552
553 dst3 = inetaddr_host_lim_ret(line, end, &line);
554 if (line == end)
555 goto missing;
556 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100557 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200558
559 sport = read_uint((const char **)&line, end);
560 if (line == end)
561 goto missing;
562 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100563 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200564
565 dport = read_uint((const char **)&line, end);
566 if (line > end - 2)
567 goto missing;
568 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100569 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200570 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100571 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200572
573 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200574 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
575 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
576 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200577
Willy Tarreau226572f2019-07-17 14:46:00 +0200578 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
579 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
580 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200581 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
582 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000583 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200584 u32 sport, dport;
585 char *src_s;
586 char *dst_s, *sport_s, *dport_s;
587 struct in6_addr src3, dst3;
588
589 line += 5;
590
591 src_s = line;
592 dst_s = sport_s = dport_s = NULL;
593 while (1) {
594 if (line > end - 2) {
595 goto missing;
596 }
597 else if (*line == '\r') {
598 *line = 0;
599 line++;
600 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100601 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200602 break;
603 }
604
605 if (*line == ' ') {
606 *line = 0;
607 if (!dst_s)
608 dst_s = line + 1;
609 else if (!sport_s)
610 sport_s = line + 1;
611 else if (!dport_s)
612 dport_s = line + 1;
613 }
614 line++;
615 }
616
617 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100618 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200619
620 sport = read_uint((const char **)&sport_s,dport_s - 1);
621 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100622 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623
624 dport = read_uint((const char **)&dport_s,line - 2);
625 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100626 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627
628 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100629 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200630
631 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100632 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633
634 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200635 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
636 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
637 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200638
Willy Tarreau226572f2019-07-17 14:46:00 +0200639 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
640 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
641 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200642 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
643 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200644 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
645 /* This can be a UNIX socket forwarded by an haproxy upstream */
646 line += 9;
647 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200648 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200649 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100650 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200651 goto fail;
652 }
653
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200655 goto eat_header;
656
657 not_v1:
658 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200660 goto missing;
661
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200663
664 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
665 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
666 conn->err_code = CO_ER_PRX_NOT_HDR;
667 goto fail;
668 }
669
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100670 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
671 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200672 goto missing;
673
674 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
675 case 0x01: /* PROXY command */
676 switch (hdr_v2->fam) {
677 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100678 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
679 goto bad_header;
680
Willy Tarreau226572f2019-07-17 14:46:00 +0200681 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
682 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
683 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
684 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
685 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
686 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200687 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200688 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200689 break;
690 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100691 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
692 goto bad_header;
693
Willy Tarreau226572f2019-07-17 14:46:00 +0200694 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
695 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
696 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
697 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
698 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
699 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200700 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200701 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200702 break;
703 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100704
705 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100706 while (tlv_offset < total_v2_len) {
707 struct tlv *tlv_packet;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100708 size_t tlv_len;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100709
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100710 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
711 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
712 goto bad_header;
713
714 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
715 tlv_len = get_tlv_length(tlv_packet);
716 tlv_offset += tlv_len + TLV_HEADER_SIZE;
717
718 /* Verify that the TLV length does not exceed the total PROXYv2 length */
719 if (tlv_offset > total_v2_len)
720 goto bad_header;
721
722 switch (tlv_packet->type) {
723 case PP2_TYPE_CRC32C: {
724 uint32_t n_crc32c;
725
726 /* Verify that this TLV is exactly 4 bytes long */
727 if (tlv_len != 4)
728 goto bad_header;
729
730 n_crc32c = read_n32(tlv_packet->value);
731 write_n32(tlv_packet->value, 0); // compute with CRC==0
732
733 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
734 goto bad_header;
735 break;
736 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200737#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100738 case PP2_TYPE_NETNS: {
739 const struct netns_entry *ns;
740
741 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
742 if (ns)
743 conn->proxy_netns = ns;
744 break;
745 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100746#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100747 case PP2_TYPE_AUTHORITY: {
748 if (tlv_len > PP2_AUTHORITY_MAX)
749 goto bad_header;
750 conn->proxy_authority = pool_alloc(pool_head_authority);
751 if (conn->proxy_authority == NULL)
752 goto fail;
753 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
754 conn->proxy_authority_len = tlv_len;
755 break;
756 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100757 case PP2_TYPE_UNIQUE_ID: {
758 const struct ist tlv = ist2((const char *)tlv_packet->value, tlv_len);
759
760 if (tlv.len > UNIQUEID_LEN)
761 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +0100762 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100763 if (!isttest(conn->proxy_unique_id))
764 goto fail;
765 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
766 /* This is technically unreachable, because we verified above
767 * that the TLV value fits.
768 */
769 goto fail;
770 }
771 break;
772 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100773 default:
774 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100775 }
776 }
777
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100778 /* Verify that the PROXYv2 header ends at a TLV boundary.
779 * This is technically unreachable, because the TLV parsing already
780 * verifies that a TLV does not exceed the total length and also
781 * that there is space for a TLV header.
782 */
783 if (tlv_offset != total_v2_len)
784 goto bad_header;
785
Willy Tarreau77992672014-06-14 11:06:17 +0200786 /* unsupported protocol, keep local connection address */
787 break;
788 case 0x00: /* LOCAL command */
789 /* keep local connection address for LOCAL */
790 break;
791 default:
792 goto bad_header; /* not a supported command */
793 }
794
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100795 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +0200796 goto eat_header;
797
798 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200799 /* remove the PROXY line from the request. For this we re-read the
800 * exact line at once. If we don't get the exact same result, we
801 * fail.
802 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100803 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +0100804 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
805
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200806 if (len2 < 0 && errno == EINTR)
807 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100809 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100810 break;
811 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200812
813 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100814 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200815 return 1;
816
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200817 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200818 return 0;
819
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200820 missing:
821 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
822 * we have not read anything. Otherwise we need to fail because we won't
823 * be able to poll anymore.
824 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100825 conn->err_code = CO_ER_PRX_TRUNCATED;
826 goto fail;
827
828 bad_header:
829 /* This is not a valid proxy protocol header */
830 conn->err_code = CO_ER_PRX_BAD_HDR;
831 goto fail;
832
833 recv_abort:
834 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100835 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100836 goto fail;
837
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200838 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200839 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200840 return 0;
841}
842
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100843/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000844 * at the beginning of the raw data stream. The header format is
845 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100846 *
847 * This line MUST be at the beginning of the buffer and MUST NOT be
848 * fragmented.
849 *
850 * The header line is small and in all cases smaller than the smallest normal
851 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
852 * can safely use MSG_PEEK and avoid buffering.
853 *
854 * Once the data is fetched, the values are set in the connection's address
855 * fields, and data are removed from the socket's buffer. The function returns
856 * zero if it needs to wait for more data or if it fails, or 1 if it completed
857 * and removed itself.
858 */
859int conn_recv_netscaler_cip(struct connection *conn, int flag)
860{
861 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000862 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100863 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200864 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100865
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100866 if (!conn_ctrl_ready(conn))
867 goto fail;
868
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100869 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
870 goto fail;
871
Willy Tarreau585744b2017-08-24 14:31:19 +0200872 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200873 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100874
Willy Tarreau157788c2020-02-11 10:08:05 +0100875 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200876 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
877 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100878 if (errno == EINTR)
879 continue;
880 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200881 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200882 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100883 }
884 goto recv_abort;
885 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200886 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100887 break;
888 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100889
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100890 conn->flags &= ~CO_FL_WAIT_L4_CONN;
891
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200892 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100893 /* client shutdown */
894 conn->err_code = CO_ER_CIP_EMPTY;
895 goto fail;
896 }
897
898 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000899 * CIP magic, header length or
900 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100902 goto missing;
903
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200904 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100905
906 /* Decode a possible NetScaler Client IP request, fail early if
907 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100908 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100909 goto bad_magic;
910
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000911 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200912 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100913 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000914 line += 8;
915 }
916 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200917 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100918 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000919 line += 12;
920 }
921 /* Unknown CIP protocol */
922 else {
923 conn->err_code = CO_ER_CIP_BAD_PROTO;
924 goto fail;
925 }
926
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100927 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000928 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200929 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100930 goto missing;
931
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100932 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100933 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100934
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100935 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100936 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100937 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100938
939 hdr_ip4 = (struct ip *)line;
940
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200941 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100942 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000943 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100944 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000945 }
946 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100947 /* The protocol does not include a TCP header */
948 conn->err_code = CO_ER_CIP_BAD_PROTO;
949 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000950 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100951
David Carlier3015a2e2016-07-04 22:51:33 +0100952 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100953
954 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200955 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
956 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
957 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100958
Willy Tarreau226572f2019-07-17 14:46:00 +0200959 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
960 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
961 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100962
963 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
964 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100965 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100966 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100967 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100968
969 hdr_ip6 = (struct ip6_hdr *)line;
970
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200971 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100972 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000973 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100974 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000975 }
976 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100977 /* The protocol does not include a TCP header */
978 conn->err_code = CO_ER_CIP_BAD_PROTO;
979 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000980 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100981
David Carlier3015a2e2016-07-04 22:51:33 +0100982 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100983
984 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200985 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
986 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
987 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100988
Willy Tarreau226572f2019-07-17 14:46:00 +0200989 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
990 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
991 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100992
993 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
994 }
995 else {
996 /* The protocol does not match something known (IPv4/IPv6) */
997 conn->err_code = CO_ER_CIP_BAD_PROTO;
998 goto fail;
999 }
1000
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001001 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001002 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001003
1004 /* remove the NetScaler Client IP header from the request. For this
1005 * we re-read the exact line at once. If we don't get the exact same
1006 * result, we fail.
1007 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001008 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001009 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001010 if (len2 < 0 && errno == EINTR)
1011 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001012 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001013 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001014 break;
1015 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001016
1017 conn->flags &= ~flag;
1018 return 1;
1019
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001020 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001021 return 0;
1022
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001023 missing:
1024 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1025 * we have not read anything. Otherwise we need to fail because we won't
1026 * be able to poll anymore.
1027 */
1028 conn->err_code = CO_ER_CIP_TRUNCATED;
1029 goto fail;
1030
1031 bad_magic:
1032 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1033 goto fail;
1034
1035 recv_abort:
1036 conn->err_code = CO_ER_CIP_ABORT;
1037 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1038 goto fail;
1039
1040 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001041 conn->flags |= CO_FL_ERROR;
1042 return 0;
1043}
1044
Alexander Liu2a54bb72019-05-22 19:44:48 +08001045
1046int conn_send_socks4_proxy_request(struct connection *conn)
1047{
1048 struct socks4_request req_line;
1049
Alexander Liu2a54bb72019-05-22 19:44:48 +08001050 if (!conn_ctrl_ready(conn))
1051 goto out_error;
1052
Willy Tarreau226572f2019-07-17 14:46:00 +02001053 if (!conn_get_dst(conn))
1054 goto out_error;
1055
Alexander Liu2a54bb72019-05-22 19:44:48 +08001056 req_line.version = 0x04;
1057 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001058 req_line.port = get_net_port(conn->dst);
1059 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001060 memcpy(req_line.user_id, "HAProxy\0", 8);
1061
1062 if (conn->send_proxy_ofs > 0) {
1063 /*
1064 * This is the first call to send the request
1065 */
1066 conn->send_proxy_ofs = -(int)sizeof(req_line);
1067 }
1068
1069 if (conn->send_proxy_ofs < 0) {
1070 int ret = 0;
1071
1072 /* we are sending the socks4_req_line here. If the data layer
1073 * has a pending write, we'll also set MSG_MORE.
1074 */
1075 ret = conn_sock_send(
1076 conn,
1077 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1078 -conn->send_proxy_ofs,
Willy Tarreau19bc2012020-02-21 08:46:19 +01001079 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001080
1081 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1082 conn->handle.fd, -conn->send_proxy_ofs, ret);
1083
1084 if (ret < 0) {
1085 goto out_error;
1086 }
1087
1088 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1089 if (conn->send_proxy_ofs != 0) {
1090 goto out_wait;
1091 }
1092 }
1093
1094 /* OK we've the whole request sent */
1095 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001096
1097 /* The connection is ready now, simply return and let the connection
1098 * handler notify upper layers if needed.
1099 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001100 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001101
1102 if (conn->flags & CO_FL_SEND_PROXY) {
1103 /*
1104 * Get the send_proxy_ofs ready for the send_proxy due to we are
1105 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1106 * before sending PROXY Protocol.
1107 */
1108 conn->send_proxy_ofs = 1;
1109 }
1110 return 1;
1111
1112 out_error:
1113 /* Write error on the file descriptor */
1114 conn->flags |= CO_FL_ERROR;
1115 if (conn->err_code == CO_ER_NONE) {
1116 conn->err_code = CO_ER_SOCKS4_SEND;
1117 }
1118 return 0;
1119
1120 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001121 return 0;
1122}
1123
1124int conn_recv_socks4_proxy_response(struct connection *conn)
1125{
1126 char line[SOCKS4_HS_RSP_LEN];
1127 int ret;
1128
Alexander Liu2a54bb72019-05-22 19:44:48 +08001129 if (!conn_ctrl_ready(conn))
1130 goto fail;
1131
1132 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001133 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001134
Willy Tarreau157788c2020-02-11 10:08:05 +01001135 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001136 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1137 * Try to peek into it, before all 8 bytes ready.
1138 */
1139 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1140
1141 if (ret == 0) {
1142 /* the socket has been closed or shutdown for send */
1143 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1144 conn->handle.fd, ret, errno);
1145 if (conn->err_code == CO_ER_NONE) {
1146 conn->err_code = CO_ER_SOCKS4_RECV;
1147 }
1148 goto fail;
1149 }
1150
1151 if (ret > 0) {
1152 if (ret == SOCKS4_HS_RSP_LEN) {
1153 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1154 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1155 }else{
1156 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], first byte is [%02X], last bye is [%02X]\n", conn->handle.fd, ret, line[0], line[ret-1]);
1157 }
1158 } else {
1159 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1160 }
1161
1162 if (ret < 0) {
1163 if (errno == EINTR) {
1164 continue;
1165 }
1166 if (errno == EAGAIN) {
1167 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001168 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001169 }
1170 goto recv_abort;
1171 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001172 break;
1173 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001174
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001175 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1176
Alexander Liu2a54bb72019-05-22 19:44:48 +08001177 if (ret < SOCKS4_HS_RSP_LEN) {
1178 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1179 * we are not able to read enough data.
1180 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001181 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001182 }
1183
1184 /*
1185 * Base on the SOCSK4 protocol:
1186 *
1187 * +----+----+----+----+----+----+----+----+
1188 * | VN | CD | DSTPORT | DSTIP |
1189 * +----+----+----+----+----+----+----+----+
1190 * # of bytes: 1 1 2 4
1191 * VN is the version of the reply code and should be 0. CD is the result
1192 * code with one of the following values:
1193 * 90: request granted
1194 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001195 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001196 * 93: request rejected because the client program and identd report different user-ids
1197 * The remaining fields are ignored.
1198 */
1199 if (line[1] != 90) {
1200 conn->flags &= ~CO_FL_SOCKS4_RECV;
1201
1202 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1203 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1204 if (conn->err_code == CO_ER_NONE) {
1205 conn->err_code = CO_ER_SOCKS4_DENY;
1206 }
1207 goto fail;
1208 }
1209
1210 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001211 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001212 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1213 if (ret < 0 && errno == EINTR) {
1214 continue;
1215 }
1216 if (ret != SOCKS4_HS_RSP_LEN) {
1217 if (conn->err_code == CO_ER_NONE) {
1218 conn->err_code = CO_ER_SOCKS4_RECV;
1219 }
1220 goto fail;
1221 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001222 break;
1223 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001224
1225 conn->flags &= ~CO_FL_SOCKS4_RECV;
1226 return 1;
1227
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001228 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001229 return 0;
1230
Alexander Liu2a54bb72019-05-22 19:44:48 +08001231 recv_abort:
1232 if (conn->err_code == CO_ER_NONE) {
1233 conn->err_code = CO_ER_SOCKS4_ABORT;
1234 }
1235 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1236 goto fail;
1237
1238 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001239 conn->flags |= CO_FL_ERROR;
1240 return 0;
1241}
1242
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001243/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001244int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001245{
1246 int ret = 0;
1247
1248 if (srv && (srv->pp_opts & SRV_PP_V2)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001249 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
David Safb76832014-05-08 23:42:08 -04001250 }
1251 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001252 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1253 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001254 else
1255 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1256 }
1257
1258 return ret;
1259}
1260
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001261/* Makes a PROXY protocol line from the two addresses. The output is sent to
1262 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1263 * It returns the number of bytes composing this line (including the trailing
1264 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001265 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1266 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001267 */
David Safb76832014-05-08 23:42:08 -04001268int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001269{
1270 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001271 char * protocol;
1272 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1273 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1274 in_port_t src_port;
1275 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001276
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001277 if ( !src
1278 || !dst
1279 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1280 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1281 /* unknown family combination */
1282 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001283 if (ret >= buf_len)
1284 return 0;
1285
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001286 return ret;
1287 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001288
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001289 /* IPv4 for both src and dst */
1290 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1291 protocol = "TCP4";
1292 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001293 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001294 src_port = ((struct sockaddr_in *)src)->sin_port;
1295 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001296 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001297 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001298 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001299 /* IPv6 for at least one of src and dst */
1300 else {
1301 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001302
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001303 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001304
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001305 if (src->ss_family == AF_INET) {
1306 /* Convert src to IPv6 */
1307 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1308 src_port = ((struct sockaddr_in *)src)->sin_port;
1309 }
1310 else {
1311 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1312 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1313 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001314
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001315 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001316 return 0;
1317
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001318 if (dst->ss_family == AF_INET) {
1319 /* Convert dst to IPv6 */
1320 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1321 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1322 }
1323 else {
1324 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1325 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1326 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001327
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001328 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001329 return 0;
1330 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001331
1332 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1333 if (ret >= buf_len)
1334 return 0;
1335
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001336 return ret;
1337}
David Safb76832014-05-08 23:42:08 -04001338
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001339static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001340{
1341 struct tlv *tlv;
1342
1343 if (!dest || (length + sizeof(*tlv) > dest_len))
1344 return 0;
1345
1346 tlv = (struct tlv *)dest;
1347
1348 tlv->type = type;
1349 tlv->length_hi = length >> 8;
1350 tlv->length_lo = length & 0x00ff;
1351 memcpy(tlv->value, value, length);
1352 return length + sizeof(*tlv);
1353}
David Safb76832014-05-08 23:42:08 -04001354
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001355/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001356int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001357{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001358 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001359 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001360 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001361 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001362 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001363 struct sockaddr_storage *src = &null_addr;
1364 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001365 const char *value;
1366 int value_len;
David Safb76832014-05-08 23:42:08 -04001367
1368 if (buf_len < PP2_HEADER_LEN)
1369 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001370 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001371
Willy Tarreau226572f2019-07-17 14:46:00 +02001372 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1373 src = remote->src;
1374 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001375 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001376
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001377 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1378 if ( !src
1379 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001380 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001381 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1382 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001383 if (buf_len < PP2_HDR_LEN_UNSPEC)
1384 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001385 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1386 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001387 ret = PP2_HDR_LEN_UNSPEC;
1388 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001389 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001390 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001391 /* IPv4 for both src and dst */
1392 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1393 if (buf_len < PP2_HDR_LEN_INET)
1394 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001395 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1396 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1397 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1398 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1399 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1400 ret = PP2_HDR_LEN_INET;
1401 }
1402 /* IPv6 for at least one of src and dst */
1403 else {
1404 struct in6_addr tmp;
1405
1406 if (buf_len < PP2_HDR_LEN_INET6)
1407 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001408 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1409 if (src->ss_family == AF_INET) {
1410 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1411 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1412 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1413 }
1414 else {
1415 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1416 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1417 }
1418 if (dst->ss_family == AF_INET) {
1419 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1420 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001421 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001422 }
1423 else {
1424 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1425 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1426 }
1427
1428 ret = PP2_HDR_LEN_INET6;
1429 }
1430 }
David Safb76832014-05-08 23:42:08 -04001431
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001432 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1433 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001434
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001435 if ((buf_len - ret) < sizeof(struct tlv))
1436 return 0;
1437 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1438 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1439 }
1440
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001441 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001442 if ((buf_len - ret) < sizeof(struct tlv))
1443 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001444 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001445 }
1446
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001447 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001448 value = NULL;
1449 if (remote && remote->proxy_authority) {
1450 value = remote->proxy_authority;
1451 value_len = remote->proxy_authority_len;
1452 }
1453#ifdef USE_OPENSSL
1454 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001455 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001456 value_len = strlen(value);
1457 }
1458#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001459 if (value) {
1460 if ((buf_len - ret) < sizeof(struct tlv))
1461 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001462 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001463 }
1464 }
1465
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001466 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001467 struct session* sess = strm_sess(strm);
1468 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1469
1470 value = unique_id.ptr;
1471 value_len = unique_id.len;
1472
1473 if (value_len >= 0) {
1474 if ((buf_len - ret) < sizeof(struct tlv))
1475 return 0;
1476 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1477 }
1478 }
1479
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001480#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001481 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001482 struct tlv_ssl *tlv;
1483 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001484
David Safb76832014-05-08 23:42:08 -04001485 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1486 return 0;
1487 tlv = (struct tlv_ssl *)&buf[ret];
1488 memset(tlv, 0, sizeof(struct tlv_ssl));
1489 ssl_tlv_len += sizeof(struct tlv_ssl);
1490 tlv->tlv.type = PP2_TYPE_SSL;
1491 if (ssl_sock_is_ssl(remote)) {
1492 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001493 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001494 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001495 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001496 }
Dave McCowan328fb582014-07-30 10:39:13 -04001497 if (ssl_sock_get_cert_used_sess(remote)) {
1498 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001499 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001500 if (ssl_sock_get_cert_used_conn(remote))
1501 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001502 }
1503 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001504 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001505 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001506 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1507 cn_trash->data,
1508 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001509 }
1510 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001511 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001512 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001513 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001514 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1515 pkey_trash->data,
1516 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001517 }
1518 }
1519 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1520 value = ssl_sock_get_cert_sig(remote);
1521 if (value) {
1522 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1523 }
1524 }
1525 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1526 value = ssl_sock_get_cipher_name(remote);
1527 if (value) {
1528 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1529 }
1530 }
David Safb76832014-05-08 23:42:08 -04001531 }
1532 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1533 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1534 ret += ssl_tlv_len;
1535 }
1536#endif
1537
Willy Tarreaue5733232019-05-22 19:24:06 +02001538#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001539 if (remote && (remote->proxy_netns)) {
1540 if ((buf_len - ret) < sizeof(struct tlv))
1541 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001542 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001543 }
1544#endif
1545
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001546 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001547
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001548 if (tlv_crc32c_p) {
1549 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1550 }
1551
David Safb76832014-05-08 23:42:08 -04001552 return ret;
1553}
Emeric Brun4f603012017-01-05 15:11:44 +01001554
Willy Tarreau119e50e2020-05-22 13:53:29 +02001555/* returns 0 on success */
1556static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
1557 struct proxy *defpx, const char *file, int line,
1558 char **err)
1559{
1560 if (too_many_args(0, args, err, NULL))
1561 return -1;
1562 pp2_never_send_local = 1;
1563 return 0;
1564}
1565
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001566/* return the major HTTP version as 1 or 2 depending on how the request arrived
1567 * before being processed.
1568 */
1569static int
1570smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1571{
Jérôme Magnin86577422018-12-07 09:03:11 +01001572 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1573 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001574
1575 smp->data.type = SMP_T_SINT;
1576 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1577 return 1;
1578}
1579
Emeric Brun4f603012017-01-05 15:11:44 +01001580/* fetch if the received connection used a PROXY protocol header */
1581int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1582{
1583 struct connection *conn;
1584
1585 conn = objt_conn(smp->sess->origin);
1586 if (!conn)
1587 return 0;
1588
Willy Tarreau911db9b2020-01-23 16:27:54 +01001589 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001590 smp->flags |= SMP_F_MAY_CHANGE;
1591 return 0;
1592 }
1593
1594 smp->flags = 0;
1595 smp->data.type = SMP_T_BOOL;
1596 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1597
1598 return 1;
1599}
1600
Geoff Simmons7185b782019-08-27 18:31:16 +02001601/* fetch the authority TLV from a PROXY protocol header */
1602int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1603{
1604 struct connection *conn;
1605
1606 conn = objt_conn(smp->sess->origin);
1607 if (!conn)
1608 return 0;
1609
Willy Tarreau911db9b2020-01-23 16:27:54 +01001610 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001611 smp->flags |= SMP_F_MAY_CHANGE;
1612 return 0;
1613 }
1614
1615 if (conn->proxy_authority == NULL)
1616 return 0;
1617
1618 smp->flags = 0;
1619 smp->data.type = SMP_T_STR;
1620 smp->data.u.str.area = conn->proxy_authority;
1621 smp->data.u.str.data = conn->proxy_authority_len;
1622
1623 return 1;
1624}
1625
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001626/* fetch the unique ID TLV from a PROXY protocol header */
1627int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1628{
1629 struct connection *conn;
1630
1631 conn = objt_conn(smp->sess->origin);
1632 if (!conn)
1633 return 0;
1634
1635 if (conn->flags & CO_FL_WAIT_XPRT) {
1636 smp->flags |= SMP_F_MAY_CHANGE;
1637 return 0;
1638 }
1639
1640 if (!isttest(conn->proxy_unique_id))
1641 return 0;
1642
1643 smp->flags = 0;
1644 smp->data.type = SMP_T_STR;
1645 smp->data.u.str.area = conn->proxy_unique_id.ptr;
1646 smp->data.u.str.data = conn->proxy_unique_id.len;
1647
1648 return 1;
1649}
1650
Emeric Brun4f603012017-01-05 15:11:44 +01001651/* Note: must not be declared <const> as its list will be overwritten.
1652 * Note: fetches that may return multiple types must be declared as the lowest
1653 * common denominator, the type that can be casted into all other ones. For
1654 * instance v4/v6 must be declared v4.
1655 */
1656static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001657 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Jérôme Magnin86577422018-12-07 09:03:11 +01001658 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001659 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001660 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001661 { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001662 { /* END */ },
1663}};
1664
Willy Tarreau0108d902018-11-25 19:14:37 +01001665INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02001666
1667static struct cfg_kw_list cfg_kws = {ILH, {
1668 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
1669 { /* END */ },
1670}};
1671
1672INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);