blob: 4bb497ef292e28ac7cbdba4ccf8693bd22771e57 [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
Willy Tarreaud29a0662012-12-10 16:33:38 +010055 /* adjust current polling status if it has been updated below us */
56 if (fd_ev_is_set(conn->t.sock.fd, DIR_RD))
57 conn->flags |= CO_FL_CURR_RD_ENA;
58
59 if (fd_ev_is_set(conn->t.sock.fd, DIR_WR))
60 conn->flags |= CO_FL_CURR_WR_ENA;
61
Willy Tarreau9a92cd52012-11-24 11:12:13 +010062 if (unlikely(conn->flags & CO_FL_ERROR))
63 goto leave;
Willy Tarreaue9dfa792012-09-01 17:26:16 +020064
Willy Tarreauc76ae332012-07-12 15:32:13 +020065 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020066 /* The handshake callbacks are called in sequence. If either of them is
67 * missing something, it must enable the required polling at the socket
68 * layer of the connection. Polling state is not guaranteed when entering
69 * these handlers, so any handshake handler which does not complete its
70 * work must explicitly disable events it's not interested in.
71 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020072 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020073 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020074 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020075
Willy Tarreau22cda212012-08-31 17:43:29 +020076 if (conn->flags & CO_FL_ACCEPT_PROXY)
77 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
78 goto leave;
79
Willy Tarreauc76ae332012-07-12 15:32:13 +020080 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020081 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020082 goto leave;
Willy Tarreau5f1504f2012-10-04 23:55:57 +020083
84 if (conn->flags & CO_FL_LOCAL_SPROXY)
85 if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY))
86 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020087#ifdef USE_OPENSSL
88 if (conn->flags & CO_FL_SSL_WAIT_HS)
89 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
90 goto leave;
91#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020092 }
93
Willy Tarreauf9dabec2012-08-17 17:33:53 +020094 /* Once we're purely in the data phase, we disable handshake polling */
95 if (!(conn->flags & CO_FL_POLL_SOCK))
96 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020097
Willy Tarreau071e1372012-10-03 01:39:48 +020098 /* The data layer might not be ready yet (eg: when using embryonic
99 * sessions). If we're about to move data, we must initialize it first.
100 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +0200101 * we must not use it anymore and should immediately leave instead.
102 */
Willy Tarreau071e1372012-10-03 01:39:48 +0200103 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +0200104 return 0;
105
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200106 /* The data transfer starts here and stops on error and handshakes. Note
107 * that we must absolutely test conn->xprt at each step in case it suddenly
108 * changes due to a quick unexpected close().
109 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200110 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200111 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200112 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200113 /* force detection of a flag change : it's impossible to have both
114 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200115 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200116 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200117 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200118 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200119
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200120 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200121 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200122 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200123 /* force detection of a flag change : it's impossible to have both
124 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200125 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200126 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200127 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200128 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200129
Willy Tarreauc76ae332012-07-12 15:32:13 +0200130 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200131 goto leave;
132
Willy Tarreauc76ae332012-07-12 15:32:13 +0200133 /* It may happen during the data phase that a handshake is
134 * enabled again (eg: SSL)
135 */
136 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
137 goto process_handshake;
138
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200139 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
140 /* still waiting for a connection to establish and nothing was
141 * attempted yet to probe the connection. Then let's retry the
142 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200143 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200144 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200145 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200146 }
147
Willy Tarreau2c6be842012-07-06 17:12:34 +0200148 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200149 /* The wake callback may be used to process a critical error and abort the
150 * connection. If so, we don't want to go further as the connection will
151 * have been released and the FD destroyed.
152 */
153 if ((conn->flags & CO_FL_WAKE_DATA) &&
154 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
155 conn->data->wake(conn) < 0)
156 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200157
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200158 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200159 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200160 conn->flags |= CO_FL_CONNECTED;
161
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200162 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100163 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200164
165 /* commit polling changes */
166 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200167 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200168}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200169
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200170/* Update polling on connection <c>'s file descriptor depending on its current
171 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
172 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
173 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200174 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200177{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200178 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200179
180 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100181 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 +0200182 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100183 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200184 }
185 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 +0200186 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100187 f |= CO_FL_CURR_RD_ENA;
188 }
189 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
190 fd_stop_recv(c->t.sock.fd);
191 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200193
194 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100195 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 +0200196 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100197 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200198 }
199 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 +0200200 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100201 f |= CO_FL_CURR_WR_ENA;
202 }
203 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
204 fd_stop_send(c->t.sock.fd);
205 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200206 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100207 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200208}
209
210/* Update polling on connection <c>'s file descriptor depending on its current
211 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
212 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
213 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200214 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200215 */
216void conn_update_sock_polling(struct connection *c)
217{
218 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200219
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200220 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100221 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 +0200222 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100223 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200224 }
225 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 +0200226 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100227 f |= CO_FL_CURR_RD_ENA;
228 }
229 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
230 fd_stop_recv(c->t.sock.fd);
231 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200232 }
233
234 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100235 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 +0200236 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100237 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200238 }
239 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 +0200240 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100241 f |= CO_FL_CURR_WR_ENA;
242 }
243 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
244 fd_stop_send(c->t.sock.fd);
245 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200246 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100247 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200248}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200249
250/* This handshake handler waits a PROXY protocol header at the beginning of the
251 * raw data stream. The header looks like this :
252 *
253 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
254 *
255 * There must be exactly one space between each field. Fields are :
256 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
257 * - SRC3 : layer 3 (eg: IP) source address in standard text form
258 * - DST3 : layer 3 (eg: IP) destination address in standard text form
259 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
260 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
261 *
262 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
263 *
264 * The header line is small and in all cases smaller than the smallest normal
265 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
266 * can safely use MSG_PEEK and avoid buffering.
267 *
268 * Once the data is fetched, the values are set in the connection's address
269 * fields, and data are removed from the socket's buffer. The function returns
270 * zero if it needs to wait for more data or if it fails, or 1 if it completed
271 * and removed itself.
272 */
273int conn_recv_proxy(struct connection *conn, int flag)
274{
275 char *line, *end;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200276
277 /* we might have been called just after an asynchronous shutr */
278 if (conn->flags & CO_FL_SOCK_RD_SH)
279 goto fail;
280
281 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100282 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
283 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200284 if (errno == EINTR)
285 continue;
286 if (errno == EAGAIN) {
287 conn_sock_poll_recv(conn);
288 return 0;
289 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100290 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200291 }
292 } while (0);
293
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100294 if (!trash.len) {
295 /* client shutdown */
296 conn->err_code = CO_ER_PRX_EMPTY;
297 goto fail;
298 }
299
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100300 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200301 goto missing;
302
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100303 line = trash.str;
304 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200305
306 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100307 if (strncmp(line, "PROXY ", 6) != 0) {
308 conn->err_code = CO_ER_PRX_NOT_HDR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200309 goto fail;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100310 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200311
312 line += 6;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100313 if (trash.len < 18) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200314 goto missing;
315
316 if (!memcmp(line, "TCP4 ", 5) != 0) {
317 u32 src3, dst3, sport, dport;
318
319 line += 5;
320
321 src3 = inetaddr_host_lim_ret(line, end, &line);
322 if (line == end)
323 goto missing;
324 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100325 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200326
327 dst3 = inetaddr_host_lim_ret(line, end, &line);
328 if (line == end)
329 goto missing;
330 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100331 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200332
333 sport = read_uint((const char **)&line, end);
334 if (line == end)
335 goto missing;
336 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100337 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200338
339 dport = read_uint((const char **)&line, end);
340 if (line > end - 2)
341 goto missing;
342 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100343 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200344 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100345 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200346
347 /* update the session's addresses and mark them set */
348 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
349 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
350 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
351
352 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
353 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
354 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
355 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
356 }
357 else if (!memcmp(line, "TCP6 ", 5) != 0) {
358 u32 sport, dport;
359 char *src_s;
360 char *dst_s, *sport_s, *dport_s;
361 struct in6_addr src3, dst3;
362
363 line += 5;
364
365 src_s = line;
366 dst_s = sport_s = dport_s = NULL;
367 while (1) {
368 if (line > end - 2) {
369 goto missing;
370 }
371 else if (*line == '\r') {
372 *line = 0;
373 line++;
374 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100375 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200376 break;
377 }
378
379 if (*line == ' ') {
380 *line = 0;
381 if (!dst_s)
382 dst_s = line + 1;
383 else if (!sport_s)
384 sport_s = line + 1;
385 else if (!dport_s)
386 dport_s = line + 1;
387 }
388 line++;
389 }
390
391 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100392 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200393
394 sport = read_uint((const char **)&sport_s,dport_s - 1);
395 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100396 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200397
398 dport = read_uint((const char **)&dport_s,line - 2);
399 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100400 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200401
402 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100403 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200404
405 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100406 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200407
408 /* update the session's addresses and mark them set */
409 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
410 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
411 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
412
413 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
414 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
415 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
416 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
417 }
418 else {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100419 /* The protocol does not match something known (TCP4/TCP6) */
420 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200421 goto fail;
422 }
423
424 /* remove the PROXY line from the request. For this we re-read the
425 * exact line at once. If we don't get the exact same result, we
426 * fail.
427 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100428 trash.len = line - trash.str;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200429 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100430 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200431 if (len2 < 0 && errno == EINTR)
432 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100433 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100434 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200435 } while (0);
436
437 conn->flags &= ~flag;
438 return 1;
439
440 missing:
441 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
442 * we have not read anything. Otherwise we need to fail because we won't
443 * be able to poll anymore.
444 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100445 conn->err_code = CO_ER_PRX_TRUNCATED;
446 goto fail;
447
448 bad_header:
449 /* This is not a valid proxy protocol header */
450 conn->err_code = CO_ER_PRX_BAD_HDR;
451 goto fail;
452
453 recv_abort:
454 conn->err_code = CO_ER_PRX_ABORT;
455 goto fail;
456
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200457 fail:
458 conn_sock_stop_both(conn);
459 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200460 return 0;
461}
462
463/* Makes a PROXY protocol line from the two addresses. The output is sent to
464 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
465 * It returns the number of bytes composing this line (including the trailing
466 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
467 * TCP6 and "UNKNOWN" formats.
468 */
469int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
470{
471 int ret = 0;
472
473 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
474 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
475 if (ret >= buf_len)
476 return 0;
477
478 /* IPv4 src */
479 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
480 return 0;
481
482 ret += strlen(buf + ret);
483 if (ret >= buf_len)
484 return 0;
485
486 buf[ret++] = ' ';
487
488 /* IPv4 dst */
489 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
490 return 0;
491
492 ret += strlen(buf + ret);
493 if (ret >= buf_len)
494 return 0;
495
496 /* source and destination ports */
497 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
498 ntohs(((struct sockaddr_in *)src)->sin_port),
499 ntohs(((struct sockaddr_in *)dst)->sin_port));
500 if (ret >= buf_len)
501 return 0;
502 }
503 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
504 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
505 if (ret >= buf_len)
506 return 0;
507
508 /* IPv6 src */
509 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
510 return 0;
511
512 ret += strlen(buf + ret);
513 if (ret >= buf_len)
514 return 0;
515
516 buf[ret++] = ' ';
517
518 /* IPv6 dst */
519 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
520 return 0;
521
522 ret += strlen(buf + ret);
523 if (ret >= buf_len)
524 return 0;
525
526 /* source and destination ports */
527 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
528 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
529 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
530 if (ret >= buf_len)
531 return 0;
532 }
533 else {
534 /* unknown family combination */
535 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
536 if (ret >= buf_len)
537 return 0;
538 }
539 return ret;
540}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200541
542/* This callback is used to send a valid PROXY protocol line to a socket being
543 * established from the local machine. It sets the protocol addresses to the
544 * local and remote address. This is typically used with health checks or when
545 * it is not possible to determine the other end's address. It returns 0 if it
546 * fails in a fatal way or needs to poll to go further, otherwise it returns
547 * non-zero and removes itself from the connection's flags (the bit is provided
548 * in <flag> by the caller). It is designed to be called by the connection
549 * handler and relies on it to commit polling changes. Note that this function
550 * expects to be able to send the whole line at once, which should always be
551 * possible since it is supposed to start at the first byte of the outgoing
552 * data segment.
553 */
554int conn_local_send_proxy(struct connection *conn, unsigned int flag)
555{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100556 int ret;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200557
558 /* we might have been called just after an asynchronous shutw */
559 if (conn->flags & CO_FL_SOCK_WR_SH)
560 goto out_error;
561
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100562 /* The target server expects a PROXY line to be sent first. Retrieving
563 * local or remote addresses may fail until the connection is established.
564 */
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200565 conn_get_from_addr(conn);
566 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100567 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200568
569 conn_get_to_addr(conn);
570 if (!(conn->flags & CO_FL_ADDR_TO_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100571 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200572
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100573 trash.len = make_proxy_line(trash.str, trash.size, &conn->addr.from, &conn->addr.to);
574 if (!trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200575 goto out_error;
576
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100577 /* we have to send the whole trash. If the data layer has a
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200578 * pending write, we'll also set MSG_MORE.
579 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100580 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 +0200581
582 if (ret == 0)
583 goto out_wait;
584
585 if (ret < 0) {
586 if (errno == EAGAIN)
587 goto out_wait;
588 goto out_error;
589 }
590
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100591 if (ret != trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200592 goto out_error;
593
594 /* The connection is ready now, simply return and let the connection
595 * handler notify upper layers if needed.
596 */
597 if (conn->flags & CO_FL_WAIT_L4_CONN)
598 conn->flags &= ~CO_FL_WAIT_L4_CONN;
599 conn->flags &= ~flag;
600 return 1;
601
602 out_error:
603 /* Write error on the file descriptor */
604 conn->flags |= CO_FL_ERROR;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200605 return 0;
606
607 out_wait:
608 __conn_sock_stop_recv(conn);
609 __conn_sock_poll_send(conn);
610 return 0;
611}