blob: 054823b1394cfefb17faeede1ae6d506d1e0bd1c [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 Tarreau2396c1c2012-10-03 21:12:16 +0200135 /* The wake callback may be used to process a critical error and abort the
136 * connection. If so, we don't want to go further as the connection will
137 * have been released and the FD destroyed.
138 */
139 if ((conn->flags & CO_FL_WAKE_DATA) &&
140 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
141 conn->data->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200142 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200143
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200144 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200145 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200146 conn->flags |= CO_FL_CONNECTED;
147
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200148 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100149 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200150
151 /* commit polling changes */
152 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200153 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200154}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200155
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200156/* Update polling on connection <c>'s file descriptor depending on its current
157 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
158 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
159 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200160 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200162void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200163{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200164 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165
Willy Tarreau3c728722014-01-23 13:50:42 +0100166 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200167 return;
168
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200169 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100170 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 +0100171 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100172 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200173 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100174 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
175 fd_stop_recv(c->t.sock.fd);
176 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200177 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200178
179 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100180 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 +0200181 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100182 f |= CO_FL_CURR_WR_ENA;
183 }
184 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
185 fd_stop_send(c->t.sock.fd);
186 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200187 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100188 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200189}
190
191/* Update polling on connection <c>'s file descriptor depending on its current
192 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
193 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
194 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200195 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200196 */
197void conn_update_sock_polling(struct connection *c)
198{
199 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200200
Willy Tarreau3c728722014-01-23 13:50:42 +0100201 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200202 return;
203
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200204 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100205 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 +0100206 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100207 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200208 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100209 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
210 fd_stop_recv(c->t.sock.fd);
211 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200212 }
213
214 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100215 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 +0100216 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100217 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200218 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100219 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
220 fd_stop_send(c->t.sock.fd);
221 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200222 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100223 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200224}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200225
Willy Tarreauff3e6482015-03-12 23:56:52 +0100226/* Send a message over an established connection. It makes use of send() and
227 * returns the same return code and errno. If the socket layer is not ready yet
228 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
229 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
230 * EMSGSIZE if called with a zero length message. The purpose is to simplify
231 * some rare attempts to directly write on the socket from above the connection
232 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
233 * It automatically retries on EINTR. Other errors cause the connection to be
234 * marked as in error state. It takes similar arguments as send() except the
235 * first one which is the connection instead of the file descriptor. Note,
236 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
237 */
238int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
239{
240 int ret;
241
242 ret = -1;
243 errno = ENOTSOCK;
244
245 if (conn->flags & CO_FL_SOCK_WR_SH)
246 goto fail;
247
248 if (!conn_ctrl_ready(conn))
249 goto fail;
250
251 errno = EMSGSIZE;
252 if (!len)
253 goto fail;
254
255 if (!fd_send_ready(conn->t.sock.fd))
256 goto wait;
257
258 do {
259 ret = send(conn->t.sock.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
260 } while (ret < 0 && errno == EINTR);
261
262
263 if (ret > 0)
264 return ret;
265
266 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
267 wait:
268 fd_cant_send(conn->t.sock.fd);
269 return 0;
270 }
271 fail:
272 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
273 return ret;
274}
275
Willy Tarreaud85c4852015-03-13 00:40:28 +0100276/* Drains possibly pending incoming data on the file descriptor attached to the
277 * connection and update the connection's flags accordingly. This is used to
278 * know whether we need to disable lingering on close. Returns non-zero if it
279 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
280 * flag may also be updated if the incoming shutdown was reported by the drain()
281 * function.
282 */
283int conn_sock_drain(struct connection *conn)
284{
285 if (!conn_ctrl_ready(conn))
286 return 1;
287
288 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
289 return 1;
290
291 if (fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) {
292 fdtab[conn->t.sock.fd].linger_risk = 0;
293 }
294 else {
295 if (!fd_recv_ready(conn->t.sock.fd))
296 return 0;
297
298 /* disable draining if we were called and have no drain function */
299 if (!conn->ctrl->drain) {
300 __conn_data_stop_recv(conn);
301 return 0;
302 }
303
304 if (conn->ctrl->drain(conn->t.sock.fd) <= 0)
305 return 0;
306 }
307
308 conn->flags |= CO_FL_SOCK_RD_SH;
309 return 1;
310}
311
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100312/*
313 * Get data length from tlv
314 */
315static int get_tlv_length(const struct tlv *src)
316{
317 return (src->length_hi << 8) | src->length_lo;
318}
319
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200320/* This handshake handler waits a PROXY protocol header at the beginning of the
321 * raw data stream. The header looks like this :
322 *
323 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
324 *
325 * There must be exactly one space between each field. Fields are :
326 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
327 * - SRC3 : layer 3 (eg: IP) source address in standard text form
328 * - DST3 : layer 3 (eg: IP) destination address in standard text form
329 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
330 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
331 *
332 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
333 *
334 * The header line is small and in all cases smaller than the smallest normal
335 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
336 * can safely use MSG_PEEK and avoid buffering.
337 *
338 * Once the data is fetched, the values are set in the connection's address
339 * fields, and data are removed from the socket's buffer. The function returns
340 * zero if it needs to wait for more data or if it fails, or 1 if it completed
341 * and removed itself.
342 */
343int conn_recv_proxy(struct connection *conn, int flag)
344{
345 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200346 struct proxy_hdr_v2 *hdr_v2;
347 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100348 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200349 int tlv_offset = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200350
351 /* we might have been called just after an asynchronous shutr */
352 if (conn->flags & CO_FL_SOCK_RD_SH)
353 goto fail;
354
Willy Tarreau3c728722014-01-23 13:50:42 +0100355 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200356 goto fail;
357
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100358 if (!fd_recv_ready(conn->t.sock.fd))
359 return 0;
360
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200361 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100362 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
363 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200364 if (errno == EINTR)
365 continue;
366 if (errno == EAGAIN) {
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100367 fd_cant_recv(conn->t.sock.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200368 return 0;
369 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100370 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200371 }
372 } while (0);
373
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100374 if (!trash.len) {
375 /* client shutdown */
376 conn->err_code = CO_ER_PRX_EMPTY;
377 goto fail;
378 }
379
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100380 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200381 goto missing;
382
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100383 line = trash.str;
384 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200385
386 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200387 if (strncmp(line, "PROXY ", 6) != 0)
388 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200389
390 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200391 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200392 goto missing;
393
David CARLIER42ff05e2016-03-24 09:22:36 +0000394 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200395 u32 src3, dst3, sport, dport;
396
397 line += 5;
398
399 src3 = inetaddr_host_lim_ret(line, end, &line);
400 if (line == end)
401 goto missing;
402 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100403 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200404
405 dst3 = inetaddr_host_lim_ret(line, end, &line);
406 if (line == end)
407 goto missing;
408 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100409 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200410
411 sport = read_uint((const char **)&line, end);
412 if (line == end)
413 goto missing;
414 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100415 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200416
417 dport = read_uint((const char **)&line, end);
418 if (line > end - 2)
419 goto missing;
420 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100421 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200422 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100423 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200424
425 /* update the session's addresses and mark them set */
426 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
427 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
428 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
429
430 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
431 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
432 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
433 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
434 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000435 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200436 u32 sport, dport;
437 char *src_s;
438 char *dst_s, *sport_s, *dport_s;
439 struct in6_addr src3, dst3;
440
441 line += 5;
442
443 src_s = line;
444 dst_s = sport_s = dport_s = NULL;
445 while (1) {
446 if (line > end - 2) {
447 goto missing;
448 }
449 else if (*line == '\r') {
450 *line = 0;
451 line++;
452 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100453 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200454 break;
455 }
456
457 if (*line == ' ') {
458 *line = 0;
459 if (!dst_s)
460 dst_s = line + 1;
461 else if (!sport_s)
462 sport_s = line + 1;
463 else if (!dport_s)
464 dport_s = line + 1;
465 }
466 line++;
467 }
468
469 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100470 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200471
472 sport = read_uint((const char **)&sport_s,dport_s - 1);
473 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100474 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200475
476 dport = read_uint((const char **)&dport_s,line - 2);
477 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100478 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200479
480 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
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, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100484 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200485
486 /* update the session's addresses and mark them set */
487 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
488 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
489 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
490
491 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
492 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
493 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
494 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
495 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200496 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
497 /* This can be a UNIX socket forwarded by an haproxy upstream */
498 line += 9;
499 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200500 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200501 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100502 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200503 goto fail;
504 }
505
Willy Tarreau77992672014-06-14 11:06:17 +0200506 trash.len = line - trash.str;
507 goto eat_header;
508
509 not_v1:
510 /* try PPv2 */
511 if (trash.len < PP2_HEADER_LEN)
512 goto missing;
513
514 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
515
516 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
517 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
518 conn->err_code = CO_ER_PRX_NOT_HDR;
519 goto fail;
520 }
521
522 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
523 goto missing;
524
525 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
526 case 0x01: /* PROXY command */
527 switch (hdr_v2->fam) {
528 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100529 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
530 goto bad_header;
531
Willy Tarreau77992672014-06-14 11:06:17 +0200532 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
533 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
534 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
535 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
536 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
537 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port;
538 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200539 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100540 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200541 break;
542 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100543 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
544 goto bad_header;
545
Willy Tarreau77992672014-06-14 11:06:17 +0200546 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
547 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
548 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
549 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
550 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
551 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
552 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200553 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100554 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200555 break;
556 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100557
558 /* TLV parsing */
559 if (tlv_length > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100560 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
561 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
562 const int tlv_len = get_tlv_length(tlv_packet);
563 tlv_offset += tlv_len + TLV_HEADER_SIZE;
564
565 switch (tlv_packet->type) {
566#ifdef CONFIG_HAP_NS
567 case PP2_TYPE_NETNS: {
568 const struct netns_entry *ns;
569 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
570 if (ns)
571 conn->proxy_netns = ns;
572 break;
573 }
574#endif
575 default:
576 break;
577 }
578 }
579 }
580
Willy Tarreau77992672014-06-14 11:06:17 +0200581 /* unsupported protocol, keep local connection address */
582 break;
583 case 0x00: /* LOCAL command */
584 /* keep local connection address for LOCAL */
585 break;
586 default:
587 goto bad_header; /* not a supported command */
588 }
589
590 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
591 goto eat_header;
592
593 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200594 /* remove the PROXY line from the request. For this we re-read the
595 * exact line at once. If we don't get the exact same result, we
596 * fail.
597 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200598 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100599 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200600 if (len2 < 0 && errno == EINTR)
601 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100602 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100603 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200604 } while (0);
605
606 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100607 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200608 return 1;
609
610 missing:
611 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
612 * we have not read anything. Otherwise we need to fail because we won't
613 * be able to poll anymore.
614 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100615 conn->err_code = CO_ER_PRX_TRUNCATED;
616 goto fail;
617
618 bad_header:
619 /* This is not a valid proxy protocol header */
620 conn->err_code = CO_ER_PRX_BAD_HDR;
621 goto fail;
622
623 recv_abort:
624 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100625 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100626 goto fail;
627
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200628 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100629 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200630 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200631 return 0;
632}
633
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100634/* This handshake handler waits a NetScaler Client IP insertion header
635 * at the beginning of the raw data stream. The header looks like this:
636 *
637 * 4 bytes: CIP magic number
638 * 4 bytes: Header length
639 * 20+ bytes: Header of the last IP packet sent by the client during
640 * TCP handshake.
641 * 20+ bytes: Header of the last TCP packet sent by the client during
642 * TCP handshake.
643 *
644 * This line MUST be at the beginning of the buffer and MUST NOT be
645 * fragmented.
646 *
647 * The header line is small and in all cases smaller than the smallest normal
648 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
649 * can safely use MSG_PEEK and avoid buffering.
650 *
651 * Once the data is fetched, the values are set in the connection's address
652 * fields, and data are removed from the socket's buffer. The function returns
653 * zero if it needs to wait for more data or if it fails, or 1 if it completed
654 * and removed itself.
655 */
656int conn_recv_netscaler_cip(struct connection *conn, int flag)
657{
658 char *line;
659 uint32_t cip_magic;
660 uint32_t cip_len;
661 uint8_t ip_v;
662
663 /* we might have been called just after an asynchronous shutr */
664 if (conn->flags & CO_FL_SOCK_RD_SH)
665 goto fail;
666
667 if (!conn_ctrl_ready(conn))
668 goto fail;
669
670 if (!fd_recv_ready(conn->t.sock.fd))
671 return 0;
672
673 do {
674 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
675 if (trash.len < 0) {
676 if (errno == EINTR)
677 continue;
678 if (errno == EAGAIN) {
679 fd_cant_recv(conn->t.sock.fd);
680 return 0;
681 }
682 goto recv_abort;
683 }
684 } while (0);
685
686 if (!trash.len) {
687 /* client shutdown */
688 conn->err_code = CO_ER_CIP_EMPTY;
689 goto fail;
690 }
691
692 /* Fail if buffer length is not large enough to contain
693 * CIP magic, CIP length */
694 if (trash.len < 8)
695 goto missing;
696
697 line = trash.str;
698
699 cip_magic = ntohl(*(uint32_t *)line);
700 cip_len = ntohl(*(uint32_t *)(line+4));
701
702 /* Decode a possible NetScaler Client IP request, fail early if
703 * it does not match */
704 if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic)
705 goto bad_magic;
706
707 /* Fail if buffer length is not large enough to contain
708 * CIP magic, CIP length, minimal IP header */
709 if (trash.len < 28)
710 goto missing;
711
712 line += 8;
713
714 /* Get IP version from the first four bits */
715 ip_v = (*line & 0xf0) >> 4;
716
717 if (ip_v == 4) {
718 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100719 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100720
721 hdr_ip4 = (struct ip *)line;
722
723 if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) {
724 /* Fail if buffer length is not large enough to contain
725 * CIP magic, CIP length, IPv4 header */
726 goto missing;
727 } else if (hdr_ip4->ip_p != IPPROTO_TCP) {
728 /* The protocol does not include a TCP header */
729 conn->err_code = CO_ER_CIP_BAD_PROTO;
730 goto fail;
731 } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) {
732 /* Fail if buffer length is not large enough to contain
733 * CIP magic, CIP length, IPv4 header, TCP header */
734 goto missing;
735 }
736
David Carlier3015a2e2016-07-04 22:51:33 +0100737 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100738
739 /* update the session's addresses and mark them set */
740 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
741 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
742 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source;
743
744 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
745 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
746 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest;
747
748 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
749 }
750 else if (ip_v == 6) {
751 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100752 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100753
754 hdr_ip6 = (struct ip6_hdr *)line;
755
756 if (trash.len < 28) {
757 /* Fail if buffer length is not large enough to contain
758 * CIP magic, CIP length, IPv6 header */
759 goto missing;
760 } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
761 /* The protocol does not include a TCP header */
762 conn->err_code = CO_ER_CIP_BAD_PROTO;
763 goto fail;
764 } else if (trash.len < 48) {
765 /* Fail if buffer length is not large enough to contain
766 * CIP magic, CIP length, IPv6 header, TCP header */
767 goto missing;
768 }
769
David Carlier3015a2e2016-07-04 22:51:33 +0100770 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100771
772 /* update the session's addresses and mark them set */
773 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
774 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src;
775 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source;
776
777 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
778 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst;
779 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest;
780
781 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
782 }
783 else {
784 /* The protocol does not match something known (IPv4/IPv6) */
785 conn->err_code = CO_ER_CIP_BAD_PROTO;
786 goto fail;
787 }
788
789 line += cip_len;
790 trash.len = line - trash.str;
791
792 /* remove the NetScaler Client IP header from the request. For this
793 * we re-read the exact line at once. If we don't get the exact same
794 * result, we fail.
795 */
796 do {
797 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
798 if (len2 < 0 && errno == EINTR)
799 continue;
800 if (len2 != trash.len)
801 goto recv_abort;
802 } while (0);
803
804 conn->flags &= ~flag;
805 return 1;
806
807 missing:
808 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
809 * we have not read anything. Otherwise we need to fail because we won't
810 * be able to poll anymore.
811 */
812 conn->err_code = CO_ER_CIP_TRUNCATED;
813 goto fail;
814
815 bad_magic:
816 conn->err_code = CO_ER_CIP_BAD_MAGIC;
817 goto fail;
818
819 recv_abort:
820 conn->err_code = CO_ER_CIP_ABORT;
821 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
822 goto fail;
823
824 fail:
825 __conn_sock_stop_both(conn);
826 conn->flags |= CO_FL_ERROR;
827 return 0;
828}
829
David Safb76832014-05-08 23:42:08 -0400830int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
831{
832 int ret = 0;
833
834 if (srv && (srv->pp_opts & SRV_PP_V2)) {
835 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
836 }
837 else {
838 if (remote)
839 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
840 else
841 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
842 }
843
844 return ret;
845}
846
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200847/* Makes a PROXY protocol line from the two addresses. The output is sent to
848 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
849 * It returns the number of bytes composing this line (including the trailing
850 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200851 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
852 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200853 */
David Safb76832014-05-08 23:42:08 -0400854int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200855{
856 int ret = 0;
857
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200858 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200859 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
860 if (ret >= buf_len)
861 return 0;
862
863 /* IPv4 src */
864 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
865 return 0;
866
867 ret += strlen(buf + ret);
868 if (ret >= buf_len)
869 return 0;
870
871 buf[ret++] = ' ';
872
873 /* IPv4 dst */
874 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
875 return 0;
876
877 ret += strlen(buf + ret);
878 if (ret >= buf_len)
879 return 0;
880
881 /* source and destination ports */
882 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
883 ntohs(((struct sockaddr_in *)src)->sin_port),
884 ntohs(((struct sockaddr_in *)dst)->sin_port));
885 if (ret >= buf_len)
886 return 0;
887 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200888 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200889 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
890 if (ret >= buf_len)
891 return 0;
892
893 /* IPv6 src */
894 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
895 return 0;
896
897 ret += strlen(buf + ret);
898 if (ret >= buf_len)
899 return 0;
900
901 buf[ret++] = ' ';
902
903 /* IPv6 dst */
904 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
905 return 0;
906
907 ret += strlen(buf + ret);
908 if (ret >= buf_len)
909 return 0;
910
911 /* source and destination ports */
912 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
913 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
914 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
915 if (ret >= buf_len)
916 return 0;
917 }
918 else {
919 /* unknown family combination */
920 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
921 if (ret >= buf_len)
922 return 0;
923 }
924 return ret;
925}
David Safb76832014-05-08 23:42:08 -0400926
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100927#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
928static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400929{
930 struct tlv *tlv;
931
932 if (!dest || (length + sizeof(*tlv) > dest_len))
933 return 0;
934
935 tlv = (struct tlv *)dest;
936
937 tlv->type = type;
938 tlv->length_hi = length >> 8;
939 tlv->length_lo = length & 0x00ff;
940 memcpy(tlv->value, value, length);
941 return length + sizeof(*tlv);
942}
943#endif
944
945int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
946{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200947 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400948 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200949 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +0200950 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -0400951 struct sockaddr_storage *src = &null_addr;
952 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100953
David Safb76832014-05-08 23:42:08 -0400954#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400955 char *value = NULL;
956 struct tlv_ssl *tlv;
957 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400958 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400959#endif
960
961 if (buf_len < PP2_HEADER_LEN)
962 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200963 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400964
965 if (remote) {
966 src = &remote->addr.from;
967 dst = &remote->addr.to;
968 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100969
David Safb76832014-05-08 23:42:08 -0400970 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
971 if (buf_len < PP2_HDR_LEN_INET)
972 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200973 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
974 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
975 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
976 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
977 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
978 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -0400979 ret = PP2_HDR_LEN_INET;
980 }
981 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
982 if (buf_len < PP2_HDR_LEN_INET6)
983 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200984 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
985 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
986 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
987 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
988 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
989 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -0400990 ret = PP2_HDR_LEN_INET6;
991 }
992 else {
993 if (buf_len < PP2_HDR_LEN_UNSPEC)
994 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200995 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
996 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -0400997 ret = PP2_HDR_LEN_UNSPEC;
998 }
999
1000#ifdef USE_OPENSSL
1001 if (srv->pp_opts & SRV_PP_V2_SSL) {
1002 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1003 return 0;
1004 tlv = (struct tlv_ssl *)&buf[ret];
1005 memset(tlv, 0, sizeof(struct tlv_ssl));
1006 ssl_tlv_len += sizeof(struct tlv_ssl);
1007 tlv->tlv.type = PP2_TYPE_SSL;
1008 if (ssl_sock_is_ssl(remote)) {
1009 tlv->client |= PP2_CLIENT_SSL;
1010 value = ssl_sock_get_version(remote);
1011 if (value) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001012 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 -04001013 }
Dave McCowan328fb582014-07-30 10:39:13 -04001014 if (ssl_sock_get_cert_used_sess(remote)) {
1015 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001016 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001017 if (ssl_sock_get_cert_used_conn(remote))
1018 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001019 }
1020 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -04001021 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001022 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001023 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 -04001024 }
1025 }
1026 }
1027 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1028 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1029 ret += ssl_tlv_len;
1030 }
1031#endif
1032
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001033#ifdef CONFIG_HAP_NS
1034 if (remote && (remote->proxy_netns)) {
1035 if ((buf_len - ret) < sizeof(struct tlv))
1036 return 0;
1037 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
1038 }
1039#endif
1040
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001041 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001042
1043 return ret;
1044}
Emeric Brun4f603012017-01-05 15:11:44 +01001045
1046/* fetch if the received connection used a PROXY protocol header */
1047int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1048{
1049 struct connection *conn;
1050
1051 conn = objt_conn(smp->sess->origin);
1052 if (!conn)
1053 return 0;
1054
1055 if (!(conn->flags & CO_FL_CONNECTED)) {
1056 smp->flags |= SMP_F_MAY_CHANGE;
1057 return 0;
1058 }
1059
1060 smp->flags = 0;
1061 smp->data.type = SMP_T_BOOL;
1062 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1063
1064 return 1;
1065}
1066
1067/* Note: must not be declared <const> as its list will be overwritten.
1068 * Note: fetches that may return multiple types must be declared as the lowest
1069 * common denominator, the type that can be casted into all other ones. For
1070 * instance v4/v6 must be declared v4.
1071 */
1072static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1073 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
1074 { /* END */ },
1075}};
1076
1077
1078__attribute__((constructor))
1079static void __connection_init(void)
1080{
1081 sample_register_fetches(&sample_fetch_keywords);
1082}