blob: 611a63de318e3fde15a0c5294cd45697569303b2 [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
Olivier Houchard477902b2020-01-22 18:08:48 +010044int conn_create_mux(struct connection *conn)
45{
Olivier Houchard477902b2020-01-22 18:08:48 +010046 if (conn_is_back(conn)) {
47 struct server *srv;
48 struct conn_stream *cs = conn->ctx;
49
50 if (conn->flags & CO_FL_ERROR)
51 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010052
Olivier Houchard477902b2020-01-22 18:08:48 +010053 if (conn_install_mux_be(conn, conn->ctx, conn->owner) < 0)
54 goto fail;
55 srv = objt_server(conn->target);
56 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
57 conn->mux->avail_streams(conn) > 0)
58 LIST_ADD(&srv->idle_conns[tid], &conn->list);
59 return 0;
60fail:
61 /* let the upper layer know the connection failed */
62 cs->data_cb->wake(cs);
63 return -1;
64 } else
65 return conn_complete_session(conn);
66
67}
68
Willy Tarreau59f98392012-07-06 14:13:49 +020069/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020070 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020071 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020072void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020073{
Willy Tarreau80184712012-07-06 14:54:49 +020074 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020075 unsigned int flags;
Olivier Houchardaf4021e2018-08-09 13:06:55 +020076 int io_available = 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020077
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010078 if (unlikely(!conn)) {
79 activity[tid].conn_dead++;
Willy Tarreau7a798e52016-04-14 11:13:20 +020080 return;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010081 }
Willy Tarreau59f98392012-07-06 14:13:49 +020082
Willy Tarreau916e12d2017-10-25 09:22:43 +020083 conn->flags |= CO_FL_WILL_UPDATE;
84
Willy Tarreau7d281492012-12-16 19:19:13 +010085 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010086
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010087 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
88 ((fd_send_ready(fd) && fd_send_active(fd)) ||
89 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
90 /* Still waiting for a connection to establish and nothing was
91 * attempted yet to probe the connection. this will clear the
92 * CO_FL_WAIT_L4_CONN flag on success.
93 */
94 if (!conn_fd_check(conn))
95 goto leave;
96 }
97
Willy Tarreau8081abe2019-11-28 18:08:49 +010098 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010099 /* force reporting of activity by clearing the previous flags :
100 * we'll have at least ERROR or CONNECTED at the end of an I/O,
101 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200102 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100103 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100104 if (conn->subs && conn->subs->events & SUB_RETRY_SEND) {
105 tasklet_wakeup(conn->subs->tasklet);
106 conn->subs->events &= ~SUB_RETRY_SEND;
107 if (!conn->subs->events)
108 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200109 } else
110 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200111 __conn_xprt_stop_send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200112 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200113
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200114 /* The data transfer starts here and stops on error and handshakes. Note
115 * that we must absolutely test conn->xprt at each step in case it suddenly
116 * changes due to a quick unexpected close().
117 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100118 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100119 /* force reporting of activity by clearing the previous flags :
120 * we'll have at least ERROR or CONNECTED at the end of an I/O,
121 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200122 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100123 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100124 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
125 tasklet_wakeup(conn->subs->tasklet);
126 conn->subs->events &= ~SUB_RETRY_RECV;
127 if (!conn->subs->events)
128 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200129 } else
130 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200131 __conn_xprt_stop_recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200132 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200133
Willy Tarreau2c6be842012-07-06 17:12:34 +0200134 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100135 /* If we don't yet have a mux, that means we were waiting for
136 * informations to create one, typically from the ALPN. If we're
137 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200138 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100139 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100140 if (conn_create_mux(conn) < 0)
141 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200142
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100143 /* The wake callback is normally used to notify the data layer about
144 * data layer activity (successful send/recv), connection establishment,
145 * shutdown and fatal errors. We need to consider the following
146 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100147 * - change among the CO_FL_NOTIFY_DONE flags :
148 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100149 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
150 * end of handshake and transition to CONNECTED
151 * - raise of CONNECTED with HANDSHAKE down
152 * - end of HANDSHAKE with CONNECTED set
153 * - regular data layer activity
154 *
155 * Note that the wake callback is allowed to release the connection and
156 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200157 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100158 if ((io_available || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
159 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200160 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200161 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200162
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200163 /* commit polling changes */
Willy Tarreau916e12d2017-10-25 09:22:43 +0200164 conn->flags &= ~CO_FL_WILL_UPDATE;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200165 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200166 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200167}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169/* Update polling on connection <c>'s file descriptor depending on its current
Willy Tarreau3381bf82020-01-17 17:39:35 +0100170 * state as reported in the connection's CO_FL_XPRT_* flags. The connection
171 * flags are updated with the new flags at the end of the operation. Polling
172 * is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200173 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200174void conn_update_xprt_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200177
Willy Tarreau3c728722014-01-23 13:50:42 +0100178 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200179 return;
180
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200181 /* update read status if needed */
Willy Tarreau3381bf82020-01-17 17:39:35 +0100182 if (f & CO_FL_XPRT_RD_ENA)
Willy Tarreau585744b2017-08-24 14:31:19 +0200183 fd_want_recv(c->handle.fd);
Willy Tarreau3381bf82020-01-17 17:39:35 +0100184 else
Willy Tarreau585744b2017-08-24 14:31:19 +0200185 fd_stop_recv(c->handle.fd);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200186
187 /* update write status if needed */
Willy Tarreau3381bf82020-01-17 17:39:35 +0100188 if (f & CO_FL_XPRT_WR_ENA)
Willy Tarreau585744b2017-08-24 14:31:19 +0200189 fd_want_send(c->handle.fd);
Willy Tarreau3381bf82020-01-17 17:39:35 +0100190 else
Willy Tarreau585744b2017-08-24 14:31:19 +0200191 fd_stop_send(c->handle.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192}
193
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100194/* This is the callback which is set when a connection establishment is pending
195 * and we have nothing to send. It may update the FD polling status to indicate
196 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
197 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
198 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
199 * leaves the error code in errno.
200 */
201int conn_fd_check(struct connection *conn)
202{
203 struct sockaddr_storage *addr;
204 int fd = conn->handle.fd;
205
206 if (conn->flags & CO_FL_ERROR)
207 return 0;
208
209 if (!conn_ctrl_ready(conn))
210 return 0;
211
212 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
213 return 1; /* strange we were called while ready */
214
215 if (!fd_send_ready(fd))
216 return 0;
217
218 /* Here we have 2 cases :
219 * - modern pollers, able to report ERR/HUP. If these ones return any
220 * of these flags then it's likely a failure, otherwise it possibly
221 * is a success (i.e. there may have been data received just before
222 * the error was reported).
223 * - select, which doesn't report these and with which it's always
224 * necessary either to try connect() again or to check for SO_ERROR.
225 * In order to simplify everything, we double-check using connect() as
226 * soon as we meet either of these delicate situations. Note that
227 * SO_ERROR would clear the error after reporting it!
228 */
229 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
230 /* modern poller, able to report ERR/HUP */
231 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
232 goto done;
233 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
234 goto done;
235 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
236 goto wait;
237 /* error present, fall through common error check path */
238 }
239
240 /* Use connect() to check the state of the socket. This has the double
241 * advantage of *not* clearing the error (so that health checks can
242 * still use getsockopt(SO_ERROR)) and giving us the following info :
243 * - error
244 * - connecting (EALREADY, EINPROGRESS)
245 * - connected (EISCONN, 0)
246 */
247 addr = conn->dst;
248 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
249 addr = &objt_server(conn->target)->socks4_addr;
250
251 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
252 if (errno == EALREADY || errno == EINPROGRESS)
253 goto wait;
254
255 if (errno && errno != EISCONN)
256 goto out_error;
257 }
258
259 done:
260 /* The FD is ready now, we'll mark the connection as complete and
261 * forward the event to the transport layer which will notify the
262 * data layer.
263 */
264 conn->flags &= ~CO_FL_WAIT_L4_CONN;
265 fd_may_send(fd);
266 fd_cond_recv(fd);
267 errno = 0; // make health checks happy
268 return 1;
269
270 out_error:
271 /* Write error on the file descriptor. Report it to the connection
272 * and disable polling on this FD.
273 */
274 fdtab[fd].linger_risk = 0;
275 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
276 __conn_xprt_stop_both(conn);
277 return 0;
278
279 wait:
280 __conn_xprt_want_send(conn);
281 fd_cant_send(fd);
282 return 0;
283}
284
Willy Tarreauff3e6482015-03-12 23:56:52 +0100285/* Send a message over an established connection. It makes use of send() and
286 * returns the same return code and errno. If the socket layer is not ready yet
287 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
288 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
289 * EMSGSIZE if called with a zero length message. The purpose is to simplify
290 * some rare attempts to directly write on the socket from above the connection
291 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
292 * It automatically retries on EINTR. Other errors cause the connection to be
293 * marked as in error state. It takes similar arguments as send() except the
294 * first one which is the connection instead of the file descriptor. Note,
295 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
296 */
297int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
298{
299 int ret;
300
301 ret = -1;
302 errno = ENOTSOCK;
303
304 if (conn->flags & CO_FL_SOCK_WR_SH)
305 goto fail;
306
307 if (!conn_ctrl_ready(conn))
308 goto fail;
309
310 errno = EMSGSIZE;
311 if (!len)
312 goto fail;
313
Willy Tarreau585744b2017-08-24 14:31:19 +0200314 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100315 goto wait;
316
317 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200318 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100319 } while (ret < 0 && errno == EINTR);
320
321
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200322 if (ret > 0) {
323 if (conn->flags & CO_FL_WAIT_L4_CONN) {
324 conn->flags &= ~CO_FL_WAIT_L4_CONN;
325 fd_may_send(conn->handle.fd);
326 fd_cond_recv(conn->handle.fd);
327 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100328 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200329 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100330
331 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
332 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200333 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100334 return 0;
335 }
336 fail:
337 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
338 return ret;
339}
340
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100341/* Called from the upper layer, to subscribe <es> to events <event_type>. The
342 * event subscriber <es> is not allowed to change from a previous call as long
343 * as at least one event is still subscribed. The <event_type> must only be a
344 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
345 */
346int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200347{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100348 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100349 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100350
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100351 es->events &= ~event_type;
352 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100353 conn->subs = NULL;
354
355 if (event_type & SUB_RETRY_RECV)
Olivier Houchard53216e72018-10-10 15:46:36 +0200356 __conn_xprt_stop_recv(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100357
358 if (event_type & SUB_RETRY_SEND)
Olivier Houchard53216e72018-10-10 15:46:36 +0200359 __conn_xprt_stop_send(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100360
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
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100365/* Called from the upper layer, to unsubscribe <es> from events <event_type>
366 * (undo fcgi_subscribe). The <es> struct is not allowed to differ from the one
367 * passed to the subscribe() call. It always returns zero.
368 */
369int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200370{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100371 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
372 BUG_ON(conn->subs && conn->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100373 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100374
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100375 conn->subs = es;
376 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100377
378 if (event_type & SUB_RETRY_RECV)
Olivier Houchard53216e72018-10-10 15:46:36 +0200379 __conn_xprt_want_recv(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100380
381 if (event_type & SUB_RETRY_SEND)
Olivier Houchard53216e72018-10-10 15:46:36 +0200382 __conn_xprt_want_send(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100383
Olivier Houchard53216e72018-10-10 15:46:36 +0200384 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200385 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200386}
387
Willy Tarreaud85c4852015-03-13 00:40:28 +0100388/* Drains possibly pending incoming data on the file descriptor attached to the
389 * connection and update the connection's flags accordingly. This is used to
390 * know whether we need to disable lingering on close. Returns non-zero if it
391 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
392 * flag may also be updated if the incoming shutdown was reported by the drain()
393 * function.
394 */
395int conn_sock_drain(struct connection *conn)
396{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200397 int turns = 2;
398 int len;
399
Willy Tarreaud85c4852015-03-13 00:40:28 +0100400 if (!conn_ctrl_ready(conn))
401 return 1;
402
403 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
404 return 1;
405
Willy Tarreaue215bba2018-08-24 14:31:53 +0200406 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
407 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100408
Willy Tarreaue215bba2018-08-24 14:31:53 +0200409 if (!fd_recv_ready(conn->handle.fd))
410 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100411
Willy Tarreaue215bba2018-08-24 14:31:53 +0200412 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200413 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100414 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200415 goto shut;
416 }
417
418 /* no drain function defined, use the generic one */
419
420 while (turns) {
421#ifdef MSG_TRUNC_CLEARS_INPUT
422 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
423 if (len == -1 && errno == EFAULT)
424#endif
425 len = recv(conn->handle.fd, trash.area, trash.size,
426 MSG_DONTWAIT | MSG_NOSIGNAL);
427
428 if (len == 0)
429 goto shut;
430
431 if (len < 0) {
432 if (errno == EAGAIN) {
433 /* connection not closed yet */
434 fd_cant_recv(conn->handle.fd);
435 break;
436 }
437 if (errno == EINTR) /* oops, try again */
438 continue;
439 /* other errors indicate a dead connection, fine. */
440 goto shut;
441 }
442 /* OK we read some data, let's try again once */
443 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100444 }
445
Willy Tarreaue215bba2018-08-24 14:31:53 +0200446 /* some data are still present, give up */
447 return 0;
448
449 shut:
450 /* we're certain the connection was shut down */
451 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100452 conn->flags |= CO_FL_SOCK_RD_SH;
453 return 1;
454}
455
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100456/*
457 * Get data length from tlv
458 */
459static int get_tlv_length(const struct tlv *src)
460{
461 return (src->length_hi << 8) | src->length_lo;
462}
463
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200464/* This handshake handler waits a PROXY protocol header at the beginning of the
465 * raw data stream. The header looks like this :
466 *
467 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
468 *
469 * There must be exactly one space between each field. Fields are :
470 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
471 * - SRC3 : layer 3 (eg: IP) source address in standard text form
472 * - DST3 : layer 3 (eg: IP) destination address in standard text form
473 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
474 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
475 *
476 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
477 *
478 * The header line is small and in all cases smaller than the smallest normal
479 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
480 * can safely use MSG_PEEK and avoid buffering.
481 *
482 * Once the data is fetched, the values are set in the connection's address
483 * fields, and data are removed from the socket's buffer. The function returns
484 * zero if it needs to wait for more data or if it fails, or 1 if it completed
485 * and removed itself.
486 */
487int conn_recv_proxy(struct connection *conn, int flag)
488{
489 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200490 struct proxy_hdr_v2 *hdr_v2;
491 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100492 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200493 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200494 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200495
Willy Tarreau3c728722014-01-23 13:50:42 +0100496 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200497 goto fail;
498
Willy Tarreauca79f592019-07-17 19:04:47 +0200499 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
500 goto fail;
501
Willy Tarreau585744b2017-08-24 14:31:19 +0200502 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200503 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100504
Willy Tarreau157788c2020-02-11 10:08:05 +0100505 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200506 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
507 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200508 if (errno == EINTR)
509 continue;
510 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200511 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200512 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200513 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100514 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200515 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200516 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100517 break;
518 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200519
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100521 /* client shutdown */
522 conn->err_code = CO_ER_PRX_EMPTY;
523 goto fail;
524 }
525
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100526 conn->flags &= ~CO_FL_WAIT_L4_CONN;
527
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200528 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200529 goto missing;
530
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 line = trash.area;
532 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200533
534 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200535 if (strncmp(line, "PROXY ", 6) != 0)
536 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537
538 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200539 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200540 goto missing;
541
David CARLIER42ff05e2016-03-24 09:22:36 +0000542 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200543 u32 src3, dst3, sport, dport;
544
545 line += 5;
546
547 src3 = inetaddr_host_lim_ret(line, end, &line);
548 if (line == end)
549 goto missing;
550 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100551 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200552
553 dst3 = inetaddr_host_lim_ret(line, end, &line);
554 if (line == end)
555 goto missing;
556 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100557 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200558
559 sport = read_uint((const char **)&line, end);
560 if (line == end)
561 goto missing;
562 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100563 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200564
565 dport = read_uint((const char **)&line, end);
566 if (line > end - 2)
567 goto missing;
568 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100569 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200570 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100571 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200572
573 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200574 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
575 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
576 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200577
Willy Tarreau226572f2019-07-17 14:46:00 +0200578 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
579 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
580 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200581 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
582 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000583 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200584 u32 sport, dport;
585 char *src_s;
586 char *dst_s, *sport_s, *dport_s;
587 struct in6_addr src3, dst3;
588
589 line += 5;
590
591 src_s = line;
592 dst_s = sport_s = dport_s = NULL;
593 while (1) {
594 if (line > end - 2) {
595 goto missing;
596 }
597 else if (*line == '\r') {
598 *line = 0;
599 line++;
600 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100601 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200602 break;
603 }
604
605 if (*line == ' ') {
606 *line = 0;
607 if (!dst_s)
608 dst_s = line + 1;
609 else if (!sport_s)
610 sport_s = line + 1;
611 else if (!dport_s)
612 dport_s = line + 1;
613 }
614 line++;
615 }
616
617 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100618 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200619
620 sport = read_uint((const char **)&sport_s,dport_s - 1);
621 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100622 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623
624 dport = read_uint((const char **)&dport_s,line - 2);
625 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100626 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627
628 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100629 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200630
631 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100632 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633
634 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200635 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
636 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
637 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200638
Willy Tarreau226572f2019-07-17 14:46:00 +0200639 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
640 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
641 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200642 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
643 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200644 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
645 /* This can be a UNIX socket forwarded by an haproxy upstream */
646 line += 9;
647 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200648 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200649 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100650 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200651 goto fail;
652 }
653
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200655 goto eat_header;
656
657 not_v1:
658 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200660 goto missing;
661
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200663
664 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
665 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
666 conn->err_code = CO_ER_PRX_NOT_HDR;
667 goto fail;
668 }
669
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200671 goto missing;
672
673 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
674 case 0x01: /* PROXY command */
675 switch (hdr_v2->fam) {
676 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100677 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
678 goto bad_header;
679
Willy Tarreau226572f2019-07-17 14:46:00 +0200680 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
681 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
682 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
683 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
684 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
685 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200686 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200687 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100688 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200689 break;
690 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100691 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
692 goto bad_header;
693
Willy Tarreau226572f2019-07-17 14:46:00 +0200694 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
695 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
696 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
697 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
698 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
699 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200700 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200701 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100702 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200703 break;
704 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100705
706 /* TLV parsing */
707 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
709 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100710 const int tlv_len = get_tlv_length(tlv_packet);
711 tlv_offset += tlv_len + TLV_HEADER_SIZE;
712
713 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100714 case PP2_TYPE_CRC32C: {
715 void *tlv_crc32c_p = (void *)tlv_packet->value;
716 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
717 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200718 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100719 goto bad_header;
720 break;
721 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200722#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100723 case PP2_TYPE_NETNS: {
724 const struct netns_entry *ns;
725 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
726 if (ns)
727 conn->proxy_netns = ns;
728 break;
729 }
730#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200731 case PP2_TYPE_AUTHORITY: {
732 if (tlv_len > PP2_AUTHORITY_MAX)
733 goto bad_header;
734 conn->proxy_authority = pool_alloc(pool_head_authority);
735 if (conn->proxy_authority == NULL)
736 goto fail;
737 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
738 conn->proxy_authority_len = tlv_len;
739 break;
740 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100741 default:
742 break;
743 }
744 }
745 }
746
Willy Tarreau77992672014-06-14 11:06:17 +0200747 /* unsupported protocol, keep local connection address */
748 break;
749 case 0x00: /* LOCAL command */
750 /* keep local connection address for LOCAL */
751 break;
752 default:
753 goto bad_header; /* not a supported command */
754 }
755
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200757 goto eat_header;
758
759 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200760 /* remove the PROXY line from the request. For this we re-read the
761 * exact line at once. If we don't get the exact same result, we
762 * fail.
763 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100764 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200766 if (len2 < 0 && errno == EINTR)
767 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100769 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100770 break;
771 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200772
773 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100774 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200775 return 1;
776
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200777 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200778 return 0;
779
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200780 missing:
781 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
782 * we have not read anything. Otherwise we need to fail because we won't
783 * be able to poll anymore.
784 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100785 conn->err_code = CO_ER_PRX_TRUNCATED;
786 goto fail;
787
788 bad_header:
789 /* This is not a valid proxy protocol header */
790 conn->err_code = CO_ER_PRX_BAD_HDR;
791 goto fail;
792
793 recv_abort:
794 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100795 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100796 goto fail;
797
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200798 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200799 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200800 return 0;
801}
802
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100803/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000804 * at the beginning of the raw data stream. The header format is
805 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100806 *
807 * This line MUST be at the beginning of the buffer and MUST NOT be
808 * fragmented.
809 *
810 * The header line is small and in all cases smaller than the smallest normal
811 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
812 * can safely use MSG_PEEK and avoid buffering.
813 *
814 * Once the data is fetched, the values are set in the connection's address
815 * fields, and data are removed from the socket's buffer. The function returns
816 * zero if it needs to wait for more data or if it fails, or 1 if it completed
817 * and removed itself.
818 */
819int conn_recv_netscaler_cip(struct connection *conn, int flag)
820{
821 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000822 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100823 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200824 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100825
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100826 if (!conn_ctrl_ready(conn))
827 goto fail;
828
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100829 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
830 goto fail;
831
Willy Tarreau585744b2017-08-24 14:31:19 +0200832 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200833 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100834
Willy Tarreau157788c2020-02-11 10:08:05 +0100835 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200836 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
837 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100838 if (errno == EINTR)
839 continue;
840 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200841 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200842 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100843 }
844 goto recv_abort;
845 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200846 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100847 break;
848 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100849
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100850 conn->flags &= ~CO_FL_WAIT_L4_CONN;
851
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100853 /* client shutdown */
854 conn->err_code = CO_ER_CIP_EMPTY;
855 goto fail;
856 }
857
858 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000859 * CIP magic, header length or
860 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200861 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100862 goto missing;
863
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200864 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100865
866 /* Decode a possible NetScaler Client IP request, fail early if
867 * it does not match */
Willy Tarreau55e0da62018-09-20 11:26:52 +0200868 if (ntohl(*(uint32_t *)line) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100869 goto bad_magic;
870
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000871 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200872 if ((trash.area[8] & 0xD0) == 0x40) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000873 hdr_len = ntohl(*(uint32_t *)(line+4));
874 line += 8;
875 }
876 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200877 else if (trash.area[8] == 0x00) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000878 hdr_len = ntohs(*(uint32_t *)(line+10));
879 line += 12;
880 }
881 /* Unknown CIP protocol */
882 else {
883 conn->err_code = CO_ER_CIP_BAD_PROTO;
884 goto fail;
885 }
886
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100887 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000888 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200889 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100890 goto missing;
891
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100892 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100893 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100894
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100895 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100896 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100897 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100898
899 hdr_ip4 = (struct ip *)line;
900
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100902 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000903 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100904 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000905 }
906 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100907 /* The protocol does not include a TCP header */
908 conn->err_code = CO_ER_CIP_BAD_PROTO;
909 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000910 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100911
David Carlier3015a2e2016-07-04 22:51:33 +0100912 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100913
914 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200915 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
916 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
917 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100918
Willy Tarreau226572f2019-07-17 14:46:00 +0200919 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
920 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
921 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100922
923 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
924 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100925 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100926 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100927 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100928
929 hdr_ip6 = (struct ip6_hdr *)line;
930
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200931 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100932 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000933 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100934 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000935 }
936 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100937 /* The protocol does not include a TCP header */
938 conn->err_code = CO_ER_CIP_BAD_PROTO;
939 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000940 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100941
David Carlier3015a2e2016-07-04 22:51:33 +0100942 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100943
944 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200945 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
946 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
947 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100948
Willy Tarreau226572f2019-07-17 14:46:00 +0200949 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
950 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
951 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100952
953 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
954 }
955 else {
956 /* The protocol does not match something known (IPv4/IPv6) */
957 conn->err_code = CO_ER_CIP_BAD_PROTO;
958 goto fail;
959 }
960
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000961 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200962 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100963
964 /* remove the NetScaler Client IP header from the request. For this
965 * we re-read the exact line at once. If we don't get the exact same
966 * result, we fail.
967 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100968 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200969 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100970 if (len2 < 0 && errno == EINTR)
971 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200972 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100973 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100974 break;
975 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100976
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
Alexander Liu2a54bb72019-05-22 19:44:48 +08001010 if (!conn_ctrl_ready(conn))
1011 goto out_error;
1012
Willy Tarreau226572f2019-07-17 14:46:00 +02001013 if (!conn_get_dst(conn))
1014 goto out_error;
1015
Alexander Liu2a54bb72019-05-22 19:44:48 +08001016 req_line.version = 0x04;
1017 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001018 req_line.port = get_net_port(conn->dst);
1019 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001020 memcpy(req_line.user_id, "HAProxy\0", 8);
1021
1022 if (conn->send_proxy_ofs > 0) {
1023 /*
1024 * This is the first call to send the request
1025 */
1026 conn->send_proxy_ofs = -(int)sizeof(req_line);
1027 }
1028
1029 if (conn->send_proxy_ofs < 0) {
1030 int ret = 0;
1031
1032 /* we are sending the socks4_req_line here. If the data layer
1033 * has a pending write, we'll also set MSG_MORE.
1034 */
1035 ret = conn_sock_send(
1036 conn,
1037 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1038 -conn->send_proxy_ofs,
1039 (conn->flags & CO_FL_XPRT_WR_ENA) ? MSG_MORE : 0);
1040
1041 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1042 conn->handle.fd, -conn->send_proxy_ofs, ret);
1043
1044 if (ret < 0) {
1045 goto out_error;
1046 }
1047
1048 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1049 if (conn->send_proxy_ofs != 0) {
1050 goto out_wait;
1051 }
1052 }
1053
1054 /* OK we've the whole request sent */
1055 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001056
1057 /* The connection is ready now, simply return and let the connection
1058 * handler notify upper layers if needed.
1059 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001060 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001061
1062 if (conn->flags & CO_FL_SEND_PROXY) {
1063 /*
1064 * Get the send_proxy_ofs ready for the send_proxy due to we are
1065 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1066 * before sending PROXY Protocol.
1067 */
1068 conn->send_proxy_ofs = 1;
1069 }
1070 return 1;
1071
1072 out_error:
1073 /* Write error on the file descriptor */
1074 conn->flags |= CO_FL_ERROR;
1075 if (conn->err_code == CO_ER_NONE) {
1076 conn->err_code = CO_ER_SOCKS4_SEND;
1077 }
1078 return 0;
1079
1080 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001081 return 0;
1082}
1083
1084int conn_recv_socks4_proxy_response(struct connection *conn)
1085{
1086 char line[SOCKS4_HS_RSP_LEN];
1087 int ret;
1088
Alexander Liu2a54bb72019-05-22 19:44:48 +08001089 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
Willy Tarreau157788c2020-02-11 10:08:05 +01001095 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001096 /* 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 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001132 break;
1133 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001134
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001135 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1136
Alexander Liu2a54bb72019-05-22 19:44:48 +08001137 if (ret < SOCKS4_HS_RSP_LEN) {
1138 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1139 * we are not able to read enough data.
1140 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001141 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001142 }
1143
1144 /*
1145 * Base on the SOCSK4 protocol:
1146 *
1147 * +----+----+----+----+----+----+----+----+
1148 * | VN | CD | DSTPORT | DSTIP |
1149 * +----+----+----+----+----+----+----+----+
1150 * # of bytes: 1 1 2 4
1151 * VN is the version of the reply code and should be 0. CD is the result
1152 * code with one of the following values:
1153 * 90: request granted
1154 * 91: request rejected or failed
1155 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1156 * 93: request rejected because the client program and identd report different user-ids
1157 * The remaining fields are ignored.
1158 */
1159 if (line[1] != 90) {
1160 conn->flags &= ~CO_FL_SOCKS4_RECV;
1161
1162 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1163 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1164 if (conn->err_code == CO_ER_NONE) {
1165 conn->err_code = CO_ER_SOCKS4_DENY;
1166 }
1167 goto fail;
1168 }
1169
1170 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001171 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001172 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1173 if (ret < 0 && errno == EINTR) {
1174 continue;
1175 }
1176 if (ret != SOCKS4_HS_RSP_LEN) {
1177 if (conn->err_code == CO_ER_NONE) {
1178 conn->err_code = CO_ER_SOCKS4_RECV;
1179 }
1180 goto fail;
1181 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001182 break;
1183 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001184
1185 conn->flags &= ~CO_FL_SOCKS4_RECV;
1186 return 1;
1187
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001188 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001189 return 0;
1190
Alexander Liu2a54bb72019-05-22 19:44:48 +08001191 recv_abort:
1192 if (conn->err_code == CO_ER_NONE) {
1193 conn->err_code = CO_ER_SOCKS4_ABORT;
1194 }
1195 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1196 goto fail;
1197
1198 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001199 conn->flags |= CO_FL_ERROR;
1200 return 0;
1201}
1202
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001203/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001204int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1205{
1206 int ret = 0;
1207
1208 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1209 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1210 }
1211 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001212 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1213 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001214 else
1215 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1216 }
1217
1218 return ret;
1219}
1220
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001221/* Makes a PROXY protocol line from the two addresses. The output is sent to
1222 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1223 * It returns the number of bytes composing this line (including the trailing
1224 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001225 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1226 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001227 */
David Safb76832014-05-08 23:42:08 -04001228int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001229{
1230 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001231 char * protocol;
1232 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1233 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1234 in_port_t src_port;
1235 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001236
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001237 if ( !src
1238 || !dst
1239 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1240 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1241 /* unknown family combination */
1242 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001243 if (ret >= buf_len)
1244 return 0;
1245
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001246 return ret;
1247 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001248
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001249 /* IPv4 for both src and dst */
1250 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1251 protocol = "TCP4";
1252 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001253 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001254 src_port = ((struct sockaddr_in *)src)->sin_port;
1255 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001256 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001257 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001258 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001259 /* IPv6 for at least one of src and dst */
1260 else {
1261 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001262
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001263 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001264
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001265 if (src->ss_family == AF_INET) {
1266 /* Convert src to IPv6 */
1267 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1268 src_port = ((struct sockaddr_in *)src)->sin_port;
1269 }
1270 else {
1271 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1272 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1273 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001274
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001275 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001276 return 0;
1277
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001278 if (dst->ss_family == AF_INET) {
1279 /* Convert dst to IPv6 */
1280 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1281 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1282 }
1283 else {
1284 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1285 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1286 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001287
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001288 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001289 return 0;
1290 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001291
1292 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1293 if (ret >= buf_len)
1294 return 0;
1295
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001296 return ret;
1297}
David Safb76832014-05-08 23:42:08 -04001298
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001299static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001300{
1301 struct tlv *tlv;
1302
1303 if (!dest || (length + sizeof(*tlv) > dest_len))
1304 return 0;
1305
1306 tlv = (struct tlv *)dest;
1307
1308 tlv->type = type;
1309 tlv->length_hi = length >> 8;
1310 tlv->length_lo = length & 0x00ff;
1311 memcpy(tlv->value, value, length);
1312 return length + sizeof(*tlv);
1313}
David Safb76832014-05-08 23:42:08 -04001314
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001315/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001316int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1317{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001318 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001319 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001320 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001321 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001322 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001323 struct sockaddr_storage *src = &null_addr;
1324 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001325 const char *value;
1326 int value_len;
David Safb76832014-05-08 23:42:08 -04001327
1328 if (buf_len < PP2_HEADER_LEN)
1329 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001330 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001331
Willy Tarreau226572f2019-07-17 14:46:00 +02001332 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1333 src = remote->src;
1334 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001335 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001336
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001337 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1338 if ( !src
1339 || !dst
1340 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1341 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001342 if (buf_len < PP2_HDR_LEN_UNSPEC)
1343 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001344 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1345 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001346 ret = PP2_HDR_LEN_UNSPEC;
1347 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001348 else {
1349 /* IPv4 for both src and dst */
1350 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1351 if (buf_len < PP2_HDR_LEN_INET)
1352 return 0;
1353 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1354 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1355 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1356 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1357 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1358 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1359 ret = PP2_HDR_LEN_INET;
1360 }
1361 /* IPv6 for at least one of src and dst */
1362 else {
1363 struct in6_addr tmp;
1364
1365 if (buf_len < PP2_HDR_LEN_INET6)
1366 return 0;
1367 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1368 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1369 if (src->ss_family == AF_INET) {
1370 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1371 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1372 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1373 }
1374 else {
1375 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1376 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1377 }
1378 if (dst->ss_family == AF_INET) {
1379 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1380 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001381 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001382 }
1383 else {
1384 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1385 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1386 }
1387
1388 ret = PP2_HDR_LEN_INET6;
1389 }
1390 }
David Safb76832014-05-08 23:42:08 -04001391
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001392 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1393 uint32_t zero_crc32c = 0;
1394 if ((buf_len - ret) < sizeof(struct tlv))
1395 return 0;
1396 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1397 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1398 }
1399
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001400 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001401 if ((buf_len - ret) < sizeof(struct tlv))
1402 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001403 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001404 }
1405
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001406 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001407 value = NULL;
1408 if (remote && remote->proxy_authority) {
1409 value = remote->proxy_authority;
1410 value_len = remote->proxy_authority_len;
1411 }
1412#ifdef USE_OPENSSL
1413 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001414 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001415 value_len = strlen(value);
1416 }
1417#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001418 if (value) {
1419 if ((buf_len - ret) < sizeof(struct tlv))
1420 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001421 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001422 }
1423 }
1424
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001425#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001426 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001427 struct tlv_ssl *tlv;
1428 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001429 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1430 return 0;
1431 tlv = (struct tlv_ssl *)&buf[ret];
1432 memset(tlv, 0, sizeof(struct tlv_ssl));
1433 ssl_tlv_len += sizeof(struct tlv_ssl);
1434 tlv->tlv.type = PP2_TYPE_SSL;
1435 if (ssl_sock_is_ssl(remote)) {
1436 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001437 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001438 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001439 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 -04001440 }
Dave McCowan328fb582014-07-30 10:39:13 -04001441 if (ssl_sock_get_cert_used_sess(remote)) {
1442 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001443 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001444 if (ssl_sock_get_cert_used_conn(remote))
1445 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001446 }
1447 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001448 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001449 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001450 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1451 cn_trash->data,
1452 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001453 }
1454 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001455 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001456 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001457 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001458 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1459 pkey_trash->data,
1460 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001461 }
1462 }
1463 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1464 value = ssl_sock_get_cert_sig(remote);
1465 if (value) {
1466 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1467 }
1468 }
1469 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1470 value = ssl_sock_get_cipher_name(remote);
1471 if (value) {
1472 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1473 }
1474 }
David Safb76832014-05-08 23:42:08 -04001475 }
1476 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1477 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1478 ret += ssl_tlv_len;
1479 }
1480#endif
1481
Willy Tarreaue5733232019-05-22 19:24:06 +02001482#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001483 if (remote && (remote->proxy_netns)) {
1484 if ((buf_len - ret) < sizeof(struct tlv))
1485 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001486 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 +01001487 }
1488#endif
1489
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001490 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001491
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001492 if (tlv_crc32c_p) {
1493 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1494 }
1495
David Safb76832014-05-08 23:42:08 -04001496 return ret;
1497}
Emeric Brun4f603012017-01-05 15:11:44 +01001498
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001499/* return the major HTTP version as 1 or 2 depending on how the request arrived
1500 * before being processed.
1501 */
1502static int
1503smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1504{
Jérôme Magnin86577422018-12-07 09:03:11 +01001505 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1506 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001507
1508 smp->data.type = SMP_T_SINT;
1509 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1510 return 1;
1511}
1512
Emeric Brun4f603012017-01-05 15:11:44 +01001513/* fetch if the received connection used a PROXY protocol header */
1514int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1515{
1516 struct connection *conn;
1517
1518 conn = objt_conn(smp->sess->origin);
1519 if (!conn)
1520 return 0;
1521
Willy Tarreau911db9b2020-01-23 16:27:54 +01001522 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001523 smp->flags |= SMP_F_MAY_CHANGE;
1524 return 0;
1525 }
1526
1527 smp->flags = 0;
1528 smp->data.type = SMP_T_BOOL;
1529 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1530
1531 return 1;
1532}
1533
Geoff Simmons7185b782019-08-27 18:31:16 +02001534/* fetch the authority TLV from a PROXY protocol header */
1535int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1536{
1537 struct connection *conn;
1538
1539 conn = objt_conn(smp->sess->origin);
1540 if (!conn)
1541 return 0;
1542
Willy Tarreau911db9b2020-01-23 16:27:54 +01001543 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001544 smp->flags |= SMP_F_MAY_CHANGE;
1545 return 0;
1546 }
1547
1548 if (conn->proxy_authority == NULL)
1549 return 0;
1550
1551 smp->flags = 0;
1552 smp->data.type = SMP_T_STR;
1553 smp->data.u.str.area = conn->proxy_authority;
1554 smp->data.u.str.data = conn->proxy_authority_len;
1555
1556 return 1;
1557}
1558
Emeric Brun4f603012017-01-05 15:11:44 +01001559/* Note: must not be declared <const> as its list will be overwritten.
1560 * Note: fetches that may return multiple types must be declared as the lowest
1561 * common denominator, the type that can be casted into all other ones. For
1562 * instance v4/v6 must be declared v4.
1563 */
1564static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001565 { "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 +01001566 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001567 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001568 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001569 { /* END */ },
1570}};
1571
Willy Tarreau0108d902018-11-25 19:14:37 +01001572INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);