Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 2 | * include/haproxy/connection.h |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 3 | * This file contains connection function prototypes |
| 4 | * |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2002 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 22 | #ifndef _HAPROXY_CONNECTION_H |
| 23 | #define _HAPROXY_CONNECTION_H |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 25 | #include <import/ist.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Christopher Faulet | bb69d78 | 2021-03-22 17:06:24 +0100 | [diff] [blame] | 28 | #include <haproxy/buf.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 29 | #include <haproxy/connection-t.h> |
| 30 | #include <haproxy/fd.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 31 | #include <haproxy/http_ana.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 32 | #include <haproxy/listener-t.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 33 | #include <haproxy/obj_type.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 34 | #include <haproxy/pool.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 35 | #include <haproxy/session.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/task-t.h> |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 37 | #include <haproxy/tcpcheck-t.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 40 | extern struct pool_head *pool_head_connection; |
| 41 | extern struct pool_head *pool_head_connstream; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 42 | extern struct pool_head *pool_head_conn_hash_node; |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 43 | extern struct pool_head *pool_head_sockaddr; |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 44 | extern struct pool_head *pool_head_authority; |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 45 | extern struct xprt_ops *registered_xprt[XPRT_ENTRIES]; |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 46 | extern struct mux_proto_list mux_proto_list; |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 47 | extern struct mux_stopping_data mux_stopping_data[MAX_THREADS]; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 48 | |
Christopher Faulet | e60abd1 | 2020-04-16 10:03:58 +0200 | [diff] [blame] | 49 | #define IS_HTX_CONN(conn) ((conn)->mux && ((conn)->mux->flags & MX_FL_HTX)) |
| 50 | #define IS_HTX_CS(cs) (IS_HTX_CONN((cs)->conn)) |
| 51 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 52 | /* receive a PROXY protocol header over a connection */ |
| 53 | int conn_recv_proxy(struct connection *conn, int flag); |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 54 | int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 55 | int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst); |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 56 | int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm); |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | a7d1455 | 2021-06-16 17:35:20 +0200 | [diff] [blame] | 58 | int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx); |
| 59 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 60 | int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es); |
| 61 | 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] | 62 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 63 | /* receive a NetScaler Client IP insertion header over a connection */ |
| 64 | int conn_recv_netscaler_cip(struct connection *conn, int flag); |
| 65 | |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 66 | /* raw send() directly on the socket */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 67 | 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] | 68 | |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 69 | /* drains any pending bytes from the socket */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 70 | int conn_ctrl_drain(struct connection *conn); |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 71 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 72 | /* scoks4 proxy handshake */ |
| 73 | int conn_send_socks4_proxy_request(struct connection *conn); |
| 74 | int conn_recv_socks4_proxy_response(struct connection *conn); |
| 75 | |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 76 | /* If we delayed the mux creation because we were waiting for the handshake, do it now */ |
| 77 | int conn_create_mux(struct connection *conn); |
| 78 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 79 | extern struct idle_conns idle_conns[MAX_THREADS]; |
Olivier Houchard | 4be7190 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 80 | |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 81 | /* returns true if the transport layer is ready */ |
Willy Tarreau | aad6938 | 2014-01-23 14:21:42 +0100 | [diff] [blame] | 82 | static inline int conn_xprt_ready(const struct connection *conn) |
Willy Tarreau | d02cdd2 | 2013-12-15 10:23:20 +0100 | [diff] [blame] | 83 | { |
Willy Tarreau | aad6938 | 2014-01-23 14:21:42 +0100 | [diff] [blame] | 84 | return (conn->flags & CO_FL_XPRT_READY); |
Willy Tarreau | d02cdd2 | 2013-12-15 10:23:20 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 87 | /* returns true if the control layer is ready */ |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 88 | static inline int conn_ctrl_ready(const struct connection *conn) |
Willy Tarreau | d02cdd2 | 2013-12-15 10:23:20 +0100 | [diff] [blame] | 89 | { |
| 90 | return (conn->flags & CO_FL_CTRL_READY); |
| 91 | } |
| 92 | |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Calls the start() function of the transport layer, if needed. |
| 95 | * Returns < 0 in case of error. |
| 96 | */ |
| 97 | |
| 98 | static inline int conn_xprt_start(struct connection *conn) |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 99 | { |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 100 | int ret = 0; |
| 101 | |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 102 | if (!conn_xprt_ready(conn) && conn->xprt && conn->xprt->start) |
| 103 | ret = conn->xprt->start(conn, conn->xprt_ctx); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 104 | |
| 105 | if (ret >= 0) |
| 106 | conn->flags |= CO_FL_XPRT_READY; |
| 107 | |
| 108 | return ret; |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 111 | /* Calls the close() function of the transport layer if any and if not done |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 112 | * yet, and clears the CO_FL_XPRT_READY flags |
| 113 | * However this is not done if the CO_FL_XPRT_TRACKED flag is set, |
| 114 | * which allows logs to take data from the transport layer very late if needed. |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 115 | */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 116 | static inline void conn_xprt_close(struct connection *conn) |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 117 | { |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 118 | if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) { |
Willy Tarreau | aad6938 | 2014-01-23 14:21:42 +0100 | [diff] [blame] | 119 | if (conn->xprt->close) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 120 | conn->xprt->close(conn, conn->xprt_ctx); |
Olivier Houchard | 478281f | 2019-05-13 19:10:46 +0200 | [diff] [blame] | 121 | conn->xprt_ctx = NULL; |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 122 | conn->flags &= ~CO_FL_XPRT_READY; |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 123 | conn->xprt = NULL; |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Initializes the connection's control layer which essentially consists in |
Willy Tarreau | c14e7ae | 2020-12-08 15:53:45 +0100 | [diff] [blame] | 128 | * registering the connection handle (e.g. file descriptor) for events and |
| 129 | * setting the CO_FL_CTRL_READY flag. The caller is responsible for ensuring |
| 130 | * that the control layer is already assigned to the connection prior to the |
| 131 | * call. |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 132 | */ |
| 133 | static inline void conn_ctrl_init(struct connection *conn) |
| 134 | { |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 135 | if (!conn_ctrl_ready(conn)) { |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 136 | conn->flags |= CO_FL_CTRL_READY; |
Willy Tarreau | c14e7ae | 2020-12-08 15:53:45 +0100 | [diff] [blame] | 137 | if (conn->ctrl->ctrl_init) |
| 138 | conn->ctrl->ctrl_init(conn); |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 139 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Willy Tarreau | c14e7ae | 2020-12-08 15:53:45 +0100 | [diff] [blame] | 142 | /* Deletes the connection's handle (e.g. FD) if the transport layer is already |
| 143 | * gone, and removes the CO_FL_CTRL_READY flag. |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 144 | */ |
| 145 | static inline void conn_ctrl_close(struct connection *conn) |
| 146 | { |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 147 | if (!conn->xprt && (conn->flags & CO_FL_CTRL_READY)) { |
Willy Tarreau | b17952c | 2021-10-21 21:31:42 +0200 | [diff] [blame] | 148 | if ((conn->flags & (CO_FL_WANT_DRAIN | CO_FL_SOCK_RD_SH)) == CO_FL_WANT_DRAIN) |
| 149 | conn_ctrl_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 150 | conn->flags &= ~CO_FL_CTRL_READY; |
Willy Tarreau | c14e7ae | 2020-12-08 15:53:45 +0100 | [diff] [blame] | 151 | if (conn->ctrl->ctrl_close) |
| 152 | conn->ctrl->ctrl_close(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Willy Tarreau | 2b199c9 | 2012-11-23 17:32:21 +0100 | [diff] [blame] | 156 | /* If the connection still has a transport layer, then call its close() function |
| 157 | * if any, and delete the file descriptor if a control layer is set. This is |
| 158 | * used to close everything at once and atomically. However this is not done if |
| 159 | * the CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the |
| 160 | * transport layer very late if needed. |
| 161 | */ |
| 162 | static inline void conn_full_close(struct connection *conn) |
| 163 | { |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 164 | conn_xprt_close(conn); |
| 165 | conn_ctrl_close(conn); |
| 166 | } |
| 167 | |
Willy Tarreau | dc42acd | 2017-10-05 18:09:20 +0200 | [diff] [blame] | 168 | /* stop tracking a connection, allowing conn_full_close() to always |
| 169 | * succeed. |
| 170 | */ |
| 171 | static inline void conn_stop_tracking(struct connection *conn) |
| 172 | { |
| 173 | conn->flags &= ~CO_FL_XPRT_TRACKED; |
| 174 | } |
| 175 | |
Willy Tarreau | 7b271b2 | 2017-10-25 09:59:22 +0200 | [diff] [blame] | 176 | /* read shutdown, called from the rcv_buf/rcv_pipe handlers when |
| 177 | * detecting an end of connection. |
| 178 | */ |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 179 | static inline void conn_sock_read0(struct connection *c) |
| 180 | { |
| 181 | c->flags |= CO_FL_SOCK_RD_SH; |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 182 | if (conn_ctrl_ready(c)) { |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 183 | /* we don't risk keeping ports unusable if we found the |
| 184 | * zero from the other side. |
| 185 | */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 186 | HA_ATOMIC_AND(&fdtab[c->handle.fd].state, ~FD_LINGER_RISK); |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 187 | } |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Willy Tarreau | 7b271b2 | 2017-10-25 09:59:22 +0200 | [diff] [blame] | 190 | /* write shutdown, indication that the upper layer is not willing to send |
Willy Tarreau | a48c141 | 2017-12-22 18:46:33 +0100 | [diff] [blame] | 191 | * anything anymore and wants to close after pending data are sent. The |
| 192 | * <clean> argument will allow not to perform the socket layer shutdown if |
| 193 | * equal to 0. |
Willy Tarreau | 7b271b2 | 2017-10-25 09:59:22 +0200 | [diff] [blame] | 194 | */ |
Willy Tarreau | a48c141 | 2017-12-22 18:46:33 +0100 | [diff] [blame] | 195 | static inline void conn_sock_shutw(struct connection *c, int clean) |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 196 | { |
| 197 | c->flags |= CO_FL_SOCK_WR_SH; |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 198 | if (conn_ctrl_ready(c)) { |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 199 | /* don't perform a clean shutdown if we're going to reset or |
| 200 | * if the shutr was already received. |
| 201 | */ |
| 202 | if (!(c->flags & CO_FL_SOCK_RD_SH) && clean) |
| 203 | shutdown(c->handle.fd, SHUT_WR); |
| 204 | } |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Olivier Houchard | 1a0545f | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 207 | static inline void conn_xprt_shutw(struct connection *c) |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 208 | { |
Willy Tarreau | 729c69f | 2015-03-12 22:51:10 +0100 | [diff] [blame] | 209 | /* clean data-layer shutdown */ |
| 210 | if (c->xprt && c->xprt->shutw) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 211 | c->xprt->shutw(c, c->xprt_ctx, 1); |
Willy Tarreau | 729c69f | 2015-03-12 22:51:10 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Olivier Houchard | 1a0545f | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 214 | static inline void conn_xprt_shutw_hard(struct connection *c) |
Willy Tarreau | 729c69f | 2015-03-12 22:51:10 +0100 | [diff] [blame] | 215 | { |
Willy Tarreau | 729c69f | 2015-03-12 22:51:10 +0100 | [diff] [blame] | 216 | /* unclean data-layer shutdown */ |
| 217 | if (c->xprt && c->xprt->shutw) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 218 | c->xprt->shutw(c, c->xprt_ctx, 0); |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Christopher Faulet | c5fd15d | 2023-03-16 11:43:05 +0100 | [diff] [blame] | 221 | /* Used to know if a connection is in an idle list. It returns connection flag |
| 222 | * corresponding to the idle list if the connection is idle (CO_FL_SAFE_LIST or |
| 223 | * CO_FL_IDLE_LIST) or 0 otherwise. Note that if the connection is scheduled to |
| 224 | * be removed, 0 is returned, regardless the connection flags. |
| 225 | */ |
| 226 | static inline unsigned int conn_get_idle_flag(const struct connection *conn) |
| 227 | { |
| 228 | return (!MT_LIST_INLIST(&conn->toremove_list) ? conn->flags & CO_FL_LIST_MASK : 0); |
| 229 | } |
| 230 | |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 231 | /* This is used at the end of the socket IOCB to possibly create the mux if it |
| 232 | * was not done yet, or wake it up if flags changed compared to old_flags or if |
| 233 | * need_wake insists on this. It returns <0 if the connection was destroyed and |
| 234 | * must not be used, >=0 otherwise. |
| 235 | */ |
| 236 | static inline int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake) |
| 237 | { |
| 238 | int ret = 0; |
| 239 | |
| 240 | /* If we don't yet have a mux, that means we were waiting for |
| 241 | * information to create one, typically from the ALPN. If we're |
| 242 | * done with the handshake, attempt to create one. |
| 243 | */ |
| 244 | if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) { |
| 245 | ret = conn_create_mux(conn); |
| 246 | if (ret < 0) |
| 247 | goto done; |
| 248 | } |
| 249 | |
| 250 | /* The wake callback is normally used to notify the data layer about |
| 251 | * data layer activity (successful send/recv), connection establishment, |
| 252 | * shutdown and fatal errors. We need to consider the following |
| 253 | * situations to wake up the data layer : |
| 254 | * - change among the CO_FL_NOTIFY_DONE flags : |
| 255 | * SOCK_{RD,WR}_SH, ERROR, |
| 256 | * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the |
| 257 | * end of handshake and transition to CONNECTED |
| 258 | * - raise of CONNECTED with HANDSHAKE down |
| 259 | * - end of HANDSHAKE with CONNECTED set |
| 260 | * - regular data layer activity |
| 261 | * |
Willy Tarreau | def47dd0 | 2021-07-28 15:27:37 +0200 | [diff] [blame] | 262 | * One tricky case is the wake up on read0 or error on an idle |
| 263 | * backend connection, that can happen on a connection that is still |
| 264 | * polled while at the same moment another thread is about to perform a |
| 265 | * takeover. The solution against this is to remove the connection from |
| 266 | * the idle list if it was in it, and possibly reinsert it at the end |
| 267 | * if the connection remains valid. The cost is non-null (locked tree |
| 268 | * removal) but remains low given that this is extremely rarely called. |
| 269 | * In any case it's guaranteed by the FD's thread_mask that we're |
| 270 | * called from the same thread the connection is queued in. |
| 271 | * |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 272 | * Note that the wake callback is allowed to release the connection and |
| 273 | * the fd (and return < 0 in this case). |
| 274 | */ |
| 275 | if ((forced_wake || |
| 276 | ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) || |
| 277 | ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) && |
| 278 | conn->mux && conn->mux->wake) { |
Christopher Faulet | c5fd15d | 2023-03-16 11:43:05 +0100 | [diff] [blame] | 279 | uint conn_in_list = conn_get_idle_flag(conn); |
Willy Tarreau | def47dd0 | 2021-07-28 15:27:37 +0200 | [diff] [blame] | 280 | struct server *srv = objt_server(conn->target); |
| 281 | |
| 282 | if (conn_in_list) { |
| 283 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 284 | conn_delete_from_tree(&conn->hash_node->node); |
| 285 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 286 | } |
| 287 | |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 288 | ret = conn->mux->wake(conn); |
| 289 | if (ret < 0) |
| 290 | goto done; |
Willy Tarreau | def47dd0 | 2021-07-28 15:27:37 +0200 | [diff] [blame] | 291 | |
| 292 | if (conn_in_list) { |
| 293 | struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ? |
| 294 | &srv->per_thr[tid].safe_conns : |
| 295 | &srv->per_thr[tid].idle_conns; |
| 296 | |
| 297 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 298 | ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
| 299 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 300 | } |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 301 | } |
| 302 | done: |
| 303 | return ret; |
| 304 | } |
| 305 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 306 | /* shut read */ |
| 307 | static inline void cs_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 308 | { |
Willy Tarreau | aa34946 | 2021-11-14 13:42:17 +0100 | [diff] [blame] | 309 | if (cs->flags & CS_FL_SHR) |
| 310 | return; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 311 | |
| 312 | /* clean data-layer shutdown */ |
| 313 | if (cs->conn->mux && cs->conn->mux->shutr) |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 314 | cs->conn->mux->shutr(cs, mode); |
| 315 | cs->flags |= (mode == CS_SHR_DRAIN) ? CS_FL_SHRD : CS_FL_SHRR; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 318 | /* shut write */ |
| 319 | static inline void cs_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 320 | { |
Willy Tarreau | aa34946 | 2021-11-14 13:42:17 +0100 | [diff] [blame] | 321 | if (cs->flags & CS_FL_SHW) |
| 322 | return; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 323 | |
| 324 | /* clean data-layer shutdown */ |
| 325 | if (cs->conn->mux && cs->conn->mux->shutw) |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 326 | cs->conn->mux->shutw(cs, mode); |
| 327 | cs->flags |= (mode == CS_SHW_NORMAL) ? CS_FL_SHWN : CS_FL_SHWS; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Willy Tarreau | 6978db3 | 2017-10-05 18:19:43 +0200 | [diff] [blame] | 330 | /* completely close a conn_stream (but do not detach it) */ |
| 331 | static inline void cs_close(struct conn_stream *cs) |
| 332 | { |
| 333 | cs_shutw(cs, CS_SHW_SILENT); |
| 334 | cs_shutr(cs, CS_SHR_RESET); |
Willy Tarreau | 6978db3 | 2017-10-05 18:19:43 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Willy Tarreau | 6aee5b9 | 2020-12-11 11:04:51 +0100 | [diff] [blame] | 337 | /* completely close a conn_stream after draining possibly pending data (but do not detach it) */ |
| 338 | static inline void cs_drain_and_close(struct conn_stream *cs) |
| 339 | { |
| 340 | cs_shutw(cs, CS_SHW_SILENT); |
| 341 | cs_shutr(cs, CS_SHR_DRAIN); |
Willy Tarreau | 6aee5b9 | 2020-12-11 11:04:51 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Willy Tarreau | e9f4301 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 344 | /* sets CS_FL_ERROR or CS_FL_ERR_PENDING on the cs */ |
| 345 | static inline void cs_set_error(struct conn_stream *cs) |
| 346 | { |
| 347 | if (cs->flags & CS_FL_EOS) |
| 348 | cs->flags |= CS_FL_ERROR; |
| 349 | else |
Willy Tarreau | 7bb39d7 | 2019-06-03 14:23:33 +0200 | [diff] [blame] | 350 | cs->flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | e9f4301 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 353 | /* detect sock->data read0 transition */ |
Olivier Houchard | 1a0545f | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 354 | static inline int conn_xprt_read0_pending(struct connection *c) |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 355 | { |
Willy Tarreau | 54e917c | 2017-08-30 07:35:35 +0200 | [diff] [blame] | 356 | return (c->flags & CO_FL_SOCK_RD_SH) != 0; |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 359 | /* prepares a connection to work with protocol <proto> and transport <xprt>. |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 360 | * The transport's is initialized as well, and the mux and its context are |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 361 | * cleared. The target is not reinitialized and it is recommended that it is |
| 362 | * set prior to calling this function so that the function may make use of it |
| 363 | * in the future to refine the mux choice if needed. |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 364 | */ |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 365 | static inline int conn_prepare(struct connection *conn, const struct protocol *proto, const struct xprt_ops *xprt) |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 366 | { |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 367 | int ret = 0; |
| 368 | |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 369 | conn->ctrl = proto; |
| 370 | conn->xprt = xprt; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 371 | conn->mux = NULL; |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 372 | conn->xprt_ctx = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 373 | conn->ctx = NULL; |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 374 | if (xprt->init) { |
| 375 | ret = xprt->init(conn, &conn->xprt_ctx); |
| 376 | if (ret < 0) |
| 377 | conn->xprt = NULL; |
| 378 | } |
| 379 | return ret; |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 382 | /* |
| 383 | * Initializes all required fields for a new conn_strema. |
| 384 | */ |
| 385 | static inline void cs_init(struct conn_stream *cs, struct connection *conn) |
| 386 | { |
| 387 | cs->obj_type = OBJ_TYPE_CS; |
| 388 | cs->flags = CS_FL_NONE; |
| 389 | cs->conn = conn; |
| 390 | } |
| 391 | |
Amaury Denoyelle | 99cca08 | 2021-05-03 14:28:30 +0200 | [diff] [blame] | 392 | /* returns 0 if the connection is valid and is a frontend connection, otherwise |
| 393 | * returns 1 indicating it's a backend connection. And uninitialized connection |
| 394 | * also returns 1 to better handle the usage in the middle of initialization. |
| 395 | */ |
| 396 | static inline int conn_is_back(const struct connection *conn) |
| 397 | { |
| 398 | return !objt_listener(conn->target); |
| 399 | } |
| 400 | |
Willy Tarreau | d015577 | 2013-10-14 17:10:08 +0200 | [diff] [blame] | 401 | /* Initializes all required fields for a new connection. Note that it does the |
| 402 | * minimum acceptable initialization for a connection that already exists and |
| 403 | * is about to be reused. It also leaves the addresses untouched, which makes |
| 404 | * it usable across connection retries to reset a connection to a known state. |
| 405 | */ |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 406 | static inline void conn_init(struct connection *conn, void *target) |
Willy Tarreau | d015577 | 2013-10-14 17:10:08 +0200 | [diff] [blame] | 407 | { |
| 408 | conn->obj_type = OBJ_TYPE_CONN; |
| 409 | conn->flags = CO_FL_NONE; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 410 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 411 | conn->ctx = NULL; |
Willy Tarreau | d015577 | 2013-10-14 17:10:08 +0200 | [diff] [blame] | 412 | conn->owner = NULL; |
Willy Tarreau | b8020ce | 2013-10-24 21:10:08 +0200 | [diff] [blame] | 413 | conn->send_proxy_ofs = 0; |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 414 | conn->handle.fd = DEAD_FD_MAGIC; |
Willy Tarreau | d015577 | 2013-10-14 17:10:08 +0200 | [diff] [blame] | 415 | conn->err_code = CO_ER_NONE; |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 416 | conn->target = target; |
Willy Tarreau | 436d333 | 2017-10-08 11:16:46 +0200 | [diff] [blame] | 417 | conn->destroy_cb = NULL; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 418 | conn->proxy_netns = NULL; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 419 | MT_LIST_INIT(&conn->toremove_list); |
Amaury Denoyelle | 99cca08 | 2021-05-03 14:28:30 +0200 | [diff] [blame] | 420 | if (conn_is_back(conn)) |
| 421 | LIST_INIT(&conn->session_list); |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 422 | else |
| 423 | LIST_INIT(&conn->stopping_list); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 424 | conn->subs = NULL; |
Willy Tarreau | e71fca8 | 2019-07-17 19:06:58 +0200 | [diff] [blame] | 425 | conn->src = NULL; |
| 426 | conn->dst = NULL; |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 427 | conn->proxy_authority = IST_NULL; |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 428 | conn->proxy_unique_id = IST_NULL; |
Frédéric Lécaille | c0ed919 | 2021-03-10 11:51:38 +0100 | [diff] [blame] | 429 | conn->qc = NULL; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 430 | conn->hash_node = NULL; |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 431 | conn->xprt = NULL; |
Willy Tarreau | d015577 | 2013-10-14 17:10:08 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 434 | static inline struct conn_hash_node *conn_alloc_hash_node(struct connection *conn) |
| 435 | { |
| 436 | struct conn_hash_node *hash_node = NULL; |
| 437 | |
Willy Tarreau | 5d110b2 | 2021-03-22 21:01:05 +0100 | [diff] [blame] | 438 | hash_node = pool_zalloc(pool_head_conn_hash_node); |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 439 | if (unlikely(!hash_node)) |
| 440 | return NULL; |
| 441 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 442 | hash_node->conn = conn; |
| 443 | |
| 444 | return hash_node; |
| 445 | } |
| 446 | |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 447 | /* sets <owner> as the connection's owner */ |
Willy Tarreau | 436d333 | 2017-10-08 11:16:46 +0200 | [diff] [blame] | 448 | static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *)) |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 449 | { |
| 450 | conn->owner = owner; |
Willy Tarreau | 436d333 | 2017-10-08 11:16:46 +0200 | [diff] [blame] | 451 | conn->destroy_cb = cb; |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 454 | |
| 455 | /* Mark the connection <conn> as private and remove it from the available connection list */ |
| 456 | static inline void conn_set_private(struct connection *conn) |
| 457 | { |
Christopher Faulet | c6e7563 | 2020-07-02 16:03:30 +0200 | [diff] [blame] | 458 | if (!(conn->flags & CO_FL_PRIVATE)) { |
| 459 | conn->flags |= CO_FL_PRIVATE; |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 460 | |
Christopher Faulet | c6e7563 | 2020-07-02 16:03:30 +0200 | [diff] [blame] | 461 | if (obj_type(conn->target) == OBJ_TYPE_SERVER) |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 462 | srv_release_conn(__objt_server(conn->target), conn); |
Christopher Faulet | c6e7563 | 2020-07-02 16:03:30 +0200 | [diff] [blame] | 463 | } |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 466 | /* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and |
| 467 | * returns it. If <sap> is NULL, the address is always allocated and returned. |
| 468 | * if <sap> is non-null, an address will only be allocated if it points to a |
| 469 | * non-null pointer. In this case the allocated address will be assigned there. |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 470 | * If <orig> is non-null and <len> positive, the address in <sa> will be copied |
| 471 | * into the allocated address. In both situations the new pointer is returned. |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 472 | */ |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 473 | static inline struct sockaddr_storage * |
| 474 | sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len) |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 475 | { |
| 476 | struct sockaddr_storage *sa; |
| 477 | |
| 478 | if (sap && *sap) |
| 479 | return *sap; |
| 480 | |
| 481 | sa = pool_alloc(pool_head_sockaddr); |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 482 | if (sa && orig && len > 0) |
| 483 | memcpy(sa, orig, len); |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 484 | if (sap) |
| 485 | *sap = sa; |
| 486 | return sa; |
| 487 | } |
| 488 | |
| 489 | /* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It |
| 490 | * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed |
| 491 | * there. |
| 492 | */ |
| 493 | static inline void sockaddr_free(struct sockaddr_storage **sap) |
| 494 | { |
| 495 | if (!sap) |
| 496 | return; |
| 497 | pool_free(pool_head_sockaddr, *sap); |
| 498 | *sap = NULL; |
| 499 | } |
| 500 | |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 501 | /* Tries to allocate a new connection and initialized its main fields. The |
| 502 | * connection is returned on success, NULL on failure. The connection must |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 503 | * be released using pool_free() or conn_free(). |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 504 | */ |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 505 | static inline struct connection *conn_new(void *target) |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 506 | { |
| 507 | struct connection *conn; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 508 | struct conn_hash_node *hash_node; |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 509 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 510 | conn = pool_alloc(pool_head_connection); |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 511 | if (unlikely(!conn)) |
| 512 | return NULL; |
| 513 | |
| 514 | conn_init(conn, target); |
| 515 | |
Amaury Denoyelle | 8ede3db | 2021-03-02 14:38:53 +0100 | [diff] [blame] | 516 | if (conn_is_back(conn)) { |
| 517 | if (obj_type(target) == OBJ_TYPE_SERVER) |
| 518 | srv_use_conn(__objt_server(target), conn); |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 519 | |
| 520 | hash_node = conn_alloc_hash_node(conn); |
| 521 | if (unlikely(!hash_node)) { |
| 522 | pool_free(pool_head_connection, conn); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | conn->hash_node = hash_node; |
Christopher Faulet | 3d52f0f | 2020-07-02 15:45:56 +0200 | [diff] [blame] | 527 | } |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 528 | |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 529 | return conn; |
| 530 | } |
| 531 | |
Willy Tarreau | 5e1cc5e | 2018-03-02 10:43:58 +0100 | [diff] [blame] | 532 | /* Releases a conn_stream previously allocated by cs_new(), as well as any |
| 533 | * buffer it would still hold. |
| 534 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 535 | static inline void cs_free(struct conn_stream *cs) |
| 536 | { |
Willy Tarreau | 5e1cc5e | 2018-03-02 10:43:58 +0100 | [diff] [blame] | 537 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 538 | pool_free(pool_head_connstream, cs); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* Tries to allocate a new conn_stream and initialize its main fields. If |
| 542 | * <conn> is NULL, then a new connection is allocated on the fly, initialized, |
| 543 | * and assigned to cs->conn ; this connection will then have to be released |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 544 | * using pool_free() or conn_free(). The conn_stream is initialized and added |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 545 | * to the mux's stream list on success, then returned. On failure, nothing is |
| 546 | * allocated and NULL is returned. |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 547 | */ |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 548 | static inline struct conn_stream *cs_new(struct connection *conn, void *target) |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 549 | { |
| 550 | struct conn_stream *cs; |
| 551 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 552 | cs = pool_alloc(pool_head_connstream); |
Tim Duesterhus | 927063b | 2020-02-21 13:02:03 +0100 | [diff] [blame] | 553 | if (unlikely(!cs)) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 554 | return NULL; |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 555 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 556 | if (!conn) { |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 557 | conn = conn_new(target); |
Tim Duesterhus | 927063b | 2020-02-21 13:02:03 +0100 | [diff] [blame] | 558 | if (unlikely(!conn)) { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 559 | cs_free(cs); |
| 560 | return NULL; |
| 561 | } |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | cs_init(cs, conn); |
| 565 | return cs; |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 568 | /* Retrieves any valid conn_stream from this connection, preferably the first |
| 569 | * valid one. The purpose is to be able to figure one other end of a private |
| 570 | * connection for purposes like source binding or proxy protocol header |
| 571 | * emission. In such cases, any conn_stream is expected to be valid so the |
| 572 | * mux is encouraged to return the first one it finds. If the connection has |
| 573 | * no mux or the mux has no get_first_cs() method or the mux has no valid |
| 574 | * conn_stream, NULL is returned. The output pointer is purposely marked |
| 575 | * const to discourage the caller from modifying anything there. |
| 576 | */ |
| 577 | static inline const struct conn_stream *cs_get_first(const struct connection *conn) |
| 578 | { |
| 579 | if (!conn || !conn->mux || !conn->mux->get_first_cs) |
| 580 | return NULL; |
| 581 | return conn->mux->get_first_cs(conn); |
| 582 | } |
| 583 | |
Olivier Houchard | 1fddc9b | 2018-10-21 00:32:01 +0200 | [diff] [blame] | 584 | static inline void conn_force_unsubscribe(struct connection *conn) |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 585 | { |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 586 | if (!conn->subs) |
| 587 | return; |
| 588 | conn->subs->events = 0; |
| 589 | conn->subs = NULL; |
Olivier Houchard | 1fddc9b | 2018-10-21 00:32:01 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* Releases a connection previously allocated by conn_new() */ |
| 593 | static inline void conn_free(struct connection *conn) |
| 594 | { |
Amaury Denoyelle | 04a24c5 | 2020-10-14 18:17:07 +0200 | [diff] [blame] | 595 | /* If the connection is owned by the session, remove it from its list |
| 596 | */ |
Amaury Denoyelle | 99cca08 | 2021-05-03 14:28:30 +0200 | [diff] [blame] | 597 | if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) { |
Amaury Denoyelle | 04a24c5 | 2020-10-14 18:17:07 +0200 | [diff] [blame] | 598 | session_unown_conn(conn->owner, conn); |
Christopher Faulet | c6e7563 | 2020-07-02 16:03:30 +0200 | [diff] [blame] | 599 | } |
Christopher Faulet | 96ddc8a | 2020-10-19 16:49:29 +0200 | [diff] [blame] | 600 | else if (!(conn->flags & CO_FL_PRIVATE)) { |
Christopher Faulet | c6e7563 | 2020-07-02 16:03:30 +0200 | [diff] [blame] | 601 | if (obj_type(conn->target) == OBJ_TYPE_SERVER) |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 602 | srv_release_conn(__objt_server(conn->target), conn); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 603 | } |
Willy Tarreau | 94031d3 | 2018-12-19 14:36:29 +0100 | [diff] [blame] | 604 | |
Amaury Denoyelle | aba5073 | 2021-02-16 15:16:17 +0100 | [diff] [blame] | 605 | /* Remove the conn from toremove_list. |
| 606 | * |
| 607 | * This is needed to prevent a double-free in case the connection was |
| 608 | * already scheduled from cleaning but is freed before via another |
| 609 | * call. |
| 610 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 611 | MT_LIST_DELETE(&conn->toremove_list); |
Amaury Denoyelle | aba5073 | 2021-02-16 15:16:17 +0100 | [diff] [blame] | 612 | |
Willy Tarreau | e71fca8 | 2019-07-17 19:06:58 +0200 | [diff] [blame] | 613 | sockaddr_free(&conn->src); |
| 614 | sockaddr_free(&conn->dst); |
| 615 | |
Tim Duesterhus | 615f81e | 2021-03-06 20:06:50 +0100 | [diff] [blame] | 616 | pool_free(pool_head_authority, istptr(conn->proxy_authority)); |
| 617 | conn->proxy_authority = IST_NULL; |
Tim Duesterhus | e004c2b | 2021-03-06 20:06:48 +0100 | [diff] [blame] | 618 | |
Tim Duesterhus | 002bd77 | 2021-03-06 20:06:49 +0100 | [diff] [blame] | 619 | pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id)); |
Tim Duesterhus | e004c2b | 2021-03-06 20:06:48 +0100 | [diff] [blame] | 620 | conn->proxy_unique_id = IST_NULL; |
| 621 | |
| 622 | pool_free(pool_head_conn_hash_node, conn->hash_node); |
| 623 | conn->hash_node = NULL; |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 624 | |
Willy Tarreau | 94031d3 | 2018-12-19 14:36:29 +0100 | [diff] [blame] | 625 | /* By convention we always place a NULL where the ctx points to if the |
| 626 | * mux is null. It may have been used to store the connection as a |
| 627 | * stream_interface's end point for example. |
Olivier Houchard | 201b9f4 | 2018-11-21 00:16:29 +0100 | [diff] [blame] | 628 | */ |
Willy Tarreau | 94031d3 | 2018-12-19 14:36:29 +0100 | [diff] [blame] | 629 | if (conn->ctx != NULL && conn->mux == NULL) |
| 630 | *(void **)conn->ctx = NULL; |
Olivier Houchard | 201b9f4 | 2018-11-21 00:16:29 +0100 | [diff] [blame] | 631 | |
Olivier Houchard | 1fddc9b | 2018-10-21 00:32:01 +0200 | [diff] [blame] | 632 | conn_force_unsubscribe(conn); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 633 | pool_free(pool_head_connection, conn); |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 636 | /* Release a conn_stream */ |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 637 | static inline void cs_destroy(struct conn_stream *cs) |
| 638 | { |
Olivier Houchard | 302f9ef | 2018-04-13 15:50:27 +0200 | [diff] [blame] | 639 | if (cs->conn->mux) |
| 640 | cs->conn->mux->detach(cs); |
| 641 | else { |
| 642 | /* It's too early to have a mux, let's just destroy |
| 643 | * the connection |
| 644 | */ |
| 645 | struct connection *conn = cs->conn; |
| 646 | |
| 647 | conn_stop_tracking(conn); |
| 648 | conn_full_close(conn); |
| 649 | if (conn->destroy_cb) |
| 650 | conn->destroy_cb(conn); |
| 651 | conn_free(conn); |
| 652 | } |
Willy Tarreau | 63dd75d | 2017-10-08 15:16:00 +0200 | [diff] [blame] | 653 | cs_free(cs); |
| 654 | } |
| 655 | |
Olivier Houchard | e2b40b9 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 656 | /* Returns the conn from a cs. If cs is NULL, returns NULL */ |
| 657 | static inline struct connection *cs_conn(const struct conn_stream *cs) |
| 658 | { |
| 659 | return cs ? cs->conn : NULL; |
| 660 | } |
Willy Tarreau | b97f3b1 | 2013-10-20 22:56:45 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | 2e34c11 | 2019-07-17 10:48:33 +0200 | [diff] [blame] | 662 | /* Retrieves the connection's original source address. Returns non-zero on |
| 663 | * success or zero on failure. The operation is only performed once and the |
| 664 | * address is stored in the connection for future use. |
| 665 | */ |
| 666 | static inline int conn_get_src(struct connection *conn) |
| 667 | { |
| 668 | if (conn->flags & CO_FL_ADDR_FROM_SET) |
| 669 | return 1; |
| 670 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 671 | if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_src) |
Willy Tarreau | 2e34c11 | 2019-07-17 10:48:33 +0200 | [diff] [blame] | 672 | return 0; |
| 673 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 674 | if (!sockaddr_alloc(&conn->src, NULL, 0)) |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 675 | return 0; |
| 676 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 677 | if (conn->ctrl->fam->get_src(conn->handle.fd, (struct sockaddr *)conn->src, |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 678 | sizeof(*conn->src), |
Willy Tarreau | 2e34c11 | 2019-07-17 10:48:33 +0200 | [diff] [blame] | 679 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
| 680 | return 0; |
| 681 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | /* Retrieves the connection's original destination address. Returns non-zero on |
| 686 | * success or zero on failure. The operation is only performed once and the |
| 687 | * address is stored in the connection for future use. |
| 688 | */ |
| 689 | static inline int conn_get_dst(struct connection *conn) |
| 690 | { |
| 691 | if (conn->flags & CO_FL_ADDR_TO_SET) |
| 692 | return 1; |
| 693 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 694 | if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_dst) |
Willy Tarreau | 2e34c11 | 2019-07-17 10:48:33 +0200 | [diff] [blame] | 695 | return 0; |
| 696 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 697 | if (!sockaddr_alloc(&conn->dst, NULL, 0)) |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 698 | return 0; |
| 699 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 700 | if (conn->ctrl->fam->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst, |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 701 | sizeof(*conn->dst), |
Willy Tarreau | 2e34c11 | 2019-07-17 10:48:33 +0200 | [diff] [blame] | 702 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
| 703 | return 0; |
| 704 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 705 | return 1; |
| 706 | } |
| 707 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 708 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 709 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). The connection is tested and if |
| 710 | * it is null, nothing is done. |
| 711 | */ |
| 712 | static inline void conn_set_tos(const struct connection *conn, int tos) |
| 713 | { |
| 714 | if (!conn || !conn_ctrl_ready(conn)) |
| 715 | return; |
| 716 | |
| 717 | #ifdef IP_TOS |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 718 | if (conn->src->ss_family == AF_INET) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 719 | setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 720 | #endif |
| 721 | #ifdef IPV6_TCLASS |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 722 | if (conn->src->ss_family == AF_INET6) { |
| 723 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)conn->src)->sin6_addr)) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 724 | /* v4-mapped addresses need IP_TOS */ |
| 725 | setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 726 | else |
| 727 | setsockopt(conn->handle.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 728 | } |
| 729 | #endif |
| 730 | } |
| 731 | |
| 732 | /* Sets the netfilter mark on the connection's socket. The connection is tested |
| 733 | * and if it is null, nothing is done. |
| 734 | */ |
| 735 | static inline void conn_set_mark(const struct connection *conn, int mark) |
| 736 | { |
| 737 | if (!conn || !conn_ctrl_ready(conn)) |
| 738 | return; |
| 739 | |
| 740 | #ifdef SO_MARK |
| 741 | setsockopt(conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
| 742 | #endif |
| 743 | } |
| 744 | |
| 745 | /* Sets adjust the TCP quick-ack feature on the connection's socket. The |
| 746 | * connection is tested and if it is null, nothing is done. |
| 747 | */ |
| 748 | static inline void conn_set_quickack(const struct connection *conn, int value) |
| 749 | { |
| 750 | if (!conn || !conn_ctrl_ready(conn)) |
| 751 | return; |
| 752 | |
| 753 | #ifdef TCP_QUICKACK |
| 754 | setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &value, sizeof(value)); |
| 755 | #endif |
| 756 | } |
| 757 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 758 | /* Attaches a conn_stream to a data layer and sets the relevant callbacks */ |
| 759 | static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb) |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 760 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 761 | cs->data_cb = data_cb; |
| 762 | cs->data = data; |
Willy Tarreau | bd99aab | 2012-10-02 20:57:19 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 765 | static inline struct wait_event *wl_set_waitcb(struct wait_event *wl, struct task *(*cb)(struct task *, void *, unsigned int), void *ctx) |
Willy Tarreau | 8318885 | 2018-07-18 08:18:20 +0200 | [diff] [blame] | 766 | { |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 767 | if (!wl->tasklet->process) { |
| 768 | wl->tasklet->process = cb; |
| 769 | wl->tasklet->context = ctx; |
Willy Tarreau | 8318885 | 2018-07-18 08:18:20 +0200 | [diff] [blame] | 770 | } |
| 771 | return wl; |
| 772 | } |
| 773 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 774 | /* Installs the connection's mux layer for upper context <ctx>. |
| 775 | * Returns < 0 on error. |
| 776 | */ |
Willy Tarreau | 175a2bb | 2018-09-12 12:02:05 +0200 | [diff] [blame] | 777 | static inline int conn_install_mux(struct connection *conn, const struct mux_ops *mux, |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 778 | void *ctx, struct proxy *prx, struct session *sess) |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 779 | { |
Willy Tarreau | 762475e | 2019-01-10 10:33:32 +0100 | [diff] [blame] | 780 | int ret; |
| 781 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 782 | conn->mux = mux; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 783 | conn->ctx = ctx; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 784 | ret = mux->init ? mux->init(conn, prx, sess, &BUF_NULL) : 0; |
Willy Tarreau | 762475e | 2019-01-10 10:33:32 +0100 | [diff] [blame] | 785 | if (ret < 0) { |
| 786 | conn->mux = NULL; |
| 787 | conn->ctx = NULL; |
| 788 | } |
| 789 | return ret; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 792 | /* returns a human-readable error code for conn->err_code, or NULL if the code |
| 793 | * is unknown. |
| 794 | */ |
| 795 | static inline const char *conn_err_code_str(struct connection *c) |
| 796 | { |
| 797 | switch (c->err_code) { |
| 798 | case CO_ER_NONE: return "Success"; |
Willy Tarreau | 45b34e8 | 2014-01-24 16:06:50 +0100 | [diff] [blame] | 799 | |
| 800 | case CO_ER_CONF_FDLIM: return "Reached configured maxconn value"; |
| 801 | case CO_ER_PROC_FDLIM: return "Too many sockets on the process"; |
| 802 | case CO_ER_SYS_FDLIM: return "Too many sockets on the system"; |
| 803 | case CO_ER_SYS_MEMLIM: return "Out of system buffers"; |
| 804 | case CO_ER_NOPROTO: return "Protocol or address family not supported"; |
| 805 | case CO_ER_SOCK_ERR: return "General socket error"; |
| 806 | case CO_ER_PORT_RANGE: return "Source port range exhausted"; |
| 807 | case CO_ER_CANT_BIND: return "Can't bind to source address"; |
| 808 | case CO_ER_FREE_PORTS: return "Out of local source ports on the system"; |
| 809 | case CO_ER_ADDR_INUSE: return "Local source address already in use"; |
| 810 | |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 811 | case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header"; |
| 812 | case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 813 | case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header"; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 814 | case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received"; |
| 815 | case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header"; |
| 816 | case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header"; |
| 817 | case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header"; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 818 | |
| 819 | case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header"; |
| 820 | case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header"; |
Remi Tricot-Le Breton | abc2d3c | 2021-07-29 09:45:48 +0200 | [diff] [blame] | 821 | case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header"; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 822 | case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received"; |
| 823 | case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number"; |
| 824 | case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header"; |
| 825 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 826 | case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake"; |
| 827 | case CO_ER_SSL_ABORT: return "Connection error during SSL handshake"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 828 | case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake"; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 829 | case CO_ER_SSL_TOO_MANY: return "Too many SSL connections"; |
| 830 | case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection"; |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 831 | case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt"; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 832 | case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified"; |
| 833 | case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted"; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 834 | case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one"; |
| 835 | case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one"; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 836 | case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure"; |
Willy Tarreau | b396637 | 2014-04-25 18:54:29 +0200 | [diff] [blame] | 837 | case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat"; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 838 | case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)"; |
Willy Tarreau | 830bf61 | 2013-12-01 20:29:58 +0100 | [diff] [blame] | 839 | case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)"; |
Remi Tricot-Le Breton | abc2d3c | 2021-07-29 09:45:48 +0200 | [diff] [blame] | 840 | case CO_ER_SSL_EARLY_FAILED: return "Server refused early data"; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 841 | |
| 842 | case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake"; |
| 843 | case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake"; |
| 844 | case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request"; |
| 845 | case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 846 | } |
| 847 | return NULL; |
| 848 | } |
| 849 | |
Amaury Denoyelle | 9262101 | 2021-10-18 14:32:36 +0200 | [diff] [blame] | 850 | int conn_update_alpn(struct connection *conn, const struct ist alpn, int force); |
| 851 | |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 852 | static inline const char *conn_get_ctrl_name(const struct connection *conn) |
| 853 | { |
Willy Tarreau | 5e6a5b3 | 2019-04-25 18:35:49 +0200 | [diff] [blame] | 854 | if (!conn || !conn_ctrl_ready(conn)) |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 855 | return "NONE"; |
| 856 | return conn->ctrl->name; |
| 857 | } |
| 858 | |
| 859 | static inline const char *conn_get_xprt_name(const struct connection *conn) |
| 860 | { |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 861 | if (!conn || !conn->xprt) |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 862 | return "NONE"; |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 863 | return conn->xprt->name; |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 866 | static inline const char *conn_get_mux_name(const struct connection *conn) |
| 867 | { |
Willy Tarreau | 5e6a5b3 | 2019-04-25 18:35:49 +0200 | [diff] [blame] | 868 | if (!conn || !conn->mux) |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 869 | return "NONE"; |
| 870 | return conn->mux->name; |
| 871 | } |
| 872 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 873 | static inline const char *cs_get_data_name(const struct conn_stream *cs) |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 874 | { |
Willy Tarreau | 5e6a5b3 | 2019-04-25 18:35:49 +0200 | [diff] [blame] | 875 | if (!cs || !cs->data_cb) |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 876 | return "NONE"; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 877 | return cs->data_cb->name; |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 878 | } |
| 879 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 880 | /* registers pointer to transport layer <id> (XPRT_*) */ |
| 881 | static inline void xprt_register(int id, struct xprt_ops *xprt) |
| 882 | { |
| 883 | if (id >= XPRT_ENTRIES) |
| 884 | return; |
| 885 | registered_xprt[id] = xprt; |
| 886 | } |
| 887 | |
| 888 | /* returns pointer to transport layer <id> (XPRT_*) or NULL if not registered */ |
| 889 | static inline struct xprt_ops *xprt_get(int id) |
| 890 | { |
| 891 | if (id >= XPRT_ENTRIES) |
| 892 | return NULL; |
| 893 | return registered_xprt[id]; |
| 894 | } |
Willy Tarreau | 2dc770c | 2016-11-23 18:00:08 +0100 | [diff] [blame] | 895 | |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 896 | /* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is |
| 897 | * raw_sock, then just use the new XPRT as the connection XPRT, otherwise |
| 898 | * call the xprt's add_xprt() method. |
| 899 | * Returns 0 on success, or non-zero on failure. |
| 900 | */ |
| 901 | static inline int xprt_add_hs(struct connection *conn) |
| 902 | { |
| 903 | void *xprt_ctx = NULL; |
| 904 | const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE); |
| 905 | void *nextxprt_ctx = NULL; |
| 906 | const struct xprt_ops *nextxprt_ops = NULL; |
| 907 | |
| 908 | if (conn->flags & CO_FL_ERROR) |
| 909 | return -1; |
| 910 | if (ops->init(conn, &xprt_ctx) < 0) |
| 911 | return -1; |
| 912 | if (conn->xprt == xprt_get(XPRT_RAW)) { |
| 913 | nextxprt_ctx = conn->xprt_ctx; |
| 914 | nextxprt_ops = conn->xprt; |
| 915 | conn->xprt_ctx = xprt_ctx; |
| 916 | conn->xprt = ops; |
| 917 | } else { |
| 918 | if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops, |
| 919 | &nextxprt_ctx, &nextxprt_ops) != 0) { |
| 920 | ops->close(conn, xprt_ctx); |
| 921 | return -1; |
| 922 | } |
| 923 | } |
| 924 | if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) { |
| 925 | ops->close(conn, xprt_ctx); |
| 926 | return -1; |
| 927 | } |
| 928 | return 0; |
| 929 | } |
| 930 | |
Willy Tarreau | 4f8cd43 | 2021-03-02 17:27:58 +0100 | [diff] [blame] | 931 | /* notify the next xprt that the connection is about to become idle and that it |
| 932 | * may be stolen at any time after the function returns and that any tasklet in |
| 933 | * the chain must be careful before dereferencing its context. |
| 934 | */ |
| 935 | static inline void xprt_set_idle(struct connection *conn, const struct xprt_ops *xprt, void *xprt_ctx) |
| 936 | { |
| 937 | if (xprt->set_idle) |
| 938 | xprt->set_idle(conn, conn->xprt_ctx); |
| 939 | } |
| 940 | |
| 941 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 942 | * not be stolen before the next xprt_set_idle(). |
| 943 | */ |
| 944 | static inline void xprt_set_used(struct connection *conn, const struct xprt_ops *xprt, void *xprt_ctx) |
| 945 | { |
| 946 | if (xprt->set_used) |
| 947 | xprt->set_used(conn, conn->xprt_ctx); |
| 948 | } |
| 949 | |
Willy Tarreau | a9c1741 | 2016-12-04 18:42:09 +0100 | [diff] [blame] | 950 | static inline int conn_get_alpn(const struct connection *conn, const char **str, int *len) |
| 951 | { |
| 952 | if (!conn_xprt_ready(conn) || !conn->xprt->get_alpn) |
| 953 | return 0; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 954 | return conn->xprt->get_alpn(conn, conn->xprt_ctx, str, len); |
Willy Tarreau | a9c1741 | 2016-12-04 18:42:09 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 957 | /* registers proto mux list <list>. Modifies the list element! */ |
| 958 | static inline void register_mux_proto(struct mux_proto_list *list) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 959 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 960 | LIST_APPEND(&mux_proto_list.list, &list->list); |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 963 | /* unregisters proto mux list <list> */ |
| 964 | static inline void unregister_mux_proto(struct mux_proto_list *list) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 965 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 966 | LIST_DELETE(&list->list); |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 967 | LIST_INIT(&list->list); |
| 968 | } |
| 969 | |
Christopher Faulet | e15c6c4 | 2018-08-08 10:21:56 +0200 | [diff] [blame] | 970 | static inline struct mux_proto_list *get_mux_proto(const struct ist proto) |
Christopher Faulet | 98d9fe2 | 2018-04-10 14:37:32 +0200 | [diff] [blame] | 971 | { |
| 972 | struct mux_proto_list *item; |
Christopher Faulet | 98d9fe2 | 2018-04-10 14:37:32 +0200 | [diff] [blame] | 973 | |
| 974 | list_for_each_entry(item, &mux_proto_list.list, list) { |
| 975 | if (isteq(proto, item->token)) |
| 976 | return item; |
| 977 | } |
| 978 | return NULL; |
| 979 | } |
| 980 | |
Willy Tarreau | e59b516 | 2021-05-08 14:06:09 +0200 | [diff] [blame] | 981 | void list_mux_proto(FILE *out); |
Willy Tarreau | 5fc311c | 2018-12-02 13:04:43 +0100 | [diff] [blame] | 982 | /* returns the first mux entry in the list matching the exact same <mux_proto> |
| 983 | * and compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 984 | * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux |
| 985 | * with exactly the same <proto_mode> or with an empty name. May return |
| 986 | * null if the code improperly registered the default mux to use as a fallback. |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 987 | */ |
Willy Tarreau | 5fc311c | 2018-12-02 13:04:43 +0100 | [diff] [blame] | 988 | static inline const struct mux_proto_list *conn_get_best_mux_entry( |
| 989 | const struct ist mux_proto, |
| 990 | int proto_side, int proto_mode) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 991 | { |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 992 | struct mux_proto_list *item; |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 993 | struct mux_proto_list *fallback = NULL; |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 994 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 995 | list_for_each_entry(item, &mux_proto_list.list, list) { |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 996 | if (!(item->side & proto_side) || !(item->mode & proto_mode)) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 997 | continue; |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 998 | if (istlen(mux_proto) && isteq(mux_proto, item->token)) |
Willy Tarreau | 5fc311c | 2018-12-02 13:04:43 +0100 | [diff] [blame] | 999 | return item; |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1000 | else if (!istlen(item->token)) { |
| 1001 | if (!fallback || (item->mode == proto_mode && fallback->mode != proto_mode)) |
| 1002 | fallback = item; |
| 1003 | } |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1004 | } |
Willy Tarreau | 5fc311c | 2018-12-02 13:04:43 +0100 | [diff] [blame] | 1005 | return fallback; |
| 1006 | |
| 1007 | } |
| 1008 | |
| 1009 | /* returns the first mux in the list matching the exact same <mux_proto> and |
| 1010 | * compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or |
| 1011 | * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux |
| 1012 | * with exactly the same <proto_mode> or with an empty name. May return |
| 1013 | * null if the code improperly registered the default mux to use as a fallback. |
| 1014 | */ |
| 1015 | static inline const struct mux_ops *conn_get_best_mux(struct connection *conn, |
| 1016 | const struct ist mux_proto, |
| 1017 | int proto_side, int proto_mode) |
| 1018 | { |
| 1019 | const struct mux_proto_list *item; |
| 1020 | |
| 1021 | item = conn_get_best_mux_entry(mux_proto, proto_side, proto_mode); |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1022 | |
Willy Tarreau | 5fc311c | 2018-12-02 13:04:43 +0100 | [diff] [blame] | 1023 | return item ? item->mux : NULL; |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Willy Tarreau | 6ac98ac | 2018-09-06 11:48:44 +0200 | [diff] [blame] | 1026 | /* returns a pointer to the proxy associated with this connection. For a front |
| 1027 | * connection it returns a pointer to the frontend ; for a back connection, it |
| 1028 | * returns a pointer to the backend. |
| 1029 | */ |
| 1030 | static inline struct proxy *conn_get_proxy(const struct connection *conn) |
| 1031 | { |
| 1032 | struct listener *l; |
| 1033 | struct server *s; |
| 1034 | |
| 1035 | /* check if it's a frontend connection */ |
| 1036 | l = objt_listener(conn->target); |
| 1037 | if (l) |
| 1038 | return l->bind_conf->frontend; |
| 1039 | |
| 1040 | /* check if it's a backend connection */ |
| 1041 | s = objt_server(conn->target); |
| 1042 | if (s) |
| 1043 | return s->proxy; |
| 1044 | |
| 1045 | return objt_proxy(conn->target); |
| 1046 | } |
| 1047 | |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1048 | /* installs the best mux for incoming connection <conn> using the upper context |
| 1049 | * <ctx>. If the mux protocol is forced, we use it to find the best |
| 1050 | * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error. |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1051 | */ |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1052 | static inline int conn_install_mux_fe(struct connection *conn, void *ctx) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1053 | { |
Willy Tarreau | 55e0da6 | 2018-09-20 11:26:52 +0200 | [diff] [blame] | 1054 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1055 | const struct mux_ops *mux_ops; |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1056 | |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1057 | if (bind_conf->mux_proto) |
| 1058 | mux_ops = bind_conf->mux_proto->mux; |
| 1059 | else { |
| 1060 | struct ist mux_proto; |
| 1061 | const char *alpn_str = NULL; |
| 1062 | int alpn_len = 0; |
Willy Tarreau | 68ad3a4 | 2018-10-22 11:49:15 +0200 | [diff] [blame] | 1063 | int mode; |
| 1064 | |
Christopher Faulet | c1918d1 | 2019-04-24 15:01:22 +0200 | [diff] [blame] | 1065 | if (bind_conf->frontend->mode == PR_MODE_HTTP) |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 1066 | mode = PROTO_MODE_HTTP; |
Willy Tarreau | 68ad3a4 | 2018-10-22 11:49:15 +0200 | [diff] [blame] | 1067 | else |
Christopher Faulet | c1918d1 | 2019-04-24 15:01:22 +0200 | [diff] [blame] | 1068 | mode = PROTO_MODE_TCP; |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1069 | |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1070 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 1071 | mux_proto = ist2(alpn_str, alpn_len); |
| 1072 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode); |
| 1073 | if (!mux_ops) |
| 1074 | return -1; |
| 1075 | } |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1076 | return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner); |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1079 | /* installs the best mux for outgoing connection <conn> using the upper context |
| 1080 | * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns |
Willy Tarreau | 2e0b2b5 | 2017-09-15 06:59:55 +0200 | [diff] [blame] | 1081 | * < 0 on error. |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1082 | */ |
Amaury Denoyelle | 6037887 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 1083 | static inline int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess, |
| 1084 | const struct mux_ops *force_mux_ops) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1085 | { |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1086 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 287527a | 2018-08-08 18:40:44 +0200 | [diff] [blame] | 1087 | struct proxy *prx = objt_proxy(conn->target); |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1088 | const struct mux_ops *mux_ops; |
| 1089 | |
Willy Tarreau | 287527a | 2018-08-08 18:40:44 +0200 | [diff] [blame] | 1090 | if (srv) |
| 1091 | prx = srv->proxy; |
| 1092 | |
| 1093 | if (!prx) // target must be either proxy or server |
| 1094 | return -1; |
| 1095 | |
Amaury Denoyelle | 6037887 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 1096 | if (srv && srv->mux_proto && likely(!force_mux_ops)) { |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1097 | mux_ops = srv->mux_proto->mux; |
Amaury Denoyelle | 6037887 | 2021-10-28 16:36:11 +0200 | [diff] [blame] | 1098 | } |
| 1099 | else if (srv && unlikely(force_mux_ops)) { |
| 1100 | mux_ops = force_mux_ops; |
| 1101 | } |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1102 | else { |
Olivier Houchard | 201b9f4 | 2018-11-21 00:16:29 +0100 | [diff] [blame] | 1103 | struct ist mux_proto; |
| 1104 | const char *alpn_str = NULL; |
| 1105 | int alpn_len = 0; |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1106 | int mode; |
| 1107 | |
Christopher Faulet | c1918d1 | 2019-04-24 15:01:22 +0200 | [diff] [blame] | 1108 | if (prx->mode == PR_MODE_HTTP) |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 1109 | mode = PROTO_MODE_HTTP; |
Willy Tarreau | 68ad3a4 | 2018-10-22 11:49:15 +0200 | [diff] [blame] | 1110 | else |
Christopher Faulet | c1918d1 | 2019-04-24 15:01:22 +0200 | [diff] [blame] | 1111 | mode = PROTO_MODE_TCP; |
Willy Tarreau | 68ad3a4 | 2018-10-22 11:49:15 +0200 | [diff] [blame] | 1112 | |
Olivier Houchard | 201b9f4 | 2018-11-21 00:16:29 +0100 | [diff] [blame] | 1113 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 1114 | mux_proto = ist2(alpn_str, alpn_len); |
| 1115 | |
| 1116 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode); |
Christopher Faulet | 7ce0c89 | 2018-04-10 15:01:45 +0200 | [diff] [blame] | 1117 | if (!mux_ops) |
| 1118 | return -1; |
| 1119 | } |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1120 | return conn_install_mux(conn, mux_ops, ctx, prx, sess); |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Christopher Faulet | a9e1c4c | 2020-04-21 12:18:05 +0200 | [diff] [blame] | 1123 | /* installs the best mux for outgoing connection <conn> for a check using the |
| 1124 | * upper context <ctx>. If the mux protocol is forced by the check, we use it to |
| 1125 | * find the best mux. Returns < 0 on error. |
| 1126 | */ |
| 1127 | static inline int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess) |
| 1128 | { |
| 1129 | struct check *check = objt_check(sess->origin); |
| 1130 | struct server *srv = objt_server(conn->target); |
| 1131 | struct proxy *prx = objt_proxy(conn->target); |
| 1132 | const struct mux_ops *mux_ops; |
| 1133 | |
| 1134 | if (!check) // Check must be defined |
| 1135 | return -1; |
| 1136 | |
| 1137 | if (srv) |
| 1138 | prx = srv->proxy; |
| 1139 | |
| 1140 | if (!prx) // target must be either proxy or server |
| 1141 | return -1; |
| 1142 | |
| 1143 | if (check->mux_proto) |
| 1144 | mux_ops = check->mux_proto->mux; |
| 1145 | else { |
| 1146 | struct ist mux_proto; |
| 1147 | const char *alpn_str = NULL; |
| 1148 | int alpn_len = 0; |
| 1149 | int mode; |
| 1150 | |
| 1151 | if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) |
| 1152 | mode = PROTO_MODE_HTTP; |
| 1153 | else |
| 1154 | mode = PROTO_MODE_TCP; |
| 1155 | |
| 1156 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 1157 | mux_proto = ist2(alpn_str, alpn_len); |
| 1158 | |
| 1159 | mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode); |
| 1160 | if (!mux_ops) |
| 1161 | return -1; |
| 1162 | } |
| 1163 | return conn_install_mux(conn, mux_ops, ctx, prx, sess); |
| 1164 | } |
| 1165 | |
Olivier Houchard | 2ab3dad | 2019-07-03 13:08:18 +0200 | [diff] [blame] | 1166 | /* Change the mux for the connection. |
| 1167 | * The caller should make sure he's not subscribed to the underlying XPRT. |
| 1168 | */ |
Christopher Faulet | c0016d8 | 2019-04-08 10:42:41 +0200 | [diff] [blame] | 1169 | static inline int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf, |
| 1170 | struct ist mux_proto, int mode) |
| 1171 | { |
| 1172 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
| 1173 | const struct mux_ops *old_mux, *new_mux; |
| 1174 | void *old_mux_ctx; |
| 1175 | const char *alpn_str = NULL; |
| 1176 | int alpn_len = 0; |
| 1177 | |
| 1178 | if (!mux_proto.len) { |
| 1179 | conn_get_alpn(conn, &alpn_str, &alpn_len); |
| 1180 | mux_proto = ist2(alpn_str, alpn_len); |
| 1181 | } |
| 1182 | new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode); |
| 1183 | old_mux = conn->mux; |
| 1184 | |
| 1185 | /* No mux found */ |
| 1186 | if (!new_mux) |
| 1187 | return -1; |
| 1188 | |
| 1189 | /* Same mux, nothing to do */ |
| 1190 | if (old_mux == new_mux) |
| 1191 | return 0; |
| 1192 | |
| 1193 | old_mux_ctx = conn->ctx; |
| 1194 | conn->mux = new_mux; |
| 1195 | conn->ctx = ctx; |
Christopher Faulet | c0016d8 | 2019-04-08 10:42:41 +0200 | [diff] [blame] | 1196 | if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) { |
| 1197 | /* The mux upgrade failed, so restore the old mux */ |
| 1198 | conn->ctx = old_mux_ctx; |
| 1199 | conn->mux = old_mux; |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | /* The mux was upgraded, destroy the old one */ |
| 1204 | *buf = BUF_NULL; |
| 1205 | old_mux->destroy(old_mux_ctx); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Amaury Denoyelle | 81c6f76 | 2021-01-18 14:57:50 +0100 | [diff] [blame] | 1209 | /* Generate the hash of a connection with params as input |
| 1210 | * Each non-null field of params is taken into account for the hash calcul. |
| 1211 | */ |
| 1212 | XXH64_hash_t conn_calculate_hash(const struct conn_hash_params *params); |
| 1213 | |
| 1214 | static inline XXH64_hash_t conn_hash_prehash(char *buf, size_t size) |
| 1215 | { |
| 1216 | return XXH64(buf, size, 0); |
| 1217 | } |
| 1218 | |
| 1219 | /* Append <data> into <buf> at <idx> offset in preparation for connection hash |
| 1220 | * calcul. <idx> is incremented beyond data <size>. In the same time, <flags> |
| 1221 | * are updated with <type> for the hash header. |
| 1222 | */ |
| 1223 | static inline void conn_hash_update(char *buf, size_t *idx, |
| 1224 | const void *data, size_t size, |
| 1225 | enum conn_hash_params_t *flags, |
| 1226 | enum conn_hash_params_t type) |
| 1227 | { |
| 1228 | memcpy(&buf[*idx], data, size); |
| 1229 | *idx += size; |
| 1230 | *flags |= type; |
| 1231 | } |
| 1232 | |
| 1233 | static inline XXH64_hash_t conn_hash_digest(char *buf, size_t bufsize, |
| 1234 | enum conn_hash_params_t flags) |
| 1235 | { |
| 1236 | const uint64_t flags_u64 = (uint64_t)flags; |
| 1237 | const XXH64_hash_t hash = XXH64(buf, bufsize, 0); |
| 1238 | |
| 1239 | return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash); |
| 1240 | } |
| 1241 | |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 1242 | #endif /* _HAPROXY_CONNECTION_H */ |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * Local variables: |
| 1246 | * c-indent-level: 8 |
| 1247 | * c-basic-offset: 8 |
| 1248 | * End: |
| 1249 | */ |