Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 15 | #include <common/compat.h> |
| 16 | #include <common/config.h> |
| 17 | |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 18 | #include <proto/connection.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 19 | #include <proto/fd.h> |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 20 | #include <proto/frontend.h> |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 21 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 22 | #include <proto/session.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 23 | #include <proto/stream_interface.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 24 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 25 | #ifdef USE_OPENSSL |
| 26 | #include <proto/ssl_sock.h> |
| 27 | #endif |
| 28 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 29 | struct pool_head *pool2_connection; |
| 30 | |
| 31 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 32 | int init_connection() |
| 33 | { |
| 34 | pool2_connection = create_pool("connection", sizeof (struct connection), MEM_F_SHARED); |
| 35 | return pool2_connection != NULL; |
| 36 | } |
| 37 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 38 | /* I/O callback for fd-based connections. It calls the read/write handlers |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 39 | * provided by the connection's sock_ops, which must be valid. It returns 0. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 40 | */ |
| 41 | int conn_fd_handler(int fd) |
| 42 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 43 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 44 | unsigned int flags; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 46 | if (unlikely(!conn)) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 47 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 49 | /* before engaging there, we clear the new WAIT_* flags so that we can |
| 50 | * more easily detect an EAGAIN condition from anywhere. |
| 51 | */ |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 52 | flags = conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 54 | process_handshake: |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 55 | /* 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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 61 | while (unlikely(conn->flags & CO_FL_HANDSHAKE)) { |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 62 | if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR))) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 63 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 65 | if (conn->flags & CO_FL_ACCEPT_PROXY) |
| 66 | if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY)) |
| 67 | goto leave; |
| 68 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 69 | if (conn->flags & CO_FL_SI_SEND_PROXY) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 70 | if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY)) |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 71 | goto leave; |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 72 | |
| 73 | if (conn->flags & CO_FL_LOCAL_SPROXY) |
| 74 | if (!conn_local_send_proxy(conn, CO_FL_LOCAL_SPROXY)) |
| 75 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 76 | #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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 83 | /* 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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 86 | |
Willy Tarreau | 071e137 | 2012-10-03 01:39:48 +0200 | [diff] [blame] | 87 | /* 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 Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 90 | * we must not use it anymore and should immediately leave instead. |
| 91 | */ |
Willy Tarreau | 071e137 | 2012-10-03 01:39:48 +0200 | [diff] [blame] | 92 | if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 93 | return 0; |
| 94 | |
Willy Tarreau | 153c3ca | 2012-10-22 22:47:55 +0200 | [diff] [blame] | 95 | /* 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 Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 99 | if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) && |
Willy Tarreau | 153c3ca | 2012-10-22 22:47:55 +0200 | [diff] [blame] | 100 | conn->xprt && |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 101 | !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) { |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 102 | /* 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 Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 104 | */ |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 105 | flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED; |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 106 | conn->data->recv(conn); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 107 | } |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 109 | if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) && |
Willy Tarreau | 153c3ca | 2012-10-22 22:47:55 +0200 | [diff] [blame] | 110 | conn->xprt && |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 111 | !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) { |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 112 | /* 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 Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 114 | */ |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 115 | flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED; |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 116 | conn->data->send(conn); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 117 | } |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 119 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 120 | goto leave; |
| 121 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 122 | /* 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 Tarreau | f8deb0c | 2012-09-01 17:59:22 +0200 | [diff] [blame] | 128 | 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 Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 132 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 133 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 134 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 137 | leave: |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 138 | /* 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 Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 147 | /* Last check, verify if the connection just established */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 148 | if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 149 | conn->flags |= CO_FL_CONNECTED; |
| 150 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 151 | /* remove the events before leaving */ |
| 152 | fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR); |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 153 | |
| 154 | /* commit polling changes */ |
| 155 | conn_cond_update_polling(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 156 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 157 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 159 | /* 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 Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 163 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 164 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 165 | void conn_update_data_polling(struct connection *c) |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 166 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 167 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 169 | 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 177 | /* update read status if needed */ |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 178 | if (unlikely((f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) { |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 179 | fd_poll_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 180 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 181 | } |
| 182 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) { |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 183 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 184 | f |= CO_FL_CURR_RD_ENA; |
| 185 | } |
| 186 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
| 187 | fd_stop_recv(c->t.sock.fd); |
| 188 | f &= ~CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 189 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 190 | |
| 191 | /* update write status if needed */ |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 192 | if (unlikely((f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) { |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 193 | fd_poll_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 194 | f |= CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 195 | } |
| 196 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) { |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 197 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 198 | f |= CO_FL_CURR_WR_ENA; |
| 199 | } |
| 200 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
| 201 | fd_stop_send(c->t.sock.fd); |
| 202 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 203 | } |
Willy Tarreau | c9f7804 | 2012-11-05 20:00:43 +0100 | [diff] [blame] | 204 | c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 208 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 209 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 210 | * The connection flags are updated with the new flags at the end of the |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 211 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 212 | */ |
| 213 | void conn_update_sock_polling(struct connection *c) |
| 214 | { |
| 215 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 216 | |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 217 | if (unlikely(f & CO_FL_ERROR)) { |
| 218 | c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA | |
| 219 | CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA | |
| 220 | CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA); |
| 221 | fd_stop_both(c->t.sock.fd); |
| 222 | return; |
| 223 | } |
| 224 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 225 | /* update read status if needed */ |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 226 | if (unlikely((f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 227 | fd_poll_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 228 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 229 | } |
| 230 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 231 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 232 | f |= CO_FL_CURR_RD_ENA; |
| 233 | } |
| 234 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
| 235 | fd_stop_recv(c->t.sock.fd); |
| 236 | f &= ~CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* update write status if needed */ |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 240 | if (unlikely((f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 241 | fd_poll_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 242 | f |= CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 243 | } |
| 244 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 245 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 246 | f |= CO_FL_CURR_WR_ENA; |
| 247 | } |
| 248 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
| 249 | fd_stop_send(c->t.sock.fd); |
| 250 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 251 | } |
Willy Tarreau | c9f7804 | 2012-11-05 20:00:43 +0100 | [diff] [blame] | 252 | c->flags = f & ~(CO_FL_WAIT_RD | CO_FL_WAIT_WR); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 253 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 254 | |
| 255 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 256 | * raw data stream. The header looks like this : |
| 257 | * |
| 258 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 259 | * |
| 260 | * There must be exactly one space between each field. Fields are : |
| 261 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 262 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 263 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 264 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 265 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 266 | * |
| 267 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 268 | * |
| 269 | * The header line is small and in all cases smaller than the smallest normal |
| 270 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 271 | * can safely use MSG_PEEK and avoid buffering. |
| 272 | * |
| 273 | * Once the data is fetched, the values are set in the connection's address |
| 274 | * fields, and data are removed from the socket's buffer. The function returns |
| 275 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 276 | * and removed itself. |
| 277 | */ |
| 278 | int conn_recv_proxy(struct connection *conn, int flag) |
| 279 | { |
| 280 | char *line, *end; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 281 | |
| 282 | /* we might have been called just after an asynchronous shutr */ |
| 283 | if (conn->flags & CO_FL_SOCK_RD_SH) |
| 284 | goto fail; |
| 285 | |
| 286 | do { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 287 | trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK); |
| 288 | if (trash.len < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 289 | if (errno == EINTR) |
| 290 | continue; |
| 291 | if (errno == EAGAIN) { |
| 292 | conn_sock_poll_recv(conn); |
| 293 | return 0; |
| 294 | } |
| 295 | goto fail; |
| 296 | } |
| 297 | } while (0); |
| 298 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 299 | if (trash.len < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 300 | goto missing; |
| 301 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 302 | line = trash.str; |
| 303 | end = trash.str + trash.len; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 304 | |
| 305 | /* Decode a possible proxy request, fail early if it does not match */ |
| 306 | if (strncmp(line, "PROXY ", 6) != 0) |
| 307 | goto fail; |
| 308 | |
| 309 | line += 6; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 310 | if (trash.len < 18) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 311 | goto missing; |
| 312 | |
| 313 | if (!memcmp(line, "TCP4 ", 5) != 0) { |
| 314 | u32 src3, dst3, sport, dport; |
| 315 | |
| 316 | line += 5; |
| 317 | |
| 318 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 319 | if (line == end) |
| 320 | goto missing; |
| 321 | if (*line++ != ' ') |
| 322 | goto fail; |
| 323 | |
| 324 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 325 | if (line == end) |
| 326 | goto missing; |
| 327 | if (*line++ != ' ') |
| 328 | goto fail; |
| 329 | |
| 330 | sport = read_uint((const char **)&line, end); |
| 331 | if (line == end) |
| 332 | goto missing; |
| 333 | if (*line++ != ' ') |
| 334 | goto fail; |
| 335 | |
| 336 | dport = read_uint((const char **)&line, end); |
| 337 | if (line > end - 2) |
| 338 | goto missing; |
| 339 | if (*line++ != '\r') |
| 340 | goto fail; |
| 341 | if (*line++ != '\n') |
| 342 | goto fail; |
| 343 | |
| 344 | /* update the session's addresses and mark them set */ |
| 345 | ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET; |
| 346 | ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3); |
| 347 | ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport); |
| 348 | |
| 349 | ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET; |
| 350 | ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3); |
| 351 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport); |
| 352 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 353 | } |
| 354 | else if (!memcmp(line, "TCP6 ", 5) != 0) { |
| 355 | u32 sport, dport; |
| 356 | char *src_s; |
| 357 | char *dst_s, *sport_s, *dport_s; |
| 358 | struct in6_addr src3, dst3; |
| 359 | |
| 360 | line += 5; |
| 361 | |
| 362 | src_s = line; |
| 363 | dst_s = sport_s = dport_s = NULL; |
| 364 | while (1) { |
| 365 | if (line > end - 2) { |
| 366 | goto missing; |
| 367 | } |
| 368 | else if (*line == '\r') { |
| 369 | *line = 0; |
| 370 | line++; |
| 371 | if (*line++ != '\n') |
| 372 | goto fail; |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (*line == ' ') { |
| 377 | *line = 0; |
| 378 | if (!dst_s) |
| 379 | dst_s = line + 1; |
| 380 | else if (!sport_s) |
| 381 | sport_s = line + 1; |
| 382 | else if (!dport_s) |
| 383 | dport_s = line + 1; |
| 384 | } |
| 385 | line++; |
| 386 | } |
| 387 | |
| 388 | if (!dst_s || !sport_s || !dport_s) |
| 389 | goto fail; |
| 390 | |
| 391 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 392 | if (*sport_s != 0) |
| 393 | goto fail; |
| 394 | |
| 395 | dport = read_uint((const char **)&dport_s,line - 2); |
| 396 | if (*dport_s != 0) |
| 397 | goto fail; |
| 398 | |
| 399 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
| 400 | goto fail; |
| 401 | |
| 402 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
| 403 | goto fail; |
| 404 | |
| 405 | /* update the session's addresses and mark them set */ |
| 406 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6; |
| 407 | memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 408 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport); |
| 409 | |
| 410 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6; |
| 411 | memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 412 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport); |
| 413 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 414 | } |
| 415 | else { |
| 416 | goto fail; |
| 417 | } |
| 418 | |
| 419 | /* remove the PROXY line from the request. For this we re-read the |
| 420 | * exact line at once. If we don't get the exact same result, we |
| 421 | * fail. |
| 422 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 423 | trash.len = line - trash.str; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 424 | do { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 425 | int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 426 | if (len2 < 0 && errno == EINTR) |
| 427 | continue; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 428 | if (len2 != trash.len) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 429 | goto fail; |
| 430 | } while (0); |
| 431 | |
| 432 | conn->flags &= ~flag; |
| 433 | return 1; |
| 434 | |
| 435 | missing: |
| 436 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 437 | * we have not read anything. Otherwise we need to fail because we won't |
| 438 | * be able to poll anymore. |
| 439 | */ |
| 440 | fail: |
| 441 | conn_sock_stop_both(conn); |
| 442 | conn->flags |= CO_FL_ERROR; |
| 443 | conn->flags &= ~flag; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 448 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 449 | * It returns the number of bytes composing this line (including the trailing |
| 450 | * LF), or zero in case of failure (eg: not enough space). It supports TCP4, |
| 451 | * TCP6 and "UNKNOWN" formats. |
| 452 | */ |
| 453 | int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst) |
| 454 | { |
| 455 | int ret = 0; |
| 456 | |
| 457 | if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) { |
| 458 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 "); |
| 459 | if (ret >= buf_len) |
| 460 | return 0; |
| 461 | |
| 462 | /* IPv4 src */ |
| 463 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret)) |
| 464 | return 0; |
| 465 | |
| 466 | ret += strlen(buf + ret); |
| 467 | if (ret >= buf_len) |
| 468 | return 0; |
| 469 | |
| 470 | buf[ret++] = ' '; |
| 471 | |
| 472 | /* IPv4 dst */ |
| 473 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret)) |
| 474 | return 0; |
| 475 | |
| 476 | ret += strlen(buf + ret); |
| 477 | if (ret >= buf_len) |
| 478 | return 0; |
| 479 | |
| 480 | /* source and destination ports */ |
| 481 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 482 | ntohs(((struct sockaddr_in *)src)->sin_port), |
| 483 | ntohs(((struct sockaddr_in *)dst)->sin_port)); |
| 484 | if (ret >= buf_len) |
| 485 | return 0; |
| 486 | } |
| 487 | else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) { |
| 488 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 "); |
| 489 | if (ret >= buf_len) |
| 490 | return 0; |
| 491 | |
| 492 | /* IPv6 src */ |
| 493 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret)) |
| 494 | return 0; |
| 495 | |
| 496 | ret += strlen(buf + ret); |
| 497 | if (ret >= buf_len) |
| 498 | return 0; |
| 499 | |
| 500 | buf[ret++] = ' '; |
| 501 | |
| 502 | /* IPv6 dst */ |
| 503 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret)) |
| 504 | return 0; |
| 505 | |
| 506 | ret += strlen(buf + ret); |
| 507 | if (ret >= buf_len) |
| 508 | return 0; |
| 509 | |
| 510 | /* source and destination ports */ |
| 511 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 512 | ntohs(((struct sockaddr_in6 *)src)->sin6_port), |
| 513 | ntohs(((struct sockaddr_in6 *)dst)->sin6_port)); |
| 514 | if (ret >= buf_len) |
| 515 | return 0; |
| 516 | } |
| 517 | else { |
| 518 | /* unknown family combination */ |
| 519 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
| 520 | if (ret >= buf_len) |
| 521 | return 0; |
| 522 | } |
| 523 | return ret; |
| 524 | } |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 525 | |
| 526 | /* This callback is used to send a valid PROXY protocol line to a socket being |
| 527 | * established from the local machine. It sets the protocol addresses to the |
| 528 | * local and remote address. This is typically used with health checks or when |
| 529 | * it is not possible to determine the other end's address. It returns 0 if it |
| 530 | * fails in a fatal way or needs to poll to go further, otherwise it returns |
| 531 | * non-zero and removes itself from the connection's flags (the bit is provided |
| 532 | * in <flag> by the caller). It is designed to be called by the connection |
| 533 | * handler and relies on it to commit polling changes. Note that this function |
| 534 | * expects to be able to send the whole line at once, which should always be |
| 535 | * possible since it is supposed to start at the first byte of the outgoing |
| 536 | * data segment. |
| 537 | */ |
| 538 | int conn_local_send_proxy(struct connection *conn, unsigned int flag) |
| 539 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 540 | int ret; |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 541 | |
| 542 | /* we might have been called just after an asynchronous shutw */ |
| 543 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 544 | goto out_error; |
| 545 | |
| 546 | /* The target server expects a PROXY line to be sent first. */ |
| 547 | conn_get_from_addr(conn); |
| 548 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) |
| 549 | goto out_error; |
| 550 | |
| 551 | conn_get_to_addr(conn); |
| 552 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) |
| 553 | goto out_error; |
| 554 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 555 | trash.len = make_proxy_line(trash.str, trash.size, &conn->addr.from, &conn->addr.to); |
| 556 | if (!trash.len) |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 557 | goto out_error; |
| 558 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 559 | /* we have to send the whole trash. If the data layer has a |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 560 | * pending write, we'll also set MSG_MORE. |
| 561 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 562 | ret = send(conn->t.sock.fd, trash.str, trash.len, (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0); |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 563 | |
| 564 | if (ret == 0) |
| 565 | goto out_wait; |
| 566 | |
| 567 | if (ret < 0) { |
| 568 | if (errno == EAGAIN) |
| 569 | goto out_wait; |
| 570 | goto out_error; |
| 571 | } |
| 572 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 573 | if (ret != trash.len) |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 574 | goto out_error; |
| 575 | |
| 576 | /* The connection is ready now, simply return and let the connection |
| 577 | * handler notify upper layers if needed. |
| 578 | */ |
| 579 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 580 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 581 | conn->flags &= ~flag; |
| 582 | return 1; |
| 583 | |
| 584 | out_error: |
| 585 | /* Write error on the file descriptor */ |
| 586 | conn->flags |= CO_FL_ERROR; |
| 587 | conn->flags &= ~flag; |
| 588 | return 0; |
| 589 | |
| 590 | out_wait: |
| 591 | __conn_sock_stop_recv(conn); |
| 592 | __conn_sock_poll_send(conn); |
| 593 | return 0; |
| 594 | } |