blob: c21c98bae95a6ffd12e6f41f84eb9311d0ec4b9b [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 Tarreau2542b532012-08-31 16:01:23 +020023#include <proto/session.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020024#include <proto/stream_interface.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;
31
32/* perform minimal intializations, report 0 in case of error, 1 if OK. */
33int init_connection()
34{
35 pool2_connection = create_pool("connection", sizeof (struct connection), MEM_F_SHARED);
36 return pool2_connection != NULL;
37}
38
Willy Tarreau59f98392012-07-06 14:13:49 +020039/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020040 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020041 */
42int conn_fd_handler(int fd)
43{
Willy Tarreau80184712012-07-06 14:54:49 +020044 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020045 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020046
Willy Tarreauc76ae332012-07-12 15:32:13 +020047 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020048 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020049
Willy Tarreau7d281492012-12-16 19:19:13 +010050 conn_refresh_polling_flags(conn);
51 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010052
Willy Tarreauc76ae332012-07-12 15:32:13 +020053 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020054 /* The handshake callbacks are called in sequence. If either of them is
55 * missing something, it must enable the required polling at the socket
56 * layer of the connection. Polling state is not guaranteed when entering
57 * these handlers, so any handshake handler which does not complete its
Willy Tarreaud6e999b2013-11-25 08:41:15 +010058 * work must explicitly disable events it's not interested in. Error
59 * handling is also performed here in order to reduce the number of tests
60 * around.
Willy Tarreauf9dabec2012-08-17 17:33:53 +020061 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +010062 while (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) {
Willy Tarreau310987a2014-01-22 19:46:33 +010063 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020064 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020065
Willy Tarreau22cda212012-08-31 17:43:29 +020066 if (conn->flags & CO_FL_ACCEPT_PROXY)
67 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
68 goto leave;
69
Willy Tarreau57cd3e42013-10-24 22:01:26 +020070 if (conn->flags & CO_FL_SEND_PROXY)
71 if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY))
Willy Tarreau5f1504f2012-10-04 23:55:57 +020072 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020073#ifdef USE_OPENSSL
74 if (conn->flags & CO_FL_SSL_WAIT_HS)
75 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
76 goto leave;
77#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020078 }
79
Willy Tarreauf9dabec2012-08-17 17:33:53 +020080 /* Once we're purely in the data phase, we disable handshake polling */
81 if (!(conn->flags & CO_FL_POLL_SOCK))
82 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020083
Willy Tarreau071e1372012-10-03 01:39:48 +020084 /* The data layer might not be ready yet (eg: when using embryonic
85 * sessions). If we're about to move data, we must initialize it first.
86 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020087 * we must not use it anymore and should immediately leave instead.
88 */
Willy Tarreau071e1372012-10-03 01:39:48 +020089 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020090 return 0;
91
Willy Tarreau153c3ca2012-10-22 22:47:55 +020092 /* The data transfer starts here and stops on error and handshakes. Note
93 * that we must absolutely test conn->xprt at each step in case it suddenly
94 * changes due to a quick unexpected close().
95 */
Willy Tarreaud8375892014-01-21 11:01:08 +010096 if (conn->xprt && fd_recv_ready(fd) &&
97 ((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 +020098 /* force detection of a flag change : it's impossible to have both
99 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200100 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200101 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200102 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200103 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200104
Willy Tarreaud8375892014-01-21 11:01:08 +0100105 if (conn->xprt && fd_send_ready(fd) &&
106 ((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 +0200107 /* force detection of a flag change : it's impossible to have both
108 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200109 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200110 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200111 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200112 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200113
Willy Tarreauc76ae332012-07-12 15:32:13 +0200114 /* It may happen during the data phase that a handshake is
115 * enabled again (eg: SSL)
116 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100117 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200118 goto process_handshake;
119
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100120 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200121 /* still waiting for a connection to establish and nothing was
122 * attempted yet to probe the connection. Then let's retry the
123 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200124 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200125 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200126 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200127 }
128
Willy Tarreau2c6be842012-07-06 17:12:34 +0200129 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200130 /* The wake callback may be used to process a critical error and abort the
131 * connection. If so, we don't want to go further as the connection will
132 * have been released and the FD destroyed.
133 */
134 if ((conn->flags & CO_FL_WAKE_DATA) &&
135 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
136 conn->data->wake(conn) < 0)
137 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200138
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200139 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200140 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200141 conn->flags |= CO_FL_CONNECTED;
142
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200143 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100144 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200145
146 /* commit polling changes */
147 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200148 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200149}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200150
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200151/* Update polling on connection <c>'s file descriptor depending on its current
152 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
153 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
154 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200155 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200156 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200157void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200158{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200159 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200160
Willy Tarreau3c728722014-01-23 13:50:42 +0100161 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200162 return;
163
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100165 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 +0100166 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100167 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200168 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100169 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
170 fd_stop_recv(c->t.sock.fd);
171 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200172 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200173
174 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100175 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 +0200176 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100177 f |= CO_FL_CURR_WR_ENA;
178 }
179 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
180 fd_stop_send(c->t.sock.fd);
181 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200182 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100183 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200184}
185
186/* Update polling on connection <c>'s file descriptor depending on its current
187 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
188 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
189 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200190 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200191 */
192void conn_update_sock_polling(struct connection *c)
193{
194 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200195
Willy Tarreau3c728722014-01-23 13:50:42 +0100196 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200197 return;
198
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200199 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100200 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 +0100201 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100202 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200203 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100204 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
205 fd_stop_recv(c->t.sock.fd);
206 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200207 }
208
209 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100210 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 +0100211 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100212 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200213 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100214 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
215 fd_stop_send(c->t.sock.fd);
216 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200217 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100218 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200219}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200220
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100221/*
222 * Get data length from tlv
223 */
224static int get_tlv_length(const struct tlv *src)
225{
226 return (src->length_hi << 8) | src->length_lo;
227}
228
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200229/* This handshake handler waits a PROXY protocol header at the beginning of the
230 * raw data stream. The header looks like this :
231 *
232 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
233 *
234 * There must be exactly one space between each field. Fields are :
235 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
236 * - SRC3 : layer 3 (eg: IP) source address in standard text form
237 * - DST3 : layer 3 (eg: IP) destination address in standard text form
238 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
239 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
240 *
241 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
242 *
243 * The header line is small and in all cases smaller than the smallest normal
244 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
245 * can safely use MSG_PEEK and avoid buffering.
246 *
247 * Once the data is fetched, the values are set in the connection's address
248 * fields, and data are removed from the socket's buffer. The function returns
249 * zero if it needs to wait for more data or if it fails, or 1 if it completed
250 * and removed itself.
251 */
252int conn_recv_proxy(struct connection *conn, int flag)
253{
254 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200255 struct proxy_hdr_v2 *hdr_v2;
256 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100257 int tlv_length = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200258
259 /* we might have been called just after an asynchronous shutr */
260 if (conn->flags & CO_FL_SOCK_RD_SH)
261 goto fail;
262
Willy Tarreau3c728722014-01-23 13:50:42 +0100263 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200264 goto fail;
265
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100266 if (!fd_recv_ready(conn->t.sock.fd))
267 return 0;
268
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200269 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100270 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
271 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200272 if (errno == EINTR)
273 continue;
274 if (errno == EAGAIN) {
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100275 fd_cant_recv(conn->t.sock.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200276 return 0;
277 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100278 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200279 }
280 } while (0);
281
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100282 if (!trash.len) {
283 /* client shutdown */
284 conn->err_code = CO_ER_PRX_EMPTY;
285 goto fail;
286 }
287
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100288 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200289 goto missing;
290
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100291 line = trash.str;
292 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200293
294 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200295 if (strncmp(line, "PROXY ", 6) != 0)
296 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200297
298 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200299 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200300 goto missing;
301
302 if (!memcmp(line, "TCP4 ", 5) != 0) {
303 u32 src3, dst3, sport, dport;
304
305 line += 5;
306
307 src3 = inetaddr_host_lim_ret(line, end, &line);
308 if (line == end)
309 goto missing;
310 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100311 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200312
313 dst3 = inetaddr_host_lim_ret(line, end, &line);
314 if (line == end)
315 goto missing;
316 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100317 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200318
319 sport = read_uint((const char **)&line, end);
320 if (line == end)
321 goto missing;
322 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100323 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200324
325 dport = read_uint((const char **)&line, end);
326 if (line > end - 2)
327 goto missing;
328 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100329 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200330 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100331 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200332
333 /* update the session's addresses and mark them set */
334 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
335 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
336 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
337
338 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
339 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
340 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
341 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
342 }
343 else if (!memcmp(line, "TCP6 ", 5) != 0) {
344 u32 sport, dport;
345 char *src_s;
346 char *dst_s, *sport_s, *dport_s;
347 struct in6_addr src3, dst3;
348
349 line += 5;
350
351 src_s = line;
352 dst_s = sport_s = dport_s = NULL;
353 while (1) {
354 if (line > end - 2) {
355 goto missing;
356 }
357 else if (*line == '\r') {
358 *line = 0;
359 line++;
360 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100361 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200362 break;
363 }
364
365 if (*line == ' ') {
366 *line = 0;
367 if (!dst_s)
368 dst_s = line + 1;
369 else if (!sport_s)
370 sport_s = line + 1;
371 else if (!dport_s)
372 dport_s = line + 1;
373 }
374 line++;
375 }
376
377 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100378 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200379
380 sport = read_uint((const char **)&sport_s,dport_s - 1);
381 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100382 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200383
384 dport = read_uint((const char **)&dport_s,line - 2);
385 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100386 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200387
388 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100389 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200390
391 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100392 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200393
394 /* update the session's addresses and mark them set */
395 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
396 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
397 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
398
399 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
400 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
401 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
402 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
403 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200404 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
405 /* This can be a UNIX socket forwarded by an haproxy upstream */
406 line += 9;
407 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200408 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200409 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100410 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200411 goto fail;
412 }
413
Willy Tarreau77992672014-06-14 11:06:17 +0200414 trash.len = line - trash.str;
415 goto eat_header;
416
417 not_v1:
418 /* try PPv2 */
419 if (trash.len < PP2_HEADER_LEN)
420 goto missing;
421
422 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
423
424 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
425 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
426 conn->err_code = CO_ER_PRX_NOT_HDR;
427 goto fail;
428 }
429
430 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
431 goto missing;
432
433 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
434 case 0x01: /* PROXY command */
435 switch (hdr_v2->fam) {
436 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100437 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
438 goto bad_header;
439
Willy Tarreau77992672014-06-14 11:06:17 +0200440 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
441 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
442 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
443 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
444 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
445 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port;
446 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100447 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200448 break;
449 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100450 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
451 goto bad_header;
452
Willy Tarreau77992672014-06-14 11:06:17 +0200453 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
454 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
455 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
456 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
457 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
458 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
459 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100460 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200461 break;
462 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100463
464 /* TLV parsing */
465 if (tlv_length > 0) {
466 int tlv_offset = trash.len - tlv_length;
467
468 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
469 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
470 const int tlv_len = get_tlv_length(tlv_packet);
471 tlv_offset += tlv_len + TLV_HEADER_SIZE;
472
473 switch (tlv_packet->type) {
474#ifdef CONFIG_HAP_NS
475 case PP2_TYPE_NETNS: {
476 const struct netns_entry *ns;
477 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
478 if (ns)
479 conn->proxy_netns = ns;
480 break;
481 }
482#endif
483 default:
484 break;
485 }
486 }
487 }
488
Willy Tarreau77992672014-06-14 11:06:17 +0200489 /* unsupported protocol, keep local connection address */
490 break;
491 case 0x00: /* LOCAL command */
492 /* keep local connection address for LOCAL */
493 break;
494 default:
495 goto bad_header; /* not a supported command */
496 }
497
498 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
499 goto eat_header;
500
501 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200502 /* remove the PROXY line from the request. For this we re-read the
503 * exact line at once. If we don't get the exact same result, we
504 * fail.
505 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200506 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100507 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200508 if (len2 < 0 && errno == EINTR)
509 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100510 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100511 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200512 } while (0);
513
514 conn->flags &= ~flag;
515 return 1;
516
517 missing:
518 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
519 * we have not read anything. Otherwise we need to fail because we won't
520 * be able to poll anymore.
521 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100522 conn->err_code = CO_ER_PRX_TRUNCATED;
523 goto fail;
524
525 bad_header:
526 /* This is not a valid proxy protocol header */
527 conn->err_code = CO_ER_PRX_BAD_HDR;
528 goto fail;
529
530 recv_abort:
531 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100532 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100533 goto fail;
534
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200535 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100536 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200537 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200538 return 0;
539}
540
David Safb76832014-05-08 23:42:08 -0400541int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
542{
543 int ret = 0;
544
545 if (srv && (srv->pp_opts & SRV_PP_V2)) {
546 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
547 }
548 else {
549 if (remote)
550 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
551 else
552 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
553 }
554
555 return ret;
556}
557
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200558/* Makes a PROXY protocol line from the two addresses. The output is sent to
559 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
560 * It returns the number of bytes composing this line (including the trailing
561 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200562 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
563 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200564 */
David Safb76832014-05-08 23:42:08 -0400565int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200566{
567 int ret = 0;
568
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200569 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200570 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
571 if (ret >= buf_len)
572 return 0;
573
574 /* IPv4 src */
575 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
576 return 0;
577
578 ret += strlen(buf + ret);
579 if (ret >= buf_len)
580 return 0;
581
582 buf[ret++] = ' ';
583
584 /* IPv4 dst */
585 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
586 return 0;
587
588 ret += strlen(buf + ret);
589 if (ret >= buf_len)
590 return 0;
591
592 /* source and destination ports */
593 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
594 ntohs(((struct sockaddr_in *)src)->sin_port),
595 ntohs(((struct sockaddr_in *)dst)->sin_port));
596 if (ret >= buf_len)
597 return 0;
598 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200599 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200600 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
601 if (ret >= buf_len)
602 return 0;
603
604 /* IPv6 src */
605 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
606 return 0;
607
608 ret += strlen(buf + ret);
609 if (ret >= buf_len)
610 return 0;
611
612 buf[ret++] = ' ';
613
614 /* IPv6 dst */
615 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
616 return 0;
617
618 ret += strlen(buf + ret);
619 if (ret >= buf_len)
620 return 0;
621
622 /* source and destination ports */
623 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
624 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
625 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
626 if (ret >= buf_len)
627 return 0;
628 }
629 else {
630 /* unknown family combination */
631 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
632 if (ret >= buf_len)
633 return 0;
634 }
635 return ret;
636}
David Safb76832014-05-08 23:42:08 -0400637
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100638#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
639static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400640{
641 struct tlv *tlv;
642
643 if (!dest || (length + sizeof(*tlv) > dest_len))
644 return 0;
645
646 tlv = (struct tlv *)dest;
647
648 tlv->type = type;
649 tlv->length_hi = length >> 8;
650 tlv->length_lo = length & 0x00ff;
651 memcpy(tlv->value, value, length);
652 return length + sizeof(*tlv);
653}
654#endif
655
656int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
657{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200658 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400659 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200660 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
David Safb76832014-05-08 23:42:08 -0400661 struct sockaddr_storage null_addr = {0};
662 struct sockaddr_storage *src = &null_addr;
663 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100664
David Safb76832014-05-08 23:42:08 -0400665#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400666 char *value = NULL;
667 struct tlv_ssl *tlv;
668 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400669 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400670#endif
671
672 if (buf_len < PP2_HEADER_LEN)
673 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200674 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400675
676 if (remote) {
677 src = &remote->addr.from;
678 dst = &remote->addr.to;
679 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100680
David Safb76832014-05-08 23:42:08 -0400681 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
682 if (buf_len < PP2_HDR_LEN_INET)
683 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200684 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
685 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
686 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
687 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
688 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
689 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -0400690 ret = PP2_HDR_LEN_INET;
691 }
692 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
693 if (buf_len < PP2_HDR_LEN_INET6)
694 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200695 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
696 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
697 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
698 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
699 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
700 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -0400701 ret = PP2_HDR_LEN_INET6;
702 }
703 else {
704 if (buf_len < PP2_HDR_LEN_UNSPEC)
705 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200706 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
707 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -0400708 ret = PP2_HDR_LEN_UNSPEC;
709 }
710
711#ifdef USE_OPENSSL
712 if (srv->pp_opts & SRV_PP_V2_SSL) {
713 if ((buf_len - ret) < sizeof(struct tlv_ssl))
714 return 0;
715 tlv = (struct tlv_ssl *)&buf[ret];
716 memset(tlv, 0, sizeof(struct tlv_ssl));
717 ssl_tlv_len += sizeof(struct tlv_ssl);
718 tlv->tlv.type = PP2_TYPE_SSL;
719 if (ssl_sock_is_ssl(remote)) {
720 tlv->client |= PP2_CLIENT_SSL;
721 value = ssl_sock_get_version(remote);
722 if (value) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100723 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 -0400724 }
Dave McCowan328fb582014-07-30 10:39:13 -0400725 if (ssl_sock_get_cert_used_sess(remote)) {
726 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -0400727 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -0400728 if (ssl_sock_get_cert_used_conn(remote))
729 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -0400730 }
731 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -0400732 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +0200733 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100734 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 -0400735 }
736 }
737 }
738 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
739 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
740 ret += ssl_tlv_len;
741 }
742#endif
743
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100744#ifdef CONFIG_HAP_NS
745 if (remote && (remote->proxy_netns)) {
746 if ((buf_len - ret) < sizeof(struct tlv))
747 return 0;
748 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
749 }
750#endif
751
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200752 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -0400753
754 return ret;
755}