blob: ee6818c3d94d8ee3311e9e5b564698ad7cb2748d [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 Tarreau9e272bf2012-10-03 21:04:48 +0200109 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200110
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200111 /* The data transfer starts here and stops on error and handshakes. Note
112 * that we must absolutely test conn->xprt at each step in case it suddenly
113 * changes due to a quick unexpected close().
114 */
Willy Tarreau8081abe2019-11-28 18:08:49 +0100115 if (fd_recv_ready(fd) && fd_recv_active(fd)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100116 /* force reporting of activity by clearing the previous flags :
117 * we'll have at least ERROR or CONNECTED at the end of an I/O,
118 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200119 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100120 flags = 0;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100121 if (conn->subs && conn->subs->events & SUB_RETRY_RECV) {
122 tasklet_wakeup(conn->subs->tasklet);
123 conn->subs->events &= ~SUB_RETRY_RECV;
124 if (!conn->subs->events)
125 conn->subs = NULL;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200126 } else
127 io_available = 1;
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200128 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200129
Willy Tarreau2c6be842012-07-06 17:12:34 +0200130 leave:
Olivier Houchard477902b2020-01-22 18:08:48 +0100131 /* If we don't yet have a mux, that means we were waiting for
132 * informations to create one, typically from the ALPN. If we're
133 * done with the handshake, attempt to create one.
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200134 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100135 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard477902b2020-01-22 18:08:48 +0100136 if (conn_create_mux(conn) < 0)
137 return;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200138
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100139 /* The wake callback is normally used to notify the data layer about
140 * data layer activity (successful send/recv), connection establishment,
141 * shutdown and fatal errors. We need to consider the following
142 * situations to wake up the data layer :
Willy Tarreau0fbc3182019-12-27 14:57:45 +0100143 * - change among the CO_FL_NOTIFY_DONE flags :
144 * SOCK_{RD,WR}_SH, ERROR,
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100145 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
146 * end of handshake and transition to CONNECTED
147 * - raise of CONNECTED with HANDSHAKE down
148 * - end of HANDSHAKE with CONNECTED set
149 * - regular data layer activity
150 *
151 * Note that the wake callback is allowed to release the connection and
152 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200153 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100154 if ((io_available || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) ||
155 ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200156 conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200157 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200158
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200159 /* commit polling changes */
160 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200161 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200162}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200163
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100164/* This is the callback which is set when a connection establishment is pending
165 * and we have nothing to send. It may update the FD polling status to indicate
166 * !READY. It returns 0 if it fails in a fatal way or needs to poll to go
167 * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN
168 * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and
169 * leaves the error code in errno.
170 */
171int conn_fd_check(struct connection *conn)
172{
173 struct sockaddr_storage *addr;
174 int fd = conn->handle.fd;
175
176 if (conn->flags & CO_FL_ERROR)
177 return 0;
178
179 if (!conn_ctrl_ready(conn))
180 return 0;
181
182 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
183 return 1; /* strange we were called while ready */
184
185 if (!fd_send_ready(fd))
186 return 0;
187
188 /* Here we have 2 cases :
189 * - modern pollers, able to report ERR/HUP. If these ones return any
190 * of these flags then it's likely a failure, otherwise it possibly
191 * is a success (i.e. there may have been data received just before
192 * the error was reported).
193 * - select, which doesn't report these and with which it's always
194 * necessary either to try connect() again or to check for SO_ERROR.
195 * In order to simplify everything, we double-check using connect() as
196 * soon as we meet either of these delicate situations. Note that
197 * SO_ERROR would clear the error after reporting it!
198 */
199 if (cur_poller.flags & HAP_POLL_F_ERRHUP) {
200 /* modern poller, able to report ERR/HUP */
201 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN)
202 goto done;
203 if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT)
204 goto done;
205 if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP)))
206 goto wait;
207 /* error present, fall through common error check path */
208 }
209
210 /* Use connect() to check the state of the socket. This has the double
211 * advantage of *not* clearing the error (so that health checks can
212 * still use getsockopt(SO_ERROR)) and giving us the following info :
213 * - error
214 * - connecting (EALREADY, EINPROGRESS)
215 * - connected (EISCONN, 0)
216 */
217 addr = conn->dst;
218 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
219 addr = &objt_server(conn->target)->socks4_addr;
220
221 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
222 if (errno == EALREADY || errno == EINPROGRESS)
223 goto wait;
224
225 if (errno && errno != EISCONN)
226 goto out_error;
227 }
228
229 done:
230 /* The FD is ready now, we'll mark the connection as complete and
231 * forward the event to the transport layer which will notify the
232 * data layer.
233 */
234 conn->flags &= ~CO_FL_WAIT_L4_CONN;
235 fd_may_send(fd);
236 fd_cond_recv(fd);
237 errno = 0; // make health checks happy
238 return 1;
239
240 out_error:
241 /* Write error on the file descriptor. Report it to the connection
242 * and disable polling on this FD.
243 */
244 fdtab[fd].linger_risk = 0;
245 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau5d4d1802020-02-21 09:58:29 +0100246 conn_stop_polling(conn);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100247 return 0;
248
249 wait:
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100250 fd_cant_send(fd);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100251 fd_want_send(fd);
Willy Tarreau4970e5a2019-12-27 10:40:21 +0100252 return 0;
253}
254
Willy Tarreauff3e6482015-03-12 23:56:52 +0100255/* Send a message over an established connection. It makes use of send() and
256 * returns the same return code and errno. If the socket layer is not ready yet
257 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
258 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
259 * EMSGSIZE if called with a zero length message. The purpose is to simplify
260 * some rare attempts to directly write on the socket from above the connection
261 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
262 * It automatically retries on EINTR. Other errors cause the connection to be
263 * marked as in error state. It takes similar arguments as send() except the
264 * first one which is the connection instead of the file descriptor. Note,
265 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
266 */
267int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
268{
269 int ret;
270
271 ret = -1;
272 errno = ENOTSOCK;
273
274 if (conn->flags & CO_FL_SOCK_WR_SH)
275 goto fail;
276
277 if (!conn_ctrl_ready(conn))
278 goto fail;
279
280 errno = EMSGSIZE;
281 if (!len)
282 goto fail;
283
Willy Tarreau585744b2017-08-24 14:31:19 +0200284 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100285 goto wait;
286
287 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200288 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100289 } while (ret < 0 && errno == EINTR);
290
291
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200292 if (ret > 0) {
293 if (conn->flags & CO_FL_WAIT_L4_CONN) {
294 conn->flags &= ~CO_FL_WAIT_L4_CONN;
295 fd_may_send(conn->handle.fd);
296 fd_cond_recv(conn->handle.fd);
297 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100298 return ret;
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200299 }
Willy Tarreauff3e6482015-03-12 23:56:52 +0100300
301 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
302 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200303 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100304 return 0;
305 }
306 fail:
307 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
308 return ret;
309}
310
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100311/* Called from the upper layer, to subscribe <es> to events <event_type>. The
312 * event subscriber <es> is not allowed to change from a previous call as long
313 * as at least one event is still subscribed. The <event_type> must only be a
314 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
315 */
316int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200317{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100318 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100319 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100320
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100321 es->events &= ~event_type;
322 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100323 conn->subs = NULL;
324
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100325 if (conn_ctrl_ready(conn)) {
326 if (event_type & SUB_RETRY_RECV)
327 fd_stop_recv(conn->handle.fd);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100328
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100329 if (event_type & SUB_RETRY_SEND)
330 fd_stop_send(conn->handle.fd);
331 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200332 return 0;
333}
334
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100335/* Called from the upper layer, to subscribe <es> to events <event_type>.
336 * The <es> struct is not allowed to differ from the one passed during a
337 * previous call to subscribe(). If the FD is ready, the wait_event is
338 * immediately woken up and the subcription is cancelled. It always
339 * returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100340 */
341int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200342{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100343 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100344 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100345
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100346 if (conn->subs && (conn->subs->events & event_type) == event_type)
347 return 0;
348
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100349 conn->subs = es;
350 es->events |= event_type;
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100351
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100352 if (conn_ctrl_ready(conn)) {
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100353 if (event_type & SUB_RETRY_RECV) {
354 if (fd_recv_ready(conn->handle.fd)) {
355 tasklet_wakeup(es->tasklet);
356 es->events &= ~SUB_RETRY_RECV;
357 if (!es->events)
358 conn->subs = NULL;
359 }
360 else
361 fd_want_recv(conn->handle.fd);
362 }
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100363
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100364 if (event_type & SUB_RETRY_SEND) {
365 if (fd_send_ready(conn->handle.fd)) {
366 tasklet_wakeup(es->tasklet);
367 es->events &= ~SUB_RETRY_SEND;
368 if (!es->events)
369 conn->subs = NULL;
370 }
371 else
372 fd_want_send(conn->handle.fd);
373 }
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100374 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200375 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200376}
377
Willy Tarreaud85c4852015-03-13 00:40:28 +0100378/* Drains possibly pending incoming data on the file descriptor attached to the
379 * connection and update the connection's flags accordingly. This is used to
380 * know whether we need to disable lingering on close. Returns non-zero if it
381 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
382 * flag may also be updated if the incoming shutdown was reported by the drain()
383 * function.
384 */
385int conn_sock_drain(struct connection *conn)
386{
Willy Tarreaue215bba2018-08-24 14:31:53 +0200387 int turns = 2;
388 int len;
389
Willy Tarreaud85c4852015-03-13 00:40:28 +0100390 if (!conn_ctrl_ready(conn))
391 return 1;
392
393 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
394 return 1;
395
Willy Tarreaue215bba2018-08-24 14:31:53 +0200396 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP))
397 goto shut;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100398
Willy Tarreaue215bba2018-08-24 14:31:53 +0200399 if (!fd_recv_ready(conn->handle.fd))
400 return 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100401
Willy Tarreaue215bba2018-08-24 14:31:53 +0200402 if (conn->ctrl->drain) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200403 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100404 return 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200405 goto shut;
406 }
407
408 /* no drain function defined, use the generic one */
409
410 while (turns) {
411#ifdef MSG_TRUNC_CLEARS_INPUT
412 len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
413 if (len == -1 && errno == EFAULT)
414#endif
415 len = recv(conn->handle.fd, trash.area, trash.size,
416 MSG_DONTWAIT | MSG_NOSIGNAL);
417
418 if (len == 0)
419 goto shut;
420
421 if (len < 0) {
422 if (errno == EAGAIN) {
423 /* connection not closed yet */
424 fd_cant_recv(conn->handle.fd);
425 break;
426 }
427 if (errno == EINTR) /* oops, try again */
428 continue;
429 /* other errors indicate a dead connection, fine. */
430 goto shut;
431 }
432 /* OK we read some data, let's try again once */
433 turns--;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100434 }
435
Willy Tarreaue215bba2018-08-24 14:31:53 +0200436 /* some data are still present, give up */
437 return 0;
438
439 shut:
440 /* we're certain the connection was shut down */
441 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100442 conn->flags |= CO_FL_SOCK_RD_SH;
443 return 1;
444}
445
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100446/*
447 * Get data length from tlv
448 */
449static int get_tlv_length(const struct tlv *src)
450{
451 return (src->length_hi << 8) | src->length_lo;
452}
453
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200454/* This handshake handler waits a PROXY protocol header at the beginning of the
455 * raw data stream. The header looks like this :
456 *
457 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
458 *
459 * There must be exactly one space between each field. Fields are :
460 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
461 * - SRC3 : layer 3 (eg: IP) source address in standard text form
462 * - DST3 : layer 3 (eg: IP) destination address in standard text form
463 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
464 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
465 *
466 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
467 *
468 * The header line is small and in all cases smaller than the smallest normal
469 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
470 * can safely use MSG_PEEK and avoid buffering.
471 *
472 * Once the data is fetched, the values are set in the connection's address
473 * fields, and data are removed from the socket's buffer. The function returns
474 * zero if it needs to wait for more data or if it fails, or 1 if it completed
475 * and removed itself.
476 */
477int conn_recv_proxy(struct connection *conn, int flag)
478{
479 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200480 struct proxy_hdr_v2 *hdr_v2;
481 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100482 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200483 int tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200484 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200485
Willy Tarreau3c728722014-01-23 13:50:42 +0100486 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200487 goto fail;
488
Willy Tarreauca79f592019-07-17 19:04:47 +0200489 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
490 goto fail;
491
Willy Tarreau585744b2017-08-24 14:31:19 +0200492 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200493 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100494
Willy Tarreau157788c2020-02-11 10:08:05 +0100495 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200496 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
497 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200498 if (errno == EINTR)
499 continue;
500 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200501 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200502 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100504 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200505 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200506 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100507 break;
508 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200509
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100511 /* client shutdown */
512 conn->err_code = CO_ER_PRX_EMPTY;
513 goto fail;
514 }
515
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100516 conn->flags &= ~CO_FL_WAIT_L4_CONN;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200519 goto missing;
520
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 line = trash.area;
522 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200523
524 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200525 if (strncmp(line, "PROXY ", 6) != 0)
526 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200527
528 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200530 goto missing;
531
David CARLIER42ff05e2016-03-24 09:22:36 +0000532 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200533 u32 src3, dst3, sport, dport;
534
535 line += 5;
536
537 src3 = inetaddr_host_lim_ret(line, end, &line);
538 if (line == end)
539 goto missing;
540 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100541 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200542
543 dst3 = inetaddr_host_lim_ret(line, end, &line);
544 if (line == end)
545 goto missing;
546 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100547 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200548
549 sport = read_uint((const char **)&line, end);
550 if (line == end)
551 goto missing;
552 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100553 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200554
555 dport = read_uint((const char **)&line, end);
556 if (line > end - 2)
557 goto missing;
558 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100559 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200560 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100561 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200562
563 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200564 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
565 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
566 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200567
Willy Tarreau226572f2019-07-17 14:46:00 +0200568 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
569 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
570 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200571 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
572 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000573 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200574 u32 sport, dport;
575 char *src_s;
576 char *dst_s, *sport_s, *dport_s;
577 struct in6_addr src3, dst3;
578
579 line += 5;
580
581 src_s = line;
582 dst_s = sport_s = dport_s = NULL;
583 while (1) {
584 if (line > end - 2) {
585 goto missing;
586 }
587 else if (*line == '\r') {
588 *line = 0;
589 line++;
590 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100591 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200592 break;
593 }
594
595 if (*line == ' ') {
596 *line = 0;
597 if (!dst_s)
598 dst_s = line + 1;
599 else if (!sport_s)
600 sport_s = line + 1;
601 else if (!dport_s)
602 dport_s = line + 1;
603 }
604 line++;
605 }
606
607 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100608 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200609
610 sport = read_uint((const char **)&sport_s,dport_s - 1);
611 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100612 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200613
614 dport = read_uint((const char **)&dport_s,line - 2);
615 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100616 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200617
618 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100619 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200620
621 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100622 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623
624 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200625 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
626 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
627 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200628
Willy Tarreau226572f2019-07-17 14:46:00 +0200629 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
630 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
631 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200632 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
633 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200634 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
635 /* This can be a UNIX socket forwarded by an haproxy upstream */
636 line += 9;
637 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200638 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200639 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100640 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200641 goto fail;
642 }
643
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200644 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200645 goto eat_header;
646
647 not_v1:
648 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200649 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200650 goto missing;
651
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200653
654 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
655 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
656 conn->err_code = CO_ER_PRX_NOT_HDR;
657 goto fail;
658 }
659
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200660 if (trash.data < PP2_HEADER_LEN + ntohs(hdr_v2->len))
Willy Tarreau77992672014-06-14 11:06:17 +0200661 goto missing;
662
663 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
664 case 0x01: /* PROXY command */
665 switch (hdr_v2->fam) {
666 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100667 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
668 goto bad_header;
669
Willy Tarreau226572f2019-07-17 14:46:00 +0200670 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
671 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
672 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
673 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
674 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
675 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200676 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200677 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100678 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200679 break;
680 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100681 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
682 goto bad_header;
683
Willy Tarreau226572f2019-07-17 14:46:00 +0200684 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
685 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
686 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
687 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
688 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
689 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200690 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200691 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100692 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200693 break;
694 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100695
696 /* TLV parsing */
697 if (tlv_length > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 while (tlv_offset + TLV_HEADER_SIZE <= trash.data) {
699 const struct tlv *tlv_packet = (struct tlv *) &trash.area[tlv_offset];
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100700 const int tlv_len = get_tlv_length(tlv_packet);
701 tlv_offset += tlv_len + TLV_HEADER_SIZE;
702
703 switch (tlv_packet->type) {
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100704 case PP2_TYPE_CRC32C: {
705 void *tlv_crc32c_p = (void *)tlv_packet->value;
706 uint32_t n_crc32c = ntohl(read_u32(tlv_crc32c_p));
707 write_u32(tlv_crc32c_p, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 if (hash_crc32c(trash.area, PP2_HEADER_LEN + ntohs(hdr_v2->len)) != n_crc32c)
Emmanuel Hocdet115df3e2018-02-05 16:23:23 +0100709 goto bad_header;
710 break;
711 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200712#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100713 case PP2_TYPE_NETNS: {
714 const struct netns_entry *ns;
715 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
716 if (ns)
717 conn->proxy_netns = ns;
718 break;
719 }
720#endif
Geoff Simmons7185b782019-08-27 18:31:16 +0200721 case PP2_TYPE_AUTHORITY: {
722 if (tlv_len > PP2_AUTHORITY_MAX)
723 goto bad_header;
724 conn->proxy_authority = pool_alloc(pool_head_authority);
725 if (conn->proxy_authority == NULL)
726 goto fail;
727 memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len);
728 conn->proxy_authority_len = tlv_len;
729 break;
730 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100731 default:
732 break;
733 }
734 }
735 }
736
Willy Tarreau77992672014-06-14 11:06:17 +0200737 /* unsupported protocol, keep local connection address */
738 break;
739 case 0x00: /* LOCAL command */
740 /* keep local connection address for LOCAL */
741 break;
742 default:
743 goto bad_header; /* not a supported command */
744 }
745
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200746 trash.data = PP2_HEADER_LEN + ntohs(hdr_v2->len);
Willy Tarreau77992672014-06-14 11:06:17 +0200747 goto eat_header;
748
749 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200750 /* remove the PROXY line from the request. For this we re-read the
751 * exact line at once. If we don't get the exact same result, we
752 * fail.
753 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100754 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200756 if (len2 < 0 && errno == EINTR)
757 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100759 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100760 break;
761 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200762
763 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100764 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200765 return 1;
766
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200767 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200768 return 0;
769
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200770 missing:
771 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
772 * we have not read anything. Otherwise we need to fail because we won't
773 * be able to poll anymore.
774 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100775 conn->err_code = CO_ER_PRX_TRUNCATED;
776 goto fail;
777
778 bad_header:
779 /* This is not a valid proxy protocol header */
780 conn->err_code = CO_ER_PRX_BAD_HDR;
781 goto fail;
782
783 recv_abort:
784 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100785 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100786 goto fail;
787
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200788 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200789 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200790 return 0;
791}
792
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100793/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000794 * at the beginning of the raw data stream. The header format is
795 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100796 *
797 * This line MUST be at the beginning of the buffer and MUST NOT be
798 * fragmented.
799 *
800 * The header line is small and in all cases smaller than the smallest normal
801 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
802 * can safely use MSG_PEEK and avoid buffering.
803 *
804 * Once the data is fetched, the values are set in the connection's address
805 * fields, and data are removed from the socket's buffer. The function returns
806 * zero if it needs to wait for more data or if it fails, or 1 if it completed
807 * and removed itself.
808 */
809int conn_recv_netscaler_cip(struct connection *conn, int flag)
810{
811 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000812 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100813 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200814 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100815
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100816 if (!conn_ctrl_ready(conn))
817 goto fail;
818
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100819 if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
820 goto fail;
821
Willy Tarreau585744b2017-08-24 14:31:19 +0200822 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200823 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100824
Willy Tarreau157788c2020-02-11 10:08:05 +0100825 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200826 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
827 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100828 if (errno == EINTR)
829 continue;
830 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200831 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200832 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100833 }
834 goto recv_abort;
835 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200836 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100837 break;
838 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100839
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100840 conn->flags &= ~CO_FL_WAIT_L4_CONN;
841
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200842 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100843 /* client shutdown */
844 conn->err_code = CO_ER_CIP_EMPTY;
845 goto fail;
846 }
847
848 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000849 * CIP magic, header length or
850 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200851 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100852 goto missing;
853
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200854 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100855
856 /* Decode a possible NetScaler Client IP request, fail early if
857 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100858 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100859 goto bad_magic;
860
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000861 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200862 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100863 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000864 line += 8;
865 }
866 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200867 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100868 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000869 line += 12;
870 }
871 /* Unknown CIP protocol */
872 else {
873 conn->err_code = CO_ER_CIP_BAD_PROTO;
874 goto fail;
875 }
876
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100877 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000878 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200879 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100880 goto missing;
881
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100882 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100883 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100884
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100885 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100886 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100887 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100888
889 hdr_ip4 = (struct ip *)line;
890
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100892 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000893 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100894 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000895 }
896 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100897 /* The protocol does not include a TCP header */
898 conn->err_code = CO_ER_CIP_BAD_PROTO;
899 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000900 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100901
David Carlier3015a2e2016-07-04 22:51:33 +0100902 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100903
904 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200905 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
906 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
907 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100908
Willy Tarreau226572f2019-07-17 14:46:00 +0200909 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
910 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
911 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100912
913 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
914 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100915 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100916 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100917 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100918
919 hdr_ip6 = (struct ip6_hdr *)line;
920
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200921 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100922 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000923 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100924 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000925 }
926 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100927 /* The protocol does not include a TCP header */
928 conn->err_code = CO_ER_CIP_BAD_PROTO;
929 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000930 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100931
David Carlier3015a2e2016-07-04 22:51:33 +0100932 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100933
934 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200935 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
936 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
937 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100938
Willy Tarreau226572f2019-07-17 14:46:00 +0200939 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
940 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
941 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100942
943 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
944 }
945 else {
946 /* The protocol does not match something known (IPv4/IPv6) */
947 conn->err_code = CO_ER_CIP_BAD_PROTO;
948 goto fail;
949 }
950
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000951 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200952 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100953
954 /* remove the NetScaler Client IP header from the request. For this
955 * we re-read the exact line at once. If we don't get the exact same
956 * result, we fail.
957 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100958 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200959 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100960 if (len2 < 0 && errno == EINTR)
961 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200962 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100963 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100964 break;
965 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100966
967 conn->flags &= ~flag;
968 return 1;
969
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200970 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200971 return 0;
972
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100973 missing:
974 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
975 * we have not read anything. Otherwise we need to fail because we won't
976 * be able to poll anymore.
977 */
978 conn->err_code = CO_ER_CIP_TRUNCATED;
979 goto fail;
980
981 bad_magic:
982 conn->err_code = CO_ER_CIP_BAD_MAGIC;
983 goto fail;
984
985 recv_abort:
986 conn->err_code = CO_ER_CIP_ABORT;
987 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
988 goto fail;
989
990 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100991 conn->flags |= CO_FL_ERROR;
992 return 0;
993}
994
Alexander Liu2a54bb72019-05-22 19:44:48 +0800995
996int conn_send_socks4_proxy_request(struct connection *conn)
997{
998 struct socks4_request req_line;
999
Alexander Liu2a54bb72019-05-22 19:44:48 +08001000 if (!conn_ctrl_ready(conn))
1001 goto out_error;
1002
Willy Tarreau226572f2019-07-17 14:46:00 +02001003 if (!conn_get_dst(conn))
1004 goto out_error;
1005
Alexander Liu2a54bb72019-05-22 19:44:48 +08001006 req_line.version = 0x04;
1007 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001008 req_line.port = get_net_port(conn->dst);
1009 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001010 memcpy(req_line.user_id, "HAProxy\0", 8);
1011
1012 if (conn->send_proxy_ofs > 0) {
1013 /*
1014 * This is the first call to send the request
1015 */
1016 conn->send_proxy_ofs = -(int)sizeof(req_line);
1017 }
1018
1019 if (conn->send_proxy_ofs < 0) {
1020 int ret = 0;
1021
1022 /* we are sending the socks4_req_line here. If the data layer
1023 * has a pending write, we'll also set MSG_MORE.
1024 */
1025 ret = conn_sock_send(
1026 conn,
1027 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1028 -conn->send_proxy_ofs,
Willy Tarreau19bc2012020-02-21 08:46:19 +01001029 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001030
1031 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1032 conn->handle.fd, -conn->send_proxy_ofs, ret);
1033
1034 if (ret < 0) {
1035 goto out_error;
1036 }
1037
1038 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1039 if (conn->send_proxy_ofs != 0) {
1040 goto out_wait;
1041 }
1042 }
1043
1044 /* OK we've the whole request sent */
1045 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001046
1047 /* The connection is ready now, simply return and let the connection
1048 * handler notify upper layers if needed.
1049 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001050 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001051
1052 if (conn->flags & CO_FL_SEND_PROXY) {
1053 /*
1054 * Get the send_proxy_ofs ready for the send_proxy due to we are
1055 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1056 * before sending PROXY Protocol.
1057 */
1058 conn->send_proxy_ofs = 1;
1059 }
1060 return 1;
1061
1062 out_error:
1063 /* Write error on the file descriptor */
1064 conn->flags |= CO_FL_ERROR;
1065 if (conn->err_code == CO_ER_NONE) {
1066 conn->err_code = CO_ER_SOCKS4_SEND;
1067 }
1068 return 0;
1069
1070 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001071 return 0;
1072}
1073
1074int conn_recv_socks4_proxy_response(struct connection *conn)
1075{
1076 char line[SOCKS4_HS_RSP_LEN];
1077 int ret;
1078
Alexander Liu2a54bb72019-05-22 19:44:48 +08001079 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
Willy Tarreau157788c2020-02-11 10:08:05 +01001085 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001086 /* 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 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001122 break;
1123 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001124
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001125 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1126
Alexander Liu2a54bb72019-05-22 19:44:48 +08001127 if (ret < SOCKS4_HS_RSP_LEN) {
1128 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1129 * we are not able to read enough data.
1130 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001131 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001132 }
1133
1134 /*
1135 * Base on the SOCSK4 protocol:
1136 *
1137 * +----+----+----+----+----+----+----+----+
1138 * | VN | CD | DSTPORT | DSTIP |
1139 * +----+----+----+----+----+----+----+----+
1140 * # of bytes: 1 1 2 4
1141 * VN is the version of the reply code and should be 0. CD is the result
1142 * code with one of the following values:
1143 * 90: request granted
1144 * 91: request rejected or failed
1145 * 92: request rejected becasue SOCKS server cannot connect to identd on the client
1146 * 93: request rejected because the client program and identd report different user-ids
1147 * The remaining fields are ignored.
1148 */
1149 if (line[1] != 90) {
1150 conn->flags &= ~CO_FL_SOCKS4_RECV;
1151
1152 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1153 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1154 if (conn->err_code == CO_ER_NONE) {
1155 conn->err_code = CO_ER_SOCKS4_DENY;
1156 }
1157 goto fail;
1158 }
1159
1160 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001161 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001162 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1163 if (ret < 0 && errno == EINTR) {
1164 continue;
1165 }
1166 if (ret != SOCKS4_HS_RSP_LEN) {
1167 if (conn->err_code == CO_ER_NONE) {
1168 conn->err_code = CO_ER_SOCKS4_RECV;
1169 }
1170 goto fail;
1171 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001172 break;
1173 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001174
1175 conn->flags &= ~CO_FL_SOCKS4_RECV;
1176 return 1;
1177
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001178 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001179 return 0;
1180
Alexander Liu2a54bb72019-05-22 19:44:48 +08001181 recv_abort:
1182 if (conn->err_code == CO_ER_NONE) {
1183 conn->err_code = CO_ER_SOCKS4_ABORT;
1184 }
1185 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1186 goto fail;
1187
1188 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001189 conn->flags |= CO_FL_ERROR;
1190 return 0;
1191}
1192
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001193/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001194int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
1195{
1196 int ret = 0;
1197
1198 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1199 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
1200 }
1201 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001202 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1203 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001204 else
1205 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1206 }
1207
1208 return ret;
1209}
1210
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001211/* Makes a PROXY protocol line from the two addresses. The output is sent to
1212 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1213 * It returns the number of bytes composing this line (including the trailing
1214 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001215 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1216 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001217 */
David Safb76832014-05-08 23:42:08 -04001218int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001219{
1220 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001221 char * protocol;
1222 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1223 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1224 in_port_t src_port;
1225 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001226
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001227 if ( !src
1228 || !dst
1229 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1230 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1231 /* unknown family combination */
1232 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001233 if (ret >= buf_len)
1234 return 0;
1235
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001236 return ret;
1237 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001238
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001239 /* IPv4 for both src and dst */
1240 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1241 protocol = "TCP4";
1242 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001243 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001244 src_port = ((struct sockaddr_in *)src)->sin_port;
1245 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001246 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001247 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001248 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001249 /* IPv6 for at least one of src and dst */
1250 else {
1251 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001252
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001253 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001254
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001255 if (src->ss_family == AF_INET) {
1256 /* Convert src to IPv6 */
1257 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1258 src_port = ((struct sockaddr_in *)src)->sin_port;
1259 }
1260 else {
1261 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1262 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1263 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001264
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001265 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001266 return 0;
1267
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001268 if (dst->ss_family == AF_INET) {
1269 /* Convert dst to IPv6 */
1270 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1271 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1272 }
1273 else {
1274 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1275 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1276 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001277
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001278 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001279 return 0;
1280 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001281
1282 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1283 if (ret >= buf_len)
1284 return 0;
1285
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001286 return ret;
1287}
David Safb76832014-05-08 23:42:08 -04001288
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001289static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001290{
1291 struct tlv *tlv;
1292
1293 if (!dest || (length + sizeof(*tlv) > dest_len))
1294 return 0;
1295
1296 tlv = (struct tlv *)dest;
1297
1298 tlv->type = type;
1299 tlv->length_hi = length >> 8;
1300 tlv->length_lo = length & 0x00ff;
1301 memcpy(tlv->value, value, length);
1302 return length + sizeof(*tlv);
1303}
David Safb76832014-05-08 23:42:08 -04001304
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001305/* Note: <remote> is explicitly allowed to be NULL */
David Safb76832014-05-08 23:42:08 -04001306int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
1307{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001308 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001309 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001310 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001311 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001312 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001313 struct sockaddr_storage *src = &null_addr;
1314 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001315 const char *value;
1316 int value_len;
David Safb76832014-05-08 23:42:08 -04001317
1318 if (buf_len < PP2_HEADER_LEN)
1319 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001320 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001321
Willy Tarreau226572f2019-07-17 14:46:00 +02001322 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1323 src = remote->src;
1324 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001325 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001326
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001327 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1328 if ( !src
1329 || !dst
1330 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1331 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001332 if (buf_len < PP2_HDR_LEN_UNSPEC)
1333 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001334 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1335 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001336 ret = PP2_HDR_LEN_UNSPEC;
1337 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001338 else {
Willy Tarreau7f263912020-02-19 15:10:00 +01001339 /* Note: due to historic compatibility with V1 which required
1340 * to send "PROXY" with local addresses for local connections,
1341 * we can end up here with the remote in fact being our outgoing
1342 * connection. We still want to send real addresses and LOCAL on
1343 * it.
1344 */
1345 hdr->ver_cmd = PP2_VERSION;
1346 hdr->ver_cmd |= conn_is_back(remote) ? PP2_CMD_LOCAL : PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001347 /* IPv4 for both src and dst */
1348 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1349 if (buf_len < PP2_HDR_LEN_INET)
1350 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001351 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1352 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1353 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1354 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1355 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1356 ret = PP2_HDR_LEN_INET;
1357 }
1358 /* IPv6 for at least one of src and dst */
1359 else {
1360 struct in6_addr tmp;
1361
1362 if (buf_len < PP2_HDR_LEN_INET6)
1363 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001364 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1365 if (src->ss_family == AF_INET) {
1366 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1367 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1368 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1369 }
1370 else {
1371 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1372 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1373 }
1374 if (dst->ss_family == AF_INET) {
1375 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1376 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001377 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001378 }
1379 else {
1380 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1381 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1382 }
1383
1384 ret = PP2_HDR_LEN_INET6;
1385 }
1386 }
David Safb76832014-05-08 23:42:08 -04001387
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001388 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1389 uint32_t zero_crc32c = 0;
1390 if ((buf_len - ret) < sizeof(struct tlv))
1391 return 0;
1392 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1393 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1394 }
1395
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001396 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001397 if ((buf_len - ret) < sizeof(struct tlv))
1398 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001399 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001400 }
1401
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001402 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001403 value = NULL;
1404 if (remote && remote->proxy_authority) {
1405 value = remote->proxy_authority;
1406 value_len = remote->proxy_authority_len;
1407 }
1408#ifdef USE_OPENSSL
1409 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001410 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001411 value_len = strlen(value);
1412 }
1413#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001414 if (value) {
1415 if ((buf_len - ret) < sizeof(struct tlv))
1416 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001417 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001418 }
1419 }
1420
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001421#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001422 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001423 struct tlv_ssl *tlv;
1424 int ssl_tlv_len = 0;
David Safb76832014-05-08 23:42:08 -04001425 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1426 return 0;
1427 tlv = (struct tlv_ssl *)&buf[ret];
1428 memset(tlv, 0, sizeof(struct tlv_ssl));
1429 ssl_tlv_len += sizeof(struct tlv_ssl);
1430 tlv->tlv.type = PP2_TYPE_SSL;
1431 if (ssl_sock_is_ssl(remote)) {
1432 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001433 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001434 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001435 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001436 }
Dave McCowan328fb582014-07-30 10:39:13 -04001437 if (ssl_sock_get_cert_used_sess(remote)) {
1438 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001439 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001440 if (ssl_sock_get_cert_used_conn(remote))
1441 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001442 }
1443 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001444 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001445 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001446 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1447 cn_trash->data,
1448 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001449 }
1450 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001451 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001452 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001453 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001454 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1455 pkey_trash->data,
1456 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001457 }
1458 }
1459 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1460 value = ssl_sock_get_cert_sig(remote);
1461 if (value) {
1462 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1463 }
1464 }
1465 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1466 value = ssl_sock_get_cipher_name(remote);
1467 if (value) {
1468 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1469 }
1470 }
David Safb76832014-05-08 23:42:08 -04001471 }
1472 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1473 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1474 ret += ssl_tlv_len;
1475 }
1476#endif
1477
Willy Tarreaue5733232019-05-22 19:24:06 +02001478#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001479 if (remote && (remote->proxy_netns)) {
1480 if ((buf_len - ret) < sizeof(struct tlv))
1481 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001482 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001483 }
1484#endif
1485
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001486 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001487
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001488 if (tlv_crc32c_p) {
1489 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1490 }
1491
David Safb76832014-05-08 23:42:08 -04001492 return ret;
1493}
Emeric Brun4f603012017-01-05 15:11:44 +01001494
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001495/* return the major HTTP version as 1 or 2 depending on how the request arrived
1496 * before being processed.
1497 */
1498static int
1499smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1500{
Jérôme Magnin86577422018-12-07 09:03:11 +01001501 struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1502 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001503
1504 smp->data.type = SMP_T_SINT;
1505 smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
1506 return 1;
1507}
1508
Emeric Brun4f603012017-01-05 15:11:44 +01001509/* fetch if the received connection used a PROXY protocol header */
1510int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1511{
1512 struct connection *conn;
1513
1514 conn = objt_conn(smp->sess->origin);
1515 if (!conn)
1516 return 0;
1517
Willy Tarreau911db9b2020-01-23 16:27:54 +01001518 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001519 smp->flags |= SMP_F_MAY_CHANGE;
1520 return 0;
1521 }
1522
1523 smp->flags = 0;
1524 smp->data.type = SMP_T_BOOL;
1525 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1526
1527 return 1;
1528}
1529
Geoff Simmons7185b782019-08-27 18:31:16 +02001530/* fetch the authority TLV from a PROXY protocol header */
1531int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1532{
1533 struct connection *conn;
1534
1535 conn = objt_conn(smp->sess->origin);
1536 if (!conn)
1537 return 0;
1538
Willy Tarreau911db9b2020-01-23 16:27:54 +01001539 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001540 smp->flags |= SMP_F_MAY_CHANGE;
1541 return 0;
1542 }
1543
1544 if (conn->proxy_authority == NULL)
1545 return 0;
1546
1547 smp->flags = 0;
1548 smp->data.type = SMP_T_STR;
1549 smp->data.u.str.area = conn->proxy_authority;
1550 smp->data.u.str.data = conn->proxy_authority_len;
1551
1552 return 1;
1553}
1554
Emeric Brun4f603012017-01-05 15:11:44 +01001555/* Note: must not be declared <const> as its list will be overwritten.
1556 * Note: fetches that may return multiple types must be declared as the lowest
1557 * common denominator, the type that can be casted into all other ones. For
1558 * instance v4/v6 must be declared v4.
1559 */
1560static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001561 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Jérôme Magnin86577422018-12-07 09:03:11 +01001562 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001563 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001564 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001565 { /* END */ },
1566}};
1567
Willy Tarreau0108d902018-11-25 19:14:37 +01001568INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);