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 | 930428c | 2021-10-06 18:27:28 +0200 | [diff] [blame] | 15 | #include <import/ebmbtree.h> |
| 16 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 18 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 19 | #include <haproxy/connection.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/fd.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 21 | #include <haproxy/frontend.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/hash.h> |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 23 | #include <haproxy/list.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 24 | #include <haproxy/log-t.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 25 | #include <haproxy/namespace.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 26 | #include <haproxy/net_helper.h> |
Willy Tarreau | fc77454 | 2020-06-04 17:31:04 +0200 | [diff] [blame] | 27 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 28 | #include <haproxy/sample.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 29 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 30 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 908908e | 2021-05-08 13:07:31 +0200 | [diff] [blame] | 31 | #include <haproxy/tools.h> |
Willy Tarreau | e5983ff | 2021-10-06 17:14:49 +0200 | [diff] [blame] | 32 | #include <haproxy/xxhash.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 33 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 34 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 35 | DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection)); |
| 36 | DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream)); |
| 37 | DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node)); |
| 38 | DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage)); |
| 39 | DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX); |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 40 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 41 | struct idle_conns idle_conns[MAX_THREADS] = { }; |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 42 | struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, }; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 43 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 44 | /* List head of all known muxes for PROTO */ |
| 45 | struct mux_proto_list mux_proto_list = { |
| 46 | .list = LIST_HEAD_INIT(mux_proto_list.list) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 49 | struct mux_stopping_data mux_stopping_data[MAX_THREADS]; |
| 50 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 51 | /* disables sending of proxy-protocol-v2's LOCAL command */ |
| 52 | static int pp2_never_send_local; |
| 53 | |
Willy Tarreau | 930428c | 2021-10-06 18:27:28 +0200 | [diff] [blame] | 54 | void conn_delete_from_tree(struct ebmb_node *node) |
| 55 | { |
| 56 | ebmb_delete(node); |
Willy Tarreau | 930428c | 2021-10-06 18:27:28 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 59 | int conn_create_mux(struct connection *conn) |
| 60 | { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 61 | if (conn_is_back(conn)) { |
| 62 | struct server *srv; |
| 63 | struct conn_stream *cs = conn->ctx; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 64 | struct session *sess = conn->owner; |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 65 | |
| 66 | if (conn->flags & CO_FL_ERROR) |
| 67 | goto fail; |
Olivier Houchard | a8a415d | 2020-01-23 13:15:14 +0100 | [diff] [blame] | 68 | |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 69 | if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) { |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 70 | if (conn_install_mux_chk(conn, conn->ctx, sess) < 0) |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 71 | goto fail; |
| 72 | } |
Amaury Denoyelle | ac03ef2 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 73 | else if (conn_install_mux_be(conn, conn->ctx, sess, NULL) < 0) |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 74 | goto fail; |
| 75 | srv = objt_server(conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 76 | |
| 77 | /* If we're doing http-reuse always, and the connection is not |
| 78 | * private with available streams (an http2 connection), add it |
| 79 | * to the available list, so that others can use it right |
| 80 | * away. If the connection is private, add it in the session |
| 81 | * server list. |
| 82 | */ |
Christopher Faulet | 2883fcf | 2020-07-01 14:59:43 +0200 | [diff] [blame] | 83 | 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] | 84 | !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0) |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 85 | 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] | 86 | else if (conn->flags & CO_FL_PRIVATE) { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 87 | /* 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] | 88 | session_add_conn(sess, conn, conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 89 | } |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | fail: |
| 92 | /* let the upper layer know the connection failed */ |
| 93 | cs->data_cb->wake(cs); |
| 94 | return -1; |
| 95 | } else |
| 96 | return conn_complete_session(conn); |
| 97 | |
Willy Tarreau | 930428c | 2021-10-06 18:27:28 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* This is used at the end of the socket IOCB to possibly create the mux if it |
| 101 | * was not done yet, or wake it up if flags changed compared to old_flags or if |
| 102 | * need_wake insists on this. It returns <0 if the connection was destroyed and |
| 103 | * must not be used, >=0 otherwise. |
| 104 | */ |
| 105 | int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | /* If we don't yet have a mux, that means we were waiting for |
| 110 | * information to create one, typically from the ALPN. If we're |
| 111 | * done with the handshake, attempt to create one. |
| 112 | */ |
| 113 | if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) { |
| 114 | ret = conn_create_mux(conn); |
| 115 | if (ret < 0) |
| 116 | goto done; |
| 117 | } |
| 118 | |
| 119 | /* The wake callback is normally used to notify the data layer about |
| 120 | * data layer activity (successful send/recv), connection establishment, |
| 121 | * shutdown and fatal errors. We need to consider the following |
| 122 | * situations to wake up the data layer : |
| 123 | * - change among the CO_FL_NOTIFY_DONE flags : |
| 124 | * SOCK_{RD,WR}_SH, ERROR, |
| 125 | * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the |
| 126 | * end of handshake and transition to CONNECTED |
| 127 | * - raise of CONNECTED with HANDSHAKE down |
| 128 | * - end of HANDSHAKE with CONNECTED set |
| 129 | * - regular data layer activity |
| 130 | * |
| 131 | * One tricky case is the wake up on read0 or error on an idle |
| 132 | * backend connection, that can happen on a connection that is still |
| 133 | * polled while at the same moment another thread is about to perform a |
| 134 | * takeover. The solution against this is to remove the connection from |
| 135 | * the idle list if it was in it, and possibly reinsert it at the end |
| 136 | * if the connection remains valid. The cost is non-null (locked tree |
| 137 | * removal) but remains low given that this is extremely rarely called. |
| 138 | * In any case it's guaranteed by the FD's thread_mask that we're |
| 139 | * called from the same thread the connection is queued in. |
| 140 | * |
| 141 | * Note that the wake callback is allowed to release the connection and |
| 142 | * the fd (and return < 0 in this case). |
| 143 | */ |
| 144 | if ((forced_wake || |
| 145 | ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) || |
| 146 | ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) && |
| 147 | conn->mux && conn->mux->wake) { |
| 148 | uint conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 149 | struct server *srv = objt_server(conn->target); |
| 150 | |
| 151 | if (conn_in_list) { |
| 152 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 153 | conn_delete_from_tree(&conn->hash_node->node); |
| 154 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 155 | } |
| 156 | |
| 157 | ret = conn->mux->wake(conn); |
| 158 | if (ret < 0) |
| 159 | goto done; |
| 160 | |
| 161 | if (conn_in_list) { |
| 162 | struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ? |
| 163 | &srv->per_thr[tid].safe_conns : |
| 164 | &srv->per_thr[tid].idle_conns; |
| 165 | |
| 166 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 167 | ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
| 168 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 169 | } |
| 170 | } |
| 171 | done: |
| 172 | return ret; |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 175 | /* Change the mux for the connection. |
| 176 | * The caller should make sure he's not subscribed to the underlying XPRT. |
| 177 | */ |
| 178 | int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf, |
| 179 | struct ist mux_proto, int mode) |
| 180 | { |
| 181 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
| 182 | const struct mux_ops *old_mux, *new_mux; |
| 183 | void *old_mux_ctx; |
| 184 | const char *alpn_str = NULL; |
| 185 | int alpn_len = 0; |
| 186 | |
| 187 | if (!mux_proto.len) { |
| 188 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 189 | mux_proto = ist2(alpn_str, alpn_len); |
| 190 | } |
| 191 | new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode); |
| 192 | old_mux = conn->mux; |
| 193 | |
| 194 | /* No mux found */ |
| 195 | if (!new_mux) |
| 196 | return -1; |
| 197 | |
| 198 | /* Same mux, nothing to do */ |
| 199 | if (old_mux == new_mux) |
| 200 | return 0; |
| 201 | |
| 202 | old_mux_ctx = conn->ctx; |
| 203 | conn->mux = new_mux; |
| 204 | conn->ctx = ctx; |
| 205 | if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) { |
| 206 | /* The mux upgrade failed, so restore the old mux */ |
| 207 | conn->ctx = old_mux_ctx; |
| 208 | conn->mux = old_mux; |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | /* The mux was upgraded, destroy the old one */ |
| 213 | *buf = BUF_NULL; |
| 214 | old_mux->destroy(old_mux_ctx); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* installs the best mux for incoming connection <conn> using the upper context |
| 219 | * <ctx>. If the mux protocol is forced, we use it to find the best |
| 220 | * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error. |
| 221 | */ |
| 222 | int conn_install_mux_fe(struct connection *conn, void *ctx) |
| 223 | { |
| 224 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
| 225 | const struct mux_ops *mux_ops; |
| 226 | |
| 227 | if (bind_conf->mux_proto) |
| 228 | mux_ops = bind_conf->mux_proto->mux; |
| 229 | else { |
| 230 | struct ist mux_proto; |
| 231 | const char *alpn_str = NULL; |
| 232 | int alpn_len = 0; |
| 233 | int mode; |
| 234 | |
| 235 | if (bind_conf->frontend->mode == PR_MODE_HTTP) |
| 236 | mode = PROTO_MODE_HTTP; |
| 237 | else |
| 238 | mode = PROTO_MODE_TCP; |
| 239 | |
| 240 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 241 | mux_proto = ist2(alpn_str, alpn_len); |
| 242 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode); |
| 243 | if (!mux_ops) |
| 244 | return -1; |
| 245 | } |
| 246 | return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner); |
| 247 | } |
| 248 | |
| 249 | /* installs the best mux for outgoing connection <conn> using the upper context |
Amaury Denoyelle | ac03ef2 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 250 | * <ctx>. If the server mux protocol is forced, we use it to find the best mux. |
| 251 | * It's also possible to specify an alternative mux protocol <force_mux_ops>, |
| 252 | * in which case it will be used instead of the default server mux protocol. |
| 253 | * |
| 254 | * Returns < 0 on error. |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 255 | */ |
Amaury Denoyelle | ac03ef2 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 256 | int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess, |
| 257 | const struct mux_ops *force_mux_ops) |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 258 | { |
| 259 | struct server *srv = objt_server(conn->target); |
| 260 | struct proxy *prx = objt_proxy(conn->target); |
| 261 | const struct mux_ops *mux_ops; |
| 262 | |
| 263 | if (srv) |
| 264 | prx = srv->proxy; |
| 265 | |
| 266 | if (!prx) // target must be either proxy or server |
| 267 | return -1; |
| 268 | |
Amaury Denoyelle | ac03ef2 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 269 | if (srv && srv->mux_proto && likely(!force_mux_ops)) { |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 270 | mux_ops = srv->mux_proto->mux; |
Amaury Denoyelle | ac03ef2 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 271 | } |
| 272 | else if (srv && unlikely(force_mux_ops)) { |
| 273 | mux_ops = force_mux_ops; |
| 274 | } |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 275 | else { |
| 276 | struct ist mux_proto; |
| 277 | const char *alpn_str = NULL; |
| 278 | int alpn_len = 0; |
| 279 | int mode; |
| 280 | |
| 281 | if (prx->mode == PR_MODE_HTTP) |
| 282 | mode = PROTO_MODE_HTTP; |
| 283 | else |
| 284 | mode = PROTO_MODE_TCP; |
| 285 | |
| 286 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 287 | mux_proto = ist2(alpn_str, alpn_len); |
| 288 | |
| 289 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode); |
| 290 | if (!mux_ops) |
| 291 | return -1; |
| 292 | } |
| 293 | return conn_install_mux(conn, mux_ops, ctx, prx, sess); |
| 294 | } |
| 295 | |
| 296 | /* installs the best mux for outgoing connection <conn> for a check using the |
| 297 | * upper context <ctx>. If the mux protocol is forced by the check, we use it to |
| 298 | * find the best mux. Returns < 0 on error. |
| 299 | */ |
| 300 | int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess) |
| 301 | { |
| 302 | struct check *check = objt_check(sess->origin); |
| 303 | struct server *srv = objt_server(conn->target); |
| 304 | struct proxy *prx = objt_proxy(conn->target); |
| 305 | const struct mux_ops *mux_ops; |
| 306 | |
| 307 | if (!check) // Check must be defined |
| 308 | return -1; |
| 309 | |
| 310 | if (srv) |
| 311 | prx = srv->proxy; |
| 312 | |
| 313 | if (!prx) // target must be either proxy or server |
| 314 | return -1; |
| 315 | |
| 316 | if (check->mux_proto) |
| 317 | mux_ops = check->mux_proto->mux; |
| 318 | else { |
| 319 | struct ist mux_proto; |
| 320 | const char *alpn_str = NULL; |
| 321 | int alpn_len = 0; |
| 322 | int mode; |
| 323 | |
| 324 | if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) |
| 325 | mode = PROTO_MODE_HTTP; |
| 326 | else |
| 327 | mode = PROTO_MODE_TCP; |
| 328 | |
| 329 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 330 | mux_proto = ist2(alpn_str, alpn_len); |
| 331 | |
| 332 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode); |
| 333 | if (!mux_ops) |
| 334 | return -1; |
| 335 | } |
| 336 | return conn_install_mux(conn, mux_ops, ctx, prx, sess); |
| 337 | } |
| 338 | |
Amaury Denoyelle | 2454bda | 2021-10-18 14:32:36 +0200 | [diff] [blame] | 339 | /* Set the ALPN of connection <conn> to <alpn>. If force is false, <alpn> must |
| 340 | * be a subset or identical to the registered protos for the parent SSL_CTX. |
| 341 | * In this case <alpn> must be a single protocol value, not a list. |
| 342 | * |
| 343 | * Returns 0 if ALPN is updated else -1. |
| 344 | */ |
| 345 | int conn_update_alpn(struct connection *conn, const struct ist alpn, int force) |
| 346 | { |
| 347 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 348 | size_t alpn_len = istlen(alpn); |
| 349 | char *ctx_alpn_str = NULL; |
| 350 | int ctx_alpn_len = 0, found = 0; |
| 351 | |
| 352 | /* if not force, first search if alpn is a subset or identical to the |
| 353 | * parent SSL_CTX. |
| 354 | */ |
| 355 | if (!force) { |
| 356 | /* retrieve the SSL_CTX according to the connection side. */ |
| 357 | if (conn_is_back(conn)) { |
| 358 | if (obj_type(conn->target) == OBJ_TYPE_SERVER) { |
| 359 | struct server *srv = __objt_server(conn->target); |
| 360 | ctx_alpn_str = srv->ssl_ctx.alpn_str; |
| 361 | ctx_alpn_len = srv->ssl_ctx.alpn_len; |
| 362 | } |
| 363 | } |
| 364 | else { |
| 365 | struct session *sess = conn->owner; |
| 366 | struct listener *li = sess->listener; |
| 367 | |
| 368 | if (li->bind_conf && li->bind_conf->is_ssl) { |
| 369 | ctx_alpn_str = li->bind_conf->ssl_conf.alpn_str; |
| 370 | ctx_alpn_len = li->bind_conf->ssl_conf.alpn_len; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (ctx_alpn_str) { |
| 375 | /* search if ALPN is present in SSL_CTX ALPN before |
| 376 | * using it. |
| 377 | */ |
| 378 | while (ctx_alpn_len) { |
| 379 | /* skip ALPN whose size is not 8 */ |
| 380 | if (*ctx_alpn_str != alpn_len - 1) { |
| 381 | ctx_alpn_len -= *ctx_alpn_str + 1; |
| 382 | } |
| 383 | else { |
| 384 | if (isteqi(ist2(ctx_alpn_str, alpn_len), alpn)) { |
| 385 | found = 1; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | ctx_alpn_str += *ctx_alpn_str + 1; |
| 390 | |
| 391 | /* This indicates an invalid ALPN formatted |
| 392 | * string and should never happen. */ |
| 393 | BUG_ON(ctx_alpn_len < 0); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (found || force) { |
| 399 | ssl_sock_set_alpn(conn, (const uchar *)istptr(alpn), istlen(alpn)); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | #endif |
| 404 | return -1; |
| 405 | } |
| 406 | |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 407 | /* Initializes all required fields for a new connection. Note that it does the |
| 408 | * minimum acceptable initialization for a connection that already exists and |
| 409 | * is about to be reused. It also leaves the addresses untouched, which makes |
| 410 | * it usable across connection retries to reset a connection to a known state. |
| 411 | */ |
| 412 | void conn_init(struct connection *conn, void *target) |
| 413 | { |
| 414 | conn->obj_type = OBJ_TYPE_CONN; |
| 415 | conn->flags = CO_FL_NONE; |
| 416 | conn->mux = NULL; |
| 417 | conn->ctx = NULL; |
| 418 | conn->owner = NULL; |
| 419 | conn->send_proxy_ofs = 0; |
| 420 | conn->handle.fd = DEAD_FD_MAGIC; |
| 421 | conn->err_code = CO_ER_NONE; |
| 422 | conn->target = target; |
| 423 | conn->destroy_cb = NULL; |
| 424 | conn->proxy_netns = NULL; |
| 425 | MT_LIST_INIT(&conn->toremove_list); |
| 426 | if (conn_is_back(conn)) |
| 427 | LIST_INIT(&conn->session_list); |
| 428 | else |
| 429 | LIST_INIT(&conn->stopping_list); |
| 430 | conn->subs = NULL; |
| 431 | conn->src = NULL; |
| 432 | conn->dst = NULL; |
| 433 | conn->proxy_authority = IST_NULL; |
| 434 | conn->proxy_unique_id = IST_NULL; |
| 435 | conn->qc = NULL; |
| 436 | conn->hash_node = NULL; |
| 437 | conn->xprt = NULL; |
| 438 | } |
| 439 | |
| 440 | /* Tries to allocate a new connection and initialized its main fields. The |
| 441 | * connection is returned on success, NULL on failure. The connection must |
| 442 | * be released using pool_free() or conn_free(). |
| 443 | */ |
| 444 | struct connection *conn_new(void *target) |
| 445 | { |
| 446 | struct connection *conn; |
| 447 | struct conn_hash_node *hash_node; |
| 448 | |
| 449 | conn = pool_alloc(pool_head_connection); |
| 450 | if (unlikely(!conn)) |
| 451 | return NULL; |
| 452 | |
| 453 | conn_init(conn, target); |
| 454 | |
| 455 | if (conn_is_back(conn)) { |
| 456 | if (obj_type(target) == OBJ_TYPE_SERVER) |
| 457 | srv_use_conn(__objt_server(target), conn); |
| 458 | |
| 459 | hash_node = conn_alloc_hash_node(conn); |
| 460 | if (unlikely(!hash_node)) { |
| 461 | pool_free(pool_head_connection, conn); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | conn->hash_node = hash_node; |
| 466 | } |
| 467 | |
| 468 | return conn; |
| 469 | } |
| 470 | |
| 471 | /* Releases a connection previously allocated by conn_new() */ |
| 472 | void conn_free(struct connection *conn) |
| 473 | { |
| 474 | /* If the connection is owned by the session, remove it from its list |
| 475 | */ |
| 476 | if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) { |
| 477 | session_unown_conn(conn->owner, conn); |
| 478 | } |
| 479 | else if (!(conn->flags & CO_FL_PRIVATE)) { |
| 480 | if (obj_type(conn->target) == OBJ_TYPE_SERVER) |
| 481 | srv_release_conn(__objt_server(conn->target), conn); |
| 482 | } |
| 483 | |
| 484 | /* Remove the conn from toremove_list. |
| 485 | * |
| 486 | * This is needed to prevent a double-free in case the connection was |
| 487 | * already scheduled from cleaning but is freed before via another |
| 488 | * call. |
| 489 | */ |
| 490 | MT_LIST_DELETE(&conn->toremove_list); |
| 491 | |
| 492 | sockaddr_free(&conn->src); |
| 493 | sockaddr_free(&conn->dst); |
| 494 | |
| 495 | pool_free(pool_head_authority, istptr(conn->proxy_authority)); |
| 496 | conn->proxy_authority = IST_NULL; |
| 497 | |
| 498 | pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id)); |
| 499 | conn->proxy_unique_id = IST_NULL; |
| 500 | |
| 501 | pool_free(pool_head_conn_hash_node, conn->hash_node); |
| 502 | conn->hash_node = NULL; |
| 503 | |
| 504 | /* By convention we always place a NULL where the ctx points to if the |
| 505 | * mux is null. It may have been used to store the connection as a |
| 506 | * stream_interface's end point for example. |
| 507 | */ |
| 508 | if (conn->ctx != NULL && conn->mux == NULL) |
| 509 | *(void **)conn->ctx = NULL; |
| 510 | |
| 511 | conn_force_unsubscribe(conn); |
| 512 | pool_free(pool_head_connection, conn); |
| 513 | } |
| 514 | |
Willy Tarreau | 8de90c7 | 2021-10-06 19:11:10 +0200 | [diff] [blame] | 515 | struct conn_hash_node *conn_alloc_hash_node(struct connection *conn) |
| 516 | { |
| 517 | struct conn_hash_node *hash_node = NULL; |
| 518 | |
| 519 | hash_node = pool_zalloc(pool_head_conn_hash_node); |
| 520 | if (unlikely(!hash_node)) |
| 521 | return NULL; |
| 522 | |
| 523 | hash_node->conn = conn; |
| 524 | |
| 525 | return hash_node; |
| 526 | } |
| 527 | |
| 528 | /* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and |
| 529 | * returns it. If <sap> is NULL, the address is always allocated and returned. |
| 530 | * if <sap> is non-null, an address will only be allocated if it points to a |
| 531 | * non-null pointer. In this case the allocated address will be assigned there. |
| 532 | * If <orig> is non-null and <len> positive, the address in <sa> will be copied |
| 533 | * into the allocated address. In both situations the new pointer is returned. |
| 534 | */ |
| 535 | struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len) |
| 536 | { |
| 537 | struct sockaddr_storage *sa; |
| 538 | |
| 539 | if (sap && *sap) |
| 540 | return *sap; |
| 541 | |
| 542 | sa = pool_alloc(pool_head_sockaddr); |
| 543 | if (sa && orig && len > 0) |
| 544 | memcpy(sa, orig, len); |
| 545 | if (sap) |
| 546 | *sap = sa; |
| 547 | return sa; |
| 548 | } |
| 549 | |
| 550 | /* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It |
| 551 | * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed |
| 552 | * there. |
| 553 | */ |
| 554 | void sockaddr_free(struct sockaddr_storage **sap) |
| 555 | { |
| 556 | if (!sap) |
| 557 | return; |
| 558 | pool_free(pool_head_sockaddr, *sap); |
| 559 | *sap = NULL; |
| 560 | } |
| 561 | |
| 562 | /* Releases a conn_stream previously allocated by cs_new(), as well as any |
| 563 | * buffer it would still hold. |
| 564 | */ |
| 565 | void cs_free(struct conn_stream *cs) |
| 566 | { |
| 567 | |
| 568 | pool_free(pool_head_connstream, cs); |
| 569 | } |
| 570 | |
| 571 | /* Tries to allocate a new conn_stream and initialize its main fields. If |
| 572 | * <conn> is NULL, then a new connection is allocated on the fly, initialized, |
| 573 | * and assigned to cs->conn ; this connection will then have to be released |
| 574 | * using pool_free() or conn_free(). The conn_stream is initialized and added |
| 575 | * to the mux's stream list on success, then returned. On failure, nothing is |
| 576 | * allocated and NULL is returned. |
| 577 | */ |
| 578 | struct conn_stream *cs_new(struct connection *conn, void *target) |
| 579 | { |
| 580 | struct conn_stream *cs; |
| 581 | |
| 582 | cs = pool_alloc(pool_head_connstream); |
| 583 | if (unlikely(!cs)) |
| 584 | return NULL; |
| 585 | |
| 586 | if (!conn) { |
| 587 | conn = conn_new(target); |
| 588 | if (unlikely(!conn)) { |
| 589 | cs_free(cs); |
| 590 | return NULL; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | cs_init(cs, conn); |
| 595 | return cs; |
| 596 | } |
| 597 | |
Willy Tarreau | aac777f | 2021-10-06 18:48:28 +0200 | [diff] [blame] | 598 | /* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is |
| 599 | * raw_sock, then just use the new XPRT as the connection XPRT, otherwise |
| 600 | * call the xprt's add_xprt() method. |
| 601 | * Returns 0 on success, or non-zero on failure. |
| 602 | */ |
| 603 | int xprt_add_hs(struct connection *conn) |
| 604 | { |
| 605 | void *xprt_ctx = NULL; |
| 606 | const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE); |
| 607 | void *nextxprt_ctx = NULL; |
| 608 | const struct xprt_ops *nextxprt_ops = NULL; |
| 609 | |
| 610 | if (conn->flags & CO_FL_ERROR) |
| 611 | return -1; |
| 612 | if (ops->init(conn, &xprt_ctx) < 0) |
| 613 | return -1; |
| 614 | if (conn->xprt == xprt_get(XPRT_RAW)) { |
| 615 | nextxprt_ctx = conn->xprt_ctx; |
| 616 | nextxprt_ops = conn->xprt; |
| 617 | conn->xprt_ctx = xprt_ctx; |
| 618 | conn->xprt = ops; |
| 619 | } else { |
| 620 | if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops, |
| 621 | &nextxprt_ctx, &nextxprt_ops) != 0) { |
| 622 | ops->close(conn, xprt_ctx); |
| 623 | return -1; |
| 624 | } |
| 625 | } |
| 626 | if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) { |
| 627 | ops->close(conn, xprt_ctx); |
| 628 | return -1; |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* returns a human-readable error code for conn->err_code, or NULL if the code |
| 634 | * is unknown. |
| 635 | */ |
| 636 | const char *conn_err_code_str(struct connection *c) |
| 637 | { |
| 638 | switch (c->err_code) { |
| 639 | case CO_ER_NONE: return "Success"; |
| 640 | |
| 641 | case CO_ER_CONF_FDLIM: return "Reached configured maxconn value"; |
| 642 | case CO_ER_PROC_FDLIM: return "Too many sockets on the process"; |
| 643 | case CO_ER_SYS_FDLIM: return "Too many sockets on the system"; |
| 644 | case CO_ER_SYS_MEMLIM: return "Out of system buffers"; |
| 645 | case CO_ER_NOPROTO: return "Protocol or address family not supported"; |
| 646 | case CO_ER_SOCK_ERR: return "General socket error"; |
| 647 | case CO_ER_PORT_RANGE: return "Source port range exhausted"; |
| 648 | case CO_ER_CANT_BIND: return "Can't bind to source address"; |
| 649 | case CO_ER_FREE_PORTS: return "Out of local source ports on the system"; |
| 650 | case CO_ER_ADDR_INUSE: return "Local source address already in use"; |
| 651 | |
| 652 | case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header"; |
| 653 | case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header"; |
| 654 | case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header"; |
| 655 | case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received"; |
| 656 | case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header"; |
| 657 | case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header"; |
| 658 | case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header"; |
| 659 | |
| 660 | case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header"; |
| 661 | case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header"; |
| 662 | case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header"; |
| 663 | case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received"; |
| 664 | case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number"; |
| 665 | case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header"; |
| 666 | |
| 667 | case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake"; |
| 668 | case CO_ER_SSL_ABORT: return "Connection error during SSL handshake"; |
| 669 | case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake"; |
| 670 | case CO_ER_SSL_TOO_MANY: return "Too many SSL connections"; |
| 671 | case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection"; |
| 672 | case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt"; |
| 673 | case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified"; |
| 674 | case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted"; |
| 675 | case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one"; |
| 676 | case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one"; |
| 677 | case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure"; |
| 678 | case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat"; |
| 679 | case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)"; |
| 680 | case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)"; |
| 681 | case CO_ER_SSL_EARLY_FAILED: return "Server refused early data"; |
| 682 | |
| 683 | case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake"; |
| 684 | case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake"; |
| 685 | case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request"; |
| 686 | case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server"; |
| 687 | |
| 688 | case CO_ERR_SSL_FATAL: return "SSL fatal error"; |
| 689 | } |
| 690 | return NULL; |
| 691 | } |
| 692 | |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 693 | /* Send a message over an established connection. It makes use of send() and |
| 694 | * returns the same return code and errno. If the socket layer is not ready yet |
| 695 | * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked |
| 696 | * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns |
| 697 | * EMSGSIZE if called with a zero length message. The purpose is to simplify |
| 698 | * some rare attempts to directly write on the socket from above the connection |
| 699 | * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send". |
| 700 | * It automatically retries on EINTR. Other errors cause the connection to be |
| 701 | * 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] | 702 | * first one which is the connection instead of the file descriptor. <flags> |
| 703 | * only support CO_SFL_MSG_MORE. |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 704 | */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 705 | 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] | 706 | { |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 707 | const struct buffer buffer = b_make((char*)buf, len, 0, len); |
| 708 | const struct xprt_ops *xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 709 | int ret; |
| 710 | |
| 711 | ret = -1; |
| 712 | errno = ENOTSOCK; |
| 713 | |
| 714 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 715 | goto fail; |
| 716 | |
| 717 | if (!conn_ctrl_ready(conn)) |
| 718 | goto fail; |
| 719 | |
| 720 | errno = EMSGSIZE; |
| 721 | if (!len) |
| 722 | goto fail; |
| 723 | |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 724 | /* snd_buf() already takes care of updating conn->flags and handling |
| 725 | * the FD polling status. |
| 726 | */ |
| 727 | ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags); |
| 728 | if (conn->flags & CO_FL_ERROR) |
| 729 | ret = -1; |
| 730 | return ret; |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 731 | fail: |
| 732 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR; |
| 733 | return ret; |
| 734 | } |
| 735 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 736 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 737 | * The event subscriber <es> is not allowed to change from a previous call as |
| 738 | * long as at least one event is still subscribed. The <event_type> must only |
| 739 | * 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] | 740 | */ |
| 741 | 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] | 742 | { |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 743 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 744 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 745 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 746 | es->events &= ~event_type; |
| 747 | if (!es->events) |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 748 | conn->subs = NULL; |
| 749 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 750 | if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events) |
| 751 | conn->ctrl->ignore_events(conn, event_type); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 752 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 756 | /* Called from the upper layer, to subscribe <es> to events <event_type>. |
| 757 | * 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] | 758 | * previous call to subscribe(). If the connection's ctrl layer is ready, |
| 759 | * the wait_event is immediately woken up and the subcription is cancelled. |
| 760 | * It always returns zero. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 761 | */ |
| 762 | 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] | 763 | { |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 764 | int ret = 0; |
| 765 | |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 766 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 767 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 768 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 769 | if (conn->subs && (conn->subs->events & event_type) == event_type) |
| 770 | return 0; |
| 771 | |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 772 | if (conn_ctrl_ready(conn) && conn->ctrl->check_events) { |
| 773 | ret = conn->ctrl->check_events(conn, event_type); |
| 774 | if (ret) |
| 775 | tasklet_wakeup(es->tasklet); |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 776 | } |
Willy Tarreau | 746b051 | 2020-12-11 17:06:11 +0100 | [diff] [blame] | 777 | |
| 778 | es->events = (es->events | event_type) & ~ret; |
| 779 | conn->subs = es->events ? es : NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 780 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 783 | /* Drains possibly pending incoming data on the connection and update the flags |
| 784 | * accordingly. This is used to know whether we need to disable lingering on |
| 785 | * close. Returns non-zero if it is safe to close without disabling lingering, |
| 786 | * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming |
| 787 | * shutdown was reported by the ->drain() function. |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 788 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 789 | int conn_ctrl_drain(struct connection *conn) |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 790 | { |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 791 | int ret = 0; |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 793 | if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) |
| 794 | ret = 1; |
| 795 | else if (conn->ctrl->drain) { |
| 796 | ret = conn->ctrl->drain(conn); |
| 797 | if (ret) |
| 798 | conn->flags |= CO_FL_SOCK_RD_SH; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 799 | } |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 800 | return ret; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 801 | } |
| 802 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 803 | /* |
| 804 | * Get data length from tlv |
| 805 | */ |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 806 | static inline size_t get_tlv_length(const struct tlv *src) |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 807 | { |
| 808 | return (src->length_hi << 8) | src->length_lo; |
| 809 | } |
| 810 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 811 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 812 | * raw data stream. The header looks like this : |
| 813 | * |
| 814 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 815 | * |
| 816 | * There must be exactly one space between each field. Fields are : |
| 817 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 818 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 819 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 820 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 821 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 822 | * |
| 823 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 824 | * |
| 825 | * The header line is small and in all cases smaller than the smallest normal |
| 826 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 827 | * can safely use MSG_PEEK and avoid buffering. |
| 828 | * |
| 829 | * Once the data is fetched, the values are set in the connection's address |
| 830 | * fields, and data are removed from the socket's buffer. The function returns |
| 831 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 832 | * and removed itself. |
| 833 | */ |
| 834 | int conn_recv_proxy(struct connection *conn, int flag) |
| 835 | { |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 836 | struct session *sess = conn->owner; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 837 | char *line, *end; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 838 | struct proxy_hdr_v2 *hdr_v2; |
| 839 | const char v2sig[] = PP2_SIGNATURE; |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 840 | size_t total_v2_len; |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 841 | size_t tlv_offset = 0; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 842 | int ret; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 843 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 844 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 845 | goto fail; |
| 846 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 847 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 848 | goto not_ready; |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 849 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 850 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 851 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 852 | if (ret < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 853 | if (errno == EINTR) |
| 854 | continue; |
| 855 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 856 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 857 | goto not_ready; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 858 | } |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 859 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 860 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 861 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 862 | break; |
| 863 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 864 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 865 | if (!trash.data) { |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 866 | /* client shutdown */ |
| 867 | conn->err_code = CO_ER_PRX_EMPTY; |
| 868 | goto fail; |
| 869 | } |
| 870 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 871 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 872 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 873 | if (trash.data < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 874 | goto missing; |
| 875 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 876 | line = trash.area; |
| 877 | end = trash.area + trash.data; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 878 | |
| 879 | /* Decode a possible proxy request, fail early if it does not match */ |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 880 | if (strncmp(line, "PROXY ", 6) != 0) |
| 881 | goto not_v1; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 882 | |
| 883 | line += 6; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 884 | if (trash.data < 9) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 885 | goto missing; |
| 886 | |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 887 | if (memcmp(line, "TCP4 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 888 | u32 src3, dst3, sport, dport; |
| 889 | |
| 890 | line += 5; |
| 891 | |
| 892 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 893 | if (line == end) |
| 894 | goto missing; |
| 895 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 896 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 897 | |
| 898 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 899 | if (line == end) |
| 900 | goto missing; |
| 901 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 902 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 903 | |
| 904 | sport = read_uint((const char **)&line, end); |
| 905 | if (line == end) |
| 906 | goto missing; |
| 907 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 908 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 909 | |
| 910 | dport = read_uint((const char **)&line, end); |
| 911 | if (line > end - 2) |
| 912 | goto missing; |
| 913 | if (*line++ != '\r') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 914 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 915 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 916 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 917 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 918 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 919 | goto fail; |
| 920 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 921 | /* update the session's addresses and mark them set */ |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 922 | ((struct sockaddr_in *)sess->src)->sin_family = AF_INET; |
| 923 | ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = htonl(src3); |
| 924 | ((struct sockaddr_in *)sess->src)->sin_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 925 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 926 | ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET; |
| 927 | ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = htonl(dst3); |
| 928 | ((struct sockaddr_in *)sess->dst)->sin_port = htons(dport); |
| 929 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 930 | } |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 931 | else if (memcmp(line, "TCP6 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 932 | u32 sport, dport; |
| 933 | char *src_s; |
| 934 | char *dst_s, *sport_s, *dport_s; |
| 935 | struct in6_addr src3, dst3; |
| 936 | |
| 937 | line += 5; |
| 938 | |
| 939 | src_s = line; |
| 940 | dst_s = sport_s = dport_s = NULL; |
| 941 | while (1) { |
| 942 | if (line > end - 2) { |
| 943 | goto missing; |
| 944 | } |
| 945 | else if (*line == '\r') { |
| 946 | *line = 0; |
| 947 | line++; |
| 948 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 949 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 950 | break; |
| 951 | } |
| 952 | |
| 953 | if (*line == ' ') { |
| 954 | *line = 0; |
| 955 | if (!dst_s) |
| 956 | dst_s = line + 1; |
| 957 | else if (!sport_s) |
| 958 | sport_s = line + 1; |
| 959 | else if (!dport_s) |
| 960 | dport_s = line + 1; |
| 961 | } |
| 962 | line++; |
| 963 | } |
| 964 | |
| 965 | if (!dst_s || !sport_s || !dport_s) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 966 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 967 | |
| 968 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 969 | if (*sport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 970 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 971 | |
| 972 | dport = read_uint((const char **)&dport_s,line - 2); |
| 973 | if (*dport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 974 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 975 | |
| 976 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 977 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 978 | |
| 979 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 980 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 981 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 982 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 983 | goto fail; |
| 984 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 985 | /* update the session's addresses and mark them set */ |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 986 | ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6; |
| 987 | memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 988 | ((struct sockaddr_in6 *)sess->src)->sin6_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 989 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 990 | ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6; |
| 991 | memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 992 | ((struct sockaddr_in6 *)sess->dst)->sin6_port = htons(dport); |
| 993 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 994 | } |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 995 | else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) { |
| 996 | /* This can be a UNIX socket forwarded by an haproxy upstream */ |
| 997 | line += 9; |
| 998 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 999 | else { |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 1000 | /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 1001 | conn->err_code = CO_ER_PRX_BAD_PROTO; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1002 | goto fail; |
| 1003 | } |
| 1004 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1005 | trash.data = line - trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1006 | goto eat_header; |
| 1007 | |
| 1008 | not_v1: |
| 1009 | /* try PPv2 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1010 | if (trash.data < PP2_HEADER_LEN) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1011 | goto missing; |
| 1012 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1013 | hdr_v2 = (struct proxy_hdr_v2 *) trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1014 | |
| 1015 | if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 || |
| 1016 | (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) { |
| 1017 | conn->err_code = CO_ER_PRX_NOT_HDR; |
| 1018 | goto fail; |
| 1019 | } |
| 1020 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1021 | total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len); |
| 1022 | if (trash.data < total_v2_len) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1023 | goto missing; |
| 1024 | |
| 1025 | switch (hdr_v2->ver_cmd & PP2_CMD_MASK) { |
| 1026 | case 0x01: /* PROXY command */ |
| 1027 | switch (hdr_v2->fam) { |
| 1028 | case 0x11: /* TCPv4 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 1029 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET) |
| 1030 | goto bad_header; |
| 1031 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 1032 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 1033 | goto fail; |
| 1034 | |
| 1035 | ((struct sockaddr_in *)sess->src)->sin_family = AF_INET; |
| 1036 | ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr; |
| 1037 | ((struct sockaddr_in *)sess->src)->sin_port = hdr_v2->addr.ip4.src_port; |
| 1038 | ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET; |
| 1039 | ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr; |
| 1040 | ((struct sockaddr_in *)sess->dst)->sin_port = hdr_v2->addr.ip4.dst_port; |
| 1041 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 1042 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1043 | break; |
| 1044 | case 0x21: /* TCPv6 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 1045 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6) |
| 1046 | goto bad_header; |
| 1047 | |
Christopher Faulet | c105c92 | 2021-10-25 08:17:11 +0200 | [diff] [blame] | 1048 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 1049 | goto fail; |
| 1050 | |
| 1051 | ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6; |
| 1052 | memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16); |
| 1053 | ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_v2->addr.ip6.src_port; |
| 1054 | ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6; |
| 1055 | memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16); |
| 1056 | ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_v2->addr.ip6.dst_port; |
| 1057 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 1058 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1059 | break; |
| 1060 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1061 | |
| 1062 | /* TLV parsing */ |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1063 | while (tlv_offset < total_v2_len) { |
| 1064 | struct tlv *tlv_packet; |
Tim Duesterhus | 56c176a | 2021-03-06 20:06:51 +0100 | [diff] [blame] | 1065 | struct ist tlv; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1066 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1067 | /* Verify that we have at least TLV_HEADER_SIZE bytes left */ |
| 1068 | if (tlv_offset + TLV_HEADER_SIZE > total_v2_len) |
| 1069 | goto bad_header; |
| 1070 | |
| 1071 | tlv_packet = (struct tlv *) &trash.area[tlv_offset]; |
Tim Duesterhus | 56c176a | 2021-03-06 20:06:51 +0100 | [diff] [blame] | 1072 | tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet)); |
| 1073 | tlv_offset += istlen(tlv) + TLV_HEADER_SIZE; |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1074 | |
| 1075 | /* Verify that the TLV length does not exceed the total PROXYv2 length */ |
| 1076 | if (tlv_offset > total_v2_len) |
| 1077 | goto bad_header; |
| 1078 | |
| 1079 | switch (tlv_packet->type) { |
| 1080 | case PP2_TYPE_CRC32C: { |
| 1081 | uint32_t n_crc32c; |
| 1082 | |
| 1083 | /* Verify that this TLV is exactly 4 bytes long */ |
Tim Duesterhus | 56c176a | 2021-03-06 20:06:51 +0100 | [diff] [blame] | 1084 | if (istlen(tlv) != 4) |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1085 | goto bad_header; |
| 1086 | |
Tim Duesterhus | 56c176a | 2021-03-06 20:06:51 +0100 | [diff] [blame] | 1087 | n_crc32c = read_n32(istptr(tlv)); |
| 1088 | write_n32(istptr(tlv), 0); // compute with CRC==0 |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1089 | |
| 1090 | if (hash_crc32c(trash.area, total_v2_len) != n_crc32c) |
| 1091 | goto bad_header; |
| 1092 | break; |
| 1093 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1094 | #ifdef USE_NS |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1095 | case PP2_TYPE_NETNS: { |
| 1096 | const struct netns_entry *ns; |
| 1097 | |
Tim Duesterhus | 56c176a | 2021-03-06 20:06:51 +0100 | [diff] [blame] | 1098 | ns = netns_store_lookup(istptr(tlv), istlen(tlv)); |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1099 | if (ns) |
| 1100 | conn->proxy_netns = ns; |
| 1101 | break; |
| 1102 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1103 | #endif |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1104 | case PP2_TYPE_AUTHORITY: { |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 1105 | if (istlen(tlv) > PP2_AUTHORITY_MAX) |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1106 | goto bad_header; |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 1107 | conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0); |
| 1108 | if (!isttest(conn->proxy_authority)) |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1109 | goto fail; |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 1110 | if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) { |
| 1111 | /* This is technically unreachable, because we verified above |
| 1112 | * that the TLV value fits. |
| 1113 | */ |
| 1114 | goto fail; |
| 1115 | } |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1116 | break; |
| 1117 | } |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1118 | case PP2_TYPE_UNIQUE_ID: { |
Tim Duesterhus | 002bd77 | 2021-03-06 20:06:49 +0100 | [diff] [blame] | 1119 | if (istlen(tlv) > UNIQUEID_LEN) |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1120 | goto bad_header; |
Tim Duesterhus | 2b7f6c2 | 2020-03-14 13:07:05 +0100 | [diff] [blame] | 1121 | conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0); |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1122 | if (!isttest(conn->proxy_unique_id)) |
| 1123 | goto fail; |
| 1124 | if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) { |
| 1125 | /* This is technically unreachable, because we verified above |
| 1126 | * that the TLV value fits. |
| 1127 | */ |
| 1128 | goto fail; |
| 1129 | } |
| 1130 | break; |
| 1131 | } |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1132 | default: |
| 1133 | break; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1137 | /* Verify that the PROXYv2 header ends at a TLV boundary. |
| 1138 | * This is technically unreachable, because the TLV parsing already |
| 1139 | * verifies that a TLV does not exceed the total length and also |
| 1140 | * that there is space for a TLV header. |
| 1141 | */ |
| 1142 | if (tlv_offset != total_v2_len) |
| 1143 | goto bad_header; |
| 1144 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1145 | /* unsupported protocol, keep local connection address */ |
| 1146 | break; |
| 1147 | case 0x00: /* LOCAL command */ |
| 1148 | /* keep local connection address for LOCAL */ |
| 1149 | break; |
| 1150 | default: |
| 1151 | goto bad_header; /* not a supported command */ |
| 1152 | } |
| 1153 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 1154 | trash.data = total_v2_len; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 1155 | goto eat_header; |
| 1156 | |
| 1157 | eat_header: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1158 | /* remove the PROXY line from the request. For this we re-read the |
| 1159 | * exact line at once. If we don't get the exact same result, we |
| 1160 | * fail. |
| 1161 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1162 | while (1) { |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1163 | ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
| 1164 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1165 | if (len2 < 0 && errno == EINTR) |
| 1166 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1167 | if (len2 != trash.data) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 1168 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1169 | break; |
| 1170 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1171 | |
| 1172 | conn->flags &= ~flag; |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1173 | conn->flags |= CO_FL_RCVD_PROXY; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1174 | return 1; |
| 1175 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1176 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1177 | return 0; |
| 1178 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1179 | missing: |
| 1180 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 1181 | * we have not read anything. Otherwise we need to fail because we won't |
| 1182 | * be able to poll anymore. |
| 1183 | */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 1184 | conn->err_code = CO_ER_PRX_TRUNCATED; |
| 1185 | goto fail; |
| 1186 | |
| 1187 | bad_header: |
| 1188 | /* This is not a valid proxy protocol header */ |
| 1189 | conn->err_code = CO_ER_PRX_BAD_HDR; |
| 1190 | goto fail; |
| 1191 | |
| 1192 | recv_abort: |
| 1193 | conn->err_code = CO_ER_PRX_ABORT; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 1194 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 1195 | goto fail; |
| 1196 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1197 | fail: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1198 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1202 | /* This handshake handler waits a NetScaler Client IP insertion header |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1203 | * at the beginning of the raw data stream. The header format is |
| 1204 | * described in doc/netscaler-client-ip-insertion-protocol.txt |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1205 | * |
| 1206 | * This line MUST be at the beginning of the buffer and MUST NOT be |
| 1207 | * fragmented. |
| 1208 | * |
| 1209 | * The header line is small and in all cases smaller than the smallest normal |
| 1210 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 1211 | * can safely use MSG_PEEK and avoid buffering. |
| 1212 | * |
| 1213 | * Once the data is fetched, the values are set in the connection's address |
| 1214 | * fields, and data are removed from the socket's buffer. The function returns |
| 1215 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 1216 | * and removed itself. |
| 1217 | */ |
| 1218 | int conn_recv_netscaler_cip(struct connection *conn, int flag) |
| 1219 | { |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1220 | struct session *sess = conn->owner; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1221 | char *line; |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 1222 | uint32_t hdr_len; |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 1223 | uint8_t ip_ver; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 1224 | int ret; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1225 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1226 | if (!conn_ctrl_ready(conn)) |
| 1227 | goto fail; |
| 1228 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1229 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1230 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1232 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 1233 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 1234 | if (ret < 0) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1235 | if (errno == EINTR) |
| 1236 | continue; |
| 1237 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1238 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1239 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1240 | } |
| 1241 | goto recv_abort; |
| 1242 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 1243 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1244 | break; |
| 1245 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1247 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 1248 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1249 | if (!trash.data) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1250 | /* client shutdown */ |
| 1251 | conn->err_code = CO_ER_CIP_EMPTY; |
| 1252 | goto fail; |
| 1253 | } |
| 1254 | |
| 1255 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1256 | * CIP magic, header length or |
| 1257 | * CIP magic, CIP length, CIP type, header length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1258 | if (trash.data < 12) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1259 | goto missing; |
| 1260 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1261 | line = trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1262 | |
| 1263 | /* Decode a possible NetScaler Client IP request, fail early if |
| 1264 | * it does not match */ |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 1265 | 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] | 1266 | goto bad_magic; |
| 1267 | |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1268 | /* Legacy CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1269 | if ((trash.area[8] & 0xD0) == 0x40) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 1270 | hdr_len = ntohl(read_u32((line+4))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1271 | line += 8; |
| 1272 | } |
| 1273 | /* Standard CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1274 | else if (trash.area[8] == 0x00) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 1275 | hdr_len = ntohs(read_u32((line+10))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1276 | line += 12; |
| 1277 | } |
| 1278 | /* Unknown CIP protocol */ |
| 1279 | else { |
| 1280 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 1281 | goto fail; |
| 1282 | } |
| 1283 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1284 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 1285 | * a minimal IP header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1286 | if (trash.data < 20) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1287 | goto missing; |
| 1288 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1289 | /* Get IP version from the first four bits */ |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 1290 | ip_ver = (*line & 0xf0) >> 4; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1291 | |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 1292 | if (ip_ver == 4) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1293 | struct ip *hdr_ip4; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 1294 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1295 | |
| 1296 | hdr_ip4 = (struct ip *)line; |
| 1297 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1298 | if (trash.data < 40 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1299 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 1300 | * IPv4 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1301 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 1302 | } |
| 1303 | else if (hdr_ip4->ip_p != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1304 | /* The protocol does not include a TCP header */ |
| 1305 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 1306 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 1307 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1308 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 1309 | hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1310 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1311 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 1312 | goto fail; |
| 1313 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1314 | /* update the session's addresses and mark them set */ |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1315 | ((struct sockaddr_in *)sess->src)->sin_family = AF_INET; |
| 1316 | ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr; |
| 1317 | ((struct sockaddr_in *)sess->src)->sin_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1318 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1319 | ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET; |
| 1320 | ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr; |
| 1321 | ((struct sockaddr_in *)sess->dst)->sin_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1322 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1323 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1324 | } |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 1325 | else if (ip_ver == 6) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1326 | struct ip6_hdr *hdr_ip6; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 1327 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1328 | |
| 1329 | hdr_ip6 = (struct ip6_hdr *)line; |
| 1330 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1331 | if (trash.data < 60 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1332 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 1333 | * IPv6 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1334 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 1335 | } |
| 1336 | else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1337 | /* The protocol does not include a TCP header */ |
| 1338 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 1339 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 1340 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1341 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 1342 | hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1343 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1344 | if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0)) |
| 1345 | goto fail; |
| 1346 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1347 | /* update the session's addresses and mark them set */ |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1348 | ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6; |
| 1349 | ((struct sockaddr_in6 *)sess->src)->sin6_addr = hdr_ip6->ip6_src; |
| 1350 | ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1351 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1352 | ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6; |
| 1353 | ((struct sockaddr_in6 *)sess->dst)->sin6_addr = hdr_ip6->ip6_dst; |
| 1354 | ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1355 | |
Christopher Faulet | e83e882 | 2021-10-25 08:23:22 +0200 | [diff] [blame] | 1356 | sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1357 | } |
| 1358 | else { |
| 1359 | /* The protocol does not match something known (IPv4/IPv6) */ |
| 1360 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 1361 | goto fail; |
| 1362 | } |
| 1363 | |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 1364 | line += hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1365 | trash.data = line - trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1366 | |
| 1367 | /* remove the NetScaler Client IP header from the request. For this |
| 1368 | * we re-read the exact line at once. If we don't get the exact same |
| 1369 | * result, we fail. |
| 1370 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1371 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1372 | int len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1373 | if (len2 < 0 && errno == EINTR) |
| 1374 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1375 | if (len2 != trash.data) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1376 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1377 | break; |
| 1378 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1379 | |
| 1380 | conn->flags &= ~flag; |
| 1381 | return 1; |
| 1382 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1383 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1384 | return 0; |
| 1385 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1386 | missing: |
| 1387 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 1388 | * we have not read anything. Otherwise we need to fail because we won't |
| 1389 | * be able to poll anymore. |
| 1390 | */ |
| 1391 | conn->err_code = CO_ER_CIP_TRUNCATED; |
| 1392 | goto fail; |
| 1393 | |
| 1394 | bad_magic: |
| 1395 | conn->err_code = CO_ER_CIP_BAD_MAGIC; |
| 1396 | goto fail; |
| 1397 | |
| 1398 | recv_abort: |
| 1399 | conn->err_code = CO_ER_CIP_ABORT; |
| 1400 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 1401 | goto fail; |
| 1402 | |
| 1403 | fail: |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1404 | conn->flags |= CO_FL_ERROR; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1408 | |
| 1409 | int conn_send_socks4_proxy_request(struct connection *conn) |
| 1410 | { |
| 1411 | struct socks4_request req_line; |
| 1412 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1413 | if (!conn_ctrl_ready(conn)) |
| 1414 | goto out_error; |
| 1415 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1416 | if (!conn_get_dst(conn)) |
| 1417 | goto out_error; |
| 1418 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1419 | req_line.version = 0x04; |
| 1420 | req_line.command = 0x01; |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1421 | req_line.port = get_net_port(conn->dst); |
| 1422 | req_line.ip = is_inet_addr(conn->dst); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1423 | memcpy(req_line.user_id, "HAProxy\0", 8); |
| 1424 | |
| 1425 | if (conn->send_proxy_ofs > 0) { |
| 1426 | /* |
| 1427 | * This is the first call to send the request |
| 1428 | */ |
| 1429 | conn->send_proxy_ofs = -(int)sizeof(req_line); |
| 1430 | } |
| 1431 | |
| 1432 | if (conn->send_proxy_ofs < 0) { |
| 1433 | int ret = 0; |
| 1434 | |
| 1435 | /* we are sending the socks4_req_line here. If the data layer |
| 1436 | * has a pending write, we'll also set MSG_MORE. |
| 1437 | */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 1438 | ret = conn_ctrl_send( |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1439 | conn, |
| 1440 | ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs), |
| 1441 | -conn->send_proxy_ofs, |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 1442 | (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] | 1443 | |
| 1444 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n", |
| 1445 | conn->handle.fd, -conn->send_proxy_ofs, ret); |
| 1446 | |
| 1447 | if (ret < 0) { |
| 1448 | goto out_error; |
| 1449 | } |
| 1450 | |
| 1451 | conn->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 1452 | if (conn->send_proxy_ofs != 0) { |
| 1453 | goto out_wait; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* OK we've the whole request sent */ |
| 1458 | conn->flags &= ~CO_FL_SOCKS4_SEND; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1459 | |
| 1460 | /* The connection is ready now, simply return and let the connection |
| 1461 | * handler notify upper layers if needed. |
| 1462 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1463 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1464 | |
| 1465 | if (conn->flags & CO_FL_SEND_PROXY) { |
| 1466 | /* |
| 1467 | * Get the send_proxy_ofs ready for the send_proxy due to we are |
| 1468 | * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done |
| 1469 | * before sending PROXY Protocol. |
| 1470 | */ |
| 1471 | conn->send_proxy_ofs = 1; |
| 1472 | } |
| 1473 | return 1; |
| 1474 | |
| 1475 | out_error: |
| 1476 | /* Write error on the file descriptor */ |
| 1477 | conn->flags |= CO_FL_ERROR; |
| 1478 | if (conn->err_code == CO_ER_NONE) { |
| 1479 | conn->err_code = CO_ER_SOCKS4_SEND; |
| 1480 | } |
| 1481 | return 0; |
| 1482 | |
| 1483 | out_wait: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | int conn_recv_socks4_proxy_response(struct connection *conn) |
| 1488 | { |
| 1489 | char line[SOCKS4_HS_RSP_LEN]; |
| 1490 | int ret; |
| 1491 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1492 | if (!conn_ctrl_ready(conn)) |
| 1493 | goto fail; |
| 1494 | |
| 1495 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1496 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1497 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1498 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1499 | /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00 |
| 1500 | * Try to peek into it, before all 8 bytes ready. |
| 1501 | */ |
| 1502 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK); |
| 1503 | |
| 1504 | if (ret == 0) { |
| 1505 | /* the socket has been closed or shutdown for send */ |
| 1506 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n", |
| 1507 | conn->handle.fd, ret, errno); |
| 1508 | if (conn->err_code == CO_ER_NONE) { |
| 1509 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 1510 | } |
| 1511 | goto fail; |
| 1512 | } |
| 1513 | |
| 1514 | if (ret > 0) { |
| 1515 | if (ret == SOCKS4_HS_RSP_LEN) { |
| 1516 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 1517 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 1518 | }else{ |
| 1519 | 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]); |
| 1520 | } |
| 1521 | } else { |
| 1522 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno); |
| 1523 | } |
| 1524 | |
| 1525 | if (ret < 0) { |
| 1526 | if (errno == EINTR) { |
| 1527 | continue; |
| 1528 | } |
| 1529 | if (errno == EAGAIN) { |
| 1530 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1531 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1532 | } |
| 1533 | goto recv_abort; |
| 1534 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1535 | break; |
| 1536 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1537 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1538 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 1539 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1540 | if (ret < SOCKS4_HS_RSP_LEN) { |
| 1541 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 1542 | * we are not able to read enough data. |
| 1543 | */ |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1544 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Base on the SOCSK4 protocol: |
| 1549 | * |
| 1550 | * +----+----+----+----+----+----+----+----+ |
| 1551 | * | VN | CD | DSTPORT | DSTIP | |
| 1552 | * +----+----+----+----+----+----+----+----+ |
| 1553 | * # of bytes: 1 1 2 4 |
| 1554 | * VN is the version of the reply code and should be 0. CD is the result |
| 1555 | * code with one of the following values: |
| 1556 | * 90: request granted |
| 1557 | * 91: request rejected or failed |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1558 | * 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] | 1559 | * 93: request rejected because the client program and identd report different user-ids |
| 1560 | * The remaining fields are ignored. |
| 1561 | */ |
| 1562 | if (line[1] != 90) { |
| 1563 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 1564 | |
| 1565 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 1566 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 1567 | if (conn->err_code == CO_ER_NONE) { |
| 1568 | conn->err_code = CO_ER_SOCKS4_DENY; |
| 1569 | } |
| 1570 | goto fail; |
| 1571 | } |
| 1572 | |
| 1573 | /* remove the 8 bytes response from the stream */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1574 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1575 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0); |
| 1576 | if (ret < 0 && errno == EINTR) { |
| 1577 | continue; |
| 1578 | } |
| 1579 | if (ret != SOCKS4_HS_RSP_LEN) { |
| 1580 | if (conn->err_code == CO_ER_NONE) { |
| 1581 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 1582 | } |
| 1583 | goto fail; |
| 1584 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1585 | break; |
| 1586 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1587 | |
| 1588 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 1589 | return 1; |
| 1590 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1591 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1592 | return 0; |
| 1593 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1594 | recv_abort: |
| 1595 | if (conn->err_code == CO_ER_NONE) { |
| 1596 | conn->err_code = CO_ER_SOCKS4_ABORT; |
| 1597 | } |
| 1598 | conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH); |
| 1599 | goto fail; |
| 1600 | |
| 1601 | fail: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1602 | conn->flags |= CO_FL_ERROR; |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Willy Tarreau | e59b516 | 2021-05-08 14:06:09 +0200 | [diff] [blame] | 1606 | /* Lists the known proto mux on <out> */ |
| 1607 | void list_mux_proto(FILE *out) |
| 1608 | { |
| 1609 | struct mux_proto_list *item; |
| 1610 | struct buffer *chk = get_trash_chunk(); |
| 1611 | struct ist proto; |
| 1612 | char *mode, *side; |
| 1613 | |
| 1614 | fprintf(out, "Available multiplexer protocols :\n" |
| 1615 | "(protocols marked as <default> cannot be specified using 'proto' keyword)\n"); |
| 1616 | list_for_each_entry(item, &mux_proto_list.list, list) { |
| 1617 | proto = item->token; |
| 1618 | |
| 1619 | if (item->mode == PROTO_MODE_ANY) |
| 1620 | mode = "TCP|HTTP"; |
| 1621 | else if (item->mode == PROTO_MODE_TCP) |
| 1622 | mode = "TCP"; |
| 1623 | else if (item->mode == PROTO_MODE_HTTP) |
| 1624 | mode = "HTTP"; |
| 1625 | else |
| 1626 | mode = "NONE"; |
| 1627 | |
| 1628 | if (item->side == PROTO_SIDE_BOTH) |
| 1629 | side = "FE|BE"; |
| 1630 | else if (item->side == PROTO_SIDE_FE) |
| 1631 | side = "FE"; |
| 1632 | else if (item->side == PROTO_SIDE_BE) |
| 1633 | side = "BE"; |
| 1634 | else |
| 1635 | side = "NONE"; |
| 1636 | |
| 1637 | chunk_reset(chk); |
| 1638 | if (item->mux->flags & MX_FL_HTX) |
| 1639 | chunk_strcpy(chk, "HTX"); |
| 1640 | if (item->mux->flags & MX_FL_CLEAN_ABRT) |
| 1641 | chunk_appendf(chk, "%sCLEAN_ABRT", (b_data(chk) ? "|": "")); |
| 1642 | if (item->mux->flags & MX_FL_HOL_RISK) |
| 1643 | chunk_appendf(chk, "%sHOL_RISK", (b_data(chk) ? "|": "")); |
| 1644 | if (item->mux->flags & MX_FL_NO_UPG) |
| 1645 | chunk_appendf(chk, "%sNO_UPG", (b_data(chk) ? "|": "")); |
| 1646 | |
| 1647 | fprintf(out, " %15s : mode=%-10s side=%-8s mux=%-8s flags=%.*s\n", |
| 1648 | (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name, |
| 1649 | (int)b_data(chk), b_orig(chk)); |
| 1650 | } |
| 1651 | } |
| 1652 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1653 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 1654 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 1655 | * It returns the number of bytes composing this line (including the trailing |
| 1656 | * 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] | 1657 | * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is |
| 1658 | * emitted as well. |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1659 | */ |
Christopher Faulet | b097aef | 2021-10-25 08:05:28 +0200 | [diff] [blame] | 1660 | static int make_proxy_line_v1(char *buf, int buf_len, const struct sockaddr_storage *src, const struct sockaddr_storage *dst) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1661 | { |
| 1662 | int ret = 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1663 | char * protocol; |
| 1664 | char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1665 | char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1666 | in_port_t src_port; |
| 1667 | in_port_t dst_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1668 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1669 | if ( !src |
| 1670 | || !dst |
| 1671 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1672 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
| 1673 | /* unknown family combination */ |
| 1674 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1675 | if (ret >= buf_len) |
| 1676 | return 0; |
| 1677 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1678 | return ret; |
| 1679 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1680 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1681 | /* IPv4 for both src and dst */ |
| 1682 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1683 | protocol = "TCP4"; |
| 1684 | 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] | 1685 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1686 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1687 | 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] | 1688 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1689 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1690 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1691 | /* IPv6 for at least one of src and dst */ |
| 1692 | else { |
| 1693 | struct in6_addr tmp; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1694 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1695 | protocol = "TCP6"; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1696 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1697 | if (src->ss_family == AF_INET) { |
| 1698 | /* Convert src to IPv6 */ |
| 1699 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1700 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1701 | } |
| 1702 | else { |
| 1703 | tmp = ((struct sockaddr_in6 *)src)->sin6_addr; |
| 1704 | src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1705 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1706 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1707 | if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1708 | return 0; |
| 1709 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1710 | if (dst->ss_family == AF_INET) { |
| 1711 | /* Convert dst to IPv6 */ |
| 1712 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1713 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1714 | } |
| 1715 | else { |
| 1716 | tmp = ((struct sockaddr_in6 *)dst)->sin6_addr; |
| 1717 | dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1718 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1719 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1720 | if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1723 | |
| 1724 | ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port)); |
| 1725 | if (ret >= buf_len) |
| 1726 | return 0; |
| 1727 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1728 | return ret; |
| 1729 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1730 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1731 | 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] | 1732 | { |
| 1733 | struct tlv *tlv; |
| 1734 | |
| 1735 | if (!dest || (length + sizeof(*tlv) > dest_len)) |
| 1736 | return 0; |
| 1737 | |
| 1738 | tlv = (struct tlv *)dest; |
| 1739 | |
| 1740 | tlv->type = type; |
| 1741 | tlv->length_hi = length >> 8; |
| 1742 | tlv->length_lo = length & 0x00ff; |
| 1743 | memcpy(tlv->value, value, length); |
| 1744 | return length + sizeof(*tlv); |
| 1745 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1746 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1747 | /* Note: <remote> is explicitly allowed to be NULL */ |
Christopher Faulet | 4bfce39 | 2021-10-22 14:33:59 +0200 | [diff] [blame] | 1748 | static 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] | 1749 | { |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1750 | const char pp2_signature[] = PP2_SIGNATURE; |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1751 | void *tlv_crc32c_p = NULL; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1752 | int ret = 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1753 | struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1754 | struct sockaddr_storage null_addr = { .ss_family = 0 }; |
Christopher Faulet | b097aef | 2021-10-25 08:05:28 +0200 | [diff] [blame] | 1755 | const struct sockaddr_storage *src = &null_addr; |
| 1756 | const struct sockaddr_storage *dst = &null_addr; |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1757 | const char *value; |
| 1758 | int value_len; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1759 | |
| 1760 | if (buf_len < PP2_HEADER_LEN) |
| 1761 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1762 | memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1763 | |
Christopher Faulet | b097aef | 2021-10-25 08:05:28 +0200 | [diff] [blame] | 1764 | if (strm) { |
| 1765 | src = si_src(&strm->si[0]); |
| 1766 | dst = si_dst(&strm->si[0]); |
| 1767 | } |
| 1768 | else if (remote && conn_get_src(remote) && conn_get_dst(remote)) { |
| 1769 | src = conn_src(remote); |
| 1770 | dst = conn_dst(remote); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1771 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1772 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1773 | /* At least one of src or dst is not of AF_INET or AF_INET6 */ |
| 1774 | if ( !src |
| 1775 | || !dst |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1776 | || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1777 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1778 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1779 | if (buf_len < PP2_HDR_LEN_UNSPEC) |
| 1780 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1781 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL; |
| 1782 | hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1783 | ret = PP2_HDR_LEN_UNSPEC; |
| 1784 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1785 | else { |
Willy Tarreau | 02c8803 | 2020-04-14 12:54:10 +0200 | [diff] [blame] | 1786 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1787 | /* IPv4 for both src and dst */ |
| 1788 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1789 | if (buf_len < PP2_HDR_LEN_INET) |
| 1790 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1791 | hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM; |
| 1792 | hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr; |
| 1793 | hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1794 | hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
| 1795 | hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1796 | ret = PP2_HDR_LEN_INET; |
| 1797 | } |
| 1798 | /* IPv6 for at least one of src and dst */ |
| 1799 | else { |
| 1800 | struct in6_addr tmp; |
| 1801 | |
| 1802 | if (buf_len < PP2_HDR_LEN_INET6) |
| 1803 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1804 | hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM; |
| 1805 | if (src->ss_family == AF_INET) { |
| 1806 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1807 | memcpy(hdr->addr.ip6.src_addr, &tmp, 16); |
| 1808 | hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1809 | } |
| 1810 | else { |
| 1811 | memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16); |
| 1812 | hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1813 | } |
| 1814 | if (dst->ss_family == AF_INET) { |
| 1815 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1816 | memcpy(hdr->addr.ip6.dst_addr, &tmp, 16); |
William Dauchy | bd8bf67 | 2020-01-26 19:06:39 +0100 | [diff] [blame] | 1817 | hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1818 | } |
| 1819 | else { |
| 1820 | memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16); |
| 1821 | hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1822 | } |
| 1823 | |
| 1824 | ret = PP2_HDR_LEN_INET6; |
| 1825 | } |
| 1826 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1827 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1828 | if (srv->pp_opts & SRV_PP_V2_CRC32C) { |
| 1829 | uint32_t zero_crc32c = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1830 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1831 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1832 | return 0; |
| 1833 | tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value; |
| 1834 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c); |
| 1835 | } |
| 1836 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1837 | if (remote && conn_get_alpn(remote, &value, &value_len)) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1838 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1839 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1840 | 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] | 1841 | } |
| 1842 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1843 | if (srv->pp_opts & SRV_PP_V2_AUTHORITY) { |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1844 | value = NULL; |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 1845 | if (remote && isttest(remote->proxy_authority)) { |
| 1846 | value = istptr(remote->proxy_authority); |
| 1847 | value_len = istlen(remote->proxy_authority); |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1848 | } |
| 1849 | #ifdef USE_OPENSSL |
| 1850 | else { |
Jerome Magnin | 78891c7 | 2019-09-02 09:53:41 +0200 | [diff] [blame] | 1851 | if ((value = ssl_sock_get_sni(remote))) |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1852 | value_len = strlen(value); |
| 1853 | } |
| 1854 | #endif |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1855 | if (value) { |
| 1856 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1857 | return 0; |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1858 | 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] | 1859 | } |
| 1860 | } |
| 1861 | |
Christopher Faulet | 3ab504f | 2020-05-26 15:16:01 +0200 | [diff] [blame] | 1862 | if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1863 | struct session* sess = strm_sess(strm); |
| 1864 | struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id); |
| 1865 | |
| 1866 | value = unique_id.ptr; |
| 1867 | value_len = unique_id.len; |
| 1868 | |
| 1869 | if (value_len >= 0) { |
| 1870 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1871 | return 0; |
| 1872 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value); |
| 1873 | } |
| 1874 | } |
| 1875 | |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1876 | #ifdef USE_OPENSSL |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1877 | if (srv->pp_opts & SRV_PP_V2_SSL) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1878 | struct tlv_ssl *tlv; |
| 1879 | int ssl_tlv_len = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1880 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1881 | if ((buf_len - ret) < sizeof(struct tlv_ssl)) |
| 1882 | return 0; |
| 1883 | tlv = (struct tlv_ssl *)&buf[ret]; |
| 1884 | memset(tlv, 0, sizeof(struct tlv_ssl)); |
| 1885 | ssl_tlv_len += sizeof(struct tlv_ssl); |
| 1886 | tlv->tlv.type = PP2_TYPE_SSL; |
Willy Tarreau | 1057bee | 2021-10-06 11:38:44 +0200 | [diff] [blame] | 1887 | if (conn_is_ssl(remote)) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1888 | tlv->client |= PP2_CLIENT_SSL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 1889 | value = ssl_sock_get_proto_version(remote); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1890 | if (value) { |
Emmanuel Hocdet | 8c0c34b | 2018-02-28 12:02:14 +0100 | [diff] [blame] | 1891 | 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] | 1892 | } |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1893 | if (ssl_sock_get_cert_used_sess(remote)) { |
| 1894 | tlv->client |= PP2_CLIENT_CERT_SESS; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1895 | tlv->verify = htonl(ssl_sock_get_verify_result(remote)); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1896 | if (ssl_sock_get_cert_used_conn(remote)) |
| 1897 | tlv->client |= PP2_CLIENT_CERT_CONN; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1898 | } |
| 1899 | if (srv->pp_opts & SRV_PP_V2_SSL_CN) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1900 | struct buffer *cn_trash = get_trash_chunk(); |
Willy Tarreau | 3b9a0c9 | 2014-07-19 06:37:33 +0200 | [diff] [blame] | 1901 | if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1902 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN, |
| 1903 | cn_trash->data, |
| 1904 | cn_trash->area); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1905 | } |
| 1906 | } |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1907 | if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1908 | struct buffer *pkey_trash = get_trash_chunk(); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1909 | if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1910 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG, |
| 1911 | pkey_trash->data, |
| 1912 | pkey_trash->area); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) { |
| 1916 | value = ssl_sock_get_cert_sig(remote); |
| 1917 | if (value) { |
| 1918 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value); |
| 1919 | } |
| 1920 | } |
| 1921 | if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) { |
| 1922 | value = ssl_sock_get_cipher_name(remote); |
| 1923 | if (value) { |
| 1924 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value); |
| 1925 | } |
| 1926 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1927 | } |
| 1928 | tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8; |
| 1929 | tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff; |
| 1930 | ret += ssl_tlv_len; |
| 1931 | } |
| 1932 | #endif |
| 1933 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1934 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1935 | if (remote && (remote->proxy_netns)) { |
| 1936 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1937 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1938 | 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] | 1939 | } |
| 1940 | #endif |
| 1941 | |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1942 | hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN)); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1943 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1944 | if (tlv_crc32c_p) { |
| 1945 | write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret))); |
| 1946 | } |
| 1947 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1948 | return ret; |
| 1949 | } |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1950 | |
Christopher Faulet | 4bfce39 | 2021-10-22 14:33:59 +0200 | [diff] [blame] | 1951 | /* Note: <remote> is explicitly allowed to be NULL */ |
| 1952 | int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm) |
| 1953 | { |
| 1954 | int ret = 0; |
| 1955 | |
| 1956 | if (srv && (srv->pp_opts & SRV_PP_V2)) { |
| 1957 | ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm); |
| 1958 | } |
| 1959 | else { |
Christopher Faulet | b097aef | 2021-10-25 08:05:28 +0200 | [diff] [blame] | 1960 | const struct sockaddr_storage *src = NULL; |
| 1961 | const struct sockaddr_storage *dst = NULL; |
| 1962 | |
| 1963 | if (strm) { |
| 1964 | src = si_src(&strm->si[0]); |
| 1965 | dst = si_dst(&strm->si[0]); |
| 1966 | } |
| 1967 | else if (remote && conn_get_src(remote) && conn_get_dst(remote)) { |
| 1968 | src = conn_src(remote); |
| 1969 | dst = conn_dst(remote); |
| 1970 | } |
| 1971 | |
| 1972 | if (src && dst) |
| 1973 | ret = make_proxy_line_v1(buf, buf_len, src, dst); |
Christopher Faulet | 4bfce39 | 2021-10-22 14:33:59 +0200 | [diff] [blame] | 1974 | else |
| 1975 | ret = make_proxy_line_v1(buf, buf_len, NULL, NULL); |
| 1976 | } |
| 1977 | |
| 1978 | return ret; |
| 1979 | } |
| 1980 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1981 | /* returns 0 on success */ |
| 1982 | static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1983 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1984 | char **err) |
| 1985 | { |
| 1986 | if (too_many_args(0, args, err, NULL)) |
| 1987 | return -1; |
| 1988 | pp2_never_send_local = 1; |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Willy Tarreau | d943a04 | 2021-06-16 17:35:20 +0200 | [diff] [blame] | 1992 | /* extracts some info from the connection and appends them to buffer <buf>. The |
| 1993 | * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source |
| 1994 | * when set, destination when set, are printed in a compact human-readable format |
| 1995 | * fitting on a single line. This is handy to complete traces or debug output. |
| 1996 | * It is permitted to pass a NULL conn pointer. The number of characters emitted |
| 1997 | * is returned. A prefix <pfx> might be prepended before the first field if not |
| 1998 | * NULL. |
| 1999 | */ |
| 2000 | int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx) |
| 2001 | { |
| 2002 | const struct listener *li; |
| 2003 | const struct server *sv; |
| 2004 | const struct proxy *px; |
| 2005 | char addr[40]; |
| 2006 | int old_len = buf->data; |
| 2007 | |
| 2008 | if (!conn) |
| 2009 | return 0; |
| 2010 | |
| 2011 | chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN"); |
| 2012 | |
| 2013 | if ((li = objt_listener(conn->target))) |
| 2014 | chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id); |
| 2015 | else if ((sv = objt_server(conn->target))) |
| 2016 | chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id); |
| 2017 | else if ((px = objt_proxy(conn->target))) |
| 2018 | chunk_appendf(buf, " be=%s", px->id); |
| 2019 | |
| 2020 | chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn)); |
| 2021 | |
| 2022 | if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr))) |
| 2023 | chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src)); |
| 2024 | |
| 2025 | if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr))) |
| 2026 | chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst)); |
| 2027 | |
| 2028 | return buf->data - old_len; |
| 2029 | } |
| 2030 | |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 2031 | /* return the major HTTP version as 1 or 2 depending on how the request arrived |
| 2032 | * before being processed. |
Christopher Faulet | f4dd9ae | 2021-04-14 15:40:30 +0200 | [diff] [blame] | 2033 | * |
| 2034 | * WARNING: Should be updated if a new major HTTP version is added. |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 2035 | */ |
| 2036 | static int |
| 2037 | smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2038 | { |
Christopher Faulet | 242f8ce | 2021-04-14 15:46:49 +0200 | [diff] [blame] | 2039 | struct connection *conn = NULL; |
| 2040 | |
| 2041 | if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) |
| 2042 | conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL; |
| 2043 | else |
| 2044 | conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) : |
| 2045 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 2046 | |
Christopher Faulet | f4dd9ae | 2021-04-14 15:40:30 +0200 | [diff] [blame] | 2047 | /* No connection or a connection with a RAW muxx */ |
| 2048 | if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX))) |
| 2049 | return 0; |
| 2050 | |
| 2051 | /* No mux install, this may change */ |
| 2052 | if (!conn->mux) { |
| 2053 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 2057 | smp->data.type = SMP_T_SINT; |
Christopher Faulet | f4dd9ae | 2021-04-14 15:40:30 +0200 | [diff] [blame] | 2058 | smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1; |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 2059 | return 1; |
| 2060 | } |
| 2061 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 2062 | /* fetch if the received connection used a PROXY protocol header */ |
| 2063 | int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2064 | { |
| 2065 | struct connection *conn; |
| 2066 | |
| 2067 | conn = objt_conn(smp->sess->origin); |
| 2068 | if (!conn) |
| 2069 | return 0; |
| 2070 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 2071 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 2072 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | smp->flags = 0; |
| 2077 | smp->data.type = SMP_T_BOOL; |
| 2078 | smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0; |
| 2079 | |
| 2080 | return 1; |
| 2081 | } |
| 2082 | |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 2083 | /* fetch the authority TLV from a PROXY protocol header */ |
| 2084 | int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2085 | { |
| 2086 | struct connection *conn; |
| 2087 | |
| 2088 | conn = objt_conn(smp->sess->origin); |
| 2089 | if (!conn) |
| 2090 | return 0; |
| 2091 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 2092 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 2093 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2094 | return 0; |
| 2095 | } |
| 2096 | |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 2097 | if (!isttest(conn->proxy_authority)) |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 2098 | return 0; |
| 2099 | |
| 2100 | smp->flags = 0; |
| 2101 | smp->data.type = SMP_T_STR; |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 2102 | smp->data.u.str.area = istptr(conn->proxy_authority); |
| 2103 | smp->data.u.str.data = istlen(conn->proxy_authority); |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 2104 | |
| 2105 | return 1; |
| 2106 | } |
| 2107 | |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 2108 | /* fetch the unique ID TLV from a PROXY protocol header */ |
| 2109 | int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2110 | { |
| 2111 | struct connection *conn; |
| 2112 | |
| 2113 | conn = objt_conn(smp->sess->origin); |
| 2114 | if (!conn) |
| 2115 | return 0; |
| 2116 | |
| 2117 | if (conn->flags & CO_FL_WAIT_XPRT) { |
| 2118 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | if (!isttest(conn->proxy_unique_id)) |
| 2123 | return 0; |
| 2124 | |
| 2125 | smp->flags = 0; |
| 2126 | smp->data.type = SMP_T_STR; |
Tim Duesterhus | 002bd77 | 2021-03-06 20:06:49 +0100 | [diff] [blame] | 2127 | smp->data.u.str.area = istptr(conn->proxy_unique_id); |
| 2128 | smp->data.u.str.data = istlen(conn->proxy_unique_id); |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 2129 | |
| 2130 | return 1; |
| 2131 | } |
| 2132 | |
Remi Tricot-Le Breton | 3d2093a | 2021-07-29 09:45:49 +0200 | [diff] [blame] | 2133 | /* fetch the error code of a connection */ |
Willy Tarreau | 6f74976 | 2021-11-05 17:07:03 +0100 | [diff] [blame] | 2134 | int smp_fetch_fc_err(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Remi Tricot-Le Breton | 3d2093a | 2021-07-29 09:45:49 +0200 | [diff] [blame] | 2135 | { |
| 2136 | struct connection *conn; |
| 2137 | |
Remi Tricot-Le Breton | 942c167 | 2021-09-01 15:52:15 +0200 | [diff] [blame] | 2138 | if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) |
| 2139 | conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL; |
| 2140 | else |
| 2141 | conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) : |
| 2142 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 2143 | |
Remi Tricot-Le Breton | 3d2093a | 2021-07-29 09:45:49 +0200 | [diff] [blame] | 2144 | if (!conn) |
| 2145 | return 0; |
| 2146 | |
| 2147 | if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) { |
| 2148 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | smp->flags = 0; |
| 2153 | smp->data.type = SMP_T_SINT; |
| 2154 | smp->data.u.sint = (unsigned long long int)conn->err_code; |
| 2155 | |
| 2156 | return 1; |
| 2157 | } |
| 2158 | |
| 2159 | /* fetch a string representation of the error code of a connection */ |
Willy Tarreau | 6f74976 | 2021-11-05 17:07:03 +0100 | [diff] [blame] | 2160 | int smp_fetch_fc_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Remi Tricot-Le Breton | 3d2093a | 2021-07-29 09:45:49 +0200 | [diff] [blame] | 2161 | { |
| 2162 | struct connection *conn; |
| 2163 | const char *err_code_str; |
| 2164 | |
Remi Tricot-Le Breton | 942c167 | 2021-09-01 15:52:15 +0200 | [diff] [blame] | 2165 | if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) |
| 2166 | conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL; |
| 2167 | else |
| 2168 | conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) : |
| 2169 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 2170 | |
Remi Tricot-Le Breton | 3d2093a | 2021-07-29 09:45:49 +0200 | [diff] [blame] | 2171 | if (!conn) |
| 2172 | return 0; |
| 2173 | |
| 2174 | if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) { |
| 2175 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | err_code_str = conn_err_code_str(conn); |
| 2180 | |
| 2181 | if (!err_code_str) |
| 2182 | return 0; |
| 2183 | |
| 2184 | smp->flags = 0; |
| 2185 | smp->data.type = SMP_T_STR; |
| 2186 | smp->data.u.str.area = (char*)err_code_str; |
| 2187 | smp->data.u.str.data = strlen(err_code_str); |
| 2188 | |
| 2189 | return 1; |
| 2190 | } |
| 2191 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 2192 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2193 | * Note: fetches that may return multiple types must be declared as the lowest |
| 2194 | * common denominator, the type that can be casted into all other ones. For |
| 2195 | * instance v4/v6 must be declared v4. |
| 2196 | */ |
| 2197 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 6f74976 | 2021-11-05 17:07:03 +0100 | [diff] [blame] | 2198 | { "bc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV }, |
| 2199 | { "bc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4SRV }, |
Jérôme Magnin | 8657742 | 2018-12-07 09:03:11 +0100 | [diff] [blame] | 2200 | { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV }, |
Willy Tarreau | 6f74976 | 2021-11-05 17:07:03 +0100 | [diff] [blame] | 2201 | { "fc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI }, |
| 2202 | { "fc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4CLI }, |
| 2203 | { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI }, |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 2204 | { "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] | 2205 | { "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] | 2206 | { "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] | 2207 | { /* END */ }, |
| 2208 | }}; |
| 2209 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2210 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 2211 | |
| 2212 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 2213 | { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local }, |
| 2214 | { /* END */ }, |
| 2215 | }}; |
| 2216 | |
| 2217 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 2218 | |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 2219 | /* private function to handle sockaddr as input for connection hash */ |
| 2220 | static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss, |
| 2221 | char *buf, size_t *idx, |
| 2222 | enum conn_hash_params_t *hash_flags, |
| 2223 | enum conn_hash_params_t param_type_addr, |
| 2224 | enum conn_hash_params_t param_type_port) |
| 2225 | { |
| 2226 | struct sockaddr_in *addr; |
| 2227 | struct sockaddr_in6 *addr6; |
| 2228 | |
| 2229 | switch (ss->ss_family) { |
| 2230 | case AF_INET: |
| 2231 | addr = (struct sockaddr_in *)ss; |
| 2232 | |
| 2233 | conn_hash_update(buf, idx, |
| 2234 | &addr->sin_addr, sizeof(addr->sin_addr), |
| 2235 | hash_flags, param_type_addr); |
| 2236 | |
| 2237 | if (addr->sin_port) { |
| 2238 | conn_hash_update(buf, idx, |
| 2239 | &addr->sin_port, sizeof(addr->sin_port), |
| 2240 | hash_flags, param_type_port); |
| 2241 | } |
| 2242 | |
| 2243 | break; |
| 2244 | |
| 2245 | case AF_INET6: |
| 2246 | addr6 = (struct sockaddr_in6 *)ss; |
| 2247 | |
| 2248 | conn_hash_update(buf, idx, |
| 2249 | &addr6->sin6_addr, sizeof(addr6->sin6_addr), |
| 2250 | hash_flags, param_type_addr); |
| 2251 | |
| 2252 | if (addr6->sin6_port) { |
| 2253 | conn_hash_update(buf, idx, |
| 2254 | &addr6->sin6_port, sizeof(addr6->sin6_port), |
| 2255 | hash_flags, param_type_port); |
| 2256 | } |
| 2257 | |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | |
Willy Tarreau | e5983ff | 2021-10-06 17:14:49 +0200 | [diff] [blame] | 2262 | /* Generate the hash of a connection with params as input |
| 2263 | * Each non-null field of params is taken into account for the hash calcul. |
| 2264 | */ |
| 2265 | uint64_t conn_hash_prehash(char *buf, size_t size) |
| 2266 | { |
| 2267 | return XXH64(buf, size, 0); |
| 2268 | } |
| 2269 | |
| 2270 | /* Append <data> into <buf> at <idx> offset in preparation for connection hash |
| 2271 | * calcul. <idx> is incremented beyond data <size>. In the same time, <flags> |
| 2272 | * are updated with <type> for the hash header. |
| 2273 | */ |
| 2274 | void conn_hash_update(char *buf, size_t *idx, |
| 2275 | const void *data, size_t size, |
| 2276 | enum conn_hash_params_t *flags, |
| 2277 | enum conn_hash_params_t type) |
| 2278 | { |
| 2279 | memcpy(&buf[*idx], data, size); |
| 2280 | *idx += size; |
| 2281 | *flags |= type; |
| 2282 | } |
| 2283 | |
| 2284 | uint64_t conn_hash_digest(char *buf, size_t bufsize, |
| 2285 | enum conn_hash_params_t flags) |
| 2286 | { |
| 2287 | const uint64_t flags_u64 = (uint64_t)flags; |
| 2288 | const uint64_t hash = XXH64(buf, bufsize, 0); |
| 2289 | |
| 2290 | return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash); |
| 2291 | } |
| 2292 | |
Willy Tarreau | fd21c6c | 2021-10-06 17:09:41 +0200 | [diff] [blame] | 2293 | uint64_t conn_calculate_hash(const struct conn_hash_params *params) |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 2294 | { |
| 2295 | char *buf; |
| 2296 | size_t idx = 0; |
Willy Tarreau | fd21c6c | 2021-10-06 17:09:41 +0200 | [diff] [blame] | 2297 | uint64_t hash = 0; |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 2298 | enum conn_hash_params_t hash_flags = 0; |
| 2299 | |
| 2300 | buf = trash.area; |
| 2301 | |
Amaury Denoyelle | 8ede3db | 2021-03-02 14:38:53 +0100 | [diff] [blame] | 2302 | 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] | 2303 | |
Amaury Denoyelle | 9b626e3 | 2021-01-06 17:03:27 +0100 | [diff] [blame] | 2304 | if (params->sni_prehash) { |
| 2305 | conn_hash_update(buf, &idx, |
Amaury Denoyelle | 36441f4 | 2021-02-17 16:25:31 +0100 | [diff] [blame] | 2306 | ¶ms->sni_prehash, sizeof(params->sni_prehash), |
Amaury Denoyelle | 9b626e3 | 2021-01-06 17:03:27 +0100 | [diff] [blame] | 2307 | &hash_flags, CONN_HASH_PARAMS_TYPE_SNI); |
| 2308 | } |
| 2309 | |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 2310 | if (params->dst_addr) { |
| 2311 | conn_calculate_hash_sockaddr(params->dst_addr, |
| 2312 | buf, &idx, &hash_flags, |
| 2313 | CONN_HASH_PARAMS_TYPE_DST_ADDR, |
| 2314 | CONN_HASH_PARAMS_TYPE_DST_PORT); |
| 2315 | } |
| 2316 | |
Amaury Denoyelle | d10a200 | 2021-02-11 19:45:19 +0100 | [diff] [blame] | 2317 | if (params->src_addr) { |
| 2318 | conn_calculate_hash_sockaddr(params->src_addr, |
| 2319 | buf, &idx, &hash_flags, |
| 2320 | CONN_HASH_PARAMS_TYPE_SRC_ADDR, |
| 2321 | CONN_HASH_PARAMS_TYPE_SRC_PORT); |
| 2322 | } |
| 2323 | |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 2324 | if (params->proxy_prehash) { |
| 2325 | conn_hash_update(buf, &idx, |
Amaury Denoyelle | 36441f4 | 2021-02-17 16:25:31 +0100 | [diff] [blame] | 2326 | ¶ms->proxy_prehash, sizeof(params->proxy_prehash), |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 2327 | &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY); |
| 2328 | } |
Amaury Denoyelle | 01a287f | 2021-02-11 16:46:53 +0100 | [diff] [blame] | 2329 | |
Amaury Denoyelle | 1921d20 | 2021-01-14 10:15:29 +0100 | [diff] [blame] | 2330 | hash = conn_hash_digest(buf, idx, hash_flags); |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 2331 | return hash; |
| 2332 | } |
Amaury Denoyelle | 4837293 | 2021-09-16 12:15:12 +0200 | [diff] [blame] | 2333 | |
| 2334 | /* Handler of the task of mux_stopping_data. |
| 2335 | * Called on soft-stop. |
| 2336 | */ |
| 2337 | static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state) |
| 2338 | { |
| 2339 | struct connection *conn, *back; |
| 2340 | |
| 2341 | list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) { |
| 2342 | if (conn->mux && conn->mux->wake) |
| 2343 | conn->mux->wake(conn); |
| 2344 | } |
| 2345 | |
| 2346 | return t; |
| 2347 | } |
| 2348 | |
| 2349 | static int allocate_mux_cleanup(void) |
| 2350 | { |
| 2351 | /* allocates the thread bound mux_stopping_data task */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 2352 | mux_stopping_data[tid].task = task_new_here(); |
Amaury Denoyelle | 4837293 | 2021-09-16 12:15:12 +0200 | [diff] [blame] | 2353 | if (!mux_stopping_data[tid].task) { |
| 2354 | ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid); |
| 2355 | return 0; |
| 2356 | } |
| 2357 | |
| 2358 | mux_stopping_data[tid].task->process = mux_stopping_process; |
| 2359 | LIST_INIT(&mux_stopping_data[tid].list); |
| 2360 | |
| 2361 | return 1; |
| 2362 | } |
| 2363 | REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup); |
| 2364 | |
| 2365 | static int deallocate_mux_cleanup(void) |
| 2366 | { |
| 2367 | task_destroy(mux_stopping_data[tid].task); |
| 2368 | return 1; |
| 2369 | } |
| 2370 | REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup); |