blob: c0aac0aaa03997caf59ebc56cb66fdd70b4222c7 [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
74 /* Verify if the connection just established. */
75 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
76 conn->flags |= CO_FL_CONNECTED;
77
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +020078 /* The connection owner might want to be notified about an end of
79 * handshake indicating the connection is ready, before we proceed with
80 * any data exchange. The callback may fail and cause the connection to
81 * be destroyed, thus we must not use it anymore and should immediately
82 * leave instead. The caller must immediately unregister itself once
83 * called.
Willy Tarreau2542b532012-08-31 16:01:23 +020084 */
Olivier Houchardfe50bfb2019-05-27 12:09:19 +020085 if (!(conn->flags & CO_FL_HANDSHAKE) &&
Olivier Houchardea8dd942019-05-20 14:02:16 +020086 conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +020087 return;
Willy Tarreau2542b532012-08-31 16:01:23 +020088
Willy Tarreau8081abe2019-11-28 18:08:49 +010089 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010090 /* force reporting of activity by clearing the previous flags :
91 * we'll have at least ERROR or CONNECTED at the end of an I/O,
92 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020093 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +010094 flags = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +020095 if (conn->send_wait != NULL) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +010096 conn->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +020097 tasklet_wakeup(conn->send_wait->tasklet);
Olivier Houchardfa8aa862018-10-10 18:25:41 +020098 conn->send_wait = NULL;
99 } else
100 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200101 __conn_xprt_stop_send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200102 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200103
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200104 /* The data transfer starts here and stops on error and handshakes. Note
105 * that we must absolutely test conn->xprt at each step in case it suddenly
106 * changes due to a quick unexpected close().
107 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100108 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100109 /* force reporting of activity by clearing the previous flags :
110 * we'll have at least ERROR or CONNECTED at the end of an I/O,
111 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200112 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100113 flags = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200114 if (conn->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100115 conn->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200116 tasklet_wakeup(conn->recv_wait->tasklet);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200117 conn->recv_wait = NULL;
118 } else
119 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200120 __conn_xprt_stop_recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200121 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200122
Willy Tarreau2c6be842012-07-06 17:12:34 +0200123 leave:
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200124 /* The connection owner might want to be notified about failures to
125 * complete the handshake. The callback may fail and cause the
126 * connection to be destroyed, thus we must not use it anymore and
127 * should immediately leave instead. The caller must immediately
128 * unregister itself once called.
129 */
130 if (((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) &&
131 conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0)
132 return;
133
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100134 /* The wake callback is normally used to notify the data layer about
135 * data layer activity (successful send/recv), connection establishment,
136 * shutdown and fatal errors. We need to consider the following
137 * situations to wake up the data layer :
138 * - change among the CO_FL_NOTIFY_DATA flags :
139 * {DATA,SOCK}_{RD,WR}_SH, ERROR,
140 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
141 * end of handshake and transition to CONNECTED
142 * - raise of CONNECTED with HANDSHAKE down
143 * - end of HANDSHAKE with CONNECTED set
144 * - regular data layer activity
145 *
146 * Note that the wake callback is allowed to release the connection and
147 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200148 */
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200149 if ((io_available || (((conn->flags ^ flags) & CO_FL_NOTIFY_DATA) ||
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100150 ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED &&
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200151 (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200152 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200153 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200154
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200155 /* commit polling changes */
Willy Tarreau916e12d2017-10-25 09:22:43 +0200156 conn->flags &= ~CO_FL_WILL_UPDATE;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200157 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200158 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200159}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200160
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200161/* Update polling on connection <c>'s file descriptor depending on its current
162 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200163 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_XPRT_*.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200164 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200165 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200166 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200167void conn_update_xprt_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200170
Willy Tarreau3c728722014-01-23 13:50:42 +0100171 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200172 return;
173
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200174 /* update read status if needed */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200175 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 +0200176 fd_want_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100177 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200178 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200179 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 +0200180 fd_stop_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100181 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200182 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200183
184 /* update write status if needed */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200185 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 +0200186 fd_want_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100187 f |= CO_FL_CURR_WR_ENA;
188 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200189 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 +0200190 fd_stop_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100191 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100193 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200194}
195
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100196/* This is the callback which is set when a connection establishment is pending
197 * and we have nothing to send. It may update the FD polling status to indicate
198 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
199 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
200 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
201 * leaves the error code in errno.
202 */
203int conn_fd_check(struct connection *conn)
204{
205 struct sockaddr_storage *addr;
206 int fd = conn->handle.fd;
207
208 if (conn->flags & CO_FL_ERROR)
209 return 0;
210
211 if (!conn_ctrl_ready(conn))
212 return 0;
213
214 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
215 return 1; /* strange we were called while ready */
216
217 if (!fd_send_ready(fd))
218 return 0;
219
220 /* Here we have 2 cases :
221 * - modern pollers, able to report ERR/HUP. If these ones return any
222 * of these flags then it's likely a failure, otherwise it possibly
223 * is a success (i.e. there may have been data received just before
224 * the error was reported).
225 * - select, which doesn't report these and with which it's always
226 * necessary either to try connect() again or to check for SO_ERROR.
227 * In order to simplify everything, we double-check using connect() as
228 * soon as we meet either of these delicate situations. Note that
229 * SO_ERROR would clear the error after reporting it!
230 */
231 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
232 /* modern poller, able to report ERR/HUP */
233 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
234 goto done;
235 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
236 goto done;
237 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
238 goto wait;
239 /* error present, fall through common error check path */
240 }
241
242 /* Use connect() to check the state of the socket. This has the double
243 * advantage of *not* clearing the error (so that health checks can
244 * still use getsockopt(SO_ERROR)) and giving us the following info :
245 * - error
246 * - connecting (EALREADY, EINPROGRESS)
247 * - connected (EISCONN, 0)
248 */
249 addr = conn->dst;
250 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
251 addr = &objt_server(conn->target)->socks4_addr;
252
253 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
254 if (errno == EALREADY || errno == EINPROGRESS)
255 goto wait;
256
257 if (errno && errno != EISCONN)
258 goto out_error;
259 }
260
261 done:
262 /* The FD is ready now, we'll mark the connection as complete and
263 * forward the event to the transport layer which will notify the
264 * data layer.
265 */
266 conn->flags &= ~CO_FL_WAIT_L4_CONN;
267 fd_may_send(fd);
268 fd_cond_recv(fd);
269 errno = 0; // make health checks happy
270 return 1;
271
272 out_error:
273 /* Write error on the file descriptor. Report it to the connection
274 * and disable polling on this FD.
275 */
276 fdtab[fd].linger_risk = 0;
277 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
278 __conn_xprt_stop_both(conn);
279 return 0;
280
281 wait:
282 __conn_xprt_want_send(conn);
283 fd_cant_send(fd);
284 return 0;
285}
286
Willy Tarreauff3e6482015-03-12 23:56:52 +0100287/* Send a message over an established connection. It makes use of send() and
288 * returns the same return code and errno. If the socket layer is not ready yet
289 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
290 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
291 * EMSGSIZE if called with a zero length message. The purpose is to simplify
292 * some rare attempts to directly write on the socket from above the connection
293 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
294 * It automatically retries on EINTR. Other errors cause the connection to be
295 * marked as in error state. It takes similar arguments as send() except the
296 * first one which is the connection instead of the file descriptor. Note,
297 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
298 */
299int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
300{
301 int ret;
302
303 ret = -1;
304 errno = ENOTSOCK;
305
306 if (conn->flags & CO_FL_SOCK_WR_SH)
307 goto fail;
308
309 if (!conn_ctrl_ready(conn))
310 goto fail;
311
312 errno = EMSGSIZE;
313 if (!len)
314 goto fail;
315
Willy Tarreau585744b2017-08-24 14:31:19 +0200316 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100317 goto wait;
318
319 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200320 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100321 } while (ret < 0 && errno == EINTR);
322
323
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200324 if (ret > 0) {
325 if (conn->flags & CO_FL_WAIT_L4_CONN) {
326 conn->flags &= ~CO_FL_WAIT_L4_CONN;
327 fd_may_send(conn->handle.fd);
328 fd_cond_recv(conn->handle.fd);
329 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100330 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200331 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100332
333 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
334 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200335 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100336 return 0;
337 }
338 fail:
339 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
340 return ret;
341}
342
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100343int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200344{
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200345 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200346
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100347 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200348 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200349 BUG_ON(conn->recv_wait != sw);
350 conn->recv_wait = NULL;
351 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchard53216e72018-10-10 15:46:36 +0200352 __conn_xprt_stop_recv(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200353 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100354 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200355 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200356 BUG_ON(conn->send_wait != sw);
357 conn->send_wait = NULL;
358 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard53216e72018-10-10 15:46:36 +0200359 __conn_xprt_stop_send(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200360 }
Olivier Houchard53216e72018-10-10 15:46:36 +0200361 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200362 return 0;
363}
364
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100365int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200366{
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200367 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200368
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100369 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +0200370 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200371 BUG_ON(conn->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
372 sw->events |= SUB_RETRY_RECV;
373 conn->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100374 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard53216e72018-10-10 15:46:36 +0200375 __conn_xprt_want_recv(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200376 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100377 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +0200378 sw = param;
Olivier Houchard35d11682019-05-14 18:02:47 +0200379 BUG_ON(conn->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
380 sw->events |= SUB_RETRY_SEND;
381 conn->send_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100382 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard53216e72018-10-10 15:46:36 +0200383 __conn_xprt_want_send(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200384 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200385 if (event_type != 0)
386 return (-1);
Olivier Houchard53216e72018-10-10 15:46:36 +0200387 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200388 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200389}
390
Willy Tarreaud85c4852015-03-13 00:40:28 +0100391/* Drains possibly pending incoming data on the file descriptor attached to the
392 * connection and update the connection's flags accordingly. This is used to
393 * know whether we need to disable lingering on close. Returns non-zero if it
394 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
395 * flag may also be updated if the incoming shutdown was reported by the drain()
396 * function.
397 */
398int conn_sock_drain(struct connection *conn)
399{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200400 int turns = 2;
401 int len;
402
Willy Tarreaud85c4852015-03-13 00:40:28 +0100403 if (!conn_ctrl_ready(conn))
404 return 1;
405
406 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
407 return 1;
408
Willy Tarreaue215bba2018-08-24 14:31:53 +0200409 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
410 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100411
Willy Tarreaue215bba2018-08-24 14:31:53 +0200412 if (!fd_recv_ready(conn->handle.fd))
413 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100414
Willy Tarreaue215bba2018-08-24 14:31:53 +0200415 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200416 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100417 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200418 goto shut;
419 }
420
421 /* no drain function defined, use the generic one */
422
423 while (turns) {
424#ifdef MSG_TRUNC_CLEARS_INPUT
425 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
426 if (len == -1 && errno == EFAULT)
427#endif
428 len = recv(conn->handle.fd, trash.area, trash.size,
429 MSG_DONTWAIT | MSG_NOSIGNAL);
430
431 if (len == 0)
432 goto shut;
433
434 if (len < 0) {
435 if (errno == EAGAIN) {
436 /* connection not closed yet */
437 fd_cant_recv(conn->handle.fd);
438 break;
439 }
440 if (errno == EINTR) /* oops, try again */
441 continue;
442 /* other errors indicate a dead connection, fine. */
443 goto shut;
444 }
445 /* OK we read some data, let's try again once */
446 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100447 }
448
Willy Tarreaue215bba2018-08-24 14:31:53 +0200449 /* some data are still present, give up */
450 return 0;
451
452 shut:
453 /* we're certain the connection was shut down */
454 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100455 conn->flags |= CO_FL_SOCK_RD_SH;
456 return 1;
457}
458
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100459/*
460 * Get data length from tlv
461 */
462static int get_tlv_length(const struct tlv *src)
463{
464 return (src->length_hi << 8) | src->length_lo;
465}
466
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200467/* This handshake handler waits a PROXY protocol header at the beginning of the
468 * raw data stream. The header looks like this :
469 *
470 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
471 *
472 * There must be exactly one space between each field. Fields are :
473 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
474 * - SRC3 : layer 3 (eg: IP) source address in standard text form
475 * - DST3 : layer 3 (eg: IP) destination address in standard text form
476 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
477 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
478 *
479 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
480 *
481 * The header line is small and in all cases smaller than the smallest normal
482 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
483 * can safely use MSG_PEEK and avoid buffering.
484 *
485 * Once the data is fetched, the values are set in the connection's address
486 * fields, and data are removed from the socket's buffer. The function returns
487 * zero if it needs to wait for more data or if it fails, or 1 if it completed
488 * and removed itself.
489 */
490int conn_recv_proxy(struct connection *conn, int flag)
491{
492 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200493 struct proxy_hdr_v2 *hdr_v2;
494 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100495 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200496 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200497 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200498
499 /* we might have been called just after an asynchronous shutr */
500 if (conn->flags & CO_FL_SOCK_RD_SH)
501 goto fail;
502
Willy Tarreau3c728722014-01-23 13:50:42 +0100503 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200504 goto fail;
505
Willy Tarreauca79f592019-07-17 19:04:47 +0200506 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
507 goto fail;
508
Willy Tarreau585744b2017-08-24 14:31:19 +0200509 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200510 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100511
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200512 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200513 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
514 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200515 if (errno == EINTR)
516 continue;
517 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200518 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200519 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200520 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100521 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200522 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200523 trash.data = ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200524 } while (0);
525
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200526 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100527 /* client shutdown */
528 conn->err_code = CO_ER_PRX_EMPTY;
529 goto fail;
530 }
531
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200533 goto missing;
534
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200535 line = trash.area;
536 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537
538 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200539 if (strncmp(line, "PROXY ", 6) != 0)
540 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200541
542 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200544 goto missing;
545
David CARLIER42ff05e2016-03-24 09:22:36 +0000546 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200547 u32 src3, dst3, sport, dport;
548
549 line += 5;
550
551 src3 = inetaddr_host_lim_ret(line, end, &line);
552 if (line == end)
553 goto missing;
554 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100555 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200556
557 dst3 = inetaddr_host_lim_ret(line, end, &line);
558 if (line == end)
559 goto missing;
560 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100561 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200562
563 sport = read_uint((const char **)&line, end);
564 if (line == end)
565 goto missing;
566 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100567 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200568
569 dport = read_uint((const char **)&line, end);
570 if (line > end - 2)
571 goto missing;
572 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100573 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200574 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100575 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200576
577 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200578 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
579 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
580 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200581
Willy Tarreau226572f2019-07-17 14:46:00 +0200582 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
583 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
584 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200585 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
586 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000587 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200588 u32 sport, dport;
589 char *src_s;
590 char *dst_s, *sport_s, *dport_s;
591 struct in6_addr src3, dst3;
592
593 line += 5;
594
595 src_s = line;
596 dst_s = sport_s = dport_s = NULL;
597 while (1) {
598 if (line > end - 2) {
599 goto missing;
600 }
601 else if (*line == '\r') {
602 *line = 0;
603 line++;
604 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100605 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200606 break;
607 }
608
609 if (*line == ' ') {
610 *line = 0;
611 if (!dst_s)
612 dst_s = line + 1;
613 else if (!sport_s)
614 sport_s = line + 1;
615 else if (!dport_s)
616 dport_s = line + 1;
617 }
618 line++;
619 }
620
621 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100622 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623
624 sport = read_uint((const char **)&sport_s,dport_s - 1);
625 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100626 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627
628 dport = read_uint((const char **)&dport_s,line - 2);
629 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100630 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200631
632 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100633 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200634
635 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100636 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200637
638 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200639 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
640 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
641 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200642
Willy Tarreau226572f2019-07-17 14:46:00 +0200643 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
644 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
645 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200646 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
647 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200648 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
649 /* This can be a UNIX socket forwarded by an haproxy upstream */
650 line += 9;
651 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200652 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200653 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100654 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200655 goto fail;
656 }
657
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200659 goto eat_header;
660
661 not_v1:
662 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200663 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200664 goto missing;
665
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200666 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200667
668 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
669 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
670 conn->err_code = CO_ER_PRX_NOT_HDR;
671 goto fail;
672 }
673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200675 goto missing;
676
677 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
678 case 0x01: /* PROXY command */
679 switch (hdr_v2->fam) {
680 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100681 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
682 goto bad_header;
683
Willy Tarreau226572f2019-07-17 14:46:00 +0200684 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
685 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
686 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
687 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
688 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
689 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200690 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200691 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100692 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200693 break;
694 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100695 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
696 goto bad_header;
697
Willy Tarreau226572f2019-07-17 14:46:00 +0200698 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
699 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
700 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
701 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
702 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
703 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200704 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200705 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100706 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200707 break;
708 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100709
710 /* TLV parsing */
711 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
713 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100714 const int tlv_len = get_tlv_length(tlv_packet);
715 tlv_offset += tlv_len + TLV_HEADER_SIZE;
716
717 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100718 case PP2_TYPE_CRC32C: {
719 void *tlv_crc32c_p = (void *)tlv_packet->value;
720 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
721 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100723 goto bad_header;
724 break;
725 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200726#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100727 case PP2_TYPE_NETNS: {
728 const struct netns_entry *ns;
729 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
730 if (ns)
731 conn->proxy_netns = ns;
732 break;
733 }
734#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200735 case PP2_TYPE_AUTHORITY: {
736 if (tlv_len > PP2_AUTHORITY_MAX)
737 goto bad_header;
738 conn->proxy_authority = pool_alloc(pool_head_authority);
739 if (conn->proxy_authority == NULL)
740 goto fail;
741 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
742 conn->proxy_authority_len = tlv_len;
743 break;
744 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100745 default:
746 break;
747 }
748 }
749 }
750
Willy Tarreau77992672014-06-14 11:06:17 +0200751 /* unsupported protocol, keep local connection address */
752 break;
753 case 0x00: /* LOCAL command */
754 /* keep local connection address for LOCAL */
755 break;
756 default:
757 goto bad_header; /* not a supported command */
758 }
759
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200761 goto eat_header;
762
763 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200764 /* remove the PROXY line from the request. For this we re-read the
765 * exact line at once. If we don't get the exact same result, we
766 * fail.
767 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200768 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200770 if (len2 < 0 && errno == EINTR)
771 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100773 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200774 } while (0);
775
776 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100777 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200778 return 1;
779
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200780 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200781 return 0;
782
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200783 missing:
784 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
785 * we have not read anything. Otherwise we need to fail because we won't
786 * be able to poll anymore.
787 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100788 conn->err_code = CO_ER_PRX_TRUNCATED;
789 goto fail;
790
791 bad_header:
792 /* This is not a valid proxy protocol header */
793 conn->err_code = CO_ER_PRX_BAD_HDR;
794 goto fail;
795
796 recv_abort:
797 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100798 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100799 goto fail;
800
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200801 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200802 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200803 return 0;
804}
805
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100806/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000807 * at the beginning of the raw data stream. The header format is
808 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100809 *
810 * This line MUST be at the beginning of the buffer and MUST NOT be
811 * fragmented.
812 *
813 * The header line is small and in all cases smaller than the smallest normal
814 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
815 * can safely use MSG_PEEK and avoid buffering.
816 *
817 * Once the data is fetched, the values are set in the connection's address
818 * fields, and data are removed from the socket's buffer. The function returns
819 * zero if it needs to wait for more data or if it fails, or 1 if it completed
820 * and removed itself.
821 */
822int conn_recv_netscaler_cip(struct connection *conn, int flag)
823{
824 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000825 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100826 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200827 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100828
829 /* we might have been called just after an asynchronous shutr */
830 if (conn->flags & CO_FL_SOCK_RD_SH)
831 goto fail;
832
833 if (!conn_ctrl_ready(conn))
834 goto fail;
835
Willy Tarreau585744b2017-08-24 14:31:19 +0200836 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200837 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100838
839 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200840 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
841 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100842 if (errno == EINTR)
843 continue;
844 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200845 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200846 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100847 }
848 goto recv_abort;
849 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200850 trash.data = ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100851 } while (0);
852
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100854 /* client shutdown */
855 conn->err_code = CO_ER_CIP_EMPTY;
856 goto fail;
857 }
858
859 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000860 * CIP magic, header length or
861 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200862 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100863 goto missing;
864
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200865 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100866
867 /* Decode a possible NetScaler Client IP request, fail early if
868 * it does not match */
Willy Tarreau55e0da62018-09-20 11:26:52 +0200869 if (ntohl(*(uint32_t *)line) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100870 goto bad_magic;
871
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000872 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200873 if ((trash.area[8] & 0xD0) == 0x40) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000874 hdr_len = ntohl(*(uint32_t *)(line+4));
875 line += 8;
876 }
877 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200878 else if (trash.area[8] == 0x00) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000879 hdr_len = ntohs(*(uint32_t *)(line+10));
880 line += 12;
881 }
882 /* Unknown CIP protocol */
883 else {
884 conn->err_code = CO_ER_CIP_BAD_PROTO;
885 goto fail;
886 }
887
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100888 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000889 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200890 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100891 goto missing;
892
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100893 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100894 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100895
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100896 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100897 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100898 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100899
900 hdr_ip4 = (struct ip *)line;
901
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200902 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100903 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000904 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100905 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000906 }
907 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100908 /* The protocol does not include a TCP header */
909 conn->err_code = CO_ER_CIP_BAD_PROTO;
910 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000911 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100912
David Carlier3015a2e2016-07-04 22:51:33 +0100913 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100914
915 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200916 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
917 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
918 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100919
Willy Tarreau226572f2019-07-17 14:46:00 +0200920 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
921 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
922 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100923
924 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
925 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100926 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100927 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100928 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100929
930 hdr_ip6 = (struct ip6_hdr *)line;
931
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200932 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100933 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000934 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100935 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000936 }
937 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100938 /* The protocol does not include a TCP header */
939 conn->err_code = CO_ER_CIP_BAD_PROTO;
940 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000941 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100942
David Carlier3015a2e2016-07-04 22:51:33 +0100943 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100944
945 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200946 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
947 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
948 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100949
Willy Tarreau226572f2019-07-17 14:46:00 +0200950 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
951 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
952 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100953
954 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
955 }
956 else {
957 /* The protocol does not match something known (IPv4/IPv6) */
958 conn->err_code = CO_ER_CIP_BAD_PROTO;
959 goto fail;
960 }
961
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000962 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200963 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100964
965 /* remove the NetScaler Client IP header from the request. For this
966 * we re-read the exact line at once. If we don't get the exact same
967 * result, we fail.
968 */
969 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200970 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100971 if (len2 < 0 && errno == EINTR)
972 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200973 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100974 goto recv_abort;
975 } while (0);
976
977 conn->flags &= ~flag;
978 return 1;
979
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200980 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200981 return 0;
982
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100983 missing:
984 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
985 * we have not read anything. Otherwise we need to fail because we won't
986 * be able to poll anymore.
987 */
988 conn->err_code = CO_ER_CIP_TRUNCATED;
989 goto fail;
990
991 bad_magic:
992 conn->err_code = CO_ER_CIP_BAD_MAGIC;
993 goto fail;
994
995 recv_abort:
996 conn->err_code = CO_ER_CIP_ABORT;
997 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
998 goto fail;
999
1000 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001001 conn->flags |= CO_FL_ERROR;
1002 return 0;
1003}
1004
Alexander Liu2a54bb72019-05-22 19:44:48 +08001005
1006int conn_send_socks4_proxy_request(struct connection *conn)
1007{
1008 struct socks4_request req_line;
1009
1010 /* we might have been called just after an asynchronous shutw */
1011 if (conn->flags & CO_FL_SOCK_WR_SH)
1012 goto out_error;
1013
1014 if (!conn_ctrl_ready(conn))
1015 goto out_error;
1016
Willy Tarreau226572f2019-07-17 14:46:00 +02001017 if (!conn_get_dst(conn))
1018 goto out_error;
1019
Alexander Liu2a54bb72019-05-22 19:44:48 +08001020 req_line.version = 0x04;
1021 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001022 req_line.port = get_net_port(conn->dst);
1023 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001024 memcpy(req_line.user_id, "HAProxy\0", 8);
1025
1026 if (conn->send_proxy_ofs > 0) {
1027 /*
1028 * This is the first call to send the request
1029 */
1030 conn->send_proxy_ofs = -(int)sizeof(req_line);
1031 }
1032
1033 if (conn->send_proxy_ofs < 0) {
1034 int ret = 0;
1035
1036 /* we are sending the socks4_req_line here. If the data layer
1037 * has a pending write, we'll also set MSG_MORE.
1038 */
1039 ret = conn_sock_send(
1040 conn,
1041 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1042 -conn->send_proxy_ofs,
1043 (conn->flags & CO_FL_XPRT_WR_ENA) ? MSG_MORE : 0);
1044
1045 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1046 conn->handle.fd, -conn->send_proxy_ofs, ret);
1047
1048 if (ret < 0) {
1049 goto out_error;
1050 }
1051
1052 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1053 if (conn->send_proxy_ofs != 0) {
1054 goto out_wait;
1055 }
1056 }
1057
1058 /* OK we've the whole request sent */
1059 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001060
1061 /* The connection is ready now, simply return and let the connection
1062 * handler notify upper layers if needed.
1063 */
1064 if (conn->flags & CO_FL_WAIT_L4_CONN)
1065 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1066
1067 if (conn->flags & CO_FL_SEND_PROXY) {
1068 /*
1069 * Get the send_proxy_ofs ready for the send_proxy due to we are
1070 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1071 * before sending PROXY Protocol.
1072 */
1073 conn->send_proxy_ofs = 1;
1074 }
1075 return 1;
1076
1077 out_error:
1078 /* Write error on the file descriptor */
1079 conn->flags |= CO_FL_ERROR;
1080 if (conn->err_code == CO_ER_NONE) {
1081 conn->err_code = CO_ER_SOCKS4_SEND;
1082 }
1083 return 0;
1084
1085 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001086 return 0;
1087}
1088
1089int conn_recv_socks4_proxy_response(struct connection *conn)
1090{
1091 char line[SOCKS4_HS_RSP_LEN];
1092 int ret;
1093
1094 /* we might have been called just after an asynchronous shutr */
1095 if (conn->flags & CO_FL_SOCK_RD_SH)
1096 goto fail;
1097
1098 if (!conn_ctrl_ready(conn))
1099 goto fail;
1100
1101 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001102 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001103
1104 do {
1105 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1106 * Try to peek into it, before all 8 bytes ready.
1107 */
1108 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1109
1110 if (ret == 0) {
1111 /* the socket has been closed or shutdown for send */
1112 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1113 conn->handle.fd, ret, errno);
1114 if (conn->err_code == CO_ER_NONE) {
1115 conn->err_code = CO_ER_SOCKS4_RECV;
1116 }
1117 goto fail;
1118 }
1119
1120 if (ret > 0) {
1121 if (ret == SOCKS4_HS_RSP_LEN) {
1122 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1123 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1124 }else{
1125 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]);
1126 }
1127 } else {
1128 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1129 }
1130
1131 if (ret < 0) {
1132 if (errno == EINTR) {
1133 continue;
1134 }
1135 if (errno == EAGAIN) {
1136 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001137 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001138 }
1139 goto recv_abort;
1140 }
1141 } while (0);
1142
1143 if (ret < SOCKS4_HS_RSP_LEN) {
1144 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1145 * we are not able to read enough data.
1146 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001147 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001148 }
1149
1150 /*
1151 * Base on the SOCSK4 protocol:
1152 *
1153 * +----+----+----+----+----+----+----+----+
1154 * | VN | CD | DSTPORT | DSTIP |
1155 * +----+----+----+----+----+----+----+----+
1156 * # of bytes: 1 1 2 4
1157 * VN is the version of the reply code and should be 0. CD is the result
1158 * code with one of the following values:
1159 * 90: request granted
1160 * 91: request rejected or failed
1161 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1162 * 93: request rejected because the client program and identd report different user-ids
1163 * The remaining fields are ignored.
1164 */
1165 if (line[1] != 90) {
1166 conn->flags &= ~CO_FL_SOCKS4_RECV;
1167
1168 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1169 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1170 if (conn->err_code == CO_ER_NONE) {
1171 conn->err_code = CO_ER_SOCKS4_DENY;
1172 }
1173 goto fail;
1174 }
1175
1176 /* remove the 8 bytes response from the stream */
1177 do {
1178 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1179 if (ret < 0 && errno == EINTR) {
1180 continue;
1181 }
1182 if (ret != SOCKS4_HS_RSP_LEN) {
1183 if (conn->err_code == CO_ER_NONE) {
1184 conn->err_code = CO_ER_SOCKS4_RECV;
1185 }
1186 goto fail;
1187 }
1188 } while (0);
1189
1190 conn->flags &= ~CO_FL_SOCKS4_RECV;
1191 return 1;
1192
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001193 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001194 return 0;
1195
Alexander Liu2a54bb72019-05-22 19:44:48 +08001196 recv_abort:
1197 if (conn->err_code == CO_ER_NONE) {
1198 conn->err_code = CO_ER_SOCKS4_ABORT;
1199 }
1200 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1201 goto fail;
1202
1203 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001204 conn->flags |= CO_FL_ERROR;
1205 return 0;
1206}
1207
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001208/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001209int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1210{
1211 int ret = 0;
1212
1213 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1214 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1215 }
1216 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001217 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1218 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001219 else
1220 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1221 }
1222
1223 return ret;
1224}
1225
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001226/* Makes a PROXY protocol line from the two addresses. The output is sent to
1227 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1228 * It returns the number of bytes composing this line (including the trailing
1229 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001230 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1231 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001232 */
David Safb76832014-05-08 23:42:08 -04001233int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001234{
1235 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001236 char * protocol;
1237 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1238 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1239 in_port_t src_port;
1240 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001241
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001242 if ( !src
1243 || !dst
1244 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1245 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1246 /* unknown family combination */
1247 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001248 if (ret >= buf_len)
1249 return 0;
1250
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001251 return ret;
1252 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001253
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001254 /* IPv4 for both src and dst */
1255 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1256 protocol = "TCP4";
1257 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001258 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001259 src_port = ((struct sockaddr_in *)src)->sin_port;
1260 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001261 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001262 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001263 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001264 /* IPv6 for at least one of src and dst */
1265 else {
1266 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001267
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001268 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001269
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001270 if (src->ss_family == AF_INET) {
1271 /* Convert src to IPv6 */
1272 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1273 src_port = ((struct sockaddr_in *)src)->sin_port;
1274 }
1275 else {
1276 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1277 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1278 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001279
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001280 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001281 return 0;
1282
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001283 if (dst->ss_family == AF_INET) {
1284 /* Convert dst to IPv6 */
1285 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1286 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1287 }
1288 else {
1289 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1290 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1291 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001292
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001293 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001294 return 0;
1295 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001296
1297 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1298 if (ret >= buf_len)
1299 return 0;
1300
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001301 return ret;
1302}
David Safb76832014-05-08 23:42:08 -04001303
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001304static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001305{
1306 struct tlv *tlv;
1307
1308 if (!dest || (length + sizeof(*tlv) > dest_len))
1309 return 0;
1310
1311 tlv = (struct tlv *)dest;
1312
1313 tlv->type = type;
1314 tlv->length_hi = length >> 8;
1315 tlv->length_lo = length & 0x00ff;
1316 memcpy(tlv->value, value, length);
1317 return length + sizeof(*tlv);
1318}
David Safb76832014-05-08 23:42:08 -04001319
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001320/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001321int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1322{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001323 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001324 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001325 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001326 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001327 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001328 struct sockaddr_storage *src = &null_addr;
1329 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001330 const char *value;
1331 int value_len;
David Safb76832014-05-08 23:42:08 -04001332
1333 if (buf_len < PP2_HEADER_LEN)
1334 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001335 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001336
Willy Tarreau226572f2019-07-17 14:46:00 +02001337 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1338 src = remote->src;
1339 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001340 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001341
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001342 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1343 if ( !src
1344 || !dst
1345 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1346 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001347 if (buf_len < PP2_HDR_LEN_UNSPEC)
1348 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001349 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1350 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001351 ret = PP2_HDR_LEN_UNSPEC;
1352 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001353 else {
1354 /* IPv4 for both src and dst */
1355 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1356 if (buf_len < PP2_HDR_LEN_INET)
1357 return 0;
1358 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1359 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1360 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1361 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1362 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1363 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1364 ret = PP2_HDR_LEN_INET;
1365 }
1366 /* IPv6 for at least one of src and dst */
1367 else {
1368 struct in6_addr tmp;
1369
1370 if (buf_len < PP2_HDR_LEN_INET6)
1371 return 0;
1372 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1373 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1374 if (src->ss_family == AF_INET) {
1375 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1376 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1377 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1378 }
1379 else {
1380 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1381 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1382 }
1383 if (dst->ss_family == AF_INET) {
1384 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1385 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
1386 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1387 }
1388 else {
1389 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1390 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1391 }
1392
1393 ret = PP2_HDR_LEN_INET6;
1394 }
1395 }
David Safb76832014-05-08 23:42:08 -04001396
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001397 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1398 uint32_t zero_crc32c = 0;
1399 if ((buf_len - ret) < sizeof(struct tlv))
1400 return 0;
1401 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1402 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1403 }
1404
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001405 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001406 if ((buf_len - ret) < sizeof(struct tlv))
1407 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001408 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001409 }
1410
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001411 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001412 value = NULL;
1413 if (remote && remote->proxy_authority) {
1414 value = remote->proxy_authority;
1415 value_len = remote->proxy_authority_len;
1416 }
1417#ifdef USE_OPENSSL
1418 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001419 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001420 value_len = strlen(value);
1421 }
1422#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001423 if (value) {
1424 if ((buf_len - ret) < sizeof(struct tlv))
1425 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001426 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001427 }
1428 }
1429
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001430#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001431 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001432 struct tlv_ssl *tlv;
1433 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001434 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1435 return 0;
1436 tlv = (struct tlv_ssl *)&buf[ret];
1437 memset(tlv, 0, sizeof(struct tlv_ssl));
1438 ssl_tlv_len += sizeof(struct tlv_ssl);
1439 tlv->tlv.type = PP2_TYPE_SSL;
1440 if (ssl_sock_is_ssl(remote)) {
1441 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001442 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001443 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001444 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 -04001445 }
Dave McCowan328fb582014-07-30 10:39:13 -04001446 if (ssl_sock_get_cert_used_sess(remote)) {
1447 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001448 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001449 if (ssl_sock_get_cert_used_conn(remote))
1450 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001451 }
1452 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001453 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001454 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001455 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1456 cn_trash->data,
1457 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001458 }
1459 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001460 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001461 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001462 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001463 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1464 pkey_trash->data,
1465 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001466 }
1467 }
1468 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1469 value = ssl_sock_get_cert_sig(remote);
1470 if (value) {
1471 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1472 }
1473 }
1474 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1475 value = ssl_sock_get_cipher_name(remote);
1476 if (value) {
1477 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1478 }
1479 }
David Safb76832014-05-08 23:42:08 -04001480 }
1481 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1482 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1483 ret += ssl_tlv_len;
1484 }
1485#endif
1486
Willy Tarreaue5733232019-05-22 19:24:06 +02001487#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001488 if (remote && (remote->proxy_netns)) {
1489 if ((buf_len - ret) < sizeof(struct tlv))
1490 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001491 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 +01001492 }
1493#endif
1494
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001495 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001496
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001497 if (tlv_crc32c_p) {
1498 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1499 }
1500
David Safb76832014-05-08 23:42:08 -04001501 return ret;
1502}
Emeric Brun4f603012017-01-05 15:11:44 +01001503
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001504/* return the major HTTP version as 1 or 2 depending on how the request arrived
1505 * before being processed.
1506 */
1507static int
1508smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1509{
Jérôme Magnin86577422018-12-07 09:03:11 +01001510 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1511 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001512
1513 smp->data.type = SMP_T_SINT;
1514 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1515 return 1;
1516}
1517
Emeric Brun4f603012017-01-05 15:11:44 +01001518/* fetch if the received connection used a PROXY protocol header */
1519int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1520{
1521 struct connection *conn;
1522
1523 conn = objt_conn(smp->sess->origin);
1524 if (!conn)
1525 return 0;
1526
1527 if (!(conn->flags & CO_FL_CONNECTED)) {
1528 smp->flags |= SMP_F_MAY_CHANGE;
1529 return 0;
1530 }
1531
1532 smp->flags = 0;
1533 smp->data.type = SMP_T_BOOL;
1534 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1535
1536 return 1;
1537}
1538
Geoff Simmons7185b782019-08-27 18:31:16 +02001539/* fetch the authority TLV from a PROXY protocol header */
1540int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1541{
1542 struct connection *conn;
1543
1544 conn = objt_conn(smp->sess->origin);
1545 if (!conn)
1546 return 0;
1547
1548 if (!(conn->flags & CO_FL_CONNECTED)) {
1549 smp->flags |= SMP_F_MAY_CHANGE;
1550 return 0;
1551 }
1552
1553 if (conn->proxy_authority == NULL)
1554 return 0;
1555
1556 smp->flags = 0;
1557 smp->data.type = SMP_T_STR;
1558 smp->data.u.str.area = conn->proxy_authority;
1559 smp->data.u.str.data = conn->proxy_authority_len;
1560
1561 return 1;
1562}
1563
Emeric Brun4f603012017-01-05 15:11:44 +01001564/* Note: must not be declared <const> as its list will be overwritten.
1565 * Note: fetches that may return multiple types must be declared as the lowest
1566 * common denominator, the type that can be casted into all other ones. For
1567 * instance v4/v6 must be declared v4.
1568 */
1569static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001570 { "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 +01001571 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001572 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001573 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001574 { /* END */ },
1575}};
1576
Willy Tarreau0108d902018-11-25 19:14:37 +01001577INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);