blob: d882bb74d3345462fc09298a22878ef3a515503b [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 Tarreaue9dfa792012-09-01 17:26:16 +020049 /* before engaging there, we clear the new WAIT_* flags so that we can
50 * more easily detect an EAGAIN condition from anywhere.
51 */
Willy Tarreau9e272bf2012-10-03 21:04:48 +020052 flags = conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
Willy Tarreau9a92cd52012-11-24 11:12:13 +010053 flags &= ~CO_FL_ERROR; /* ensure to call the wake handler upon error */
54
55 if (unlikely(conn->flags & CO_FL_ERROR))
56 goto leave;
Willy Tarreaue9dfa792012-09-01 17:26:16 +020057
Willy Tarreauc76ae332012-07-12 15:32:13 +020058 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020059 /* The handshake callbacks are called in sequence. If either of them is
60 * missing something, it must enable the required polling at the socket
61 * layer of the connection. Polling state is not guaranteed when entering
62 * these handlers, so any handshake handler which does not complete its
63 * work must explicitly disable events it's not interested in.
64 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020065 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020066 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020067 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020068
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 Tarreauc76ae332012-07-12 15:32:13 +020073 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020074 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020075 goto leave;
Willy Tarreau5f1504f2012-10-04 23:55:57 +020076
77 if (conn->flags & CO_FL_LOCAL_SPROXY)
78 if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY))
79 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020080#ifdef USE_OPENSSL
81 if (conn->flags & CO_FL_SSL_WAIT_HS)
82 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
83 goto leave;
84#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020085 }
86
Willy Tarreauf9dabec2012-08-17 17:33:53 +020087 /* Once we're purely in the data phase, we disable handshake polling */
88 if (!(conn->flags & CO_FL_POLL_SOCK))
89 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020090
Willy Tarreau071e1372012-10-03 01:39:48 +020091 /* The data layer might not be ready yet (eg: when using embryonic
92 * sessions). If we're about to move data, we must initialize it first.
93 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020094 * we must not use it anymore and should immediately leave instead.
95 */
Willy Tarreau071e1372012-10-03 01:39:48 +020096 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020097 return 0;
98
Willy Tarreau153c3ca2012-10-22 22:47:55 +020099 /* The data transfer starts here and stops on error and handshakes. Note
100 * that we must absolutely test conn->xprt at each step in case it suddenly
101 * changes due to a quick unexpected close().
102 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200103 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200104 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200105 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200106 /* force detection of a flag change : it's impossible to have both
107 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200108 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200109 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200110 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200111 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200112
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200113 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200114 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200115 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200116 /* force detection of a flag change : it's impossible to have both
117 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200118 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200119 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200120 conn->data->send(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 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200124 goto leave;
125
Willy Tarreauc76ae332012-07-12 15:32:13 +0200126 /* It may happen during the data phase that a handshake is
127 * enabled again (eg: SSL)
128 */
129 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
130 goto process_handshake;
131
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200132 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
133 /* still waiting for a connection to establish and nothing was
134 * attempted yet to probe the connection. Then let's retry the
135 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200136 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200137 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200138 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200139 }
140
Willy Tarreau2c6be842012-07-06 17:12:34 +0200141 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200142 /* The wake callback may be used to process a critical error and abort the
143 * connection. If so, we don't want to go further as the connection will
144 * have been released and the FD destroyed.
145 */
146 if ((conn->flags & CO_FL_WAKE_DATA) &&
147 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
148 conn->data->wake(conn) < 0)
149 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200150
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200151 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200152 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200153 conn->flags |= CO_FL_CONNECTED;
154
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200155 /* remove the events before leaving */
156 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200157
158 /* commit polling changes */
159 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200160 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200161}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200162
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200163/* Update polling on connection <c>'s file descriptor depending on its current
164 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
165 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
166 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200167 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200170{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200171 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200172
173 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100174 if (unlikely((f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100176 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200177 }
178 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 +0200179 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100180 f |= CO_FL_CURR_RD_ENA;
181 }
182 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
183 fd_stop_recv(c->t.sock.fd);
184 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200185 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200186
187 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100188 if (unlikely((f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200189 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100190 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200191 }
192 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 +0200193 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100194 f |= CO_FL_CURR_WR_ENA;
195 }
196 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
197 fd_stop_send(c->t.sock.fd);
198 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200199 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100200 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200201}
202
203/* Update polling on connection <c>'s file descriptor depending on its current
204 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
205 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
206 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200207 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200208 */
209void conn_update_sock_polling(struct connection *c)
210{
211 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200212
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200213 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100214 if (unlikely((f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200215 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100216 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200217 }
218 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 +0200219 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100220 f |= CO_FL_CURR_RD_ENA;
221 }
222 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
223 fd_stop_recv(c->t.sock.fd);
224 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200225 }
226
227 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100228 if (unlikely((f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200229 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100230 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200231 }
232 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 +0200233 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100234 f |= CO_FL_CURR_WR_ENA;
235 }
236 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
237 fd_stop_send(c->t.sock.fd);
238 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200239 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100240 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200241}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200242
243/* This handshake handler waits a PROXY protocol header at the beginning of the
244 * raw data stream. The header looks like this :
245 *
246 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
247 *
248 * There must be exactly one space between each field. Fields are :
249 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
250 * - SRC3 : layer 3 (eg: IP) source address in standard text form
251 * - DST3 : layer 3 (eg: IP) destination address in standard text form
252 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
253 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
254 *
255 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
256 *
257 * The header line is small and in all cases smaller than the smallest normal
258 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
259 * can safely use MSG_PEEK and avoid buffering.
260 *
261 * Once the data is fetched, the values are set in the connection's address
262 * fields, and data are removed from the socket's buffer. The function returns
263 * zero if it needs to wait for more data or if it fails, or 1 if it completed
264 * and removed itself.
265 */
266int conn_recv_proxy(struct connection *conn, int flag)
267{
268 char *line, *end;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200269
270 /* we might have been called just after an asynchronous shutr */
271 if (conn->flags & CO_FL_SOCK_RD_SH)
272 goto fail;
273
274 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100275 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
276 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200277 if (errno == EINTR)
278 continue;
279 if (errno == EAGAIN) {
280 conn_sock_poll_recv(conn);
281 return 0;
282 }
283 goto fail;
284 }
285 } while (0);
286
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100287 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200288 goto missing;
289
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100290 line = trash.str;
291 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200292
293 /* Decode a possible proxy request, fail early if it does not match */
294 if (strncmp(line, "PROXY ", 6) != 0)
295 goto fail;
296
297 line += 6;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100298 if (trash.len < 18) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200299 goto missing;
300
301 if (!memcmp(line, "TCP4 ", 5) != 0) {
302 u32 src3, dst3, sport, dport;
303
304 line += 5;
305
306 src3 = inetaddr_host_lim_ret(line, end, &line);
307 if (line == end)
308 goto missing;
309 if (*line++ != ' ')
310 goto fail;
311
312 dst3 = inetaddr_host_lim_ret(line, end, &line);
313 if (line == end)
314 goto missing;
315 if (*line++ != ' ')
316 goto fail;
317
318 sport = read_uint((const char **)&line, end);
319 if (line == end)
320 goto missing;
321 if (*line++ != ' ')
322 goto fail;
323
324 dport = read_uint((const char **)&line, end);
325 if (line > end - 2)
326 goto missing;
327 if (*line++ != '\r')
328 goto fail;
329 if (*line++ != '\n')
330 goto fail;
331
332 /* update the session's addresses and mark them set */
333 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
334 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
335 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
336
337 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
338 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
339 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
340 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
341 }
342 else if (!memcmp(line, "TCP6 ", 5) != 0) {
343 u32 sport, dport;
344 char *src_s;
345 char *dst_s, *sport_s, *dport_s;
346 struct in6_addr src3, dst3;
347
348 line += 5;
349
350 src_s = line;
351 dst_s = sport_s = dport_s = NULL;
352 while (1) {
353 if (line > end - 2) {
354 goto missing;
355 }
356 else if (*line == '\r') {
357 *line = 0;
358 line++;
359 if (*line++ != '\n')
360 goto fail;
361 break;
362 }
363
364 if (*line == ' ') {
365 *line = 0;
366 if (!dst_s)
367 dst_s = line + 1;
368 else if (!sport_s)
369 sport_s = line + 1;
370 else if (!dport_s)
371 dport_s = line + 1;
372 }
373 line++;
374 }
375
376 if (!dst_s || !sport_s || !dport_s)
377 goto fail;
378
379 sport = read_uint((const char **)&sport_s,dport_s - 1);
380 if (*sport_s != 0)
381 goto fail;
382
383 dport = read_uint((const char **)&dport_s,line - 2);
384 if (*dport_s != 0)
385 goto fail;
386
387 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
388 goto fail;
389
390 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
391 goto fail;
392
393 /* update the session's addresses and mark them set */
394 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
395 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
396 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
397
398 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
399 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
400 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
401 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
402 }
403 else {
404 goto fail;
405 }
406
407 /* remove the PROXY line from the request. For this we re-read the
408 * exact line at once. If we don't get the exact same result, we
409 * fail.
410 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100411 trash.len = line - trash.str;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200412 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100413 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200414 if (len2 < 0 && errno == EINTR)
415 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416 if (len2 != trash.len)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200417 goto fail;
418 } while (0);
419
420 conn->flags &= ~flag;
421 return 1;
422
423 missing:
424 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
425 * we have not read anything. Otherwise we need to fail because we won't
426 * be able to poll anymore.
427 */
428 fail:
429 conn_sock_stop_both(conn);
430 conn->flags |= CO_FL_ERROR;
431 conn->flags &= ~flag;
432 return 0;
433}
434
435/* Makes a PROXY protocol line from the two addresses. The output is sent to
436 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
437 * It returns the number of bytes composing this line (including the trailing
438 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
439 * TCP6 and "UNKNOWN" formats.
440 */
441int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
442{
443 int ret = 0;
444
445 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
446 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
447 if (ret >= buf_len)
448 return 0;
449
450 /* IPv4 src */
451 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
452 return 0;
453
454 ret += strlen(buf + ret);
455 if (ret >= buf_len)
456 return 0;
457
458 buf[ret++] = ' ';
459
460 /* IPv4 dst */
461 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
462 return 0;
463
464 ret += strlen(buf + ret);
465 if (ret >= buf_len)
466 return 0;
467
468 /* source and destination ports */
469 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
470 ntohs(((struct sockaddr_in *)src)->sin_port),
471 ntohs(((struct sockaddr_in *)dst)->sin_port));
472 if (ret >= buf_len)
473 return 0;
474 }
475 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
476 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
477 if (ret >= buf_len)
478 return 0;
479
480 /* IPv6 src */
481 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
482 return 0;
483
484 ret += strlen(buf + ret);
485 if (ret >= buf_len)
486 return 0;
487
488 buf[ret++] = ' ';
489
490 /* IPv6 dst */
491 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
492 return 0;
493
494 ret += strlen(buf + ret);
495 if (ret >= buf_len)
496 return 0;
497
498 /* source and destination ports */
499 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
500 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
501 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
502 if (ret >= buf_len)
503 return 0;
504 }
505 else {
506 /* unknown family combination */
507 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
508 if (ret >= buf_len)
509 return 0;
510 }
511 return ret;
512}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200513
514/* This callback is used to send a valid PROXY protocol line to a socket being
515 * established from the local machine. It sets the protocol addresses to the
516 * local and remote address. This is typically used with health checks or when
517 * it is not possible to determine the other end's address. It returns 0 if it
518 * fails in a fatal way or needs to poll to go further, otherwise it returns
519 * non-zero and removes itself from the connection's flags (the bit is provided
520 * in <flag> by the caller). It is designed to be called by the connection
521 * handler and relies on it to commit polling changes. Note that this function
522 * expects to be able to send the whole line at once, which should always be
523 * possible since it is supposed to start at the first byte of the outgoing
524 * data segment.
525 */
526int conn_local_send_proxy(struct connection *conn, unsigned int flag)
527{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100528 int ret;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200529
530 /* we might have been called just after an asynchronous shutw */
531 if (conn->flags & CO_FL_SOCK_WR_SH)
532 goto out_error;
533
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100534 /* The target server expects a PROXY line to be sent first. Retrieving
535 * local or remote addresses may fail until the connection is established.
536 */
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200537 conn_get_from_addr(conn);
538 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100539 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200540
541 conn_get_to_addr(conn);
542 if (!(conn->flags & CO_FL_ADDR_TO_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100543 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200544
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100545 trash.len = make_proxy_line(trash.str, trash.size, &conn->addr.from, &conn->addr.to);
546 if (!trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200547 goto out_error;
548
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100549 /* we have to send the whole trash. If the data layer has a
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200550 * pending write, we'll also set MSG_MORE.
551 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100552 ret = send(conn->t.sock.fd, trash.str, trash.len, (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0);
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200553
554 if (ret == 0)
555 goto out_wait;
556
557 if (ret < 0) {
558 if (errno == EAGAIN)
559 goto out_wait;
560 goto out_error;
561 }
562
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100563 if (ret != trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200564 goto out_error;
565
566 /* The connection is ready now, simply return and let the connection
567 * handler notify upper layers if needed.
568 */
569 if (conn->flags & CO_FL_WAIT_L4_CONN)
570 conn->flags &= ~CO_FL_WAIT_L4_CONN;
571 conn->flags &= ~flag;
572 return 1;
573
574 out_error:
575 /* Write error on the file descriptor */
576 conn->flags |= CO_FL_ERROR;
577 conn->flags &= ~flag;
578 return 0;
579
580 out_wait:
581 __conn_sock_stop_recv(conn);
582 __conn_sock_poll_send(conn);
583 return 0;
584}