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 | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 16 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 17 | #include <haproxy/connection.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/fd.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 19 | #include <haproxy/frontend.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/hash.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/log-t.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 22 | #include <haproxy/namespace.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 23 | #include <haproxy/net_helper.h> |
Willy Tarreau | fc77454 | 2020-06-04 17:31:04 +0200 | [diff] [blame] | 24 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 25 | #include <haproxy/sample.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 26 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 27 | #include <haproxy/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 28 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 29 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 30 | DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection)); |
| 31 | DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream)); |
| 32 | DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node)); |
| 33 | DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage)); |
| 34 | DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX); |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 36 | struct idle_conns idle_conns[MAX_THREADS] = { }; |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 37 | struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, }; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 38 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 39 | /* List head of all known muxes for PROTO */ |
| 40 | struct mux_proto_list mux_proto_list = { |
| 41 | .list = LIST_HEAD_INIT(mux_proto_list.list) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 44 | /* disables sending of proxy-protocol-v2's LOCAL command */ |
| 45 | static int pp2_never_send_local; |
| 46 | |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 47 | int conn_create_mux(struct connection *conn) |
| 48 | { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 49 | if (conn_is_back(conn)) { |
| 50 | struct server *srv; |
| 51 | struct conn_stream *cs = conn->ctx; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 52 | struct session *sess = conn->owner; |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 53 | |
| 54 | if (conn->flags & CO_FL_ERROR) |
| 55 | goto fail; |
Olivier Houchard | a8a415d | 2020-01-23 13:15:14 +0100 | [diff] [blame] | 56 | |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 57 | if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) { |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 58 | if (conn_install_mux_chk(conn, conn->ctx, sess) < 0) |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 59 | goto fail; |
| 60 | } |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 61 | else if (conn_install_mux_be(conn, conn->ctx, sess) < 0) |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 62 | goto fail; |
| 63 | srv = objt_server(conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 64 | |
| 65 | /* If we're doing http-reuse always, and the connection is not |
| 66 | * private with available streams (an http2 connection), add it |
| 67 | * to the available list, so that others can use it right |
| 68 | * away. If the connection is private, add it in the session |
| 69 | * server list. |
| 70 | */ |
Christopher Faulet | 2883fcf | 2020-07-01 14:59:43 +0200 | [diff] [blame] | 71 | if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) && |
Christopher Faulet | aa27853 | 2020-06-30 14:47:46 +0200 | [diff] [blame] | 72 | !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0) |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 73 | ebmb_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 74 | else if (conn->flags & CO_FL_PRIVATE) { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 75 | /* If it fail now, the same will be done in mux->detach() callback */ |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 76 | session_add_conn(sess, conn, conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 77 | } |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | fail: |
| 80 | /* let the upper layer know the connection failed */ |
| 81 | cs->data_cb->wake(cs); |
| 82 | return -1; |
| 83 | } else |
| 84 | return conn_complete_session(conn); |
| 85 | |
| 86 | } |
| 87 | |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 88 | /* Send a message over an established connection. It makes use of send() and |
| 89 | * returns the same return code and errno. If the socket layer is not ready yet |
| 90 | * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked |
| 91 | * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns |
| 92 | * EMSGSIZE if called with a zero length message. The purpose is to simplify |
| 93 | * some rare attempts to directly write on the socket from above the connection |
| 94 | * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send". |
| 95 | * It automatically retries on EINTR. Other errors cause the connection to be |
| 96 | * marked as in error state. It takes similar arguments as send() except the |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 97 | * first one which is the connection instead of the file descriptor. <flags> |
| 98 | * only support CO_SFL_MSG_MORE. |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 99 | */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 100 | int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags) |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 101 | { |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 102 | const struct buffer buffer = b_make((char*)buf, len, 0, len); |
| 103 | const struct xprt_ops *xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | ret = -1; |
| 107 | errno = ENOTSOCK; |
| 108 | |
| 109 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 110 | goto fail; |
| 111 | |
| 112 | if (!conn_ctrl_ready(conn)) |
| 113 | goto fail; |
| 114 | |
| 115 | errno = EMSGSIZE; |
| 116 | if (!len) |
| 117 | goto fail; |
| 118 | |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 119 | /* snd_buf() already takes care of updating conn->flags and handling |
| 120 | * the FD polling status. |
| 121 | */ |
| 122 | ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags); |
| 123 | if (conn->flags & CO_FL_ERROR) |
| 124 | ret = -1; |
| 125 | return ret; |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 126 | fail: |
| 127 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR; |
| 128 | return ret; |
| 129 | } |
| 130 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 131 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 132 | * The event subscriber <es> is not allowed to change from a previous call as |
| 133 | * long as at least one event is still subscribed. The <event_type> must only |
| 134 | * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 135 | */ |
| 136 | int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 137 | { |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 138 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 139 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 140 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 141 | es->events &= ~event_type; |
| 142 | if (!es->events) |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 143 | conn->subs = NULL; |
| 144 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 145 | if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events) |
| 146 | conn->ctrl->ignore_events(conn, event_type); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 147 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 151 | /* Called from the upper layer, to subscribe <es> to events <event_type>. |
| 152 | * The <es> struct is not allowed to differ from the one passed during a |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 153 | * previous call to subscribe(). If the connection's ctrl layer is ready, |
| 154 | * the wait_event is immediately woken up and the subcription is cancelled. |
| 155 | * It always returns zero. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 156 | */ |
| 157 | int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 158 | { |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 159 | int ret = 0; |
| 160 | |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 161 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 162 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 163 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 164 | if (conn->subs && (conn->subs->events & event_type) == event_type) |
| 165 | return 0; |
| 166 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 167 | if (conn_ctrl_ready(conn) && conn->ctrl->check_events) { |
| 168 | ret = conn->ctrl->check_events(conn, event_type); |
| 169 | if (ret) |
| 170 | tasklet_wakeup(es->tasklet); |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 171 | } |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 172 | |
| 173 | es->events = (es->events | event_type) & ~ret; |
| 174 | conn->subs = es->events ? es : NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 175 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 178 | /* Drains possibly pending incoming data on the connection and update the flags |
| 179 | * accordingly. This is used to know whether we need to disable lingering on |
| 180 | * close. Returns non-zero if it is safe to close without disabling lingering, |
| 181 | * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming |
| 182 | * shutdown was reported by the ->drain() function. |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 183 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 184 | int conn_ctrl_drain(struct connection *conn) |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 185 | { |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 186 | int ret = 0; |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 188 | if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) |
| 189 | ret = 1; |
| 190 | else if (conn->ctrl->drain) { |
| 191 | ret = conn->ctrl->drain(conn); |
| 192 | if (ret) |
| 193 | conn->flags |= CO_FL_SOCK_RD_SH; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 194 | } |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 195 | return ret; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 196 | } |
| 197 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Get data length from tlv |
| 200 | */ |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 201 | static inline size_t get_tlv_length(const struct tlv *src) |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 202 | { |
| 203 | return (src->length_hi << 8) | src->length_lo; |
| 204 | } |
| 205 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 206 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 207 | * raw data stream. The header looks like this : |
| 208 | * |
| 209 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 210 | * |
| 211 | * There must be exactly one space between each field. Fields are : |
| 212 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 213 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 214 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 215 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 216 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 217 | * |
| 218 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 219 | * |
| 220 | * The header line is small and in all cases smaller than the smallest normal |
| 221 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 222 | * can safely use MSG_PEEK and avoid buffering. |
| 223 | * |
| 224 | * Once the data is fetched, the values are set in the connection's address |
| 225 | * fields, and data are removed from the socket's buffer. The function returns |
| 226 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 227 | * and removed itself. |
| 228 | */ |
| 229 | int conn_recv_proxy(struct connection *conn, int flag) |
| 230 | { |
| 231 | char *line, *end; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 232 | struct proxy_hdr_v2 *hdr_v2; |
| 233 | const char v2sig[] = PP2_SIGNATURE; |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 234 | size_t total_v2_len; |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 235 | size_t tlv_offset = 0; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 236 | int ret; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 237 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 238 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 239 | goto fail; |
| 240 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 241 | if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0)) |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 242 | goto fail; |
| 243 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 244 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 245 | goto not_ready; |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 246 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 247 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 248 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 249 | if (ret < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 250 | if (errno == EINTR) |
| 251 | continue; |
| 252 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 253 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 254 | goto not_ready; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 255 | } |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 256 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 257 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 258 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 259 | break; |
| 260 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 261 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 262 | if (!trash.data) { |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 263 | /* client shutdown */ |
| 264 | conn->err_code = CO_ER_PRX_EMPTY; |
| 265 | goto fail; |
| 266 | } |
| 267 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 268 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 269 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 270 | if (trash.data < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 271 | goto missing; |
| 272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 273 | line = trash.area; |
| 274 | end = trash.area + trash.data; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 275 | |
| 276 | /* Decode a possible proxy request, fail early if it does not match */ |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 277 | if (strncmp(line, "PROXY ", 6) != 0) |
| 278 | goto not_v1; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 279 | |
| 280 | line += 6; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 281 | if (trash.data < 9) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 282 | goto missing; |
| 283 | |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 284 | if (memcmp(line, "TCP4 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 285 | u32 src3, dst3, sport, dport; |
| 286 | |
| 287 | line += 5; |
| 288 | |
| 289 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 290 | if (line == end) |
| 291 | goto missing; |
| 292 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 293 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 294 | |
| 295 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 296 | if (line == end) |
| 297 | goto missing; |
| 298 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 299 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 300 | |
| 301 | sport = read_uint((const char **)&line, end); |
| 302 | if (line == end) |
| 303 | goto missing; |
| 304 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 305 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 306 | |
| 307 | dport = read_uint((const char **)&line, end); |
| 308 | if (line > end - 2) |
| 309 | goto missing; |
| 310 | if (*line++ != '\r') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 311 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 312 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 313 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 314 | |
| 315 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 316 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 317 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3); |
| 318 | ((struct sockaddr_in *)conn->src)->sin_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 320 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 321 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3); |
| 322 | ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 323 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 324 | } |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 325 | else if (memcmp(line, "TCP6 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 326 | u32 sport, dport; |
| 327 | char *src_s; |
| 328 | char *dst_s, *sport_s, *dport_s; |
| 329 | struct in6_addr src3, dst3; |
| 330 | |
| 331 | line += 5; |
| 332 | |
| 333 | src_s = line; |
| 334 | dst_s = sport_s = dport_s = NULL; |
| 335 | while (1) { |
| 336 | if (line > end - 2) { |
| 337 | goto missing; |
| 338 | } |
| 339 | else if (*line == '\r') { |
| 340 | *line = 0; |
| 341 | line++; |
| 342 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 343 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | |
| 347 | if (*line == ' ') { |
| 348 | *line = 0; |
| 349 | if (!dst_s) |
| 350 | dst_s = line + 1; |
| 351 | else if (!sport_s) |
| 352 | sport_s = line + 1; |
| 353 | else if (!dport_s) |
| 354 | dport_s = line + 1; |
| 355 | } |
| 356 | line++; |
| 357 | } |
| 358 | |
| 359 | if (!dst_s || !sport_s || !dport_s) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 360 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 361 | |
| 362 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 363 | if (*sport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 364 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 365 | |
| 366 | dport = read_uint((const char **)&dport_s,line - 2); |
| 367 | if (*dport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 368 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 369 | |
| 370 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 371 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 372 | |
| 373 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 374 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 375 | |
| 376 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 377 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 378 | memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 379 | ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 380 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 381 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 382 | memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 383 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 384 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 385 | } |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 386 | else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) { |
| 387 | /* This can be a UNIX socket forwarded by an haproxy upstream */ |
| 388 | line += 9; |
| 389 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 390 | else { |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 391 | /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 392 | conn->err_code = CO_ER_PRX_BAD_PROTO; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 393 | goto fail; |
| 394 | } |
| 395 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 396 | trash.data = line - trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 397 | goto eat_header; |
| 398 | |
| 399 | not_v1: |
| 400 | /* try PPv2 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 401 | if (trash.data < PP2_HEADER_LEN) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 402 | goto missing; |
| 403 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 404 | hdr_v2 = (struct proxy_hdr_v2 *) trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 405 | |
| 406 | if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 || |
| 407 | (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) { |
| 408 | conn->err_code = CO_ER_PRX_NOT_HDR; |
| 409 | goto fail; |
| 410 | } |
| 411 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 412 | total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len); |
| 413 | if (trash.data < total_v2_len) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 414 | goto missing; |
| 415 | |
| 416 | switch (hdr_v2->ver_cmd & PP2_CMD_MASK) { |
| 417 | case 0x01: /* PROXY command */ |
| 418 | switch (hdr_v2->fam) { |
| 419 | case 0x11: /* TCPv4 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 420 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET) |
| 421 | goto bad_header; |
| 422 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 423 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 424 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr; |
| 425 | ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port; |
| 426 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 427 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr; |
| 428 | ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 429 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 430 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 431 | break; |
| 432 | case 0x21: /* TCPv6 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 433 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6) |
| 434 | goto bad_header; |
| 435 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 436 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 437 | memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16); |
| 438 | ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port; |
| 439 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 440 | memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16); |
| 441 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 442 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 443 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 444 | break; |
| 445 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 446 | |
| 447 | /* TLV parsing */ |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 448 | while (tlv_offset < total_v2_len) { |
| 449 | struct tlv *tlv_packet; |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 450 | size_t tlv_len; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 451 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 452 | /* Verify that we have at least TLV_HEADER_SIZE bytes left */ |
| 453 | if (tlv_offset + TLV_HEADER_SIZE > total_v2_len) |
| 454 | goto bad_header; |
| 455 | |
| 456 | tlv_packet = (struct tlv *) &trash.area[tlv_offset]; |
| 457 | tlv_len = get_tlv_length(tlv_packet); |
| 458 | tlv_offset += tlv_len + TLV_HEADER_SIZE; |
| 459 | |
| 460 | /* Verify that the TLV length does not exceed the total PROXYv2 length */ |
| 461 | if (tlv_offset > total_v2_len) |
| 462 | goto bad_header; |
| 463 | |
| 464 | switch (tlv_packet->type) { |
| 465 | case PP2_TYPE_CRC32C: { |
| 466 | uint32_t n_crc32c; |
| 467 | |
| 468 | /* Verify that this TLV is exactly 4 bytes long */ |
| 469 | if (tlv_len != 4) |
| 470 | goto bad_header; |
| 471 | |
| 472 | n_crc32c = read_n32(tlv_packet->value); |
| 473 | write_n32(tlv_packet->value, 0); // compute with CRC==0 |
| 474 | |
| 475 | if (hash_crc32c(trash.area, total_v2_len) != n_crc32c) |
| 476 | goto bad_header; |
| 477 | break; |
| 478 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 479 | #ifdef USE_NS |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 480 | case PP2_TYPE_NETNS: { |
| 481 | const struct netns_entry *ns; |
| 482 | |
| 483 | ns = netns_store_lookup((char*)tlv_packet->value, tlv_len); |
| 484 | if (ns) |
| 485 | conn->proxy_netns = ns; |
| 486 | break; |
| 487 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 488 | #endif |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 489 | case PP2_TYPE_AUTHORITY: { |
| 490 | if (tlv_len > PP2_AUTHORITY_MAX) |
| 491 | goto bad_header; |
| 492 | conn->proxy_authority = pool_alloc(pool_head_authority); |
| 493 | if (conn->proxy_authority == NULL) |
| 494 | goto fail; |
| 495 | memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len); |
| 496 | conn->proxy_authority_len = tlv_len; |
| 497 | break; |
| 498 | } |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 499 | case PP2_TYPE_UNIQUE_ID: { |
| 500 | const struct ist tlv = ist2((const char *)tlv_packet->value, tlv_len); |
| 501 | |
| 502 | if (tlv.len > UNIQUEID_LEN) |
| 503 | goto bad_header; |
Tim Duesterhus | 2b7f6c2 | 2020-03-14 13:07:05 +0100 | [diff] [blame] | 504 | conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0); |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 505 | if (!isttest(conn->proxy_unique_id)) |
| 506 | goto fail; |
| 507 | if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) { |
| 508 | /* This is technically unreachable, because we verified above |
| 509 | * that the TLV value fits. |
| 510 | */ |
| 511 | goto fail; |
| 512 | } |
| 513 | break; |
| 514 | } |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 515 | default: |
| 516 | break; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 520 | /* Verify that the PROXYv2 header ends at a TLV boundary. |
| 521 | * This is technically unreachable, because the TLV parsing already |
| 522 | * verifies that a TLV does not exceed the total length and also |
| 523 | * that there is space for a TLV header. |
| 524 | */ |
| 525 | if (tlv_offset != total_v2_len) |
| 526 | goto bad_header; |
| 527 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 528 | /* unsupported protocol, keep local connection address */ |
| 529 | break; |
| 530 | case 0x00: /* LOCAL command */ |
| 531 | /* keep local connection address for LOCAL */ |
| 532 | break; |
| 533 | default: |
| 534 | goto bad_header; /* not a supported command */ |
| 535 | } |
| 536 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 537 | trash.data = total_v2_len; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 538 | goto eat_header; |
| 539 | |
| 540 | eat_header: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 541 | /* remove the PROXY line from the request. For this we re-read the |
| 542 | * exact line at once. If we don't get the exact same result, we |
| 543 | * fail. |
| 544 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 545 | while (1) { |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 546 | ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
| 547 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 548 | if (len2 < 0 && errno == EINTR) |
| 549 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 550 | if (len2 != trash.data) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 551 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 552 | break; |
| 553 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 554 | |
| 555 | conn->flags &= ~flag; |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 556 | conn->flags |= CO_FL_RCVD_PROXY; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 557 | return 1; |
| 558 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 559 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 560 | return 0; |
| 561 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 562 | missing: |
| 563 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 564 | * we have not read anything. Otherwise we need to fail because we won't |
| 565 | * be able to poll anymore. |
| 566 | */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 567 | conn->err_code = CO_ER_PRX_TRUNCATED; |
| 568 | goto fail; |
| 569 | |
| 570 | bad_header: |
| 571 | /* This is not a valid proxy protocol header */ |
| 572 | conn->err_code = CO_ER_PRX_BAD_HDR; |
| 573 | goto fail; |
| 574 | |
| 575 | recv_abort: |
| 576 | conn->err_code = CO_ER_PRX_ABORT; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 577 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 578 | goto fail; |
| 579 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 580 | fail: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 581 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 585 | /* This handshake handler waits a NetScaler Client IP insertion header |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 586 | * at the beginning of the raw data stream. The header format is |
| 587 | * described in doc/netscaler-client-ip-insertion-protocol.txt |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 588 | * |
| 589 | * This line MUST be at the beginning of the buffer and MUST NOT be |
| 590 | * fragmented. |
| 591 | * |
| 592 | * The header line is small and in all cases smaller than the smallest normal |
| 593 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 594 | * can safely use MSG_PEEK and avoid buffering. |
| 595 | * |
| 596 | * Once the data is fetched, the values are set in the connection's address |
| 597 | * fields, and data are removed from the socket's buffer. The function returns |
| 598 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 599 | * and removed itself. |
| 600 | */ |
| 601 | int conn_recv_netscaler_cip(struct connection *conn, int flag) |
| 602 | { |
| 603 | char *line; |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 604 | uint32_t hdr_len; |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 605 | uint8_t ip_ver; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 606 | int ret; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 607 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 608 | if (!conn_ctrl_ready(conn)) |
| 609 | goto fail; |
| 610 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 611 | if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0)) |
Olivier Houchard | 1a9dbe5 | 2020-01-22 15:31:09 +0100 | [diff] [blame] | 612 | goto fail; |
| 613 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 614 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 615 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 616 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 617 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 618 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 619 | if (ret < 0) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 620 | if (errno == EINTR) |
| 621 | continue; |
| 622 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 623 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 624 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 625 | } |
| 626 | goto recv_abort; |
| 627 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 628 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 629 | break; |
| 630 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 631 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 632 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 633 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 634 | if (!trash.data) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 635 | /* client shutdown */ |
| 636 | conn->err_code = CO_ER_CIP_EMPTY; |
| 637 | goto fail; |
| 638 | } |
| 639 | |
| 640 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 641 | * CIP magic, header length or |
| 642 | * CIP magic, CIP length, CIP type, header length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 643 | if (trash.data < 12) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 644 | goto missing; |
| 645 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 646 | line = trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 647 | |
| 648 | /* Decode a possible NetScaler Client IP request, fail early if |
| 649 | * it does not match */ |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 650 | if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 651 | goto bad_magic; |
| 652 | |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 653 | /* Legacy CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 654 | if ((trash.area[8] & 0xD0) == 0x40) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 655 | hdr_len = ntohl(read_u32((line+4))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 656 | line += 8; |
| 657 | } |
| 658 | /* Standard CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 659 | else if (trash.area[8] == 0x00) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 660 | hdr_len = ntohs(read_u32((line+10))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 661 | line += 12; |
| 662 | } |
| 663 | /* Unknown CIP protocol */ |
| 664 | else { |
| 665 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 666 | goto fail; |
| 667 | } |
| 668 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 669 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 670 | * a minimal IP header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | if (trash.data < 20) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 672 | goto missing; |
| 673 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 674 | /* Get IP version from the first four bits */ |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 675 | ip_ver = (*line & 0xf0) >> 4; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 676 | |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 677 | if (ip_ver == 4) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 678 | struct ip *hdr_ip4; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 679 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 680 | |
| 681 | hdr_ip4 = (struct ip *)line; |
| 682 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 683 | if (trash.data < 40 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 684 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 685 | * IPv4 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 686 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 687 | } |
| 688 | else if (hdr_ip4->ip_p != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 689 | /* The protocol does not include a TCP header */ |
| 690 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 691 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 692 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 693 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 694 | hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 695 | |
| 696 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 697 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 698 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr; |
| 699 | ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 700 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 701 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 702 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr; |
| 703 | ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 704 | |
| 705 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 706 | } |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 707 | else if (ip_ver == 6) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 708 | struct ip6_hdr *hdr_ip6; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 709 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 710 | |
| 711 | hdr_ip6 = (struct ip6_hdr *)line; |
| 712 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | if (trash.data < 60 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 714 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 715 | * IPv6 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 716 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 717 | } |
| 718 | else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 719 | /* The protocol does not include a TCP header */ |
| 720 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 721 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 722 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 723 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 724 | hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 725 | |
| 726 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 727 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 728 | ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src; |
| 729 | ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 730 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 731 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 732 | ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst; |
| 733 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 734 | |
| 735 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 736 | } |
| 737 | else { |
| 738 | /* The protocol does not match something known (IPv4/IPv6) */ |
| 739 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 740 | goto fail; |
| 741 | } |
| 742 | |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 743 | line += hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 744 | trash.data = line - trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 745 | |
| 746 | /* remove the NetScaler Client IP header from the request. For this |
| 747 | * we re-read the exact line at once. If we don't get the exact same |
| 748 | * result, we fail. |
| 749 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 750 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 751 | int len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 752 | if (len2 < 0 && errno == EINTR) |
| 753 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 754 | if (len2 != trash.data) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 755 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 756 | break; |
| 757 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 758 | |
| 759 | conn->flags &= ~flag; |
| 760 | return 1; |
| 761 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 762 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 763 | return 0; |
| 764 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 765 | missing: |
| 766 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 767 | * we have not read anything. Otherwise we need to fail because we won't |
| 768 | * be able to poll anymore. |
| 769 | */ |
| 770 | conn->err_code = CO_ER_CIP_TRUNCATED; |
| 771 | goto fail; |
| 772 | |
| 773 | bad_magic: |
| 774 | conn->err_code = CO_ER_CIP_BAD_MAGIC; |
| 775 | goto fail; |
| 776 | |
| 777 | recv_abort: |
| 778 | conn->err_code = CO_ER_CIP_ABORT; |
| 779 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 780 | goto fail; |
| 781 | |
| 782 | fail: |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 783 | conn->flags |= CO_FL_ERROR; |
| 784 | return 0; |
| 785 | } |
| 786 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 787 | |
| 788 | int conn_send_socks4_proxy_request(struct connection *conn) |
| 789 | { |
| 790 | struct socks4_request req_line; |
| 791 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 792 | if (!conn_ctrl_ready(conn)) |
| 793 | goto out_error; |
| 794 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 795 | if (!conn_get_dst(conn)) |
| 796 | goto out_error; |
| 797 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 798 | req_line.version = 0x04; |
| 799 | req_line.command = 0x01; |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 800 | req_line.port = get_net_port(conn->dst); |
| 801 | req_line.ip = is_inet_addr(conn->dst); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 802 | memcpy(req_line.user_id, "HAProxy\0", 8); |
| 803 | |
| 804 | if (conn->send_proxy_ofs > 0) { |
| 805 | /* |
| 806 | * This is the first call to send the request |
| 807 | */ |
| 808 | conn->send_proxy_ofs = -(int)sizeof(req_line); |
| 809 | } |
| 810 | |
| 811 | if (conn->send_proxy_ofs < 0) { |
| 812 | int ret = 0; |
| 813 | |
| 814 | /* we are sending the socks4_req_line here. If the data layer |
| 815 | * has a pending write, we'll also set MSG_MORE. |
| 816 | */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 817 | ret = conn_ctrl_send( |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 818 | conn, |
| 819 | ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs), |
| 820 | -conn->send_proxy_ofs, |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 821 | (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 822 | |
| 823 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n", |
| 824 | conn->handle.fd, -conn->send_proxy_ofs, ret); |
| 825 | |
| 826 | if (ret < 0) { |
| 827 | goto out_error; |
| 828 | } |
| 829 | |
| 830 | conn->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 831 | if (conn->send_proxy_ofs != 0) { |
| 832 | goto out_wait; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /* OK we've the whole request sent */ |
| 837 | conn->flags &= ~CO_FL_SOCKS4_SEND; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 838 | |
| 839 | /* The connection is ready now, simply return and let the connection |
| 840 | * handler notify upper layers if needed. |
| 841 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 842 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 843 | |
| 844 | if (conn->flags & CO_FL_SEND_PROXY) { |
| 845 | /* |
| 846 | * Get the send_proxy_ofs ready for the send_proxy due to we are |
| 847 | * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done |
| 848 | * before sending PROXY Protocol. |
| 849 | */ |
| 850 | conn->send_proxy_ofs = 1; |
| 851 | } |
| 852 | return 1; |
| 853 | |
| 854 | out_error: |
| 855 | /* Write error on the file descriptor */ |
| 856 | conn->flags |= CO_FL_ERROR; |
| 857 | if (conn->err_code == CO_ER_NONE) { |
| 858 | conn->err_code = CO_ER_SOCKS4_SEND; |
| 859 | } |
| 860 | return 0; |
| 861 | |
| 862 | out_wait: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | int conn_recv_socks4_proxy_response(struct connection *conn) |
| 867 | { |
| 868 | char line[SOCKS4_HS_RSP_LEN]; |
| 869 | int ret; |
| 870 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 871 | if (!conn_ctrl_ready(conn)) |
| 872 | goto fail; |
| 873 | |
| 874 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 875 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 876 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 877 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 878 | /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00 |
| 879 | * Try to peek into it, before all 8 bytes ready. |
| 880 | */ |
| 881 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK); |
| 882 | |
| 883 | if (ret == 0) { |
| 884 | /* the socket has been closed or shutdown for send */ |
| 885 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n", |
| 886 | conn->handle.fd, ret, errno); |
| 887 | if (conn->err_code == CO_ER_NONE) { |
| 888 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 889 | } |
| 890 | goto fail; |
| 891 | } |
| 892 | |
| 893 | if (ret > 0) { |
| 894 | if (ret == SOCKS4_HS_RSP_LEN) { |
| 895 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 896 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 897 | }else{ |
| 898 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], first byte is [%02X], last bye is [%02X]\n", conn->handle.fd, ret, line[0], line[ret-1]); |
| 899 | } |
| 900 | } else { |
| 901 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno); |
| 902 | } |
| 903 | |
| 904 | if (ret < 0) { |
| 905 | if (errno == EINTR) { |
| 906 | continue; |
| 907 | } |
| 908 | if (errno == EAGAIN) { |
| 909 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 910 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 911 | } |
| 912 | goto recv_abort; |
| 913 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 914 | break; |
| 915 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 916 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 917 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 918 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 919 | if (ret < SOCKS4_HS_RSP_LEN) { |
| 920 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 921 | * we are not able to read enough data. |
| 922 | */ |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 923 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Base on the SOCSK4 protocol: |
| 928 | * |
| 929 | * +----+----+----+----+----+----+----+----+ |
| 930 | * | VN | CD | DSTPORT | DSTIP | |
| 931 | * +----+----+----+----+----+----+----+----+ |
| 932 | * # of bytes: 1 1 2 4 |
| 933 | * VN is the version of the reply code and should be 0. CD is the result |
| 934 | * code with one of the following values: |
| 935 | * 90: request granted |
| 936 | * 91: request rejected or failed |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 937 | * 92: request rejected because SOCKS server cannot connect to identd on the client |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 938 | * 93: request rejected because the client program and identd report different user-ids |
| 939 | * The remaining fields are ignored. |
| 940 | */ |
| 941 | if (line[1] != 90) { |
| 942 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 943 | |
| 944 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 945 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 946 | if (conn->err_code == CO_ER_NONE) { |
| 947 | conn->err_code = CO_ER_SOCKS4_DENY; |
| 948 | } |
| 949 | goto fail; |
| 950 | } |
| 951 | |
| 952 | /* remove the 8 bytes response from the stream */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 953 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 954 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0); |
| 955 | if (ret < 0 && errno == EINTR) { |
| 956 | continue; |
| 957 | } |
| 958 | if (ret != SOCKS4_HS_RSP_LEN) { |
| 959 | if (conn->err_code == CO_ER_NONE) { |
| 960 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 961 | } |
| 962 | goto fail; |
| 963 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 964 | break; |
| 965 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 966 | |
| 967 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 968 | return 1; |
| 969 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 970 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 971 | return 0; |
| 972 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 973 | recv_abort: |
| 974 | if (conn->err_code == CO_ER_NONE) { |
| 975 | conn->err_code = CO_ER_SOCKS4_ABORT; |
| 976 | } |
| 977 | conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH); |
| 978 | goto fail; |
| 979 | |
| 980 | fail: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 981 | conn->flags |= CO_FL_ERROR; |
| 982 | return 0; |
| 983 | } |
| 984 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 985 | /* Note: <remote> is explicitly allowed to be NULL */ |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 986 | int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 987 | { |
| 988 | int ret = 0; |
| 989 | |
| 990 | if (srv && (srv->pp_opts & SRV_PP_V2)) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 991 | ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 992 | } |
| 993 | else { |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 994 | if (remote && conn_get_src(remote) && conn_get_dst(remote)) |
| 995 | ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 996 | else |
| 997 | ret = make_proxy_line_v1(buf, buf_len, NULL, NULL); |
| 998 | } |
| 999 | |
| 1000 | return ret; |
| 1001 | } |
| 1002 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1003 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 1004 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 1005 | * It returns the number of bytes composing this line (including the trailing |
| 1006 | * 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] | 1007 | * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is |
| 1008 | * emitted as well. |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1009 | */ |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1010 | 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] | 1011 | { |
| 1012 | int ret = 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1013 | char * protocol; |
| 1014 | char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1015 | char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1016 | in_port_t src_port; |
| 1017 | in_port_t dst_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1018 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1019 | if ( !src |
| 1020 | || !dst |
| 1021 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1022 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
| 1023 | /* unknown family combination */ |
| 1024 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1025 | if (ret >= buf_len) |
| 1026 | return 0; |
| 1027 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1028 | return ret; |
| 1029 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1030 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1031 | /* IPv4 for both src and dst */ |
| 1032 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1033 | protocol = "TCP4"; |
| 1034 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1035 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1036 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1037 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1038 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1039 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1040 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1041 | /* IPv6 for at least one of src and dst */ |
| 1042 | else { |
| 1043 | struct in6_addr tmp; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1044 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1045 | protocol = "TCP6"; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1046 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1047 | if (src->ss_family == AF_INET) { |
| 1048 | /* Convert src to IPv6 */ |
| 1049 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1050 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1051 | } |
| 1052 | else { |
| 1053 | tmp = ((struct sockaddr_in6 *)src)->sin6_addr; |
| 1054 | src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1055 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1056 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1057 | if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1058 | return 0; |
| 1059 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1060 | if (dst->ss_family == AF_INET) { |
| 1061 | /* Convert dst to IPv6 */ |
| 1062 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1063 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1064 | } |
| 1065 | else { |
| 1066 | tmp = ((struct sockaddr_in6 *)dst)->sin6_addr; |
| 1067 | dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1068 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1069 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1070 | if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1073 | |
| 1074 | ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port)); |
| 1075 | if (ret >= buf_len) |
| 1076 | return 0; |
| 1077 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1078 | return ret; |
| 1079 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1080 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1081 | 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] | 1082 | { |
| 1083 | struct tlv *tlv; |
| 1084 | |
| 1085 | if (!dest || (length + sizeof(*tlv) > dest_len)) |
| 1086 | return 0; |
| 1087 | |
| 1088 | tlv = (struct tlv *)dest; |
| 1089 | |
| 1090 | tlv->type = type; |
| 1091 | tlv->length_hi = length >> 8; |
| 1092 | tlv->length_lo = length & 0x00ff; |
| 1093 | memcpy(tlv->value, value, length); |
| 1094 | return length + sizeof(*tlv); |
| 1095 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1096 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1097 | /* Note: <remote> is explicitly allowed to be NULL */ |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1098 | int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1099 | { |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1100 | const char pp2_signature[] = PP2_SIGNATURE; |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1101 | void *tlv_crc32c_p = NULL; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1102 | int ret = 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1103 | struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1104 | struct sockaddr_storage null_addr = { .ss_family = 0 }; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1105 | struct sockaddr_storage *src = &null_addr; |
| 1106 | struct sockaddr_storage *dst = &null_addr; |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1107 | const char *value; |
| 1108 | int value_len; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1109 | |
| 1110 | if (buf_len < PP2_HEADER_LEN) |
| 1111 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1112 | memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1113 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1114 | if (remote && conn_get_src(remote) && conn_get_dst(remote)) { |
| 1115 | src = remote->src; |
| 1116 | dst = remote->dst; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1117 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1118 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1119 | /* At least one of src or dst is not of AF_INET or AF_INET6 */ |
| 1120 | if ( !src |
| 1121 | || !dst |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1122 | || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1123 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1124 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1125 | if (buf_len < PP2_HDR_LEN_UNSPEC) |
| 1126 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1127 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL; |
| 1128 | hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1129 | ret = PP2_HDR_LEN_UNSPEC; |
| 1130 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1131 | else { |
Willy Tarreau | 02c8803 | 2020-04-14 12:54:10 +0200 | [diff] [blame] | 1132 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1133 | /* IPv4 for both src and dst */ |
| 1134 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1135 | if (buf_len < PP2_HDR_LEN_INET) |
| 1136 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1137 | hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM; |
| 1138 | hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr; |
| 1139 | hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1140 | hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
| 1141 | hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1142 | ret = PP2_HDR_LEN_INET; |
| 1143 | } |
| 1144 | /* IPv6 for at least one of src and dst */ |
| 1145 | else { |
| 1146 | struct in6_addr tmp; |
| 1147 | |
| 1148 | if (buf_len < PP2_HDR_LEN_INET6) |
| 1149 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1150 | hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM; |
| 1151 | if (src->ss_family == AF_INET) { |
| 1152 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1153 | memcpy(hdr->addr.ip6.src_addr, &tmp, 16); |
| 1154 | hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1155 | } |
| 1156 | else { |
| 1157 | memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16); |
| 1158 | hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1159 | } |
| 1160 | if (dst->ss_family == AF_INET) { |
| 1161 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1162 | memcpy(hdr->addr.ip6.dst_addr, &tmp, 16); |
William Dauchy | bd8bf67 | 2020-01-26 19:06:39 +0100 | [diff] [blame] | 1163 | hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1164 | } |
| 1165 | else { |
| 1166 | memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16); |
| 1167 | hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1168 | } |
| 1169 | |
| 1170 | ret = PP2_HDR_LEN_INET6; |
| 1171 | } |
| 1172 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1173 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1174 | if (srv->pp_opts & SRV_PP_V2_CRC32C) { |
| 1175 | uint32_t zero_crc32c = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1176 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1177 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1178 | return 0; |
| 1179 | tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value; |
| 1180 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c); |
| 1181 | } |
| 1182 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1183 | if (remote && conn_get_alpn(remote, &value, &value_len)) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1184 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1185 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1186 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value); |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1189 | if (srv->pp_opts & SRV_PP_V2_AUTHORITY) { |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1190 | value = NULL; |
| 1191 | if (remote && remote->proxy_authority) { |
| 1192 | value = remote->proxy_authority; |
| 1193 | value_len = remote->proxy_authority_len; |
| 1194 | } |
| 1195 | #ifdef USE_OPENSSL |
| 1196 | else { |
Jerome Magnin | 78891c7 | 2019-09-02 09:53:41 +0200 | [diff] [blame] | 1197 | if ((value = ssl_sock_get_sni(remote))) |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1198 | value_len = strlen(value); |
| 1199 | } |
| 1200 | #endif |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1201 | if (value) { |
| 1202 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1203 | return 0; |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1204 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Christopher Faulet | 3ab504f | 2020-05-26 15:16:01 +0200 | [diff] [blame] | 1208 | if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1209 | struct session* sess = strm_sess(strm); |
| 1210 | struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id); |
| 1211 | |
| 1212 | value = unique_id.ptr; |
| 1213 | value_len = unique_id.len; |
| 1214 | |
| 1215 | if (value_len >= 0) { |
| 1216 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1217 | return 0; |
| 1218 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value); |
| 1219 | } |
| 1220 | } |
| 1221 | |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1222 | #ifdef USE_OPENSSL |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1223 | if (srv->pp_opts & SRV_PP_V2_SSL) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1224 | struct tlv_ssl *tlv; |
| 1225 | int ssl_tlv_len = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1226 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1227 | if ((buf_len - ret) < sizeof(struct tlv_ssl)) |
| 1228 | return 0; |
| 1229 | tlv = (struct tlv_ssl *)&buf[ret]; |
| 1230 | memset(tlv, 0, sizeof(struct tlv_ssl)); |
| 1231 | ssl_tlv_len += sizeof(struct tlv_ssl); |
| 1232 | tlv->tlv.type = PP2_TYPE_SSL; |
| 1233 | if (ssl_sock_is_ssl(remote)) { |
| 1234 | tlv->client |= PP2_CLIENT_SSL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 1235 | value = ssl_sock_get_proto_version(remote); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1236 | if (value) { |
Emmanuel Hocdet | 8c0c34b | 2018-02-28 12:02:14 +0100 | [diff] [blame] | 1237 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1238 | } |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1239 | if (ssl_sock_get_cert_used_sess(remote)) { |
| 1240 | tlv->client |= PP2_CLIENT_CERT_SESS; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1241 | tlv->verify = htonl(ssl_sock_get_verify_result(remote)); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1242 | if (ssl_sock_get_cert_used_conn(remote)) |
| 1243 | tlv->client |= PP2_CLIENT_CERT_CONN; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1244 | } |
| 1245 | if (srv->pp_opts & SRV_PP_V2_SSL_CN) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1246 | struct buffer *cn_trash = get_trash_chunk(); |
Willy Tarreau | 3b9a0c9 | 2014-07-19 06:37:33 +0200 | [diff] [blame] | 1247 | if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1248 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN, |
| 1249 | cn_trash->data, |
| 1250 | cn_trash->area); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1251 | } |
| 1252 | } |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1253 | if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1254 | struct buffer *pkey_trash = get_trash_chunk(); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1255 | if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1256 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG, |
| 1257 | pkey_trash->data, |
| 1258 | pkey_trash->area); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) { |
| 1262 | value = ssl_sock_get_cert_sig(remote); |
| 1263 | if (value) { |
| 1264 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value); |
| 1265 | } |
| 1266 | } |
| 1267 | if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) { |
| 1268 | value = ssl_sock_get_cipher_name(remote); |
| 1269 | if (value) { |
| 1270 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value); |
| 1271 | } |
| 1272 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1273 | } |
| 1274 | tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8; |
| 1275 | tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff; |
| 1276 | ret += ssl_tlv_len; |
| 1277 | } |
| 1278 | #endif |
| 1279 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1280 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1281 | if (remote && (remote->proxy_netns)) { |
| 1282 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1283 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1284 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1285 | } |
| 1286 | #endif |
| 1287 | |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1288 | hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN)); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1289 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1290 | if (tlv_crc32c_p) { |
| 1291 | write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret))); |
| 1292 | } |
| 1293 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1294 | return ret; |
| 1295 | } |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1296 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1297 | /* returns 0 on success */ |
| 1298 | static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx, |
| 1299 | struct proxy *defpx, const char *file, int line, |
| 1300 | char **err) |
| 1301 | { |
| 1302 | if (too_many_args(0, args, err, NULL)) |
| 1303 | return -1; |
| 1304 | pp2_never_send_local = 1; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1308 | /* return the major HTTP version as 1 or 2 depending on how the request arrived |
| 1309 | * before being processed. |
| 1310 | */ |
| 1311 | static int |
| 1312 | smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1313 | { |
Jérôme Magnin | 8657742 | 2018-12-07 09:03:11 +0100 | [diff] [blame] | 1314 | struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) : |
| 1315 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1316 | |
| 1317 | smp->data.type = SMP_T_SINT; |
| 1318 | smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1; |
| 1319 | return 1; |
| 1320 | } |
| 1321 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1322 | /* fetch if the received connection used a PROXY protocol header */ |
| 1323 | int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1324 | { |
| 1325 | struct connection *conn; |
| 1326 | |
| 1327 | conn = objt_conn(smp->sess->origin); |
| 1328 | if (!conn) |
| 1329 | return 0; |
| 1330 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1331 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1332 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | smp->flags = 0; |
| 1337 | smp->data.type = SMP_T_BOOL; |
| 1338 | smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0; |
| 1339 | |
| 1340 | return 1; |
| 1341 | } |
| 1342 | |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1343 | /* fetch the authority TLV from a PROXY protocol header */ |
| 1344 | int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1345 | { |
| 1346 | struct connection *conn; |
| 1347 | |
| 1348 | conn = objt_conn(smp->sess->origin); |
| 1349 | if (!conn) |
| 1350 | return 0; |
| 1351 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1352 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1353 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | if (conn->proxy_authority == NULL) |
| 1358 | return 0; |
| 1359 | |
| 1360 | smp->flags = 0; |
| 1361 | smp->data.type = SMP_T_STR; |
| 1362 | smp->data.u.str.area = conn->proxy_authority; |
| 1363 | smp->data.u.str.data = conn->proxy_authority_len; |
| 1364 | |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1368 | /* fetch the unique ID TLV from a PROXY protocol header */ |
| 1369 | int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1370 | { |
| 1371 | struct connection *conn; |
| 1372 | |
| 1373 | conn = objt_conn(smp->sess->origin); |
| 1374 | if (!conn) |
| 1375 | return 0; |
| 1376 | |
| 1377 | if (conn->flags & CO_FL_WAIT_XPRT) { |
| 1378 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | if (!isttest(conn->proxy_unique_id)) |
| 1383 | return 0; |
| 1384 | |
| 1385 | smp->flags = 0; |
| 1386 | smp->data.type = SMP_T_STR; |
| 1387 | smp->data.u.str.area = conn->proxy_unique_id.ptr; |
| 1388 | smp->data.u.str.data = conn->proxy_unique_id.len; |
| 1389 | |
| 1390 | return 1; |
| 1391 | } |
| 1392 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1393 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1394 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1395 | * common denominator, the type that can be casted into all other ones. For |
| 1396 | * instance v4/v6 must be declared v4. |
| 1397 | */ |
| 1398 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1399 | { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI }, |
Jérôme Magnin | 8657742 | 2018-12-07 09:03:11 +0100 | [diff] [blame] | 1400 | { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV }, |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1401 | { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI }, |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1402 | { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI }, |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1403 | { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI }, |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1404 | { /* END */ }, |
| 1405 | }}; |
| 1406 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1407 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1408 | |
| 1409 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1410 | { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local }, |
| 1411 | { /* END */ }, |
| 1412 | }}; |
| 1413 | |
| 1414 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 1415 | |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 1416 | /* private function to handle sockaddr as input for connection hash */ |
| 1417 | static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss, |
| 1418 | char *buf, size_t *idx, |
| 1419 | enum conn_hash_params_t *hash_flags, |
| 1420 | enum conn_hash_params_t param_type_addr, |
| 1421 | enum conn_hash_params_t param_type_port) |
| 1422 | { |
| 1423 | struct sockaddr_in *addr; |
| 1424 | struct sockaddr_in6 *addr6; |
| 1425 | |
| 1426 | switch (ss->ss_family) { |
| 1427 | case AF_INET: |
| 1428 | addr = (struct sockaddr_in *)ss; |
| 1429 | |
| 1430 | conn_hash_update(buf, idx, |
| 1431 | &addr->sin_addr, sizeof(addr->sin_addr), |
| 1432 | hash_flags, param_type_addr); |
| 1433 | |
| 1434 | if (addr->sin_port) { |
| 1435 | conn_hash_update(buf, idx, |
| 1436 | &addr->sin_port, sizeof(addr->sin_port), |
| 1437 | hash_flags, param_type_port); |
| 1438 | } |
| 1439 | |
| 1440 | break; |
| 1441 | |
| 1442 | case AF_INET6: |
| 1443 | addr6 = (struct sockaddr_in6 *)ss; |
| 1444 | |
| 1445 | conn_hash_update(buf, idx, |
| 1446 | &addr6->sin6_addr, sizeof(addr6->sin6_addr), |
| 1447 | hash_flags, param_type_addr); |
| 1448 | |
| 1449 | if (addr6->sin6_port) { |
| 1450 | conn_hash_update(buf, idx, |
| 1451 | &addr6->sin6_port, sizeof(addr6->sin6_port), |
| 1452 | hash_flags, param_type_port); |
| 1453 | } |
| 1454 | |
| 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 1459 | XXH64_hash_t conn_calculate_hash(const struct conn_hash_params *params) |
| 1460 | { |
| 1461 | char *buf; |
| 1462 | size_t idx = 0; |
| 1463 | XXH64_hash_t hash = 0; |
| 1464 | enum conn_hash_params_t hash_flags = 0; |
| 1465 | |
| 1466 | buf = trash.area; |
| 1467 | |
Amaury Denoyelle | 8ede3db | 2021-03-02 14:38:53 +0100 | [diff] [blame] | 1468 | conn_hash_update(buf, &idx, ¶ms->target, sizeof(params->target), &hash_flags, 0); |
Amaury Denoyelle | 1a58aca | 2021-01-22 16:47:46 +0100 | [diff] [blame] | 1469 | |
Amaury Denoyelle | 9b626e3 | 2021-01-06 17:03:27 +0100 | [diff] [blame] | 1470 | if (params->sni_prehash) { |
| 1471 | conn_hash_update(buf, &idx, |
Amaury Denoyelle | 36441f4 | 2021-02-17 16:25:31 +0100 | [diff] [blame] | 1472 | ¶ms->sni_prehash, sizeof(params->sni_prehash), |
Amaury Denoyelle | 9b626e3 | 2021-01-06 17:03:27 +0100 | [diff] [blame] | 1473 | &hash_flags, CONN_HASH_PARAMS_TYPE_SNI); |
| 1474 | } |
| 1475 | |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 1476 | if (params->dst_addr) { |
| 1477 | conn_calculate_hash_sockaddr(params->dst_addr, |
| 1478 | buf, &idx, &hash_flags, |
| 1479 | CONN_HASH_PARAMS_TYPE_DST_ADDR, |
| 1480 | CONN_HASH_PARAMS_TYPE_DST_PORT); |
| 1481 | } |
| 1482 | |
Amaury Denoyelle | d10a200 | 2021-02-11 19:45:19 +0100 | [diff] [blame] | 1483 | if (params->src_addr) { |
| 1484 | conn_calculate_hash_sockaddr(params->src_addr, |
| 1485 | buf, &idx, &hash_flags, |
| 1486 | CONN_HASH_PARAMS_TYPE_SRC_ADDR, |
| 1487 | CONN_HASH_PARAMS_TYPE_SRC_PORT); |
| 1488 | } |
| 1489 | |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 1490 | if (params->proxy_prehash) { |
| 1491 | conn_hash_update(buf, &idx, |
Amaury Denoyelle | 36441f4 | 2021-02-17 16:25:31 +0100 | [diff] [blame] | 1492 | ¶ms->proxy_prehash, sizeof(params->proxy_prehash), |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 1493 | &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY); |
| 1494 | } |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 1495 | |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 1496 | hash = conn_hash_digest(buf, idx, hash_flags); |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 1497 | return hash; |
| 1498 | } |