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