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> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 17 | #include <common/namespace.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 19 | #include <proto/connection.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 20 | #include <proto/fd.h> |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 21 | #include <proto/frontend.h> |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 22 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 23 | #include <proto/stream_interface.h> |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 24 | #include <proto/sample.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 25 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 26 | #ifdef USE_OPENSSL |
| 27 | #include <proto/ssl_sock.h> |
| 28 | #endif |
| 29 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 30 | struct pool_head *pool2_connection; |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 31 | struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, }; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 32 | |
| 33 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 34 | int init_connection() |
| 35 | { |
| 36 | pool2_connection = create_pool("connection", sizeof (struct connection), MEM_F_SHARED); |
| 37 | return pool2_connection != NULL; |
| 38 | } |
| 39 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 40 | /* I/O callback for fd-based connections. It calls the read/write handlers |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 41 | * provided by the connection's sock_ops, which must be valid. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 43 | void conn_fd_handler(int fd) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 45 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 46 | unsigned int flags; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 48 | if (unlikely(!conn)) |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 49 | return; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | 7d28149 | 2012-12-16 19:19:13 +0100 | [diff] [blame] | 51 | conn_refresh_polling_flags(conn); |
| 52 | 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] | 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 |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 59 | * work must explicitly disable events it's not interested in. Error |
| 60 | * handling is also performed here in order to reduce the number of tests |
| 61 | * around. |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 62 | */ |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 63 | while (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) { |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 64 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 65 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 66 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 67 | if (conn->flags & CO_FL_ACCEPT_CIP) |
| 68 | if (!conn_recv_netscaler_cip(conn, CO_FL_ACCEPT_CIP)) |
| 69 | goto leave; |
| 70 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 71 | if (conn->flags & CO_FL_ACCEPT_PROXY) |
| 72 | if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY)) |
| 73 | goto leave; |
| 74 | |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 75 | if (conn->flags & CO_FL_SEND_PROXY) |
| 76 | if (!conn_si_send_proxy(conn, CO_FL_SEND_PROXY)) |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 77 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 78 | #ifdef USE_OPENSSL |
| 79 | if (conn->flags & CO_FL_SSL_WAIT_HS) |
| 80 | if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS)) |
| 81 | goto leave; |
| 82 | #endif |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 85 | /* Once we're purely in the data phase, we disable handshake polling */ |
| 86 | if (!(conn->flags & CO_FL_POLL_SOCK)) |
| 87 | __conn_sock_stop_both(conn); |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 89 | /* The connection owner might want to be notified about an end of |
| 90 | * handshake indicating the connection is ready, before we proceed with |
| 91 | * any data exchange. The callback may fail and cause the connection to |
| 92 | * be destroyed, thus we must not use it anymore and should immediately |
| 93 | * leave instead. The caller must immediately unregister itself once |
| 94 | * called. |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 95 | */ |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 96 | if (conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0) |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 97 | return; |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 57ec32f | 2017-04-11 19:59:33 +0200 | [diff] [blame] | 99 | if (conn->xprt && fd_send_ready(fd) && |
| 100 | ((conn->flags & (CO_FL_DATA_WR_ENA|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_WR_ENA)) { |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 101 | /* force reporting of activity by clearing the previous flags : |
| 102 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 103 | * both of which will be detected below. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 104 | */ |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 105 | flags = 0; |
Willy Tarreau | 57ec32f | 2017-04-11 19:59:33 +0200 | [diff] [blame] | 106 | conn->data->send(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 | 57ec32f | 2017-04-11 19:59:33 +0200 | [diff] [blame] | 109 | /* The data transfer starts here and stops on error and handshakes. Note |
| 110 | * that we must absolutely test conn->xprt at each step in case it suddenly |
| 111 | * changes due to a quick unexpected close(). |
| 112 | */ |
| 113 | if (conn->xprt && fd_recv_ready(fd) && |
| 114 | ((conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE)) == CO_FL_DATA_RD_ENA)) { |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 115 | /* force reporting of activity by clearing the previous flags : |
| 116 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 117 | * both of which will be detected below. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 118 | */ |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 119 | flags = 0; |
Willy Tarreau | 57ec32f | 2017-04-11 19:59:33 +0200 | [diff] [blame] | 120 | conn->data->recv(conn); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 121 | } |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 122 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 123 | /* It may happen during the data phase that a handshake is |
| 124 | * enabled again (eg: SSL) |
| 125 | */ |
Willy Tarreau | d6e999b | 2013-11-25 08:41:15 +0100 | [diff] [blame] | 126 | if (unlikely(conn->flags & (CO_FL_HANDSHAKE | CO_FL_ERROR))) |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 127 | goto process_handshake; |
| 128 | |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 129 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) { |
Willy Tarreau | f8deb0c | 2012-09-01 17:59:22 +0200 | [diff] [blame] | 130 | /* still waiting for a connection to establish and nothing was |
| 131 | * attempted yet to probe the connection. Then let's retry the |
| 132 | * connect(). |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 133 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 134 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 135 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 136 | } |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 137 | leave: |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 138 | /* Verify if the connection just established. */ |
Willy Tarreau | 7bf3fa3 | 2017-03-14 20:19:29 +0100 | [diff] [blame] | 139 | if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) |
| 140 | conn->flags |= CO_FL_CONNECTED; |
| 141 | |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 142 | /* The connection owner might want to be notified about failures to |
| 143 | * complete the handshake. The callback may fail and cause the |
| 144 | * connection to be destroyed, thus we must not use it anymore and |
| 145 | * should immediately leave instead. The caller must immediately |
| 146 | * unregister itself once called. |
| 147 | */ |
| 148 | if (((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) && |
| 149 | conn->xprt_done_cb && conn->xprt_done_cb(conn) < 0) |
| 150 | return; |
| 151 | |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 152 | /* The wake callback is normally used to notify the data layer about |
| 153 | * data layer activity (successful send/recv), connection establishment, |
| 154 | * shutdown and fatal errors. We need to consider the following |
| 155 | * situations to wake up the data layer : |
| 156 | * - change among the CO_FL_NOTIFY_DATA flags : |
| 157 | * {DATA,SOCK}_{RD,WR}_SH, ERROR, |
| 158 | * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the |
| 159 | * end of handshake and transition to CONNECTED |
| 160 | * - raise of CONNECTED with HANDSHAKE down |
| 161 | * - end of HANDSHAKE with CONNECTED set |
| 162 | * - regular data layer activity |
| 163 | * |
| 164 | * Note that the wake callback is allowed to release the connection and |
| 165 | * the fd (and return < 0 in this case). |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 166 | */ |
Willy Tarreau | 9fa1ee6 | 2017-03-18 15:39:57 +0100 | [diff] [blame] | 167 | if ((((conn->flags ^ flags) & CO_FL_NOTIFY_DATA) || |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 168 | ((flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) != CO_FL_CONNECTED && |
| 169 | (conn->flags & (CO_FL_CONNECTED|CO_FL_HANDSHAKE)) == CO_FL_CONNECTED)) && |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 170 | conn->data->wake(conn) < 0) |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 171 | return; |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 172 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 173 | /* remove the events before leaving */ |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 174 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 175 | |
| 176 | /* commit polling changes */ |
| 177 | conn_cond_update_polling(conn); |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 178 | return; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 179 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 181 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 182 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 183 | * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*. |
| 184 | * 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] | 185 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 186 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 187 | void conn_update_data_polling(struct connection *c) |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 188 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 189 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 190 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 191 | if (!conn_ctrl_ready(c)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 192 | return; |
| 193 | |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 194 | /* update read status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 195 | if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 196 | fd_want_recv(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 197 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 198 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 199 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 200 | fd_stop_recv(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 201 | f &= ~CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 202 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 203 | |
| 204 | /* update write status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 205 | if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 206 | fd_want_send(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 207 | f |= CO_FL_CURR_WR_ENA; |
| 208 | } |
| 209 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 210 | fd_stop_send(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 211 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 212 | } |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 213 | c->flags = f; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 217 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 218 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 219 | * 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] | 220 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 221 | */ |
| 222 | void conn_update_sock_polling(struct connection *c) |
| 223 | { |
| 224 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 225 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 226 | if (!conn_ctrl_ready(c)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 227 | return; |
| 228 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 229 | /* update read status if needed */ |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 230 | if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 231 | fd_want_recv(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 232 | f |= CO_FL_CURR_RD_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 233 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 234 | else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 235 | fd_stop_recv(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 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 | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 240 | if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 241 | fd_want_send(c->handle.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 | } |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 244 | else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 245 | fd_stop_send(c->handle.fd); |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 246 | f &= ~CO_FL_CURR_WR_ENA; |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 247 | } |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 248 | c->flags = f; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 249 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 250 | |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 251 | /* Send a message over an established connection. It makes use of send() and |
| 252 | * returns the same return code and errno. If the socket layer is not ready yet |
| 253 | * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked |
| 254 | * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns |
| 255 | * EMSGSIZE if called with a zero length message. The purpose is to simplify |
| 256 | * some rare attempts to directly write on the socket from above the connection |
| 257 | * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send". |
| 258 | * It automatically retries on EINTR. Other errors cause the connection to be |
| 259 | * marked as in error state. It takes similar arguments as send() except the |
| 260 | * first one which is the connection instead of the file descriptor. Note, |
| 261 | * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags. |
| 262 | */ |
| 263 | int conn_sock_send(struct connection *conn, const void *buf, int len, int flags) |
| 264 | { |
| 265 | int ret; |
| 266 | |
| 267 | ret = -1; |
| 268 | errno = ENOTSOCK; |
| 269 | |
| 270 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 271 | goto fail; |
| 272 | |
| 273 | if (!conn_ctrl_ready(conn)) |
| 274 | goto fail; |
| 275 | |
| 276 | errno = EMSGSIZE; |
| 277 | if (!len) |
| 278 | goto fail; |
| 279 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 280 | if (!fd_send_ready(conn->handle.fd)) |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 281 | goto wait; |
| 282 | |
| 283 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 284 | ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 285 | } while (ret < 0 && errno == EINTR); |
| 286 | |
| 287 | |
| 288 | if (ret > 0) |
| 289 | return ret; |
| 290 | |
| 291 | if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) { |
| 292 | wait: |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 293 | fd_cant_send(conn->handle.fd); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | fail: |
| 297 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR; |
| 298 | return ret; |
| 299 | } |
| 300 | |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 301 | /* Drains possibly pending incoming data on the file descriptor attached to the |
| 302 | * connection and update the connection's flags accordingly. This is used to |
| 303 | * know whether we need to disable lingering on close. Returns non-zero if it |
| 304 | * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH |
| 305 | * flag may also be updated if the incoming shutdown was reported by the drain() |
| 306 | * function. |
| 307 | */ |
| 308 | int conn_sock_drain(struct connection *conn) |
| 309 | { |
| 310 | if (!conn_ctrl_ready(conn)) |
| 311 | return 1; |
| 312 | |
| 313 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) |
| 314 | return 1; |
| 315 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 316 | if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) { |
| 317 | fdtab[conn->handle.fd].linger_risk = 0; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 318 | } |
| 319 | else { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 320 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 321 | return 0; |
| 322 | |
| 323 | /* disable draining if we were called and have no drain function */ |
| 324 | if (!conn->ctrl->drain) { |
| 325 | __conn_data_stop_recv(conn); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 329 | if (conn->ctrl->drain(conn->handle.fd) <= 0) |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | conn->flags |= CO_FL_SOCK_RD_SH; |
| 334 | return 1; |
| 335 | } |
| 336 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 337 | /* |
| 338 | * Get data length from tlv |
| 339 | */ |
| 340 | static int get_tlv_length(const struct tlv *src) |
| 341 | { |
| 342 | return (src->length_hi << 8) | src->length_lo; |
| 343 | } |
| 344 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 345 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 346 | * raw data stream. The header looks like this : |
| 347 | * |
| 348 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 349 | * |
| 350 | * There must be exactly one space between each field. Fields are : |
| 351 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 352 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 353 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 354 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 355 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 356 | * |
| 357 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 358 | * |
| 359 | * The header line is small and in all cases smaller than the smallest normal |
| 360 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 361 | * can safely use MSG_PEEK and avoid buffering. |
| 362 | * |
| 363 | * Once the data is fetched, the values are set in the connection's address |
| 364 | * fields, and data are removed from the socket's buffer. The function returns |
| 365 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 366 | * and removed itself. |
| 367 | */ |
| 368 | int conn_recv_proxy(struct connection *conn, int flag) |
| 369 | { |
| 370 | char *line, *end; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 371 | struct proxy_hdr_v2 *hdr_v2; |
| 372 | const char v2sig[] = PP2_SIGNATURE; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 373 | int tlv_length = 0; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 374 | int tlv_offset = 0; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 375 | |
| 376 | /* we might have been called just after an asynchronous shutr */ |
| 377 | if (conn->flags & CO_FL_SOCK_RD_SH) |
| 378 | goto fail; |
| 379 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 380 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 381 | goto fail; |
| 382 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 383 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 384 | return 0; |
| 385 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 386 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 387 | trash.len = recv(conn->handle.fd, trash.str, trash.size, MSG_PEEK); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 388 | if (trash.len < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 389 | if (errno == EINTR) |
| 390 | continue; |
| 391 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 392 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 393 | return 0; |
| 394 | } |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 395 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 396 | } |
| 397 | } while (0); |
| 398 | |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 399 | if (!trash.len) { |
| 400 | /* client shutdown */ |
| 401 | conn->err_code = CO_ER_PRX_EMPTY; |
| 402 | goto fail; |
| 403 | } |
| 404 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 405 | if (trash.len < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 406 | goto missing; |
| 407 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 408 | line = trash.str; |
| 409 | end = trash.str + trash.len; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 410 | |
| 411 | /* Decode a possible proxy request, fail early if it does not match */ |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 412 | if (strncmp(line, "PROXY ", 6) != 0) |
| 413 | goto not_v1; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 414 | |
| 415 | line += 6; |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 416 | if (trash.len < 9) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 417 | goto missing; |
| 418 | |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 419 | if (memcmp(line, "TCP4 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 420 | u32 src3, dst3, sport, dport; |
| 421 | |
| 422 | line += 5; |
| 423 | |
| 424 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 425 | if (line == end) |
| 426 | goto missing; |
| 427 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 428 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 429 | |
| 430 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 431 | if (line == end) |
| 432 | goto missing; |
| 433 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 434 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 435 | |
| 436 | sport = read_uint((const char **)&line, end); |
| 437 | if (line == end) |
| 438 | goto missing; |
| 439 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 440 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 441 | |
| 442 | dport = read_uint((const char **)&line, end); |
| 443 | if (line > end - 2) |
| 444 | goto missing; |
| 445 | if (*line++ != '\r') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 446 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 447 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 448 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 449 | |
| 450 | /* update the session's addresses and mark them set */ |
| 451 | ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET; |
| 452 | ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3); |
| 453 | ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport); |
| 454 | |
| 455 | ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET; |
| 456 | ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3); |
| 457 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport); |
| 458 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 459 | } |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 460 | else if (memcmp(line, "TCP6 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 461 | u32 sport, dport; |
| 462 | char *src_s; |
| 463 | char *dst_s, *sport_s, *dport_s; |
| 464 | struct in6_addr src3, dst3; |
| 465 | |
| 466 | line += 5; |
| 467 | |
| 468 | src_s = line; |
| 469 | dst_s = sport_s = dport_s = NULL; |
| 470 | while (1) { |
| 471 | if (line > end - 2) { |
| 472 | goto missing; |
| 473 | } |
| 474 | else if (*line == '\r') { |
| 475 | *line = 0; |
| 476 | line++; |
| 477 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 478 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | |
| 482 | if (*line == ' ') { |
| 483 | *line = 0; |
| 484 | if (!dst_s) |
| 485 | dst_s = line + 1; |
| 486 | else if (!sport_s) |
| 487 | sport_s = line + 1; |
| 488 | else if (!dport_s) |
| 489 | dport_s = line + 1; |
| 490 | } |
| 491 | line++; |
| 492 | } |
| 493 | |
| 494 | if (!dst_s || !sport_s || !dport_s) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 495 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 496 | |
| 497 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 498 | if (*sport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 499 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 500 | |
| 501 | dport = read_uint((const char **)&dport_s,line - 2); |
| 502 | if (*dport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 503 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 504 | |
| 505 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 506 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 507 | |
| 508 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 509 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 510 | |
| 511 | /* update the session's addresses and mark them set */ |
| 512 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6; |
| 513 | memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 514 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport); |
| 515 | |
| 516 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6; |
| 517 | memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 518 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport); |
| 519 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 520 | } |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 521 | else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) { |
| 522 | /* This can be a UNIX socket forwarded by an haproxy upstream */ |
| 523 | line += 9; |
| 524 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 525 | else { |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 526 | /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 527 | conn->err_code = CO_ER_PRX_BAD_PROTO; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 528 | goto fail; |
| 529 | } |
| 530 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 531 | trash.len = line - trash.str; |
| 532 | goto eat_header; |
| 533 | |
| 534 | not_v1: |
| 535 | /* try PPv2 */ |
| 536 | if (trash.len < PP2_HEADER_LEN) |
| 537 | goto missing; |
| 538 | |
| 539 | hdr_v2 = (struct proxy_hdr_v2 *)trash.str; |
| 540 | |
| 541 | if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 || |
| 542 | (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) { |
| 543 | conn->err_code = CO_ER_PRX_NOT_HDR; |
| 544 | goto fail; |
| 545 | } |
| 546 | |
| 547 | if (trash.len < PP2_HEADER_LEN + ntohs(hdr_v2->len)) |
| 548 | goto missing; |
| 549 | |
| 550 | switch (hdr_v2->ver_cmd & PP2_CMD_MASK) { |
| 551 | case 0x01: /* PROXY command */ |
| 552 | switch (hdr_v2->fam) { |
| 553 | case 0x11: /* TCPv4 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 554 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET) |
| 555 | goto bad_header; |
| 556 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 557 | ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET; |
| 558 | ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr; |
| 559 | ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_v2->addr.ip4.src_port; |
| 560 | ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET; |
| 561 | ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr; |
| 562 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_v2->addr.ip4.dst_port; |
| 563 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 564 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 565 | tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 566 | break; |
| 567 | case 0x21: /* TCPv6 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 568 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6) |
| 569 | goto bad_header; |
| 570 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 571 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6; |
| 572 | memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16); |
| 573 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_v2->addr.ip6.src_port; |
| 574 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6; |
| 575 | memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16); |
| 576 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_v2->addr.ip6.dst_port; |
| 577 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 578 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 579 | tlv_length = ntohs(hdr_v2->len) - PP2_ADDR_LEN_INET6; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 580 | break; |
| 581 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 582 | |
| 583 | /* TLV parsing */ |
| 584 | if (tlv_length > 0) { |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 585 | while (tlv_offset + TLV_HEADER_SIZE <= trash.len) { |
| 586 | const struct tlv *tlv_packet = (struct tlv *) &trash.str[tlv_offset]; |
| 587 | const int tlv_len = get_tlv_length(tlv_packet); |
| 588 | tlv_offset += tlv_len + TLV_HEADER_SIZE; |
| 589 | |
| 590 | switch (tlv_packet->type) { |
| 591 | #ifdef CONFIG_HAP_NS |
| 592 | case PP2_TYPE_NETNS: { |
| 593 | const struct netns_entry *ns; |
| 594 | ns = netns_store_lookup((char*)tlv_packet->value, tlv_len); |
| 595 | if (ns) |
| 596 | conn->proxy_netns = ns; |
| 597 | break; |
| 598 | } |
| 599 | #endif |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 606 | /* unsupported protocol, keep local connection address */ |
| 607 | break; |
| 608 | case 0x00: /* LOCAL command */ |
| 609 | /* keep local connection address for LOCAL */ |
| 610 | break; |
| 611 | default: |
| 612 | goto bad_header; /* not a supported command */ |
| 613 | } |
| 614 | |
| 615 | trash.len = PP2_HEADER_LEN + ntohs(hdr_v2->len); |
| 616 | goto eat_header; |
| 617 | |
| 618 | eat_header: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 619 | /* remove the PROXY line from the request. For this we re-read the |
| 620 | * exact line at once. If we don't get the exact same result, we |
| 621 | * fail. |
| 622 | */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 623 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 624 | int len2 = recv(conn->handle.fd, trash.str, trash.len, 0); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 625 | if (len2 < 0 && errno == EINTR) |
| 626 | continue; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 627 | if (len2 != trash.len) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 628 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 629 | } while (0); |
| 630 | |
| 631 | conn->flags &= ~flag; |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 632 | conn->flags |= CO_FL_RCVD_PROXY; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 633 | return 1; |
| 634 | |
| 635 | missing: |
| 636 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 637 | * we have not read anything. Otherwise we need to fail because we won't |
| 638 | * be able to poll anymore. |
| 639 | */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 640 | conn->err_code = CO_ER_PRX_TRUNCATED; |
| 641 | goto fail; |
| 642 | |
| 643 | bad_header: |
| 644 | /* This is not a valid proxy protocol header */ |
| 645 | conn->err_code = CO_ER_PRX_BAD_HDR; |
| 646 | goto fail; |
| 647 | |
| 648 | recv_abort: |
| 649 | conn->err_code = CO_ER_PRX_ABORT; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 650 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 651 | goto fail; |
| 652 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 653 | fail: |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 654 | __conn_sock_stop_both(conn); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 655 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 659 | /* This handshake handler waits a NetScaler Client IP insertion header |
| 660 | * at the beginning of the raw data stream. The header looks like this: |
| 661 | * |
| 662 | * 4 bytes: CIP magic number |
| 663 | * 4 bytes: Header length |
| 664 | * 20+ bytes: Header of the last IP packet sent by the client during |
| 665 | * TCP handshake. |
| 666 | * 20+ bytes: Header of the last TCP packet sent by the client during |
| 667 | * TCP handshake. |
| 668 | * |
| 669 | * This line MUST be at the beginning of the buffer and MUST NOT be |
| 670 | * fragmented. |
| 671 | * |
| 672 | * The header line is small and in all cases smaller than the smallest normal |
| 673 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 674 | * can safely use MSG_PEEK and avoid buffering. |
| 675 | * |
| 676 | * Once the data is fetched, the values are set in the connection's address |
| 677 | * fields, and data are removed from the socket's buffer. The function returns |
| 678 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 679 | * and removed itself. |
| 680 | */ |
| 681 | int conn_recv_netscaler_cip(struct connection *conn, int flag) |
| 682 | { |
| 683 | char *line; |
| 684 | uint32_t cip_magic; |
| 685 | uint32_t cip_len; |
| 686 | uint8_t ip_v; |
| 687 | |
| 688 | /* we might have been called just after an asynchronous shutr */ |
| 689 | if (conn->flags & CO_FL_SOCK_RD_SH) |
| 690 | goto fail; |
| 691 | |
| 692 | if (!conn_ctrl_ready(conn)) |
| 693 | goto fail; |
| 694 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 695 | if (!fd_recv_ready(conn->handle.fd)) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 696 | return 0; |
| 697 | |
| 698 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 699 | trash.len = recv(conn->handle.fd, trash.str, trash.size, MSG_PEEK); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 700 | if (trash.len < 0) { |
| 701 | if (errno == EINTR) |
| 702 | continue; |
| 703 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 704 | fd_cant_recv(conn->handle.fd); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | goto recv_abort; |
| 708 | } |
| 709 | } while (0); |
| 710 | |
| 711 | if (!trash.len) { |
| 712 | /* client shutdown */ |
| 713 | conn->err_code = CO_ER_CIP_EMPTY; |
| 714 | goto fail; |
| 715 | } |
| 716 | |
| 717 | /* Fail if buffer length is not large enough to contain |
| 718 | * CIP magic, CIP length */ |
| 719 | if (trash.len < 8) |
| 720 | goto missing; |
| 721 | |
| 722 | line = trash.str; |
| 723 | |
| 724 | cip_magic = ntohl(*(uint32_t *)line); |
| 725 | cip_len = ntohl(*(uint32_t *)(line+4)); |
| 726 | |
| 727 | /* Decode a possible NetScaler Client IP request, fail early if |
| 728 | * it does not match */ |
| 729 | if (cip_magic != objt_listener(conn->target)->bind_conf->ns_cip_magic) |
| 730 | goto bad_magic; |
| 731 | |
| 732 | /* Fail if buffer length is not large enough to contain |
| 733 | * CIP magic, CIP length, minimal IP header */ |
| 734 | if (trash.len < 28) |
| 735 | goto missing; |
| 736 | |
| 737 | line += 8; |
| 738 | |
| 739 | /* Get IP version from the first four bits */ |
| 740 | ip_v = (*line & 0xf0) >> 4; |
| 741 | |
| 742 | if (ip_v == 4) { |
| 743 | struct ip *hdr_ip4; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 744 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 745 | |
| 746 | hdr_ip4 = (struct ip *)line; |
| 747 | |
| 748 | if (trash.len < (8 + ntohs(hdr_ip4->ip_len))) { |
| 749 | /* Fail if buffer length is not large enough to contain |
| 750 | * CIP magic, CIP length, IPv4 header */ |
| 751 | goto missing; |
| 752 | } else if (hdr_ip4->ip_p != IPPROTO_TCP) { |
| 753 | /* The protocol does not include a TCP header */ |
| 754 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 755 | goto fail; |
| 756 | } else if (trash.len < (28 + ntohs(hdr_ip4->ip_len))) { |
| 757 | /* Fail if buffer length is not large enough to contain |
| 758 | * CIP magic, CIP length, IPv4 header, TCP header */ |
| 759 | goto missing; |
| 760 | } |
| 761 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 762 | hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 763 | |
| 764 | /* update the session's addresses and mark them set */ |
| 765 | ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET; |
| 766 | ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr; |
| 767 | ((struct sockaddr_in *)&conn->addr.from)->sin_port = hdr_tcp->source; |
| 768 | |
| 769 | ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET; |
| 770 | ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr; |
| 771 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = hdr_tcp->dest; |
| 772 | |
| 773 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 774 | } |
| 775 | else if (ip_v == 6) { |
| 776 | struct ip6_hdr *hdr_ip6; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 777 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 778 | |
| 779 | hdr_ip6 = (struct ip6_hdr *)line; |
| 780 | |
| 781 | if (trash.len < 28) { |
| 782 | /* Fail if buffer length is not large enough to contain |
| 783 | * CIP magic, CIP length, IPv6 header */ |
| 784 | goto missing; |
| 785 | } else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) { |
| 786 | /* The protocol does not include a TCP header */ |
| 787 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 788 | goto fail; |
| 789 | } else if (trash.len < 48) { |
| 790 | /* Fail if buffer length is not large enough to contain |
| 791 | * CIP magic, CIP length, IPv6 header, TCP header */ |
| 792 | goto missing; |
| 793 | } |
| 794 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 795 | hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 796 | |
| 797 | /* update the session's addresses and mark them set */ |
| 798 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6; |
| 799 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr = hdr_ip6->ip6_src; |
| 800 | ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = hdr_tcp->source; |
| 801 | |
| 802 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6; |
| 803 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr = hdr_ip6->ip6_dst; |
| 804 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = hdr_tcp->dest; |
| 805 | |
| 806 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 807 | } |
| 808 | else { |
| 809 | /* The protocol does not match something known (IPv4/IPv6) */ |
| 810 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 811 | goto fail; |
| 812 | } |
| 813 | |
| 814 | line += cip_len; |
| 815 | trash.len = line - trash.str; |
| 816 | |
| 817 | /* remove the NetScaler Client IP header from the request. For this |
| 818 | * we re-read the exact line at once. If we don't get the exact same |
| 819 | * result, we fail. |
| 820 | */ |
| 821 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 822 | int len2 = recv(conn->handle.fd, trash.str, trash.len, 0); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 823 | if (len2 < 0 && errno == EINTR) |
| 824 | continue; |
| 825 | if (len2 != trash.len) |
| 826 | goto recv_abort; |
| 827 | } while (0); |
| 828 | |
| 829 | conn->flags &= ~flag; |
| 830 | return 1; |
| 831 | |
| 832 | missing: |
| 833 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 834 | * we have not read anything. Otherwise we need to fail because we won't |
| 835 | * be able to poll anymore. |
| 836 | */ |
| 837 | conn->err_code = CO_ER_CIP_TRUNCATED; |
| 838 | goto fail; |
| 839 | |
| 840 | bad_magic: |
| 841 | conn->err_code = CO_ER_CIP_BAD_MAGIC; |
| 842 | goto fail; |
| 843 | |
| 844 | recv_abort: |
| 845 | conn->err_code = CO_ER_CIP_ABORT; |
| 846 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 847 | goto fail; |
| 848 | |
| 849 | fail: |
| 850 | __conn_sock_stop_both(conn); |
| 851 | conn->flags |= CO_FL_ERROR; |
| 852 | return 0; |
| 853 | } |
| 854 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 855 | int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote) |
| 856 | { |
| 857 | int ret = 0; |
| 858 | |
| 859 | if (srv && (srv->pp_opts & SRV_PP_V2)) { |
| 860 | ret = make_proxy_line_v2(buf, buf_len, srv, remote); |
| 861 | } |
| 862 | else { |
| 863 | if (remote) |
| 864 | ret = make_proxy_line_v1(buf, buf_len, &remote->addr.from, &remote->addr.to); |
| 865 | else |
| 866 | ret = make_proxy_line_v1(buf, buf_len, NULL, NULL); |
| 867 | } |
| 868 | |
| 869 | return ret; |
| 870 | } |
| 871 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 872 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 873 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 874 | * It returns the number of bytes composing this line (including the trailing |
| 875 | * 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] | 876 | * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is |
| 877 | * emitted as well. |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 878 | */ |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 879 | int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 880 | { |
| 881 | int ret = 0; |
| 882 | |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 883 | 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] | 884 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 "); |
| 885 | if (ret >= buf_len) |
| 886 | return 0; |
| 887 | |
| 888 | /* IPv4 src */ |
| 889 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret)) |
| 890 | return 0; |
| 891 | |
| 892 | ret += strlen(buf + ret); |
| 893 | if (ret >= buf_len) |
| 894 | return 0; |
| 895 | |
| 896 | buf[ret++] = ' '; |
| 897 | |
| 898 | /* IPv4 dst */ |
| 899 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret)) |
| 900 | return 0; |
| 901 | |
| 902 | ret += strlen(buf + ret); |
| 903 | if (ret >= buf_len) |
| 904 | return 0; |
| 905 | |
| 906 | /* source and destination ports */ |
| 907 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 908 | ntohs(((struct sockaddr_in *)src)->sin_port), |
| 909 | ntohs(((struct sockaddr_in *)dst)->sin_port)); |
| 910 | if (ret >= buf_len) |
| 911 | return 0; |
| 912 | } |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 913 | 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] | 914 | ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 "); |
| 915 | if (ret >= buf_len) |
| 916 | return 0; |
| 917 | |
| 918 | /* IPv6 src */ |
| 919 | if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret)) |
| 920 | return 0; |
| 921 | |
| 922 | ret += strlen(buf + ret); |
| 923 | if (ret >= buf_len) |
| 924 | return 0; |
| 925 | |
| 926 | buf[ret++] = ' '; |
| 927 | |
| 928 | /* IPv6 dst */ |
| 929 | if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret)) |
| 930 | return 0; |
| 931 | |
| 932 | ret += strlen(buf + ret); |
| 933 | if (ret >= buf_len) |
| 934 | return 0; |
| 935 | |
| 936 | /* source and destination ports */ |
| 937 | ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n", |
| 938 | ntohs(((struct sockaddr_in6 *)src)->sin6_port), |
| 939 | ntohs(((struct sockaddr_in6 *)dst)->sin6_port)); |
| 940 | if (ret >= buf_len) |
| 941 | return 0; |
| 942 | } |
| 943 | else { |
| 944 | /* unknown family combination */ |
| 945 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
| 946 | if (ret >= buf_len) |
| 947 | return 0; |
| 948 | } |
| 949 | return ret; |
| 950 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 951 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 952 | #if defined(USE_OPENSSL) || defined(CONFIG_HAP_NS) |
| 953 | static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 954 | { |
| 955 | struct tlv *tlv; |
| 956 | |
| 957 | if (!dest || (length + sizeof(*tlv) > dest_len)) |
| 958 | return 0; |
| 959 | |
| 960 | tlv = (struct tlv *)dest; |
| 961 | |
| 962 | tlv->type = type; |
| 963 | tlv->length_hi = length >> 8; |
| 964 | tlv->length_lo = length & 0x00ff; |
| 965 | memcpy(tlv->value, value, length); |
| 966 | return length + sizeof(*tlv); |
| 967 | } |
| 968 | #endif |
| 969 | |
| 970 | int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote) |
| 971 | { |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 972 | const char pp2_signature[] = PP2_SIGNATURE; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 973 | int ret = 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 974 | struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 975 | struct sockaddr_storage null_addr = { .ss_family = 0 }; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 976 | struct sockaddr_storage *src = &null_addr; |
| 977 | struct sockaddr_storage *dst = &null_addr; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 978 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 979 | #ifdef USE_OPENSSL |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 980 | char *value = NULL; |
| 981 | struct tlv_ssl *tlv; |
| 982 | int ssl_tlv_len = 0; |
Dave McCowan | 77d1f01 | 2014-07-17 14:34:01 -0400 | [diff] [blame] | 983 | struct chunk *cn_trash; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 984 | #endif |
| 985 | |
| 986 | if (buf_len < PP2_HEADER_LEN) |
| 987 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 988 | memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 989 | |
| 990 | if (remote) { |
| 991 | src = &remote->addr.from; |
| 992 | dst = &remote->addr.to; |
| 993 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 994 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 995 | if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET) { |
| 996 | if (buf_len < PP2_HDR_LEN_INET) |
| 997 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 998 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY; |
| 999 | hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM; |
| 1000 | hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr; |
| 1001 | hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
| 1002 | hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1003 | hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1004 | ret = PP2_HDR_LEN_INET; |
| 1005 | } |
| 1006 | else if (src && dst && src->ss_family == dst->ss_family && src->ss_family == AF_INET6) { |
| 1007 | if (buf_len < PP2_HDR_LEN_INET6) |
| 1008 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1009 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY; |
| 1010 | hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM; |
| 1011 | memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16); |
| 1012 | memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16); |
| 1013 | hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1014 | hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1015 | ret = PP2_HDR_LEN_INET6; |
| 1016 | } |
| 1017 | else { |
| 1018 | if (buf_len < PP2_HDR_LEN_UNSPEC) |
| 1019 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1020 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL; |
| 1021 | hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1022 | ret = PP2_HDR_LEN_UNSPEC; |
| 1023 | } |
| 1024 | |
| 1025 | #ifdef USE_OPENSSL |
| 1026 | if (srv->pp_opts & SRV_PP_V2_SSL) { |
| 1027 | if ((buf_len - ret) < sizeof(struct tlv_ssl)) |
| 1028 | return 0; |
| 1029 | tlv = (struct tlv_ssl *)&buf[ret]; |
| 1030 | memset(tlv, 0, sizeof(struct tlv_ssl)); |
| 1031 | ssl_tlv_len += sizeof(struct tlv_ssl); |
| 1032 | tlv->tlv.type = PP2_TYPE_SSL; |
| 1033 | if (ssl_sock_is_ssl(remote)) { |
| 1034 | tlv->client |= PP2_CLIENT_SSL; |
| 1035 | value = ssl_sock_get_version(remote); |
| 1036 | if (value) { |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1037 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_TYPE_SSL_VERSION, strlen(value), value); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1038 | } |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1039 | if (ssl_sock_get_cert_used_sess(remote)) { |
| 1040 | tlv->client |= PP2_CLIENT_CERT_SESS; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1041 | tlv->verify = htonl(ssl_sock_get_verify_result(remote)); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1042 | if (ssl_sock_get_cert_used_conn(remote)) |
| 1043 | tlv->client |= PP2_CLIENT_CERT_CONN; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1044 | } |
| 1045 | if (srv->pp_opts & SRV_PP_V2_SSL_CN) { |
Dave McCowan | 77d1f01 | 2014-07-17 14:34:01 -0400 | [diff] [blame] | 1046 | cn_trash = get_trash_chunk(); |
Willy Tarreau | 3b9a0c9 | 2014-07-19 06:37:33 +0200 | [diff] [blame] | 1047 | if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) { |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1048 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_TYPE_SSL_CN, cn_trash->len, cn_trash->str); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8; |
| 1053 | tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff; |
| 1054 | ret += ssl_tlv_len; |
| 1055 | } |
| 1056 | #endif |
| 1057 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1058 | #ifdef CONFIG_HAP_NS |
| 1059 | if (remote && (remote->proxy_netns)) { |
| 1060 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1061 | return 0; |
| 1062 | ret += make_tlv(&buf[ret], buf_len, PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key); |
| 1063 | } |
| 1064 | #endif |
| 1065 | |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1066 | hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN)); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1067 | |
| 1068 | return ret; |
| 1069 | } |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1070 | |
| 1071 | /* fetch if the received connection used a PROXY protocol header */ |
| 1072 | int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1073 | { |
| 1074 | struct connection *conn; |
| 1075 | |
| 1076 | conn = objt_conn(smp->sess->origin); |
| 1077 | if (!conn) |
| 1078 | return 0; |
| 1079 | |
| 1080 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 1081 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | smp->flags = 0; |
| 1086 | smp->data.type = SMP_T_BOOL; |
| 1087 | smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0; |
| 1088 | |
| 1089 | return 1; |
| 1090 | } |
| 1091 | |
| 1092 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1093 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1094 | * common denominator, the type that can be casted into all other ones. For |
| 1095 | * instance v4/v6 must be declared v4. |
| 1096 | */ |
| 1097 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 1098 | { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI }, |
| 1099 | { /* END */ }, |
| 1100 | }}; |
| 1101 | |
| 1102 | |
| 1103 | __attribute__((constructor)) |
| 1104 | static void __connection_init(void) |
| 1105 | { |
| 1106 | sample_register_fetches(&sample_fetch_keywords); |
| 1107 | } |