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 | 7d28149 | 2012-12-16 19:19:13 +0100 | [diff] [blame] | 49 | conn_refresh_polling_flags(conn); |
| 50 | flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */ |
Willy Tarreau | d29a066 | 2012-12-10 16:33:38 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 52 | process_handshake: |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 53 | /* The handshake callbacks are called in sequence. If either of them is |
| 54 | * missing something, it must enable the required polling at the socket |
| 55 | * layer of the connection. Polling state is not guaranteed when entering |
| 56 | * these handlers, so any handshake handler which does not complete its |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 57 | * work must explicitly disable events it's not interested in. Error |
| 58 | * handling is also performed here in order to reduce the number of tests |
| 59 | * around. |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 60 | */ |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 61 | while (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) { |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 62 | if (unlikely(conn->flags & CO_FL_ERROR)) |
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 | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 69 | if (conn->flags & CO_FL_SEND_PROXY) |
| 70 | if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY)) |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 71 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #ifdef USE_OPENSSL |
| 73 | if (conn->flags & CO_FL_SSL_WAIT_HS) |
| 74 | if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS)) |
| 75 | goto leave; |
| 76 | #endif |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 79 | /* Once we're purely in the data phase, we disable handshake polling */ |
| 80 | if (!(conn->flags & CO_FL_POLL_SOCK)) |
| 81 | __conn_sock_stop_both(conn); |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 071e137 | 2012-10-03 01:39:48 +0200 | [diff] [blame] | 83 | /* The data layer might not be ready yet (eg: when using embryonic |
| 84 | * sessions). If we're about to move data, we must initialize it first. |
| 85 | * The function may fail and cause the connection to be destroyed, thus |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 86 | * we must not use it anymore and should immediately leave instead. |
| 87 | */ |
Willy Tarreau | 071e137 | 2012-10-03 01:39:48 +0200 | [diff] [blame] | 88 | if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | |
Willy Tarreau | 153c3ca | 2012-10-22 22:47:55 +0200 | [diff] [blame] | 91 | /* The data transfer starts here and stops on error and handshakes. Note |
| 92 | * that we must absolutely test conn->xprt at each step in case it suddenly |
| 93 | * changes due to a quick unexpected close(). |
| 94 | */ |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 95 | 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] | 96 | conn->xprt && |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 97 | fd_recv_ready(fd) && |
| 98 | !(conn->flags & (CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) { |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 99 | /* force detection of a flag change : it's impossible to have both |
| 100 | * CONNECTED and WAIT_CONN so we're certain to trigger a change. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 101 | */ |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 102 | flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED; |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 103 | conn->data->recv(conn); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 104 | } |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 105 | |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 106 | if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) && |
Willy Tarreau | 153c3ca | 2012-10-22 22:47:55 +0200 | [diff] [blame] | 107 | conn->xprt && |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 108 | fd_send_ready(fd) && |
| 109 | !(conn->flags & (CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) { |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 110 | /* force detection of a flag change : it's impossible to have both |
| 111 | * CONNECTED and WAIT_CONN so we're certain to trigger a change. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 112 | */ |
Willy Tarreau | 3b5bc66 | 2012-10-05 21:29:37 +0200 | [diff] [blame] | 113 | flags = CO_FL_WAIT_L4_CONN | CO_FL_CONNECTED; |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 114 | conn->data->send(conn); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 115 | } |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 117 | /* It may happen during the data phase that a handshake is |
| 118 | * enabled again (eg: SSL) |
| 119 | */ |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 120 | if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 121 | goto process_handshake; |
| 122 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 123 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && fd_send_ready(conn->t.sock.fd)) { |
Willy Tarreau | f8deb0c | 2012-09-01 17:59:22 +0200 | [diff] [blame] | 124 | /* still waiting for a connection to establish and nothing was |
| 125 | * attempted yet to probe the connection. Then let's retry the |
| 126 | * connect(). |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 127 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 128 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 129 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 132 | leave: |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 133 | /* The wake callback may be used to process a critical error and abort the |
| 134 | * connection. If so, we don't want to go further as the connection will |
| 135 | * have been released and the FD destroyed. |
| 136 | */ |
| 137 | if ((conn->flags & CO_FL_WAKE_DATA) && |
| 138 | ((conn->flags ^ flags) & CO_FL_CONN_STATE) && |
| 139 | conn->data->wake(conn) < 0) |
| 140 | return 0; |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 141 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 142 | /* Last check, verify if the connection just established */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 143 | 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] | 144 | conn->flags |= CO_FL_CONNECTED; |
| 145 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 146 | /* remove the events before leaving */ |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 147 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 148 | |
| 149 | /* commit polling changes */ |
| 150 | conn_cond_update_polling(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 151 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 152 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 153 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 154 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 155 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 156 | * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*. |
| 157 | * 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] | 158 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 159 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 160 | void conn_update_data_polling(struct connection *c) |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 161 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 162 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 163 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 164 | if (!(c->flags & CO_FL_CTRL_READY)) |
| 165 | return; |
| 166 | |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 167 | /* update read status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 168 | if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 169 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 170 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 171 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 172 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
| 173 | fd_stop_recv(c->t.sock.fd); |
| 174 | f &= ~CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 175 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 176 | |
| 177 | /* update write status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 178 | 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] | 179 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 180 | f |= CO_FL_CURR_WR_ENA; |
| 181 | } |
| 182 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
| 183 | fd_stop_send(c->t.sock.fd); |
| 184 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 185 | } |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 186 | c->flags = f; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 190 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 191 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 192 | * 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] | 193 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 194 | */ |
| 195 | void conn_update_sock_polling(struct connection *c) |
| 196 | { |
| 197 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 199 | if (!(c->flags & CO_FL_CTRL_READY)) |
| 200 | return; |
| 201 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 202 | /* update read status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 203 | if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 204 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 205 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 206 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 207 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
| 208 | fd_stop_recv(c->t.sock.fd); |
| 209 | f &= ~CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* update write status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 213 | if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 214 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 215 | f |= CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 216 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 217 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
| 218 | fd_stop_send(c->t.sock.fd); |
| 219 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 220 | } |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame^] | 221 | c->flags = f; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 222 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 223 | |
| 224 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 225 | * raw data stream. The header looks like this : |
| 226 | * |
| 227 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 228 | * |
| 229 | * There must be exactly one space between each field. Fields are : |
| 230 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 231 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 232 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 233 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 234 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 235 | * |
| 236 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 237 | * |
| 238 | * The header line is small and in all cases smaller than the smallest normal |
| 239 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 240 | * can safely use MSG_PEEK and avoid buffering. |
| 241 | * |
| 242 | * Once the data is fetched, the values are set in the connection's address |
| 243 | * fields, and data are removed from the socket's buffer. The function returns |
| 244 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 245 | * and removed itself. |
| 246 | */ |
| 247 | int conn_recv_proxy(struct connection *conn, int flag) |
| 248 | { |
| 249 | char *line, *end; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 250 | |
| 251 | /* we might have been called just after an asynchronous shutr */ |
| 252 | if (conn->flags & CO_FL_SOCK_RD_SH) |
| 253 | goto fail; |
| 254 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 255 | if (!(conn->flags & CO_FL_CTRL_READY)) |
| 256 | goto fail; |
| 257 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 258 | do { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 259 | trash.len = recv(conn->t.sock.fd, trash.str, trash.size, MSG_PEEK); |
| 260 | if (trash.len < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 261 | if (errno == EINTR) |
| 262 | continue; |
| 263 | if (errno == EAGAIN) { |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 264 | __conn_sock_poll_recv(conn); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 265 | return 0; |
| 266 | } |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 267 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 268 | } |
| 269 | } while (0); |
| 270 | |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 271 | if (!trash.len) { |
| 272 | /* client shutdown */ |
| 273 | conn->err_code = CO_ER_PRX_EMPTY; |
| 274 | goto fail; |
| 275 | } |
| 276 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 277 | if (trash.len < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 278 | goto missing; |
| 279 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 280 | line = trash.str; |
| 281 | end = trash.str + trash.len; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 282 | |
| 283 | /* Decode a possible proxy request, fail early if it does not match */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 284 | if (strncmp(line, "PROXY ", 6) != 0) { |
| 285 | conn->err_code = CO_ER_PRX_NOT_HDR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 286 | goto fail; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 287 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 288 | |
| 289 | line += 6; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 290 | if (trash.len < 18) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 291 | goto missing; |
| 292 | |
| 293 | if (!memcmp(line, "TCP4 ", 5) != 0) { |
| 294 | u32 src3, dst3, sport, dport; |
| 295 | |
| 296 | line += 5; |
| 297 | |
| 298 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 299 | if (line == end) |
| 300 | goto missing; |
| 301 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 302 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 303 | |
| 304 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 305 | if (line == end) |
| 306 | goto missing; |
| 307 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 308 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 309 | |
| 310 | sport = read_uint((const char **)&line, end); |
| 311 | if (line == end) |
| 312 | goto missing; |
| 313 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 314 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 315 | |
| 316 | dport = read_uint((const char **)&line, end); |
| 317 | if (line > end - 2) |
| 318 | goto missing; |
| 319 | if (*line++ != '\r') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 320 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 321 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 322 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 323 | |
| 324 | /* update the session's addresses and mark them set */ |
| 325 | ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET; |
| 326 | ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3); |
| 327 | ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport); |
| 328 | |
| 329 | ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET; |
| 330 | ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3); |
| 331 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport); |
| 332 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 333 | } |
| 334 | else if (!memcmp(line, "TCP6 ", 5) != 0) { |
| 335 | u32 sport, dport; |
| 336 | char *src_s; |
| 337 | char *dst_s, *sport_s, *dport_s; |
| 338 | struct in6_addr src3, dst3; |
| 339 | |
| 340 | line += 5; |
| 341 | |
| 342 | src_s = line; |
| 343 | dst_s = sport_s = dport_s = NULL; |
| 344 | while (1) { |
| 345 | if (line > end - 2) { |
| 346 | goto missing; |
| 347 | } |
| 348 | else if (*line == '\r') { |
| 349 | *line = 0; |
| 350 | line++; |
| 351 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 352 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | |
| 356 | if (*line == ' ') { |
| 357 | *line = 0; |
| 358 | if (!dst_s) |
| 359 | dst_s = line + 1; |
| 360 | else if (!sport_s) |
| 361 | sport_s = line + 1; |
| 362 | else if (!dport_s) |
| 363 | dport_s = line + 1; |
| 364 | } |
| 365 | line++; |
| 366 | } |
| 367 | |
| 368 | if (!dst_s || !sport_s || !dport_s) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 369 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 370 | |
| 371 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 372 | if (*sport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 373 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 374 | |
| 375 | dport = read_uint((const char **)&dport_s,line - 2); |
| 376 | if (*dport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 377 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 378 | |
| 379 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 380 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 381 | |
| 382 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 383 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 384 | |
| 385 | /* update the session's addresses and mark them set */ |
| 386 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6; |
| 387 | memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 388 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport); |
| 389 | |
| 390 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6; |
| 391 | memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 392 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport); |
| 393 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 394 | } |
| 395 | else { |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 396 | /* The protocol does not match something known (TCP4/TCP6) */ |
| 397 | conn->err_code = CO_ER_PRX_BAD_PROTO; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 398 | goto fail; |
| 399 | } |
| 400 | |
| 401 | /* remove the PROXY line from the request. For this we re-read the |
| 402 | * exact line at once. If we don't get the exact same result, we |
| 403 | * fail. |
| 404 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 405 | trash.len = line - trash.str; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 406 | do { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 407 | int len2 = recv(conn->t.sock.fd, trash.str, trash.len, 0); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 408 | if (len2 < 0 && errno == EINTR) |
| 409 | continue; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 410 | if (len2 != trash.len) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 411 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 412 | } while (0); |
| 413 | |
| 414 | conn->flags &= ~flag; |
| 415 | return 1; |
| 416 | |
| 417 | missing: |
| 418 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 419 | * we have not read anything. Otherwise we need to fail because we won't |
| 420 | * be able to poll anymore. |
| 421 | */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 422 | conn->err_code = CO_ER_PRX_TRUNCATED; |
| 423 | goto fail; |
| 424 | |
| 425 | bad_header: |
| 426 | /* This is not a valid proxy protocol header */ |
| 427 | conn->err_code = CO_ER_PRX_BAD_HDR; |
| 428 | goto fail; |
| 429 | |
| 430 | recv_abort: |
| 431 | conn->err_code = CO_ER_PRX_ABORT; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 432 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 433 | goto fail; |
| 434 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 435 | fail: |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 436 | __conn_sock_stop_both(conn); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 437 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 442 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 443 | * It returns the number of bytes composing this line (including the trailing |
| 444 | * LF), or zero in case of failure (eg: not enough space). It supports TCP4, |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 445 | * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is |
| 446 | * emitted as well. |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 447 | */ |
| 448 | int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst) |
| 449 | { |
| 450 | int ret = 0; |
| 451 | |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 452 | if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 453 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 "); |
| 454 | if (ret >= buf_len) |
| 455 | return 0; |
| 456 | |
| 457 | /* IPv4 src */ |
| 458 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret)) |
| 459 | return 0; |
| 460 | |
| 461 | ret += strlen(buf + ret); |
| 462 | if (ret >= buf_len) |
| 463 | return 0; |
| 464 | |
| 465 | buf[ret++] = ' '; |
| 466 | |
| 467 | /* IPv4 dst */ |
| 468 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->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 | /* source and destination ports */ |
| 476 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 477 | ntohs(((struct sockaddr_in *)src)->sin_port), |
| 478 | ntohs(((struct sockaddr_in *)dst)->sin_port)); |
| 479 | if (ret >= buf_len) |
| 480 | return 0; |
| 481 | } |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 482 | else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 483 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 "); |
| 484 | if (ret >= buf_len) |
| 485 | return 0; |
| 486 | |
| 487 | /* IPv6 src */ |
| 488 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret)) |
| 489 | return 0; |
| 490 | |
| 491 | ret += strlen(buf + ret); |
| 492 | if (ret >= buf_len) |
| 493 | return 0; |
| 494 | |
| 495 | buf[ret++] = ' '; |
| 496 | |
| 497 | /* IPv6 dst */ |
| 498 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->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 | /* source and destination ports */ |
| 506 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 507 | ntohs(((struct sockaddr_in6 *)src)->sin6_port), |
| 508 | ntohs(((struct sockaddr_in6 *)dst)->sin6_port)); |
| 509 | if (ret >= buf_len) |
| 510 | return 0; |
| 511 | } |
| 512 | else { |
| 513 | /* unknown family combination */ |
| 514 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
| 515 | if (ret >= buf_len) |
| 516 | return 0; |
| 517 | } |
| 518 | return ret; |
| 519 | } |