blob: 732a7eaac36401bb7d42bc747560342e6701c415 [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 Tarreau58363cf2012-09-06 14:12:03 +020086 /* The data transfer starts here and stops on error and handshakes */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020087 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020088 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +020089 /* force detection of a flag change : it's impossible to have both
90 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +020091 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +020092 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +020093 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020094 }
Willy Tarreau59f98392012-07-06 14:13:49 +020095
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020096 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020097 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
Willy Tarreau3b5bc662012-10-05 21:29:37 +020098 /* force detection of a flag change : it's impossible to have both
99 * CONNECTED and WAIT_CONN so we're certain to trigger a change.
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200100 */
Willy Tarreau3b5bc662012-10-05 21:29:37 +0200101 flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED;
Willy Tarreau74beec32012-10-03 00:41:04 +0200102 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +0200103 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200104
Willy Tarreauc76ae332012-07-12 15:32:13 +0200105 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200106 goto leave;
107
Willy Tarreauc76ae332012-07-12 15:32:13 +0200108 /* It may happen during the data phase that a handshake is
109 * enabled again (eg: SSL)
110 */
111 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
112 goto process_handshake;
113
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200114 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
115 /* still waiting for a connection to establish and nothing was
116 * attempted yet to probe the connection. Then let's retry the
117 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200118 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200119 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200120 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200121 }
122
Willy Tarreau2c6be842012-07-06 17:12:34 +0200123 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200124 /* The wake callback may be used to process a critical error and abort the
125 * connection. If so, we don't want to go further as the connection will
126 * have been released and the FD destroyed.
127 */
128 if ((conn->flags & CO_FL_WAKE_DATA) &&
129 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
130 conn->data->wake(conn) < 0)
131 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200132
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200133 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200134 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200135 conn->flags |= CO_FL_CONNECTED;
136
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200137 /* remove the events before leaving */
138 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200139
140 /* commit polling changes */
141 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200142 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200143}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200144
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200145/* Update polling on connection <c>'s file descriptor depending on its current
146 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
147 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
148 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200149 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200150 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200151void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200152{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200153 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200154
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200155 if (unlikely(f & CO_FL_ERROR)) {
156 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
157 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
158 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
159 fd_stop_both(c->t.sock.fd);
160 return;
161 }
162
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200163 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200164 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
165 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
166 fd_stop_recv(c->t.sock.fd);
167 }
168 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
169 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
170 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200171 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200172 }
173 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
174 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200177
178 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200179 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
180 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
181 fd_stop_send(c->t.sock.fd);
182 }
183 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
184 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
185 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200186 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200187 }
188 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
189 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200190 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200191 }
192 c->flags = f;
193}
194
195/* Update polling on connection <c>'s file descriptor depending on its current
196 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
197 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
198 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200199 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200200 */
201void conn_update_sock_polling(struct connection *c)
202{
203 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200204
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200205 if (unlikely(f & CO_FL_ERROR)) {
206 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
207 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
208 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
209 fd_stop_both(c->t.sock.fd);
210 return;
211 }
212
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200213 /* update read status if needed */
214 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
215 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
216 fd_stop_recv(c->t.sock.fd);
217 }
218 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
219 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
220 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
221 fd_poll_recv(c->t.sock.fd);
222 }
223 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
224 f |= CO_FL_CURR_RD_ENA;
225 fd_want_recv(c->t.sock.fd);
226 }
227
228 /* update write status if needed */
229 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
230 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
231 fd_stop_send(c->t.sock.fd);
232 }
233 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
234 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
235 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
236 fd_poll_send(c->t.sock.fd);
237 }
238 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
239 f |= CO_FL_CURR_WR_ENA;
240 fd_want_send(c->t.sock.fd);
241 }
242 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200243}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200244
245/* This handshake handler waits a PROXY protocol header at the beginning of the
246 * raw data stream. The header looks like this :
247 *
248 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
249 *
250 * There must be exactly one space between each field. Fields are :
251 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
252 * - SRC3 : layer 3 (eg: IP) source address in standard text form
253 * - DST3 : layer 3 (eg: IP) destination address in standard text form
254 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
255 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
256 *
257 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
258 *
259 * The header line is small and in all cases smaller than the smallest normal
260 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
261 * can safely use MSG_PEEK and avoid buffering.
262 *
263 * Once the data is fetched, the values are set in the connection's address
264 * fields, and data are removed from the socket's buffer. The function returns
265 * zero if it needs to wait for more data or if it fails, or 1 if it completed
266 * and removed itself.
267 */
268int conn_recv_proxy(struct connection *conn, int flag)
269{
270 char *line, *end;
271 int len;
272
273 /* we might have been called just after an asynchronous shutr */
274 if (conn->flags & CO_FL_SOCK_RD_SH)
275 goto fail;
276
277 do {
278 len = recv(conn->t.sock.fd, trash, trashlen, MSG_PEEK);
279 if (len < 0) {
280 if (errno == EINTR)
281 continue;
282 if (errno == EAGAIN) {
283 conn_sock_poll_recv(conn);
284 return 0;
285 }
286 goto fail;
287 }
288 } while (0);
289
290 if (len < 6)
291 goto missing;
292
293 line = trash;
294 end = trash + len;
295
296 /* Decode a possible proxy request, fail early if it does not match */
297 if (strncmp(line, "PROXY ", 6) != 0)
298 goto fail;
299
300 line += 6;
301 if (len < 18) /* shortest possible line */
302 goto missing;
303
304 if (!memcmp(line, "TCP4 ", 5) != 0) {
305 u32 src3, dst3, sport, dport;
306
307 line += 5;
308
309 src3 = inetaddr_host_lim_ret(line, end, &line);
310 if (line == end)
311 goto missing;
312 if (*line++ != ' ')
313 goto fail;
314
315 dst3 = inetaddr_host_lim_ret(line, end, &line);
316 if (line == end)
317 goto missing;
318 if (*line++ != ' ')
319 goto fail;
320
321 sport = read_uint((const char **)&line, end);
322 if (line == end)
323 goto missing;
324 if (*line++ != ' ')
325 goto fail;
326
327 dport = read_uint((const char **)&line, end);
328 if (line > end - 2)
329 goto missing;
330 if (*line++ != '\r')
331 goto fail;
332 if (*line++ != '\n')
333 goto fail;
334
335 /* update the session's addresses and mark them set */
336 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
337 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
338 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
339
340 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
341 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
342 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
343 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
344 }
345 else if (!memcmp(line, "TCP6 ", 5) != 0) {
346 u32 sport, dport;
347 char *src_s;
348 char *dst_s, *sport_s, *dport_s;
349 struct in6_addr src3, dst3;
350
351 line += 5;
352
353 src_s = line;
354 dst_s = sport_s = dport_s = NULL;
355 while (1) {
356 if (line > end - 2) {
357 goto missing;
358 }
359 else if (*line == '\r') {
360 *line = 0;
361 line++;
362 if (*line++ != '\n')
363 goto fail;
364 break;
365 }
366
367 if (*line == ' ') {
368 *line = 0;
369 if (!dst_s)
370 dst_s = line + 1;
371 else if (!sport_s)
372 sport_s = line + 1;
373 else if (!dport_s)
374 dport_s = line + 1;
375 }
376 line++;
377 }
378
379 if (!dst_s || !sport_s || !dport_s)
380 goto fail;
381
382 sport = read_uint((const char **)&sport_s,dport_s - 1);
383 if (*sport_s != 0)
384 goto fail;
385
386 dport = read_uint((const char **)&dport_s,line - 2);
387 if (*dport_s != 0)
388 goto fail;
389
390 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
391 goto fail;
392
393 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
394 goto fail;
395
396 /* update the session's addresses and mark them set */
397 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
398 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
399 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
400
401 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
402 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
403 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
404 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
405 }
406 else {
407 goto fail;
408 }
409
410 /* remove the PROXY line from the request. For this we re-read the
411 * exact line at once. If we don't get the exact same result, we
412 * fail.
413 */
414 len = line - trash;
415 do {
416 int len2 = recv(conn->t.sock.fd, trash, len, 0);
417 if (len2 < 0 && errno == EINTR)
418 continue;
419 if (len2 != len)
420 goto fail;
421 } while (0);
422
423 conn->flags &= ~flag;
424 return 1;
425
426 missing:
427 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
428 * we have not read anything. Otherwise we need to fail because we won't
429 * be able to poll anymore.
430 */
431 fail:
432 conn_sock_stop_both(conn);
433 conn->flags |= CO_FL_ERROR;
434 conn->flags &= ~flag;
435 return 0;
436}
437
438/* Makes a PROXY protocol line from the two addresses. The output is sent to
439 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
440 * It returns the number of bytes composing this line (including the trailing
441 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
442 * TCP6 and "UNKNOWN" formats.
443 */
444int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
445{
446 int ret = 0;
447
448 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
449 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
450 if (ret >= buf_len)
451 return 0;
452
453 /* IPv4 src */
454 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
455 return 0;
456
457 ret += strlen(buf + ret);
458 if (ret >= buf_len)
459 return 0;
460
461 buf[ret++] = ' ';
462
463 /* IPv4 dst */
464 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
465 return 0;
466
467 ret += strlen(buf + ret);
468 if (ret >= buf_len)
469 return 0;
470
471 /* source and destination ports */
472 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
473 ntohs(((struct sockaddr_in *)src)->sin_port),
474 ntohs(((struct sockaddr_in *)dst)->sin_port));
475 if (ret >= buf_len)
476 return 0;
477 }
478 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
479 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
480 if (ret >= buf_len)
481 return 0;
482
483 /* IPv6 src */
484 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
485 return 0;
486
487 ret += strlen(buf + ret);
488 if (ret >= buf_len)
489 return 0;
490
491 buf[ret++] = ' ';
492
493 /* IPv6 dst */
494 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
495 return 0;
496
497 ret += strlen(buf + ret);
498 if (ret >= buf_len)
499 return 0;
500
501 /* source and destination ports */
502 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
503 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
504 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
505 if (ret >= buf_len)
506 return 0;
507 }
508 else {
509 /* unknown family combination */
510 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
511 if (ret >= buf_len)
512 return 0;
513 }
514 return ret;
515}
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200516
517/* This callback is used to send a valid PROXY protocol line to a socket being
518 * established from the local machine. It sets the protocol addresses to the
519 * local and remote address. This is typically used with health checks or when
520 * it is not possible to determine the other end's address. It returns 0 if it
521 * fails in a fatal way or needs to poll to go further, otherwise it returns
522 * non-zero and removes itself from the connection's flags (the bit is provided
523 * in <flag> by the caller). It is designed to be called by the connection
524 * handler and relies on it to commit polling changes. Note that this function
525 * expects to be able to send the whole line at once, which should always be
526 * possible since it is supposed to start at the first byte of the outgoing
527 * data segment.
528 */
529int conn_local_send_proxy(struct connection *conn, unsigned int flag)
530{
531 int ret, len;
532
533 /* we might have been called just after an asynchronous shutw */
534 if (conn->flags & CO_FL_SOCK_WR_SH)
535 goto out_error;
536
537 /* The target server expects a PROXY line to be sent first. */
538 conn_get_from_addr(conn);
539 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
540 goto out_error;
541
542 conn_get_to_addr(conn);
543 if (!(conn->flags & CO_FL_ADDR_TO_SET))
544 goto out_error;
545
546 len = make_proxy_line(trash, trashlen, &conn->addr.from, &conn->addr.to);
547 if (!len)
548 goto out_error;
549
550 /* we have to send trash from len bytes. If the data layer has a
551 * pending write, we'll also set MSG_MORE.
552 */
553 ret = send(conn->t.sock.fd, trash, len, (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0);
554
555 if (ret == 0)
556 goto out_wait;
557
558 if (ret < 0) {
559 if (errno == EAGAIN)
560 goto out_wait;
561 goto out_error;
562 }
563
564 if (ret != len)
565 goto out_error;
566
567 /* The connection is ready now, simply return and let the connection
568 * handler notify upper layers if needed.
569 */
570 if (conn->flags & CO_FL_WAIT_L4_CONN)
571 conn->flags &= ~CO_FL_WAIT_L4_CONN;
572 conn->flags &= ~flag;
573 return 1;
574
575 out_error:
576 /* Write error on the file descriptor */
577 conn->flags |= CO_FL_ERROR;
578 conn->flags &= ~flag;
579 return 0;
580
581 out_wait:
582 __conn_sock_stop_recv(conn);
583 __conn_sock_poll_send(conn);
584 return 0;
585}