blob: ae53d56df3984ab93bf8ffb97933c1d109ea8443 [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 Tarreau7d281492012-12-16 19:19:13 +010083 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010084
Willy Tarreaub2a7ab02019-12-27 10:54:22 +010085 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) &&
86 ((fd_send_ready(fd) && fd_send_active(fd)) ||
87 (fd_recv_ready(fd) && fd_recv_active(fd)))) {
88 /* Still waiting for a connection to establish and nothing was
89 * attempted yet to probe the connection. this will clear the
90 * CO_FL_WAIT_L4_CONN flag on success.
91 */
92 if (!conn_fd_check(conn))
93 goto leave;
94 }
95
Willy Tarreau8081abe2019-11-28 18:08:49 +010096 if (fd_send_ready(fd) && fd_send_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +010097 /* force reporting of activity by clearing the previous flags :
98 * we'll have at least ERROR or CONNECTED at the end of an I/O,
99 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200100 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100101 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100102 if (conn->subs && conn->subs->events & SUB_RETRY_SEND) {
103 tasklet_wakeup(conn->subs->tasklet);
104 conn->subs->events &= ~SUB_RETRY_SEND;
105 if (!conn->subs->events)
106 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200107 } else
108 io_available = 1;
Willy Tarreau667fefd2020-03-04 17:22:10 +0100109 fd_stop_send(fd);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200110 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200111
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200112 /* The data transfer starts here and stops on error and handshakes. Note
113 * that we must absolutely test conn->xprt at each step in case it suddenly
114 * changes due to a quick unexpected close().
115 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100116 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100117 /* force reporting of activity by clearing the previous flags :
118 * we'll have at least ERROR or CONNECTED at the end of an I/O,
119 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200120 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100121 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100122 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
123 tasklet_wakeup(conn->subs->tasklet);
124 conn->subs->events &= ~SUB_RETRY_RECV;
125 if (!conn->subs->events)
126 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200127 } else
128 io_available = 1;
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200129 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200130
Willy Tarreau2c6be842012-07-06 17:12:34 +0200131 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100132 /* If we don't yet have a mux, that means we were waiting for
133 * informations to create one, typically from the ALPN. If we're
134 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200135 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100136 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100137 if (conn_create_mux(conn) < 0)
138 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200139
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100140 /* The wake callback is normally used to notify the data layer about
141 * data layer activity (successful send/recv), connection establishment,
142 * shutdown and fatal errors. We need to consider the following
143 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100144 * - change among the CO_FL_NOTIFY_DONE flags :
145 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100146 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
147 * end of handshake and transition to CONNECTED
148 * - raise of CONNECTED with HANDSHAKE down
149 * - end of HANDSHAKE with CONNECTED set
150 * - regular data layer activity
151 *
152 * Note that the wake callback is allowed to release the connection and
153 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200154 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100155 if ((io_available || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
156 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200157 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200158 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200159
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200160 /* commit polling changes */
161 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200162 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200163}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100165/* This is the callback which is set when a connection establishment is pending
166 * and we have nothing to send. It may update the FD polling status to indicate
167 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
168 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
169 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
170 * leaves the error code in errno.
171 */
172int conn_fd_check(struct connection *conn)
173{
174 struct sockaddr_storage *addr;
175 int fd = conn->handle.fd;
176
177 if (conn->flags & CO_FL_ERROR)
178 return 0;
179
180 if (!conn_ctrl_ready(conn))
181 return 0;
182
183 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
184 return 1; /* strange we were called while ready */
185
186 if (!fd_send_ready(fd))
187 return 0;
188
189 /* Here we have 2 cases :
190 * - modern pollers, able to report ERR/HUP. If these ones return any
191 * of these flags then it's likely a failure, otherwise it possibly
192 * is a success (i.e. there may have been data received just before
193 * the error was reported).
194 * - select, which doesn't report these and with which it's always
195 * necessary either to try connect() again or to check for SO_ERROR.
196 * In order to simplify everything, we double-check using connect() as
197 * soon as we meet either of these delicate situations. Note that
198 * SO_ERROR would clear the error after reporting it!
199 */
200 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
201 /* modern poller, able to report ERR/HUP */
202 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
203 goto done;
204 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
205 goto done;
206 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
207 goto wait;
208 /* error present, fall through common error check path */
209 }
210
211 /* Use connect() to check the state of the socket. This has the double
212 * advantage of *not* clearing the error (so that health checks can
213 * still use getsockopt(SO_ERROR)) and giving us the following info :
214 * - error
215 * - connecting (EALREADY, EINPROGRESS)
216 * - connected (EISCONN, 0)
217 */
218 addr = conn->dst;
219 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
220 addr = &objt_server(conn->target)->socks4_addr;
221
222 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
223 if (errno == EALREADY || errno == EINPROGRESS)
224 goto wait;
225
226 if (errno && errno != EISCONN)
227 goto out_error;
228 }
229
230 done:
231 /* The FD is ready now, we'll mark the connection as complete and
232 * forward the event to the transport layer which will notify the
233 * data layer.
234 */
235 conn->flags &= ~CO_FL_WAIT_L4_CONN;
236 fd_may_send(fd);
237 fd_cond_recv(fd);
238 errno = 0; // make health checks happy
239 return 1;
240
241 out_error:
242 /* Write error on the file descriptor. Report it to the connection
243 * and disable polling on this FD.
244 */
245 fdtab[fd].linger_risk = 0;
246 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau5d4d1802020-02-21 09:58:29 +0100247 conn_stop_polling(conn);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100248 return 0;
249
250 wait:
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100251 fd_cant_send(fd);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100252 fd_want_send(fd);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100253 return 0;
254}
255
Willy Tarreauff3e6482015-03-12 23:56:52 +0100256/* Send a message over an established connection. It makes use of send() and
257 * returns the same return code and errno. If the socket layer is not ready yet
258 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
259 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
260 * EMSGSIZE if called with a zero length message. The purpose is to simplify
261 * some rare attempts to directly write on the socket from above the connection
262 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
263 * It automatically retries on EINTR. Other errors cause the connection to be
264 * marked as in error state. It takes similar arguments as send() except the
265 * first one which is the connection instead of the file descriptor. Note,
266 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
267 */
268int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
269{
270 int ret;
271
272 ret = -1;
273 errno = ENOTSOCK;
274
275 if (conn->flags & CO_FL_SOCK_WR_SH)
276 goto fail;
277
278 if (!conn_ctrl_ready(conn))
279 goto fail;
280
281 errno = EMSGSIZE;
282 if (!len)
283 goto fail;
284
Willy Tarreau585744b2017-08-24 14:31:19 +0200285 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100286 goto wait;
287
288 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200289 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100290 } while (ret < 0 && errno == EINTR);
291
292
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200293 if (ret > 0) {
294 if (conn->flags & CO_FL_WAIT_L4_CONN) {
295 conn->flags &= ~CO_FL_WAIT_L4_CONN;
296 fd_may_send(conn->handle.fd);
297 fd_cond_recv(conn->handle.fd);
298 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100299 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200300 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100301
302 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
303 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200304 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100305 return 0;
306 }
307 fail:
308 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
309 return ret;
310}
311
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100312/* Called from the upper layer, to subscribe <es> to events <event_type>. The
313 * event subscriber <es> is not allowed to change from a previous call as long
314 * as at least one event is still subscribed. The <event_type> must only be a
315 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
316 */
317int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200318{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100319 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100320 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100321
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100322 es->events &= ~event_type;
323 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100324 conn->subs = NULL;
325
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100326 if (conn_ctrl_ready(conn)) {
327 if (event_type & SUB_RETRY_RECV)
328 fd_stop_recv(conn->handle.fd);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100329
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100330 if (event_type & SUB_RETRY_SEND)
331 fd_stop_send(conn->handle.fd);
332 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200333 return 0;
334}
335
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100336/* Called from the upper layer, to subscribe <es> to events <event_type>.
337 * The <es> struct is not allowed to differ from the one passed during a
338 * previous call to subscribe(). If the FD is ready, the wait_event is
339 * immediately woken up and the subcription is cancelled. It always
340 * returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100341 */
342int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200343{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100344 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100345 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100346
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100347 if (conn->subs && (conn->subs->events & event_type) == event_type)
348 return 0;
349
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100350 conn->subs = es;
351 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100352
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100353 if (conn_ctrl_ready(conn)) {
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100354 if (event_type & SUB_RETRY_RECV) {
355 if (fd_recv_ready(conn->handle.fd)) {
356 tasklet_wakeup(es->tasklet);
357 es->events &= ~SUB_RETRY_RECV;
358 if (!es->events)
359 conn->subs = NULL;
360 }
361 else
362 fd_want_recv(conn->handle.fd);
363 }
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100364
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100365 if (event_type & SUB_RETRY_SEND) {
366 if (fd_send_ready(conn->handle.fd)) {
367 tasklet_wakeup(es->tasklet);
368 es->events &= ~SUB_RETRY_SEND;
369 if (!es->events)
370 conn->subs = NULL;
371 }
372 else
373 fd_want_send(conn->handle.fd);
374 }
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100375 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200376 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200377}
378
Willy Tarreaud85c4852015-03-13 00:40:28 +0100379/* Drains possibly pending incoming data on the file descriptor attached to the
380 * connection and update the connection's flags accordingly. This is used to
381 * know whether we need to disable lingering on close. Returns non-zero if it
382 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
383 * flag may also be updated if the incoming shutdown was reported by the drain()
384 * function.
385 */
386int conn_sock_drain(struct connection *conn)
387{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200388 int turns = 2;
389 int len;
390
Willy Tarreaud85c4852015-03-13 00:40:28 +0100391 if (!conn_ctrl_ready(conn))
392 return 1;
393
394 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
395 return 1;
396
Willy Tarreaue215bba2018-08-24 14:31:53 +0200397 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
398 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100399
Willy Tarreaue215bba2018-08-24 14:31:53 +0200400 if (!fd_recv_ready(conn->handle.fd))
401 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100402
Willy Tarreaue215bba2018-08-24 14:31:53 +0200403 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200404 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100405 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200406 goto shut;
407 }
408
409 /* no drain function defined, use the generic one */
410
411 while (turns) {
412#ifdef MSG_TRUNC_CLEARS_INPUT
413 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
414 if (len == -1 && errno == EFAULT)
415#endif
416 len = recv(conn->handle.fd, trash.area, trash.size,
417 MSG_DONTWAIT | MSG_NOSIGNAL);
418
419 if (len == 0)
420 goto shut;
421
422 if (len < 0) {
423 if (errno == EAGAIN) {
424 /* connection not closed yet */
425 fd_cant_recv(conn->handle.fd);
426 break;
427 }
428 if (errno == EINTR) /* oops, try again */
429 continue;
430 /* other errors indicate a dead connection, fine. */
431 goto shut;
432 }
433 /* OK we read some data, let's try again once */
434 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100435 }
436
Willy Tarreaue215bba2018-08-24 14:31:53 +0200437 /* some data are still present, give up */
438 return 0;
439
440 shut:
441 /* we're certain the connection was shut down */
442 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100443 conn->flags |= CO_FL_SOCK_RD_SH;
444 return 1;
445}
446
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100447/*
448 * Get data length from tlv
449 */
450static int get_tlv_length(const struct tlv *src)
451{
452 return (src->length_hi << 8) | src->length_lo;
453}
454
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200455/* This handshake handler waits a PROXY protocol header at the beginning of the
456 * raw data stream. The header looks like this :
457 *
458 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
459 *
460 * There must be exactly one space between each field. Fields are :
461 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
462 * - SRC3 : layer 3 (eg: IP) source address in standard text form
463 * - DST3 : layer 3 (eg: IP) destination address in standard text form
464 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
465 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
466 *
467 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
468 *
469 * The header line is small and in all cases smaller than the smallest normal
470 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
471 * can safely use MSG_PEEK and avoid buffering.
472 *
473 * Once the data is fetched, the values are set in the connection's address
474 * fields, and data are removed from the socket's buffer. The function returns
475 * zero if it needs to wait for more data or if it fails, or 1 if it completed
476 * and removed itself.
477 */
478int conn_recv_proxy(struct connection *conn, int flag)
479{
480 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200481 struct proxy_hdr_v2 *hdr_v2;
482 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100483 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200484 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200485 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200486
Willy Tarreau3c728722014-01-23 13:50:42 +0100487 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200488 goto fail;
489
Willy Tarreauca79f592019-07-17 19:04:47 +0200490 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
491 goto fail;
492
Willy Tarreau585744b2017-08-24 14:31:19 +0200493 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200494 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100495
Willy Tarreau157788c2020-02-11 10:08:05 +0100496 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200497 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
498 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200499 if (errno == EINTR)
500 continue;
501 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200502 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200503 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200504 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100505 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200506 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200507 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100508 break;
509 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200510
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100512 /* client shutdown */
513 conn->err_code = CO_ER_PRX_EMPTY;
514 goto fail;
515 }
516
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100517 conn->flags &= ~CO_FL_WAIT_L4_CONN;
518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200520 goto missing;
521
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200522 line = trash.area;
523 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200524
525 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200526 if (strncmp(line, "PROXY ", 6) != 0)
527 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200528
529 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200531 goto missing;
532
David CARLIER42ff05e2016-03-24 09:22:36 +0000533 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200534 u32 src3, dst3, sport, dport;
535
536 line += 5;
537
538 src3 = 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 dst3 = inetaddr_host_lim_ret(line, end, &line);
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 sport = read_uint((const char **)&line, end);
551 if (line == end)
552 goto missing;
553 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100554 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200555
556 dport = read_uint((const char **)&line, end);
557 if (line > end - 2)
558 goto missing;
559 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100560 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200561 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100562 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200563
564 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200565 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
566 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
567 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200568
Willy Tarreau226572f2019-07-17 14:46:00 +0200569 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
570 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
571 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200572 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
573 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000574 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200575 u32 sport, dport;
576 char *src_s;
577 char *dst_s, *sport_s, *dport_s;
578 struct in6_addr src3, dst3;
579
580 line += 5;
581
582 src_s = line;
583 dst_s = sport_s = dport_s = NULL;
584 while (1) {
585 if (line > end - 2) {
586 goto missing;
587 }
588 else if (*line == '\r') {
589 *line = 0;
590 line++;
591 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100592 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200593 break;
594 }
595
596 if (*line == ' ') {
597 *line = 0;
598 if (!dst_s)
599 dst_s = line + 1;
600 else if (!sport_s)
601 sport_s = line + 1;
602 else if (!dport_s)
603 dport_s = line + 1;
604 }
605 line++;
606 }
607
608 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100609 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200610
611 sport = read_uint((const char **)&sport_s,dport_s - 1);
612 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100613 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200614
615 dport = read_uint((const char **)&dport_s,line - 2);
616 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100617 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200618
619 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100620 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200621
622 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100623 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200624
625 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200626 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
627 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
628 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200629
Willy Tarreau226572f2019-07-17 14:46:00 +0200630 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
631 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
632 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
634 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200635 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
636 /* This can be a UNIX socket forwarded by an haproxy upstream */
637 line += 9;
638 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200639 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200640 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100641 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200642 goto fail;
643 }
644
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200645 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200646 goto eat_header;
647
648 not_v1:
649 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200650 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200651 goto missing;
652
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200654
655 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
656 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
657 conn->err_code = CO_ER_PRX_NOT_HDR;
658 goto fail;
659 }
660
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200661 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200662 goto missing;
663
664 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
665 case 0x01: /* PROXY command */
666 switch (hdr_v2->fam) {
667 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100668 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
669 goto bad_header;
670
Willy Tarreau226572f2019-07-17 14:46:00 +0200671 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
672 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
673 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
674 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
675 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
676 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200677 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200678 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100679 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200680 break;
681 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100682 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
683 goto bad_header;
684
Willy Tarreau226572f2019-07-17 14:46:00 +0200685 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
686 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
687 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
688 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
689 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
690 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200691 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200692 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100693 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200694 break;
695 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100696
697 /* TLV parsing */
698 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
700 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100701 const int tlv_len = get_tlv_length(tlv_packet);
702 tlv_offset += tlv_len + TLV_HEADER_SIZE;
703
704 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100705 case PP2_TYPE_CRC32C: {
706 void *tlv_crc32c_p = (void *)tlv_packet->value;
707 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
708 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100710 goto bad_header;
711 break;
712 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200713#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100714 case PP2_TYPE_NETNS: {
715 const struct netns_entry *ns;
716 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
717 if (ns)
718 conn->proxy_netns = ns;
719 break;
720 }
721#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200722 case PP2_TYPE_AUTHORITY: {
723 if (tlv_len > PP2_AUTHORITY_MAX)
724 goto bad_header;
725 conn->proxy_authority = pool_alloc(pool_head_authority);
726 if (conn->proxy_authority == NULL)
727 goto fail;
728 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
729 conn->proxy_authority_len = tlv_len;
730 break;
731 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100732 default:
733 break;
734 }
735 }
736 }
737
Willy Tarreau77992672014-06-14 11:06:17 +0200738 /* unsupported protocol, keep local connection address */
739 break;
740 case 0x00: /* LOCAL command */
741 /* keep local connection address for LOCAL */
742 break;
743 default:
744 goto bad_header; /* not a supported command */
745 }
746
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200748 goto eat_header;
749
750 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200751 /* remove the PROXY line from the request. For this we re-read the
752 * exact line at once. If we don't get the exact same result, we
753 * fail.
754 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100755 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200757 if (len2 < 0 && errno == EINTR)
758 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100760 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100761 break;
762 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200763
764 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100765 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200766 return 1;
767
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200768 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200769 return 0;
770
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200771 missing:
772 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
773 * we have not read anything. Otherwise we need to fail because we won't
774 * be able to poll anymore.
775 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100776 conn->err_code = CO_ER_PRX_TRUNCATED;
777 goto fail;
778
779 bad_header:
780 /* This is not a valid proxy protocol header */
781 conn->err_code = CO_ER_PRX_BAD_HDR;
782 goto fail;
783
784 recv_abort:
785 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100786 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100787 goto fail;
788
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200789 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200790 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200791 return 0;
792}
793
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100794/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000795 * at the beginning of the raw data stream. The header format is
796 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100797 *
798 * This line MUST be at the beginning of the buffer and MUST NOT be
799 * fragmented.
800 *
801 * The header line is small and in all cases smaller than the smallest normal
802 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
803 * can safely use MSG_PEEK and avoid buffering.
804 *
805 * Once the data is fetched, the values are set in the connection's address
806 * fields, and data are removed from the socket's buffer. The function returns
807 * zero if it needs to wait for more data or if it fails, or 1 if it completed
808 * and removed itself.
809 */
810int conn_recv_netscaler_cip(struct connection *conn, int flag)
811{
812 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000813 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100814 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200815 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100816
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100817 if (!conn_ctrl_ready(conn))
818 goto fail;
819
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100820 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
821 goto fail;
822
Willy Tarreau585744b2017-08-24 14:31:19 +0200823 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200824 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100825
Willy Tarreau157788c2020-02-11 10:08:05 +0100826 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200827 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
828 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100829 if (errno == EINTR)
830 continue;
831 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200832 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200833 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100834 }
835 goto recv_abort;
836 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200837 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100838 break;
839 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100840
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100841 conn->flags &= ~CO_FL_WAIT_L4_CONN;
842
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200843 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100844 /* client shutdown */
845 conn->err_code = CO_ER_CIP_EMPTY;
846 goto fail;
847 }
848
849 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000850 * CIP magic, header length or
851 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100853 goto missing;
854
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200855 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100856
857 /* Decode a possible NetScaler Client IP request, fail early if
858 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100859 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100860 goto bad_magic;
861
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000862 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200863 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100864 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000865 line += 8;
866 }
867 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200868 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100869 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000870 line += 12;
871 }
872 /* Unknown CIP protocol */
873 else {
874 conn->err_code = CO_ER_CIP_BAD_PROTO;
875 goto fail;
876 }
877
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100878 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000879 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200880 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100881 goto missing;
882
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100883 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100884 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100885
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100886 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100887 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100888 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100889
890 hdr_ip4 = (struct ip *)line;
891
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200892 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100893 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000894 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100895 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000896 }
897 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100898 /* The protocol does not include a TCP header */
899 conn->err_code = CO_ER_CIP_BAD_PROTO;
900 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000901 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100902
David Carlier3015a2e2016-07-04 22:51:33 +0100903 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100904
905 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200906 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
907 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
908 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100909
Willy Tarreau226572f2019-07-17 14:46:00 +0200910 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
911 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
912 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100913
914 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
915 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100916 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100917 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100918 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100919
920 hdr_ip6 = (struct ip6_hdr *)line;
921
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200922 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100923 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000924 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100925 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000926 }
927 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100928 /* The protocol does not include a TCP header */
929 conn->err_code = CO_ER_CIP_BAD_PROTO;
930 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000931 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100932
David Carlier3015a2e2016-07-04 22:51:33 +0100933 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100934
935 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200936 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
937 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
938 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100939
Willy Tarreau226572f2019-07-17 14:46:00 +0200940 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
941 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
942 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100943
944 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
945 }
946 else {
947 /* The protocol does not match something known (IPv4/IPv6) */
948 conn->err_code = CO_ER_CIP_BAD_PROTO;
949 goto fail;
950 }
951
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000952 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200953 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100954
955 /* remove the NetScaler Client IP header from the request. For this
956 * we re-read the exact line at once. If we don't get the exact same
957 * result, we fail.
958 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100959 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200960 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100961 if (len2 < 0 && errno == EINTR)
962 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200963 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100964 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100965 break;
966 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100967
968 conn->flags &= ~flag;
969 return 1;
970
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200971 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200972 return 0;
973
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100974 missing:
975 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
976 * we have not read anything. Otherwise we need to fail because we won't
977 * be able to poll anymore.
978 */
979 conn->err_code = CO_ER_CIP_TRUNCATED;
980 goto fail;
981
982 bad_magic:
983 conn->err_code = CO_ER_CIP_BAD_MAGIC;
984 goto fail;
985
986 recv_abort:
987 conn->err_code = CO_ER_CIP_ABORT;
988 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
989 goto fail;
990
991 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100992 conn->flags |= CO_FL_ERROR;
993 return 0;
994}
995
Alexander Liu2a54bb72019-05-22 19:44:48 +0800996
997int conn_send_socks4_proxy_request(struct connection *conn)
998{
999 struct socks4_request req_line;
1000
Alexander Liu2a54bb72019-05-22 19:44:48 +08001001 if (!conn_ctrl_ready(conn))
1002 goto out_error;
1003
Willy Tarreau226572f2019-07-17 14:46:00 +02001004 if (!conn_get_dst(conn))
1005 goto out_error;
1006
Alexander Liu2a54bb72019-05-22 19:44:48 +08001007 req_line.version = 0x04;
1008 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001009 req_line.port = get_net_port(conn->dst);
1010 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001011 memcpy(req_line.user_id, "HAProxy\0", 8);
1012
1013 if (conn->send_proxy_ofs > 0) {
1014 /*
1015 * This is the first call to send the request
1016 */
1017 conn->send_proxy_ofs = -(int)sizeof(req_line);
1018 }
1019
1020 if (conn->send_proxy_ofs < 0) {
1021 int ret = 0;
1022
1023 /* we are sending the socks4_req_line here. If the data layer
1024 * has a pending write, we'll also set MSG_MORE.
1025 */
1026 ret = conn_sock_send(
1027 conn,
1028 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1029 -conn->send_proxy_ofs,
Willy Tarreau19bc2012020-02-21 08:46:19 +01001030 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001031
1032 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1033 conn->handle.fd, -conn->send_proxy_ofs, ret);
1034
1035 if (ret < 0) {
1036 goto out_error;
1037 }
1038
1039 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1040 if (conn->send_proxy_ofs != 0) {
1041 goto out_wait;
1042 }
1043 }
1044
1045 /* OK we've the whole request sent */
1046 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001047
1048 /* The connection is ready now, simply return and let the connection
1049 * handler notify upper layers if needed.
1050 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001051 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001052
1053 if (conn->flags & CO_FL_SEND_PROXY) {
1054 /*
1055 * Get the send_proxy_ofs ready for the send_proxy due to we are
1056 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1057 * before sending PROXY Protocol.
1058 */
1059 conn->send_proxy_ofs = 1;
1060 }
1061 return 1;
1062
1063 out_error:
1064 /* Write error on the file descriptor */
1065 conn->flags |= CO_FL_ERROR;
1066 if (conn->err_code == CO_ER_NONE) {
1067 conn->err_code = CO_ER_SOCKS4_SEND;
1068 }
1069 return 0;
1070
1071 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001072 return 0;
1073}
1074
1075int conn_recv_socks4_proxy_response(struct connection *conn)
1076{
1077 char line[SOCKS4_HS_RSP_LEN];
1078 int ret;
1079
Alexander Liu2a54bb72019-05-22 19:44:48 +08001080 if (!conn_ctrl_ready(conn))
1081 goto fail;
1082
1083 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001084 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001085
Willy Tarreau157788c2020-02-11 10:08:05 +01001086 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001087 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1088 * Try to peek into it, before all 8 bytes ready.
1089 */
1090 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1091
1092 if (ret == 0) {
1093 /* the socket has been closed or shutdown for send */
1094 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1095 conn->handle.fd, ret, errno);
1096 if (conn->err_code == CO_ER_NONE) {
1097 conn->err_code = CO_ER_SOCKS4_RECV;
1098 }
1099 goto fail;
1100 }
1101
1102 if (ret > 0) {
1103 if (ret == SOCKS4_HS_RSP_LEN) {
1104 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1105 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1106 }else{
1107 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]);
1108 }
1109 } else {
1110 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1111 }
1112
1113 if (ret < 0) {
1114 if (errno == EINTR) {
1115 continue;
1116 }
1117 if (errno == EAGAIN) {
1118 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001119 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001120 }
1121 goto recv_abort;
1122 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001123 break;
1124 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001125
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001126 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1127
Alexander Liu2a54bb72019-05-22 19:44:48 +08001128 if (ret < SOCKS4_HS_RSP_LEN) {
1129 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1130 * we are not able to read enough data.
1131 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001132 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001133 }
1134
1135 /*
1136 * Base on the SOCSK4 protocol:
1137 *
1138 * +----+----+----+----+----+----+----+----+
1139 * | VN | CD | DSTPORT | DSTIP |
1140 * +----+----+----+----+----+----+----+----+
1141 * # of bytes: 1 1 2 4
1142 * VN is the version of the reply code and should be 0. CD is the result
1143 * code with one of the following values:
1144 * 90: request granted
1145 * 91: request rejected or failed
1146 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1147 * 93: request rejected because the client program and identd report different user-ids
1148 * The remaining fields are ignored.
1149 */
1150 if (line[1] != 90) {
1151 conn->flags &= ~CO_FL_SOCKS4_RECV;
1152
1153 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1154 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1155 if (conn->err_code == CO_ER_NONE) {
1156 conn->err_code = CO_ER_SOCKS4_DENY;
1157 }
1158 goto fail;
1159 }
1160
1161 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001162 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001163 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1164 if (ret < 0 && errno == EINTR) {
1165 continue;
1166 }
1167 if (ret != SOCKS4_HS_RSP_LEN) {
1168 if (conn->err_code == CO_ER_NONE) {
1169 conn->err_code = CO_ER_SOCKS4_RECV;
1170 }
1171 goto fail;
1172 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001173 break;
1174 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001175
1176 conn->flags &= ~CO_FL_SOCKS4_RECV;
1177 return 1;
1178
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001179 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001180 return 0;
1181
Alexander Liu2a54bb72019-05-22 19:44:48 +08001182 recv_abort:
1183 if (conn->err_code == CO_ER_NONE) {
1184 conn->err_code = CO_ER_SOCKS4_ABORT;
1185 }
1186 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1187 goto fail;
1188
1189 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001190 conn->flags |= CO_FL_ERROR;
1191 return 0;
1192}
1193
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001194/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001195int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1196{
1197 int ret = 0;
1198
1199 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1200 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1201 }
1202 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001203 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1204 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001205 else
1206 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1207 }
1208
1209 return ret;
1210}
1211
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001212/* Makes a PROXY protocol line from the two addresses. The output is sent to
1213 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1214 * It returns the number of bytes composing this line (including the trailing
1215 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001216 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1217 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001218 */
David Safb76832014-05-08 23:42:08 -04001219int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001220{
1221 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001222 char * protocol;
1223 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1224 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1225 in_port_t src_port;
1226 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001227
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001228 if ( !src
1229 || !dst
1230 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1231 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1232 /* unknown family combination */
1233 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001234 if (ret >= buf_len)
1235 return 0;
1236
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001237 return ret;
1238 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001239
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001240 /* IPv4 for both src and dst */
1241 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1242 protocol = "TCP4";
1243 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001244 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001245 src_port = ((struct sockaddr_in *)src)->sin_port;
1246 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001247 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001248 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001249 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001250 /* IPv6 for at least one of src and dst */
1251 else {
1252 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001253
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001254 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001255
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001256 if (src->ss_family == AF_INET) {
1257 /* Convert src to IPv6 */
1258 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1259 src_port = ((struct sockaddr_in *)src)->sin_port;
1260 }
1261 else {
1262 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1263 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1264 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001265
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001266 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001267 return 0;
1268
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001269 if (dst->ss_family == AF_INET) {
1270 /* Convert dst to IPv6 */
1271 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1272 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1273 }
1274 else {
1275 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1276 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1277 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001278
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001279 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001280 return 0;
1281 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001282
1283 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1284 if (ret >= buf_len)
1285 return 0;
1286
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001287 return ret;
1288}
David Safb76832014-05-08 23:42:08 -04001289
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001290static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001291{
1292 struct tlv *tlv;
1293
1294 if (!dest || (length + sizeof(*tlv) > dest_len))
1295 return 0;
1296
1297 tlv = (struct tlv *)dest;
1298
1299 tlv->type = type;
1300 tlv->length_hi = length >> 8;
1301 tlv->length_lo = length & 0x00ff;
1302 memcpy(tlv->value, value, length);
1303 return length + sizeof(*tlv);
1304}
David Safb76832014-05-08 23:42:08 -04001305
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001306/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001307int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1308{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001309 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001310 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001311 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001312 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001313 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001314 struct sockaddr_storage *src = &null_addr;
1315 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001316 const char *value;
1317 int value_len;
David Safb76832014-05-08 23:42:08 -04001318
1319 if (buf_len < PP2_HEADER_LEN)
1320 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001321 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001322
Willy Tarreau226572f2019-07-17 14:46:00 +02001323 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1324 src = remote->src;
1325 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001326 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001327
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001328 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1329 if ( !src
1330 || !dst
1331 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1332 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001333 if (buf_len < PP2_HDR_LEN_UNSPEC)
1334 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001335 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1336 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001337 ret = PP2_HDR_LEN_UNSPEC;
1338 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001339 else {
Willy Tarreau7f263912020-02-19 15:10:00 +01001340 /* Note: due to historic compatibility with V1 which required
1341 * to send "PROXY" with local addresses for local connections,
1342 * we can end up here with the remote in fact being our outgoing
1343 * connection. We still want to send real addresses and LOCAL on
1344 * it.
1345 */
1346 hdr->ver_cmd = PP2_VERSION;
1347 hdr->ver_cmd |= conn_is_back(remote) ? PP2_CMD_LOCAL : PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001348 /* IPv4 for both src and dst */
1349 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1350 if (buf_len < PP2_HDR_LEN_INET)
1351 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001352 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1353 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1354 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1355 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1356 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1357 ret = PP2_HDR_LEN_INET;
1358 }
1359 /* IPv6 for at least one of src and dst */
1360 else {
1361 struct in6_addr tmp;
1362
1363 if (buf_len < PP2_HDR_LEN_INET6)
1364 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001365 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1366 if (src->ss_family == AF_INET) {
1367 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1368 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1369 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1370 }
1371 else {
1372 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1373 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1374 }
1375 if (dst->ss_family == AF_INET) {
1376 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1377 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001378 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001379 }
1380 else {
1381 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1382 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1383 }
1384
1385 ret = PP2_HDR_LEN_INET6;
1386 }
1387 }
David Safb76832014-05-08 23:42:08 -04001388
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001389 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1390 uint32_t zero_crc32c = 0;
1391 if ((buf_len - ret) < sizeof(struct tlv))
1392 return 0;
1393 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1394 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1395 }
1396
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001397 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001398 if ((buf_len - ret) < sizeof(struct tlv))
1399 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001400 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001401 }
1402
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001403 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001404 value = NULL;
1405 if (remote && remote->proxy_authority) {
1406 value = remote->proxy_authority;
1407 value_len = remote->proxy_authority_len;
1408 }
1409#ifdef USE_OPENSSL
1410 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001411 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001412 value_len = strlen(value);
1413 }
1414#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001415 if (value) {
1416 if ((buf_len - ret) < sizeof(struct tlv))
1417 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001418 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001419 }
1420 }
1421
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001422#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001423 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001424 struct tlv_ssl *tlv;
1425 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001426 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1427 return 0;
1428 tlv = (struct tlv_ssl *)&buf[ret];
1429 memset(tlv, 0, sizeof(struct tlv_ssl));
1430 ssl_tlv_len += sizeof(struct tlv_ssl);
1431 tlv->tlv.type = PP2_TYPE_SSL;
1432 if (ssl_sock_is_ssl(remote)) {
1433 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001434 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001435 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001436 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 -04001437 }
Dave McCowan328fb582014-07-30 10:39:13 -04001438 if (ssl_sock_get_cert_used_sess(remote)) {
1439 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001440 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001441 if (ssl_sock_get_cert_used_conn(remote))
1442 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001443 }
1444 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001445 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001446 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001447 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1448 cn_trash->data,
1449 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001450 }
1451 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001452 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001453 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001454 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001455 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1456 pkey_trash->data,
1457 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001458 }
1459 }
1460 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1461 value = ssl_sock_get_cert_sig(remote);
1462 if (value) {
1463 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1464 }
1465 }
1466 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1467 value = ssl_sock_get_cipher_name(remote);
1468 if (value) {
1469 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1470 }
1471 }
David Safb76832014-05-08 23:42:08 -04001472 }
1473 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1474 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1475 ret += ssl_tlv_len;
1476 }
1477#endif
1478
Willy Tarreaue5733232019-05-22 19:24:06 +02001479#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001480 if (remote && (remote->proxy_netns)) {
1481 if ((buf_len - ret) < sizeof(struct tlv))
1482 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001483 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 +01001484 }
1485#endif
1486
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001487 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001488
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001489 if (tlv_crc32c_p) {
1490 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1491 }
1492
David Safb76832014-05-08 23:42:08 -04001493 return ret;
1494}
Emeric Brun4f603012017-01-05 15:11:44 +01001495
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001496/* return the major HTTP version as 1 or 2 depending on how the request arrived
1497 * before being processed.
1498 */
1499static int
1500smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1501{
Jérôme Magnin86577422018-12-07 09:03:11 +01001502 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1503 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001504
1505 smp->data.type = SMP_T_SINT;
1506 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1507 return 1;
1508}
1509
Emeric Brun4f603012017-01-05 15:11:44 +01001510/* fetch if the received connection used a PROXY protocol header */
1511int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1512{
1513 struct connection *conn;
1514
1515 conn = objt_conn(smp->sess->origin);
1516 if (!conn)
1517 return 0;
1518
Willy Tarreau911db9b2020-01-23 16:27:54 +01001519 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001520 smp->flags |= SMP_F_MAY_CHANGE;
1521 return 0;
1522 }
1523
1524 smp->flags = 0;
1525 smp->data.type = SMP_T_BOOL;
1526 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1527
1528 return 1;
1529}
1530
Geoff Simmons7185b782019-08-27 18:31:16 +02001531/* fetch the authority TLV from a PROXY protocol header */
1532int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1533{
1534 struct connection *conn;
1535
1536 conn = objt_conn(smp->sess->origin);
1537 if (!conn)
1538 return 0;
1539
Willy Tarreau911db9b2020-01-23 16:27:54 +01001540 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001541 smp->flags |= SMP_F_MAY_CHANGE;
1542 return 0;
1543 }
1544
1545 if (conn->proxy_authority == NULL)
1546 return 0;
1547
1548 smp->flags = 0;
1549 smp->data.type = SMP_T_STR;
1550 smp->data.u.str.area = conn->proxy_authority;
1551 smp->data.u.str.data = conn->proxy_authority_len;
1552
1553 return 1;
1554}
1555
Emeric Brun4f603012017-01-05 15:11:44 +01001556/* Note: must not be declared <const> as its list will be overwritten.
1557 * Note: fetches that may return multiple types must be declared as the lowest
1558 * common denominator, the type that can be casted into all other ones. For
1559 * instance v4/v6 must be declared v4.
1560 */
1561static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001562 { "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 +01001563 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001564 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001565 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001566 { /* END */ },
1567}};
1568
Willy Tarreau0108d902018-11-25 19:14:37 +01001569INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);