blob: 54bbc0737b62b4e7ee1e1fa7fdc37d4316fd4a12 [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 Tarreau8e3c6ce2017-08-28 15:46:01 +020089 /* The connection owner might want to be notified about an end of
90 * handshake indicating the connection is ready, before we proceed with
91 * any data exchange. The callback may fail and cause the connection to
92 * be destroyed, thus we must not use it anymore and should immediately
93 * leave instead. The caller must immediately unregister itself once
94 * called.
Willy Tarreau2542b532012-08-31 16:01:23 +020095 */
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +020096 if (conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +020097 return;
Willy Tarreau2542b532012-08-31 16:01:23 +020098
Willy Tarreau57ec32f2017-04-11 19:59:33 +020099 if (conn->xprt && fd_send_ready(fd) &&
100 ((conn->flags & (CO_FL_DATA_WR_ENA|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_WR_ENA)) {
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100101 /* force reporting of activity by clearing the previous flags :
102 * we'll have at least ERROR or CONNECTED at the end of an I/O,
103 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200104 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100105 flags = 0;
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200106 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200107 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200108
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200109 /* The data transfer starts here and stops on error and handshakes. Note
110 * that we must absolutely test conn->xprt at each step in case it suddenly
111 * changes due to a quick unexpected close().
112 */
113 if (conn->xprt && fd_recv_ready(fd) &&
114 ((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 +0100115 /* force reporting of activity by clearing the previous flags :
116 * we'll have at least ERROR or CONNECTED at the end of an I/O,
117 * both of which will be detected below.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200118 */
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100119 flags = 0;
Willy Tarreau57ec32f2017-04-11 19:59:33 +0200120 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200121 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200122
Willy Tarreauc76ae332012-07-12 15:32:13 +0200123 /* It may happen during the data phase that a handshake is
124 * enabled again (eg: SSL)
125 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100126 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200127 goto process_handshake;
128
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100129 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200130 /* still waiting for a connection to establish and nothing was
131 * attempted yet to probe the connection. Then let's retry the
132 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200133 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200134 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200135 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200136 }
Willy Tarreau2c6be842012-07-06 17:12:34 +0200137 leave:
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100138 /* Verify if the connection just established. */
Willy Tarreau7bf3fa32017-03-14 20:19:29 +0100139 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
140 conn->flags |= CO_FL_CONNECTED;
141
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200142 /* The connection owner might want to be notified about failures to
143 * complete the handshake. The callback may fail and cause the
144 * connection to be destroyed, thus we must not use it anymore and
145 * should immediately leave instead. The caller must immediately
146 * unregister itself once called.
147 */
148 if (((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) &&
149 conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0)
150 return;
151
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100152 /* The wake callback is normally used to notify the data layer about
153 * data layer activity (successful send/recv), connection establishment,
154 * shutdown and fatal errors. We need to consider the following
155 * situations to wake up the data layer :
156 * - change among the CO_FL_NOTIFY_DATA flags :
157 * {DATA,SOCK}_{RD,WR}_SH, ERROR,
158 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
159 * end of handshake and transition to CONNECTED
160 * - raise of CONNECTED with HANDSHAKE down
161 * - end of HANDSHAKE with CONNECTED set
162 * - regular data layer activity
163 *
164 * Note that the wake callback is allowed to release the connection and
165 * the fd (and return < 0 in this case).
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200166 */
Willy Tarreau9fa1ee62017-03-18 15:39:57 +0100167 if ((((conn->flags ^ flags) & CO_FL_NOTIFY_DATA) ||
Willy Tarreau3c0cc492017-03-19 07:54:28 +0100168 ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED &&
169 (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED)) &&
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200170 conn->data->wake(conn) < 0)
Willy Tarreau7a798e52016-04-14 11:13:20 +0200171 return;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200172
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200173 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100174 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200175
176 /* commit polling changes */
177 conn_cond_update_polling(conn);
Willy Tarreau7a798e52016-04-14 11:13:20 +0200178 return;
Willy Tarreau59f98392012-07-06 14:13:49 +0200179}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200180
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200181/* Update polling on connection <c>'s file descriptor depending on its current
182 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
183 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
184 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200185 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200186 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200187void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200188{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200189 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200190
Willy Tarreau3c728722014-01-23 13:50:42 +0100191 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200192 return;
193
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200194 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100195 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200196 fd_want_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100197 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200198 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100199 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200200 fd_stop_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100201 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200202 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200203
204 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100205 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200206 fd_want_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100207 f |= CO_FL_CURR_WR_ENA;
208 }
209 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200210 fd_stop_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100211 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200212 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100213 c->flags = f;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200214}
215
216/* Update polling on connection <c>'s file descriptor depending on its current
217 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
218 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
219 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200220 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200221 */
222void conn_update_sock_polling(struct connection *c)
223{
224 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200225
Willy Tarreau3c728722014-01-23 13:50:42 +0100226 if (!conn_ctrl_ready(c))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200227 return;
228
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200229 /* update read status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100230 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200231 fd_want_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100232 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200233 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100234 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200235 fd_stop_recv(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100236 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200237 }
238
239 /* update write status if needed */
Willy Tarreau310987a2014-01-22 19:46:33 +0100240 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200241 fd_want_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100242 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200243 }
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100244 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200245 fd_stop_send(c->handle.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100246 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200247 }
Willy Tarreau310987a2014-01-22 19:46:33 +0100248 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200249}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200250
Willy Tarreauff3e6482015-03-12 23:56:52 +0100251/* Send a message over an established connection. It makes use of send() and
252 * returns the same return code and errno. If the socket layer is not ready yet
253 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
254 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
255 * EMSGSIZE if called with a zero length message. The purpose is to simplify
256 * some rare attempts to directly write on the socket from above the connection
257 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
258 * It automatically retries on EINTR. Other errors cause the connection to be
259 * marked as in error state. It takes similar arguments as send() except the
260 * first one which is the connection instead of the file descriptor. Note,
261 * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags.
262 */
263int conn_sock_send(struct connection *conn, const void *buf, int len, int flags)
264{
265 int ret;
266
267 ret = -1;
268 errno = ENOTSOCK;
269
270 if (conn->flags & CO_FL_SOCK_WR_SH)
271 goto fail;
272
273 if (!conn_ctrl_ready(conn))
274 goto fail;
275
276 errno = EMSGSIZE;
277 if (!len)
278 goto fail;
279
Willy Tarreau585744b2017-08-24 14:31:19 +0200280 if (!fd_send_ready(conn->handle.fd))
Willy Tarreauff3e6482015-03-12 23:56:52 +0100281 goto wait;
282
283 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200284 ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100285 } while (ret < 0 && errno == EINTR);
286
287
288 if (ret > 0)
289 return ret;
290
291 if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
292 wait:
Willy Tarreau585744b2017-08-24 14:31:19 +0200293 fd_cant_send(conn->handle.fd);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100294 return 0;
295 }
296 fail:
297 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
298 return ret;
299}
300
Willy Tarreaud85c4852015-03-13 00:40:28 +0100301/* Drains possibly pending incoming data on the file descriptor attached to the
302 * connection and update the connection's flags accordingly. This is used to
303 * know whether we need to disable lingering on close. Returns non-zero if it
304 * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH
305 * flag may also be updated if the incoming shutdown was reported by the drain()
306 * function.
307 */
308int conn_sock_drain(struct connection *conn)
309{
310 if (!conn_ctrl_ready(conn))
311 return 1;
312
313 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
314 return 1;
315
Willy Tarreau585744b2017-08-24 14:31:19 +0200316 if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) {
317 fdtab[conn->handle.fd].linger_risk = 0;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100318 }
319 else {
Willy Tarreau585744b2017-08-24 14:31:19 +0200320 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreaud85c4852015-03-13 00:40:28 +0100321 return 0;
322
323 /* disable draining if we were called and have no drain function */
324 if (!conn->ctrl->drain) {
325 __conn_data_stop_recv(conn);
326 return 0;
327 }
328
Willy Tarreau585744b2017-08-24 14:31:19 +0200329 if (conn->ctrl->drain(conn->handle.fd) <= 0)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100330 return 0;
331 }
332
333 conn->flags |= CO_FL_SOCK_RD_SH;
334 return 1;
335}
336
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100337/*
338 * Get data length from tlv
339 */
340static int get_tlv_length(const struct tlv *src)
341{
342 return (src->length_hi << 8) | src->length_lo;
343}
344
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200345/* This handshake handler waits a PROXY protocol header at the beginning of the
346 * raw data stream. The header looks like this :
347 *
348 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
349 *
350 * There must be exactly one space between each field. Fields are :
351 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
352 * - SRC3 : layer 3 (eg: IP) source address in standard text form
353 * - DST3 : layer 3 (eg: IP) destination address in standard text form
354 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
355 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
356 *
357 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
358 *
359 * The header line is small and in all cases smaller than the smallest normal
360 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
361 * can safely use MSG_PEEK and avoid buffering.
362 *
363 * Once the data is fetched, the values are set in the connection's address
364 * fields, and data are removed from the socket's buffer. The function returns
365 * zero if it needs to wait for more data or if it fails, or 1 if it completed
366 * and removed itself.
367 */
368int conn_recv_proxy(struct connection *conn, int flag)
369{
370 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200371 struct proxy_hdr_v2 *hdr_v2;
372 const char v2sig[] = PP2_SIGNATURE;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100373 int tlv_length = 0;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200374 int tlv_offset = 0;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200375
376 /* we might have been called just after an asynchronous shutr */
377 if (conn->flags & CO_FL_SOCK_RD_SH)
378 goto fail;
379
Willy Tarreau3c728722014-01-23 13:50:42 +0100380 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200381 goto fail;
382
Willy Tarreau585744b2017-08-24 14:31:19 +0200383 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100384 return 0;
385
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200386 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200387 trash.len = recv(conn->handle.fd, trash.str, trash.size, MSG_PEEK);
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100388 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200389 if (errno == EINTR)
390 continue;
391 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200392 fd_cant_recv(conn->handle.fd);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200393 return 0;
394 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100395 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200396 }
397 } while (0);
398
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100399 if (!trash.len) {
400 /* client shutdown */
401 conn->err_code = CO_ER_PRX_EMPTY;
402 goto fail;
403 }
404
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100405 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200406 goto missing;
407
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100408 line = trash.str;
409 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200410
411 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200412 if (strncmp(line, "PROXY ", 6) != 0)
413 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200414
415 line += 6;
Willy Tarreau4c20d292014-06-14 11:41:36 +0200416 if (trash.len < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200417 goto missing;
418
David CARLIER42ff05e2016-03-24 09:22:36 +0000419 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200420 u32 src3, dst3, sport, dport;
421
422 line += 5;
423
424 src3 = inetaddr_host_lim_ret(line, end, &line);
425 if (line == end)
426 goto missing;
427 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100428 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200429
430 dst3 = inetaddr_host_lim_ret(line, end, &line);
431 if (line == end)
432 goto missing;
433 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100434 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200435
436 sport = read_uint((const char **)&line, end);
437 if (line == end)
438 goto missing;
439 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100440 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200441
442 dport = read_uint((const char **)&line, end);
443 if (line > end - 2)
444 goto missing;
445 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100446 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200447 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100448 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200449
450 /* update the session's addresses and mark them set */
451 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
452 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
453 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
454
455 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
456 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
457 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
458 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
459 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000460 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200461 u32 sport, dport;
462 char *src_s;
463 char *dst_s, *sport_s, *dport_s;
464 struct in6_addr src3, dst3;
465
466 line += 5;
467
468 src_s = line;
469 dst_s = sport_s = dport_s = NULL;
470 while (1) {
471 if (line > end - 2) {
472 goto missing;
473 }
474 else if (*line == '\r') {
475 *line = 0;
476 line++;
477 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100478 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200479 break;
480 }
481
482 if (*line == ' ') {
483 *line = 0;
484 if (!dst_s)
485 dst_s = line + 1;
486 else if (!sport_s)
487 sport_s = line + 1;
488 else if (!dport_s)
489 dport_s = line + 1;
490 }
491 line++;
492 }
493
494 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100495 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200496
497 sport = read_uint((const char **)&sport_s,dport_s - 1);
498 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100499 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200500
501 dport = read_uint((const char **)&dport_s,line - 2);
502 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100503 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200504
505 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100506 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200507
508 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100509 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200510
511 /* update the session's addresses and mark them set */
512 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
513 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
514 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
515
516 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
517 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
518 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
519 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
520 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200521 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
522 /* This can be a UNIX socket forwarded by an haproxy upstream */
523 line += 9;
524 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200525 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200526 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100527 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200528 goto fail;
529 }
530
Willy Tarreau77992672014-06-14 11:06:17 +0200531 trash.len = line - trash.str;
532 goto eat_header;
533
534 not_v1:
535 /* try PPv2 */
536 if (trash.len < PP2_HEADER_LEN)
537 goto missing;
538
539 hdr_v2 = (struct proxy_hdr_v2 *)trash.str;
540
541 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
542 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
543 conn->err_code = CO_ER_PRX_NOT_HDR;
544 goto fail;
545 }
546
547 if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len))
548 goto missing;
549
550 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
551 case 0x01: /* PROXY command */
552 switch (hdr_v2->fam) {
553 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100554 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
555 goto bad_header;
556
Willy Tarreau77992672014-06-14 11:06:17 +0200557 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
558 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
559 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port;
560 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
561 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
562 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port;
563 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200564 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100565 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200566 break;
567 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100568 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
569 goto bad_header;
570
Willy Tarreau77992672014-06-14 11:06:17 +0200571 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
572 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
573 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port;
574 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
575 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
576 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port;
577 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200578 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100579 tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200580 break;
581 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100582
583 /* TLV parsing */
584 if (tlv_length > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100585 while (tlv_offset + TLV_HEADER_SIZE <= trash.len) {
586 const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset];
587 const int tlv_len = get_tlv_length(tlv_packet);
588 tlv_offset += tlv_len + TLV_HEADER_SIZE;
589
590 switch (tlv_packet->type) {
591#ifdef CONFIG_HAP_NS
592 case PP2_TYPE_NETNS: {
593 const struct netns_entry *ns;
594 ns = netns_store_lookup((char*)tlv_packet->value, tlv_len);
595 if (ns)
596 conn->proxy_netns = ns;
597 break;
598 }
599#endif
600 default:
601 break;
602 }
603 }
604 }
605
Willy Tarreau77992672014-06-14 11:06:17 +0200606 /* unsupported protocol, keep local connection address */
607 break;
608 case 0x00: /* LOCAL command */
609 /* keep local connection address for LOCAL */
610 break;
611 default:
612 goto bad_header; /* not a supported command */
613 }
614
615 trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
616 goto eat_header;
617
618 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200619 /* remove the PROXY line from the request. For this we re-read the
620 * exact line at once. If we don't get the exact same result, we
621 * fail.
622 */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200623 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200624 int len2 = recv(conn->handle.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200625 if (len2 < 0 && errno == EINTR)
626 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100627 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100628 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200629 } while (0);
630
631 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100632 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200633 return 1;
634
635 missing:
636 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
637 * we have not read anything. Otherwise we need to fail because we won't
638 * be able to poll anymore.
639 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100640 conn->err_code = CO_ER_PRX_TRUNCATED;
641 goto fail;
642
643 bad_header:
644 /* This is not a valid proxy protocol header */
645 conn->err_code = CO_ER_PRX_BAD_HDR;
646 goto fail;
647
648 recv_abort:
649 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100650 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100651 goto fail;
652
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200653 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100654 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200655 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200656 return 0;
657}
658
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100659/* This handshake handler waits a NetScaler Client IP insertion header
660 * at the beginning of the raw data stream. The header looks like this:
661 *
662 * 4 bytes: CIP magic number
663 * 4 bytes: Header length
664 * 20+ bytes: Header of the last IP packet sent by the client during
665 * TCP handshake.
666 * 20+ bytes: Header of the last TCP packet sent by the client during
667 * TCP handshake.
668 *
669 * This line MUST be at the beginning of the buffer and MUST NOT be
670 * fragmented.
671 *
672 * The header line is small and in all cases smaller than the smallest normal
673 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
674 * can safely use MSG_PEEK and avoid buffering.
675 *
676 * Once the data is fetched, the values are set in the connection's address
677 * fields, and data are removed from the socket's buffer. The function returns
678 * zero if it needs to wait for more data or if it fails, or 1 if it completed
679 * and removed itself.
680 */
681int conn_recv_netscaler_cip(struct connection *conn, int flag)
682{
683 char *line;
684 uint32_t cip_magic;
685 uint32_t cip_len;
686 uint8_t ip_v;
687
688 /* we might have been called just after an asynchronous shutr */
689 if (conn->flags & CO_FL_SOCK_RD_SH)
690 goto fail;
691
692 if (!conn_ctrl_ready(conn))
693 goto fail;
694
Willy Tarreau585744b2017-08-24 14:31:19 +0200695 if (!fd_recv_ready(conn->handle.fd))
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100696 return 0;
697
698 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200699 trash.len = recv(conn->handle.fd, trash.str, trash.size, MSG_PEEK);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100700 if (trash.len < 0) {
701 if (errno == EINTR)
702 continue;
703 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200704 fd_cant_recv(conn->handle.fd);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100705 return 0;
706 }
707 goto recv_abort;
708 }
709 } while (0);
710
711 if (!trash.len) {
712 /* client shutdown */
713 conn->err_code = CO_ER_CIP_EMPTY;
714 goto fail;
715 }
716
717 /* Fail if buffer length is not large enough to contain
718 * CIP magic, CIP length */
719 if (trash.len < 8)
720 goto missing;
721
722 line = trash.str;
723
724 cip_magic = ntohl(*(uint32_t *)line);
725 cip_len = ntohl(*(uint32_t *)(line+4));
726
727 /* Decode a possible NetScaler Client IP request, fail early if
728 * it does not match */
729 if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic)
730 goto bad_magic;
731
732 /* Fail if buffer length is not large enough to contain
733 * CIP magic, CIP length, minimal IP header */
734 if (trash.len < 28)
735 goto missing;
736
737 line += 8;
738
739 /* Get IP version from the first four bits */
740 ip_v = (*line & 0xf0) >> 4;
741
742 if (ip_v == 4) {
743 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100744 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100745
746 hdr_ip4 = (struct ip *)line;
747
748 if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) {
749 /* Fail if buffer length is not large enough to contain
750 * CIP magic, CIP length, IPv4 header */
751 goto missing;
752 } else if (hdr_ip4->ip_p != IPPROTO_TCP) {
753 /* The protocol does not include a TCP header */
754 conn->err_code = CO_ER_CIP_BAD_PROTO;
755 goto fail;
756 } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) {
757 /* Fail if buffer length is not large enough to contain
758 * CIP magic, CIP length, IPv4 header, TCP header */
759 goto missing;
760 }
761
David Carlier3015a2e2016-07-04 22:51:33 +0100762 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100763
764 /* update the session's addresses and mark them set */
765 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
766 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
767 ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source;
768
769 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
770 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
771 ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest;
772
773 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
774 }
775 else if (ip_v == 6) {
776 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100777 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100778
779 hdr_ip6 = (struct ip6_hdr *)line;
780
781 if (trash.len < 28) {
782 /* Fail if buffer length is not large enough to contain
783 * CIP magic, CIP length, IPv6 header */
784 goto missing;
785 } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
786 /* The protocol does not include a TCP header */
787 conn->err_code = CO_ER_CIP_BAD_PROTO;
788 goto fail;
789 } else if (trash.len < 48) {
790 /* Fail if buffer length is not large enough to contain
791 * CIP magic, CIP length, IPv6 header, TCP header */
792 goto missing;
793 }
794
David Carlier3015a2e2016-07-04 22:51:33 +0100795 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100796
797 /* update the session's addresses and mark them set */
798 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
799 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src;
800 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source;
801
802 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
803 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst;
804 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest;
805
806 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
807 }
808 else {
809 /* The protocol does not match something known (IPv4/IPv6) */
810 conn->err_code = CO_ER_CIP_BAD_PROTO;
811 goto fail;
812 }
813
814 line += cip_len;
815 trash.len = line - trash.str;
816
817 /* remove the NetScaler Client IP header from the request. For this
818 * we re-read the exact line at once. If we don't get the exact same
819 * result, we fail.
820 */
821 do {
Willy Tarreau585744b2017-08-24 14:31:19 +0200822 int len2 = recv(conn->handle.fd, trash.str, trash.len, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100823 if (len2 < 0 && errno == EINTR)
824 continue;
825 if (len2 != trash.len)
826 goto recv_abort;
827 } while (0);
828
829 conn->flags &= ~flag;
830 return 1;
831
832 missing:
833 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
834 * we have not read anything. Otherwise we need to fail because we won't
835 * be able to poll anymore.
836 */
837 conn->err_code = CO_ER_CIP_TRUNCATED;
838 goto fail;
839
840 bad_magic:
841 conn->err_code = CO_ER_CIP_BAD_MAGIC;
842 goto fail;
843
844 recv_abort:
845 conn->err_code = CO_ER_CIP_ABORT;
846 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
847 goto fail;
848
849 fail:
850 __conn_sock_stop_both(conn);
851 conn->flags |= CO_FL_ERROR;
852 return 0;
853}
854
David Safb76832014-05-08 23:42:08 -0400855int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote)
856{
857 int ret = 0;
858
859 if (srv && (srv->pp_opts & SRV_PP_V2)) {
860 ret = make_proxy_line_v2(buf, buf_len, srv, remote);
861 }
862 else {
863 if (remote)
864 ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to);
865 else
866 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
867 }
868
869 return ret;
870}
871
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200872/* Makes a PROXY protocol line from the two addresses. The output is sent to
873 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
874 * It returns the number of bytes composing this line (including the trailing
875 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200876 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
877 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200878 */
David Safb76832014-05-08 23:42:08 -0400879int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200880{
881 int ret = 0;
882
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200883 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200884 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
885 if (ret >= buf_len)
886 return 0;
887
888 /* IPv4 src */
889 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
890 return 0;
891
892 ret += strlen(buf + ret);
893 if (ret >= buf_len)
894 return 0;
895
896 buf[ret++] = ' ';
897
898 /* IPv4 dst */
899 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
900 return 0;
901
902 ret += strlen(buf + ret);
903 if (ret >= buf_len)
904 return 0;
905
906 /* source and destination ports */
907 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
908 ntohs(((struct sockaddr_in *)src)->sin_port),
909 ntohs(((struct sockaddr_in *)dst)->sin_port));
910 if (ret >= buf_len)
911 return 0;
912 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200913 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200914 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
915 if (ret >= buf_len)
916 return 0;
917
918 /* IPv6 src */
919 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
920 return 0;
921
922 ret += strlen(buf + ret);
923 if (ret >= buf_len)
924 return 0;
925
926 buf[ret++] = ' ';
927
928 /* IPv6 dst */
929 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
930 return 0;
931
932 ret += strlen(buf + ret);
933 if (ret >= buf_len)
934 return 0;
935
936 /* source and destination ports */
937 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
938 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
939 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
940 if (ret >= buf_len)
941 return 0;
942 }
943 else {
944 /* unknown family combination */
945 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
946 if (ret >= buf_len)
947 return 0;
948 }
949 return ret;
950}
David Safb76832014-05-08 23:42:08 -0400951
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100952#if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS)
953static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -0400954{
955 struct tlv *tlv;
956
957 if (!dest || (length + sizeof(*tlv) > dest_len))
958 return 0;
959
960 tlv = (struct tlv *)dest;
961
962 tlv->type = type;
963 tlv->length_hi = length >> 8;
964 tlv->length_lo = length & 0x00ff;
965 memcpy(tlv->value, value, length);
966 return length + sizeof(*tlv);
967}
968#endif
969
970int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote)
971{
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200972 const char pp2_signature[] = PP2_SIGNATURE;
David Safb76832014-05-08 23:42:08 -0400973 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200974 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +0200975 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -0400976 struct sockaddr_storage *src = &null_addr;
977 struct sockaddr_storage *dst = &null_addr;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100978
David Safb76832014-05-08 23:42:08 -0400979#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -0400980 char *value = NULL;
981 struct tlv_ssl *tlv;
982 int ssl_tlv_len = 0;
Dave McCowan77d1f012014-07-17 14:34:01 -0400983 struct chunk *cn_trash;
David Safb76832014-05-08 23:42:08 -0400984#endif
985
986 if (buf_len < PP2_HEADER_LEN)
987 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200988 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -0400989
990 if (remote) {
991 src = &remote->addr.from;
992 dst = &remote->addr.to;
993 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100994
David Safb76832014-05-08 23:42:08 -0400995 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
996 if (buf_len < PP2_HDR_LEN_INET)
997 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +0200998 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
999 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1000 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1001 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1002 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1003 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
David Safb76832014-05-08 23:42:08 -04001004 ret = PP2_HDR_LEN_INET;
1005 }
1006 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
1007 if (buf_len < PP2_HDR_LEN_INET6)
1008 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001009 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
1010 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1011 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1012 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1013 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1014 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
David Safb76832014-05-08 23:42:08 -04001015 ret = PP2_HDR_LEN_INET6;
1016 }
1017 else {
1018 if (buf_len < PP2_HDR_LEN_UNSPEC)
1019 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001020 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1021 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001022 ret = PP2_HDR_LEN_UNSPEC;
1023 }
1024
1025#ifdef USE_OPENSSL
1026 if (srv->pp_opts & SRV_PP_V2_SSL) {
1027 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1028 return 0;
1029 tlv = (struct tlv_ssl *)&buf[ret];
1030 memset(tlv, 0, sizeof(struct tlv_ssl));
1031 ssl_tlv_len += sizeof(struct tlv_ssl);
1032 tlv->tlv.type = PP2_TYPE_SSL;
1033 if (ssl_sock_is_ssl(remote)) {
1034 tlv->client |= PP2_CLIENT_SSL;
1035 value = ssl_sock_get_version(remote);
1036 if (value) {
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_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001038 }
Dave McCowan328fb582014-07-30 10:39:13 -04001039 if (ssl_sock_get_cert_used_sess(remote)) {
1040 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001041 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001042 if (ssl_sock_get_cert_used_conn(remote))
1043 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001044 }
1045 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Dave McCowan77d1f012014-07-17 14:34:01 -04001046 cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001047 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001048 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 -04001049 }
1050 }
1051 }
1052 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1053 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1054 ret += ssl_tlv_len;
1055 }
1056#endif
1057
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001058#ifdef CONFIG_HAP_NS
1059 if (remote && (remote->proxy_netns)) {
1060 if ((buf_len - ret) < sizeof(struct tlv))
1061 return 0;
1062 ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
1063 }
1064#endif
1065
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001066 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001067
1068 return ret;
1069}
Emeric Brun4f603012017-01-05 15:11:44 +01001070
1071/* fetch if the received connection used a PROXY protocol header */
1072int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1073{
1074 struct connection *conn;
1075
1076 conn = objt_conn(smp->sess->origin);
1077 if (!conn)
1078 return 0;
1079
1080 if (!(conn->flags & CO_FL_CONNECTED)) {
1081 smp->flags |= SMP_F_MAY_CHANGE;
1082 return 0;
1083 }
1084
1085 smp->flags = 0;
1086 smp->data.type = SMP_T_BOOL;
1087 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1088
1089 return 1;
1090}
1091
1092/* Note: must not be declared <const> as its list will be overwritten.
1093 * Note: fetches that may return multiple types must be declared as the lowest
1094 * common denominator, the type that can be casted into all other ones. For
1095 * instance v4/v6 must be declared v4.
1096 */
1097static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1098 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
1099 { /* END */ },
1100}};
1101
1102
1103__attribute__((constructor))
1104static void __connection_init(void)
1105{
1106 sample_register_fetches(&sample_fetch_keywords);
1107}