blob: 3337538c8d729ae824de66ca48778c2e369a54f6 [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 Tarreau59f98392012-07-06 14:13:49 +020015#include <common/compat.h>
16#include <common/config.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010018#include <common/namespace.h>
Emmanuel Hocdet4399c752018-02-05 15:26:43 +010019#include <common/hash.h>
20#include <common/net_helper.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020021
Willy Tarreauc5788912012-08-24 18:12:41 +020022#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020023#include <proto/fd.h>
Willy Tarreau5f1504f2012-10-04 23:55:57 +020024#include <proto/frontend.h>
Willy Tarreau2da156f2012-07-23 15:07:23 +020025#include <proto/proto_tcp.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020026#include <proto/stream_interface.h>
Emeric Brun4f603012017-01-05 15:11:44 +010027#include <proto/sample.h>
Emeric Brun46591952012-05-18 15:47:34 +020028#include <proto/ssl_sock.h>
Emeric Brun46591952012-05-18 15:47:34 +020029
Alexander Liu2a54bb72019-05-22 19:44:48 +080030#include <common/debug.h>
31
Willy Tarreau8ceae722018-11-26 11:58:30 +010032DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
33DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
Willy Tarreauff5d57b2019-07-17 18:37:02 +020034DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
Geoff Simmons7185b782019-08-27 18:31:16 +020035DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010036
Willy Tarreau13e14102016-12-22 20:25:26 +010037struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020038
Christopher Faulet32f61c02018-04-10 14:33:41 +020039/* List head of all known muxes for PROTO */
40struct mux_proto_list mux_proto_list = {
41 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020042};
43
Willy Tarreau59f98392012-07-06 14:13:49 +020044/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020045 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020046 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020047void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020048{
Willy Tarreau80184712012-07-06 14:54:49 +020049 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020050 unsigned int flags;
Olivier Houchardaf4021e2018-08-09 13:06:55 +020051 int io_available = 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020052
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010053 if (unlikely(!conn)) {
54 activity[tid].conn_dead++;
Willy Tarreau7a798e52016-04-14 11:13:20 +020055 return;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010056 }
Willy Tarreau59f98392012-07-06 14:13:49 +020057
Willy Tarreau7d281492012-12-16 19:19:13 +010058 conn_refresh_polling_flags(conn);
Willy Tarreau916e12d2017-10-25 09:22:43 +020059 conn->flags |= CO_FL_WILL_UPDATE;
60
Willy Tarreau7d281492012-12-16 19:19:13 +010061 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010062
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010063 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
64 ((fd_send_ready(fd) && fd_send_active(fd)) ||
65 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
66 /* Still waiting for a connection to establish and nothing was
67 * attempted yet to probe the connection. this will clear the
68 * CO_FL_WAIT_L4_CONN flag on success.
69 */
70 if (!conn_fd_check(conn))
71 goto leave;
72 }
73
Willy Tarreau8081abe2019-11-28 18:08:49 +010074 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010075 /* force reporting of activity by clearing the previous flags :
76 * we'll have at least ERROR or CONNECTED at the end of an I/O,
77 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020078 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +010079 flags = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +020080 if (conn->send_wait != NULL) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +010081 conn->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +020082 tasklet_wakeup(conn->send_wait->tasklet);
Olivier Houchardfa8aa862018-10-10 18:25:41 +020083 conn->send_wait = NULL;
84 } else
85 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +020086 __conn_xprt_stop_send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020087 }
Willy Tarreau59f98392012-07-06 14:13:49 +020088
Willy Tarreau57ec32f2017-04-11 19:59:33 +020089 /* The data transfer starts here and stops on error and handshakes. Note
90 * that we must absolutely test conn->xprt at each step in case it suddenly
91 * changes due to a quick unexpected close().
92 */
Willy Tarreau8081abe2019-11-28 18:08:49 +010093 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010094 /* force reporting of activity by clearing the previous flags :
95 * we'll have at least ERROR or CONNECTED at the end of an I/O,
96 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020097 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +010098 flags = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +020099 if (conn->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100100 conn->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200101 tasklet_wakeup(conn->recv_wait->tasklet);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200102 conn->recv_wait = NULL;
103 } else
104 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200105 __conn_xprt_stop_recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200106 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200107
Willy Tarreau2c6be842012-07-06 17:12:34 +0200108 leave:
Willy Tarreaucbcf77e2019-12-27 14:49:19 +0100109 /* Verify if the connection just established. */
110 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
111 conn->flags |= CO_FL_CONNECTED;
112
113 /* The connection owner might want to be notified about failures
114 * and/or handshake completeion. The callback may fail and cause the
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200115 * connection to be destroyed, thus we must not use it anymore and
116 * should immediately leave instead. The caller must immediately
117 * unregister itself once called.
118 */
Willy Tarreaucbcf77e2019-12-27 14:49:19 +0100119 if (unlikely(conn->xprt_done_cb) &&
120 (!(conn->flags & CO_FL_HANDSHAKE) ||
121 ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE)) &&
122 conn->xprt_done_cb(conn) < 0)
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200123 return;
124
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100125 /* The wake callback is normally used to notify the data layer about
126 * data layer activity (successful send/recv), connection establishment,
127 * shutdown and fatal errors. We need to consider the following
128 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100129 * - change among the CO_FL_NOTIFY_DONE flags :
130 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100131 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
132 * end of handshake and transition to CONNECTED
133 * - raise of CONNECTED with HANDSHAKE down
134 * - end of HANDSHAKE with CONNECTED set
135 * - regular data layer activity
136 *
137 * Note that the wake callback is allowed to release the connection and
138 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200139 */
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100140 if ((io_available || (((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100141 ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED &&
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200142 (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200143 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200144 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200145
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200146 /* commit polling changes */
Willy Tarreau916e12d2017-10-25 09:22:43 +0200147 conn->flags &= ~CO_FL_WILL_UPDATE;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200148 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200149 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200150}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200151
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200152/* Update polling on connection <c>'s file descriptor depending on its current
153 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200154 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_XPRT_*.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200155 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200156 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200157 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200158void conn_update_xprt_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200160 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161
Willy Tarreau3c728722014-01-23 13:50:42 +0100162 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200163 return;
164
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165 /* update read status if needed */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200166 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_XPRT_RD_ENA)) == CO_FL_XPRT_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200167 fd_want_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100168 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200170 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_XPRT_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200171 fd_stop_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100172 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200173 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200174
175 /* update write status if needed */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200176 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_XPRT_WR_ENA)) == CO_FL_XPRT_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200177 fd_want_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100178 f |= CO_FL_CURR_WR_ENA;
179 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200180 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_XPRT_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200181 fd_stop_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100182 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200183 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100184 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200185}
186
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100187/* This is the callback which is set when a connection establishment is pending
188 * and we have nothing to send. It may update the FD polling status to indicate
189 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
190 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
191 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
192 * leaves the error code in errno.
193 */
194int conn_fd_check(struct connection *conn)
195{
196 struct sockaddr_storage *addr;
197 int fd = conn->handle.fd;
198
199 if (conn->flags & CO_FL_ERROR)
200 return 0;
201
202 if (!conn_ctrl_ready(conn))
203 return 0;
204
205 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
206 return 1; /* strange we were called while ready */
207
208 if (!fd_send_ready(fd))
209 return 0;
210
211 /* Here we have 2 cases :
212 * - modern pollers, able to report ERR/HUP. If these ones return any
213 * of these flags then it's likely a failure, otherwise it possibly
214 * is a success (i.e. there may have been data received just before
215 * the error was reported).
216 * - select, which doesn't report these and with which it's always
217 * necessary either to try connect() again or to check for SO_ERROR.
218 * In order to simplify everything, we double-check using connect() as
219 * soon as we meet either of these delicate situations. Note that
220 * SO_ERROR would clear the error after reporting it!
221 */
222 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
223 /* modern poller, able to report ERR/HUP */
224 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
225 goto done;
226 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
227 goto done;
228 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
229 goto wait;
230 /* error present, fall through common error check path */
231 }
232
233 /* Use connect() to check the state of the socket. This has the double
234 * advantage of *not* clearing the error (so that health checks can
235 * still use getsockopt(SO_ERROR)) and giving us the following info :
236 * - error
237 * - connecting (EALREADY, EINPROGRESS)
238 * - connected (EISCONN, 0)
239 */
240 addr = conn->dst;
241 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
242 addr = &objt_server(conn->target)->socks4_addr;
243
244 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
245 if (errno == EALREADY || errno == EINPROGRESS)
246 goto wait;
247
248 if (errno && errno != EISCONN)
249 goto out_error;
250 }
251
252 done:
253 /* The FD is ready now, we'll mark the connection as complete and
254 * forward the event to the transport layer which will notify the
255 * data layer.
256 */
257 conn->flags &= ~CO_FL_WAIT_L4_CONN;
258 fd_may_send(fd);
259 fd_cond_recv(fd);
260 errno = 0; // make health checks happy
261 return 1;
262
263 out_error:
264 /* Write error on the file descriptor. Report it to the connection
265 * and disable polling on this FD.
266 */
267 fdtab[fd].linger_risk = 0;
268 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
269 __conn_xprt_stop_both(conn);
270 return 0;
271
272 wait:
273 __conn_xprt_want_send(conn);
274 fd_cant_send(fd);
275 return 0;
276}
277
Willy Tarreauff3e6482015-03-12 23:56:52 +0100278/* Send a message over an established connection. It makes use of send() and
279 * returns the same return code and errno. If the socket layer is not ready yet
280 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
281 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
282 * EMSGSIZE if called with a zero length message. The purpose is to simplify
283 * some rare attempts to directly write on the socket from above the connection
284 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
285 * It automatically retries on EINTR. Other errors cause the connection to be
286 * marked as in error state. It takes similar arguments as send() except the
287 * first one which is the connection instead of the file descriptor. Note,
288 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
289 */
290int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
291{
292 int ret;
293
294 ret = -1;
295 errno = ENOTSOCK;
296
297 if (conn->flags & CO_FL_SOCK_WR_SH)
298 goto fail;
299
300 if (!conn_ctrl_ready(conn))
301 goto fail;
302
303 errno = EMSGSIZE;
304 if (!len)
305 goto fail;
306
Willy Tarreau585744b2017-08-24 14:31:19 +0200307 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100308 goto wait;
309
310 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200311 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100312 } while (ret < 0 && errno == EINTR);
313
314
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200315 if (ret > 0) {
316 if (conn->flags & CO_FL_WAIT_L4_CONN) {
317 conn->flags &= ~CO_FL_WAIT_L4_CONN;
318 fd_may_send(conn->handle.fd);
319 fd_cond_recv(conn->handle.fd);
320 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100321 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200322 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100323
324 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
325 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200326 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100327 return 0;
328 }
329 fail:
330 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
331 return ret;
332}
333
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100334int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200335{
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200336 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200337
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100338 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200339 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200340 BUG_ON(conn->recv_wait != sw);
341 conn->recv_wait = NULL;
342 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchard53216e72018-10-10 15:46:36 +0200343 __conn_xprt_stop_recv(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200344 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100345 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200346 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200347 BUG_ON(conn->send_wait != sw);
348 conn->send_wait = NULL;
349 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard53216e72018-10-10 15:46:36 +0200350 __conn_xprt_stop_send(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200351 }
Olivier Houchard53216e72018-10-10 15:46:36 +0200352 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200353 return 0;
354}
355
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100356int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200357{
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200358 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200359
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100360 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +0200361 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200362 BUG_ON(conn->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
363 sw->events |= SUB_RETRY_RECV;
364 conn->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100365 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard53216e72018-10-10 15:46:36 +0200366 __conn_xprt_want_recv(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200367 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100368 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +0200369 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200370 BUG_ON(conn->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
371 sw->events |= SUB_RETRY_SEND;
372 conn->send_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100373 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard53216e72018-10-10 15:46:36 +0200374 __conn_xprt_want_send(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200375 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200376 if (event_type != 0)
377 return (-1);
Olivier Houchard53216e72018-10-10 15:46:36 +0200378 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200379 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200380}
381
Willy Tarreaud85c4852015-03-13 00:40:28 +0100382/* Drains possibly pending incoming data on the file descriptor attached to the
383 * connection and update the connection's flags accordingly. This is used to
384 * know whether we need to disable lingering on close. Returns non-zero if it
385 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
386 * flag may also be updated if the incoming shutdown was reported by the drain()
387 * function.
388 */
389int conn_sock_drain(struct connection *conn)
390{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200391 int turns = 2;
392 int len;
393
Willy Tarreaud85c4852015-03-13 00:40:28 +0100394 if (!conn_ctrl_ready(conn))
395 return 1;
396
397 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
398 return 1;
399
Willy Tarreaue215bba2018-08-24 14:31:53 +0200400 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
401 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100402
Willy Tarreaue215bba2018-08-24 14:31:53 +0200403 if (!fd_recv_ready(conn->handle.fd))
404 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100405
Willy Tarreaue215bba2018-08-24 14:31:53 +0200406 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200407 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100408 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200409 goto shut;
410 }
411
412 /* no drain function defined, use the generic one */
413
414 while (turns) {
415#ifdef MSG_TRUNC_CLEARS_INPUT
416 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
417 if (len == -1 && errno == EFAULT)
418#endif
419 len = recv(conn->handle.fd, trash.area, trash.size,
420 MSG_DONTWAIT | MSG_NOSIGNAL);
421
422 if (len == 0)
423 goto shut;
424
425 if (len < 0) {
426 if (errno == EAGAIN) {
427 /* connection not closed yet */
428 fd_cant_recv(conn->handle.fd);
429 break;
430 }
431 if (errno == EINTR) /* oops, try again */
432 continue;
433 /* other errors indicate a dead connection, fine. */
434 goto shut;
435 }
436 /* OK we read some data, let's try again once */
437 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100438 }
439
Willy Tarreaue215bba2018-08-24 14:31:53 +0200440 /* some data are still present, give up */
441 return 0;
442
443 shut:
444 /* we're certain the connection was shut down */
445 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100446 conn->flags |= CO_FL_SOCK_RD_SH;
447 return 1;
448}
449
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100450/*
451 * Get data length from tlv
452 */
453static int get_tlv_length(const struct tlv *src)
454{
455 return (src->length_hi << 8) | src->length_lo;
456}
457
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200458/* This handshake handler waits a PROXY protocol header at the beginning of the
459 * raw data stream. The header looks like this :
460 *
461 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
462 *
463 * There must be exactly one space between each field. Fields are :
464 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
465 * - SRC3 : layer 3 (eg: IP) source address in standard text form
466 * - DST3 : layer 3 (eg: IP) destination address in standard text form
467 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
468 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
469 *
470 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
471 *
472 * The header line is small and in all cases smaller than the smallest normal
473 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
474 * can safely use MSG_PEEK and avoid buffering.
475 *
476 * Once the data is fetched, the values are set in the connection's address
477 * fields, and data are removed from the socket's buffer. The function returns
478 * zero if it needs to wait for more data or if it fails, or 1 if it completed
479 * and removed itself.
480 */
481int conn_recv_proxy(struct connection *conn, int flag)
482{
483 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200484 struct proxy_hdr_v2 *hdr_v2;
485 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100486 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200487 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200488 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200489
490 /* we might have been called just after an asynchronous shutr */
491 if (conn->flags & CO_FL_SOCK_RD_SH)
492 goto fail;
493
Willy Tarreau3c728722014-01-23 13:50:42 +0100494 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200495 goto fail;
496
Willy Tarreauca79f592019-07-17 19:04:47 +0200497 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
498 goto fail;
499
Willy Tarreau585744b2017-08-24 14:31:19 +0200500 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200501 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100502
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200504 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
505 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200506 if (errno == EINTR)
507 continue;
508 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200509 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200510 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200511 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100512 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200513 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200514 trash.data = ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200515 } while (0);
516
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100518 /* client shutdown */
519 conn->err_code = CO_ER_PRX_EMPTY;
520 goto fail;
521 }
522
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200523 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200524 goto missing;
525
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200526 line = trash.area;
527 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200528
529 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200530 if (strncmp(line, "PROXY ", 6) != 0)
531 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200532
533 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200534 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200535 goto missing;
536
David CARLIER42ff05e2016-03-24 09:22:36 +0000537 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200538 u32 src3, dst3, sport, dport;
539
540 line += 5;
541
542 src3 = inetaddr_host_lim_ret(line, end, &line);
543 if (line == end)
544 goto missing;
545 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100546 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200547
548 dst3 = inetaddr_host_lim_ret(line, end, &line);
549 if (line == end)
550 goto missing;
551 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100552 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200553
554 sport = read_uint((const char **)&line, end);
555 if (line == end)
556 goto missing;
557 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100558 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200559
560 dport = read_uint((const char **)&line, end);
561 if (line > end - 2)
562 goto missing;
563 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100564 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200565 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100566 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200567
568 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200569 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
570 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
571 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200572
Willy Tarreau226572f2019-07-17 14:46:00 +0200573 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
574 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
575 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200576 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
577 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000578 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200579 u32 sport, dport;
580 char *src_s;
581 char *dst_s, *sport_s, *dport_s;
582 struct in6_addr src3, dst3;
583
584 line += 5;
585
586 src_s = line;
587 dst_s = sport_s = dport_s = NULL;
588 while (1) {
589 if (line > end - 2) {
590 goto missing;
591 }
592 else if (*line == '\r') {
593 *line = 0;
594 line++;
595 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100596 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200597 break;
598 }
599
600 if (*line == ' ') {
601 *line = 0;
602 if (!dst_s)
603 dst_s = line + 1;
604 else if (!sport_s)
605 sport_s = line + 1;
606 else if (!dport_s)
607 dport_s = line + 1;
608 }
609 line++;
610 }
611
612 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100613 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200614
615 sport = read_uint((const char **)&sport_s,dport_s - 1);
616 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100617 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200618
619 dport = read_uint((const char **)&dport_s,line - 2);
620 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100621 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200622
623 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100624 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200625
626 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100627 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200628
629 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200630 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
631 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
632 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633
Willy Tarreau226572f2019-07-17 14:46:00 +0200634 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
635 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
636 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200637 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
638 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200639 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
640 /* This can be a UNIX socket forwarded by an haproxy upstream */
641 line += 9;
642 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200643 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200644 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100645 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200646 goto fail;
647 }
648
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200649 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200650 goto eat_header;
651
652 not_v1:
653 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200655 goto missing;
656
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200658
659 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
660 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
661 conn->err_code = CO_ER_PRX_NOT_HDR;
662 goto fail;
663 }
664
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200665 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200666 goto missing;
667
668 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
669 case 0x01: /* PROXY command */
670 switch (hdr_v2->fam) {
671 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100672 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
673 goto bad_header;
674
Willy Tarreau226572f2019-07-17 14:46:00 +0200675 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
676 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
677 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
678 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
679 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
680 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200681 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200682 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100683 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200684 break;
685 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100686 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
687 goto bad_header;
688
Willy Tarreau226572f2019-07-17 14:46:00 +0200689 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
690 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
691 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
692 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
693 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
694 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.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_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100697 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200698 break;
699 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100700
701 /* TLV parsing */
702 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
704 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100705 const int tlv_len = get_tlv_length(tlv_packet);
706 tlv_offset += tlv_len + TLV_HEADER_SIZE;
707
708 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100709 case PP2_TYPE_CRC32C: {
710 void *tlv_crc32c_p = (void *)tlv_packet->value;
711 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
712 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100714 goto bad_header;
715 break;
716 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200717#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100718 case PP2_TYPE_NETNS: {
719 const struct netns_entry *ns;
720 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
721 if (ns)
722 conn->proxy_netns = ns;
723 break;
724 }
725#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200726 case PP2_TYPE_AUTHORITY: {
727 if (tlv_len > PP2_AUTHORITY_MAX)
728 goto bad_header;
729 conn->proxy_authority = pool_alloc(pool_head_authority);
730 if (conn->proxy_authority == NULL)
731 goto fail;
732 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
733 conn->proxy_authority_len = tlv_len;
734 break;
735 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100736 default:
737 break;
738 }
739 }
740 }
741
Willy Tarreau77992672014-06-14 11:06:17 +0200742 /* unsupported protocol, keep local connection address */
743 break;
744 case 0x00: /* LOCAL command */
745 /* keep local connection address for LOCAL */
746 break;
747 default:
748 goto bad_header; /* not a supported command */
749 }
750
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200752 goto eat_header;
753
754 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200755 /* remove the PROXY line from the request. For this we re-read the
756 * exact line at once. If we don't get the exact same result, we
757 * fail.
758 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200759 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200761 if (len2 < 0 && errno == EINTR)
762 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200763 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100764 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200765 } while (0);
766
767 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100768 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200769 return 1;
770
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200771 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200772 return 0;
773
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200774 missing:
775 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
776 * we have not read anything. Otherwise we need to fail because we won't
777 * be able to poll anymore.
778 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100779 conn->err_code = CO_ER_PRX_TRUNCATED;
780 goto fail;
781
782 bad_header:
783 /* This is not a valid proxy protocol header */
784 conn->err_code = CO_ER_PRX_BAD_HDR;
785 goto fail;
786
787 recv_abort:
788 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100789 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100790 goto fail;
791
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200792 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200793 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200794 return 0;
795}
796
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100797/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000798 * at the beginning of the raw data stream. The header format is
799 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100800 *
801 * This line MUST be at the beginning of the buffer and MUST NOT be
802 * fragmented.
803 *
804 * The header line is small and in all cases smaller than the smallest normal
805 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
806 * can safely use MSG_PEEK and avoid buffering.
807 *
808 * Once the data is fetched, the values are set in the connection's address
809 * fields, and data are removed from the socket's buffer. The function returns
810 * zero if it needs to wait for more data or if it fails, or 1 if it completed
811 * and removed itself.
812 */
813int conn_recv_netscaler_cip(struct connection *conn, int flag)
814{
815 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000816 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100817 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200818 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100819
820 /* we might have been called just after an asynchronous shutr */
821 if (conn->flags & CO_FL_SOCK_RD_SH)
822 goto fail;
823
824 if (!conn_ctrl_ready(conn))
825 goto fail;
826
Willy Tarreau585744b2017-08-24 14:31:19 +0200827 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200828 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100829
830 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200831 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
832 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100833 if (errno == EINTR)
834 continue;
835 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200836 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200837 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100838 }
839 goto recv_abort;
840 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200841 trash.data = ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100842 } while (0);
843
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200844 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100845 /* client shutdown */
846 conn->err_code = CO_ER_CIP_EMPTY;
847 goto fail;
848 }
849
850 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000851 * CIP magic, header length or
852 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100854 goto missing;
855
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200856 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100857
858 /* Decode a possible NetScaler Client IP request, fail early if
859 * it does not match */
Willy Tarreau55e0da62018-09-20 11:26:52 +0200860 if (ntohl(*(uint32_t *)line) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100861 goto bad_magic;
862
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000863 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200864 if ((trash.area[8] & 0xD0) == 0x40) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000865 hdr_len = ntohl(*(uint32_t *)(line+4));
866 line += 8;
867 }
868 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200869 else if (trash.area[8] == 0x00) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000870 hdr_len = ntohs(*(uint32_t *)(line+10));
871 line += 12;
872 }
873 /* Unknown CIP protocol */
874 else {
875 conn->err_code = CO_ER_CIP_BAD_PROTO;
876 goto fail;
877 }
878
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100879 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000880 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200881 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100882 goto missing;
883
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100884 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100885 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100886
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100887 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100888 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100889 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100890
891 hdr_ip4 = (struct ip *)line;
892
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200893 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100894 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000895 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100896 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000897 }
898 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100899 /* The protocol does not include a TCP header */
900 conn->err_code = CO_ER_CIP_BAD_PROTO;
901 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000902 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100903
David Carlier3015a2e2016-07-04 22:51:33 +0100904 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100905
906 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200907 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
908 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
909 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100910
Willy Tarreau226572f2019-07-17 14:46:00 +0200911 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
912 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
913 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100914
915 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
916 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100917 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100918 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100919 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100920
921 hdr_ip6 = (struct ip6_hdr *)line;
922
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200923 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100924 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000925 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100926 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000927 }
928 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100929 /* The protocol does not include a TCP header */
930 conn->err_code = CO_ER_CIP_BAD_PROTO;
931 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000932 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100933
David Carlier3015a2e2016-07-04 22:51:33 +0100934 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100935
936 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200937 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
938 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
939 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100940
Willy Tarreau226572f2019-07-17 14:46:00 +0200941 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
942 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
943 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100944
945 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
946 }
947 else {
948 /* The protocol does not match something known (IPv4/IPv6) */
949 conn->err_code = CO_ER_CIP_BAD_PROTO;
950 goto fail;
951 }
952
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000953 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200954 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100955
956 /* remove the NetScaler Client IP header from the request. For this
957 * we re-read the exact line at once. If we don't get the exact same
958 * result, we fail.
959 */
960 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200961 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100962 if (len2 < 0 && errno == EINTR)
963 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200964 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100965 goto recv_abort;
966 } while (0);
967
968 conn->flags &= ~flag;
969 return 1;
970
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200971 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200972 return 0;
973
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100974 missing:
975 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
976 * we have not read anything. Otherwise we need to fail because we won't
977 * be able to poll anymore.
978 */
979 conn->err_code = CO_ER_CIP_TRUNCATED;
980 goto fail;
981
982 bad_magic:
983 conn->err_code = CO_ER_CIP_BAD_MAGIC;
984 goto fail;
985
986 recv_abort:
987 conn->err_code = CO_ER_CIP_ABORT;
988 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
989 goto fail;
990
991 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100992 conn->flags |= CO_FL_ERROR;
993 return 0;
994}
995
Alexander Liu2a54bb72019-05-22 19:44:48 +0800996
997int conn_send_socks4_proxy_request(struct connection *conn)
998{
999 struct socks4_request req_line;
1000
1001 /* we might have been called just after an asynchronous shutw */
1002 if (conn->flags & CO_FL_SOCK_WR_SH)
1003 goto out_error;
1004
1005 if (!conn_ctrl_ready(conn))
1006 goto out_error;
1007
Willy Tarreau226572f2019-07-17 14:46:00 +02001008 if (!conn_get_dst(conn))
1009 goto out_error;
1010
Alexander Liu2a54bb72019-05-22 19:44:48 +08001011 req_line.version = 0x04;
1012 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001013 req_line.port = get_net_port(conn->dst);
1014 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001015 memcpy(req_line.user_id, "HAProxy\0", 8);
1016
1017 if (conn->send_proxy_ofs > 0) {
1018 /*
1019 * This is the first call to send the request
1020 */
1021 conn->send_proxy_ofs = -(int)sizeof(req_line);
1022 }
1023
1024 if (conn->send_proxy_ofs < 0) {
1025 int ret = 0;
1026
1027 /* we are sending the socks4_req_line here. If the data layer
1028 * has a pending write, we'll also set MSG_MORE.
1029 */
1030 ret = conn_sock_send(
1031 conn,
1032 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1033 -conn->send_proxy_ofs,
1034 (conn->flags & CO_FL_XPRT_WR_ENA) ? MSG_MORE : 0);
1035
1036 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1037 conn->handle.fd, -conn->send_proxy_ofs, ret);
1038
1039 if (ret < 0) {
1040 goto out_error;
1041 }
1042
1043 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1044 if (conn->send_proxy_ofs != 0) {
1045 goto out_wait;
1046 }
1047 }
1048
1049 /* OK we've the whole request sent */
1050 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001051
1052 /* The connection is ready now, simply return and let the connection
1053 * handler notify upper layers if needed.
1054 */
1055 if (conn->flags & CO_FL_WAIT_L4_CONN)
1056 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1057
1058 if (conn->flags & CO_FL_SEND_PROXY) {
1059 /*
1060 * Get the send_proxy_ofs ready for the send_proxy due to we are
1061 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1062 * before sending PROXY Protocol.
1063 */
1064 conn->send_proxy_ofs = 1;
1065 }
1066 return 1;
1067
1068 out_error:
1069 /* Write error on the file descriptor */
1070 conn->flags |= CO_FL_ERROR;
1071 if (conn->err_code == CO_ER_NONE) {
1072 conn->err_code = CO_ER_SOCKS4_SEND;
1073 }
1074 return 0;
1075
1076 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001077 return 0;
1078}
1079
1080int conn_recv_socks4_proxy_response(struct connection *conn)
1081{
1082 char line[SOCKS4_HS_RSP_LEN];
1083 int ret;
1084
1085 /* we might have been called just after an asynchronous shutr */
1086 if (conn->flags & CO_FL_SOCK_RD_SH)
1087 goto fail;
1088
1089 if (!conn_ctrl_ready(conn))
1090 goto fail;
1091
1092 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001093 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001094
1095 do {
1096 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1097 * Try to peek into it, before all 8 bytes ready.
1098 */
1099 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1100
1101 if (ret == 0) {
1102 /* the socket has been closed or shutdown for send */
1103 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1104 conn->handle.fd, ret, errno);
1105 if (conn->err_code == CO_ER_NONE) {
1106 conn->err_code = CO_ER_SOCKS4_RECV;
1107 }
1108 goto fail;
1109 }
1110
1111 if (ret > 0) {
1112 if (ret == SOCKS4_HS_RSP_LEN) {
1113 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1114 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1115 }else{
1116 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]);
1117 }
1118 } else {
1119 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1120 }
1121
1122 if (ret < 0) {
1123 if (errno == EINTR) {
1124 continue;
1125 }
1126 if (errno == EAGAIN) {
1127 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001128 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001129 }
1130 goto recv_abort;
1131 }
1132 } while (0);
1133
1134 if (ret < SOCKS4_HS_RSP_LEN) {
1135 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1136 * we are not able to read enough data.
1137 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001138 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001139 }
1140
1141 /*
1142 * Base on the SOCSK4 protocol:
1143 *
1144 * +----+----+----+----+----+----+----+----+
1145 * | VN | CD | DSTPORT | DSTIP |
1146 * +----+----+----+----+----+----+----+----+
1147 * # of bytes: 1 1 2 4
1148 * VN is the version of the reply code and should be 0. CD is the result
1149 * code with one of the following values:
1150 * 90: request granted
1151 * 91: request rejected or failed
1152 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1153 * 93: request rejected because the client program and identd report different user-ids
1154 * The remaining fields are ignored.
1155 */
1156 if (line[1] != 90) {
1157 conn->flags &= ~CO_FL_SOCKS4_RECV;
1158
1159 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1160 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1161 if (conn->err_code == CO_ER_NONE) {
1162 conn->err_code = CO_ER_SOCKS4_DENY;
1163 }
1164 goto fail;
1165 }
1166
1167 /* remove the 8 bytes response from the stream */
1168 do {
1169 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1170 if (ret < 0 && errno == EINTR) {
1171 continue;
1172 }
1173 if (ret != SOCKS4_HS_RSP_LEN) {
1174 if (conn->err_code == CO_ER_NONE) {
1175 conn->err_code = CO_ER_SOCKS4_RECV;
1176 }
1177 goto fail;
1178 }
1179 } while (0);
1180
1181 conn->flags &= ~CO_FL_SOCKS4_RECV;
1182 return 1;
1183
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001184 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001185 return 0;
1186
Alexander Liu2a54bb72019-05-22 19:44:48 +08001187 recv_abort:
1188 if (conn->err_code == CO_ER_NONE) {
1189 conn->err_code = CO_ER_SOCKS4_ABORT;
1190 }
1191 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1192 goto fail;
1193
1194 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001195 conn->flags |= CO_FL_ERROR;
1196 return 0;
1197}
1198
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001199/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001200int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1201{
1202 int ret = 0;
1203
1204 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1205 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1206 }
1207 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001208 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1209 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001210 else
1211 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1212 }
1213
1214 return ret;
1215}
1216
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001217/* Makes a PROXY protocol line from the two addresses. The output is sent to
1218 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1219 * It returns the number of bytes composing this line (including the trailing
1220 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001221 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1222 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001223 */
David Safb76832014-05-08 23:42:08 -04001224int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001225{
1226 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001227 char * protocol;
1228 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1229 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1230 in_port_t src_port;
1231 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001232
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001233 if ( !src
1234 || !dst
1235 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1236 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1237 /* unknown family combination */
1238 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001239 if (ret >= buf_len)
1240 return 0;
1241
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001242 return ret;
1243 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001244
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001245 /* IPv4 for both src and dst */
1246 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1247 protocol = "TCP4";
1248 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001249 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001250 src_port = ((struct sockaddr_in *)src)->sin_port;
1251 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001252 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001253 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001254 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001255 /* IPv6 for at least one of src and dst */
1256 else {
1257 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001258
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001259 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001260
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001261 if (src->ss_family == AF_INET) {
1262 /* Convert src to IPv6 */
1263 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1264 src_port = ((struct sockaddr_in *)src)->sin_port;
1265 }
1266 else {
1267 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1268 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1269 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001270
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001271 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001272 return 0;
1273
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001274 if (dst->ss_family == AF_INET) {
1275 /* Convert dst to IPv6 */
1276 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1277 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1278 }
1279 else {
1280 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1281 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1282 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001283
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001284 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001285 return 0;
1286 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001287
1288 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1289 if (ret >= buf_len)
1290 return 0;
1291
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001292 return ret;
1293}
David Safb76832014-05-08 23:42:08 -04001294
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001295static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001296{
1297 struct tlv *tlv;
1298
1299 if (!dest || (length + sizeof(*tlv) > dest_len))
1300 return 0;
1301
1302 tlv = (struct tlv *)dest;
1303
1304 tlv->type = type;
1305 tlv->length_hi = length >> 8;
1306 tlv->length_lo = length & 0x00ff;
1307 memcpy(tlv->value, value, length);
1308 return length + sizeof(*tlv);
1309}
David Safb76832014-05-08 23:42:08 -04001310
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001311/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001312int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1313{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001314 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001315 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001316 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001317 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001318 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001319 struct sockaddr_storage *src = &null_addr;
1320 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001321 const char *value;
1322 int value_len;
David Safb76832014-05-08 23:42:08 -04001323
1324 if (buf_len < PP2_HEADER_LEN)
1325 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001326 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001327
Willy Tarreau226572f2019-07-17 14:46:00 +02001328 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1329 src = remote->src;
1330 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001331 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001332
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001333 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1334 if ( !src
1335 || !dst
1336 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1337 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001338 if (buf_len < PP2_HDR_LEN_UNSPEC)
1339 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001340 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1341 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001342 ret = PP2_HDR_LEN_UNSPEC;
1343 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001344 else {
1345 /* IPv4 for both src and dst */
1346 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1347 if (buf_len < PP2_HDR_LEN_INET)
1348 return 0;
1349 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1350 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1351 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1352 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1353 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1354 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1355 ret = PP2_HDR_LEN_INET;
1356 }
1357 /* IPv6 for at least one of src and dst */
1358 else {
1359 struct in6_addr tmp;
1360
1361 if (buf_len < PP2_HDR_LEN_INET6)
1362 return 0;
1363 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1364 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1365 if (src->ss_family == AF_INET) {
1366 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1367 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1368 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1369 }
1370 else {
1371 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1372 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1373 }
1374 if (dst->ss_family == AF_INET) {
1375 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1376 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
1377 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1378 }
1379 else {
1380 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1381 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1382 }
1383
1384 ret = PP2_HDR_LEN_INET6;
1385 }
1386 }
David Safb76832014-05-08 23:42:08 -04001387
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001388 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1389 uint32_t zero_crc32c = 0;
1390 if ((buf_len - ret) < sizeof(struct tlv))
1391 return 0;
1392 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1393 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1394 }
1395
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001396 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001397 if ((buf_len - ret) < sizeof(struct tlv))
1398 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001399 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001400 }
1401
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001402 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001403 value = NULL;
1404 if (remote && remote->proxy_authority) {
1405 value = remote->proxy_authority;
1406 value_len = remote->proxy_authority_len;
1407 }
1408#ifdef USE_OPENSSL
1409 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001410 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001411 value_len = strlen(value);
1412 }
1413#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001414 if (value) {
1415 if ((buf_len - ret) < sizeof(struct tlv))
1416 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001417 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001418 }
1419 }
1420
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001421#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001422 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001423 struct tlv_ssl *tlv;
1424 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001425 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1426 return 0;
1427 tlv = (struct tlv_ssl *)&buf[ret];
1428 memset(tlv, 0, sizeof(struct tlv_ssl));
1429 ssl_tlv_len += sizeof(struct tlv_ssl);
1430 tlv->tlv.type = PP2_TYPE_SSL;
1431 if (ssl_sock_is_ssl(remote)) {
1432 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001433 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001434 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001435 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 -04001436 }
Dave McCowan328fb582014-07-30 10:39:13 -04001437 if (ssl_sock_get_cert_used_sess(remote)) {
1438 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001439 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001440 if (ssl_sock_get_cert_used_conn(remote))
1441 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001442 }
1443 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001444 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001445 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001446 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1447 cn_trash->data,
1448 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001449 }
1450 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001451 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001452 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001453 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001454 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1455 pkey_trash->data,
1456 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001457 }
1458 }
1459 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1460 value = ssl_sock_get_cert_sig(remote);
1461 if (value) {
1462 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1463 }
1464 }
1465 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1466 value = ssl_sock_get_cipher_name(remote);
1467 if (value) {
1468 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1469 }
1470 }
David Safb76832014-05-08 23:42:08 -04001471 }
1472 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1473 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1474 ret += ssl_tlv_len;
1475 }
1476#endif
1477
Willy Tarreaue5733232019-05-22 19:24:06 +02001478#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001479 if (remote && (remote->proxy_netns)) {
1480 if ((buf_len - ret) < sizeof(struct tlv))
1481 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001482 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 +01001483 }
1484#endif
1485
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001486 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001487
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001488 if (tlv_crc32c_p) {
1489 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1490 }
1491
David Safb76832014-05-08 23:42:08 -04001492 return ret;
1493}
Emeric Brun4f603012017-01-05 15:11:44 +01001494
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001495/* return the major HTTP version as 1 or 2 depending on how the request arrived
1496 * before being processed.
1497 */
1498static int
1499smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1500{
Jérôme Magnin86577422018-12-07 09:03:11 +01001501 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1502 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001503
1504 smp->data.type = SMP_T_SINT;
1505 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1506 return 1;
1507}
1508
Emeric Brun4f603012017-01-05 15:11:44 +01001509/* fetch if the received connection used a PROXY protocol header */
1510int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1511{
1512 struct connection *conn;
1513
1514 conn = objt_conn(smp->sess->origin);
1515 if (!conn)
1516 return 0;
1517
1518 if (!(conn->flags & CO_FL_CONNECTED)) {
1519 smp->flags |= SMP_F_MAY_CHANGE;
1520 return 0;
1521 }
1522
1523 smp->flags = 0;
1524 smp->data.type = SMP_T_BOOL;
1525 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1526
1527 return 1;
1528}
1529
Geoff Simmons7185b782019-08-27 18:31:16 +02001530/* fetch the authority TLV from a PROXY protocol header */
1531int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1532{
1533 struct connection *conn;
1534
1535 conn = objt_conn(smp->sess->origin);
1536 if (!conn)
1537 return 0;
1538
1539 if (!(conn->flags & CO_FL_CONNECTED)) {
1540 smp->flags |= SMP_F_MAY_CHANGE;
1541 return 0;
1542 }
1543
1544 if (conn->proxy_authority == NULL)
1545 return 0;
1546
1547 smp->flags = 0;
1548 smp->data.type = SMP_T_STR;
1549 smp->data.u.str.area = conn->proxy_authority;
1550 smp->data.u.str.data = conn->proxy_authority_len;
1551
1552 return 1;
1553}
1554
Emeric Brun4f603012017-01-05 15:11:44 +01001555/* Note: must not be declared <const> as its list will be overwritten.
1556 * Note: fetches that may return multiple types must be declared as the lowest
1557 * common denominator, the type that can be casted into all other ones. For
1558 * instance v4/v6 must be declared v4.
1559 */
1560static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001561 { "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 +01001562 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001563 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001564 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001565 { /* END */ },
1566}};
1567
Willy Tarreau0108d902018-11-25 19:14:37 +01001568INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);