blob: ba9b67545b775637371360dcb46f6d03a0fdc48a [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 Tarreau3c0cc492017-03-19 07:54:28 +0100103 /* force reporting of activity by clearing the previous flags :
104 * we'll have at least ERROR or CONNECTED at the end of an I/O,
105 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200106 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100107 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +0200108 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200109 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200110
Willy Tarreaud8375892014-01-21 11:01:08 +0100111 if (conn->xprt && fd_send_ready(fd) &&
112 ((conn->flags & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_WR_ENA)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100113 /* force reporting of activity by clearing the previous flags :
114 * we'll have at least ERROR or CONNECTED at the end of an I/O,
115 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200116 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100117 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +0200118 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200119 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200120
Willy Tarreauc76ae332012-07-12 15:32:13 +0200121 /* It may happen during the data phase that a handshake is
122 * enabled again (eg: SSL)
123 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100124 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200125 goto process_handshake;
126
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100127 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200128 /* still waiting for a connection to establish and nothing was
129 * attempted yet to probe the connection. Then let's retry the
130 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200131 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200132 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200133 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200134 }
Willy Tarreau2c6be842012-07-06 17:12:34 +0200135 leave:
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100136 /* Verify if the connection just established. */
Willy Tarreau7bf3fa32017-03-14 20:19:29 +0100137 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
138 conn->flags |= CO_FL_CONNECTED;
139
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100140 /* The wake callback is normally used to notify the data layer about
141 * data layer activity (successful send/recv), connection establishment,
142 * shutdown and fatal errors. We need to consider the following
143 * situations to wake up the data layer :
144 * - change among the CO_FL_NOTIFY_DATA flags :
145 * {DATA,SOCK}_{RD,WR}_SH, ERROR,
146 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
147 * end of handshake and transition to CONNECTED
148 * - raise of CONNECTED with HANDSHAKE down
149 * - end of HANDSHAKE with CONNECTED set
150 * - regular data layer activity
151 *
152 * Note that the wake callback is allowed to release the connection and
153 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200154 */
155 if ((conn->flags & CO_FL_WAKE_DATA) &&
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100156 (((conn->flags ^ flags) & CO_FL_NOTIFY_DATA) ||
157 ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED &&
158 (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED)) &&
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200159 conn->data->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200160 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200161
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200162 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100163 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200164
165 /* commit polling changes */
166 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200167 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200168}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200169
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200170/* Update polling on connection <c>'s file descriptor depending on its current
171 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
172 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
173 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200174 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200177{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200178 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200179
Willy Tarreau3c728722014-01-23 13:50:42 +0100180 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200181 return;
182
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200183 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100184 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 +0100185 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100186 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200187 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100188 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
189 fd_stop_recv(c->t.sock.fd);
190 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200191 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200192
193 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100194 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 +0200195 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100196 f |= CO_FL_CURR_WR_ENA;
197 }
198 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
199 fd_stop_send(c->t.sock.fd);
200 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200201 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100202 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200203}
204
205/* Update polling on connection <c>'s file descriptor depending on its current
206 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
207 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
208 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200209 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200210 */
211void conn_update_sock_polling(struct connection *c)
212{
213 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200214
Willy Tarreau3c728722014-01-23 13:50:42 +0100215 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200216 return;
217
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200218 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100219 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 +0100220 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100221 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200222 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100223 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
224 fd_stop_recv(c->t.sock.fd);
225 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200226 }
227
228 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100229 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 +0100230 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100231 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200232 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100233 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
234 fd_stop_send(c->t.sock.fd);
235 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200236 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100237 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200238}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200239
Willy Tarreauff3e6482015-03-12 23:56:52 +0100240/* Send a message over an established connection. It makes use of send() and
241 * returns the same return code and errno. If the socket layer is not ready yet
242 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
243 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
244 * EMSGSIZE if called with a zero length message. The purpose is to simplify
245 * some rare attempts to directly write on the socket from above the connection
246 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
247 * It automatically retries on EINTR. Other errors cause the connection to be
248 * marked as in error state. It takes similar arguments as send() except the
249 * first one which is the connection instead of the file descriptor. Note,
250 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
251 */
252int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
253{
254 int ret;
255
256 ret = -1;
257 errno = ENOTSOCK;
258
259 if (conn->flags & CO_FL_SOCK_WR_SH)
260 goto fail;
261
262 if (!conn_ctrl_ready(conn))
263 goto fail;
264
265 errno = EMSGSIZE;
266 if (!len)
267 goto fail;
268
269 if (!fd_send_ready(conn->t.sock.fd))
270 goto wait;
271
272 do {
273 ret = send(conn->t.sock.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
274 } while (ret < 0 && errno == EINTR);
275
276
277 if (ret > 0)
278 return ret;
279
280 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
281 wait:
282 fd_cant_send(conn->t.sock.fd);
283 return 0;
284 }
285 fail:
286 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
287 return ret;
288}
289
Willy Tarreaud85c4852015-03-13 00:40:28 +0100290/* Drains possibly pending incoming data on the file descriptor attached to the
291 * connection and update the connection's flags accordingly. This is used to
292 * know whether we need to disable lingering on close. Returns non-zero if it
293 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
294 * flag may also be updated if the incoming shutdown was reported by the drain()
295 * function.
296 */
297int conn_sock_drain(struct connection *conn)
298{
299 if (!conn_ctrl_ready(conn))
300 return 1;
301
302 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
303 return 1;
304
305 if (fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) {
306 fdtab[conn->t.sock.fd].linger_risk = 0;
307 }
308 else {
309 if (!fd_recv_ready(conn->t.sock.fd))
310 return 0;
311
312 /* disable draining if we were called and have no drain function */
313 if (!conn->ctrl->drain) {
314 __conn_data_stop_recv(conn);
315 return 0;
316 }
317
318 if (conn->ctrl->drain(conn->t.sock.fd) <= 0)
319 return 0;
320 }
321
322 conn->flags |= CO_FL_SOCK_RD_SH;
323 return 1;
324}
325
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100326/*
327 * Get data length from tlv
328 */
329static int get_tlv_length(const struct tlv *src)
330{
331 return (src->length_hi << 8) | src->length_lo;
332}
333
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200334/* This handshake handler waits a PROXY protocol header at the beginning of the
335 * raw data stream. The header looks like this :
336 *
337 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
338 *
339 * There must be exactly one space between each field. Fields are :
340 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
341 * - SRC3 : layer 3 (eg: IP) source address in standard text form
342 * - DST3 : layer 3 (eg: IP) destination address in standard text form
343 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
344 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
345 *
346 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
347 *
348 * The header line is small and in all cases smaller than the smallest normal
349 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
350 * can safely use MSG_PEEK and avoid buffering.
351 *
352 * Once the data is fetched, the values are set in the connection's address
353 * fields, and data are removed from the socket's buffer. The function returns
354 * zero if it needs to wait for more data or if it fails, or 1 if it completed
355 * and removed itself.
356 */
357int conn_recv_proxy(struct connection *conn, int flag)
358{
359 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200360 struct proxy_hdr_v2 *hdr_v2;
361 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100362 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200363 int tlv_offset = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200364
365 /* we might have been called just after an asynchronous shutr */
366 if (conn->flags & CO_FL_SOCK_RD_SH)
367 goto fail;
368
Willy Tarreau3c728722014-01-23 13:50:42 +0100369 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200370 goto fail;
371
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100372 if (!fd_recv_ready(conn->t.sock.fd))
373 return 0;
374
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200375 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100376 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
377 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200378 if (errno == EINTR)
379 continue;
380 if (errno == EAGAIN) {
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100381 fd_cant_recv(conn->t.sock.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200382 return 0;
383 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100384 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200385 }
386 } while (0);
387
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100388 if (!trash.len) {
389 /* client shutdown */
390 conn->err_code = CO_ER_PRX_EMPTY;
391 goto fail;
392 }
393
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100394 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200395 goto missing;
396
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100397 line = trash.str;
398 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200399
400 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200401 if (strncmp(line, "PROXY ", 6) != 0)
402 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200403
404 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200405 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200406 goto missing;
407
David CARLIER42ff05e2016-03-24 09:22:36 +0000408 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200409 u32 src3, dst3, sport, dport;
410
411 line += 5;
412
413 src3 = inetaddr_host_lim_ret(line, end, &line);
414 if (line == end)
415 goto missing;
416 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100417 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200418
419 dst3 = inetaddr_host_lim_ret(line, end, &line);
420 if (line == end)
421 goto missing;
422 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100423 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200424
425 sport = read_uint((const char **)&line, end);
426 if (line == end)
427 goto missing;
428 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100429 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200430
431 dport = read_uint((const char **)&line, end);
432 if (line > end - 2)
433 goto missing;
434 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100435 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200436 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100437 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200438
439 /* update the session's addresses and mark them set */
440 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
441 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
442 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
443
444 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
445 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
446 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
447 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
448 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000449 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200450 u32 sport, dport;
451 char *src_s;
452 char *dst_s, *sport_s, *dport_s;
453 struct in6_addr src3, dst3;
454
455 line += 5;
456
457 src_s = line;
458 dst_s = sport_s = dport_s = NULL;
459 while (1) {
460 if (line > end - 2) {
461 goto missing;
462 }
463 else if (*line == '\r') {
464 *line = 0;
465 line++;
466 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100467 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200468 break;
469 }
470
471 if (*line == ' ') {
472 *line = 0;
473 if (!dst_s)
474 dst_s = line + 1;
475 else if (!sport_s)
476 sport_s = line + 1;
477 else if (!dport_s)
478 dport_s = line + 1;
479 }
480 line++;
481 }
482
483 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100484 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200485
486 sport = read_uint((const char **)&sport_s,dport_s - 1);
487 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100488 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200489
490 dport = read_uint((const char **)&dport_s,line - 2);
491 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100492 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200493
494 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100495 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200496
497 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100498 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200499
500 /* update the session's addresses and mark them set */
501 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
502 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
503 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
504
505 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
506 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
507 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
508 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
509 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200510 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
511 /* This can be a UNIX socket forwarded by an haproxy upstream */
512 line += 9;
513 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200514 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200515 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100516 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200517 goto fail;
518 }
519
Willy Tarreau77992672014-06-14 11:06:17 +0200520 trash.len = line - trash.str;
521 goto eat_header;
522
523 not_v1:
524 /* try PPv2 */
525 if (trash.len < PP2_HEADER_LEN)
526 goto missing;
527
528 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
529
530 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
531 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
532 conn->err_code = CO_ER_PRX_NOT_HDR;
533 goto fail;
534 }
535
536 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
537 goto missing;
538
539 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
540 case 0x01: /* PROXY command */
541 switch (hdr_v2->fam) {
542 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100543 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
544 goto bad_header;
545
Willy Tarreau77992672014-06-14 11:06:17 +0200546 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
547 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
548 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
549 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
550 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
551 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.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_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100554 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200555 break;
556 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100557 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
558 goto bad_header;
559
Willy Tarreau77992672014-06-14 11:06:17 +0200560 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
561 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
562 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
563 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
564 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
565 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
566 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200567 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100568 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200569 break;
570 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100571
572 /* TLV parsing */
573 if (tlv_length > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100574 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
575 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
576 const int tlv_len = get_tlv_length(tlv_packet);
577 tlv_offset += tlv_len + TLV_HEADER_SIZE;
578
579 switch (tlv_packet->type) {
580#ifdef CONFIG_HAP_NS
581 case PP2_TYPE_NETNS: {
582 const struct netns_entry *ns;
583 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
584 if (ns)
585 conn->proxy_netns = ns;
586 break;
587 }
588#endif
589 default:
590 break;
591 }
592 }
593 }
594
Willy Tarreau77992672014-06-14 11:06:17 +0200595 /* unsupported protocol, keep local connection address */
596 break;
597 case 0x00: /* LOCAL command */
598 /* keep local connection address for LOCAL */
599 break;
600 default:
601 goto bad_header; /* not a supported command */
602 }
603
604 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
605 goto eat_header;
606
607 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200608 /* remove the PROXY line from the request. For this we re-read the
609 * exact line at once. If we don't get the exact same result, we
610 * fail.
611 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200612 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100613 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200614 if (len2 < 0 && errno == EINTR)
615 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100616 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100617 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200618 } while (0);
619
620 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100621 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200622 return 1;
623
624 missing:
625 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
626 * we have not read anything. Otherwise we need to fail because we won't
627 * be able to poll anymore.
628 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100629 conn->err_code = CO_ER_PRX_TRUNCATED;
630 goto fail;
631
632 bad_header:
633 /* This is not a valid proxy protocol header */
634 conn->err_code = CO_ER_PRX_BAD_HDR;
635 goto fail;
636
637 recv_abort:
638 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100639 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100640 goto fail;
641
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200642 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100643 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200644 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200645 return 0;
646}
647
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100648/* This handshake handler waits a NetScaler Client IP insertion header
649 * at the beginning of the raw data stream. The header looks like this:
650 *
651 * 4 bytes: CIP magic number
652 * 4 bytes: Header length
653 * 20+ bytes: Header of the last IP packet sent by the client during
654 * TCP handshake.
655 * 20+ bytes: Header of the last TCP packet sent by the client during
656 * TCP handshake.
657 *
658 * This line MUST be at the beginning of the buffer and MUST NOT be
659 * fragmented.
660 *
661 * The header line is small and in all cases smaller than the smallest normal
662 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
663 * can safely use MSG_PEEK and avoid buffering.
664 *
665 * Once the data is fetched, the values are set in the connection's address
666 * fields, and data are removed from the socket's buffer. The function returns
667 * zero if it needs to wait for more data or if it fails, or 1 if it completed
668 * and removed itself.
669 */
670int conn_recv_netscaler_cip(struct connection *conn, int flag)
671{
672 char *line;
673 uint32_t cip_magic;
674 uint32_t cip_len;
675 uint8_t ip_v;
676
677 /* we might have been called just after an asynchronous shutr */
678 if (conn->flags & CO_FL_SOCK_RD_SH)
679 goto fail;
680
681 if (!conn_ctrl_ready(conn))
682 goto fail;
683
684 if (!fd_recv_ready(conn->t.sock.fd))
685 return 0;
686
687 do {
688 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
689 if (trash.len < 0) {
690 if (errno == EINTR)
691 continue;
692 if (errno == EAGAIN) {
693 fd_cant_recv(conn->t.sock.fd);
694 return 0;
695 }
696 goto recv_abort;
697 }
698 } while (0);
699
700 if (!trash.len) {
701 /* client shutdown */
702 conn->err_code = CO_ER_CIP_EMPTY;
703 goto fail;
704 }
705
706 /* Fail if buffer length is not large enough to contain
707 * CIP magic, CIP length */
708 if (trash.len < 8)
709 goto missing;
710
711 line = trash.str;
712
713 cip_magic = ntohl(*(uint32_t *)line);
714 cip_len = ntohl(*(uint32_t *)(line+4));
715
716 /* Decode a possible NetScaler Client IP request, fail early if
717 * it does not match */
718 if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic)
719 goto bad_magic;
720
721 /* Fail if buffer length is not large enough to contain
722 * CIP magic, CIP length, minimal IP header */
723 if (trash.len < 28)
724 goto missing;
725
726 line += 8;
727
728 /* Get IP version from the first four bits */
729 ip_v = (*line & 0xf0) >> 4;
730
731 if (ip_v == 4) {
732 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100733 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100734
735 hdr_ip4 = (struct ip *)line;
736
737 if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) {
738 /* Fail if buffer length is not large enough to contain
739 * CIP magic, CIP length, IPv4 header */
740 goto missing;
741 } else if (hdr_ip4->ip_p != IPPROTO_TCP) {
742 /* The protocol does not include a TCP header */
743 conn->err_code = CO_ER_CIP_BAD_PROTO;
744 goto fail;
745 } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) {
746 /* Fail if buffer length is not large enough to contain
747 * CIP magic, CIP length, IPv4 header, TCP header */
748 goto missing;
749 }
750
David Carlier3015a2e2016-07-04 22:51:33 +0100751 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100752
753 /* update the session's addresses and mark them set */
754 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
755 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
756 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source;
757
758 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
759 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
760 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest;
761
762 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
763 }
764 else if (ip_v == 6) {
765 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100766 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100767
768 hdr_ip6 = (struct ip6_hdr *)line;
769
770 if (trash.len < 28) {
771 /* Fail if buffer length is not large enough to contain
772 * CIP magic, CIP length, IPv6 header */
773 goto missing;
774 } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
775 /* The protocol does not include a TCP header */
776 conn->err_code = CO_ER_CIP_BAD_PROTO;
777 goto fail;
778 } else if (trash.len < 48) {
779 /* Fail if buffer length is not large enough to contain
780 * CIP magic, CIP length, IPv6 header, TCP header */
781 goto missing;
782 }
783
David Carlier3015a2e2016-07-04 22:51:33 +0100784 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100785
786 /* update the session's addresses and mark them set */
787 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
788 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src;
789 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source;
790
791 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
792 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst;
793 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest;
794
795 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
796 }
797 else {
798 /* The protocol does not match something known (IPv4/IPv6) */
799 conn->err_code = CO_ER_CIP_BAD_PROTO;
800 goto fail;
801 }
802
803 line += cip_len;
804 trash.len = line - trash.str;
805
806 /* remove the NetScaler Client IP header from the request. For this
807 * we re-read the exact line at once. If we don't get the exact same
808 * result, we fail.
809 */
810 do {
811 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
812 if (len2 < 0 && errno == EINTR)
813 continue;
814 if (len2 != trash.len)
815 goto recv_abort;
816 } while (0);
817
818 conn->flags &= ~flag;
819 return 1;
820
821 missing:
822 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
823 * we have not read anything. Otherwise we need to fail because we won't
824 * be able to poll anymore.
825 */
826 conn->err_code = CO_ER_CIP_TRUNCATED;
827 goto fail;
828
829 bad_magic:
830 conn->err_code = CO_ER_CIP_BAD_MAGIC;
831 goto fail;
832
833 recv_abort:
834 conn->err_code = CO_ER_CIP_ABORT;
835 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
836 goto fail;
837
838 fail:
839 __conn_sock_stop_both(conn);
840 conn->flags |= CO_FL_ERROR;
841 return 0;
842}
843
David Safb76832014-05-08 23:42:08 -0400844int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
845{
846 int ret = 0;
847
848 if (srv && (srv->pp_opts & SRV_PP_V2)) {
849 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
850 }
851 else {
852 if (remote)
853 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
854 else
855 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
856 }
857
858 return ret;
859}
860
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200861/* Makes a PROXY protocol line from the two addresses. The output is sent to
862 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
863 * It returns the number of bytes composing this line (including the trailing
864 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200865 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
866 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200867 */
David Safb76832014-05-08 23:42:08 -0400868int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200869{
870 int ret = 0;
871
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200872 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200873 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
874 if (ret >= buf_len)
875 return 0;
876
877 /* IPv4 src */
878 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
879 return 0;
880
881 ret += strlen(buf + ret);
882 if (ret >= buf_len)
883 return 0;
884
885 buf[ret++] = ' ';
886
887 /* IPv4 dst */
888 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
889 return 0;
890
891 ret += strlen(buf + ret);
892 if (ret >= buf_len)
893 return 0;
894
895 /* source and destination ports */
896 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
897 ntohs(((struct sockaddr_in *)src)->sin_port),
898 ntohs(((struct sockaddr_in *)dst)->sin_port));
899 if (ret >= buf_len)
900 return 0;
901 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200902 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200903 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
904 if (ret >= buf_len)
905 return 0;
906
907 /* IPv6 src */
908 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
909 return 0;
910
911 ret += strlen(buf + ret);
912 if (ret >= buf_len)
913 return 0;
914
915 buf[ret++] = ' ';
916
917 /* IPv6 dst */
918 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
919 return 0;
920
921 ret += strlen(buf + ret);
922 if (ret >= buf_len)
923 return 0;
924
925 /* source and destination ports */
926 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
927 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
928 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
929 if (ret >= buf_len)
930 return 0;
931 }
932 else {
933 /* unknown family combination */
934 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
935 if (ret >= buf_len)
936 return 0;
937 }
938 return ret;
939}
David Safb76832014-05-08 23:42:08 -0400940
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100941#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
942static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400943{
944 struct tlv *tlv;
945
946 if (!dest || (length + sizeof(*tlv) > dest_len))
947 return 0;
948
949 tlv = (struct tlv *)dest;
950
951 tlv->type = type;
952 tlv->length_hi = length >> 8;
953 tlv->length_lo = length & 0x00ff;
954 memcpy(tlv->value, value, length);
955 return length + sizeof(*tlv);
956}
957#endif
958
959int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
960{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200961 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400962 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200963 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +0200964 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -0400965 struct sockaddr_storage *src = &null_addr;
966 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100967
David Safb76832014-05-08 23:42:08 -0400968#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400969 char *value = NULL;
970 struct tlv_ssl *tlv;
971 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400972 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400973#endif
974
975 if (buf_len < PP2_HEADER_LEN)
976 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200977 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400978
979 if (remote) {
980 src = &remote->addr.from;
981 dst = &remote->addr.to;
982 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100983
David Safb76832014-05-08 23:42:08 -0400984 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
985 if (buf_len < PP2_HDR_LEN_INET)
986 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200987 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
988 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
989 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
990 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
991 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
992 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -0400993 ret = PP2_HDR_LEN_INET;
994 }
995 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
996 if (buf_len < PP2_HDR_LEN_INET6)
997 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200998 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
999 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1000 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1001 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1002 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1003 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -04001004 ret = PP2_HDR_LEN_INET6;
1005 }
1006 else {
1007 if (buf_len < PP2_HDR_LEN_UNSPEC)
1008 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001009 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1010 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001011 ret = PP2_HDR_LEN_UNSPEC;
1012 }
1013
1014#ifdef USE_OPENSSL
1015 if (srv->pp_opts & SRV_PP_V2_SSL) {
1016 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1017 return 0;
1018 tlv = (struct tlv_ssl *)&buf[ret];
1019 memset(tlv, 0, sizeof(struct tlv_ssl));
1020 ssl_tlv_len += sizeof(struct tlv_ssl);
1021 tlv->tlv.type = PP2_TYPE_SSL;
1022 if (ssl_sock_is_ssl(remote)) {
1023 tlv->client |= PP2_CLIENT_SSL;
1024 value = ssl_sock_get_version(remote);
1025 if (value) {
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_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001027 }
Dave McCowan328fb582014-07-30 10:39:13 -04001028 if (ssl_sock_get_cert_used_sess(remote)) {
1029 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001030 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001031 if (ssl_sock_get_cert_used_conn(remote))
1032 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001033 }
1034 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -04001035 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001036 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001037 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 -04001038 }
1039 }
1040 }
1041 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1042 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1043 ret += ssl_tlv_len;
1044 }
1045#endif
1046
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001047#ifdef CONFIG_HAP_NS
1048 if (remote && (remote->proxy_netns)) {
1049 if ((buf_len - ret) < sizeof(struct tlv))
1050 return 0;
1051 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
1052 }
1053#endif
1054
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001055 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001056
1057 return ret;
1058}
Emeric Brun4f603012017-01-05 15:11:44 +01001059
1060/* fetch if the received connection used a PROXY protocol header */
1061int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1062{
1063 struct connection *conn;
1064
1065 conn = objt_conn(smp->sess->origin);
1066 if (!conn)
1067 return 0;
1068
1069 if (!(conn->flags & CO_FL_CONNECTED)) {
1070 smp->flags |= SMP_F_MAY_CHANGE;
1071 return 0;
1072 }
1073
1074 smp->flags = 0;
1075 smp->data.type = SMP_T_BOOL;
1076 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1077
1078 return 1;
1079}
1080
1081/* Note: must not be declared <const> as its list will be overwritten.
1082 * Note: fetches that may return multiple types must be declared as the lowest
1083 * common denominator, the type that can be casted into all other ones. For
1084 * instance v4/v6 must be declared v4.
1085 */
1086static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1087 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
1088 { /* END */ },
1089}};
1090
1091
1092__attribute__((constructor))
1093static void __connection_init(void)
1094{
1095 sample_register_fetches(&sample_fetch_keywords);
1096}