blob: f9741ef5545b55d586c2e6f0ad57152cb3f1a0cb [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 Tarreau762d7a52020-06-04 11:23:07 +020018#include <haproxy/frontend.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020019#include <haproxy/log-t.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020020#include <haproxy/namespace.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020021#include <haproxy/hash.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020022#include <haproxy/net_helper.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020023
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020024#include <haproxy/fd.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020025#include <haproxy/proto_tcp.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020026#include <haproxy/sample.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020027#include <haproxy/ssl_sock.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020029
Alexander Liu2a54bb72019-05-22 19:44:48 +080030
Willy Tarreau8ceae722018-11-26 11:58:30 +010031DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
32DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
Willy Tarreauff5d57b2019-07-17 18:37:02 +020033DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
Geoff Simmons7185b782019-08-27 18:31:16 +020034DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010035
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 Tarreau6f95f6e2020-03-04 18:33:19 +0100137 else if (tasks_run_queue_cur >= 16*global.tune.runqueue_depth) {
138 /* In order to save syscalls especially with epoll, we
139 * prefer *not* to disable receiving and instead let
140 * the handler do its job. But if the run queue becomes
141 * high, the excess of events may cause extra wakeups
142 * and in this case we'd rather flow-control ourselves.
143 */
144 fd_stop_recv(fd);
145 }
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200146 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200147
Willy Tarreau2c6be842012-07-06 17:12:34 +0200148 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100149 /* If we don't yet have a mux, that means we were waiting for
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500150 * information to create one, typically from the ALPN. If we're
Olivier Houchard477902b2020-01-22 18:08:48 +0100151 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200152 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100153 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100154 if (conn_create_mux(conn) < 0)
155 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200156
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100157 /* The wake callback is normally used to notify the data layer about
158 * data layer activity (successful send/recv), connection establishment,
159 * shutdown and fatal errors. We need to consider the following
160 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100161 * - change among the CO_FL_NOTIFY_DONE flags :
162 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100163 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
164 * end of handshake and transition to CONNECTED
165 * - raise of CONNECTED with HANDSHAKE down
166 * - end of HANDSHAKE with CONNECTED set
167 * - regular data layer activity
168 *
169 * Note that the wake callback is allowed to release the connection and
170 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200171 */
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100172 if ((need_wake || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
Willy Tarreau911db9b2020-01-23 16:27:54 +0100173 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200174 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200175 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200176
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200177 /* commit polling changes */
178 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200179 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200180}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200181
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100182/* This is the callback which is set when a connection establishment is pending
183 * and we have nothing to send. It may update the FD polling status to indicate
184 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
185 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
186 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
187 * leaves the error code in errno.
188 */
189int conn_fd_check(struct connection *conn)
190{
191 struct sockaddr_storage *addr;
192 int fd = conn->handle.fd;
193
194 if (conn->flags & CO_FL_ERROR)
195 return 0;
196
197 if (!conn_ctrl_ready(conn))
198 return 0;
199
200 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
201 return 1; /* strange we were called while ready */
202
203 if (!fd_send_ready(fd))
204 return 0;
205
206 /* Here we have 2 cases :
207 * - modern pollers, able to report ERR/HUP. If these ones return any
208 * of these flags then it's likely a failure, otherwise it possibly
209 * is a success (i.e. there may have been data received just before
210 * the error was reported).
211 * - select, which doesn't report these and with which it's always
212 * necessary either to try connect() again or to check for SO_ERROR.
213 * In order to simplify everything, we double-check using connect() as
214 * soon as we meet either of these delicate situations. Note that
215 * SO_ERROR would clear the error after reporting it!
216 */
217 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
218 /* modern poller, able to report ERR/HUP */
219 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
220 goto done;
221 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
222 goto done;
223 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
224 goto wait;
225 /* error present, fall through common error check path */
226 }
227
228 /* Use connect() to check the state of the socket. This has the double
229 * advantage of *not* clearing the error (so that health checks can
230 * still use getsockopt(SO_ERROR)) and giving us the following info :
231 * - error
232 * - connecting (EALREADY, EINPROGRESS)
233 * - connected (EISCONN, 0)
234 */
235 addr = conn->dst;
236 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
237 addr = &objt_server(conn->target)->socks4_addr;
238
239 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
240 if (errno == EALREADY || errno == EINPROGRESS)
241 goto wait;
242
243 if (errno && errno != EISCONN)
244 goto out_error;
245 }
246
247 done:
248 /* The FD is ready now, we'll mark the connection as complete and
249 * forward the event to the transport layer which will notify the
250 * data layer.
251 */
252 conn->flags &= ~CO_FL_WAIT_L4_CONN;
253 fd_may_send(fd);
254 fd_cond_recv(fd);
255 errno = 0; // make health checks happy
256 return 1;
257
258 out_error:
259 /* Write error on the file descriptor. Report it to the connection
260 * and disable polling on this FD.
261 */
262 fdtab[fd].linger_risk = 0;
263 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau5d4d1802020-02-21 09:58:29 +0100264 conn_stop_polling(conn);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100265 return 0;
266
267 wait:
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100268 fd_cant_send(fd);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100269 fd_want_send(fd);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100270 return 0;
271}
272
Willy Tarreauff3e6482015-03-12 23:56:52 +0100273/* Send a message over an established connection. It makes use of send() and
274 * returns the same return code and errno. If the socket layer is not ready yet
275 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
276 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
277 * EMSGSIZE if called with a zero length message. The purpose is to simplify
278 * some rare attempts to directly write on the socket from above the connection
279 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
280 * It automatically retries on EINTR. Other errors cause the connection to be
281 * marked as in error state. It takes similar arguments as send() except the
282 * first one which is the connection instead of the file descriptor. Note,
283 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
284 */
285int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
286{
287 int ret;
288
289 ret = -1;
290 errno = ENOTSOCK;
291
292 if (conn->flags & CO_FL_SOCK_WR_SH)
293 goto fail;
294
295 if (!conn_ctrl_ready(conn))
296 goto fail;
297
298 errno = EMSGSIZE;
299 if (!len)
300 goto fail;
301
Willy Tarreau585744b2017-08-24 14:31:19 +0200302 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100303 goto wait;
304
305 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200306 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100307 } while (ret < 0 && errno == EINTR);
308
309
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200310 if (ret > 0) {
311 if (conn->flags & CO_FL_WAIT_L4_CONN) {
312 conn->flags &= ~CO_FL_WAIT_L4_CONN;
313 fd_may_send(conn->handle.fd);
314 fd_cond_recv(conn->handle.fd);
315 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100316 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200317 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100318
319 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
320 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200321 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100322 return 0;
323 }
324 fail:
325 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
326 return ret;
327}
328
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100329/* Called from the upper layer, to subscribe <es> to events <event_type>. The
330 * event subscriber <es> is not allowed to change from a previous call as long
331 * as at least one event is still subscribed. The <event_type> must only be a
332 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
333 */
334int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200335{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100336 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100337 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100338
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100339 es->events &= ~event_type;
340 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100341 conn->subs = NULL;
342
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100343 if (conn_ctrl_ready(conn)) {
344 if (event_type & SUB_RETRY_RECV)
345 fd_stop_recv(conn->handle.fd);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100346
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100347 if (event_type & SUB_RETRY_SEND)
348 fd_stop_send(conn->handle.fd);
349 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200350 return 0;
351}
352
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100353/* Called from the upper layer, to subscribe <es> to events <event_type>.
354 * The <es> struct is not allowed to differ from the one passed during a
355 * previous call to subscribe(). If the FD is ready, the wait_event is
356 * immediately woken up and the subcription is cancelled. It always
357 * returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100358 */
359int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200360{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100361 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100362 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100363
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100364 if (conn->subs && (conn->subs->events & event_type) == event_type)
365 return 0;
366
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100367 conn->subs = es;
368 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100369
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100370 if (conn_ctrl_ready(conn)) {
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100371 if (event_type & SUB_RETRY_RECV) {
372 if (fd_recv_ready(conn->handle.fd)) {
373 tasklet_wakeup(es->tasklet);
374 es->events &= ~SUB_RETRY_RECV;
375 if (!es->events)
376 conn->subs = NULL;
377 }
378 else
379 fd_want_recv(conn->handle.fd);
380 }
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100381
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100382 if (event_type & SUB_RETRY_SEND) {
383 if (fd_send_ready(conn->handle.fd)) {
384 tasklet_wakeup(es->tasklet);
385 es->events &= ~SUB_RETRY_SEND;
386 if (!es->events)
387 conn->subs = NULL;
388 }
389 else
390 fd_want_send(conn->handle.fd);
391 }
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100392 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200393 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200394}
395
Willy Tarreaud85c4852015-03-13 00:40:28 +0100396/* Drains possibly pending incoming data on the file descriptor attached to the
397 * connection and update the connection's flags accordingly. This is used to
398 * know whether we need to disable lingering on close. Returns non-zero if it
399 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
400 * flag may also be updated if the incoming shutdown was reported by the drain()
401 * function.
402 */
403int conn_sock_drain(struct connection *conn)
404{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200405 int turns = 2;
406 int len;
407
Willy Tarreaud85c4852015-03-13 00:40:28 +0100408 if (!conn_ctrl_ready(conn))
409 return 1;
410
411 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
412 return 1;
413
Willy Tarreaue215bba2018-08-24 14:31:53 +0200414 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
415 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100416
Willy Tarreaue215bba2018-08-24 14:31:53 +0200417 if (!fd_recv_ready(conn->handle.fd))
418 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100419
Willy Tarreaue215bba2018-08-24 14:31:53 +0200420 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200421 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100422 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200423 goto shut;
424 }
425
426 /* no drain function defined, use the generic one */
427
428 while (turns) {
429#ifdef MSG_TRUNC_CLEARS_INPUT
430 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
431 if (len == -1 && errno == EFAULT)
432#endif
433 len = recv(conn->handle.fd, trash.area, trash.size,
434 MSG_DONTWAIT | MSG_NOSIGNAL);
435
436 if (len == 0)
437 goto shut;
438
439 if (len < 0) {
440 if (errno == EAGAIN) {
441 /* connection not closed yet */
442 fd_cant_recv(conn->handle.fd);
443 break;
444 }
445 if (errno == EINTR) /* oops, try again */
446 continue;
447 /* other errors indicate a dead connection, fine. */
448 goto shut;
449 }
450 /* OK we read some data, let's try again once */
451 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100452 }
453
Willy Tarreaue215bba2018-08-24 14:31:53 +0200454 /* some data are still present, give up */
455 return 0;
456
457 shut:
458 /* we're certain the connection was shut down */
459 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100460 conn->flags |= CO_FL_SOCK_RD_SH;
461 return 1;
462}
463
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100464/*
465 * Get data length from tlv
466 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100467static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100468{
469 return (src->length_hi << 8) | src->length_lo;
470}
471
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200472/* This handshake handler waits a PROXY protocol header at the beginning of the
473 * raw data stream. The header looks like this :
474 *
475 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
476 *
477 * There must be exactly one space between each field. Fields are :
478 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
479 * - SRC3 : layer 3 (eg: IP) source address in standard text form
480 * - DST3 : layer 3 (eg: IP) destination address in standard text form
481 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
482 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
483 *
484 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
485 *
486 * The header line is small and in all cases smaller than the smallest normal
487 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
488 * can safely use MSG_PEEK and avoid buffering.
489 *
490 * Once the data is fetched, the values are set in the connection's address
491 * fields, and data are removed from the socket's buffer. The function returns
492 * zero if it needs to wait for more data or if it fails, or 1 if it completed
493 * and removed itself.
494 */
495int conn_recv_proxy(struct connection *conn, int flag)
496{
497 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200498 struct proxy_hdr_v2 *hdr_v2;
499 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100500 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100501 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200502 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503
Willy Tarreau3c728722014-01-23 13:50:42 +0100504 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200505 goto fail;
506
Willy Tarreauca79f592019-07-17 19:04:47 +0200507 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
508 goto fail;
509
Willy Tarreau585744b2017-08-24 14:31:19 +0200510 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200511 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100512
Willy Tarreau157788c2020-02-11 10:08:05 +0100513 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200514 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
515 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200516 if (errno == EINTR)
517 continue;
518 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200519 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200520 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200521 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100522 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200523 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200524 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100525 break;
526 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200527
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200528 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100529 /* client shutdown */
530 conn->err_code = CO_ER_PRX_EMPTY;
531 goto fail;
532 }
533
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100534 conn->flags &= ~CO_FL_WAIT_L4_CONN;
535
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537 goto missing;
538
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200539 line = trash.area;
540 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200541
542 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200543 if (strncmp(line, "PROXY ", 6) != 0)
544 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200545
546 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200548 goto missing;
549
David CARLIER42ff05e2016-03-24 09:22:36 +0000550 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200551 u32 src3, dst3, sport, dport;
552
553 line += 5;
554
555 src3 = inetaddr_host_lim_ret(line, end, &line);
556 if (line == end)
557 goto missing;
558 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100559 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200560
561 dst3 = inetaddr_host_lim_ret(line, end, &line);
562 if (line == end)
563 goto missing;
564 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100565 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200566
567 sport = read_uint((const char **)&line, end);
568 if (line == end)
569 goto missing;
570 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100571 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200572
573 dport = read_uint((const char **)&line, end);
574 if (line > end - 2)
575 goto missing;
576 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100577 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200578 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100579 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200580
581 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200582 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
583 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
584 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200585
Willy Tarreau226572f2019-07-17 14:46:00 +0200586 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
587 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
588 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200589 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
590 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000591 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200592 u32 sport, dport;
593 char *src_s;
594 char *dst_s, *sport_s, *dport_s;
595 struct in6_addr src3, dst3;
596
597 line += 5;
598
599 src_s = line;
600 dst_s = sport_s = dport_s = NULL;
601 while (1) {
602 if (line > end - 2) {
603 goto missing;
604 }
605 else if (*line == '\r') {
606 *line = 0;
607 line++;
608 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100609 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200610 break;
611 }
612
613 if (*line == ' ') {
614 *line = 0;
615 if (!dst_s)
616 dst_s = line + 1;
617 else if (!sport_s)
618 sport_s = line + 1;
619 else if (!dport_s)
620 dport_s = line + 1;
621 }
622 line++;
623 }
624
625 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100626 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627
628 sport = read_uint((const char **)&sport_s,dport_s - 1);
629 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100630 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200631
632 dport = read_uint((const char **)&dport_s,line - 2);
633 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100634 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200635
636 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100637 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200638
639 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100640 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200641
642 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200643 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
644 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
645 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200646
Willy Tarreau226572f2019-07-17 14:46:00 +0200647 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
648 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
649 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200650 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
651 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200652 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
653 /* This can be a UNIX socket forwarded by an haproxy upstream */
654 line += 9;
655 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200656 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200657 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100658 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200659 goto fail;
660 }
661
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200663 goto eat_header;
664
665 not_v1:
666 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200667 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200668 goto missing;
669
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200671
672 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
673 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
674 conn->err_code = CO_ER_PRX_NOT_HDR;
675 goto fail;
676 }
677
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100678 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
679 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200680 goto missing;
681
682 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
683 case 0x01: /* PROXY command */
684 switch (hdr_v2->fam) {
685 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100686 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
687 goto bad_header;
688
Willy Tarreau226572f2019-07-17 14:46:00 +0200689 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
690 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
691 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
692 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
693 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
694 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200695 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200696 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200697 break;
698 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100699 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
700 goto bad_header;
701
Willy Tarreau226572f2019-07-17 14:46:00 +0200702 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
703 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
704 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
705 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
706 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
707 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200708 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200709 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200710 break;
711 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100712
713 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100714 while (tlv_offset < total_v2_len) {
715 struct tlv *tlv_packet;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100716 size_t tlv_len;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100717
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100718 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
719 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
720 goto bad_header;
721
722 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
723 tlv_len = get_tlv_length(tlv_packet);
724 tlv_offset += tlv_len + TLV_HEADER_SIZE;
725
726 /* Verify that the TLV length does not exceed the total PROXYv2 length */
727 if (tlv_offset > total_v2_len)
728 goto bad_header;
729
730 switch (tlv_packet->type) {
731 case PP2_TYPE_CRC32C: {
732 uint32_t n_crc32c;
733
734 /* Verify that this TLV is exactly 4 bytes long */
735 if (tlv_len != 4)
736 goto bad_header;
737
738 n_crc32c = read_n32(tlv_packet->value);
739 write_n32(tlv_packet->value, 0); // compute with CRC==0
740
741 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
742 goto bad_header;
743 break;
744 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200745#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100746 case PP2_TYPE_NETNS: {
747 const struct netns_entry *ns;
748
749 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
750 if (ns)
751 conn->proxy_netns = ns;
752 break;
753 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100754#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100755 case PP2_TYPE_AUTHORITY: {
756 if (tlv_len > PP2_AUTHORITY_MAX)
757 goto bad_header;
758 conn->proxy_authority = pool_alloc(pool_head_authority);
759 if (conn->proxy_authority == NULL)
760 goto fail;
761 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
762 conn->proxy_authority_len = tlv_len;
763 break;
764 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100765 case PP2_TYPE_UNIQUE_ID: {
766 const struct ist tlv = ist2((const char *)tlv_packet->value, tlv_len);
767
768 if (tlv.len > UNIQUEID_LEN)
769 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +0100770 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100771 if (!isttest(conn->proxy_unique_id))
772 goto fail;
773 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
774 /* This is technically unreachable, because we verified above
775 * that the TLV value fits.
776 */
777 goto fail;
778 }
779 break;
780 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100781 default:
782 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100783 }
784 }
785
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100786 /* Verify that the PROXYv2 header ends at a TLV boundary.
787 * This is technically unreachable, because the TLV parsing already
788 * verifies that a TLV does not exceed the total length and also
789 * that there is space for a TLV header.
790 */
791 if (tlv_offset != total_v2_len)
792 goto bad_header;
793
Willy Tarreau77992672014-06-14 11:06:17 +0200794 /* unsupported protocol, keep local connection address */
795 break;
796 case 0x00: /* LOCAL command */
797 /* keep local connection address for LOCAL */
798 break;
799 default:
800 goto bad_header; /* not a supported command */
801 }
802
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100803 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +0200804 goto eat_header;
805
806 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200807 /* remove the PROXY line from the request. For this we re-read the
808 * exact line at once. If we don't get the exact same result, we
809 * fail.
810 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100811 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +0100812 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
813
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200814 if (len2 < 0 && errno == EINTR)
815 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200816 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100817 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100818 break;
819 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200820
821 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100822 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200823 return 1;
824
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200825 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200826 return 0;
827
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200828 missing:
829 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
830 * we have not read anything. Otherwise we need to fail because we won't
831 * be able to poll anymore.
832 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100833 conn->err_code = CO_ER_PRX_TRUNCATED;
834 goto fail;
835
836 bad_header:
837 /* This is not a valid proxy protocol header */
838 conn->err_code = CO_ER_PRX_BAD_HDR;
839 goto fail;
840
841 recv_abort:
842 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100843 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100844 goto fail;
845
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200846 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200847 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200848 return 0;
849}
850
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100851/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000852 * at the beginning of the raw data stream. The header format is
853 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100854 *
855 * This line MUST be at the beginning of the buffer and MUST NOT be
856 * fragmented.
857 *
858 * The header line is small and in all cases smaller than the smallest normal
859 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
860 * can safely use MSG_PEEK and avoid buffering.
861 *
862 * Once the data is fetched, the values are set in the connection's address
863 * fields, and data are removed from the socket's buffer. The function returns
864 * zero if it needs to wait for more data or if it fails, or 1 if it completed
865 * and removed itself.
866 */
867int conn_recv_netscaler_cip(struct connection *conn, int flag)
868{
869 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000870 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100871 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200872 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100873
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100874 if (!conn_ctrl_ready(conn))
875 goto fail;
876
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100877 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
878 goto fail;
879
Willy Tarreau585744b2017-08-24 14:31:19 +0200880 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200881 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100882
Willy Tarreau157788c2020-02-11 10:08:05 +0100883 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200884 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
885 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100886 if (errno == EINTR)
887 continue;
888 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200889 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200890 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100891 }
892 goto recv_abort;
893 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200894 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100895 break;
896 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100897
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100898 conn->flags &= ~CO_FL_WAIT_L4_CONN;
899
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200900 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100901 /* client shutdown */
902 conn->err_code = CO_ER_CIP_EMPTY;
903 goto fail;
904 }
905
906 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000907 * CIP magic, header length or
908 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200909 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100910 goto missing;
911
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200912 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100913
914 /* Decode a possible NetScaler Client IP request, fail early if
915 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100916 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100917 goto bad_magic;
918
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000919 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200920 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100921 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000922 line += 8;
923 }
924 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200925 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100926 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000927 line += 12;
928 }
929 /* Unknown CIP protocol */
930 else {
931 conn->err_code = CO_ER_CIP_BAD_PROTO;
932 goto fail;
933 }
934
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100935 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000936 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200937 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100938 goto missing;
939
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100940 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100941 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100942
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100943 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100944 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100945 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100946
947 hdr_ip4 = (struct ip *)line;
948
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200949 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100950 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000951 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100952 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000953 }
954 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100955 /* The protocol does not include a TCP header */
956 conn->err_code = CO_ER_CIP_BAD_PROTO;
957 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000958 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100959
David Carlier3015a2e2016-07-04 22:51:33 +0100960 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100961
962 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200963 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
964 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
965 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100966
Willy Tarreau226572f2019-07-17 14:46:00 +0200967 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
968 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
969 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100970
971 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
972 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100973 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100974 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100975 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100976
977 hdr_ip6 = (struct ip6_hdr *)line;
978
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200979 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100980 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000981 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100982 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000983 }
984 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100985 /* The protocol does not include a TCP header */
986 conn->err_code = CO_ER_CIP_BAD_PROTO;
987 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000988 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100989
David Carlier3015a2e2016-07-04 22:51:33 +0100990 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100991
992 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200993 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
994 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
995 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100996
Willy Tarreau226572f2019-07-17 14:46:00 +0200997 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
998 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
999 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001000
1001 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
1002 }
1003 else {
1004 /* The protocol does not match something known (IPv4/IPv6) */
1005 conn->err_code = CO_ER_CIP_BAD_PROTO;
1006 goto fail;
1007 }
1008
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001009 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001010 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001011
1012 /* remove the NetScaler Client IP header from the request. For this
1013 * we re-read the exact line at once. If we don't get the exact same
1014 * result, we fail.
1015 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001016 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001017 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001018 if (len2 < 0 && errno == EINTR)
1019 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001020 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001021 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001022 break;
1023 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001024
1025 conn->flags &= ~flag;
1026 return 1;
1027
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001028 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001029 return 0;
1030
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001031 missing:
1032 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1033 * we have not read anything. Otherwise we need to fail because we won't
1034 * be able to poll anymore.
1035 */
1036 conn->err_code = CO_ER_CIP_TRUNCATED;
1037 goto fail;
1038
1039 bad_magic:
1040 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1041 goto fail;
1042
1043 recv_abort:
1044 conn->err_code = CO_ER_CIP_ABORT;
1045 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1046 goto fail;
1047
1048 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001049 conn->flags |= CO_FL_ERROR;
1050 return 0;
1051}
1052
Alexander Liu2a54bb72019-05-22 19:44:48 +08001053
1054int conn_send_socks4_proxy_request(struct connection *conn)
1055{
1056 struct socks4_request req_line;
1057
Alexander Liu2a54bb72019-05-22 19:44:48 +08001058 if (!conn_ctrl_ready(conn))
1059 goto out_error;
1060
Willy Tarreau226572f2019-07-17 14:46:00 +02001061 if (!conn_get_dst(conn))
1062 goto out_error;
1063
Alexander Liu2a54bb72019-05-22 19:44:48 +08001064 req_line.version = 0x04;
1065 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001066 req_line.port = get_net_port(conn->dst);
1067 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001068 memcpy(req_line.user_id, "HAProxy\0", 8);
1069
1070 if (conn->send_proxy_ofs > 0) {
1071 /*
1072 * This is the first call to send the request
1073 */
1074 conn->send_proxy_ofs = -(int)sizeof(req_line);
1075 }
1076
1077 if (conn->send_proxy_ofs < 0) {
1078 int ret = 0;
1079
1080 /* we are sending the socks4_req_line here. If the data layer
1081 * has a pending write, we'll also set MSG_MORE.
1082 */
1083 ret = conn_sock_send(
1084 conn,
1085 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1086 -conn->send_proxy_ofs,
Willy Tarreau19bc2012020-02-21 08:46:19 +01001087 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001088
1089 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1090 conn->handle.fd, -conn->send_proxy_ofs, ret);
1091
1092 if (ret < 0) {
1093 goto out_error;
1094 }
1095
1096 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1097 if (conn->send_proxy_ofs != 0) {
1098 goto out_wait;
1099 }
1100 }
1101
1102 /* OK we've the whole request sent */
1103 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001104
1105 /* The connection is ready now, simply return and let the connection
1106 * handler notify upper layers if needed.
1107 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001108 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001109
1110 if (conn->flags & CO_FL_SEND_PROXY) {
1111 /*
1112 * Get the send_proxy_ofs ready for the send_proxy due to we are
1113 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1114 * before sending PROXY Protocol.
1115 */
1116 conn->send_proxy_ofs = 1;
1117 }
1118 return 1;
1119
1120 out_error:
1121 /* Write error on the file descriptor */
1122 conn->flags |= CO_FL_ERROR;
1123 if (conn->err_code == CO_ER_NONE) {
1124 conn->err_code = CO_ER_SOCKS4_SEND;
1125 }
1126 return 0;
1127
1128 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001129 return 0;
1130}
1131
1132int conn_recv_socks4_proxy_response(struct connection *conn)
1133{
1134 char line[SOCKS4_HS_RSP_LEN];
1135 int ret;
1136
Alexander Liu2a54bb72019-05-22 19:44:48 +08001137 if (!conn_ctrl_ready(conn))
1138 goto fail;
1139
1140 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001141 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001142
Willy Tarreau157788c2020-02-11 10:08:05 +01001143 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001144 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1145 * Try to peek into it, before all 8 bytes ready.
1146 */
1147 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1148
1149 if (ret == 0) {
1150 /* the socket has been closed or shutdown for send */
1151 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1152 conn->handle.fd, ret, errno);
1153 if (conn->err_code == CO_ER_NONE) {
1154 conn->err_code = CO_ER_SOCKS4_RECV;
1155 }
1156 goto fail;
1157 }
1158
1159 if (ret > 0) {
1160 if (ret == SOCKS4_HS_RSP_LEN) {
1161 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1162 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1163 }else{
1164 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]);
1165 }
1166 } else {
1167 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1168 }
1169
1170 if (ret < 0) {
1171 if (errno == EINTR) {
1172 continue;
1173 }
1174 if (errno == EAGAIN) {
1175 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001176 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001177 }
1178 goto recv_abort;
1179 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001180 break;
1181 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001182
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001183 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1184
Alexander Liu2a54bb72019-05-22 19:44:48 +08001185 if (ret < SOCKS4_HS_RSP_LEN) {
1186 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1187 * we are not able to read enough data.
1188 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001189 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001190 }
1191
1192 /*
1193 * Base on the SOCSK4 protocol:
1194 *
1195 * +----+----+----+----+----+----+----+----+
1196 * | VN | CD | DSTPORT | DSTIP |
1197 * +----+----+----+----+----+----+----+----+
1198 * # of bytes: 1 1 2 4
1199 * VN is the version of the reply code and should be 0. CD is the result
1200 * code with one of the following values:
1201 * 90: request granted
1202 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001203 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001204 * 93: request rejected because the client program and identd report different user-ids
1205 * The remaining fields are ignored.
1206 */
1207 if (line[1] != 90) {
1208 conn->flags &= ~CO_FL_SOCKS4_RECV;
1209
1210 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1211 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1212 if (conn->err_code == CO_ER_NONE) {
1213 conn->err_code = CO_ER_SOCKS4_DENY;
1214 }
1215 goto fail;
1216 }
1217
1218 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001219 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001220 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1221 if (ret < 0 && errno == EINTR) {
1222 continue;
1223 }
1224 if (ret != SOCKS4_HS_RSP_LEN) {
1225 if (conn->err_code == CO_ER_NONE) {
1226 conn->err_code = CO_ER_SOCKS4_RECV;
1227 }
1228 goto fail;
1229 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001230 break;
1231 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001232
1233 conn->flags &= ~CO_FL_SOCKS4_RECV;
1234 return 1;
1235
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001236 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001237 return 0;
1238
Alexander Liu2a54bb72019-05-22 19:44:48 +08001239 recv_abort:
1240 if (conn->err_code == CO_ER_NONE) {
1241 conn->err_code = CO_ER_SOCKS4_ABORT;
1242 }
1243 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1244 goto fail;
1245
1246 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001247 conn->flags |= CO_FL_ERROR;
1248 return 0;
1249}
1250
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001251/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001252int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001253{
1254 int ret = 0;
1255
1256 if (srv && (srv->pp_opts & SRV_PP_V2)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001257 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
David Safb76832014-05-08 23:42:08 -04001258 }
1259 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001260 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1261 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001262 else
1263 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1264 }
1265
1266 return ret;
1267}
1268
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001269/* Makes a PROXY protocol line from the two addresses. The output is sent to
1270 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1271 * It returns the number of bytes composing this line (including the trailing
1272 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001273 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1274 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001275 */
David Safb76832014-05-08 23:42:08 -04001276int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001277{
1278 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001279 char * protocol;
1280 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1281 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1282 in_port_t src_port;
1283 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001284
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001285 if ( !src
1286 || !dst
1287 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1288 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1289 /* unknown family combination */
1290 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001291 if (ret >= buf_len)
1292 return 0;
1293
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001294 return ret;
1295 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001296
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001297 /* IPv4 for both src and dst */
1298 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1299 protocol = "TCP4";
1300 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001301 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001302 src_port = ((struct sockaddr_in *)src)->sin_port;
1303 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001304 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001305 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001306 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001307 /* IPv6 for at least one of src and dst */
1308 else {
1309 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001310
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001311 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001312
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001313 if (src->ss_family == AF_INET) {
1314 /* Convert src to IPv6 */
1315 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1316 src_port = ((struct sockaddr_in *)src)->sin_port;
1317 }
1318 else {
1319 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1320 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1321 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001322
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001323 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001324 return 0;
1325
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001326 if (dst->ss_family == AF_INET) {
1327 /* Convert dst to IPv6 */
1328 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1329 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1330 }
1331 else {
1332 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1333 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1334 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001335
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001336 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001337 return 0;
1338 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001339
1340 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1341 if (ret >= buf_len)
1342 return 0;
1343
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001344 return ret;
1345}
David Safb76832014-05-08 23:42:08 -04001346
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001347static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001348{
1349 struct tlv *tlv;
1350
1351 if (!dest || (length + sizeof(*tlv) > dest_len))
1352 return 0;
1353
1354 tlv = (struct tlv *)dest;
1355
1356 tlv->type = type;
1357 tlv->length_hi = length >> 8;
1358 tlv->length_lo = length & 0x00ff;
1359 memcpy(tlv->value, value, length);
1360 return length + sizeof(*tlv);
1361}
David Safb76832014-05-08 23:42:08 -04001362
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001363/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001364int 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 -04001365{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001366 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001367 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001368 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001369 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001370 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001371 struct sockaddr_storage *src = &null_addr;
1372 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001373 const char *value;
1374 int value_len;
David Safb76832014-05-08 23:42:08 -04001375
1376 if (buf_len < PP2_HEADER_LEN)
1377 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001378 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001379
Willy Tarreau226572f2019-07-17 14:46:00 +02001380 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1381 src = remote->src;
1382 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001383 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001384
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001385 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1386 if ( !src
1387 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001388 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001389 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1390 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001391 if (buf_len < PP2_HDR_LEN_UNSPEC)
1392 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001393 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1394 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001395 ret = PP2_HDR_LEN_UNSPEC;
1396 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001397 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001398 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001399 /* IPv4 for both src and dst */
1400 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1401 if (buf_len < PP2_HDR_LEN_INET)
1402 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001403 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1404 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1405 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1406 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1407 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1408 ret = PP2_HDR_LEN_INET;
1409 }
1410 /* IPv6 for at least one of src and dst */
1411 else {
1412 struct in6_addr tmp;
1413
1414 if (buf_len < PP2_HDR_LEN_INET6)
1415 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001416 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1417 if (src->ss_family == AF_INET) {
1418 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1419 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1420 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1421 }
1422 else {
1423 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1424 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1425 }
1426 if (dst->ss_family == AF_INET) {
1427 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1428 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001429 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001430 }
1431 else {
1432 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1433 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1434 }
1435
1436 ret = PP2_HDR_LEN_INET6;
1437 }
1438 }
David Safb76832014-05-08 23:42:08 -04001439
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001440 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1441 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001442
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001443 if ((buf_len - ret) < sizeof(struct tlv))
1444 return 0;
1445 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1446 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1447 }
1448
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001449 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001450 if ((buf_len - ret) < sizeof(struct tlv))
1451 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001452 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001453 }
1454
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001455 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001456 value = NULL;
1457 if (remote && remote->proxy_authority) {
1458 value = remote->proxy_authority;
1459 value_len = remote->proxy_authority_len;
1460 }
1461#ifdef USE_OPENSSL
1462 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001463 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001464 value_len = strlen(value);
1465 }
1466#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001467 if (value) {
1468 if ((buf_len - ret) < sizeof(struct tlv))
1469 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001470 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001471 }
1472 }
1473
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001474 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001475 struct session* sess = strm_sess(strm);
1476 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1477
1478 value = unique_id.ptr;
1479 value_len = unique_id.len;
1480
1481 if (value_len >= 0) {
1482 if ((buf_len - ret) < sizeof(struct tlv))
1483 return 0;
1484 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1485 }
1486 }
1487
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001488#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001489 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001490 struct tlv_ssl *tlv;
1491 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001492
David Safb76832014-05-08 23:42:08 -04001493 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1494 return 0;
1495 tlv = (struct tlv_ssl *)&buf[ret];
1496 memset(tlv, 0, sizeof(struct tlv_ssl));
1497 ssl_tlv_len += sizeof(struct tlv_ssl);
1498 tlv->tlv.type = PP2_TYPE_SSL;
1499 if (ssl_sock_is_ssl(remote)) {
1500 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001501 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001502 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001503 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 -04001504 }
Dave McCowan328fb582014-07-30 10:39:13 -04001505 if (ssl_sock_get_cert_used_sess(remote)) {
1506 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001507 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001508 if (ssl_sock_get_cert_used_conn(remote))
1509 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001510 }
1511 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001512 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001513 if (ssl_sock_get_remote_common_name(remote, cn_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_CN,
1515 cn_trash->data,
1516 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001517 }
1518 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001519 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001520 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001521 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001522 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1523 pkey_trash->data,
1524 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001525 }
1526 }
1527 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1528 value = ssl_sock_get_cert_sig(remote);
1529 if (value) {
1530 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1531 }
1532 }
1533 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1534 value = ssl_sock_get_cipher_name(remote);
1535 if (value) {
1536 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1537 }
1538 }
David Safb76832014-05-08 23:42:08 -04001539 }
1540 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1541 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1542 ret += ssl_tlv_len;
1543 }
1544#endif
1545
Willy Tarreaue5733232019-05-22 19:24:06 +02001546#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001547 if (remote && (remote->proxy_netns)) {
1548 if ((buf_len - ret) < sizeof(struct tlv))
1549 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001550 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 +01001551 }
1552#endif
1553
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001554 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001555
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001556 if (tlv_crc32c_p) {
1557 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1558 }
1559
David Safb76832014-05-08 23:42:08 -04001560 return ret;
1561}
Emeric Brun4f603012017-01-05 15:11:44 +01001562
Willy Tarreau119e50e2020-05-22 13:53:29 +02001563/* returns 0 on success */
1564static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
1565 struct proxy *defpx, const char *file, int line,
1566 char **err)
1567{
1568 if (too_many_args(0, args, err, NULL))
1569 return -1;
1570 pp2_never_send_local = 1;
1571 return 0;
1572}
1573
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001574/* return the major HTTP version as 1 or 2 depending on how the request arrived
1575 * before being processed.
1576 */
1577static int
1578smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1579{
Jérôme Magnin86577422018-12-07 09:03:11 +01001580 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1581 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001582
1583 smp->data.type = SMP_T_SINT;
1584 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1585 return 1;
1586}
1587
Emeric Brun4f603012017-01-05 15:11:44 +01001588/* fetch if the received connection used a PROXY protocol header */
1589int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1590{
1591 struct connection *conn;
1592
1593 conn = objt_conn(smp->sess->origin);
1594 if (!conn)
1595 return 0;
1596
Willy Tarreau911db9b2020-01-23 16:27:54 +01001597 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001598 smp->flags |= SMP_F_MAY_CHANGE;
1599 return 0;
1600 }
1601
1602 smp->flags = 0;
1603 smp->data.type = SMP_T_BOOL;
1604 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1605
1606 return 1;
1607}
1608
Geoff Simmons7185b782019-08-27 18:31:16 +02001609/* fetch the authority TLV from a PROXY protocol header */
1610int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1611{
1612 struct connection *conn;
1613
1614 conn = objt_conn(smp->sess->origin);
1615 if (!conn)
1616 return 0;
1617
Willy Tarreau911db9b2020-01-23 16:27:54 +01001618 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001619 smp->flags |= SMP_F_MAY_CHANGE;
1620 return 0;
1621 }
1622
1623 if (conn->proxy_authority == NULL)
1624 return 0;
1625
1626 smp->flags = 0;
1627 smp->data.type = SMP_T_STR;
1628 smp->data.u.str.area = conn->proxy_authority;
1629 smp->data.u.str.data = conn->proxy_authority_len;
1630
1631 return 1;
1632}
1633
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001634/* fetch the unique ID TLV from a PROXY protocol header */
1635int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1636{
1637 struct connection *conn;
1638
1639 conn = objt_conn(smp->sess->origin);
1640 if (!conn)
1641 return 0;
1642
1643 if (conn->flags & CO_FL_WAIT_XPRT) {
1644 smp->flags |= SMP_F_MAY_CHANGE;
1645 return 0;
1646 }
1647
1648 if (!isttest(conn->proxy_unique_id))
1649 return 0;
1650
1651 smp->flags = 0;
1652 smp->data.type = SMP_T_STR;
1653 smp->data.u.str.area = conn->proxy_unique_id.ptr;
1654 smp->data.u.str.data = conn->proxy_unique_id.len;
1655
1656 return 1;
1657}
1658
Emeric Brun4f603012017-01-05 15:11:44 +01001659/* Note: must not be declared <const> as its list will be overwritten.
1660 * Note: fetches that may return multiple types must be declared as the lowest
1661 * common denominator, the type that can be casted into all other ones. For
1662 * instance v4/v6 must be declared v4.
1663 */
1664static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001665 { "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 +01001666 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001667 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001668 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001669 { "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 +01001670 { /* END */ },
1671}};
1672
Willy Tarreau0108d902018-11-25 19:14:37 +01001673INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02001674
1675static struct cfg_kw_list cfg_kws = {ILH, {
1676 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
1677 { /* END */ },
1678}};
1679
1680INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);