blob: 08b53ae3e30d3b149dde29adeeaaa9b660bd19f2 [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 Tarreau59f98392012-07-06 14:13:49 +020029/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020030 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020031 */
32int conn_fd_handler(int fd)
33{
Willy Tarreau80184712012-07-06 14:54:49 +020034 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020035 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020036
Willy Tarreauc76ae332012-07-12 15:32:13 +020037 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020038 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020039
Willy Tarreaue9dfa792012-09-01 17:26:16 +020040 /* before engaging there, we clear the new WAIT_* flags so that we can
41 * more easily detect an EAGAIN condition from anywhere.
42 */
Willy Tarreau9e272bf2012-10-03 21:04:48 +020043 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 +020044
Willy Tarreauc76ae332012-07-12 15:32:13 +020045 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020046 /* The handshake callbacks are called in sequence. If either of them is
47 * missing something, it must enable the required polling at the socket
48 * layer of the connection. Polling state is not guaranteed when entering
49 * these handlers, so any handshake handler which does not complete its
50 * work must explicitly disable events it's not interested in.
51 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020052 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020053 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020054 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020055
Willy Tarreau22cda212012-08-31 17:43:29 +020056 if (conn->flags & CO_FL_ACCEPT_PROXY)
57 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
58 goto leave;
59
Willy Tarreauc76ae332012-07-12 15:32:13 +020060 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020061 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020062 goto leave;
Willy Tarreau5f1504f2012-10-04 23:55:57 +020063
64 if (conn->flags & CO_FL_LOCAL_SPROXY)
65 if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY))
66 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020067#ifdef USE_OPENSSL
68 if (conn->flags & CO_FL_SSL_WAIT_HS)
69 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
70 goto leave;
71#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020072 }
73
Willy Tarreauf9dabec2012-08-17 17:33:53 +020074 /* Once we're purely in the data phase, we disable handshake polling */
75 if (!(conn->flags & CO_FL_POLL_SOCK))
76 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020077
Willy Tarreau071e1372012-10-03 01:39:48 +020078 /* The data layer might not be ready yet (eg: when using embryonic
79 * sessions). If we're about to move data, we must initialize it first.
80 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020081 * we must not use it anymore and should immediately leave instead.
82 */
Willy Tarreau071e1372012-10-03 01:39:48 +020083 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020084 return 0;
85
Willy Tarreau153c3ca2012-10-22 22:47:55 +020086 /* The data transfer starts here and stops on error and handshakes. Note
87 * that we must absolutely test conn->xprt at each step in case it suddenly
88 * changes due to a quick unexpected close().
89 */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020090 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau153c3ca2012-10-22 22:47:55 +020091 conn->xprt &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020092 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +020093 /* force detection of a flag change : it's impossible to have both
94 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020095 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +020096 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +020097 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020098 }
Willy Tarreau59f98392012-07-06 14:13:49 +020099
Willy Tarreaud9de7ca2012-09-02 18:48:46 +0200100 if ((fdtab[fd].ev & (FD_POLL_OUT | 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_WR|CO_FL_WAIT_DATA|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->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200108 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200109
Willy Tarreauc76ae332012-07-12 15:32:13 +0200110 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200111 goto leave;
112
Willy Tarreauc76ae332012-07-12 15:32:13 +0200113 /* It may happen during the data phase that a handshake is
114 * enabled again (eg: SSL)
115 */
116 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
117 goto process_handshake;
118
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200119 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
120 /* still waiting for a connection to establish and nothing was
121 * attempted yet to probe the connection. Then let's retry the
122 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200123 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200124 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200125 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200126 }
127
Willy Tarreau2c6be842012-07-06 17:12:34 +0200128 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200129 /* The wake callback may be used to process a critical error and abort the
130 * connection. If so, we don't want to go further as the connection will
131 * have been released and the FD destroyed.
132 */
133 if ((conn->flags & CO_FL_WAKE_DATA) &&
134 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
135 conn->data->wake(conn) < 0)
136 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200137
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200138 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200139 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200140 conn->flags |= CO_FL_CONNECTED;
141
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200142 /* remove the events before leaving */
143 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200144
145 /* commit polling changes */
146 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200147 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200148}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200149
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200150/* Update polling on connection <c>'s file descriptor depending on its current
151 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
152 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
153 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200154 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200155 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200156void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200157{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200158 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200159
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200160 if (unlikely(f & CO_FL_ERROR)) {
161 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
162 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
163 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
164 fd_stop_both(c->t.sock.fd);
165 return;
166 }
167
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
170 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
171 fd_stop_recv(c->t.sock.fd);
172 }
173 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
174 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
175 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200176 fd_poll_recv(c->t.sock.fd);
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)) {
179 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200180 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200181 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200182
183 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200184 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
185 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
186 fd_stop_send(c->t.sock.fd);
187 }
188 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
189 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
190 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200191 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192 }
193 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
194 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200195 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200196 }
197 c->flags = f;
198}
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 Tarreau0ffde2c2012-10-04 22:21:15 +0200210 if (unlikely(f & CO_FL_ERROR)) {
211 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
212 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
213 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
214 fd_stop_both(c->t.sock.fd);
215 return;
216 }
217
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200218 /* update read status if needed */
219 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
220 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
221 fd_stop_recv(c->t.sock.fd);
222 }
223 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
224 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
225 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
226 fd_poll_recv(c->t.sock.fd);
227 }
228 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
229 f |= CO_FL_CURR_RD_ENA;
230 fd_want_recv(c->t.sock.fd);
231 }
232
233 /* update write status if needed */
234 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
235 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
236 fd_stop_send(c->t.sock.fd);
237 }
238 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
239 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
240 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
241 fd_poll_send(c->t.sock.fd);
242 }
243 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
244 f |= CO_FL_CURR_WR_ENA;
245 fd_want_send(c->t.sock.fd);
246 }
247 c->flags = f;
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;
276 int len;
277
278 /* we might have been called just after an asynchronous shutr */
279 if (conn->flags & CO_FL_SOCK_RD_SH)
280 goto fail;
281
282 do {
283 len = recv(conn->t.sock.fd, trash, trashlen, MSG_PEEK);
284 if (len < 0) {
285 if (errno == EINTR)
286 continue;
287 if (errno == EAGAIN) {
288 conn_sock_poll_recv(conn);
289 return 0;
290 }
291 goto fail;
292 }
293 } while (0);
294
295 if (len < 6)
296 goto missing;
297
298 line = trash;
299 end = trash + len;
300
301 /* Decode a possible proxy request, fail early if it does not match */
302 if (strncmp(line, "PROXY ", 6) != 0)
303 goto fail;
304
305 line += 6;
306 if (len < 18) /* shortest possible line */
307 goto missing;
308
309 if (!memcmp(line, "TCP4 ", 5) != 0) {
310 u32 src3, dst3, sport, dport;
311
312 line += 5;
313
314 src3 = inetaddr_host_lim_ret(line, end, &line);
315 if (line == end)
316 goto missing;
317 if (*line++ != ' ')
318 goto fail;
319
320 dst3 = inetaddr_host_lim_ret(line, end, &line);
321 if (line == end)
322 goto missing;
323 if (*line++ != ' ')
324 goto fail;
325
326 sport = read_uint((const char **)&line, end);
327 if (line == end)
328 goto missing;
329 if (*line++ != ' ')
330 goto fail;
331
332 dport = read_uint((const char **)&line, end);
333 if (line > end - 2)
334 goto missing;
335 if (*line++ != '\r')
336 goto fail;
337 if (*line++ != '\n')
338 goto fail;
339
340 /* update the session's addresses and mark them set */
341 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
342 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
343 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
344
345 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
346 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
347 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
348 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
349 }
350 else if (!memcmp(line, "TCP6 ", 5) != 0) {
351 u32 sport, dport;
352 char *src_s;
353 char *dst_s, *sport_s, *dport_s;
354 struct in6_addr src3, dst3;
355
356 line += 5;
357
358 src_s = line;
359 dst_s = sport_s = dport_s = NULL;
360 while (1) {
361 if (line > end - 2) {
362 goto missing;
363 }
364 else if (*line == '\r') {
365 *line = 0;
366 line++;
367 if (*line++ != '\n')
368 goto fail;
369 break;
370 }
371
372 if (*line == ' ') {
373 *line = 0;
374 if (!dst_s)
375 dst_s = line + 1;
376 else if (!sport_s)
377 sport_s = line + 1;
378 else if (!dport_s)
379 dport_s = line + 1;
380 }
381 line++;
382 }
383
384 if (!dst_s || !sport_s || !dport_s)
385 goto fail;
386
387 sport = read_uint((const char **)&sport_s,dport_s - 1);
388 if (*sport_s != 0)
389 goto fail;
390
391 dport = read_uint((const char **)&dport_s,line - 2);
392 if (*dport_s != 0)
393 goto fail;
394
395 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
396 goto fail;
397
398 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
399 goto fail;
400
401 /* update the session's addresses and mark them set */
402 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
403 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
404 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
405
406 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
407 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
408 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
409 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
410 }
411 else {
412 goto fail;
413 }
414
415 /* remove the PROXY line from the request. For this we re-read the
416 * exact line at once. If we don't get the exact same result, we
417 * fail.
418 */
419 len = line - trash;
420 do {
421 int len2 = recv(conn->t.sock.fd, trash, len, 0);
422 if (len2 < 0 && errno == EINTR)
423 continue;
424 if (len2 != len)
425 goto fail;
426 } while (0);
427
428 conn->flags &= ~flag;
429 return 1;
430
431 missing:
432 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
433 * we have not read anything. Otherwise we need to fail because we won't
434 * be able to poll anymore.
435 */
436 fail:
437 conn_sock_stop_both(conn);
438 conn->flags |= CO_FL_ERROR;
439 conn->flags &= ~flag;
440 return 0;
441}
442
443/* Makes a PROXY protocol line from the two addresses. The output is sent to
444 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
445 * It returns the number of bytes composing this line (including the trailing
446 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
447 * TCP6 and "UNKNOWN" formats.
448 */
449int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
450{
451 int ret = 0;
452
453 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
454 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
455 if (ret >= buf_len)
456 return 0;
457
458 /* IPv4 src */
459 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
460 return 0;
461
462 ret += strlen(buf + ret);
463 if (ret >= buf_len)
464 return 0;
465
466 buf[ret++] = ' ';
467
468 /* IPv4 dst */
469 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->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 /* source and destination ports */
477 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
478 ntohs(((struct sockaddr_in *)src)->sin_port),
479 ntohs(((struct sockaddr_in *)dst)->sin_port));
480 if (ret >= buf_len)
481 return 0;
482 }
483 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
484 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
485 if (ret >= buf_len)
486 return 0;
487
488 /* IPv6 src */
489 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_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 buf[ret++] = ' ';
497
498 /* IPv6 dst */
499 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->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 /* source and destination ports */
507 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
508 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
509 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
510 if (ret >= buf_len)
511 return 0;
512 }
513 else {
514 /* unknown family combination */
515 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
516 if (ret >= buf_len)
517 return 0;
518 }
519 return ret;
520}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200521
522/* This callback is used to send a valid PROXY protocol line to a socket being
523 * established from the local machine. It sets the protocol addresses to the
524 * local and remote address. This is typically used with health checks or when
525 * it is not possible to determine the other end's address. It returns 0 if it
526 * fails in a fatal way or needs to poll to go further, otherwise it returns
527 * non-zero and removes itself from the connection's flags (the bit is provided
528 * in <flag> by the caller). It is designed to be called by the connection
529 * handler and relies on it to commit polling changes. Note that this function
530 * expects to be able to send the whole line at once, which should always be
531 * possible since it is supposed to start at the first byte of the outgoing
532 * data segment.
533 */
534int conn_local_send_proxy(struct connection *conn, unsigned int flag)
535{
536 int ret, len;
537
538 /* we might have been called just after an asynchronous shutw */
539 if (conn->flags & CO_FL_SOCK_WR_SH)
540 goto out_error;
541
542 /* The target server expects a PROXY line to be sent first. */
543 conn_get_from_addr(conn);
544 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
545 goto out_error;
546
547 conn_get_to_addr(conn);
548 if (!(conn->flags & CO_FL_ADDR_TO_SET))
549 goto out_error;
550
551 len = make_proxy_line(trash, trashlen, &conn->addr.from, &conn->addr.to);
552 if (!len)
553 goto out_error;
554
555 /* we have to send trash from len bytes. If the data layer has a
556 * pending write, we'll also set MSG_MORE.
557 */
558 ret = send(conn->t.sock.fd, trash, len, (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0);
559
560 if (ret == 0)
561 goto out_wait;
562
563 if (ret < 0) {
564 if (errno == EAGAIN)
565 goto out_wait;
566 goto out_error;
567 }
568
569 if (ret != len)
570 goto out_error;
571
572 /* The connection is ready now, simply return and let the connection
573 * handler notify upper layers if needed.
574 */
575 if (conn->flags & CO_FL_WAIT_L4_CONN)
576 conn->flags &= ~CO_FL_WAIT_L4_CONN;
577 conn->flags &= ~flag;
578 return 1;
579
580 out_error:
581 /* Write error on the file descriptor */
582 conn->flags |= CO_FL_ERROR;
583 conn->flags &= ~flag;
584 return 0;
585
586 out_wait:
587 __conn_sock_stop_recv(conn);
588 __conn_sock_poll_send(conn);
589 return 0;
590}