blob: 580aa3313be89a9f7eb82ae9951efda62f9d1510 [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 Tarreau916e12d2017-10-25 09:22:43 +020058 conn->flags |= CO_FL_WILL_UPDATE;
59
Willy Tarreau7d281492012-12-16 19:19:13 +010060 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010061
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010062 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
63 ((fd_send_ready(fd) && fd_send_active(fd)) ||
64 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
65 /* Still waiting for a connection to establish and nothing was
66 * attempted yet to probe the connection. this will clear the
67 * CO_FL_WAIT_L4_CONN flag on success.
68 */
69 if (!conn_fd_check(conn))
70 goto leave;
71 }
72
Willy Tarreau8081abe2019-11-28 18:08:49 +010073 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010074 /* force reporting of activity by clearing the previous flags :
75 * we'll have at least ERROR or CONNECTED at the end of an I/O,
76 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020077 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +010078 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +010079 if (conn->subs && conn->subs->events & SUB_RETRY_SEND) {
80 tasklet_wakeup(conn->subs->tasklet);
81 conn->subs->events &= ~SUB_RETRY_SEND;
82 if (!conn->subs->events)
83 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +020084 } else
85 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +020086 __conn_xprt_stop_send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020087 }
Willy Tarreau59f98392012-07-06 14:13:49 +020088
Willy Tarreau57ec32f2017-04-11 19:59:33 +020089 /* The data transfer starts here and stops on error and handshakes. Note
90 * that we must absolutely test conn->xprt at each step in case it suddenly
91 * changes due to a quick unexpected close().
92 */
Willy Tarreau8081abe2019-11-28 18:08:49 +010093 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010094 /* force reporting of activity by clearing the previous flags :
95 * we'll have at least ERROR or CONNECTED at the end of an I/O,
96 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020097 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +010098 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +010099 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
100 tasklet_wakeup(conn->subs->tasklet);
101 conn->subs->events &= ~SUB_RETRY_RECV;
102 if (!conn->subs->events)
103 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200104 } else
105 io_available = 1;
Olivier Houchard53216e72018-10-10 15:46:36 +0200106 __conn_xprt_stop_recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200107 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200108
Willy Tarreau2c6be842012-07-06 17:12:34 +0200109 leave:
Willy Tarreaucbcf77e2019-12-27 14:49:19 +0100110 /* Verify if the connection just established. */
111 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
112 conn->flags |= CO_FL_CONNECTED;
113
114 /* The connection owner might want to be notified about failures
115 * and/or handshake completeion. The callback may fail and cause the
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200116 * connection to be destroyed, thus we must not use it anymore and
117 * should immediately leave instead. The caller must immediately
118 * unregister itself once called.
119 */
Willy Tarreaucbcf77e2019-12-27 14:49:19 +0100120 if (unlikely(conn->xprt_done_cb) &&
121 (!(conn->flags & CO_FL_HANDSHAKE) ||
122 ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE)) &&
123 conn->xprt_done_cb(conn) < 0)
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200124 return;
125
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100126 /* The wake callback is normally used to notify the data layer about
127 * data layer activity (successful send/recv), connection establishment,
128 * shutdown and fatal errors. We need to consider the following
129 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100130 * - change among the CO_FL_NOTIFY_DONE flags :
131 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100132 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
133 * end of handshake and transition to CONNECTED
134 * - raise of CONNECTED with HANDSHAKE down
135 * - end of HANDSHAKE with CONNECTED set
136 * - regular data layer activity
137 *
138 * Note that the wake callback is allowed to release the connection and
139 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200140 */
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100141 if ((io_available || (((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100142 ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED &&
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200143 (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200144 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200145 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200146
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200147 /* commit polling changes */
Willy Tarreau916e12d2017-10-25 09:22:43 +0200148 conn->flags &= ~CO_FL_WILL_UPDATE;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200149 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200150 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200151}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200152
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200153/* Update polling on connection <c>'s file descriptor depending on its current
Willy Tarreau3381bf82020-01-17 17:39:35 +0100154 * state as reported in the connection's CO_FL_XPRT_* flags. The connection
155 * flags are updated with the new flags at the end of the operation. Polling
156 * is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200157 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200158void conn_update_xprt_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200160 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161
Willy Tarreau3c728722014-01-23 13:50:42 +0100162 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200163 return;
164
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165 /* update read status if needed */
Willy Tarreau3381bf82020-01-17 17:39:35 +0100166 if (f & CO_FL_XPRT_RD_ENA)
Willy Tarreau585744b2017-08-24 14:31:19 +0200167 fd_want_recv(c->handle.fd);
Willy Tarreau3381bf82020-01-17 17:39:35 +0100168 else
Willy Tarreau585744b2017-08-24 14:31:19 +0200169 fd_stop_recv(c->handle.fd);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200170
171 /* update write status if needed */
Willy Tarreau3381bf82020-01-17 17:39:35 +0100172 if (f & CO_FL_XPRT_WR_ENA)
Willy Tarreau585744b2017-08-24 14:31:19 +0200173 fd_want_send(c->handle.fd);
Willy Tarreau3381bf82020-01-17 17:39:35 +0100174 else
Willy Tarreau585744b2017-08-24 14:31:19 +0200175 fd_stop_send(c->handle.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176}
177
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100178/* This is the callback which is set when a connection establishment is pending
179 * and we have nothing to send. It may update the FD polling status to indicate
180 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
181 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
182 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
183 * leaves the error code in errno.
184 */
185int conn_fd_check(struct connection *conn)
186{
187 struct sockaddr_storage *addr;
188 int fd = conn->handle.fd;
189
190 if (conn->flags & CO_FL_ERROR)
191 return 0;
192
193 if (!conn_ctrl_ready(conn))
194 return 0;
195
196 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
197 return 1; /* strange we were called while ready */
198
199 if (!fd_send_ready(fd))
200 return 0;
201
202 /* Here we have 2 cases :
203 * - modern pollers, able to report ERR/HUP. If these ones return any
204 * of these flags then it's likely a failure, otherwise it possibly
205 * is a success (i.e. there may have been data received just before
206 * the error was reported).
207 * - select, which doesn't report these and with which it's always
208 * necessary either to try connect() again or to check for SO_ERROR.
209 * In order to simplify everything, we double-check using connect() as
210 * soon as we meet either of these delicate situations. Note that
211 * SO_ERROR would clear the error after reporting it!
212 */
213 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
214 /* modern poller, able to report ERR/HUP */
215 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
216 goto done;
217 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
218 goto done;
219 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
220 goto wait;
221 /* error present, fall through common error check path */
222 }
223
224 /* Use connect() to check the state of the socket. This has the double
225 * advantage of *not* clearing the error (so that health checks can
226 * still use getsockopt(SO_ERROR)) and giving us the following info :
227 * - error
228 * - connecting (EALREADY, EINPROGRESS)
229 * - connected (EISCONN, 0)
230 */
231 addr = conn->dst;
232 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
233 addr = &objt_server(conn->target)->socks4_addr;
234
235 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
236 if (errno == EALREADY || errno == EINPROGRESS)
237 goto wait;
238
239 if (errno && errno != EISCONN)
240 goto out_error;
241 }
242
243 done:
244 /* The FD is ready now, we'll mark the connection as complete and
245 * forward the event to the transport layer which will notify the
246 * data layer.
247 */
248 conn->flags &= ~CO_FL_WAIT_L4_CONN;
249 fd_may_send(fd);
250 fd_cond_recv(fd);
251 errno = 0; // make health checks happy
252 return 1;
253
254 out_error:
255 /* Write error on the file descriptor. Report it to the connection
256 * and disable polling on this FD.
257 */
258 fdtab[fd].linger_risk = 0;
259 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
260 __conn_xprt_stop_both(conn);
261 return 0;
262
263 wait:
264 __conn_xprt_want_send(conn);
265 fd_cant_send(fd);
266 return 0;
267}
268
Willy Tarreauff3e6482015-03-12 23:56:52 +0100269/* Send a message over an established connection. It makes use of send() and
270 * returns the same return code and errno. If the socket layer is not ready yet
271 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
272 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
273 * EMSGSIZE if called with a zero length message. The purpose is to simplify
274 * some rare attempts to directly write on the socket from above the connection
275 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
276 * It automatically retries on EINTR. Other errors cause the connection to be
277 * marked as in error state. It takes similar arguments as send() except the
278 * first one which is the connection instead of the file descriptor. Note,
279 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
280 */
281int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
282{
283 int ret;
284
285 ret = -1;
286 errno = ENOTSOCK;
287
288 if (conn->flags & CO_FL_SOCK_WR_SH)
289 goto fail;
290
291 if (!conn_ctrl_ready(conn))
292 goto fail;
293
294 errno = EMSGSIZE;
295 if (!len)
296 goto fail;
297
Willy Tarreau585744b2017-08-24 14:31:19 +0200298 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100299 goto wait;
300
301 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200302 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100303 } while (ret < 0 && errno == EINTR);
304
305
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200306 if (ret > 0) {
307 if (conn->flags & CO_FL_WAIT_L4_CONN) {
308 conn->flags &= ~CO_FL_WAIT_L4_CONN;
309 fd_may_send(conn->handle.fd);
310 fd_cond_recv(conn->handle.fd);
311 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100312 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200313 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100314
315 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
316 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200317 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100318 return 0;
319 }
320 fail:
321 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
322 return ret;
323}
324
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100325/* Called from the upper layer, to subscribe <es> to events <event_type>. The
326 * event subscriber <es> is not allowed to change from a previous call as long
327 * as at least one event is still subscribed. The <event_type> must only be a
328 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
329 */
330int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200331{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100332 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100333 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100334
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100335 es->events &= ~event_type;
336 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100337 conn->subs = NULL;
338
339 if (event_type & SUB_RETRY_RECV)
Olivier Houchard53216e72018-10-10 15:46:36 +0200340 __conn_xprt_stop_recv(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100341
342 if (event_type & SUB_RETRY_SEND)
Olivier Houchard53216e72018-10-10 15:46:36 +0200343 __conn_xprt_stop_send(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100344
Olivier Houchard53216e72018-10-10 15:46:36 +0200345 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200346 return 0;
347}
348
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100349/* Called from the upper layer, to unsubscribe <es> from events <event_type>
350 * (undo fcgi_subscribe). The <es> struct is not allowed to differ from the one
351 * passed to the subscribe() call. It always returns zero.
352 */
353int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200354{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100355 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
356 BUG_ON(conn->subs && conn->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100357 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100358
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100359 conn->subs = es;
360 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100361
362 if (event_type & SUB_RETRY_RECV)
Olivier Houchard53216e72018-10-10 15:46:36 +0200363 __conn_xprt_want_recv(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100364
365 if (event_type & SUB_RETRY_SEND)
Olivier Houchard53216e72018-10-10 15:46:36 +0200366 __conn_xprt_want_send(conn);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100367
Olivier Houchard53216e72018-10-10 15:46:36 +0200368 conn_update_xprt_polling(conn);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200369 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200370}
371
Willy Tarreaud85c4852015-03-13 00:40:28 +0100372/* Drains possibly pending incoming data on the file descriptor attached to the
373 * connection and update the connection's flags accordingly. This is used to
374 * know whether we need to disable lingering on close. Returns non-zero if it
375 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
376 * flag may also be updated if the incoming shutdown was reported by the drain()
377 * function.
378 */
379int conn_sock_drain(struct connection *conn)
380{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200381 int turns = 2;
382 int len;
383
Willy Tarreaud85c4852015-03-13 00:40:28 +0100384 if (!conn_ctrl_ready(conn))
385 return 1;
386
387 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
388 return 1;
389
Willy Tarreaue215bba2018-08-24 14:31:53 +0200390 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
391 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100392
Willy Tarreaue215bba2018-08-24 14:31:53 +0200393 if (!fd_recv_ready(conn->handle.fd))
394 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100395
Willy Tarreaue215bba2018-08-24 14:31:53 +0200396 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200397 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100398 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200399 goto shut;
400 }
401
402 /* no drain function defined, use the generic one */
403
404 while (turns) {
405#ifdef MSG_TRUNC_CLEARS_INPUT
406 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
407 if (len == -1 && errno == EFAULT)
408#endif
409 len = recv(conn->handle.fd, trash.area, trash.size,
410 MSG_DONTWAIT | MSG_NOSIGNAL);
411
412 if (len == 0)
413 goto shut;
414
415 if (len < 0) {
416 if (errno == EAGAIN) {
417 /* connection not closed yet */
418 fd_cant_recv(conn->handle.fd);
419 break;
420 }
421 if (errno == EINTR) /* oops, try again */
422 continue;
423 /* other errors indicate a dead connection, fine. */
424 goto shut;
425 }
426 /* OK we read some data, let's try again once */
427 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100428 }
429
Willy Tarreaue215bba2018-08-24 14:31:53 +0200430 /* some data are still present, give up */
431 return 0;
432
433 shut:
434 /* we're certain the connection was shut down */
435 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100436 conn->flags |= CO_FL_SOCK_RD_SH;
437 return 1;
438}
439
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100440/*
441 * Get data length from tlv
442 */
443static int get_tlv_length(const struct tlv *src)
444{
445 return (src->length_hi << 8) | src->length_lo;
446}
447
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200448/* This handshake handler waits a PROXY protocol header at the beginning of the
449 * raw data stream. The header looks like this :
450 *
451 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
452 *
453 * There must be exactly one space between each field. Fields are :
454 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
455 * - SRC3 : layer 3 (eg: IP) source address in standard text form
456 * - DST3 : layer 3 (eg: IP) destination address in standard text form
457 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
458 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
459 *
460 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
461 *
462 * The header line is small and in all cases smaller than the smallest normal
463 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
464 * can safely use MSG_PEEK and avoid buffering.
465 *
466 * Once the data is fetched, the values are set in the connection's address
467 * fields, and data are removed from the socket's buffer. The function returns
468 * zero if it needs to wait for more data or if it fails, or 1 if it completed
469 * and removed itself.
470 */
471int conn_recv_proxy(struct connection *conn, int flag)
472{
473 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200474 struct proxy_hdr_v2 *hdr_v2;
475 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100476 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200477 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200478 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200479
480 /* we might have been called just after an asynchronous shutr */
481 if (conn->flags & CO_FL_SOCK_RD_SH)
482 goto fail;
483
Willy Tarreau3c728722014-01-23 13:50:42 +0100484 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200485 goto fail;
486
Willy Tarreauca79f592019-07-17 19:04:47 +0200487 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
488 goto fail;
489
Willy Tarreau585744b2017-08-24 14:31:19 +0200490 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200491 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100492
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200493 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200494 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
495 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200496 if (errno == EINTR)
497 continue;
498 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200499 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200500 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200501 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100502 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200504 trash.data = ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200505 } while (0);
506
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100508 /* client shutdown */
509 conn->err_code = CO_ER_PRX_EMPTY;
510 goto fail;
511 }
512
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200514 goto missing;
515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 line = trash.area;
517 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200518
519 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200520 if (strncmp(line, "PROXY ", 6) != 0)
521 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200522
523 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200524 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200525 goto missing;
526
David CARLIER42ff05e2016-03-24 09:22:36 +0000527 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200528 u32 src3, dst3, sport, dport;
529
530 line += 5;
531
532 src3 = inetaddr_host_lim_ret(line, end, &line);
533 if (line == end)
534 goto missing;
535 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100536 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537
538 dst3 = inetaddr_host_lim_ret(line, end, &line);
539 if (line == end)
540 goto missing;
541 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100542 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200543
544 sport = read_uint((const char **)&line, end);
545 if (line == end)
546 goto missing;
547 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100548 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200549
550 dport = read_uint((const char **)&line, end);
551 if (line > end - 2)
552 goto missing;
553 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100554 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200555 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100556 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200557
558 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200559 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
560 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
561 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200562
Willy Tarreau226572f2019-07-17 14:46:00 +0200563 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
564 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
565 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200566 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
567 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000568 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200569 u32 sport, dport;
570 char *src_s;
571 char *dst_s, *sport_s, *dport_s;
572 struct in6_addr src3, dst3;
573
574 line += 5;
575
576 src_s = line;
577 dst_s = sport_s = dport_s = NULL;
578 while (1) {
579 if (line > end - 2) {
580 goto missing;
581 }
582 else if (*line == '\r') {
583 *line = 0;
584 line++;
585 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100586 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200587 break;
588 }
589
590 if (*line == ' ') {
591 *line = 0;
592 if (!dst_s)
593 dst_s = line + 1;
594 else if (!sport_s)
595 sport_s = line + 1;
596 else if (!dport_s)
597 dport_s = line + 1;
598 }
599 line++;
600 }
601
602 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100603 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200604
605 sport = read_uint((const char **)&sport_s,dport_s - 1);
606 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100607 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200608
609 dport = read_uint((const char **)&dport_s,line - 2);
610 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100611 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200612
613 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100614 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200615
616 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100617 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200618
619 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200620 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
621 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
622 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623
Willy Tarreau226572f2019-07-17 14:46:00 +0200624 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
625 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
626 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
628 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200629 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
630 /* This can be a UNIX socket forwarded by an haproxy upstream */
631 line += 9;
632 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200634 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100635 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200636 goto fail;
637 }
638
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200640 goto eat_header;
641
642 not_v1:
643 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200644 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200645 goto missing;
646
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200648
649 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
650 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
651 conn->err_code = CO_ER_PRX_NOT_HDR;
652 goto fail;
653 }
654
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200656 goto missing;
657
658 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
659 case 0x01: /* PROXY command */
660 switch (hdr_v2->fam) {
661 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100662 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
663 goto bad_header;
664
Willy Tarreau226572f2019-07-17 14:46:00 +0200665 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
666 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
667 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
668 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
669 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
670 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200671 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200672 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100673 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200674 break;
675 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100676 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
677 goto bad_header;
678
Willy Tarreau226572f2019-07-17 14:46:00 +0200679 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
680 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
681 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
682 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
683 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
684 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200685 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200686 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100687 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200688 break;
689 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100690
691 /* TLV parsing */
692 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
694 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100695 const int tlv_len = get_tlv_length(tlv_packet);
696 tlv_offset += tlv_len + TLV_HEADER_SIZE;
697
698 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100699 case PP2_TYPE_CRC32C: {
700 void *tlv_crc32c_p = (void *)tlv_packet->value;
701 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
702 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100704 goto bad_header;
705 break;
706 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200707#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100708 case PP2_TYPE_NETNS: {
709 const struct netns_entry *ns;
710 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
711 if (ns)
712 conn->proxy_netns = ns;
713 break;
714 }
715#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200716 case PP2_TYPE_AUTHORITY: {
717 if (tlv_len > PP2_AUTHORITY_MAX)
718 goto bad_header;
719 conn->proxy_authority = pool_alloc(pool_head_authority);
720 if (conn->proxy_authority == NULL)
721 goto fail;
722 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
723 conn->proxy_authority_len = tlv_len;
724 break;
725 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100726 default:
727 break;
728 }
729 }
730 }
731
Willy Tarreau77992672014-06-14 11:06:17 +0200732 /* unsupported protocol, keep local connection address */
733 break;
734 case 0x00: /* LOCAL command */
735 /* keep local connection address for LOCAL */
736 break;
737 default:
738 goto bad_header; /* not a supported command */
739 }
740
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200742 goto eat_header;
743
744 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200745 /* remove the PROXY line from the request. For this we re-read the
746 * exact line at once. If we don't get the exact same result, we
747 * fail.
748 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200749 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200751 if (len2 < 0 && errno == EINTR)
752 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100754 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200755 } while (0);
756
757 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100758 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200759 return 1;
760
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200761 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200762 return 0;
763
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200764 missing:
765 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
766 * we have not read anything. Otherwise we need to fail because we won't
767 * be able to poll anymore.
768 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100769 conn->err_code = CO_ER_PRX_TRUNCATED;
770 goto fail;
771
772 bad_header:
773 /* This is not a valid proxy protocol header */
774 conn->err_code = CO_ER_PRX_BAD_HDR;
775 goto fail;
776
777 recv_abort:
778 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100779 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100780 goto fail;
781
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200782 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200783 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200784 return 0;
785}
786
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100787/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000788 * at the beginning of the raw data stream. The header format is
789 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100790 *
791 * This line MUST be at the beginning of the buffer and MUST NOT be
792 * fragmented.
793 *
794 * The header line is small and in all cases smaller than the smallest normal
795 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
796 * can safely use MSG_PEEK and avoid buffering.
797 *
798 * Once the data is fetched, the values are set in the connection's address
799 * fields, and data are removed from the socket's buffer. The function returns
800 * zero if it needs to wait for more data or if it fails, or 1 if it completed
801 * and removed itself.
802 */
803int conn_recv_netscaler_cip(struct connection *conn, int flag)
804{
805 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000806 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100807 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200808 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100809
810 /* we might have been called just after an asynchronous shutr */
811 if (conn->flags & CO_FL_SOCK_RD_SH)
812 goto fail;
813
814 if (!conn_ctrl_ready(conn))
815 goto fail;
816
Willy Tarreau585744b2017-08-24 14:31:19 +0200817 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200818 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100819
820 do {
Willy Tarreaub406b872018-08-22 05:20:32 +0200821 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
822 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100823 if (errno == EINTR)
824 continue;
825 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200826 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200827 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100828 }
829 goto recv_abort;
830 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200831 trash.data = ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100832 } while (0);
833
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200834 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100835 /* client shutdown */
836 conn->err_code = CO_ER_CIP_EMPTY;
837 goto fail;
838 }
839
840 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000841 * CIP magic, header length or
842 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200843 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100844 goto missing;
845
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100847
848 /* Decode a possible NetScaler Client IP request, fail early if
849 * it does not match */
Willy Tarreau55e0da62018-09-20 11:26:52 +0200850 if (ntohl(*(uint32_t *)line) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100851 goto bad_magic;
852
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000853 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200854 if ((trash.area[8] & 0xD0) == 0x40) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000855 hdr_len = ntohl(*(uint32_t *)(line+4));
856 line += 8;
857 }
858 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200859 else if (trash.area[8] == 0x00) {
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000860 hdr_len = ntohs(*(uint32_t *)(line+10));
861 line += 12;
862 }
863 /* Unknown CIP protocol */
864 else {
865 conn->err_code = CO_ER_CIP_BAD_PROTO;
866 goto fail;
867 }
868
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100869 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000870 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200871 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100872 goto missing;
873
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100874 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100875 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100876
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100877 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100878 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100879 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100880
881 hdr_ip4 = (struct ip *)line;
882
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200883 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100884 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000885 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100886 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000887 }
888 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100889 /* The protocol does not include a TCP header */
890 conn->err_code = CO_ER_CIP_BAD_PROTO;
891 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000892 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100893
David Carlier3015a2e2016-07-04 22:51:33 +0100894 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100895
896 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200897 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
898 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
899 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100900
Willy Tarreau226572f2019-07-17 14:46:00 +0200901 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
902 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
903 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100904
905 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
906 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100907 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100908 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100909 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100910
911 hdr_ip6 = (struct ip6_hdr *)line;
912
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200913 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100914 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000915 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100916 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000917 }
918 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100919 /* The protocol does not include a TCP header */
920 conn->err_code = CO_ER_CIP_BAD_PROTO;
921 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000922 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100923
David Carlier3015a2e2016-07-04 22:51:33 +0100924 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100925
926 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200927 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
928 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
929 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100930
Willy Tarreau226572f2019-07-17 14:46:00 +0200931 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
932 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
933 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100934
935 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
936 }
937 else {
938 /* The protocol does not match something known (IPv4/IPv6) */
939 conn->err_code = CO_ER_CIP_BAD_PROTO;
940 goto fail;
941 }
942
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000943 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200944 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100945
946 /* remove the NetScaler Client IP header from the request. For this
947 * we re-read the exact line at once. If we don't get the exact same
948 * result, we fail.
949 */
950 do {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200951 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100952 if (len2 < 0 && errno == EINTR)
953 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200954 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100955 goto recv_abort;
956 } while (0);
957
958 conn->flags &= ~flag;
959 return 1;
960
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200961 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200962 return 0;
963
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100964 missing:
965 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
966 * we have not read anything. Otherwise we need to fail because we won't
967 * be able to poll anymore.
968 */
969 conn->err_code = CO_ER_CIP_TRUNCATED;
970 goto fail;
971
972 bad_magic:
973 conn->err_code = CO_ER_CIP_BAD_MAGIC;
974 goto fail;
975
976 recv_abort:
977 conn->err_code = CO_ER_CIP_ABORT;
978 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
979 goto fail;
980
981 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100982 conn->flags |= CO_FL_ERROR;
983 return 0;
984}
985
Alexander Liu2a54bb72019-05-22 19:44:48 +0800986
987int conn_send_socks4_proxy_request(struct connection *conn)
988{
989 struct socks4_request req_line;
990
991 /* we might have been called just after an asynchronous shutw */
992 if (conn->flags & CO_FL_SOCK_WR_SH)
993 goto out_error;
994
995 if (!conn_ctrl_ready(conn))
996 goto out_error;
997
Willy Tarreau226572f2019-07-17 14:46:00 +0200998 if (!conn_get_dst(conn))
999 goto out_error;
1000
Alexander Liu2a54bb72019-05-22 19:44:48 +08001001 req_line.version = 0x04;
1002 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001003 req_line.port = get_net_port(conn->dst);
1004 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001005 memcpy(req_line.user_id, "HAProxy\0", 8);
1006
1007 if (conn->send_proxy_ofs > 0) {
1008 /*
1009 * This is the first call to send the request
1010 */
1011 conn->send_proxy_ofs = -(int)sizeof(req_line);
1012 }
1013
1014 if (conn->send_proxy_ofs < 0) {
1015 int ret = 0;
1016
1017 /* we are sending the socks4_req_line here. If the data layer
1018 * has a pending write, we'll also set MSG_MORE.
1019 */
1020 ret = conn_sock_send(
1021 conn,
1022 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1023 -conn->send_proxy_ofs,
1024 (conn->flags & CO_FL_XPRT_WR_ENA) ? MSG_MORE : 0);
1025
1026 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1027 conn->handle.fd, -conn->send_proxy_ofs, ret);
1028
1029 if (ret < 0) {
1030 goto out_error;
1031 }
1032
1033 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1034 if (conn->send_proxy_ofs != 0) {
1035 goto out_wait;
1036 }
1037 }
1038
1039 /* OK we've the whole request sent */
1040 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001041
1042 /* The connection is ready now, simply return and let the connection
1043 * handler notify upper layers if needed.
1044 */
1045 if (conn->flags & CO_FL_WAIT_L4_CONN)
1046 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1047
1048 if (conn->flags & CO_FL_SEND_PROXY) {
1049 /*
1050 * Get the send_proxy_ofs ready for the send_proxy due to we are
1051 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1052 * before sending PROXY Protocol.
1053 */
1054 conn->send_proxy_ofs = 1;
1055 }
1056 return 1;
1057
1058 out_error:
1059 /* Write error on the file descriptor */
1060 conn->flags |= CO_FL_ERROR;
1061 if (conn->err_code == CO_ER_NONE) {
1062 conn->err_code = CO_ER_SOCKS4_SEND;
1063 }
1064 return 0;
1065
1066 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001067 return 0;
1068}
1069
1070int conn_recv_socks4_proxy_response(struct connection *conn)
1071{
1072 char line[SOCKS4_HS_RSP_LEN];
1073 int ret;
1074
1075 /* we might have been called just after an asynchronous shutr */
1076 if (conn->flags & CO_FL_SOCK_RD_SH)
1077 goto fail;
1078
1079 if (!conn_ctrl_ready(conn))
1080 goto fail;
1081
1082 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001083 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001084
1085 do {
1086 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1087 * Try to peek into it, before all 8 bytes ready.
1088 */
1089 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1090
1091 if (ret == 0) {
1092 /* the socket has been closed or shutdown for send */
1093 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1094 conn->handle.fd, ret, errno);
1095 if (conn->err_code == CO_ER_NONE) {
1096 conn->err_code = CO_ER_SOCKS4_RECV;
1097 }
1098 goto fail;
1099 }
1100
1101 if (ret > 0) {
1102 if (ret == SOCKS4_HS_RSP_LEN) {
1103 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1104 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1105 }else{
1106 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]);
1107 }
1108 } else {
1109 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1110 }
1111
1112 if (ret < 0) {
1113 if (errno == EINTR) {
1114 continue;
1115 }
1116 if (errno == EAGAIN) {
1117 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001118 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001119 }
1120 goto recv_abort;
1121 }
1122 } while (0);
1123
1124 if (ret < SOCKS4_HS_RSP_LEN) {
1125 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1126 * we are not able to read enough data.
1127 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001128 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001129 }
1130
1131 /*
1132 * Base on the SOCSK4 protocol:
1133 *
1134 * +----+----+----+----+----+----+----+----+
1135 * | VN | CD | DSTPORT | DSTIP |
1136 * +----+----+----+----+----+----+----+----+
1137 * # of bytes: 1 1 2 4
1138 * VN is the version of the reply code and should be 0. CD is the result
1139 * code with one of the following values:
1140 * 90: request granted
1141 * 91: request rejected or failed
1142 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1143 * 93: request rejected because the client program and identd report different user-ids
1144 * The remaining fields are ignored.
1145 */
1146 if (line[1] != 90) {
1147 conn->flags &= ~CO_FL_SOCKS4_RECV;
1148
1149 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1150 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1151 if (conn->err_code == CO_ER_NONE) {
1152 conn->err_code = CO_ER_SOCKS4_DENY;
1153 }
1154 goto fail;
1155 }
1156
1157 /* remove the 8 bytes response from the stream */
1158 do {
1159 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1160 if (ret < 0 && errno == EINTR) {
1161 continue;
1162 }
1163 if (ret != SOCKS4_HS_RSP_LEN) {
1164 if (conn->err_code == CO_ER_NONE) {
1165 conn->err_code = CO_ER_SOCKS4_RECV;
1166 }
1167 goto fail;
1168 }
1169 } while (0);
1170
1171 conn->flags &= ~CO_FL_SOCKS4_RECV;
1172 return 1;
1173
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001174 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001175 return 0;
1176
Alexander Liu2a54bb72019-05-22 19:44:48 +08001177 recv_abort:
1178 if (conn->err_code == CO_ER_NONE) {
1179 conn->err_code = CO_ER_SOCKS4_ABORT;
1180 }
1181 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1182 goto fail;
1183
1184 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001185 conn->flags |= CO_FL_ERROR;
1186 return 0;
1187}
1188
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001189/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001190int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1191{
1192 int ret = 0;
1193
1194 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1195 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1196 }
1197 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001198 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1199 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001200 else
1201 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1202 }
1203
1204 return ret;
1205}
1206
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001207/* Makes a PROXY protocol line from the two addresses. The output is sent to
1208 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1209 * It returns the number of bytes composing this line (including the trailing
1210 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001211 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1212 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001213 */
David Safb76832014-05-08 23:42:08 -04001214int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001215{
1216 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001217 char * protocol;
1218 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1219 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1220 in_port_t src_port;
1221 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001222
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001223 if ( !src
1224 || !dst
1225 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1226 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1227 /* unknown family combination */
1228 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001229 if (ret >= buf_len)
1230 return 0;
1231
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001232 return ret;
1233 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001234
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001235 /* IPv4 for both src and dst */
1236 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1237 protocol = "TCP4";
1238 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001239 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001240 src_port = ((struct sockaddr_in *)src)->sin_port;
1241 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001242 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001243 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001244 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001245 /* IPv6 for at least one of src and dst */
1246 else {
1247 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001248
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001249 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001250
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001251 if (src->ss_family == AF_INET) {
1252 /* Convert src to IPv6 */
1253 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1254 src_port = ((struct sockaddr_in *)src)->sin_port;
1255 }
1256 else {
1257 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1258 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1259 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001260
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001261 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001262 return 0;
1263
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001264 if (dst->ss_family == AF_INET) {
1265 /* Convert dst to IPv6 */
1266 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1267 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1268 }
1269 else {
1270 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1271 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1272 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001273
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001274 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001275 return 0;
1276 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001277
1278 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1279 if (ret >= buf_len)
1280 return 0;
1281
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001282 return ret;
1283}
David Safb76832014-05-08 23:42:08 -04001284
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001285static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001286{
1287 struct tlv *tlv;
1288
1289 if (!dest || (length + sizeof(*tlv) > dest_len))
1290 return 0;
1291
1292 tlv = (struct tlv *)dest;
1293
1294 tlv->type = type;
1295 tlv->length_hi = length >> 8;
1296 tlv->length_lo = length & 0x00ff;
1297 memcpy(tlv->value, value, length);
1298 return length + sizeof(*tlv);
1299}
David Safb76832014-05-08 23:42:08 -04001300
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001301/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001302int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1303{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001304 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001305 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001306 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001307 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001308 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001309 struct sockaddr_storage *src = &null_addr;
1310 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001311 const char *value;
1312 int value_len;
David Safb76832014-05-08 23:42:08 -04001313
1314 if (buf_len < PP2_HEADER_LEN)
1315 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001316 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001317
Willy Tarreau226572f2019-07-17 14:46:00 +02001318 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1319 src = remote->src;
1320 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001321 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001322
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001323 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1324 if ( !src
1325 || !dst
1326 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1327 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001328 if (buf_len < PP2_HDR_LEN_UNSPEC)
1329 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001330 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1331 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001332 ret = PP2_HDR_LEN_UNSPEC;
1333 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001334 else {
1335 /* IPv4 for both src and dst */
1336 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1337 if (buf_len < PP2_HDR_LEN_INET)
1338 return 0;
1339 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1340 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1341 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1342 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1343 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1344 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1345 ret = PP2_HDR_LEN_INET;
1346 }
1347 /* IPv6 for at least one of src and dst */
1348 else {
1349 struct in6_addr tmp;
1350
1351 if (buf_len < PP2_HDR_LEN_INET6)
1352 return 0;
1353 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1354 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1355 if (src->ss_family == AF_INET) {
1356 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1357 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1358 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1359 }
1360 else {
1361 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1362 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1363 }
1364 if (dst->ss_family == AF_INET) {
1365 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1366 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
1367 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1368 }
1369 else {
1370 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1371 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1372 }
1373
1374 ret = PP2_HDR_LEN_INET6;
1375 }
1376 }
David Safb76832014-05-08 23:42:08 -04001377
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001378 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1379 uint32_t zero_crc32c = 0;
1380 if ((buf_len - ret) < sizeof(struct tlv))
1381 return 0;
1382 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1383 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1384 }
1385
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001386 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001387 if ((buf_len - ret) < sizeof(struct tlv))
1388 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001389 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001390 }
1391
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001392 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001393 value = NULL;
1394 if (remote && remote->proxy_authority) {
1395 value = remote->proxy_authority;
1396 value_len = remote->proxy_authority_len;
1397 }
1398#ifdef USE_OPENSSL
1399 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001400 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001401 value_len = strlen(value);
1402 }
1403#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001404 if (value) {
1405 if ((buf_len - ret) < sizeof(struct tlv))
1406 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001407 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001408 }
1409 }
1410
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001411#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001412 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001413 struct tlv_ssl *tlv;
1414 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001415 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1416 return 0;
1417 tlv = (struct tlv_ssl *)&buf[ret];
1418 memset(tlv, 0, sizeof(struct tlv_ssl));
1419 ssl_tlv_len += sizeof(struct tlv_ssl);
1420 tlv->tlv.type = PP2_TYPE_SSL;
1421 if (ssl_sock_is_ssl(remote)) {
1422 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001423 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001424 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001425 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 -04001426 }
Dave McCowan328fb582014-07-30 10:39:13 -04001427 if (ssl_sock_get_cert_used_sess(remote)) {
1428 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001429 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001430 if (ssl_sock_get_cert_used_conn(remote))
1431 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001432 }
1433 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001434 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001435 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001436 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1437 cn_trash->data,
1438 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001439 }
1440 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001441 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001442 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001443 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001444 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1445 pkey_trash->data,
1446 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001447 }
1448 }
1449 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1450 value = ssl_sock_get_cert_sig(remote);
1451 if (value) {
1452 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1453 }
1454 }
1455 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1456 value = ssl_sock_get_cipher_name(remote);
1457 if (value) {
1458 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1459 }
1460 }
David Safb76832014-05-08 23:42:08 -04001461 }
1462 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1463 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1464 ret += ssl_tlv_len;
1465 }
1466#endif
1467
Willy Tarreaue5733232019-05-22 19:24:06 +02001468#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001469 if (remote && (remote->proxy_netns)) {
1470 if ((buf_len - ret) < sizeof(struct tlv))
1471 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001472 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 +01001473 }
1474#endif
1475
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001476 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001477
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001478 if (tlv_crc32c_p) {
1479 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1480 }
1481
David Safb76832014-05-08 23:42:08 -04001482 return ret;
1483}
Emeric Brun4f603012017-01-05 15:11:44 +01001484
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001485/* return the major HTTP version as 1 or 2 depending on how the request arrived
1486 * before being processed.
1487 */
1488static int
1489smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1490{
Jérôme Magnin86577422018-12-07 09:03:11 +01001491 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1492 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001493
1494 smp->data.type = SMP_T_SINT;
1495 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1496 return 1;
1497}
1498
Emeric Brun4f603012017-01-05 15:11:44 +01001499/* fetch if the received connection used a PROXY protocol header */
1500int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1501{
1502 struct connection *conn;
1503
1504 conn = objt_conn(smp->sess->origin);
1505 if (!conn)
1506 return 0;
1507
1508 if (!(conn->flags & CO_FL_CONNECTED)) {
1509 smp->flags |= SMP_F_MAY_CHANGE;
1510 return 0;
1511 }
1512
1513 smp->flags = 0;
1514 smp->data.type = SMP_T_BOOL;
1515 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1516
1517 return 1;
1518}
1519
Geoff Simmons7185b782019-08-27 18:31:16 +02001520/* fetch the authority TLV from a PROXY protocol header */
1521int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1522{
1523 struct connection *conn;
1524
1525 conn = objt_conn(smp->sess->origin);
1526 if (!conn)
1527 return 0;
1528
1529 if (!(conn->flags & CO_FL_CONNECTED)) {
1530 smp->flags |= SMP_F_MAY_CHANGE;
1531 return 0;
1532 }
1533
1534 if (conn->proxy_authority == NULL)
1535 return 0;
1536
1537 smp->flags = 0;
1538 smp->data.type = SMP_T_STR;
1539 smp->data.u.str.area = conn->proxy_authority;
1540 smp->data.u.str.data = conn->proxy_authority_len;
1541
1542 return 1;
1543}
1544
Emeric Brun4f603012017-01-05 15:11:44 +01001545/* Note: must not be declared <const> as its list will be overwritten.
1546 * Note: fetches that may return multiple types must be declared as the lowest
1547 * common denominator, the type that can be casted into all other ones. For
1548 * instance v4/v6 must be declared v4.
1549 */
1550static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001551 { "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 +01001552 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001553 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001554 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001555 { /* END */ },
1556}};
1557
Willy Tarreau0108d902018-11-25 19:14:37 +01001558INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);