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