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