blob: 7a9f3913ca74589a8a5f2d0a1c4e135652ce01d1 [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>
Willy Tarreau59f98392012-07-06 14:13:49 +020024
Emeric Brun46591952012-05-18 15:47:34 +020025#ifdef USE_OPENSSL
26#include <proto/ssl_sock.h>
27#endif
28
Willy Tarreauf2943dc2012-10-26 20:10:28 +020029struct pool_head *pool2_connection;
30
31/* perform minimal intializations, report 0 in case of error, 1 if OK. */
32int init_connection()
33{
34 pool2_connection = create_pool("connection", sizeof (struct connection), MEM_F_SHARED);
35 return pool2_connection != NULL;
36}
37
Willy Tarreau59f98392012-07-06 14:13:49 +020038/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreau7a798e52016-04-14 11:13:20 +020039 * provided by the connection's sock_ops, which must be valid.
Willy Tarreau59f98392012-07-06 14:13:49 +020040 */
Willy Tarreau7a798e52016-04-14 11:13:20 +020041void conn_fd_handler(int fd)
Willy Tarreau59f98392012-07-06 14:13:49 +020042{
Willy Tarreau80184712012-07-06 14:54:49 +020043 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020044 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020045
Willy Tarreauc76ae332012-07-12 15:32:13 +020046 if (unlikely(!conn))
Willy Tarreau7a798e52016-04-14 11:13:20 +020047 return;
Willy Tarreau59f98392012-07-06 14:13:49 +020048
Willy Tarreau7d281492012-12-16 19:19:13 +010049 conn_refresh_polling_flags(conn);
50 flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
Willy Tarreaud29a0662012-12-10 16:33:38 +010051
Willy Tarreauc76ae332012-07-12 15:32:13 +020052 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020053 /* The handshake callbacks are called in sequence. If either of them is
54 * missing something, it must enable the required polling at the socket
55 * layer of the connection. Polling state is not guaranteed when entering
56 * these handlers, so any handshake handler which does not complete its
Willy Tarreaud6e999b2013-11-25 08:41:15 +010057 * work must explicitly disable events it's not interested in. Error
58 * handling is also performed here in order to reduce the number of tests
59 * around.
Willy Tarreauf9dabec2012-08-17 17:33:53 +020060 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +010061 while (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) {
Willy Tarreau310987a2014-01-22 19:46:33 +010062 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020063 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020064
Bertrand Jacquin93b227d2016-06-04 15:11:10 +010065 if (conn->flags & CO_FL_ACCEPT_CIP)
66 if (!conn_recv_netscaler_cip(conn, CO_FL_ACCEPT_CIP))
67 goto leave;
68
Willy Tarreau22cda212012-08-31 17:43:29 +020069 if (conn->flags & CO_FL_ACCEPT_PROXY)
70 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
71 goto leave;
72
Willy Tarreau57cd3e42013-10-24 22:01:26 +020073 if (conn->flags & CO_FL_SEND_PROXY)
74 if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY))
Willy Tarreau5f1504f2012-10-04 23:55:57 +020075 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020076#ifdef USE_OPENSSL
77 if (conn->flags & CO_FL_SSL_WAIT_HS)
78 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
79 goto leave;
80#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020081 }
82
Willy Tarreauf9dabec2012-08-17 17:33:53 +020083 /* Once we're purely in the data phase, we disable handshake polling */
84 if (!(conn->flags & CO_FL_POLL_SOCK))
85 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020086
Willy Tarreau071e1372012-10-03 01:39:48 +020087 /* The data layer might not be ready yet (eg: when using embryonic
88 * sessions). If we're about to move data, we must initialize it first.
89 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020090 * we must not use it anymore and should immediately leave instead.
91 */
Willy Tarreau071e1372012-10-03 01:39:48 +020092 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +020093 return;
Willy Tarreau2542b532012-08-31 16:01:23 +020094
Willy Tarreau153c3ca2012-10-22 22:47:55 +020095 /* The data transfer starts here and stops on error and handshakes. Note
96 * that we must absolutely test conn->xprt at each step in case it suddenly
97 * changes due to a quick unexpected close().
98 */
Willy Tarreaud8375892014-01-21 11:01:08 +010099 if (conn->xprt && fd_recv_ready(fd) &&
100 ((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 +0200101 /* force detection of a flag change : it's impossible to have both
102 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200103 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200104 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200105 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200106 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200107
Willy Tarreaud8375892014-01-21 11:01:08 +0100108 if (conn->xprt && fd_send_ready(fd) &&
109 ((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 +0200110 /* force detection of a flag change : it's impossible to have both
111 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200112 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200113 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200114 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200115 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200116
Willy Tarreauc76ae332012-07-12 15:32:13 +0200117 /* It may happen during the data phase that a handshake is
118 * enabled again (eg: SSL)
119 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100120 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200121 goto process_handshake;
122
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100123 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200124 /* still waiting for a connection to establish and nothing was
125 * attempted yet to probe the connection. Then let's retry the
126 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200127 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200128 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200129 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200130 }
131
Willy Tarreau2c6be842012-07-06 17:12:34 +0200132 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200133 /* The wake callback may be used to process a critical error and abort the
134 * connection. If so, we don't want to go further as the connection will
135 * have been released and the FD destroyed.
136 */
137 if ((conn->flags & CO_FL_WAKE_DATA) &&
138 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
139 conn->data->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200140 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200141
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200142 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200143 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200144 conn->flags |= CO_FL_CONNECTED;
145
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200146 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100147 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200148
149 /* commit polling changes */
150 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200151 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200152}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200153
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200154/* Update polling on connection <c>'s file descriptor depending on its current
155 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
156 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
157 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200158 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200160void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200162 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200163
Willy Tarreau3c728722014-01-23 13:50:42 +0100164 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200165 return;
166
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200167 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100168 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 +0100169 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100170 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200171 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100172 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
173 fd_stop_recv(c->t.sock.fd);
174 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200175 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200176
177 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100178 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 +0200179 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100180 f |= CO_FL_CURR_WR_ENA;
181 }
182 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
183 fd_stop_send(c->t.sock.fd);
184 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200185 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100186 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200187}
188
189/* Update polling on connection <c>'s file descriptor depending on its current
190 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
191 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
192 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200193 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200194 */
195void conn_update_sock_polling(struct connection *c)
196{
197 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200198
Willy Tarreau3c728722014-01-23 13:50:42 +0100199 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200200 return;
201
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200202 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100203 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 +0100204 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100205 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200206 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100207 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
208 fd_stop_recv(c->t.sock.fd);
209 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200210 }
211
212 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100213 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 +0100214 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100215 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200216 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100217 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
218 fd_stop_send(c->t.sock.fd);
219 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200220 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100221 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200222}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200223
Willy Tarreauff3e6482015-03-12 23:56:52 +0100224/* Send a message over an established connection. It makes use of send() and
225 * returns the same return code and errno. If the socket layer is not ready yet
226 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
227 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
228 * EMSGSIZE if called with a zero length message. The purpose is to simplify
229 * some rare attempts to directly write on the socket from above the connection
230 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
231 * It automatically retries on EINTR. Other errors cause the connection to be
232 * marked as in error state. It takes similar arguments as send() except the
233 * first one which is the connection instead of the file descriptor. Note,
234 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
235 */
236int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
237{
238 int ret;
239
240 ret = -1;
241 errno = ENOTSOCK;
242
243 if (conn->flags & CO_FL_SOCK_WR_SH)
244 goto fail;
245
246 if (!conn_ctrl_ready(conn))
247 goto fail;
248
249 errno = EMSGSIZE;
250 if (!len)
251 goto fail;
252
253 if (!fd_send_ready(conn->t.sock.fd))
254 goto wait;
255
256 do {
257 ret = send(conn->t.sock.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
258 } while (ret < 0 && errno == EINTR);
259
260
261 if (ret > 0)
262 return ret;
263
264 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
265 wait:
266 fd_cant_send(conn->t.sock.fd);
267 return 0;
268 }
269 fail:
270 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
271 return ret;
272}
273
Willy Tarreaud85c4852015-03-13 00:40:28 +0100274/* Drains possibly pending incoming data on the file descriptor attached to the
275 * connection and update the connection's flags accordingly. This is used to
276 * know whether we need to disable lingering on close. Returns non-zero if it
277 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
278 * flag may also be updated if the incoming shutdown was reported by the drain()
279 * function.
280 */
281int conn_sock_drain(struct connection *conn)
282{
283 if (!conn_ctrl_ready(conn))
284 return 1;
285
286 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
287 return 1;
288
289 if (fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) {
290 fdtab[conn->t.sock.fd].linger_risk = 0;
291 }
292 else {
293 if (!fd_recv_ready(conn->t.sock.fd))
294 return 0;
295
296 /* disable draining if we were called and have no drain function */
297 if (!conn->ctrl->drain) {
298 __conn_data_stop_recv(conn);
299 return 0;
300 }
301
302 if (conn->ctrl->drain(conn->t.sock.fd) <= 0)
303 return 0;
304 }
305
306 conn->flags |= CO_FL_SOCK_RD_SH;
307 return 1;
308}
309
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100310/*
311 * Get data length from tlv
312 */
313static int get_tlv_length(const struct tlv *src)
314{
315 return (src->length_hi << 8) | src->length_lo;
316}
317
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200318/* This handshake handler waits a PROXY protocol header at the beginning of the
319 * raw data stream. The header looks like this :
320 *
321 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
322 *
323 * There must be exactly one space between each field. Fields are :
324 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
325 * - SRC3 : layer 3 (eg: IP) source address in standard text form
326 * - DST3 : layer 3 (eg: IP) destination address in standard text form
327 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
328 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
329 *
330 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
331 *
332 * The header line is small and in all cases smaller than the smallest normal
333 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
334 * can safely use MSG_PEEK and avoid buffering.
335 *
336 * Once the data is fetched, the values are set in the connection's address
337 * fields, and data are removed from the socket's buffer. The function returns
338 * zero if it needs to wait for more data or if it fails, or 1 if it completed
339 * and removed itself.
340 */
341int conn_recv_proxy(struct connection *conn, int flag)
342{
343 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200344 struct proxy_hdr_v2 *hdr_v2;
345 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100346 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200347 int tlv_offset = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200348
349 /* we might have been called just after an asynchronous shutr */
350 if (conn->flags & CO_FL_SOCK_RD_SH)
351 goto fail;
352
Willy Tarreau3c728722014-01-23 13:50:42 +0100353 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200354 goto fail;
355
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100356 if (!fd_recv_ready(conn->t.sock.fd))
357 return 0;
358
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200359 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100360 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
361 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200362 if (errno == EINTR)
363 continue;
364 if (errno == EAGAIN) {
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100365 fd_cant_recv(conn->t.sock.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200366 return 0;
367 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100368 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200369 }
370 } while (0);
371
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100372 if (!trash.len) {
373 /* client shutdown */
374 conn->err_code = CO_ER_PRX_EMPTY;
375 goto fail;
376 }
377
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100378 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200379 goto missing;
380
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100381 line = trash.str;
382 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200383
384 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200385 if (strncmp(line, "PROXY ", 6) != 0)
386 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200387
388 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200389 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200390 goto missing;
391
David CARLIER42ff05e2016-03-24 09:22:36 +0000392 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200393 u32 src3, dst3, sport, dport;
394
395 line += 5;
396
397 src3 = inetaddr_host_lim_ret(line, end, &line);
398 if (line == end)
399 goto missing;
400 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100401 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200402
403 dst3 = inetaddr_host_lim_ret(line, end, &line);
404 if (line == end)
405 goto missing;
406 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100407 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200408
409 sport = read_uint((const char **)&line, end);
410 if (line == end)
411 goto missing;
412 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100413 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200414
415 dport = read_uint((const char **)&line, end);
416 if (line > end - 2)
417 goto missing;
418 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100419 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200420 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100421 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200422
423 /* update the session's addresses and mark them set */
424 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
425 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
426 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
427
428 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
429 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
430 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
431 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
432 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000433 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200434 u32 sport, dport;
435 char *src_s;
436 char *dst_s, *sport_s, *dport_s;
437 struct in6_addr src3, dst3;
438
439 line += 5;
440
441 src_s = line;
442 dst_s = sport_s = dport_s = NULL;
443 while (1) {
444 if (line > end - 2) {
445 goto missing;
446 }
447 else if (*line == '\r') {
448 *line = 0;
449 line++;
450 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100451 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200452 break;
453 }
454
455 if (*line == ' ') {
456 *line = 0;
457 if (!dst_s)
458 dst_s = line + 1;
459 else if (!sport_s)
460 sport_s = line + 1;
461 else if (!dport_s)
462 dport_s = line + 1;
463 }
464 line++;
465 }
466
467 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100468 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200469
470 sport = read_uint((const char **)&sport_s,dport_s - 1);
471 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100472 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200473
474 dport = read_uint((const char **)&dport_s,line - 2);
475 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100476 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200477
478 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100479 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200480
481 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100482 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200483
484 /* update the session's addresses and mark them set */
485 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
486 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
487 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
488
489 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
490 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
491 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
492 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
493 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200494 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
495 /* This can be a UNIX socket forwarded by an haproxy upstream */
496 line += 9;
497 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200498 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200499 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100500 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200501 goto fail;
502 }
503
Willy Tarreau77992672014-06-14 11:06:17 +0200504 trash.len = line - trash.str;
505 goto eat_header;
506
507 not_v1:
508 /* try PPv2 */
509 if (trash.len < PP2_HEADER_LEN)
510 goto missing;
511
512 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
513
514 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
515 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
516 conn->err_code = CO_ER_PRX_NOT_HDR;
517 goto fail;
518 }
519
520 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
521 goto missing;
522
523 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
524 case 0x01: /* PROXY command */
525 switch (hdr_v2->fam) {
526 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100527 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
528 goto bad_header;
529
Willy Tarreau77992672014-06-14 11:06:17 +0200530 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
531 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
532 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
533 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
534 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
535 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port;
536 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200537 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100538 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200539 break;
540 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100541 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
542 goto bad_header;
543
Willy Tarreau77992672014-06-14 11:06:17 +0200544 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
545 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
546 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
547 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
548 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
549 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
550 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200551 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100552 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200553 break;
554 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100555
556 /* TLV parsing */
557 if (tlv_length > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100558 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
559 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
560 const int tlv_len = get_tlv_length(tlv_packet);
561 tlv_offset += tlv_len + TLV_HEADER_SIZE;
562
563 switch (tlv_packet->type) {
564#ifdef CONFIG_HAP_NS
565 case PP2_TYPE_NETNS: {
566 const struct netns_entry *ns;
567 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
568 if (ns)
569 conn->proxy_netns = ns;
570 break;
571 }
572#endif
573 default:
574 break;
575 }
576 }
577 }
578
Willy Tarreau77992672014-06-14 11:06:17 +0200579 /* unsupported protocol, keep local connection address */
580 break;
581 case 0x00: /* LOCAL command */
582 /* keep local connection address for LOCAL */
583 break;
584 default:
585 goto bad_header; /* not a supported command */
586 }
587
588 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
589 goto eat_header;
590
591 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200592 /* remove the PROXY line from the request. For this we re-read the
593 * exact line at once. If we don't get the exact same result, we
594 * fail.
595 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200596 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100597 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200598 if (len2 < 0 && errno == EINTR)
599 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100600 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100601 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200602 } while (0);
603
604 conn->flags &= ~flag;
605 return 1;
606
607 missing:
608 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
609 * we have not read anything. Otherwise we need to fail because we won't
610 * be able to poll anymore.
611 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100612 conn->err_code = CO_ER_PRX_TRUNCATED;
613 goto fail;
614
615 bad_header:
616 /* This is not a valid proxy protocol header */
617 conn->err_code = CO_ER_PRX_BAD_HDR;
618 goto fail;
619
620 recv_abort:
621 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100622 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100623 goto fail;
624
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200625 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100626 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200627 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200628 return 0;
629}
630
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100631/* This handshake handler waits a NetScaler Client IP insertion header
632 * at the beginning of the raw data stream. The header looks like this:
633 *
634 * 4 bytes: CIP magic number
635 * 4 bytes: Header length
636 * 20+ bytes: Header of the last IP packet sent by the client during
637 * TCP handshake.
638 * 20+ bytes: Header of the last TCP packet sent by the client during
639 * TCP handshake.
640 *
641 * This line MUST be at the beginning of the buffer and MUST NOT be
642 * fragmented.
643 *
644 * The header line is small and in all cases smaller than the smallest normal
645 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
646 * can safely use MSG_PEEK and avoid buffering.
647 *
648 * Once the data is fetched, the values are set in the connection's address
649 * fields, and data are removed from the socket's buffer. The function returns
650 * zero if it needs to wait for more data or if it fails, or 1 if it completed
651 * and removed itself.
652 */
653int conn_recv_netscaler_cip(struct connection *conn, int flag)
654{
655 char *line;
656 uint32_t cip_magic;
657 uint32_t cip_len;
658 uint8_t ip_v;
659
660 /* we might have been called just after an asynchronous shutr */
661 if (conn->flags & CO_FL_SOCK_RD_SH)
662 goto fail;
663
664 if (!conn_ctrl_ready(conn))
665 goto fail;
666
667 if (!fd_recv_ready(conn->t.sock.fd))
668 return 0;
669
670 do {
671 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
672 if (trash.len < 0) {
673 if (errno == EINTR)
674 continue;
675 if (errno == EAGAIN) {
676 fd_cant_recv(conn->t.sock.fd);
677 return 0;
678 }
679 goto recv_abort;
680 }
681 } while (0);
682
683 if (!trash.len) {
684 /* client shutdown */
685 conn->err_code = CO_ER_CIP_EMPTY;
686 goto fail;
687 }
688
689 /* Fail if buffer length is not large enough to contain
690 * CIP magic, CIP length */
691 if (trash.len < 8)
692 goto missing;
693
694 line = trash.str;
695
696 cip_magic = ntohl(*(uint32_t *)line);
697 cip_len = ntohl(*(uint32_t *)(line+4));
698
699 /* Decode a possible NetScaler Client IP request, fail early if
700 * it does not match */
701 if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic)
702 goto bad_magic;
703
704 /* Fail if buffer length is not large enough to contain
705 * CIP magic, CIP length, minimal IP header */
706 if (trash.len < 28)
707 goto missing;
708
709 line += 8;
710
711 /* Get IP version from the first four bits */
712 ip_v = (*line & 0xf0) >> 4;
713
714 if (ip_v == 4) {
715 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100716 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100717
718 hdr_ip4 = (struct ip *)line;
719
720 if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) {
721 /* Fail if buffer length is not large enough to contain
722 * CIP magic, CIP length, IPv4 header */
723 goto missing;
724 } else if (hdr_ip4->ip_p != IPPROTO_TCP) {
725 /* The protocol does not include a TCP header */
726 conn->err_code = CO_ER_CIP_BAD_PROTO;
727 goto fail;
728 } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) {
729 /* Fail if buffer length is not large enough to contain
730 * CIP magic, CIP length, IPv4 header, TCP header */
731 goto missing;
732 }
733
David Carlier3015a2e2016-07-04 22:51:33 +0100734 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100735
736 /* update the session's addresses and mark them set */
737 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
738 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
739 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source;
740
741 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
742 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
743 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest;
744
745 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
746 }
747 else if (ip_v == 6) {
748 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100749 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100750
751 hdr_ip6 = (struct ip6_hdr *)line;
752
753 if (trash.len < 28) {
754 /* Fail if buffer length is not large enough to contain
755 * CIP magic, CIP length, IPv6 header */
756 goto missing;
757 } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
758 /* The protocol does not include a TCP header */
759 conn->err_code = CO_ER_CIP_BAD_PROTO;
760 goto fail;
761 } else if (trash.len < 48) {
762 /* Fail if buffer length is not large enough to contain
763 * CIP magic, CIP length, IPv6 header, TCP header */
764 goto missing;
765 }
766
David Carlier3015a2e2016-07-04 22:51:33 +0100767 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100768
769 /* update the session's addresses and mark them set */
770 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
771 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src;
772 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source;
773
774 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
775 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst;
776 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest;
777
778 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
779 }
780 else {
781 /* The protocol does not match something known (IPv4/IPv6) */
782 conn->err_code = CO_ER_CIP_BAD_PROTO;
783 goto fail;
784 }
785
786 line += cip_len;
787 trash.len = line - trash.str;
788
789 /* remove the NetScaler Client IP header from the request. For this
790 * we re-read the exact line at once. If we don't get the exact same
791 * result, we fail.
792 */
793 do {
794 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
795 if (len2 < 0 && errno == EINTR)
796 continue;
797 if (len2 != trash.len)
798 goto recv_abort;
799 } while (0);
800
801 conn->flags &= ~flag;
802 return 1;
803
804 missing:
805 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
806 * we have not read anything. Otherwise we need to fail because we won't
807 * be able to poll anymore.
808 */
809 conn->err_code = CO_ER_CIP_TRUNCATED;
810 goto fail;
811
812 bad_magic:
813 conn->err_code = CO_ER_CIP_BAD_MAGIC;
814 goto fail;
815
816 recv_abort:
817 conn->err_code = CO_ER_CIP_ABORT;
818 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
819 goto fail;
820
821 fail:
822 __conn_sock_stop_both(conn);
823 conn->flags |= CO_FL_ERROR;
824 return 0;
825}
826
David Safb76832014-05-08 23:42:08 -0400827int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
828{
829 int ret = 0;
830
831 if (srv && (srv->pp_opts & SRV_PP_V2)) {
832 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
833 }
834 else {
835 if (remote)
836 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
837 else
838 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
839 }
840
841 return ret;
842}
843
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200844/* Makes a PROXY protocol line from the two addresses. The output is sent to
845 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
846 * It returns the number of bytes composing this line (including the trailing
847 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200848 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
849 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200850 */
David Safb76832014-05-08 23:42:08 -0400851int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200852{
853 int ret = 0;
854
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200855 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200856 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
857 if (ret >= buf_len)
858 return 0;
859
860 /* IPv4 src */
861 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
862 return 0;
863
864 ret += strlen(buf + ret);
865 if (ret >= buf_len)
866 return 0;
867
868 buf[ret++] = ' ';
869
870 /* IPv4 dst */
871 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
872 return 0;
873
874 ret += strlen(buf + ret);
875 if (ret >= buf_len)
876 return 0;
877
878 /* source and destination ports */
879 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
880 ntohs(((struct sockaddr_in *)src)->sin_port),
881 ntohs(((struct sockaddr_in *)dst)->sin_port));
882 if (ret >= buf_len)
883 return 0;
884 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200885 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200886 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
887 if (ret >= buf_len)
888 return 0;
889
890 /* IPv6 src */
891 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
892 return 0;
893
894 ret += strlen(buf + ret);
895 if (ret >= buf_len)
896 return 0;
897
898 buf[ret++] = ' ';
899
900 /* IPv6 dst */
901 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
902 return 0;
903
904 ret += strlen(buf + ret);
905 if (ret >= buf_len)
906 return 0;
907
908 /* source and destination ports */
909 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
910 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
911 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
912 if (ret >= buf_len)
913 return 0;
914 }
915 else {
916 /* unknown family combination */
917 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
918 if (ret >= buf_len)
919 return 0;
920 }
921 return ret;
922}
David Safb76832014-05-08 23:42:08 -0400923
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100924#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
925static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400926{
927 struct tlv *tlv;
928
929 if (!dest || (length + sizeof(*tlv) > dest_len))
930 return 0;
931
932 tlv = (struct tlv *)dest;
933
934 tlv->type = type;
935 tlv->length_hi = length >> 8;
936 tlv->length_lo = length & 0x00ff;
937 memcpy(tlv->value, value, length);
938 return length + sizeof(*tlv);
939}
940#endif
941
942int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
943{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200944 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400945 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200946 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +0200947 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -0400948 struct sockaddr_storage *src = &null_addr;
949 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100950
David Safb76832014-05-08 23:42:08 -0400951#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400952 char *value = NULL;
953 struct tlv_ssl *tlv;
954 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400955 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400956#endif
957
958 if (buf_len < PP2_HEADER_LEN)
959 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200960 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400961
962 if (remote) {
963 src = &remote->addr.from;
964 dst = &remote->addr.to;
965 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100966
David Safb76832014-05-08 23:42:08 -0400967 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
968 if (buf_len < PP2_HDR_LEN_INET)
969 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200970 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
971 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
972 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
973 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
974 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
975 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -0400976 ret = PP2_HDR_LEN_INET;
977 }
978 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
979 if (buf_len < PP2_HDR_LEN_INET6)
980 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200981 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
982 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
983 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
984 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
985 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
986 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -0400987 ret = PP2_HDR_LEN_INET6;
988 }
989 else {
990 if (buf_len < PP2_HDR_LEN_UNSPEC)
991 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200992 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
993 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -0400994 ret = PP2_HDR_LEN_UNSPEC;
995 }
996
997#ifdef USE_OPENSSL
998 if (srv->pp_opts & SRV_PP_V2_SSL) {
999 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1000 return 0;
1001 tlv = (struct tlv_ssl *)&buf[ret];
1002 memset(tlv, 0, sizeof(struct tlv_ssl));
1003 ssl_tlv_len += sizeof(struct tlv_ssl);
1004 tlv->tlv.type = PP2_TYPE_SSL;
1005 if (ssl_sock_is_ssl(remote)) {
1006 tlv->client |= PP2_CLIENT_SSL;
1007 value = ssl_sock_get_version(remote);
1008 if (value) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001009 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 -04001010 }
Dave McCowan328fb582014-07-30 10:39:13 -04001011 if (ssl_sock_get_cert_used_sess(remote)) {
1012 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001013 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001014 if (ssl_sock_get_cert_used_conn(remote))
1015 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001016 }
1017 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -04001018 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001019 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001020 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 -04001021 }
1022 }
1023 }
1024 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1025 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1026 ret += ssl_tlv_len;
1027 }
1028#endif
1029
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001030#ifdef CONFIG_HAP_NS
1031 if (remote && (remote->proxy_netns)) {
1032 if ((buf_len - ret) < sizeof(struct tlv))
1033 return 0;
1034 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
1035 }
1036#endif
1037
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001038 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001039
1040 return ret;
1041}