blob: 1267416990d3f236058ac1137b527095c3338e53 [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 Tarreau13e14102016-12-22 20:25:26 +010035struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020036
Christopher Faulet32f61c02018-04-10 14:33:41 +020037/* List head of all known muxes for PROTO */
38struct mux_proto_list mux_proto_list = {
39 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020040};
41
Willy Tarreau119e50e2020-05-22 13:53:29 +020042/* disables sending of proxy-protocol-v2's LOCAL command */
43static int pp2_never_send_local;
44
Olivier Houchard477902b2020-01-22 18:08:48 +010045int conn_create_mux(struct connection *conn)
46{
Olivier Houchard477902b2020-01-22 18:08:48 +010047 if (conn_is_back(conn)) {
48 struct server *srv;
49 struct conn_stream *cs = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020050 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010051
52 if (conn->flags & CO_FL_ERROR)
53 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010054
Christopher Faulet14cd3162020-04-16 14:50:06 +020055 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
56 if (conn_install_mux_chk(conn, conn->ctx, conn->owner) < 0)
57 goto fail;
58 }
59 else if (conn_install_mux_be(conn, conn->ctx, conn->owner) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010060 goto fail;
61 srv = objt_server(conn->target);
62 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
63 conn->mux->avail_streams(conn) > 0)
Olivier Houchardf0d4dff2020-03-06 18:12:03 +010064 LIST_ADDQ(&srv->available_conns[tid], mt_list_to_list(&conn->list));
Olivier Houchard477902b2020-01-22 18:08:48 +010065 return 0;
66fail:
67 /* let the upper layer know the connection failed */
68 cs->data_cb->wake(cs);
69 return -1;
70 } else
71 return conn_complete_session(conn);
72
73}
74
Willy Tarreau59f98392012-07-06 14:13:49 +020075/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020076 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020077 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020078void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020079{
Willy Tarreau80184712012-07-06 14:54:49 +020080 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020081 unsigned int flags;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +010082 int need_wake = 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020083
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010084 if (unlikely(!conn)) {
85 activity[tid].conn_dead++;
Willy Tarreau7a798e52016-04-14 11:13:20 +020086 return;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010087 }
Willy Tarreau59f98392012-07-06 14:13:49 +020088
Willy Tarreau7d281492012-12-16 19:19:13 +010089 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010090
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010091 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
92 ((fd_send_ready(fd) && fd_send_active(fd)) ||
93 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
94 /* Still waiting for a connection to establish and nothing was
95 * attempted yet to probe the connection. this will clear the
96 * CO_FL_WAIT_L4_CONN flag on success.
97 */
98 if (!conn_fd_check(conn))
99 goto leave;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100100 need_wake = 1;
Willy Tarreaub2a7ab02019-12-27 10:54:22 +0100101 }
102
Willy Tarreau8081abe2019-11-28 18:08:49 +0100103 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100104 /* force reporting of activity by clearing the previous flags :
105 * we'll have at least ERROR or CONNECTED at the end of an I/O,
106 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200107 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100108 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100109 if (conn->subs && conn->subs->events & SUB_RETRY_SEND) {
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100110 need_wake = 0; // wake will be called after this I/O
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100111 tasklet_wakeup(conn->subs->tasklet);
112 conn->subs->events &= ~SUB_RETRY_SEND;
113 if (!conn->subs->events)
114 conn->subs = NULL;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100115 }
Willy Tarreau667fefd2020-03-04 17:22:10 +0100116 fd_stop_send(fd);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200117 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200118
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200119 /* The data transfer starts here and stops on error and handshakes. Note
120 * that we must absolutely test conn->xprt at each step in case it suddenly
121 * changes due to a quick unexpected close().
122 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100123 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100124 /* force reporting of activity by clearing the previous flags :
125 * we'll have at least ERROR or CONNECTED at the end of an I/O,
126 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200127 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100128 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100129 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100130 need_wake = 0; // wake will be called after this I/O
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100131 tasklet_wakeup(conn->subs->tasklet);
132 conn->subs->events &= ~SUB_RETRY_RECV;
133 if (!conn->subs->events)
134 conn->subs = NULL;
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100135 }
Willy Tarreau4cabfc12020-06-17 16:26:22 +0200136 fd_stop_recv(fd);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200137 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200138
Willy Tarreau2c6be842012-07-06 17:12:34 +0200139 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100140 /* If we don't yet have a mux, that means we were waiting for
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500141 * information to create one, typically from the ALPN. If we're
Olivier Houchard477902b2020-01-22 18:08:48 +0100142 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200143 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100144 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100145 if (conn_create_mux(conn) < 0)
146 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200147
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100148 /* The wake callback is normally used to notify the data layer about
149 * data layer activity (successful send/recv), connection establishment,
150 * shutdown and fatal errors. We need to consider the following
151 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100152 * - change among the CO_FL_NOTIFY_DONE flags :
153 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100154 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
155 * end of handshake and transition to CONNECTED
156 * - raise of CONNECTED with HANDSHAKE down
157 * - end of HANDSHAKE with CONNECTED set
158 * - regular data layer activity
159 *
160 * Note that the wake callback is allowed to release the connection and
161 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200162 */
Willy Tarreau8de5c4f2020-03-04 17:45:21 +0100163 if ((need_wake || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
Willy Tarreau911db9b2020-01-23 16:27:54 +0100164 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200165 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200166 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200167
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200168 /* commit polling changes */
169 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200170 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200171}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200172
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100173/* This is the callback which is set when a connection establishment is pending
174 * and we have nothing to send. It may update the FD polling status to indicate
175 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
176 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
177 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
178 * leaves the error code in errno.
179 */
180int conn_fd_check(struct connection *conn)
181{
182 struct sockaddr_storage *addr;
183 int fd = conn->handle.fd;
184
185 if (conn->flags & CO_FL_ERROR)
186 return 0;
187
188 if (!conn_ctrl_ready(conn))
189 return 0;
190
191 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
192 return 1; /* strange we were called while ready */
193
194 if (!fd_send_ready(fd))
195 return 0;
196
197 /* Here we have 2 cases :
198 * - modern pollers, able to report ERR/HUP. If these ones return any
199 * of these flags then it's likely a failure, otherwise it possibly
200 * is a success (i.e. there may have been data received just before
201 * the error was reported).
202 * - select, which doesn't report these and with which it's always
203 * necessary either to try connect() again or to check for SO_ERROR.
204 * In order to simplify everything, we double-check using connect() as
205 * soon as we meet either of these delicate situations. Note that
206 * SO_ERROR would clear the error after reporting it!
207 */
208 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
209 /* modern poller, able to report ERR/HUP */
210 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
211 goto done;
212 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
213 goto done;
214 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
215 goto wait;
216 /* error present, fall through common error check path */
217 }
218
219 /* Use connect() to check the state of the socket. This has the double
220 * advantage of *not* clearing the error (so that health checks can
221 * still use getsockopt(SO_ERROR)) and giving us the following info :
222 * - error
223 * - connecting (EALREADY, EINPROGRESS)
224 * - connected (EISCONN, 0)
225 */
226 addr = conn->dst;
227 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
228 addr = &objt_server(conn->target)->socks4_addr;
229
230 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
231 if (errno == EALREADY || errno == EINPROGRESS)
232 goto wait;
233
234 if (errno && errno != EISCONN)
235 goto out_error;
236 }
237
238 done:
239 /* The FD is ready now, we'll mark the connection as complete and
240 * forward the event to the transport layer which will notify the
241 * data layer.
242 */
243 conn->flags &= ~CO_FL_WAIT_L4_CONN;
244 fd_may_send(fd);
245 fd_cond_recv(fd);
246 errno = 0; // make health checks happy
247 return 1;
248
249 out_error:
250 /* Write error on the file descriptor. Report it to the connection
251 * and disable polling on this FD.
252 */
253 fdtab[fd].linger_risk = 0;
254 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau5d4d1802020-02-21 09:58:29 +0100255 conn_stop_polling(conn);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100256 return 0;
257
258 wait:
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100259 fd_cant_send(fd);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100260 fd_want_send(fd);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100261 return 0;
262}
263
Willy Tarreauff3e6482015-03-12 23:56:52 +0100264/* Send a message over an established connection. It makes use of send() and
265 * returns the same return code and errno. If the socket layer is not ready yet
266 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
267 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
268 * EMSGSIZE if called with a zero length message. The purpose is to simplify
269 * some rare attempts to directly write on the socket from above the connection
270 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
271 * It automatically retries on EINTR. Other errors cause the connection to be
272 * marked as in error state. It takes similar arguments as send() except the
273 * first one which is the connection instead of the file descriptor. Note,
274 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
275 */
276int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
277{
278 int ret;
279
280 ret = -1;
281 errno = ENOTSOCK;
282
283 if (conn->flags & CO_FL_SOCK_WR_SH)
284 goto fail;
285
286 if (!conn_ctrl_ready(conn))
287 goto fail;
288
289 errno = EMSGSIZE;
290 if (!len)
291 goto fail;
292
Willy Tarreau585744b2017-08-24 14:31:19 +0200293 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100294 goto wait;
295
296 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200297 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100298 } while (ret < 0 && errno == EINTR);
299
300
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200301 if (ret > 0) {
302 if (conn->flags & CO_FL_WAIT_L4_CONN) {
303 conn->flags &= ~CO_FL_WAIT_L4_CONN;
304 fd_may_send(conn->handle.fd);
305 fd_cond_recv(conn->handle.fd);
306 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100307 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200308 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100309
310 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
311 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200312 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100313 return 0;
314 }
315 fail:
316 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
317 return ret;
318}
319
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100320/* Called from the upper layer, to subscribe <es> to events <event_type>. The
321 * event subscriber <es> is not allowed to change from a previous call as long
322 * as at least one event is still subscribed. The <event_type> must only be a
323 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
324 */
325int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200326{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100327 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100328 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100329
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100330 es->events &= ~event_type;
331 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100332 conn->subs = NULL;
333
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100334 if (conn_ctrl_ready(conn)) {
335 if (event_type & SUB_RETRY_RECV)
336 fd_stop_recv(conn->handle.fd);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100337
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100338 if (event_type & SUB_RETRY_SEND)
339 fd_stop_send(conn->handle.fd);
340 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200341 return 0;
342}
343
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100344/* Called from the upper layer, to subscribe <es> to events <event_type>.
345 * The <es> struct is not allowed to differ from the one passed during a
346 * previous call to subscribe(). If the FD is ready, the wait_event is
347 * immediately woken up and the subcription is cancelled. It always
348 * returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100349 */
350int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200351{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100352 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100353 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100354
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100355 if (conn->subs && (conn->subs->events & event_type) == event_type)
356 return 0;
357
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100358 conn->subs = es;
359 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100360
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100361 if (conn_ctrl_ready(conn)) {
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100362 if (event_type & SUB_RETRY_RECV) {
363 if (fd_recv_ready(conn->handle.fd)) {
364 tasklet_wakeup(es->tasklet);
365 es->events &= ~SUB_RETRY_RECV;
366 if (!es->events)
367 conn->subs = NULL;
368 }
369 else
370 fd_want_recv(conn->handle.fd);
371 }
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100372
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100373 if (event_type & SUB_RETRY_SEND) {
374 if (fd_send_ready(conn->handle.fd)) {
375 tasklet_wakeup(es->tasklet);
376 es->events &= ~SUB_RETRY_SEND;
377 if (!es->events)
378 conn->subs = NULL;
379 }
380 else
381 fd_want_send(conn->handle.fd);
382 }
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100383 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200384 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200385}
386
Willy Tarreaud85c4852015-03-13 00:40:28 +0100387/* Drains possibly pending incoming data on the file descriptor attached to the
388 * connection and update the connection's flags accordingly. This is used to
389 * know whether we need to disable lingering on close. Returns non-zero if it
390 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
391 * flag may also be updated if the incoming shutdown was reported by the drain()
392 * function.
393 */
394int conn_sock_drain(struct connection *conn)
395{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200396 int turns = 2;
397 int len;
398
Willy Tarreaud85c4852015-03-13 00:40:28 +0100399 if (!conn_ctrl_ready(conn))
400 return 1;
401
402 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
403 return 1;
404
Willy Tarreaue215bba2018-08-24 14:31:53 +0200405 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
406 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100407
Willy Tarreaue215bba2018-08-24 14:31:53 +0200408 if (!fd_recv_ready(conn->handle.fd))
409 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100410
Willy Tarreaue215bba2018-08-24 14:31:53 +0200411 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200412 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100413 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200414 goto shut;
415 }
416
417 /* no drain function defined, use the generic one */
418
419 while (turns) {
420#ifdef MSG_TRUNC_CLEARS_INPUT
421 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
422 if (len == -1 && errno == EFAULT)
423#endif
424 len = recv(conn->handle.fd, trash.area, trash.size,
425 MSG_DONTWAIT | MSG_NOSIGNAL);
426
427 if (len == 0)
428 goto shut;
429
430 if (len < 0) {
431 if (errno == EAGAIN) {
432 /* connection not closed yet */
433 fd_cant_recv(conn->handle.fd);
434 break;
435 }
436 if (errno == EINTR) /* oops, try again */
437 continue;
438 /* other errors indicate a dead connection, fine. */
439 goto shut;
440 }
441 /* OK we read some data, let's try again once */
442 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100443 }
444
Willy Tarreaue215bba2018-08-24 14:31:53 +0200445 /* some data are still present, give up */
446 return 0;
447
448 shut:
449 /* we're certain the connection was shut down */
450 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100451 conn->flags |= CO_FL_SOCK_RD_SH;
452 return 1;
453}
454
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100455/*
456 * Get data length from tlv
457 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100458static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100459{
460 return (src->length_hi << 8) | src->length_lo;
461}
462
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200463/* This handshake handler waits a PROXY protocol header at the beginning of the
464 * raw data stream. The header looks like this :
465 *
466 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
467 *
468 * There must be exactly one space between each field. Fields are :
469 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
470 * - SRC3 : layer 3 (eg: IP) source address in standard text form
471 * - DST3 : layer 3 (eg: IP) destination address in standard text form
472 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
473 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
474 *
475 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
476 *
477 * The header line is small and in all cases smaller than the smallest normal
478 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
479 * can safely use MSG_PEEK and avoid buffering.
480 *
481 * Once the data is fetched, the values are set in the connection's address
482 * fields, and data are removed from the socket's buffer. The function returns
483 * zero if it needs to wait for more data or if it fails, or 1 if it completed
484 * and removed itself.
485 */
486int conn_recv_proxy(struct connection *conn, int flag)
487{
488 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200489 struct proxy_hdr_v2 *hdr_v2;
490 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100491 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100492 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200493 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200494
Willy Tarreau3c728722014-01-23 13:50:42 +0100495 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200496 goto fail;
497
Willy Tarreauca79f592019-07-17 19:04:47 +0200498 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
499 goto fail;
500
Willy Tarreau585744b2017-08-24 14:31:19 +0200501 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200502 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100503
Willy Tarreau157788c2020-02-11 10:08:05 +0100504 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200505 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
506 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200507 if (errno == EINTR)
508 continue;
509 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200510 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200511 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200512 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100513 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200514 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200515 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100516 break;
517 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100520 /* client shutdown */
521 conn->err_code = CO_ER_PRX_EMPTY;
522 goto fail;
523 }
524
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100525 conn->flags &= ~CO_FL_WAIT_L4_CONN;
526
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200528 goto missing;
529
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 line = trash.area;
531 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200532
533 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200534 if (strncmp(line, "PROXY ", 6) != 0)
535 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200536
537 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200539 goto missing;
540
David CARLIER42ff05e2016-03-24 09:22:36 +0000541 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200542 u32 src3, dst3, sport, dport;
543
544 line += 5;
545
546 src3 = inetaddr_host_lim_ret(line, end, &line);
547 if (line == end)
548 goto missing;
549 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100550 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200551
552 dst3 = inetaddr_host_lim_ret(line, end, &line);
553 if (line == end)
554 goto missing;
555 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100556 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200557
558 sport = read_uint((const char **)&line, end);
559 if (line == end)
560 goto missing;
561 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100562 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200563
564 dport = read_uint((const char **)&line, end);
565 if (line > end - 2)
566 goto missing;
567 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100568 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200569 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100570 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200571
572 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200573 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
574 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
575 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200576
Willy Tarreau226572f2019-07-17 14:46:00 +0200577 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
578 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
579 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200580 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
581 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000582 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200583 u32 sport, dport;
584 char *src_s;
585 char *dst_s, *sport_s, *dport_s;
586 struct in6_addr src3, dst3;
587
588 line += 5;
589
590 src_s = line;
591 dst_s = sport_s = dport_s = NULL;
592 while (1) {
593 if (line > end - 2) {
594 goto missing;
595 }
596 else if (*line == '\r') {
597 *line = 0;
598 line++;
599 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100600 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200601 break;
602 }
603
604 if (*line == ' ') {
605 *line = 0;
606 if (!dst_s)
607 dst_s = line + 1;
608 else if (!sport_s)
609 sport_s = line + 1;
610 else if (!dport_s)
611 dport_s = line + 1;
612 }
613 line++;
614 }
615
616 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100617 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200618
619 sport = read_uint((const char **)&sport_s,dport_s - 1);
620 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100621 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200622
623 dport = read_uint((const char **)&dport_s,line - 2);
624 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100625 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200626
627 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100628 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200629
630 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100631 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200632
633 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200634 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
635 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
636 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200637
Willy Tarreau226572f2019-07-17 14:46:00 +0200638 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
639 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
640 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200641 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
642 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200643 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
644 /* This can be a UNIX socket forwarded by an haproxy upstream */
645 line += 9;
646 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200647 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200648 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100649 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200650 goto fail;
651 }
652
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200654 goto eat_header;
655
656 not_v1:
657 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200659 goto missing;
660
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200661 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200662
663 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
664 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
665 conn->err_code = CO_ER_PRX_NOT_HDR;
666 goto fail;
667 }
668
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100669 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
670 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200671 goto missing;
672
673 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
674 case 0x01: /* PROXY command */
675 switch (hdr_v2->fam) {
676 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100677 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
678 goto bad_header;
679
Willy Tarreau226572f2019-07-17 14:46:00 +0200680 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
681 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
682 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
683 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
684 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
685 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200686 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200687 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200688 break;
689 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100690 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
691 goto bad_header;
692
Willy Tarreau226572f2019-07-17 14:46:00 +0200693 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
694 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
695 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
696 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
697 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
698 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200699 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200700 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200701 break;
702 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100703
704 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100705 while (tlv_offset < total_v2_len) {
706 struct tlv *tlv_packet;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100707 size_t tlv_len;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100708
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100709 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
710 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
711 goto bad_header;
712
713 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
714 tlv_len = get_tlv_length(tlv_packet);
715 tlv_offset += tlv_len + TLV_HEADER_SIZE;
716
717 /* Verify that the TLV length does not exceed the total PROXYv2 length */
718 if (tlv_offset > total_v2_len)
719 goto bad_header;
720
721 switch (tlv_packet->type) {
722 case PP2_TYPE_CRC32C: {
723 uint32_t n_crc32c;
724
725 /* Verify that this TLV is exactly 4 bytes long */
726 if (tlv_len != 4)
727 goto bad_header;
728
729 n_crc32c = read_n32(tlv_packet->value);
730 write_n32(tlv_packet->value, 0); // compute with CRC==0
731
732 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
733 goto bad_header;
734 break;
735 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200736#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100737 case PP2_TYPE_NETNS: {
738 const struct netns_entry *ns;
739
740 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
741 if (ns)
742 conn->proxy_netns = ns;
743 break;
744 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100745#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100746 case PP2_TYPE_AUTHORITY: {
747 if (tlv_len > PP2_AUTHORITY_MAX)
748 goto bad_header;
749 conn->proxy_authority = pool_alloc(pool_head_authority);
750 if (conn->proxy_authority == NULL)
751 goto fail;
752 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
753 conn->proxy_authority_len = tlv_len;
754 break;
755 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100756 case PP2_TYPE_UNIQUE_ID: {
757 const struct ist tlv = ist2((const char *)tlv_packet->value, tlv_len);
758
759 if (tlv.len > UNIQUEID_LEN)
760 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +0100761 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100762 if (!isttest(conn->proxy_unique_id))
763 goto fail;
764 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
765 /* This is technically unreachable, because we verified above
766 * that the TLV value fits.
767 */
768 goto fail;
769 }
770 break;
771 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100772 default:
773 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100774 }
775 }
776
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100777 /* Verify that the PROXYv2 header ends at a TLV boundary.
778 * This is technically unreachable, because the TLV parsing already
779 * verifies that a TLV does not exceed the total length and also
780 * that there is space for a TLV header.
781 */
782 if (tlv_offset != total_v2_len)
783 goto bad_header;
784
Willy Tarreau77992672014-06-14 11:06:17 +0200785 /* unsupported protocol, keep local connection address */
786 break;
787 case 0x00: /* LOCAL command */
788 /* keep local connection address for LOCAL */
789 break;
790 default:
791 goto bad_header; /* not a supported command */
792 }
793
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100794 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +0200795 goto eat_header;
796
797 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200798 /* remove the PROXY line from the request. For this we re-read the
799 * exact line at once. If we don't get the exact same result, we
800 * fail.
801 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100802 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +0100803 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
804
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200805 if (len2 < 0 && errno == EINTR)
806 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200807 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100808 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100809 break;
810 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200811
812 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100813 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200814 return 1;
815
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200816 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200817 return 0;
818
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200819 missing:
820 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
821 * we have not read anything. Otherwise we need to fail because we won't
822 * be able to poll anymore.
823 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100824 conn->err_code = CO_ER_PRX_TRUNCATED;
825 goto fail;
826
827 bad_header:
828 /* This is not a valid proxy protocol header */
829 conn->err_code = CO_ER_PRX_BAD_HDR;
830 goto fail;
831
832 recv_abort:
833 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100834 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100835 goto fail;
836
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200837 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200838 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200839 return 0;
840}
841
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100842/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000843 * at the beginning of the raw data stream. The header format is
844 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100845 *
846 * This line MUST be at the beginning of the buffer and MUST NOT be
847 * fragmented.
848 *
849 * The header line is small and in all cases smaller than the smallest normal
850 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
851 * can safely use MSG_PEEK and avoid buffering.
852 *
853 * Once the data is fetched, the values are set in the connection's address
854 * fields, and data are removed from the socket's buffer. The function returns
855 * zero if it needs to wait for more data or if it fails, or 1 if it completed
856 * and removed itself.
857 */
858int conn_recv_netscaler_cip(struct connection *conn, int flag)
859{
860 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000861 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100862 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200863 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100864
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100865 if (!conn_ctrl_ready(conn))
866 goto fail;
867
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100868 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
869 goto fail;
870
Willy Tarreau585744b2017-08-24 14:31:19 +0200871 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200872 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100873
Willy Tarreau157788c2020-02-11 10:08:05 +0100874 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200875 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
876 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100877 if (errno == EINTR)
878 continue;
879 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200880 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200881 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100882 }
883 goto recv_abort;
884 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200885 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100886 break;
887 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100888
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100889 conn->flags &= ~CO_FL_WAIT_L4_CONN;
890
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100892 /* client shutdown */
893 conn->err_code = CO_ER_CIP_EMPTY;
894 goto fail;
895 }
896
897 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000898 * CIP magic, header length or
899 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200900 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100901 goto missing;
902
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200903 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100904
905 /* Decode a possible NetScaler Client IP request, fail early if
906 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100907 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100908 goto bad_magic;
909
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000910 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200911 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100912 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000913 line += 8;
914 }
915 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200916 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100917 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000918 line += 12;
919 }
920 /* Unknown CIP protocol */
921 else {
922 conn->err_code = CO_ER_CIP_BAD_PROTO;
923 goto fail;
924 }
925
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100926 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000927 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200928 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100929 goto missing;
930
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100931 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100932 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100933
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100934 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100935 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100936 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100937
938 hdr_ip4 = (struct ip *)line;
939
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200940 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100941 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000942 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100943 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000944 }
945 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100946 /* The protocol does not include a TCP header */
947 conn->err_code = CO_ER_CIP_BAD_PROTO;
948 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000949 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100950
David Carlier3015a2e2016-07-04 22:51:33 +0100951 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100952
953 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200954 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
955 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
956 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100957
Willy Tarreau226572f2019-07-17 14:46:00 +0200958 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
959 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
960 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100961
962 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
963 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100964 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100965 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100966 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100967
968 hdr_ip6 = (struct ip6_hdr *)line;
969
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200970 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100971 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000972 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100973 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000974 }
975 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100976 /* The protocol does not include a TCP header */
977 conn->err_code = CO_ER_CIP_BAD_PROTO;
978 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000979 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100980
David Carlier3015a2e2016-07-04 22:51:33 +0100981 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100982
983 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200984 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
985 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
986 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100987
Willy Tarreau226572f2019-07-17 14:46:00 +0200988 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
989 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
990 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100991
992 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
993 }
994 else {
995 /* The protocol does not match something known (IPv4/IPv6) */
996 conn->err_code = CO_ER_CIP_BAD_PROTO;
997 goto fail;
998 }
999
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001000 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001001 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001002
1003 /* remove the NetScaler Client IP header from the request. For this
1004 * we re-read the exact line at once. If we don't get the exact same
1005 * result, we fail.
1006 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001007 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001008 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001009 if (len2 < 0 && errno == EINTR)
1010 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001011 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001012 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001013 break;
1014 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001015
1016 conn->flags &= ~flag;
1017 return 1;
1018
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001019 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001020 return 0;
1021
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001022 missing:
1023 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1024 * we have not read anything. Otherwise we need to fail because we won't
1025 * be able to poll anymore.
1026 */
1027 conn->err_code = CO_ER_CIP_TRUNCATED;
1028 goto fail;
1029
1030 bad_magic:
1031 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1032 goto fail;
1033
1034 recv_abort:
1035 conn->err_code = CO_ER_CIP_ABORT;
1036 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1037 goto fail;
1038
1039 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001040 conn->flags |= CO_FL_ERROR;
1041 return 0;
1042}
1043
Alexander Liu2a54bb72019-05-22 19:44:48 +08001044
1045int conn_send_socks4_proxy_request(struct connection *conn)
1046{
1047 struct socks4_request req_line;
1048
Alexander Liu2a54bb72019-05-22 19:44:48 +08001049 if (!conn_ctrl_ready(conn))
1050 goto out_error;
1051
Willy Tarreau226572f2019-07-17 14:46:00 +02001052 if (!conn_get_dst(conn))
1053 goto out_error;
1054
Alexander Liu2a54bb72019-05-22 19:44:48 +08001055 req_line.version = 0x04;
1056 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001057 req_line.port = get_net_port(conn->dst);
1058 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001059 memcpy(req_line.user_id, "HAProxy\0", 8);
1060
1061 if (conn->send_proxy_ofs > 0) {
1062 /*
1063 * This is the first call to send the request
1064 */
1065 conn->send_proxy_ofs = -(int)sizeof(req_line);
1066 }
1067
1068 if (conn->send_proxy_ofs < 0) {
1069 int ret = 0;
1070
1071 /* we are sending the socks4_req_line here. If the data layer
1072 * has a pending write, we'll also set MSG_MORE.
1073 */
1074 ret = conn_sock_send(
1075 conn,
1076 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1077 -conn->send_proxy_ofs,
Willy Tarreau19bc2012020-02-21 08:46:19 +01001078 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001079
1080 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1081 conn->handle.fd, -conn->send_proxy_ofs, ret);
1082
1083 if (ret < 0) {
1084 goto out_error;
1085 }
1086
1087 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1088 if (conn->send_proxy_ofs != 0) {
1089 goto out_wait;
1090 }
1091 }
1092
1093 /* OK we've the whole request sent */
1094 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001095
1096 /* The connection is ready now, simply return and let the connection
1097 * handler notify upper layers if needed.
1098 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001099 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001100
1101 if (conn->flags & CO_FL_SEND_PROXY) {
1102 /*
1103 * Get the send_proxy_ofs ready for the send_proxy due to we are
1104 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1105 * before sending PROXY Protocol.
1106 */
1107 conn->send_proxy_ofs = 1;
1108 }
1109 return 1;
1110
1111 out_error:
1112 /* Write error on the file descriptor */
1113 conn->flags |= CO_FL_ERROR;
1114 if (conn->err_code == CO_ER_NONE) {
1115 conn->err_code = CO_ER_SOCKS4_SEND;
1116 }
1117 return 0;
1118
1119 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001120 return 0;
1121}
1122
1123int conn_recv_socks4_proxy_response(struct connection *conn)
1124{
1125 char line[SOCKS4_HS_RSP_LEN];
1126 int ret;
1127
Alexander Liu2a54bb72019-05-22 19:44:48 +08001128 if (!conn_ctrl_ready(conn))
1129 goto fail;
1130
1131 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001132 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001133
Willy Tarreau157788c2020-02-11 10:08:05 +01001134 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001135 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1136 * Try to peek into it, before all 8 bytes ready.
1137 */
1138 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1139
1140 if (ret == 0) {
1141 /* the socket has been closed or shutdown for send */
1142 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1143 conn->handle.fd, ret, errno);
1144 if (conn->err_code == CO_ER_NONE) {
1145 conn->err_code = CO_ER_SOCKS4_RECV;
1146 }
1147 goto fail;
1148 }
1149
1150 if (ret > 0) {
1151 if (ret == SOCKS4_HS_RSP_LEN) {
1152 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1153 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1154 }else{
1155 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]);
1156 }
1157 } else {
1158 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1159 }
1160
1161 if (ret < 0) {
1162 if (errno == EINTR) {
1163 continue;
1164 }
1165 if (errno == EAGAIN) {
1166 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001167 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001168 }
1169 goto recv_abort;
1170 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001171 break;
1172 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001173
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001174 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1175
Alexander Liu2a54bb72019-05-22 19:44:48 +08001176 if (ret < SOCKS4_HS_RSP_LEN) {
1177 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1178 * we are not able to read enough data.
1179 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001180 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001181 }
1182
1183 /*
1184 * Base on the SOCSK4 protocol:
1185 *
1186 * +----+----+----+----+----+----+----+----+
1187 * | VN | CD | DSTPORT | DSTIP |
1188 * +----+----+----+----+----+----+----+----+
1189 * # of bytes: 1 1 2 4
1190 * VN is the version of the reply code and should be 0. CD is the result
1191 * code with one of the following values:
1192 * 90: request granted
1193 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001194 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001195 * 93: request rejected because the client program and identd report different user-ids
1196 * The remaining fields are ignored.
1197 */
1198 if (line[1] != 90) {
1199 conn->flags &= ~CO_FL_SOCKS4_RECV;
1200
1201 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1202 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1203 if (conn->err_code == CO_ER_NONE) {
1204 conn->err_code = CO_ER_SOCKS4_DENY;
1205 }
1206 goto fail;
1207 }
1208
1209 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001210 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001211 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1212 if (ret < 0 && errno == EINTR) {
1213 continue;
1214 }
1215 if (ret != SOCKS4_HS_RSP_LEN) {
1216 if (conn->err_code == CO_ER_NONE) {
1217 conn->err_code = CO_ER_SOCKS4_RECV;
1218 }
1219 goto fail;
1220 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001221 break;
1222 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001223
1224 conn->flags &= ~CO_FL_SOCKS4_RECV;
1225 return 1;
1226
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001227 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001228 return 0;
1229
Alexander Liu2a54bb72019-05-22 19:44:48 +08001230 recv_abort:
1231 if (conn->err_code == CO_ER_NONE) {
1232 conn->err_code = CO_ER_SOCKS4_ABORT;
1233 }
1234 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1235 goto fail;
1236
1237 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001238 conn->flags |= CO_FL_ERROR;
1239 return 0;
1240}
1241
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001242/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001243int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001244{
1245 int ret = 0;
1246
1247 if (srv && (srv->pp_opts & SRV_PP_V2)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001248 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
David Safb76832014-05-08 23:42:08 -04001249 }
1250 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001251 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1252 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001253 else
1254 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1255 }
1256
1257 return ret;
1258}
1259
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001260/* Makes a PROXY protocol line from the two addresses. The output is sent to
1261 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1262 * It returns the number of bytes composing this line (including the trailing
1263 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001264 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1265 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001266 */
David Safb76832014-05-08 23:42:08 -04001267int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001268{
1269 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001270 char * protocol;
1271 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1272 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1273 in_port_t src_port;
1274 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001275
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001276 if ( !src
1277 || !dst
1278 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1279 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1280 /* unknown family combination */
1281 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001282 if (ret >= buf_len)
1283 return 0;
1284
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001285 return ret;
1286 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001287
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001288 /* IPv4 for both src and dst */
1289 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1290 protocol = "TCP4";
1291 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001292 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001293 src_port = ((struct sockaddr_in *)src)->sin_port;
1294 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001295 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001296 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001297 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001298 /* IPv6 for at least one of src and dst */
1299 else {
1300 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001301
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001302 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001303
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001304 if (src->ss_family == AF_INET) {
1305 /* Convert src to IPv6 */
1306 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1307 src_port = ((struct sockaddr_in *)src)->sin_port;
1308 }
1309 else {
1310 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1311 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1312 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001313
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001314 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001315 return 0;
1316
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001317 if (dst->ss_family == AF_INET) {
1318 /* Convert dst to IPv6 */
1319 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1320 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1321 }
1322 else {
1323 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1324 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1325 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001326
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001327 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001328 return 0;
1329 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001330
1331 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1332 if (ret >= buf_len)
1333 return 0;
1334
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001335 return ret;
1336}
David Safb76832014-05-08 23:42:08 -04001337
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001338static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001339{
1340 struct tlv *tlv;
1341
1342 if (!dest || (length + sizeof(*tlv) > dest_len))
1343 return 0;
1344
1345 tlv = (struct tlv *)dest;
1346
1347 tlv->type = type;
1348 tlv->length_hi = length >> 8;
1349 tlv->length_lo = length & 0x00ff;
1350 memcpy(tlv->value, value, length);
1351 return length + sizeof(*tlv);
1352}
David Safb76832014-05-08 23:42:08 -04001353
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001354/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001355int 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 -04001356{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001357 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001358 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001359 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001360 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001361 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001362 struct sockaddr_storage *src = &null_addr;
1363 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001364 const char *value;
1365 int value_len;
David Safb76832014-05-08 23:42:08 -04001366
1367 if (buf_len < PP2_HEADER_LEN)
1368 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001369 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001370
Willy Tarreau226572f2019-07-17 14:46:00 +02001371 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1372 src = remote->src;
1373 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001374 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001375
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001376 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1377 if ( !src
1378 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001379 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001380 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1381 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001382 if (buf_len < PP2_HDR_LEN_UNSPEC)
1383 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001384 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1385 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001386 ret = PP2_HDR_LEN_UNSPEC;
1387 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001388 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001389 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001390 /* IPv4 for both src and dst */
1391 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1392 if (buf_len < PP2_HDR_LEN_INET)
1393 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001394 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1395 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1396 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1397 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1398 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1399 ret = PP2_HDR_LEN_INET;
1400 }
1401 /* IPv6 for at least one of src and dst */
1402 else {
1403 struct in6_addr tmp;
1404
1405 if (buf_len < PP2_HDR_LEN_INET6)
1406 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001407 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1408 if (src->ss_family == AF_INET) {
1409 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1410 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1411 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1412 }
1413 else {
1414 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1415 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1416 }
1417 if (dst->ss_family == AF_INET) {
1418 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1419 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001420 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001421 }
1422 else {
1423 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1424 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1425 }
1426
1427 ret = PP2_HDR_LEN_INET6;
1428 }
1429 }
David Safb76832014-05-08 23:42:08 -04001430
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001431 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1432 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001433
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001434 if ((buf_len - ret) < sizeof(struct tlv))
1435 return 0;
1436 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1437 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1438 }
1439
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001440 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001441 if ((buf_len - ret) < sizeof(struct tlv))
1442 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001443 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001444 }
1445
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001446 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001447 value = NULL;
1448 if (remote && remote->proxy_authority) {
1449 value = remote->proxy_authority;
1450 value_len = remote->proxy_authority_len;
1451 }
1452#ifdef USE_OPENSSL
1453 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001454 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001455 value_len = strlen(value);
1456 }
1457#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001458 if (value) {
1459 if ((buf_len - ret) < sizeof(struct tlv))
1460 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001461 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001462 }
1463 }
1464
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001465 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001466 struct session* sess = strm_sess(strm);
1467 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1468
1469 value = unique_id.ptr;
1470 value_len = unique_id.len;
1471
1472 if (value_len >= 0) {
1473 if ((buf_len - ret) < sizeof(struct tlv))
1474 return 0;
1475 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1476 }
1477 }
1478
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001479#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001480 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001481 struct tlv_ssl *tlv;
1482 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001483
David Safb76832014-05-08 23:42:08 -04001484 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1485 return 0;
1486 tlv = (struct tlv_ssl *)&buf[ret];
1487 memset(tlv, 0, sizeof(struct tlv_ssl));
1488 ssl_tlv_len += sizeof(struct tlv_ssl);
1489 tlv->tlv.type = PP2_TYPE_SSL;
1490 if (ssl_sock_is_ssl(remote)) {
1491 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001492 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001493 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001494 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 -04001495 }
Dave McCowan328fb582014-07-30 10:39:13 -04001496 if (ssl_sock_get_cert_used_sess(remote)) {
1497 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001498 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001499 if (ssl_sock_get_cert_used_conn(remote))
1500 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001501 }
1502 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001503 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001504 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001505 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1506 cn_trash->data,
1507 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001508 }
1509 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001510 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001511 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001512 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001513 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1514 pkey_trash->data,
1515 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001516 }
1517 }
1518 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1519 value = ssl_sock_get_cert_sig(remote);
1520 if (value) {
1521 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1522 }
1523 }
1524 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1525 value = ssl_sock_get_cipher_name(remote);
1526 if (value) {
1527 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1528 }
1529 }
David Safb76832014-05-08 23:42:08 -04001530 }
1531 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1532 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1533 ret += ssl_tlv_len;
1534 }
1535#endif
1536
Willy Tarreaue5733232019-05-22 19:24:06 +02001537#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001538 if (remote && (remote->proxy_netns)) {
1539 if ((buf_len - ret) < sizeof(struct tlv))
1540 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001541 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 +01001542 }
1543#endif
1544
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001545 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001546
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001547 if (tlv_crc32c_p) {
1548 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1549 }
1550
David Safb76832014-05-08 23:42:08 -04001551 return ret;
1552}
Emeric Brun4f603012017-01-05 15:11:44 +01001553
Willy Tarreau119e50e2020-05-22 13:53:29 +02001554/* returns 0 on success */
1555static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
1556 struct proxy *defpx, const char *file, int line,
1557 char **err)
1558{
1559 if (too_many_args(0, args, err, NULL))
1560 return -1;
1561 pp2_never_send_local = 1;
1562 return 0;
1563}
1564
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001565/* return the major HTTP version as 1 or 2 depending on how the request arrived
1566 * before being processed.
1567 */
1568static int
1569smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1570{
Jérôme Magnin86577422018-12-07 09:03:11 +01001571 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1572 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001573
1574 smp->data.type = SMP_T_SINT;
1575 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1576 return 1;
1577}
1578
Emeric Brun4f603012017-01-05 15:11:44 +01001579/* fetch if the received connection used a PROXY protocol header */
1580int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1581{
1582 struct connection *conn;
1583
1584 conn = objt_conn(smp->sess->origin);
1585 if (!conn)
1586 return 0;
1587
Willy Tarreau911db9b2020-01-23 16:27:54 +01001588 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001589 smp->flags |= SMP_F_MAY_CHANGE;
1590 return 0;
1591 }
1592
1593 smp->flags = 0;
1594 smp->data.type = SMP_T_BOOL;
1595 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1596
1597 return 1;
1598}
1599
Geoff Simmons7185b782019-08-27 18:31:16 +02001600/* fetch the authority TLV from a PROXY protocol header */
1601int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1602{
1603 struct connection *conn;
1604
1605 conn = objt_conn(smp->sess->origin);
1606 if (!conn)
1607 return 0;
1608
Willy Tarreau911db9b2020-01-23 16:27:54 +01001609 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001610 smp->flags |= SMP_F_MAY_CHANGE;
1611 return 0;
1612 }
1613
1614 if (conn->proxy_authority == NULL)
1615 return 0;
1616
1617 smp->flags = 0;
1618 smp->data.type = SMP_T_STR;
1619 smp->data.u.str.area = conn->proxy_authority;
1620 smp->data.u.str.data = conn->proxy_authority_len;
1621
1622 return 1;
1623}
1624
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001625/* fetch the unique ID TLV from a PROXY protocol header */
1626int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1627{
1628 struct connection *conn;
1629
1630 conn = objt_conn(smp->sess->origin);
1631 if (!conn)
1632 return 0;
1633
1634 if (conn->flags & CO_FL_WAIT_XPRT) {
1635 smp->flags |= SMP_F_MAY_CHANGE;
1636 return 0;
1637 }
1638
1639 if (!isttest(conn->proxy_unique_id))
1640 return 0;
1641
1642 smp->flags = 0;
1643 smp->data.type = SMP_T_STR;
1644 smp->data.u.str.area = conn->proxy_unique_id.ptr;
1645 smp->data.u.str.data = conn->proxy_unique_id.len;
1646
1647 return 1;
1648}
1649
Emeric Brun4f603012017-01-05 15:11:44 +01001650/* Note: must not be declared <const> as its list will be overwritten.
1651 * Note: fetches that may return multiple types must be declared as the lowest
1652 * common denominator, the type that can be casted into all other ones. For
1653 * instance v4/v6 must be declared v4.
1654 */
1655static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001656 { "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 +01001657 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001658 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001659 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001660 { "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 +01001661 { /* END */ },
1662}};
1663
Willy Tarreau0108d902018-11-25 19:14:37 +01001664INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02001665
1666static struct cfg_kw_list cfg_kws = {ILH, {
1667 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
1668 { /* END */ },
1669}};
1670
1671INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);