blob: bc34ae76b0453851f42b3178923ac2cbf5481407 [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010017#include <common/namespace.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020018
Willy Tarreauc5788912012-08-24 18:12:41 +020019#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020020#include <proto/fd.h>
Willy Tarreau5f1504f2012-10-04 23:55:57 +020021#include <proto/frontend.h>
Willy Tarreau2da156f2012-07-23 15:07:23 +020022#include <proto/proto_tcp.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020023#include <proto/stream_interface.h>
Emeric Brun4f603012017-01-05 15:11:44 +010024#include <proto/sample.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020025
Emeric Brun46591952012-05-18 15:47:34 +020026#ifdef USE_OPENSSL
27#include <proto/ssl_sock.h>
28#endif
29
Willy Tarreauf2943dc2012-10-26 20:10:28 +020030struct pool_head *pool2_connection;
Willy Tarreau13e14102016-12-22 20:25:26 +010031struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020032
33/* perform minimal intializations, report 0 in case of error, 1 if OK. */
34int init_connection()
35{
36 pool2_connection = create_pool("connection", sizeof (struct connection), MEM_F_SHARED);
37 return pool2_connection != NULL;
38}
39
Willy Tarreau59f98392012-07-06 14:13:49 +020040/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020041 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020042 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020043void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020044{
Willy Tarreau80184712012-07-06 14:54:49 +020045 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020046 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020047
Willy Tarreauc76ae332012-07-12 15:32:13 +020048 if (unlikely(!conn))
Willy Tarreau7a798e52016-04-14 11:13:20 +020049 return;
Willy Tarreau59f98392012-07-06 14:13:49 +020050
Willy Tarreau7d281492012-12-16 19:19:13 +010051 conn_refresh_polling_flags(conn);
52 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010053
Willy Tarreauc76ae332012-07-12 15:32:13 +020054 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020055 /* The handshake callbacks are called in sequence. If either of them is
56 * missing something, it must enable the required polling at the socket
57 * layer of the connection. Polling state is not guaranteed when entering
58 * these handlers, so any handshake handler which does not complete its
Willy Tarreaud6e999b2013-11-25 08:41:15 +010059 * work must explicitly disable events it's not interested in. Error
60 * handling is also performed here in order to reduce the number of tests
61 * around.
Willy Tarreauf9dabec2012-08-17 17:33:53 +020062 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +010063 while (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) {
Willy Tarreau310987a2014-01-22 19:46:33 +010064 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020065 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020066
Bertrand Jacquin93b227d2016-06-04 15:11:10 +010067 if (conn->flags & CO_FL_ACCEPT_CIP)
68 if (!conn_recv_netscaler_cip(conn, CO_FL_ACCEPT_CIP))
69 goto leave;
70
Willy Tarreau22cda212012-08-31 17:43:29 +020071 if (conn->flags & CO_FL_ACCEPT_PROXY)
72 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
73 goto leave;
74
Willy Tarreau57cd3e42013-10-24 22:01:26 +020075 if (conn->flags & CO_FL_SEND_PROXY)
76 if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY))
Willy Tarreau5f1504f2012-10-04 23:55:57 +020077 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020078#ifdef USE_OPENSSL
79 if (conn->flags & CO_FL_SSL_WAIT_HS)
80 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
81 goto leave;
82#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020083 }
84
Willy Tarreauf9dabec2012-08-17 17:33:53 +020085 /* Once we're purely in the data phase, we disable handshake polling */
86 if (!(conn->flags & CO_FL_POLL_SOCK))
87 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020088
Willy Tarreau071e1372012-10-03 01:39:48 +020089 /* The data layer might not be ready yet (eg: when using embryonic
90 * sessions). If we're about to move data, we must initialize it first.
91 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020092 * we must not use it anymore and should immediately leave instead.
93 */
Willy Tarreau071e1372012-10-03 01:39:48 +020094 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +020095 return;
Willy Tarreau2542b532012-08-31 16:01:23 +020096
Willy Tarreau153c3ca2012-10-22 22:47:55 +020097 /* The data transfer starts here and stops on error and handshakes. Note
98 * that we must absolutely test conn->xprt at each step in case it suddenly
99 * changes due to a quick unexpected close().
100 */
Willy Tarreaud8375892014-01-21 11:01:08 +0100101 if (conn->xprt && fd_recv_ready(fd) &&
102 ((conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_RD_ENA)) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200103 /* force detection of a flag change : it's impossible to have both
104 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200105 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200106 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200107 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200108 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200109
Willy Tarreaud8375892014-01-21 11:01:08 +0100110 if (conn->xprt && fd_send_ready(fd) &&
111 ((conn->flags & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_WR_ENA)) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200112 /* force detection of a flag change : it's impossible to have both
113 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200114 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200115 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200116 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200117 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200118
Willy Tarreauc76ae332012-07-12 15:32:13 +0200119 /* It may happen during the data phase that a handshake is
120 * enabled again (eg: SSL)
121 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100122 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200123 goto process_handshake;
124
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100125 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200126 /* still waiting for a connection to establish and nothing was
127 * attempted yet to probe the connection. Then let's retry the
128 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200129 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200130 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200131 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200132 }
133
Willy Tarreau2c6be842012-07-06 17:12:34 +0200134 leave:
Willy Tarreau7bf3fa32017-03-14 20:19:29 +0100135 /* Verify if the connection just established. The CO_FL_CONNECTED flag
136 * being included in CO_FL_CONN_STATE, its change will be noticed by
137 * the next block and be used to wake up the data layer.
138 */
139 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
140 conn->flags |= CO_FL_CONNECTED;
141
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200142 /* The wake callback may be used to process a critical error and abort the
143 * connection. If so, we don't want to go further as the connection will
144 * have been released and the FD destroyed.
145 */
146 if ((conn->flags & CO_FL_WAKE_DATA) &&
147 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
148 conn->data->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200149 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200150
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200151 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100152 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200153
154 /* commit polling changes */
155 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200156 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200157}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200158
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200159/* Update polling on connection <c>'s file descriptor depending on its current
160 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
161 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
162 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200163 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200165void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200166{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200167 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168
Willy Tarreau3c728722014-01-23 13:50:42 +0100169 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200170 return;
171
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200172 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100173 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100174 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100175 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100177 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
178 fd_stop_recv(c->t.sock.fd);
179 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200180 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200181
182 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100183 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200184 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100185 f |= CO_FL_CURR_WR_ENA;
186 }
187 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
188 fd_stop_send(c->t.sock.fd);
189 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200190 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100191 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192}
193
194/* Update polling on connection <c>'s file descriptor depending on its current
195 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
196 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
197 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200198 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200199 */
200void conn_update_sock_polling(struct connection *c)
201{
202 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200203
Willy Tarreau3c728722014-01-23 13:50:42 +0100204 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200205 return;
206
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200207 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100208 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100209 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100210 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200211 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100212 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
213 fd_stop_recv(c->t.sock.fd);
214 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200215 }
216
217 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100218 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100219 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100220 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200221 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100222 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
223 fd_stop_send(c->t.sock.fd);
224 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200225 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100226 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200227}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200228
Willy Tarreauff3e6482015-03-12 23:56:52 +0100229/* Send a message over an established connection. It makes use of send() and
230 * returns the same return code and errno. If the socket layer is not ready yet
231 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
232 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
233 * EMSGSIZE if called with a zero length message. The purpose is to simplify
234 * some rare attempts to directly write on the socket from above the connection
235 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
236 * It automatically retries on EINTR. Other errors cause the connection to be
237 * marked as in error state. It takes similar arguments as send() except the
238 * first one which is the connection instead of the file descriptor. Note,
239 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
240 */
241int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
242{
243 int ret;
244
245 ret = -1;
246 errno = ENOTSOCK;
247
248 if (conn->flags & CO_FL_SOCK_WR_SH)
249 goto fail;
250
251 if (!conn_ctrl_ready(conn))
252 goto fail;
253
254 errno = EMSGSIZE;
255 if (!len)
256 goto fail;
257
258 if (!fd_send_ready(conn->t.sock.fd))
259 goto wait;
260
261 do {
262 ret = send(conn->t.sock.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
263 } while (ret < 0 && errno == EINTR);
264
265
266 if (ret > 0)
267 return ret;
268
269 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
270 wait:
271 fd_cant_send(conn->t.sock.fd);
272 return 0;
273 }
274 fail:
275 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
276 return ret;
277}
278
Willy Tarreaud85c4852015-03-13 00:40:28 +0100279/* Drains possibly pending incoming data on the file descriptor attached to the
280 * connection and update the connection's flags accordingly. This is used to
281 * know whether we need to disable lingering on close. Returns non-zero if it
282 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
283 * flag may also be updated if the incoming shutdown was reported by the drain()
284 * function.
285 */
286int conn_sock_drain(struct connection *conn)
287{
288 if (!conn_ctrl_ready(conn))
289 return 1;
290
291 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
292 return 1;
293
294 if (fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) {
295 fdtab[conn->t.sock.fd].linger_risk = 0;
296 }
297 else {
298 if (!fd_recv_ready(conn->t.sock.fd))
299 return 0;
300
301 /* disable draining if we were called and have no drain function */
302 if (!conn->ctrl->drain) {
303 __conn_data_stop_recv(conn);
304 return 0;
305 }
306
307 if (conn->ctrl->drain(conn->t.sock.fd) <= 0)
308 return 0;
309 }
310
311 conn->flags |= CO_FL_SOCK_RD_SH;
312 return 1;
313}
314
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100315/*
316 * Get data length from tlv
317 */
318static int get_tlv_length(const struct tlv *src)
319{
320 return (src->length_hi << 8) | src->length_lo;
321}
322
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200323/* This handshake handler waits a PROXY protocol header at the beginning of the
324 * raw data stream. The header looks like this :
325 *
326 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
327 *
328 * There must be exactly one space between each field. Fields are :
329 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
330 * - SRC3 : layer 3 (eg: IP) source address in standard text form
331 * - DST3 : layer 3 (eg: IP) destination address in standard text form
332 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
333 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
334 *
335 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
336 *
337 * The header line is small and in all cases smaller than the smallest normal
338 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
339 * can safely use MSG_PEEK and avoid buffering.
340 *
341 * Once the data is fetched, the values are set in the connection's address
342 * fields, and data are removed from the socket's buffer. The function returns
343 * zero if it needs to wait for more data or if it fails, or 1 if it completed
344 * and removed itself.
345 */
346int conn_recv_proxy(struct connection *conn, int flag)
347{
348 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200349 struct proxy_hdr_v2 *hdr_v2;
350 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100351 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200352 int tlv_offset = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200353
354 /* we might have been called just after an asynchronous shutr */
355 if (conn->flags & CO_FL_SOCK_RD_SH)
356 goto fail;
357
Willy Tarreau3c728722014-01-23 13:50:42 +0100358 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200359 goto fail;
360
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100361 if (!fd_recv_ready(conn->t.sock.fd))
362 return 0;
363
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200364 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100365 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
366 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200367 if (errno == EINTR)
368 continue;
369 if (errno == EAGAIN) {
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100370 fd_cant_recv(conn->t.sock.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200371 return 0;
372 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100373 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200374 }
375 } while (0);
376
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100377 if (!trash.len) {
378 /* client shutdown */
379 conn->err_code = CO_ER_PRX_EMPTY;
380 goto fail;
381 }
382
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100383 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200384 goto missing;
385
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100386 line = trash.str;
387 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200388
389 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200390 if (strncmp(line, "PROXY ", 6) != 0)
391 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200392
393 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200394 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200395 goto missing;
396
David CARLIER42ff05e2016-03-24 09:22:36 +0000397 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200398 u32 src3, dst3, sport, dport;
399
400 line += 5;
401
402 src3 = inetaddr_host_lim_ret(line, end, &line);
403 if (line == end)
404 goto missing;
405 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100406 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200407
408 dst3 = inetaddr_host_lim_ret(line, end, &line);
409 if (line == end)
410 goto missing;
411 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100412 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200413
414 sport = read_uint((const char **)&line, end);
415 if (line == end)
416 goto missing;
417 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100418 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200419
420 dport = read_uint((const char **)&line, end);
421 if (line > end - 2)
422 goto missing;
423 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100424 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200425 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100426 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200427
428 /* update the session's addresses and mark them set */
429 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
430 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
431 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
432
433 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
434 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
435 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
436 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
437 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000438 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200439 u32 sport, dport;
440 char *src_s;
441 char *dst_s, *sport_s, *dport_s;
442 struct in6_addr src3, dst3;
443
444 line += 5;
445
446 src_s = line;
447 dst_s = sport_s = dport_s = NULL;
448 while (1) {
449 if (line > end - 2) {
450 goto missing;
451 }
452 else if (*line == '\r') {
453 *line = 0;
454 line++;
455 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100456 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200457 break;
458 }
459
460 if (*line == ' ') {
461 *line = 0;
462 if (!dst_s)
463 dst_s = line + 1;
464 else if (!sport_s)
465 sport_s = line + 1;
466 else if (!dport_s)
467 dport_s = line + 1;
468 }
469 line++;
470 }
471
472 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100473 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200474
475 sport = read_uint((const char **)&sport_s,dport_s - 1);
476 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100477 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200478
479 dport = read_uint((const char **)&dport_s,line - 2);
480 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100481 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200482
483 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100484 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200485
486 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100487 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200488
489 /* update the session's addresses and mark them set */
490 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
491 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
492 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
493
494 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
495 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
496 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
497 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
498 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200499 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
500 /* This can be a UNIX socket forwarded by an haproxy upstream */
501 line += 9;
502 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200504 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100505 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200506 goto fail;
507 }
508
Willy Tarreau77992672014-06-14 11:06:17 +0200509 trash.len = line - trash.str;
510 goto eat_header;
511
512 not_v1:
513 /* try PPv2 */
514 if (trash.len < PP2_HEADER_LEN)
515 goto missing;
516
517 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
518
519 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
520 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
521 conn->err_code = CO_ER_PRX_NOT_HDR;
522 goto fail;
523 }
524
525 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
526 goto missing;
527
528 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
529 case 0x01: /* PROXY command */
530 switch (hdr_v2->fam) {
531 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100532 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
533 goto bad_header;
534
Willy Tarreau77992672014-06-14 11:06:17 +0200535 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
536 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
537 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
538 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
539 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
540 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port;
541 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200542 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100543 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200544 break;
545 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100546 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
547 goto bad_header;
548
Willy Tarreau77992672014-06-14 11:06:17 +0200549 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
550 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
551 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
552 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
553 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
554 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
555 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200556 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100557 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200558 break;
559 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100560
561 /* TLV parsing */
562 if (tlv_length > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100563 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
564 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
565 const int tlv_len = get_tlv_length(tlv_packet);
566 tlv_offset += tlv_len + TLV_HEADER_SIZE;
567
568 switch (tlv_packet->type) {
569#ifdef CONFIG_HAP_NS
570 case PP2_TYPE_NETNS: {
571 const struct netns_entry *ns;
572 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
573 if (ns)
574 conn->proxy_netns = ns;
575 break;
576 }
577#endif
578 default:
579 break;
580 }
581 }
582 }
583
Willy Tarreau77992672014-06-14 11:06:17 +0200584 /* unsupported protocol, keep local connection address */
585 break;
586 case 0x00: /* LOCAL command */
587 /* keep local connection address for LOCAL */
588 break;
589 default:
590 goto bad_header; /* not a supported command */
591 }
592
593 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
594 goto eat_header;
595
596 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200597 /* remove the PROXY line from the request. For this we re-read the
598 * exact line at once. If we don't get the exact same result, we
599 * fail.
600 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200601 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100602 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200603 if (len2 < 0 && errno == EINTR)
604 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100605 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100606 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200607 } while (0);
608
609 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100610 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200611 return 1;
612
613 missing:
614 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
615 * we have not read anything. Otherwise we need to fail because we won't
616 * be able to poll anymore.
617 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100618 conn->err_code = CO_ER_PRX_TRUNCATED;
619 goto fail;
620
621 bad_header:
622 /* This is not a valid proxy protocol header */
623 conn->err_code = CO_ER_PRX_BAD_HDR;
624 goto fail;
625
626 recv_abort:
627 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100628 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100629 goto fail;
630
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200631 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100632 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200634 return 0;
635}
636
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100637/* This handshake handler waits a NetScaler Client IP insertion header
638 * at the beginning of the raw data stream. The header looks like this:
639 *
640 * 4 bytes: CIP magic number
641 * 4 bytes: Header length
642 * 20+ bytes: Header of the last IP packet sent by the client during
643 * TCP handshake.
644 * 20+ bytes: Header of the last TCP packet sent by the client during
645 * TCP handshake.
646 *
647 * This line MUST be at the beginning of the buffer and MUST NOT be
648 * fragmented.
649 *
650 * The header line is small and in all cases smaller than the smallest normal
651 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
652 * can safely use MSG_PEEK and avoid buffering.
653 *
654 * Once the data is fetched, the values are set in the connection's address
655 * fields, and data are removed from the socket's buffer. The function returns
656 * zero if it needs to wait for more data or if it fails, or 1 if it completed
657 * and removed itself.
658 */
659int conn_recv_netscaler_cip(struct connection *conn, int flag)
660{
661 char *line;
662 uint32_t cip_magic;
663 uint32_t cip_len;
664 uint8_t ip_v;
665
666 /* we might have been called just after an asynchronous shutr */
667 if (conn->flags & CO_FL_SOCK_RD_SH)
668 goto fail;
669
670 if (!conn_ctrl_ready(conn))
671 goto fail;
672
673 if (!fd_recv_ready(conn->t.sock.fd))
674 return 0;
675
676 do {
677 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
678 if (trash.len < 0) {
679 if (errno == EINTR)
680 continue;
681 if (errno == EAGAIN) {
682 fd_cant_recv(conn->t.sock.fd);
683 return 0;
684 }
685 goto recv_abort;
686 }
687 } while (0);
688
689 if (!trash.len) {
690 /* client shutdown */
691 conn->err_code = CO_ER_CIP_EMPTY;
692 goto fail;
693 }
694
695 /* Fail if buffer length is not large enough to contain
696 * CIP magic, CIP length */
697 if (trash.len < 8)
698 goto missing;
699
700 line = trash.str;
701
702 cip_magic = ntohl(*(uint32_t *)line);
703 cip_len = ntohl(*(uint32_t *)(line+4));
704
705 /* Decode a possible NetScaler Client IP request, fail early if
706 * it does not match */
707 if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic)
708 goto bad_magic;
709
710 /* Fail if buffer length is not large enough to contain
711 * CIP magic, CIP length, minimal IP header */
712 if (trash.len < 28)
713 goto missing;
714
715 line += 8;
716
717 /* Get IP version from the first four bits */
718 ip_v = (*line & 0xf0) >> 4;
719
720 if (ip_v == 4) {
721 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100722 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100723
724 hdr_ip4 = (struct ip *)line;
725
726 if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) {
727 /* Fail if buffer length is not large enough to contain
728 * CIP magic, CIP length, IPv4 header */
729 goto missing;
730 } else if (hdr_ip4->ip_p != IPPROTO_TCP) {
731 /* The protocol does not include a TCP header */
732 conn->err_code = CO_ER_CIP_BAD_PROTO;
733 goto fail;
734 } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) {
735 /* Fail if buffer length is not large enough to contain
736 * CIP magic, CIP length, IPv4 header, TCP header */
737 goto missing;
738 }
739
David Carlier3015a2e2016-07-04 22:51:33 +0100740 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100741
742 /* update the session's addresses and mark them set */
743 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
744 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
745 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source;
746
747 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
748 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
749 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest;
750
751 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
752 }
753 else if (ip_v == 6) {
754 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100755 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100756
757 hdr_ip6 = (struct ip6_hdr *)line;
758
759 if (trash.len < 28) {
760 /* Fail if buffer length is not large enough to contain
761 * CIP magic, CIP length, IPv6 header */
762 goto missing;
763 } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
764 /* The protocol does not include a TCP header */
765 conn->err_code = CO_ER_CIP_BAD_PROTO;
766 goto fail;
767 } else if (trash.len < 48) {
768 /* Fail if buffer length is not large enough to contain
769 * CIP magic, CIP length, IPv6 header, TCP header */
770 goto missing;
771 }
772
David Carlier3015a2e2016-07-04 22:51:33 +0100773 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100774
775 /* update the session's addresses and mark them set */
776 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
777 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src;
778 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source;
779
780 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
781 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst;
782 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest;
783
784 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
785 }
786 else {
787 /* The protocol does not match something known (IPv4/IPv6) */
788 conn->err_code = CO_ER_CIP_BAD_PROTO;
789 goto fail;
790 }
791
792 line += cip_len;
793 trash.len = line - trash.str;
794
795 /* remove the NetScaler Client IP header from the request. For this
796 * we re-read the exact line at once. If we don't get the exact same
797 * result, we fail.
798 */
799 do {
800 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
801 if (len2 < 0 && errno == EINTR)
802 continue;
803 if (len2 != trash.len)
804 goto recv_abort;
805 } while (0);
806
807 conn->flags &= ~flag;
808 return 1;
809
810 missing:
811 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
812 * we have not read anything. Otherwise we need to fail because we won't
813 * be able to poll anymore.
814 */
815 conn->err_code = CO_ER_CIP_TRUNCATED;
816 goto fail;
817
818 bad_magic:
819 conn->err_code = CO_ER_CIP_BAD_MAGIC;
820 goto fail;
821
822 recv_abort:
823 conn->err_code = CO_ER_CIP_ABORT;
824 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
825 goto fail;
826
827 fail:
828 __conn_sock_stop_both(conn);
829 conn->flags |= CO_FL_ERROR;
830 return 0;
831}
832
David Safb76832014-05-08 23:42:08 -0400833int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
834{
835 int ret = 0;
836
837 if (srv && (srv->pp_opts & SRV_PP_V2)) {
838 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
839 }
840 else {
841 if (remote)
842 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
843 else
844 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
845 }
846
847 return ret;
848}
849
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200850/* Makes a PROXY protocol line from the two addresses. The output is sent to
851 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
852 * It returns the number of bytes composing this line (including the trailing
853 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200854 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
855 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200856 */
David Safb76832014-05-08 23:42:08 -0400857int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200858{
859 int ret = 0;
860
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200861 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200862 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
863 if (ret >= buf_len)
864 return 0;
865
866 /* IPv4 src */
867 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
868 return 0;
869
870 ret += strlen(buf + ret);
871 if (ret >= buf_len)
872 return 0;
873
874 buf[ret++] = ' ';
875
876 /* IPv4 dst */
877 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
878 return 0;
879
880 ret += strlen(buf + ret);
881 if (ret >= buf_len)
882 return 0;
883
884 /* source and destination ports */
885 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
886 ntohs(((struct sockaddr_in *)src)->sin_port),
887 ntohs(((struct sockaddr_in *)dst)->sin_port));
888 if (ret >= buf_len)
889 return 0;
890 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200891 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200892 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
893 if (ret >= buf_len)
894 return 0;
895
896 /* IPv6 src */
897 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
898 return 0;
899
900 ret += strlen(buf + ret);
901 if (ret >= buf_len)
902 return 0;
903
904 buf[ret++] = ' ';
905
906 /* IPv6 dst */
907 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
908 return 0;
909
910 ret += strlen(buf + ret);
911 if (ret >= buf_len)
912 return 0;
913
914 /* source and destination ports */
915 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
916 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
917 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
918 if (ret >= buf_len)
919 return 0;
920 }
921 else {
922 /* unknown family combination */
923 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
924 if (ret >= buf_len)
925 return 0;
926 }
927 return ret;
928}
David Safb76832014-05-08 23:42:08 -0400929
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100930#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
931static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400932{
933 struct tlv *tlv;
934
935 if (!dest || (length + sizeof(*tlv) > dest_len))
936 return 0;
937
938 tlv = (struct tlv *)dest;
939
940 tlv->type = type;
941 tlv->length_hi = length >> 8;
942 tlv->length_lo = length & 0x00ff;
943 memcpy(tlv->value, value, length);
944 return length + sizeof(*tlv);
945}
946#endif
947
948int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
949{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200950 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400951 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200952 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +0200953 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -0400954 struct sockaddr_storage *src = &null_addr;
955 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100956
David Safb76832014-05-08 23:42:08 -0400957#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400958 char *value = NULL;
959 struct tlv_ssl *tlv;
960 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400961 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400962#endif
963
964 if (buf_len < PP2_HEADER_LEN)
965 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200966 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400967
968 if (remote) {
969 src = &remote->addr.from;
970 dst = &remote->addr.to;
971 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100972
David Safb76832014-05-08 23:42:08 -0400973 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
974 if (buf_len < PP2_HDR_LEN_INET)
975 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200976 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
977 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
978 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
979 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
980 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
981 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -0400982 ret = PP2_HDR_LEN_INET;
983 }
984 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
985 if (buf_len < PP2_HDR_LEN_INET6)
986 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200987 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
988 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
989 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
990 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
991 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
992 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -0400993 ret = PP2_HDR_LEN_INET6;
994 }
995 else {
996 if (buf_len < PP2_HDR_LEN_UNSPEC)
997 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200998 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
999 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001000 ret = PP2_HDR_LEN_UNSPEC;
1001 }
1002
1003#ifdef USE_OPENSSL
1004 if (srv->pp_opts & SRV_PP_V2_SSL) {
1005 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1006 return 0;
1007 tlv = (struct tlv_ssl *)&buf[ret];
1008 memset(tlv, 0, sizeof(struct tlv_ssl));
1009 ssl_tlv_len += sizeof(struct tlv_ssl);
1010 tlv->tlv.type = PP2_TYPE_SSL;
1011 if (ssl_sock_is_ssl(remote)) {
1012 tlv->client |= PP2_CLIENT_SSL;
1013 value = ssl_sock_get_version(remote);
1014 if (value) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001015 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_TYPE_SSL_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001016 }
Dave McCowan328fb582014-07-30 10:39:13 -04001017 if (ssl_sock_get_cert_used_sess(remote)) {
1018 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001019 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001020 if (ssl_sock_get_cert_used_conn(remote))
1021 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001022 }
1023 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -04001024 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001025 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001026 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_TYPE_SSL_CN, cn_trash->len, cn_trash->str);
David Safb76832014-05-08 23:42:08 -04001027 }
1028 }
1029 }
1030 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1031 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1032 ret += ssl_tlv_len;
1033 }
1034#endif
1035
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001036#ifdef CONFIG_HAP_NS
1037 if (remote && (remote->proxy_netns)) {
1038 if ((buf_len - ret) < sizeof(struct tlv))
1039 return 0;
1040 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
1041 }
1042#endif
1043
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001044 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001045
1046 return ret;
1047}
Emeric Brun4f603012017-01-05 15:11:44 +01001048
1049/* fetch if the received connection used a PROXY protocol header */
1050int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1051{
1052 struct connection *conn;
1053
1054 conn = objt_conn(smp->sess->origin);
1055 if (!conn)
1056 return 0;
1057
1058 if (!(conn->flags & CO_FL_CONNECTED)) {
1059 smp->flags |= SMP_F_MAY_CHANGE;
1060 return 0;
1061 }
1062
1063 smp->flags = 0;
1064 smp->data.type = SMP_T_BOOL;
1065 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1066
1067 return 1;
1068}
1069
1070/* Note: must not be declared <const> as its list will be overwritten.
1071 * Note: fetches that may return multiple types must be declared as the lowest
1072 * common denominator, the type that can be casted into all other ones. For
1073 * instance v4/v6 must be declared v4.
1074 */
1075static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1076 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
1077 { /* END */ },
1078}};
1079
1080
1081__attribute__((constructor))
1082static void __connection_init(void)
1083{
1084 sample_register_fetches(&sample_fetch_keywords);
1085}