blob: f86afde816114a0301c9a58a1e416738aba0fa0d [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 Tarreau9a92cd52012-11-24 11:12:13 +010052 if (unlikely(conn->flags & CO_FL_ERROR))
53 goto leave;
Willy Tarreaue9dfa792012-09-01 17:26:16 +020054
Willy Tarreauc76ae332012-07-12 15:32:13 +020055 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020056 /* The handshake callbacks are called in sequence. If either of them is
57 * missing something, it must enable the required polling at the socket
58 * layer of the connection. Polling state is not guaranteed when entering
59 * these handlers, so any handshake handler which does not complete its
60 * work must explicitly disable events it's not interested in.
61 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020062 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020063 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020064 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020065
Willy Tarreau22cda212012-08-31 17:43:29 +020066 if (conn->flags & CO_FL_ACCEPT_PROXY)
67 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
68 goto leave;
69
Willy Tarreauc76ae332012-07-12 15:32:13 +020070 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020071 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020072 goto leave;
Willy Tarreau5f1504f2012-10-04 23:55:57 +020073
74 if (conn->flags & CO_FL_LOCAL_SPROXY)
75 if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY))
76 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020077#ifdef USE_OPENSSL
78 if (conn->flags & CO_FL_SSL_WAIT_HS)
79 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
80 goto leave;
81#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020082 }
83
Willy Tarreauf9dabec2012-08-17 17:33:53 +020084 /* Once we're purely in the data phase, we disable handshake polling */
85 if (!(conn->flags & CO_FL_POLL_SOCK))
86 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020087
Willy Tarreau071e1372012-10-03 01:39:48 +020088 /* The data layer might not be ready yet (eg: when using embryonic
89 * sessions). If we're about to move data, we must initialize it first.
90 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020091 * we must not use it anymore and should immediately leave instead.
92 */
Willy Tarreau071e1372012-10-03 01:39:48 +020093 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020094 return 0;
95
Willy Tarreau153c3ca2012-10-22 22:47:55 +020096 /* The data transfer starts here and stops on error and handshakes. Note
97 * that we must absolutely test conn->xprt at each step in case it suddenly
98 * changes due to a quick unexpected close().
99 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200100 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200101 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200102 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200103 /* force detection of a flag change : it's impossible to have both
104 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200105 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200106 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200107 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200108 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200109
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200110 if ((fdtab[fd].ev & (FD_POLL_OUT | 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_WR|CO_FL_WAIT_DATA|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->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200118 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200119
Willy Tarreauc76ae332012-07-12 15:32:13 +0200120 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200121 goto leave;
122
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 */
126 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
127 goto process_handshake;
128
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200129 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
130 /* 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 }
137
Willy Tarreau2c6be842012-07-06 17:12:34 +0200138 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200139 /* The wake callback may be used to process a critical error and abort the
140 * connection. If so, we don't want to go further as the connection will
141 * have been released and the FD destroyed.
142 */
143 if ((conn->flags & CO_FL_WAKE_DATA) &&
144 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
145 conn->data->wake(conn) < 0)
146 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200147
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200148 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200149 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200150 conn->flags |= CO_FL_CONNECTED;
151
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200152 /* remove the events before leaving */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100153 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200154
155 /* commit polling changes */
156 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200157 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200158}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200160/* Update polling on connection <c>'s file descriptor depending on its current
161 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
162 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
163 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200164 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200166void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200167{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200168 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200169
170 /* update read status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100171 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 +0200172 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100173 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200174 }
175 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 +0200176 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100177 f |= CO_FL_CURR_RD_ENA;
178 }
179 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
180 fd_stop_recv(c->t.sock.fd);
181 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200182 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200183
184 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100185 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 +0200186 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100187 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200188 }
189 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 +0200190 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100191 f |= CO_FL_CURR_WR_ENA;
192 }
193 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
194 fd_stop_send(c->t.sock.fd);
195 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200196 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100197 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200198}
199
200/* Update polling on connection <c>'s file descriptor depending on its current
201 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
202 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
203 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200204 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200205 */
206void conn_update_sock_polling(struct connection *c)
207{
208 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200209
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 Tarreaue9dfa792012-09-01 17:26:16 +0200212 fd_poll_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100213 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200214 }
215 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 +0200216 fd_want_recv(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100217 f |= CO_FL_CURR_RD_ENA;
218 }
219 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
220 fd_stop_recv(c->t.sock.fd);
221 f &= ~CO_FL_CURR_RD_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200222 }
223
224 /* update write status if needed */
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100225 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 +0200226 fd_poll_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100227 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200228 }
229 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 +0200230 fd_want_send(c->t.sock.fd);
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100231 f |= CO_FL_CURR_WR_ENA;
232 }
233 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
234 fd_stop_send(c->t.sock.fd);
235 f &= ~CO_FL_CURR_WR_ENA;
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200236 }
Willy Tarreauc9f78042012-11-05 20:00:43 +0100237 c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200238}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200239
240/* This handshake handler waits a PROXY protocol header at the beginning of the
241 * raw data stream. The header looks like this :
242 *
243 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
244 *
245 * There must be exactly one space between each field. Fields are :
246 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
247 * - SRC3 : layer 3 (eg: IP) source address in standard text form
248 * - DST3 : layer 3 (eg: IP) destination address in standard text form
249 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
250 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
251 *
252 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
253 *
254 * The header line is small and in all cases smaller than the smallest normal
255 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
256 * can safely use MSG_PEEK and avoid buffering.
257 *
258 * Once the data is fetched, the values are set in the connection's address
259 * fields, and data are removed from the socket's buffer. The function returns
260 * zero if it needs to wait for more data or if it fails, or 1 if it completed
261 * and removed itself.
262 */
263int conn_recv_proxy(struct connection *conn, int flag)
264{
265 char *line, *end;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200266
267 /* we might have been called just after an asynchronous shutr */
268 if (conn->flags & CO_FL_SOCK_RD_SH)
269 goto fail;
270
271 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100272 trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK);
273 if (trash.len < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200274 if (errno == EINTR)
275 continue;
276 if (errno == EAGAIN) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100277 __conn_sock_poll_recv(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200278 return 0;
279 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100280 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200281 }
282 } while (0);
283
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100284 if (!trash.len) {
285 /* client shutdown */
286 conn->err_code = CO_ER_PRX_EMPTY;
287 goto fail;
288 }
289
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100290 if (trash.len < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200291 goto missing;
292
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100293 line = trash.str;
294 end = trash.str + trash.len;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200295
296 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100297 if (strncmp(line, "PROXY ", 6) != 0) {
298 conn->err_code = CO_ER_PRX_NOT_HDR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200299 goto fail;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100300 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200301
302 line += 6;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100303 if (trash.len < 18) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200304 goto missing;
305
306 if (!memcmp(line, "TCP4 ", 5) != 0) {
307 u32 src3, dst3, sport, dport;
308
309 line += 5;
310
311 src3 = inetaddr_host_lim_ret(line, end, &line);
312 if (line == end)
313 goto missing;
314 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100315 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200316
317 dst3 = inetaddr_host_lim_ret(line, end, &line);
318 if (line == end)
319 goto missing;
320 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100321 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200322
323 sport = read_uint((const char **)&line, end);
324 if (line == end)
325 goto missing;
326 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100327 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200328
329 dport = read_uint((const char **)&line, end);
330 if (line > end - 2)
331 goto missing;
332 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100333 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200334 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100335 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200336
337 /* update the session's addresses and mark them set */
338 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
339 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
340 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
341
342 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
343 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
344 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
345 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
346 }
347 else if (!memcmp(line, "TCP6 ", 5) != 0) {
348 u32 sport, dport;
349 char *src_s;
350 char *dst_s, *sport_s, *dport_s;
351 struct in6_addr src3, dst3;
352
353 line += 5;
354
355 src_s = line;
356 dst_s = sport_s = dport_s = NULL;
357 while (1) {
358 if (line > end - 2) {
359 goto missing;
360 }
361 else if (*line == '\r') {
362 *line = 0;
363 line++;
364 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100365 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200366 break;
367 }
368
369 if (*line == ' ') {
370 *line = 0;
371 if (!dst_s)
372 dst_s = line + 1;
373 else if (!sport_s)
374 sport_s = line + 1;
375 else if (!dport_s)
376 dport_s = line + 1;
377 }
378 line++;
379 }
380
381 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100382 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200383
384 sport = read_uint((const char **)&sport_s,dport_s - 1);
385 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100386 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200387
388 dport = read_uint((const char **)&dport_s,line - 2);
389 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100390 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200391
392 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100393 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200394
395 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100396 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200397
398 /* update the session's addresses and mark them set */
399 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
400 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
401 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
402
403 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
404 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
405 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
406 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
407 }
408 else {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100409 /* The protocol does not match something known (TCP4/TCP6) */
410 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200411 goto fail;
412 }
413
414 /* remove the PROXY line from the request. For this we re-read the
415 * exact line at once. If we don't get the exact same result, we
416 * fail.
417 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100418 trash.len = line - trash.str;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200419 do {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100420 int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200421 if (len2 < 0 && errno == EINTR)
422 continue;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100423 if (len2 != trash.len)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100424 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200425 } while (0);
426
427 conn->flags &= ~flag;
428 return 1;
429
430 missing:
431 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
432 * we have not read anything. Otherwise we need to fail because we won't
433 * be able to poll anymore.
434 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100435 conn->err_code = CO_ER_PRX_TRUNCATED;
436 goto fail;
437
438 bad_header:
439 /* This is not a valid proxy protocol header */
440 conn->err_code = CO_ER_PRX_BAD_HDR;
441 goto fail;
442
443 recv_abort:
444 conn->err_code = CO_ER_PRX_ABORT;
445 goto fail;
446
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200447 fail:
Willy Tarreaud486ef52012-12-10 17:03:52 +0100448 __conn_sock_stop_both(conn);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200449 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200450 return 0;
451}
452
453/* Makes a PROXY protocol line from the two addresses. The output is sent to
454 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
455 * It returns the number of bytes composing this line (including the trailing
456 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
457 * TCP6 and "UNKNOWN" formats.
458 */
459int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
460{
461 int ret = 0;
462
463 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
464 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
465 if (ret >= buf_len)
466 return 0;
467
468 /* IPv4 src */
469 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
470 return 0;
471
472 ret += strlen(buf + ret);
473 if (ret >= buf_len)
474 return 0;
475
476 buf[ret++] = ' ';
477
478 /* IPv4 dst */
479 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->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 /* source and destination ports */
487 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
488 ntohs(((struct sockaddr_in *)src)->sin_port),
489 ntohs(((struct sockaddr_in *)dst)->sin_port));
490 if (ret >= buf_len)
491 return 0;
492 }
493 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
494 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
495 if (ret >= buf_len)
496 return 0;
497
498 /* IPv6 src */
499 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
500 return 0;
501
502 ret += strlen(buf + ret);
503 if (ret >= buf_len)
504 return 0;
505
506 buf[ret++] = ' ';
507
508 /* IPv6 dst */
509 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->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 /* source and destination ports */
517 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
518 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
519 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
520 if (ret >= buf_len)
521 return 0;
522 }
523 else {
524 /* unknown family combination */
525 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
526 if (ret >= buf_len)
527 return 0;
528 }
529 return ret;
530}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200531
532/* This callback is used to send a valid PROXY protocol line to a socket being
533 * established from the local machine. It sets the protocol addresses to the
534 * local and remote address. This is typically used with health checks or when
535 * it is not possible to determine the other end's address. It returns 0 if it
536 * fails in a fatal way or needs to poll to go further, otherwise it returns
537 * non-zero and removes itself from the connection's flags (the bit is provided
538 * in <flag> by the caller). It is designed to be called by the connection
539 * handler and relies on it to commit polling changes. Note that this function
540 * expects to be able to send the whole line at once, which should always be
541 * possible since it is supposed to start at the first byte of the outgoing
542 * data segment.
543 */
544int conn_local_send_proxy(struct connection *conn, unsigned int flag)
545{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100546 int ret;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200547
548 /* we might have been called just after an asynchronous shutw */
549 if (conn->flags & CO_FL_SOCK_WR_SH)
550 goto out_error;
551
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100552 /* The target server expects a PROXY line to be sent first. Retrieving
553 * local or remote addresses may fail until the connection is established.
554 */
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200555 conn_get_from_addr(conn);
556 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100557 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200558
559 conn_get_to_addr(conn);
560 if (!(conn->flags & CO_FL_ADDR_TO_SET))
Willy Tarreaue3635ed2012-11-24 11:23:04 +0100561 goto out_wait;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200562
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100563 trash.len = make_proxy_line(trash.str, trash.size, &conn->addr.from, &conn->addr.to);
564 if (!trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200565 goto out_error;
566
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100567 /* we have to send the whole trash. If the data layer has a
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200568 * pending write, we'll also set MSG_MORE.
569 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100570 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 +0200571
572 if (ret == 0)
573 goto out_wait;
574
575 if (ret < 0) {
576 if (errno == EAGAIN)
577 goto out_wait;
578 goto out_error;
579 }
580
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100581 if (ret != trash.len)
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200582 goto out_error;
583
584 /* The connection is ready now, simply return and let the connection
585 * handler notify upper layers if needed.
586 */
587 if (conn->flags & CO_FL_WAIT_L4_CONN)
588 conn->flags &= ~CO_FL_WAIT_L4_CONN;
589 conn->flags &= ~flag;
590 return 1;
591
592 out_error:
593 /* Write error on the file descriptor */
594 conn->flags |= CO_FL_ERROR;
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200595 return 0;
596
597 out_wait:
598 __conn_sock_stop_recv(conn);
599 __conn_sock_poll_send(conn);
600 return 0;
601}