blob: 9ccebbc9c428507eb9b37981037383ec5e7e4cc3 [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 Tarreaue9dfa792012-09-01 17:26:16 +020053
Willy Tarreauc76ae332012-07-12 15:32:13 +020054 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020055 /* The handshake callbacks are called in sequence. If either of them is
56 * missing something, it must enable the required polling at the socket
57 * layer of the connection. Polling state is not guaranteed when entering
58 * these handlers, so any handshake handler which does not complete its
59 * work must explicitly disable events it's not interested in.
60 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020061 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
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 Tarreauc76ae332012-07-12 15:32:13 +020069 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020070 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020071 goto leave;
Willy Tarreau5f1504f2012-10-04 23:55:57 +020072
73 if (conn->flags & CO_FL_LOCAL_SPROXY)
74 if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY))
75 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020076#ifdef USE_OPENSSL
77 if (conn->flags & CO_FL_SSL_WAIT_HS)
78 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
79 goto leave;
80#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020081 }
82
Willy Tarreauf9dabec2012-08-17 17:33:53 +020083 /* Once we're purely in the data phase, we disable handshake polling */
84 if (!(conn->flags & CO_FL_POLL_SOCK))
85 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020086
Willy Tarreau071e1372012-10-03 01:39:48 +020087 /* The data layer might not be ready yet (eg: when using embryonic
88 * sessions). If we're about to move data, we must initialize it first.
89 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020090 * we must not use it anymore and should immediately leave instead.
91 */
Willy Tarreau071e1372012-10-03 01:39:48 +020092 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020093 return 0;
94
Willy Tarreau153c3ca2012-10-22 22:47:55 +020095 /* The data transfer starts here and stops on error and handshakes. Note
96 * that we must absolutely test conn->xprt at each step in case it suddenly
97 * changes due to a quick unexpected close().
98 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020099 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200100 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200101 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200102 /* force detection of a flag change : it's impossible to have both
103 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200104 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200105 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200106 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200107 }
Willy Tarreau59f98392012-07-06 14:13:49 +0200108
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200109 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +0200110 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200111 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200112 /* force detection of a flag change : it's impossible to have both
113 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200114 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200115 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200116 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200117 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200118
Willy Tarreauc76ae332012-07-12 15:32:13 +0200119 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200120 goto leave;
121
Willy Tarreauc76ae332012-07-12 15:32:13 +0200122 /* It may happen during the data phase that a handshake is
123 * enabled again (eg: SSL)
124 */
125 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
126 goto process_handshake;
127
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200128 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
129 /* still waiting for a connection to establish and nothing was
130 * attempted yet to probe the connection. Then let's retry the
131 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200132 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200133 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200134 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200135 }
136
Willy Tarreau2c6be842012-07-06 17:12:34 +0200137 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200138 /* The wake callback may be used to process a critical error and abort the
139 * connection. If so, we don't want to go further as the connection will
140 * have been released and the FD destroyed.
141 */
142 if ((conn->flags & CO_FL_WAKE_DATA) &&
143 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
144 conn->data->wake(conn) < 0)
145 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200146
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200147 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200148 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200149 conn->flags |= CO_FL_CONNECTED;
150
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200151 /* remove the events before leaving */
152 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200153
154 /* commit polling changes */
155 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200156 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200157}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200158
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200159/* Update polling on connection <c>'s file descriptor depending on its current
160 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
161 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
162 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200163 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200165void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200166{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200167 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200169 if (unlikely(f & CO_FL_ERROR)) {
170 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
171 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
172 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
173 fd_stop_both(c->t.sock.fd);
174 return;
175 }
176
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200177 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200178 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
179 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
180 fd_stop_recv(c->t.sock.fd);
181 }
182 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
183 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
184 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200185 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200186 }
187 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
188 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200189 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200190 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200191
192 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200193 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
194 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
195 fd_stop_send(c->t.sock.fd);
196 }
197 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
198 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
199 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200200 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200201 }
202 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
203 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200204 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200205 }
206 c->flags = f;
207}
208
209/* Update polling on connection <c>'s file descriptor depending on its current
210 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
211 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
212 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200213 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200214 */
215void conn_update_sock_polling(struct connection *c)
216{
217 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200218
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200219 if (unlikely(f & CO_FL_ERROR)) {
220 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
221 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
222 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
223 fd_stop_both(c->t.sock.fd);
224 return;
225 }
226
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200227 /* update read status if needed */
228 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
229 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
230 fd_stop_recv(c->t.sock.fd);
231 }
232 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
233 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
234 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
235 fd_poll_recv(c->t.sock.fd);
236 }
237 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
238 f |= CO_FL_CURR_RD_ENA;
239 fd_want_recv(c->t.sock.fd);
240 }
241
242 /* update write status if needed */
243 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
244 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
245 fd_stop_send(c->t.sock.fd);
246 }
247 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
248 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
249 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
250 fd_poll_send(c->t.sock.fd);
251 }
252 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
253 f |= CO_FL_CURR_WR_ENA;
254 fd_want_send(c->t.sock.fd);
255 }
256 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200257}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200258
259/* This handshake handler waits a PROXY protocol header at the beginning of the
260 * raw data stream. The header looks like this :
261 *
262 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
263 *
264 * There must be exactly one space between each field. Fields are :
265 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
266 * - SRC3 : layer 3 (eg: IP) source address in standard text form
267 * - DST3 : layer 3 (eg: IP) destination address in standard text form
268 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
269 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
270 *
271 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
272 *
273 * The header line is small and in all cases smaller than the smallest normal
274 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
275 * can safely use MSG_PEEK and avoid buffering.
276 *
277 * Once the data is fetched, the values are set in the connection's address
278 * fields, and data are removed from the socket's buffer. The function returns
279 * zero if it needs to wait for more data or if it fails, or 1 if it completed
280 * and removed itself.
281 */
282int conn_recv_proxy(struct connection *conn, int flag)
283{
284 char *line, *end;
285 int len;
286
287 /* we might have been called just after an asynchronous shutr */
288 if (conn->flags & CO_FL_SOCK_RD_SH)
289 goto fail;
290
291 do {
Willy Tarreauc919dc62012-10-26 17:35:22 +0200292 len = recv(conn->t.sock.fd, trash, global.tune.bufsize, MSG_PEEK);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200293 if (len < 0) {
294 if (errno == EINTR)
295 continue;
296 if (errno == EAGAIN) {
297 conn_sock_poll_recv(conn);
298 return 0;
299 }
300 goto fail;
301 }
302 } while (0);
303
304 if (len < 6)
305 goto missing;
306
307 line = trash;
308 end = trash + len;
309
310 /* Decode a possible proxy request, fail early if it does not match */
311 if (strncmp(line, "PROXY ", 6) != 0)
312 goto fail;
313
314 line += 6;
315 if (len < 18) /* shortest possible line */
316 goto missing;
317
318 if (!memcmp(line, "TCP4 ", 5) != 0) {
319 u32 src3, dst3, sport, dport;
320
321 line += 5;
322
323 src3 = inetaddr_host_lim_ret(line, end, &line);
324 if (line == end)
325 goto missing;
326 if (*line++ != ' ')
327 goto fail;
328
329 dst3 = inetaddr_host_lim_ret(line, end, &line);
330 if (line == end)
331 goto missing;
332 if (*line++ != ' ')
333 goto fail;
334
335 sport = read_uint((const char **)&line, end);
336 if (line == end)
337 goto missing;
338 if (*line++ != ' ')
339 goto fail;
340
341 dport = read_uint((const char **)&line, end);
342 if (line > end - 2)
343 goto missing;
344 if (*line++ != '\r')
345 goto fail;
346 if (*line++ != '\n')
347 goto fail;
348
349 /* update the session's addresses and mark them set */
350 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
351 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
352 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
353
354 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
355 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
356 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
357 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
358 }
359 else if (!memcmp(line, "TCP6 ", 5) != 0) {
360 u32 sport, dport;
361 char *src_s;
362 char *dst_s, *sport_s, *dport_s;
363 struct in6_addr src3, dst3;
364
365 line += 5;
366
367 src_s = line;
368 dst_s = sport_s = dport_s = NULL;
369 while (1) {
370 if (line > end - 2) {
371 goto missing;
372 }
373 else if (*line == '\r') {
374 *line = 0;
375 line++;
376 if (*line++ != '\n')
377 goto fail;
378 break;
379 }
380
381 if (*line == ' ') {
382 *line = 0;
383 if (!dst_s)
384 dst_s = line + 1;
385 else if (!sport_s)
386 sport_s = line + 1;
387 else if (!dport_s)
388 dport_s = line + 1;
389 }
390 line++;
391 }
392
393 if (!dst_s || !sport_s || !dport_s)
394 goto fail;
395
396 sport = read_uint((const char **)&sport_s,dport_s - 1);
397 if (*sport_s != 0)
398 goto fail;
399
400 dport = read_uint((const char **)&dport_s,line - 2);
401 if (*dport_s != 0)
402 goto fail;
403
404 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
405 goto fail;
406
407 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
408 goto fail;
409
410 /* update the session's addresses and mark them set */
411 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
412 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
413 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
414
415 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
416 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
417 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
418 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
419 }
420 else {
421 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 */
428 len = line - trash;
429 do {
430 int len2 = recv(conn->t.sock.fd, trash, len, 0);
431 if (len2 < 0 && errno == EINTR)
432 continue;
433 if (len2 != len)
434 goto fail;
435 } 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 */
445 fail:
446 conn_sock_stop_both(conn);
447 conn->flags |= CO_FL_ERROR;
448 conn->flags &= ~flag;
449 return 0;
450}
451
452/* Makes a PROXY protocol line from the two addresses. The output is sent to
453 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
454 * It returns the number of bytes composing this line (including the trailing
455 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
456 * TCP6 and "UNKNOWN" formats.
457 */
458int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
459{
460 int ret = 0;
461
462 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
463 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
464 if (ret >= buf_len)
465 return 0;
466
467 /* IPv4 src */
468 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
469 return 0;
470
471 ret += strlen(buf + ret);
472 if (ret >= buf_len)
473 return 0;
474
475 buf[ret++] = ' ';
476
477 /* IPv4 dst */
478 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
479 return 0;
480
481 ret += strlen(buf + ret);
482 if (ret >= buf_len)
483 return 0;
484
485 /* source and destination ports */
486 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
487 ntohs(((struct sockaddr_in *)src)->sin_port),
488 ntohs(((struct sockaddr_in *)dst)->sin_port));
489 if (ret >= buf_len)
490 return 0;
491 }
492 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
493 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
494 if (ret >= buf_len)
495 return 0;
496
497 /* IPv6 src */
498 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
499 return 0;
500
501 ret += strlen(buf + ret);
502 if (ret >= buf_len)
503 return 0;
504
505 buf[ret++] = ' ';
506
507 /* IPv6 dst */
508 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
509 return 0;
510
511 ret += strlen(buf + ret);
512 if (ret >= buf_len)
513 return 0;
514
515 /* source and destination ports */
516 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
517 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
518 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
519 if (ret >= buf_len)
520 return 0;
521 }
522 else {
523 /* unknown family combination */
524 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
525 if (ret >= buf_len)
526 return 0;
527 }
528 return ret;
529}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200530
531/* This callback is used to send a valid PROXY protocol line to a socket being
532 * established from the local machine. It sets the protocol addresses to the
533 * local and remote address. This is typically used with health checks or when
534 * it is not possible to determine the other end's address. It returns 0 if it
535 * fails in a fatal way or needs to poll to go further, otherwise it returns
536 * non-zero and removes itself from the connection's flags (the bit is provided
537 * in <flag> by the caller). It is designed to be called by the connection
538 * handler and relies on it to commit polling changes. Note that this function
539 * expects to be able to send the whole line at once, which should always be
540 * possible since it is supposed to start at the first byte of the outgoing
541 * data segment.
542 */
543int conn_local_send_proxy(struct connection *conn, unsigned int flag)
544{
545 int ret, len;
546
547 /* we might have been called just after an asynchronous shutw */
548 if (conn->flags & CO_FL_SOCK_WR_SH)
549 goto out_error;
550
551 /* The target server expects a PROXY line to be sent first. */
552 conn_get_from_addr(conn);
553 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
554 goto out_error;
555
556 conn_get_to_addr(conn);
557 if (!(conn->flags & CO_FL_ADDR_TO_SET))
558 goto out_error;
559
Willy Tarreauc919dc62012-10-26 17:35:22 +0200560 len = make_proxy_line(trash, global.tune.bufsize, &conn->addr.from, &conn->addr.to);
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200561 if (!len)
562 goto out_error;
563
564 /* we have to send trash from len bytes. If the data layer has a
565 * pending write, we'll also set MSG_MORE.
566 */
567 ret = send(conn->t.sock.fd, trash, len, (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0);
568
569 if (ret == 0)
570 goto out_wait;
571
572 if (ret < 0) {
573 if (errno == EAGAIN)
574 goto out_wait;
575 goto out_error;
576 }
577
578 if (ret != len)
579 goto out_error;
580
581 /* The connection is ready now, simply return and let the connection
582 * handler notify upper layers if needed.
583 */
584 if (conn->flags & CO_FL_WAIT_L4_CONN)
585 conn->flags &= ~CO_FL_WAIT_L4_CONN;
586 conn->flags &= ~flag;
587 return 1;
588
589 out_error:
590 /* Write error on the file descriptor */
591 conn->flags |= CO_FL_ERROR;
592 conn->flags &= ~flag;
593 return 0;
594
595 out_wait:
596 __conn_sock_stop_recv(conn);
597 __conn_sock_poll_send(conn);
598 return 0;
599}