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