blob: 364fafae17b94fde150cb6d2fe52ea631ea12109 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
Willy Tarreau7ea393d2020-06-04 18:02:10 +02002 * include/haproxy/connection.h
Willy Tarreau59f98392012-07-06 14:13:49 +02003 * This file contains connection function prototypes
4 *
Willy Tarreau7ea393d2020-06-04 18:02:10 +02005 * Copyright (C) 2000-2002 Willy Tarreau - w@1wt.eu
Willy Tarreau59f98392012-07-06 14:13:49 +02006 *
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 Tarreau7ea393d2020-06-04 18:02:10 +020022#ifndef _HAPROXY_CONNECTION_H
23#define _HAPROXY_CONNECTION_H
Willy Tarreau59f98392012-07-06 14:13:49 +020024
Willy Tarreaueb6f7012020-05-27 16:21:26 +020025#include <import/ist.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020026
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020027#include <haproxy/api.h>
Christopher Fauletbb69d782021-03-22 17:06:24 +010028#include <haproxy/buf.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020029#include <haproxy/connection-t.h>
30#include <haproxy/fd.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/http_ana.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020032#include <haproxy/listener-t.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020033#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020034#include <haproxy/pool.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020035#include <haproxy/session.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020036#include <haproxy/task-t.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020037#include <haproxy/tcpcheck-t.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020038
Willy Tarreau59f98392012-07-06 14:13:49 +020039
Willy Tarreaubafbe012017-11-24 17:34:44 +010040extern struct pool_head *pool_head_connection;
41extern struct pool_head *pool_head_connstream;
Amaury Denoyelle8990b012021-02-19 15:29:16 +010042extern struct pool_head *pool_head_conn_hash_node;
Willy Tarreauff5d57b2019-07-17 18:37:02 +020043extern struct pool_head *pool_head_sockaddr;
Geoff Simmons7185b782019-08-27 18:31:16 +020044extern struct pool_head *pool_head_authority;
Willy Tarreau13e14102016-12-22 20:25:26 +010045extern struct xprt_ops *registered_xprt[XPRT_ENTRIES];
Christopher Faulet32f61c02018-04-10 14:33:41 +020046extern struct mux_proto_list mux_proto_list;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +020047extern struct mux_stopping_data mux_stopping_data[MAX_THREADS];
Willy Tarreauf2943dc2012-10-26 20:10:28 +020048
Christopher Faulete60abd12020-04-16 10:03:58 +020049#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 Tarreau22cda212012-08-31 17:43:29 +020052/* receive a PROXY protocol header over a connection */
53int conn_recv_proxy(struct connection *conn, int flag);
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +010054int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
David Safb76832014-05-08 23:42:08 -040055int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +010056int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
Willy Tarreau22cda212012-08-31 17:43:29 +020057
Willy Tarreaua7d14552021-06-16 17:35:20 +020058int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx);
59
Willy Tarreauee1a6fc2020-01-17 07:52:13 +010060int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
61int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
Olivier Houchard83a0cd82018-09-28 17:57:58 +020062
Bertrand Jacquin93b227d2016-06-04 15:11:10 +010063/* receive a NetScaler Client IP insertion header over a connection */
64int conn_recv_netscaler_cip(struct connection *conn, int flag);
65
Willy Tarreauff3e6482015-03-12 23:56:52 +010066/* raw send() directly on the socket */
Willy Tarreau827fee72020-12-11 15:26:55 +010067int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags);
Willy Tarreauff3e6482015-03-12 23:56:52 +010068
Willy Tarreaud85c4852015-03-13 00:40:28 +010069/* drains any pending bytes from the socket */
Willy Tarreau2ded48d2020-12-11 16:20:34 +010070int conn_ctrl_drain(struct connection *conn);
Willy Tarreaud85c4852015-03-13 00:40:28 +010071
Alexander Liu2a54bb72019-05-22 19:44:48 +080072/* scoks4 proxy handshake */
73int conn_send_socks4_proxy_request(struct connection *conn);
74int conn_recv_socks4_proxy_response(struct connection *conn);
75
Olivier Houchard477902b2020-01-22 18:08:48 +010076/* If we delayed the mux creation because we were waiting for the handshake, do it now */
77int conn_create_mux(struct connection *conn);
78
Willy Tarreau4d82bf52020-06-28 00:19:17 +020079extern struct idle_conns idle_conns[MAX_THREADS];
Olivier Houchard4be71902019-07-11 15:49:00 +020080
Olivier Houchard1b3c9312021-03-05 23:37:48 +010081/* returns true if the transport layer is ready */
Willy Tarreauaad69382014-01-23 14:21:42 +010082static inline int conn_xprt_ready(const struct connection *conn)
Willy Tarreaud02cdd22013-12-15 10:23:20 +010083{
Willy Tarreauaad69382014-01-23 14:21:42 +010084 return (conn->flags & CO_FL_XPRT_READY);
Willy Tarreaud02cdd22013-12-15 10:23:20 +010085}
86
Olivier Houchard1b3c9312021-03-05 23:37:48 +010087/* returns true if the control layer is ready */
Willy Tarreau3c728722014-01-23 13:50:42 +010088static inline int conn_ctrl_ready(const struct connection *conn)
Willy Tarreaud02cdd22013-12-15 10:23:20 +010089{
90 return (conn->flags & CO_FL_CTRL_READY);
91}
92
Olivier Houchard1b3c9312021-03-05 23:37:48 +010093/*
94 * Calls the start() function of the transport layer, if needed.
95 * Returns < 0 in case of error.
96*/
97
98static inline int conn_xprt_start(struct connection *conn)
Willy Tarreau15678ef2012-08-31 13:54:11 +020099{
Willy Tarreauf79c8172013-10-21 16:30:56 +0200100 int ret = 0;
101
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100102 if (!conn_xprt_ready(conn) && conn->xprt && conn->xprt->start)
103 ret = conn->xprt->start(conn, conn->xprt_ctx);
Willy Tarreauf79c8172013-10-21 16:30:56 +0200104
105 if (ret >= 0)
106 conn->flags |= CO_FL_XPRT_READY;
107
108 return ret;
Willy Tarreau15678ef2012-08-31 13:54:11 +0200109}
110
Willy Tarreauf79c8172013-10-21 16:30:56 +0200111/* Calls the close() function of the transport layer if any and if not done
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100112 * 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 Tarreau6c03a642012-10-12 17:00:05 +0200115 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200116static inline void conn_xprt_close(struct connection *conn)
Willy Tarreau8b117082012-08-06 15:06:49 +0200117{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100118 if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) {
Willy Tarreauaad69382014-01-23 14:21:42 +0100119 if (conn->xprt->close)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100120 conn->xprt->close(conn, conn->xprt_ctx);
Olivier Houchard478281f2019-05-13 19:10:46 +0200121 conn->xprt_ctx = NULL;
Willy Tarreauf79c8172013-10-21 16:30:56 +0200122 conn->flags &= ~CO_FL_XPRT_READY;
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100123 conn->xprt = NULL;
Willy Tarreauf79c8172013-10-21 16:30:56 +0200124 }
125}
126
127/* Initializes the connection's control layer which essentially consists in
Willy Tarreauc14e7ae2020-12-08 15:53:45 +0100128 * 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 Tarreauf79c8172013-10-21 16:30:56 +0200132 */
133static inline void conn_ctrl_init(struct connection *conn)
134{
Willy Tarreau3c728722014-01-23 13:50:42 +0100135 if (!conn_ctrl_ready(conn)) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200136 conn->flags |= CO_FL_CTRL_READY;
Willy Tarreauc14e7ae2020-12-08 15:53:45 +0100137 if (conn->ctrl->ctrl_init)
138 conn->ctrl->ctrl_init(conn);
Willy Tarreau6c03a642012-10-12 17:00:05 +0200139 }
Willy Tarreau8b117082012-08-06 15:06:49 +0200140}
141
Willy Tarreauc14e7ae2020-12-08 15:53:45 +0100142/* 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 Tarreauf79c8172013-10-21 16:30:56 +0200144 */
145static inline void conn_ctrl_close(struct connection *conn)
146{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100147 if (!conn->xprt && (conn->flags & CO_FL_CTRL_READY)) {
Willy Tarreaub17952c2021-10-21 21:31:42 +0200148 if ((conn->flags & (CO_FL_WANT_DRAIN | CO_FL_SOCK_RD_SH)) == CO_FL_WANT_DRAIN)
149 conn_ctrl_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +0200150 conn->flags &= ~CO_FL_CTRL_READY;
Willy Tarreauc14e7ae2020-12-08 15:53:45 +0100151 if (conn->ctrl->ctrl_close)
152 conn->ctrl->ctrl_close(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +0200153 }
154}
155
Willy Tarreau2b199c92012-11-23 17:32:21 +0100156/* 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 */
162static inline void conn_full_close(struct connection *conn)
163{
Willy Tarreauf79c8172013-10-21 16:30:56 +0200164 conn_xprt_close(conn);
165 conn_ctrl_close(conn);
166}
167
Willy Tarreaudc42acd2017-10-05 18:09:20 +0200168/* stop tracking a connection, allowing conn_full_close() to always
169 * succeed.
170 */
171static inline void conn_stop_tracking(struct connection *conn)
172{
173 conn->flags &= ~CO_FL_XPRT_TRACKED;
174}
175
Willy Tarreau7b271b22017-10-25 09:59:22 +0200176/* read shutdown, called from the rcv_buf/rcv_pipe handlers when
177 * detecting an end of connection.
178 */
Willy Tarreau3af56a92012-08-20 16:55:48 +0200179static inline void conn_sock_read0(struct connection *c)
180{
181 c->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100182 if (conn_ctrl_ready(c)) {
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100183 /* we don't risk keeping ports unusable if we found the
184 * zero from the other side.
185 */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200186 HA_ATOMIC_AND(&fdtab[c->handle.fd].state, ~FD_LINGER_RISK);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100187 }
Willy Tarreau3af56a92012-08-20 16:55:48 +0200188}
189
Willy Tarreau7b271b22017-10-25 09:59:22 +0200190/* write shutdown, indication that the upper layer is not willing to send
Willy Tarreaua48c1412017-12-22 18:46:33 +0100191 * 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 Tarreau7b271b22017-10-25 09:59:22 +0200194 */
Willy Tarreaua48c1412017-12-22 18:46:33 +0100195static inline void conn_sock_shutw(struct connection *c, int clean)
Willy Tarreau3af56a92012-08-20 16:55:48 +0200196{
197 c->flags |= CO_FL_SOCK_WR_SH;
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100198 if (conn_ctrl_ready(c)) {
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100199 /* 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 Tarreau3af56a92012-08-20 16:55:48 +0200205}
206
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200207static inline void conn_xprt_shutw(struct connection *c)
Willy Tarreau3af56a92012-08-20 16:55:48 +0200208{
Willy Tarreau729c69f2015-03-12 22:51:10 +0100209 /* clean data-layer shutdown */
210 if (c->xprt && c->xprt->shutw)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100211 c->xprt->shutw(c, c->xprt_ctx, 1);
Willy Tarreau729c69f2015-03-12 22:51:10 +0100212}
213
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200214static inline void conn_xprt_shutw_hard(struct connection *c)
Willy Tarreau729c69f2015-03-12 22:51:10 +0100215{
Willy Tarreau729c69f2015-03-12 22:51:10 +0100216 /* unclean data-layer shutdown */
217 if (c->xprt && c->xprt->shutw)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100218 c->xprt->shutw(c, c->xprt_ctx, 0);
Willy Tarreau3af56a92012-08-20 16:55:48 +0200219}
220
Willy Tarreau586f71b2020-12-11 15:54:36 +0100221/* 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 */
226static 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 Tarreaudef47dd02021-07-28 15:27:37 +0200252 * 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 Tarreau586f71b2020-12-11 15:54:36 +0100262 * 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 Tarreaudef47dd02021-07-28 15:27:37 +0200269 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 Tarreau586f71b2020-12-11 15:54:36 +0100278 ret = conn->mux->wake(conn);
279 if (ret < 0)
280 goto done;
Willy Tarreaudef47dd02021-07-28 15:27:37 +0200281
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 Tarreau586f71b2020-12-11 15:54:36 +0100291 }
292 done:
293 return ret;
294}
295
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200296/* shut read */
297static inline void cs_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200298{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200299
300 /* clean data-layer shutdown */
301 if (cs->conn->mux && cs->conn->mux->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200302 cs->conn->mux->shutr(cs, mode);
303 cs->flags |= (mode == CS_SHR_DRAIN) ? CS_FL_SHRD : CS_FL_SHRR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200304}
305
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200306/* shut write */
307static inline void cs_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200308{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200309
310 /* clean data-layer shutdown */
311 if (cs->conn->mux && cs->conn->mux->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200312 cs->conn->mux->shutw(cs, mode);
313 cs->flags |= (mode == CS_SHW_NORMAL) ? CS_FL_SHWN : CS_FL_SHWS;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200314}
315
Willy Tarreau6978db32017-10-05 18:19:43 +0200316/* completely close a conn_stream (but do not detach it) */
317static 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 Tarreau6aee5b92020-12-11 11:04:51 +0100324/* completely close a conn_stream after draining possibly pending data (but do not detach it) */
325static 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 Tarreaue9f43012018-12-19 17:59:30 +0100332/* sets CS_FL_ERROR or CS_FL_ERR_PENDING on the cs */
333static 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 Tarreau7bb39d72019-06-03 14:23:33 +0200338 cs->flags |= CS_FL_ERR_PENDING;
Willy Tarreaue9f43012018-12-19 17:59:30 +0100339}
340
Willy Tarreau3af56a92012-08-20 16:55:48 +0200341/* detect sock->data read0 transition */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200342static inline int conn_xprt_read0_pending(struct connection *c)
Willy Tarreau3af56a92012-08-20 16:55:48 +0200343{
Willy Tarreau54e917c2017-08-30 07:35:35 +0200344 return (c->flags & CO_FL_SOCK_RD_SH) != 0;
Willy Tarreau3af56a92012-08-20 16:55:48 +0200345}
346
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200347/* prepares a connection to work with protocol <proto> and transport <xprt>.
Willy Tarreau53a47662017-08-28 10:53:00 +0200348 * The transport's is initialized as well, and the mux and its context are
Willy Tarreaube373152018-09-06 11:45:30 +0200349 * 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 Tarreau910c6aa2013-10-24 15:08:37 +0200352 */
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100353static inline int conn_prepare(struct connection *conn, const struct protocol *proto, const struct xprt_ops *xprt)
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200354{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100355 int ret = 0;
356
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200357 conn->ctrl = proto;
358 conn->xprt = xprt;
Willy Tarreau53a47662017-08-28 10:53:00 +0200359 conn->mux = NULL;
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200360 conn->xprt_ctx = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100361 conn->ctx = NULL;
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100362 if (xprt->init) {
363 ret = xprt->init(conn, &conn->xprt_ctx);
364 if (ret < 0)
365 conn->xprt = NULL;
366 }
367 return ret;
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200368}
369
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200370/*
371 * Initializes all required fields for a new conn_strema.
372 */
373static 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 Denoyelle99cca082021-05-03 14:28:30 +0200380/* 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 */
384static inline int conn_is_back(const struct connection *conn)
385{
386 return !objt_listener(conn->target);
387}
388
Willy Tarreaud0155772013-10-14 17:10:08 +0200389/* 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 Faulet236c93b2020-07-02 09:19:54 +0200394static inline void conn_init(struct connection *conn, void *target)
Willy Tarreaud0155772013-10-14 17:10:08 +0200395{
396 conn->obj_type = OBJ_TYPE_CONN;
397 conn->flags = CO_FL_NONE;
Willy Tarreau53a47662017-08-28 10:53:00 +0200398 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100399 conn->ctx = NULL;
Willy Tarreaud0155772013-10-14 17:10:08 +0200400 conn->owner = NULL;
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200401 conn->send_proxy_ofs = 0;
Willy Tarreau585744b2017-08-24 14:31:19 +0200402 conn->handle.fd = DEAD_FD_MAGIC;
Willy Tarreaud0155772013-10-14 17:10:08 +0200403 conn->err_code = CO_ER_NONE;
Christopher Faulet236c93b2020-07-02 09:19:54 +0200404 conn->target = target;
Willy Tarreau436d3332017-10-08 11:16:46 +0200405 conn->destroy_cb = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100406 conn->proxy_netns = NULL;
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100407 MT_LIST_INIT(&conn->toremove_list);
Amaury Denoyelle99cca082021-05-03 14:28:30 +0200408 if (conn_is_back(conn))
409 LIST_INIT(&conn->session_list);
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200410 else
411 LIST_INIT(&conn->stopping_list);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100412 conn->subs = NULL;
Willy Tarreaue71fca82019-07-17 19:06:58 +0200413 conn->src = NULL;
414 conn->dst = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100415 conn->proxy_authority = IST_NULL;
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100416 conn->proxy_unique_id = IST_NULL;
Frédéric Lécaillec0ed9192021-03-10 11:51:38 +0100417 conn->qc = NULL;
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100418 conn->hash_node = NULL;
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100419 conn->xprt = NULL;
Willy Tarreaud0155772013-10-14 17:10:08 +0200420}
421
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100422static inline struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
423{
424 struct conn_hash_node *hash_node = NULL;
425
Willy Tarreau5d110b22021-03-22 21:01:05 +0100426 hash_node = pool_zalloc(pool_head_conn_hash_node);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100427 if (unlikely(!hash_node))
428 return NULL;
429
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100430 hash_node->conn = conn;
431
432 return hash_node;
433}
434
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200435/* sets <owner> as the connection's owner */
Willy Tarreau436d3332017-10-08 11:16:46 +0200436static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *))
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200437{
438 conn->owner = owner;
Willy Tarreau436d3332017-10-08 11:16:46 +0200439 conn->destroy_cb = cb;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200440}
441
Christopher Faulet21ddc742020-07-01 15:26:14 +0200442
443/* Mark the connection <conn> as private and remove it from the available connection list */
444static inline void conn_set_private(struct connection *conn)
445{
Christopher Fauletc6e75632020-07-02 16:03:30 +0200446 if (!(conn->flags & CO_FL_PRIVATE)) {
447 conn->flags |= CO_FL_PRIVATE;
Christopher Faulet21ddc742020-07-01 15:26:14 +0200448
Christopher Fauletc6e75632020-07-02 16:03:30 +0200449 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100450 srv_release_conn(__objt_server(conn->target), conn);
Christopher Fauletc6e75632020-07-02 16:03:30 +0200451 }
Christopher Faulet21ddc742020-07-01 15:26:14 +0200452}
453
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200454/* 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 Tarreau9b7587a2020-10-15 07:32:10 +0200458 * 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 Tarreauff5d57b2019-07-17 18:37:02 +0200460 */
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200461static inline struct sockaddr_storage *
462sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200463{
464 struct sockaddr_storage *sa;
465
466 if (sap && *sap)
467 return *sap;
468
469 sa = pool_alloc(pool_head_sockaddr);
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200470 if (sa && orig && len > 0)
471 memcpy(sa, orig, len);
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200472 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 */
481static 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 Tarreaub97f3b12013-10-20 22:56:45 +0200489/* 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 Tarreaubafbe012017-11-24 17:34:44 +0100491 * be released using pool_free() or conn_free().
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200492 */
Christopher Faulet236c93b2020-07-02 09:19:54 +0200493static inline struct connection *conn_new(void *target)
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200494{
495 struct connection *conn;
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100496 struct conn_hash_node *hash_node;
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200497
Willy Tarreaubafbe012017-11-24 17:34:44 +0100498 conn = pool_alloc(pool_head_connection);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100499 if (unlikely(!conn))
500 return NULL;
501
502 conn_init(conn, target);
503
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +0100504 if (conn_is_back(conn)) {
505 if (obj_type(target) == OBJ_TYPE_SERVER)
506 srv_use_conn(__objt_server(target), conn);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100507
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 Faulet3d52f0f2020-07-02 15:45:56 +0200515 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100516
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200517 return conn;
518}
519
Willy Tarreau5e1cc5e2018-03-02 10:43:58 +0100520/* Releases a conn_stream previously allocated by cs_new(), as well as any
521 * buffer it would still hold.
522 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200523static inline void cs_free(struct conn_stream *cs)
524{
Willy Tarreau5e1cc5e2018-03-02 10:43:58 +0100525
Willy Tarreaubafbe012017-11-24 17:34:44 +0100526 pool_free(pool_head_connstream, cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200527}
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 Tarreaubafbe012017-11-24 17:34:44 +0100532 * using pool_free() or conn_free(). The conn_stream is initialized and added
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200533 * to the mux's stream list on success, then returned. On failure, nothing is
534 * allocated and NULL is returned.
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200535 */
Christopher Faulet236c93b2020-07-02 09:19:54 +0200536static inline struct conn_stream *cs_new(struct connection *conn, void *target)
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200537{
538 struct conn_stream *cs;
539
Willy Tarreaubafbe012017-11-24 17:34:44 +0100540 cs = pool_alloc(pool_head_connstream);
Tim Duesterhus927063b2020-02-21 13:02:03 +0100541 if (unlikely(!cs))
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200542 return NULL;
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200543
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200544 if (!conn) {
Christopher Faulet236c93b2020-07-02 09:19:54 +0200545 conn = conn_new(target);
Tim Duesterhus927063b2020-02-21 13:02:03 +0100546 if (unlikely(!conn)) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200547 cs_free(cs);
548 return NULL;
549 }
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200550 }
551
552 cs_init(cs, conn);
553 return cs;
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200554}
555
Willy Tarreaufafd3982018-11-18 21:29:20 +0100556/* 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 */
565static 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 Houchard1fddc9b2018-10-21 00:32:01 +0200572static inline void conn_force_unsubscribe(struct connection *conn)
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200573{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100574 if (!conn->subs)
575 return;
576 conn->subs->events = 0;
577 conn->subs = NULL;
Olivier Houchard1fddc9b2018-10-21 00:32:01 +0200578}
579
580/* Releases a connection previously allocated by conn_new() */
581static inline void conn_free(struct connection *conn)
582{
Amaury Denoyelle04a24c52020-10-14 18:17:07 +0200583 /* If the connection is owned by the session, remove it from its list
584 */
Amaury Denoyelle99cca082021-05-03 14:28:30 +0200585 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
Amaury Denoyelle04a24c52020-10-14 18:17:07 +0200586 session_unown_conn(conn->owner, conn);
Christopher Fauletc6e75632020-07-02 16:03:30 +0200587 }
Christopher Faulet96ddc8a2020-10-19 16:49:29 +0200588 else if (!(conn->flags & CO_FL_PRIVATE)) {
Christopher Fauletc6e75632020-07-02 16:03:30 +0200589 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100590 srv_release_conn(__objt_server(conn->target), conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100591 }
Willy Tarreau94031d32018-12-19 14:36:29 +0100592
Amaury Denoyelleaba50732021-02-16 15:16:17 +0100593 /* 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 Tarreau2b718102021-04-21 07:32:39 +0200599 MT_LIST_DELETE(&conn->toremove_list);
Amaury Denoyelleaba50732021-02-16 15:16:17 +0100600
Willy Tarreaue71fca82019-07-17 19:06:58 +0200601 sockaddr_free(&conn->src);
602 sockaddr_free(&conn->dst);
603
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100604 pool_free(pool_head_authority, istptr(conn->proxy_authority));
605 conn->proxy_authority = IST_NULL;
Tim Duesterhuse004c2b2021-03-06 20:06:48 +0100606
Tim Duesterhus002bd772021-03-06 20:06:49 +0100607 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
Tim Duesterhuse004c2b2021-03-06 20:06:48 +0100608 conn->proxy_unique_id = IST_NULL;
609
610 pool_free(pool_head_conn_hash_node, conn->hash_node);
611 conn->hash_node = NULL;
Geoff Simmons7185b782019-08-27 18:31:16 +0200612
Willy Tarreau94031d32018-12-19 14:36:29 +0100613 /* 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 Houchard201b9f42018-11-21 00:16:29 +0100616 */
Willy Tarreau94031d32018-12-19 14:36:29 +0100617 if (conn->ctx != NULL && conn->mux == NULL)
618 *(void **)conn->ctx = NULL;
Olivier Houchard201b9f42018-11-21 00:16:29 +0100619
Olivier Houchard1fddc9b2018-10-21 00:32:01 +0200620 conn_force_unsubscribe(conn);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100621 pool_free(pool_head_connection, conn);
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200622}
623
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100624/* Release a conn_stream */
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200625static inline void cs_destroy(struct conn_stream *cs)
626{
Olivier Houchard302f9ef2018-04-13 15:50:27 +0200627 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 Tarreau63dd75d2017-10-08 15:16:00 +0200641 cs_free(cs);
642}
643
Olivier Houcharde2b40b92017-09-13 18:30:23 +0200644/* Returns the conn from a cs. If cs is NULL, returns NULL */
645static inline struct connection *cs_conn(const struct conn_stream *cs)
646{
647 return cs ? cs->conn : NULL;
648}
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200649
Willy Tarreau2e34c112019-07-17 10:48:33 +0200650/* 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 */
654static inline int conn_get_src(struct connection *conn)
655{
656 if (conn->flags & CO_FL_ADDR_FROM_SET)
657 return 1;
658
Willy Tarreauf1f66092020-09-04 08:15:31 +0200659 if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_src)
Willy Tarreau2e34c112019-07-17 10:48:33 +0200660 return 0;
661
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200662 if (!sockaddr_alloc(&conn->src, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200663 return 0;
664
Willy Tarreauf1f66092020-09-04 08:15:31 +0200665 if (conn->ctrl->fam->get_src(conn->handle.fd, (struct sockaddr *)conn->src,
Willy Tarreau226572f2019-07-17 14:46:00 +0200666 sizeof(*conn->src),
Willy Tarreau2e34c112019-07-17 10:48:33 +0200667 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 */
677static inline int conn_get_dst(struct connection *conn)
678{
679 if (conn->flags & CO_FL_ADDR_TO_SET)
680 return 1;
681
Willy Tarreauf1f66092020-09-04 08:15:31 +0200682 if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_dst)
Willy Tarreau2e34c112019-07-17 10:48:33 +0200683 return 0;
684
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200685 if (!sockaddr_alloc(&conn->dst, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200686 return 0;
687
Willy Tarreauf1f66092020-09-04 08:15:31 +0200688 if (conn->ctrl->fam->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst,
Willy Tarreau226572f2019-07-17 14:46:00 +0200689 sizeof(*conn->dst),
Willy Tarreau2e34c112019-07-17 10:48:33 +0200690 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 Tarreau1a18b542018-12-11 16:37:42 +0100696/* 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 */
700static 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 Tarreau226572f2019-07-17 14:46:00 +0200706 if (conn->src->ss_family == AF_INET)
Willy Tarreau1a18b542018-12-11 16:37:42 +0100707 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
708#endif
709#ifdef IPV6_TCLASS
Willy Tarreau226572f2019-07-17 14:46:00 +0200710 if (conn->src->ss_family == AF_INET6) {
711 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)conn->src)->sin6_addr))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100712 /* 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 */
723static 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 */
736static 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 Houchard9aaf7782017-09-13 18:30:23 +0200746/* Attaches a conn_stream to a data layer and sets the relevant callbacks */
747static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200748{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200749 cs->data_cb = data_cb;
750 cs->data = data;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200751}
752
Willy Tarreau144f84a2021-03-02 16:09:26 +0100753static inline struct wait_event *wl_set_waitcb(struct wait_event *wl, struct task *(*cb)(struct task *, void *, unsigned int), void *ctx)
Willy Tarreau83188852018-07-18 08:18:20 +0200754{
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200755 if (!wl->tasklet->process) {
756 wl->tasklet->process = cb;
757 wl->tasklet->context = ctx;
Willy Tarreau83188852018-07-18 08:18:20 +0200758 }
759 return wl;
760}
761
Willy Tarreau53a47662017-08-28 10:53:00 +0200762/* Installs the connection's mux layer for upper context <ctx>.
763 * Returns < 0 on error.
764 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200765static inline int conn_install_mux(struct connection *conn, const struct mux_ops *mux,
Olivier Houchardf502aca2018-12-14 19:42:40 +0100766 void *ctx, struct proxy *prx, struct session *sess)
Willy Tarreau53a47662017-08-28 10:53:00 +0200767{
Willy Tarreau762475e2019-01-10 10:33:32 +0100768 int ret;
769
Willy Tarreau53a47662017-08-28 10:53:00 +0200770 conn->mux = mux;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100771 conn->ctx = ctx;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200772 ret = mux->init ? mux->init(conn, prx, sess, &BUF_NULL) : 0;
Willy Tarreau762475e2019-01-10 10:33:32 +0100773 if (ret < 0) {
774 conn->mux = NULL;
775 conn->ctx = NULL;
776 }
777 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200778}
779
Willy Tarreau0af29122012-12-03 15:35:00 +0100780/* returns a human-readable error code for conn->err_code, or NULL if the code
781 * is unknown.
782 */
783static inline const char *conn_err_code_str(struct connection *c)
784{
785 switch (c->err_code) {
786 case CO_ER_NONE: return "Success";
Willy Tarreau45b34e82014-01-24 16:06:50 +0100787
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 Tarreau8e3bf692012-12-03 15:41:18 +0100799 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 Tarreau0af29122012-12-03 15:35:00 +0100801 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100802 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 Jacquin93b227d2016-06-04 15:11:10 +0100806
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 Bretonabc2d3c2021-07-29 09:45:48 +0200809 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100810 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 Tarreau20879a02012-12-03 16:32:10 +0100814 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
815 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
Willy Tarreau0af29122012-12-03 15:35:00 +0100816 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
Willy Tarreau20879a02012-12-03 16:32:10 +0100817 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 Shipitsin77e3b4a2020-03-10 12:06:11 +0500819 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
Willy Tarreau20879a02012-12-03 16:32:10 +0100820 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 Tarreau71d058c2017-07-26 20:09:56 +0200822 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 Tarreau20879a02012-12-03 16:32:10 +0100824 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
Willy Tarreaub3966372014-04-25 18:54:29 +0200825 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
Willy Tarreauf51c6982014-04-25 20:02:39 +0200826 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
Willy Tarreau830bf612013-12-01 20:29:58 +0100827 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
Remi Tricot-Le Bretonabc2d3c2021-07-29 09:45:48 +0200828 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
Alexander Liu2a54bb72019-05-22 19:44:48 +0800829
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 Tarreau0af29122012-12-03 15:35:00 +0100834 }
835 return NULL;
836}
837
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100838static inline const char *conn_get_ctrl_name(const struct connection *conn)
839{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200840 if (!conn || !conn_ctrl_ready(conn))
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100841 return "NONE";
842 return conn->ctrl->name;
843}
844
845static inline const char *conn_get_xprt_name(const struct connection *conn)
846{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100847 if (!conn || !conn->xprt)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100848 return "NONE";
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +0100849 return conn->xprt->name;
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100850}
851
Willy Tarreau53a47662017-08-28 10:53:00 +0200852static inline const char *conn_get_mux_name(const struct connection *conn)
853{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200854 if (!conn || !conn->mux)
Willy Tarreau53a47662017-08-28 10:53:00 +0200855 return "NONE";
856 return conn->mux->name;
857}
858
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200859static inline const char *cs_get_data_name(const struct conn_stream *cs)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100860{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200861 if (!cs || !cs->data_cb)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100862 return "NONE";
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200863 return cs->data_cb->name;
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100864}
865
Willy Tarreau13e14102016-12-22 20:25:26 +0100866/* registers pointer to transport layer <id> (XPRT_*) */
867static 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 */
875static inline struct xprt_ops *xprt_get(int id)
876{
877 if (id >= XPRT_ENTRIES)
878 return NULL;
879 return registered_xprt[id];
880}
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100881
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200882/* 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 */
887static 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 Tarreau4f8cd432021-03-02 17:27:58 +0100917/* 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 */
921static 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 */
930static 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 Tarreaua9c17412016-12-04 18:42:09 +0100936static 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 Houcharde179d0e2019-03-21 18:27:17 +0100940 return conn->xprt->get_alpn(conn, conn->xprt_ctx, str, len);
Willy Tarreaua9c17412016-12-04 18:42:09 +0100941}
942
Christopher Faulet32f61c02018-04-10 14:33:41 +0200943/* registers proto mux list <list>. Modifies the list element! */
944static inline void register_mux_proto(struct mux_proto_list *list)
Willy Tarreau2386be62017-09-21 19:40:52 +0200945{
Willy Tarreau2b718102021-04-21 07:32:39 +0200946 LIST_APPEND(&mux_proto_list.list, &list->list);
Willy Tarreau2386be62017-09-21 19:40:52 +0200947}
948
Christopher Faulet32f61c02018-04-10 14:33:41 +0200949/* unregisters proto mux list <list> */
950static inline void unregister_mux_proto(struct mux_proto_list *list)
Willy Tarreau2386be62017-09-21 19:40:52 +0200951{
Willy Tarreau2b718102021-04-21 07:32:39 +0200952 LIST_DELETE(&list->list);
Willy Tarreau2386be62017-09-21 19:40:52 +0200953 LIST_INIT(&list->list);
954}
955
Christopher Faulete15c6c42018-08-08 10:21:56 +0200956static inline struct mux_proto_list *get_mux_proto(const struct ist proto)
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200957{
958 struct mux_proto_list *item;
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200959
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 Tarreaue59b5162021-05-08 14:06:09 +0200967void list_mux_proto(FILE *out);
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100968/* 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 Faulet7ce0c892018-04-10 15:01:45 +0200970 * 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 Tarreau2386be62017-09-21 19:40:52 +0200973 */
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100974static 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 Tarreau2386be62017-09-21 19:40:52 +0200977{
Christopher Faulet32f61c02018-04-10 14:33:41 +0200978 struct mux_proto_list *item;
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200979 struct mux_proto_list *fallback = NULL;
Willy Tarreau2386be62017-09-21 19:40:52 +0200980
Christopher Faulet32f61c02018-04-10 14:33:41 +0200981 list_for_each_entry(item, &mux_proto_list.list, list) {
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200982 if (!(item->side & proto_side) || !(item->mode & proto_mode))
Willy Tarreau2386be62017-09-21 19:40:52 +0200983 continue;
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200984 if (istlen(mux_proto) && isteq(mux_proto, item->token))
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100985 return item;
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200986 else if (!istlen(item->token)) {
987 if (!fallback || (item->mode == proto_mode && fallback->mode != proto_mode))
988 fallback = item;
989 }
Willy Tarreau2386be62017-09-21 19:40:52 +0200990 }
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100991 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 */
1001static 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 Faulet7ce0c892018-04-10 15:01:45 +02001008
Willy Tarreau5fc311c2018-12-02 13:04:43 +01001009 return item ? item->mux : NULL;
Willy Tarreau2386be62017-09-21 19:40:52 +02001010}
1011
Willy Tarreau6ac98ac2018-09-06 11:48:44 +02001012/* 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 */
1016static 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 Faulet7ce0c892018-04-10 15:01:45 +02001034/* 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 Tarreau2386be62017-09-21 19:40:52 +02001037 */
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001038static inline int conn_install_mux_fe(struct connection *conn, void *ctx)
Willy Tarreau2386be62017-09-21 19:40:52 +02001039{
Willy Tarreau55e0da62018-09-20 11:26:52 +02001040 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001041 const struct mux_ops *mux_ops;
Willy Tarreau2386be62017-09-21 19:40:52 +02001042
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001043 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 Tarreau68ad3a42018-10-22 11:49:15 +02001049 int mode;
1050
Christopher Fauletc1918d12019-04-24 15:01:22 +02001051 if (bind_conf->frontend->mode == PR_MODE_HTTP)
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001052 mode = PROTO_MODE_HTTP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001053 else
Christopher Fauletc1918d12019-04-24 15:01:22 +02001054 mode = PROTO_MODE_TCP;
Willy Tarreau2386be62017-09-21 19:40:52 +02001055
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001056 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 Houchardf502aca2018-12-14 19:42:40 +01001062 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
Willy Tarreau2386be62017-09-21 19:40:52 +02001063}
1064
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001065/* 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 Tarreau2e0b2b52017-09-15 06:59:55 +02001067 * < 0 on error.
Willy Tarreau2386be62017-09-21 19:40:52 +02001068 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001069static inline int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess)
Willy Tarreau2386be62017-09-21 19:40:52 +02001070{
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001071 struct server *srv = objt_server(conn->target);
Willy Tarreau287527a2018-08-08 18:40:44 +02001072 struct proxy *prx = objt_proxy(conn->target);
Willy Tarreau2386be62017-09-21 19:40:52 +02001073 const struct mux_ops *mux_ops;
1074
Willy Tarreau287527a2018-08-08 18:40:44 +02001075 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 Faulet7ce0c892018-04-10 15:01:45 +02001082 mux_ops = srv->mux_proto->mux;
1083 else {
Olivier Houchard201b9f42018-11-21 00:16:29 +01001084 struct ist mux_proto;
1085 const char *alpn_str = NULL;
1086 int alpn_len = 0;
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001087 int mode;
1088
Christopher Fauletc1918d12019-04-24 15:01:22 +02001089 if (prx->mode == PR_MODE_HTTP)
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001090 mode = PROTO_MODE_HTTP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001091 else
Christopher Fauletc1918d12019-04-24 15:01:22 +02001092 mode = PROTO_MODE_TCP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001093
Olivier Houchard201b9f42018-11-21 00:16:29 +01001094 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 Faulet7ce0c892018-04-10 15:01:45 +02001098 if (!mux_ops)
1099 return -1;
1100 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01001101 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
Willy Tarreau2386be62017-09-21 19:40:52 +02001102}
1103
Christopher Fauleta9e1c4c2020-04-21 12:18:05 +02001104/* 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 */
1108static 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 Houchard2ab3dad2019-07-03 13:08:18 +02001147/* Change the mux for the connection.
1148 * The caller should make sure he's not subscribed to the underlying XPRT.
1149 */
Christopher Fauletc0016d82019-04-08 10:42:41 +02001150static 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 Fauletc0016d82019-04-08 10:42:41 +02001177 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 Denoyelle81c6f762021-01-18 14:57:50 +01001190/* 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 */
1193XXH64_hash_t conn_calculate_hash(const struct conn_hash_params *params);
1194
1195static 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 */
1204static 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
1214static 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 Tarreau7ea393d2020-06-04 18:02:10 +02001223#endif /* _HAPROXY_CONNECTION_H */
Willy Tarreau59f98392012-07-06 14:13:49 +02001224
1225/*
1226 * Local variables:
1227 * c-indent-level: 8
1228 * c-basic-offset: 8
1229 * End:
1230 */