blob: 367d1e3bbba17f3fb97318f7ea31a89a95b9be96 [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>
17
Willy Tarreauc5788912012-08-24 18:12:41 +020018#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020019#include <proto/fd.h>
Willy Tarreau5f1504f2012-10-04 23:55:57 +020020#include <proto/frontend.h>
Willy Tarreau2da156f2012-07-23 15:07:23 +020021#include <proto/proto_tcp.h>
Willy Tarreau2542b532012-08-31 16:01:23 +020022#include <proto/session.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 Tarreauafad0e02012-08-09 14:45:22 +020039 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020040 */
41int conn_fd_handler(int fd)
42{
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 Tarreau2542b532012-08-31 16:01:23 +020047 return 0;
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 Tarreaud9de7ca2012-09-02 18:48:46 +020062 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020063 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020064
Willy Tarreau22cda212012-08-31 17:43:29 +020065 if (conn->flags & CO_FL_ACCEPT_PROXY)
66 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
67 goto leave;
68
Willy Tarreau57cd3e42013-10-24 22:01:26 +020069 if (conn->flags & CO_FL_SEND_PROXY)
70 if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY))
Willy Tarreau5f1504f2012-10-04 23:55:57 +020071 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020072#ifdef USE_OPENSSL
73 if (conn->flags & CO_FL_SSL_WAIT_HS)
74 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
75 goto leave;
76#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020077 }
78
Willy Tarreauf9dabec2012-08-17 17:33:53 +020079 /* Once we're purely in the data phase, we disable handshake polling */
80 if (!(conn->flags & CO_FL_POLL_SOCK))
81 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020082
Willy Tarreau071e1372012-10-03 01:39:48 +020083 /* The data layer might not be ready yet (eg: when using embryonic
84 * sessions). If we're about to move data, we must initialize it first.
85 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020086 * we must not use it anymore and should immediately leave instead.
87 */
Willy Tarreau071e1372012-10-03 01:39:48 +020088 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020089 return 0;
90
Willy Tarreau153c3ca2012-10-22 22:47:55 +020091 /* The data transfer starts here and stops on error and handshakes. Note
92 * that we must absolutely test conn->xprt at each step in case it suddenly
93 * changes due to a quick unexpected close().
94 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020095 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +020096 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020097 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +020098 /* force detection of a flag change : it's impossible to have both
99 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200100 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200101 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200102 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200103 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200104
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200105 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200106 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200107 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200108 /* force detection of a flag change : it's impossible to have both
109 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200110 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200111 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200112 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200113 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200114
Willy Tarreauc76ae332012-07-12 15:32:13 +0200115 /* It may happen during the data phase that a handshake is
116 * enabled again (eg: SSL)
117 */
Willy Tarreaud6e999b2013-11-25 08:41:15 +0100118 if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR)))
Willy Tarreauc76ae332012-07-12 15:32:13 +0200119 goto process_handshake;
120
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200121 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
122 /* still waiting for a connection to establish and nothing was
123 * attempted yet to probe the connection. Then let's retry the
124 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200125 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200126 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200127 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200128 }
129
Willy Tarreau2c6be842012-07-06 17:12:34 +0200130 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200131 /* The wake callback may be used to process a critical error and abort the
132 * connection. If so, we don't want to go further as the connection will
133 * have been released and the FD destroyed.
134 */
135 if ((conn->flags & CO_FL_WAKE_DATA) &&
136 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
137 conn->data->wake(conn) < 0)
138 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200139
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200140 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200141 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200142 conn->flags |= CO_FL_CONNECTED;
143
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200144 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100145 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200146
147 /* commit polling changes */
148 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200149 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200150}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200151
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200152/* Update polling on connection <c>'s file descriptor depending on its current
153 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
154 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
155 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200156 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200157 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200158void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200160 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161
Willy Tarreauf79c8172013-10-21 16:30:56 +0200162 if (!(c->flags & CO_FL_CTRL_READY))
163 return;
164
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100166 if (unlikely((f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100167 fd_want_recv(c->t.sock.fd);
168 fd_cant_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100169 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200170 }
171 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200172 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100173 f |= CO_FL_CURR_RD_ENA;
174 }
175 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
176 fd_stop_recv(c->t.sock.fd);
177 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200178 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200179
180 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100181 if (unlikely((f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100182 fd_want_send(c->t.sock.fd);
183 fd_cant_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100184 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200185 }
186 else 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 +0200187 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100188 f |= CO_FL_CURR_WR_ENA;
189 }
190 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
191 fd_stop_send(c->t.sock.fd);
192 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200193 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100194 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200195}
196
197/* Update polling on connection <c>'s file descriptor depending on its current
198 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
199 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
200 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200201 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200202 */
203void conn_update_sock_polling(struct connection *c)
204{
205 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200206
Willy Tarreauf79c8172013-10-21 16:30:56 +0200207 if (!(c->flags & CO_FL_CTRL_READY))
208 return;
209
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200210 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100211 if (unlikely((f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212 fd_want_recv(c->t.sock.fd);
213 fd_cant_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100214 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200215 }
216 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200217 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100218 f |= CO_FL_CURR_RD_ENA;
219 }
220 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
221 fd_stop_recv(c->t.sock.fd);
222 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200223 }
224
225 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100226 if (unlikely((f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100227 fd_want_send(c->t.sock.fd);
228 fd_cant_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100229 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200230 }
231 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200232 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100233 f |= CO_FL_CURR_WR_ENA;
234 }
235 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
236 fd_stop_send(c->t.sock.fd);
237 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200238 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100239 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200240}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200241
242/* This handshake handler waits a PROXY protocol header at the beginning of the
243 * raw data stream. The header looks like this :
244 *
245 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
246 *
247 * There must be exactly one space between each field. Fields are :
248 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
249 * - SRC3 : layer 3 (eg: IP) source address in standard text form
250 * - DST3 : layer 3 (eg: IP) destination address in standard text form
251 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
252 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
253 *
254 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
255 *
256 * The header line is small and in all cases smaller than the smallest normal
257 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
258 * can safely use MSG_PEEK and avoid buffering.
259 *
260 * Once the data is fetched, the values are set in the connection's address
261 * fields, and data are removed from the socket's buffer. The function returns
262 * zero if it needs to wait for more data or if it fails, or 1 if it completed
263 * and removed itself.
264 */
265int conn_recv_proxy(struct connection *conn, int flag)
266{
267 char *line, *end;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200268
269 /* we might have been called just after an asynchronous shutr */
270 if (conn->flags & CO_FL_SOCK_RD_SH)
271 goto fail;
272
Willy Tarreauf79c8172013-10-21 16:30:56 +0200273 if (!(conn->flags & CO_FL_CTRL_READY))
274 goto fail;
275
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200276 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100277 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
278 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200279 if (errno == EINTR)
280 continue;
281 if (errno == EAGAIN) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100282 __conn_sock_poll_recv(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200283 return 0;
284 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100285 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200286 }
287 } while (0);
288
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100289 if (!trash.len) {
290 /* client shutdown */
291 conn->err_code = CO_ER_PRX_EMPTY;
292 goto fail;
293 }
294
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100295 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200296 goto missing;
297
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100298 line = trash.str;
299 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200300
301 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100302 if (strncmp(line, "PROXY ", 6) != 0) {
303 conn->err_code = CO_ER_PRX_NOT_HDR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200304 goto fail;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100305 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200306
307 line += 6;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100308 if (trash.len < 18) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200309 goto missing;
310
311 if (!memcmp(line, "TCP4 ", 5) != 0) {
312 u32 src3, dst3, sport, dport;
313
314 line += 5;
315
316 src3 = inetaddr_host_lim_ret(line, end, &line);
317 if (line == end)
318 goto missing;
319 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100320 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200321
322 dst3 = inetaddr_host_lim_ret(line, end, &line);
323 if (line == end)
324 goto missing;
325 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100326 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200327
328 sport = read_uint((const char **)&line, end);
329 if (line == end)
330 goto missing;
331 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100332 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200333
334 dport = read_uint((const char **)&line, end);
335 if (line > end - 2)
336 goto missing;
337 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100338 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200339 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100340 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200341
342 /* update the session's addresses and mark them set */
343 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
344 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
345 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
346
347 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
348 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
349 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
350 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
351 }
352 else if (!memcmp(line, "TCP6 ", 5) != 0) {
353 u32 sport, dport;
354 char *src_s;
355 char *dst_s, *sport_s, *dport_s;
356 struct in6_addr src3, dst3;
357
358 line += 5;
359
360 src_s = line;
361 dst_s = sport_s = dport_s = NULL;
362 while (1) {
363 if (line > end - 2) {
364 goto missing;
365 }
366 else if (*line == '\r') {
367 *line = 0;
368 line++;
369 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100370 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200371 break;
372 }
373
374 if (*line == ' ') {
375 *line = 0;
376 if (!dst_s)
377 dst_s = line + 1;
378 else if (!sport_s)
379 sport_s = line + 1;
380 else if (!dport_s)
381 dport_s = line + 1;
382 }
383 line++;
384 }
385
386 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100387 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200388
389 sport = read_uint((const char **)&sport_s,dport_s - 1);
390 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100391 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200392
393 dport = read_uint((const char **)&dport_s,line - 2);
394 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100395 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200396
397 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100398 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200399
400 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100401 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200402
403 /* update the session's addresses and mark them set */
404 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
405 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
406 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
407
408 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
409 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
410 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
411 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
412 }
413 else {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100414 /* The protocol does not match something known (TCP4/TCP6) */
415 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200416 goto fail;
417 }
418
419 /* remove the PROXY line from the request. For this we re-read the
420 * exact line at once. If we don't get the exact same result, we
421 * fail.
422 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100423 trash.len = line - trash.str;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200424 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100425 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200426 if (len2 < 0 && errno == EINTR)
427 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100428 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100429 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200430 } while (0);
431
432 conn->flags &= ~flag;
433 return 1;
434
435 missing:
436 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
437 * we have not read anything. Otherwise we need to fail because we won't
438 * be able to poll anymore.
439 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100440 conn->err_code = CO_ER_PRX_TRUNCATED;
441 goto fail;
442
443 bad_header:
444 /* This is not a valid proxy protocol header */
445 conn->err_code = CO_ER_PRX_BAD_HDR;
446 goto fail;
447
448 recv_abort:
449 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100450 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100451 goto fail;
452
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200453 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100454 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200455 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200456 return 0;
457}
458
459/* Makes a PROXY protocol line from the two addresses. The output is sent to
460 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
461 * It returns the number of bytes composing this line (including the trailing
462 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200463 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
464 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200465 */
466int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
467{
468 int ret = 0;
469
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200470 if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200471 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
472 if (ret >= buf_len)
473 return 0;
474
475 /* IPv4 src */
476 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
477 return 0;
478
479 ret += strlen(buf + ret);
480 if (ret >= buf_len)
481 return 0;
482
483 buf[ret++] = ' ';
484
485 /* IPv4 dst */
486 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
487 return 0;
488
489 ret += strlen(buf + ret);
490 if (ret >= buf_len)
491 return 0;
492
493 /* source and destination ports */
494 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
495 ntohs(((struct sockaddr_in *)src)->sin_port),
496 ntohs(((struct sockaddr_in *)dst)->sin_port));
497 if (ret >= buf_len)
498 return 0;
499 }
Willy Tarreau2e1401a2013-10-01 11:41:55 +0200500 else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200501 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
502 if (ret >= buf_len)
503 return 0;
504
505 /* IPv6 src */
506 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
507 return 0;
508
509 ret += strlen(buf + ret);
510 if (ret >= buf_len)
511 return 0;
512
513 buf[ret++] = ' ';
514
515 /* IPv6 dst */
516 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
517 return 0;
518
519 ret += strlen(buf + ret);
520 if (ret >= buf_len)
521 return 0;
522
523 /* source and destination ports */
524 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
525 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
526 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
527 if (ret >= buf_len)
528 return 0;
529 }
530 else {
531 /* unknown family combination */
532 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
533 if (ret >= buf_len)
534 return 0;
535 }
536 return ret;
537}