blob: 157387b32383ebd493907e350260c58bc5c5cda4 [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
Christopher Fauletc5fd15d2023-03-16 11:43:05 +0100221/* Used to know if a connection is in an idle list. It returns connection flag
222 * corresponding to the idle list if the connection is idle (CO_FL_SAFE_LIST or
223 * CO_FL_IDLE_LIST) or 0 otherwise. Note that if the connection is scheduled to
224 * be removed, 0 is returned, regardless the connection flags.
225 */
226static inline unsigned int conn_get_idle_flag(const struct connection *conn)
227{
228 return (!MT_LIST_INLIST(&conn->toremove_list) ? conn->flags & CO_FL_LIST_MASK : 0);
229}
230
Willy Tarreau586f71b2020-12-11 15:54:36 +0100231/* This is used at the end of the socket IOCB to possibly create the mux if it
232 * was not done yet, or wake it up if flags changed compared to old_flags or if
233 * need_wake insists on this. It returns <0 if the connection was destroyed and
234 * must not be used, >=0 otherwise.
235 */
236static inline int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake)
237{
238 int ret = 0;
239
240 /* If we don't yet have a mux, that means we were waiting for
241 * information to create one, typically from the ALPN. If we're
242 * done with the handshake, attempt to create one.
243 */
244 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) {
245 ret = conn_create_mux(conn);
246 if (ret < 0)
247 goto done;
248 }
249
250 /* The wake callback is normally used to notify the data layer about
251 * data layer activity (successful send/recv), connection establishment,
252 * shutdown and fatal errors. We need to consider the following
253 * situations to wake up the data layer :
254 * - change among the CO_FL_NOTIFY_DONE flags :
255 * SOCK_{RD,WR}_SH, ERROR,
256 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
257 * end of handshake and transition to CONNECTED
258 * - raise of CONNECTED with HANDSHAKE down
259 * - end of HANDSHAKE with CONNECTED set
260 * - regular data layer activity
261 *
Willy Tarreaudef47dd02021-07-28 15:27:37 +0200262 * One tricky case is the wake up on read0 or error on an idle
263 * backend connection, that can happen on a connection that is still
264 * polled while at the same moment another thread is about to perform a
265 * takeover. The solution against this is to remove the connection from
266 * the idle list if it was in it, and possibly reinsert it at the end
267 * if the connection remains valid. The cost is non-null (locked tree
268 * removal) but remains low given that this is extremely rarely called.
269 * In any case it's guaranteed by the FD's thread_mask that we're
270 * called from the same thread the connection is queued in.
271 *
Willy Tarreau586f71b2020-12-11 15:54:36 +0100272 * Note that the wake callback is allowed to release the connection and
273 * the fd (and return < 0 in this case).
274 */
275 if ((forced_wake ||
276 ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) ||
277 ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
278 conn->mux && conn->mux->wake) {
Christopher Fauletc5fd15d2023-03-16 11:43:05 +0100279 uint conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaudef47dd02021-07-28 15:27:37 +0200280 struct server *srv = objt_server(conn->target);
281
282 if (conn_in_list) {
283 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
284 conn_delete_from_tree(&conn->hash_node->node);
285 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
286 }
287
Willy Tarreau586f71b2020-12-11 15:54:36 +0100288 ret = conn->mux->wake(conn);
289 if (ret < 0)
290 goto done;
Willy Tarreaudef47dd02021-07-28 15:27:37 +0200291
292 if (conn_in_list) {
293 struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ?
294 &srv->per_thr[tid].safe_conns :
295 &srv->per_thr[tid].idle_conns;
296
297 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
298 ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash));
299 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
300 }
Willy Tarreau586f71b2020-12-11 15:54:36 +0100301 }
302 done:
303 return ret;
304}
305
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200306/* shut read */
307static inline void cs_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200308{
Willy Tarreauaa349462021-11-14 13:42:17 +0100309 if (cs->flags & CS_FL_SHR)
310 return;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200311
312 /* clean data-layer shutdown */
313 if (cs->conn->mux && cs->conn->mux->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200314 cs->conn->mux->shutr(cs, mode);
315 cs->flags |= (mode == CS_SHR_DRAIN) ? CS_FL_SHRD : CS_FL_SHRR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200316}
317
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200318/* shut write */
319static inline void cs_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200320{
Willy Tarreauaa349462021-11-14 13:42:17 +0100321 if (cs->flags & CS_FL_SHW)
322 return;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200323
324 /* clean data-layer shutdown */
325 if (cs->conn->mux && cs->conn->mux->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200326 cs->conn->mux->shutw(cs, mode);
327 cs->flags |= (mode == CS_SHW_NORMAL) ? CS_FL_SHWN : CS_FL_SHWS;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200328}
329
Willy Tarreau6978db32017-10-05 18:19:43 +0200330/* completely close a conn_stream (but do not detach it) */
331static inline void cs_close(struct conn_stream *cs)
332{
333 cs_shutw(cs, CS_SHW_SILENT);
334 cs_shutr(cs, CS_SHR_RESET);
Willy Tarreau6978db32017-10-05 18:19:43 +0200335}
336
Willy Tarreau6aee5b92020-12-11 11:04:51 +0100337/* completely close a conn_stream after draining possibly pending data (but do not detach it) */
338static inline void cs_drain_and_close(struct conn_stream *cs)
339{
340 cs_shutw(cs, CS_SHW_SILENT);
341 cs_shutr(cs, CS_SHR_DRAIN);
Willy Tarreau6aee5b92020-12-11 11:04:51 +0100342}
343
Willy Tarreaue9f43012018-12-19 17:59:30 +0100344/* sets CS_FL_ERROR or CS_FL_ERR_PENDING on the cs */
345static inline void cs_set_error(struct conn_stream *cs)
346{
347 if (cs->flags & CS_FL_EOS)
348 cs->flags |= CS_FL_ERROR;
349 else
Willy Tarreau7bb39d72019-06-03 14:23:33 +0200350 cs->flags |= CS_FL_ERR_PENDING;
Willy Tarreaue9f43012018-12-19 17:59:30 +0100351}
352
Willy Tarreau3af56a92012-08-20 16:55:48 +0200353/* detect sock->data read0 transition */
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200354static inline int conn_xprt_read0_pending(struct connection *c)
Willy Tarreau3af56a92012-08-20 16:55:48 +0200355{
Willy Tarreau54e917c2017-08-30 07:35:35 +0200356 return (c->flags & CO_FL_SOCK_RD_SH) != 0;
Willy Tarreau3af56a92012-08-20 16:55:48 +0200357}
358
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200359/* prepares a connection to work with protocol <proto> and transport <xprt>.
Willy Tarreau53a47662017-08-28 10:53:00 +0200360 * The transport's is initialized as well, and the mux and its context are
Willy Tarreaube373152018-09-06 11:45:30 +0200361 * cleared. The target is not reinitialized and it is recommended that it is
362 * set prior to calling this function so that the function may make use of it
363 * in the future to refine the mux choice if needed.
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200364 */
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100365static inline int conn_prepare(struct connection *conn, const struct protocol *proto, const struct xprt_ops *xprt)
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200366{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100367 int ret = 0;
368
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200369 conn->ctrl = proto;
370 conn->xprt = xprt;
Willy Tarreau53a47662017-08-28 10:53:00 +0200371 conn->mux = NULL;
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200372 conn->xprt_ctx = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100373 conn->ctx = NULL;
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100374 if (xprt->init) {
375 ret = xprt->init(conn, &conn->xprt_ctx);
376 if (ret < 0)
377 conn->xprt = NULL;
378 }
379 return ret;
Willy Tarreau910c6aa2013-10-24 15:08:37 +0200380}
381
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200382/*
383 * Initializes all required fields for a new conn_strema.
384 */
385static inline void cs_init(struct conn_stream *cs, struct connection *conn)
386{
387 cs->obj_type = OBJ_TYPE_CS;
388 cs->flags = CS_FL_NONE;
389 cs->conn = conn;
390}
391
Amaury Denoyelle99cca082021-05-03 14:28:30 +0200392/* returns 0 if the connection is valid and is a frontend connection, otherwise
393 * returns 1 indicating it's a backend connection. And uninitialized connection
394 * also returns 1 to better handle the usage in the middle of initialization.
395 */
396static inline int conn_is_back(const struct connection *conn)
397{
398 return !objt_listener(conn->target);
399}
400
Willy Tarreaud0155772013-10-14 17:10:08 +0200401/* Initializes all required fields for a new connection. Note that it does the
402 * minimum acceptable initialization for a connection that already exists and
403 * is about to be reused. It also leaves the addresses untouched, which makes
404 * it usable across connection retries to reset a connection to a known state.
405 */
Christopher Faulet236c93b2020-07-02 09:19:54 +0200406static inline void conn_init(struct connection *conn, void *target)
Willy Tarreaud0155772013-10-14 17:10:08 +0200407{
408 conn->obj_type = OBJ_TYPE_CONN;
409 conn->flags = CO_FL_NONE;
Willy Tarreau53a47662017-08-28 10:53:00 +0200410 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100411 conn->ctx = NULL;
Willy Tarreaud0155772013-10-14 17:10:08 +0200412 conn->owner = NULL;
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200413 conn->send_proxy_ofs = 0;
Willy Tarreau585744b2017-08-24 14:31:19 +0200414 conn->handle.fd = DEAD_FD_MAGIC;
Willy Tarreaud0155772013-10-14 17:10:08 +0200415 conn->err_code = CO_ER_NONE;
Christopher Faulet236c93b2020-07-02 09:19:54 +0200416 conn->target = target;
Willy Tarreau436d3332017-10-08 11:16:46 +0200417 conn->destroy_cb = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100418 conn->proxy_netns = NULL;
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100419 MT_LIST_INIT(&conn->toremove_list);
Amaury Denoyelle99cca082021-05-03 14:28:30 +0200420 if (conn_is_back(conn))
421 LIST_INIT(&conn->session_list);
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200422 else
423 LIST_INIT(&conn->stopping_list);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100424 conn->subs = NULL;
Willy Tarreaue71fca82019-07-17 19:06:58 +0200425 conn->src = NULL;
426 conn->dst = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100427 conn->proxy_authority = IST_NULL;
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100428 conn->proxy_unique_id = IST_NULL;
Frédéric Lécaillec0ed9192021-03-10 11:51:38 +0100429 conn->qc = NULL;
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100430 conn->hash_node = NULL;
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100431 conn->xprt = NULL;
Willy Tarreaud0155772013-10-14 17:10:08 +0200432}
433
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100434static inline struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
435{
436 struct conn_hash_node *hash_node = NULL;
437
Willy Tarreau5d110b22021-03-22 21:01:05 +0100438 hash_node = pool_zalloc(pool_head_conn_hash_node);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100439 if (unlikely(!hash_node))
440 return NULL;
441
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100442 hash_node->conn = conn;
443
444 return hash_node;
445}
446
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200447/* sets <owner> as the connection's owner */
Willy Tarreau436d3332017-10-08 11:16:46 +0200448static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *))
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200449{
450 conn->owner = owner;
Willy Tarreau436d3332017-10-08 11:16:46 +0200451 conn->destroy_cb = cb;
Willy Tarreau8e3c6ce2017-08-28 15:46:01 +0200452}
453
Christopher Faulet21ddc742020-07-01 15:26:14 +0200454
455/* Mark the connection <conn> as private and remove it from the available connection list */
456static inline void conn_set_private(struct connection *conn)
457{
Christopher Fauletc6e75632020-07-02 16:03:30 +0200458 if (!(conn->flags & CO_FL_PRIVATE)) {
459 conn->flags |= CO_FL_PRIVATE;
Christopher Faulet21ddc742020-07-01 15:26:14 +0200460
Christopher Fauletc6e75632020-07-02 16:03:30 +0200461 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100462 srv_release_conn(__objt_server(conn->target), conn);
Christopher Fauletc6e75632020-07-02 16:03:30 +0200463 }
Christopher Faulet21ddc742020-07-01 15:26:14 +0200464}
465
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200466/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
467 * returns it. If <sap> is NULL, the address is always allocated and returned.
468 * if <sap> is non-null, an address will only be allocated if it points to a
469 * non-null pointer. In this case the allocated address will be assigned there.
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200470 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
471 * into the allocated address. In both situations the new pointer is returned.
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200472 */
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200473static inline struct sockaddr_storage *
474sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200475{
476 struct sockaddr_storage *sa;
477
478 if (sap && *sap)
479 return *sap;
480
481 sa = pool_alloc(pool_head_sockaddr);
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200482 if (sa && orig && len > 0)
483 memcpy(sa, orig, len);
Willy Tarreauff5d57b2019-07-17 18:37:02 +0200484 if (sap)
485 *sap = sa;
486 return sa;
487}
488
489/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
490 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
491 * there.
492 */
493static inline void sockaddr_free(struct sockaddr_storage **sap)
494{
495 if (!sap)
496 return;
497 pool_free(pool_head_sockaddr, *sap);
498 *sap = NULL;
499}
500
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200501/* Tries to allocate a new connection and initialized its main fields. The
502 * connection is returned on success, NULL on failure. The connection must
Willy Tarreaubafbe012017-11-24 17:34:44 +0100503 * be released using pool_free() or conn_free().
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200504 */
Christopher Faulet236c93b2020-07-02 09:19:54 +0200505static inline struct connection *conn_new(void *target)
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200506{
507 struct connection *conn;
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100508 struct conn_hash_node *hash_node;
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200509
Willy Tarreaubafbe012017-11-24 17:34:44 +0100510 conn = pool_alloc(pool_head_connection);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100511 if (unlikely(!conn))
512 return NULL;
513
514 conn_init(conn, target);
515
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +0100516 if (conn_is_back(conn)) {
517 if (obj_type(target) == OBJ_TYPE_SERVER)
518 srv_use_conn(__objt_server(target), conn);
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100519
520 hash_node = conn_alloc_hash_node(conn);
521 if (unlikely(!hash_node)) {
522 pool_free(pool_head_connection, conn);
523 return NULL;
524 }
525
526 conn->hash_node = hash_node;
Christopher Faulet3d52f0f2020-07-02 15:45:56 +0200527 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +0100528
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200529 return conn;
530}
531
Willy Tarreau5e1cc5e2018-03-02 10:43:58 +0100532/* Releases a conn_stream previously allocated by cs_new(), as well as any
533 * buffer it would still hold.
534 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200535static inline void cs_free(struct conn_stream *cs)
536{
Willy Tarreau5e1cc5e2018-03-02 10:43:58 +0100537
Willy Tarreaubafbe012017-11-24 17:34:44 +0100538 pool_free(pool_head_connstream, cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200539}
540
541/* Tries to allocate a new conn_stream and initialize its main fields. If
542 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
543 * and assigned to cs->conn ; this connection will then have to be released
Willy Tarreaubafbe012017-11-24 17:34:44 +0100544 * using pool_free() or conn_free(). The conn_stream is initialized and added
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200545 * to the mux's stream list on success, then returned. On failure, nothing is
546 * allocated and NULL is returned.
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200547 */
Christopher Faulet236c93b2020-07-02 09:19:54 +0200548static inline struct conn_stream *cs_new(struct connection *conn, void *target)
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200549{
550 struct conn_stream *cs;
551
Willy Tarreaubafbe012017-11-24 17:34:44 +0100552 cs = pool_alloc(pool_head_connstream);
Tim Duesterhus927063b2020-02-21 13:02:03 +0100553 if (unlikely(!cs))
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200554 return NULL;
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200555
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200556 if (!conn) {
Christopher Faulet236c93b2020-07-02 09:19:54 +0200557 conn = conn_new(target);
Tim Duesterhus927063b2020-02-21 13:02:03 +0100558 if (unlikely(!conn)) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200559 cs_free(cs);
560 return NULL;
561 }
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200562 }
563
564 cs_init(cs, conn);
565 return cs;
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200566}
567
Willy Tarreaufafd3982018-11-18 21:29:20 +0100568/* Retrieves any valid conn_stream from this connection, preferably the first
569 * valid one. The purpose is to be able to figure one other end of a private
570 * connection for purposes like source binding or proxy protocol header
571 * emission. In such cases, any conn_stream is expected to be valid so the
572 * mux is encouraged to return the first one it finds. If the connection has
573 * no mux or the mux has no get_first_cs() method or the mux has no valid
574 * conn_stream, NULL is returned. The output pointer is purposely marked
575 * const to discourage the caller from modifying anything there.
576 */
577static inline const struct conn_stream *cs_get_first(const struct connection *conn)
578{
579 if (!conn || !conn->mux || !conn->mux->get_first_cs)
580 return NULL;
581 return conn->mux->get_first_cs(conn);
582}
583
Olivier Houchard1fddc9b2018-10-21 00:32:01 +0200584static inline void conn_force_unsubscribe(struct connection *conn)
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200585{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100586 if (!conn->subs)
587 return;
588 conn->subs->events = 0;
589 conn->subs = NULL;
Olivier Houchard1fddc9b2018-10-21 00:32:01 +0200590}
591
592/* Releases a connection previously allocated by conn_new() */
593static inline void conn_free(struct connection *conn)
594{
Amaury Denoyelle04a24c52020-10-14 18:17:07 +0200595 /* If the connection is owned by the session, remove it from its list
596 */
Amaury Denoyelle99cca082021-05-03 14:28:30 +0200597 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
Amaury Denoyelle04a24c52020-10-14 18:17:07 +0200598 session_unown_conn(conn->owner, conn);
Christopher Fauletc6e75632020-07-02 16:03:30 +0200599 }
Christopher Faulet96ddc8a2020-10-19 16:49:29 +0200600 else if (!(conn->flags & CO_FL_PRIVATE)) {
Christopher Fauletc6e75632020-07-02 16:03:30 +0200601 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
Amaury Denoyellef232cb32021-01-06 16:14:12 +0100602 srv_release_conn(__objt_server(conn->target), conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100603 }
Willy Tarreau94031d32018-12-19 14:36:29 +0100604
Amaury Denoyelleaba50732021-02-16 15:16:17 +0100605 /* Remove the conn from toremove_list.
606 *
607 * This is needed to prevent a double-free in case the connection was
608 * already scheduled from cleaning but is freed before via another
609 * call.
610 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200611 MT_LIST_DELETE(&conn->toremove_list);
Amaury Denoyelleaba50732021-02-16 15:16:17 +0100612
Willy Tarreaue71fca82019-07-17 19:06:58 +0200613 sockaddr_free(&conn->src);
614 sockaddr_free(&conn->dst);
615
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100616 pool_free(pool_head_authority, istptr(conn->proxy_authority));
617 conn->proxy_authority = IST_NULL;
Tim Duesterhuse004c2b2021-03-06 20:06:48 +0100618
Tim Duesterhus002bd772021-03-06 20:06:49 +0100619 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
Tim Duesterhuse004c2b2021-03-06 20:06:48 +0100620 conn->proxy_unique_id = IST_NULL;
621
622 pool_free(pool_head_conn_hash_node, conn->hash_node);
623 conn->hash_node = NULL;
Geoff Simmons7185b782019-08-27 18:31:16 +0200624
Willy Tarreau94031d32018-12-19 14:36:29 +0100625 /* By convention we always place a NULL where the ctx points to if the
626 * mux is null. It may have been used to store the connection as a
627 * stream_interface's end point for example.
Olivier Houchard201b9f42018-11-21 00:16:29 +0100628 */
Willy Tarreau94031d32018-12-19 14:36:29 +0100629 if (conn->ctx != NULL && conn->mux == NULL)
630 *(void **)conn->ctx = NULL;
Olivier Houchard201b9f42018-11-21 00:16:29 +0100631
Olivier Houchard1fddc9b2018-10-21 00:32:01 +0200632 conn_force_unsubscribe(conn);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100633 pool_free(pool_head_connection, conn);
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200634}
635
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100636/* Release a conn_stream */
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200637static inline void cs_destroy(struct conn_stream *cs)
638{
Olivier Houchard302f9ef2018-04-13 15:50:27 +0200639 if (cs->conn->mux)
640 cs->conn->mux->detach(cs);
641 else {
642 /* It's too early to have a mux, let's just destroy
643 * the connection
644 */
645 struct connection *conn = cs->conn;
646
647 conn_stop_tracking(conn);
648 conn_full_close(conn);
649 if (conn->destroy_cb)
650 conn->destroy_cb(conn);
651 conn_free(conn);
652 }
Willy Tarreau63dd75d2017-10-08 15:16:00 +0200653 cs_free(cs);
654}
655
Olivier Houcharde2b40b92017-09-13 18:30:23 +0200656/* Returns the conn from a cs. If cs is NULL, returns NULL */
657static inline struct connection *cs_conn(const struct conn_stream *cs)
658{
659 return cs ? cs->conn : NULL;
660}
Willy Tarreaub97f3b12013-10-20 22:56:45 +0200661
Willy Tarreau2e34c112019-07-17 10:48:33 +0200662/* Retrieves the connection's original source address. Returns non-zero on
663 * success or zero on failure. The operation is only performed once and the
664 * address is stored in the connection for future use.
665 */
666static inline int conn_get_src(struct connection *conn)
667{
668 if (conn->flags & CO_FL_ADDR_FROM_SET)
669 return 1;
670
Willy Tarreauf1f66092020-09-04 08:15:31 +0200671 if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_src)
Willy Tarreau2e34c112019-07-17 10:48:33 +0200672 return 0;
673
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200674 if (!sockaddr_alloc(&conn->src, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200675 return 0;
676
Willy Tarreauf1f66092020-09-04 08:15:31 +0200677 if (conn->ctrl->fam->get_src(conn->handle.fd, (struct sockaddr *)conn->src,
Willy Tarreau226572f2019-07-17 14:46:00 +0200678 sizeof(*conn->src),
Willy Tarreau2e34c112019-07-17 10:48:33 +0200679 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
680 return 0;
681 conn->flags |= CO_FL_ADDR_FROM_SET;
682 return 1;
683}
684
685/* Retrieves the connection's original destination address. Returns non-zero on
686 * success or zero on failure. The operation is only performed once and the
687 * address is stored in the connection for future use.
688 */
689static inline int conn_get_dst(struct connection *conn)
690{
691 if (conn->flags & CO_FL_ADDR_TO_SET)
692 return 1;
693
Willy Tarreauf1f66092020-09-04 08:15:31 +0200694 if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_dst)
Willy Tarreau2e34c112019-07-17 10:48:33 +0200695 return 0;
696
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200697 if (!sockaddr_alloc(&conn->dst, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200698 return 0;
699
Willy Tarreauf1f66092020-09-04 08:15:31 +0200700 if (conn->ctrl->fam->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst,
Willy Tarreau226572f2019-07-17 14:46:00 +0200701 sizeof(*conn->dst),
Willy Tarreau2e34c112019-07-17 10:48:33 +0200702 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
703 return 0;
704 conn->flags |= CO_FL_ADDR_TO_SET;
705 return 1;
706}
707
Willy Tarreau1a18b542018-12-11 16:37:42 +0100708/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
709 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). The connection is tested and if
710 * it is null, nothing is done.
711 */
712static inline void conn_set_tos(const struct connection *conn, int tos)
713{
714 if (!conn || !conn_ctrl_ready(conn))
715 return;
716
717#ifdef IP_TOS
Willy Tarreau226572f2019-07-17 14:46:00 +0200718 if (conn->src->ss_family == AF_INET)
Willy Tarreau1a18b542018-12-11 16:37:42 +0100719 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
720#endif
721#ifdef IPV6_TCLASS
Willy Tarreau226572f2019-07-17 14:46:00 +0200722 if (conn->src->ss_family == AF_INET6) {
723 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)conn->src)->sin6_addr))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100724 /* v4-mapped addresses need IP_TOS */
725 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
726 else
727 setsockopt(conn->handle.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
728 }
729#endif
730}
731
732/* Sets the netfilter mark on the connection's socket. The connection is tested
733 * and if it is null, nothing is done.
734 */
735static inline void conn_set_mark(const struct connection *conn, int mark)
736{
737 if (!conn || !conn_ctrl_ready(conn))
738 return;
739
740#ifdef SO_MARK
741 setsockopt(conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
742#endif
743}
744
745/* Sets adjust the TCP quick-ack feature on the connection's socket. The
746 * connection is tested and if it is null, nothing is done.
747 */
748static inline void conn_set_quickack(const struct connection *conn, int value)
749{
750 if (!conn || !conn_ctrl_ready(conn))
751 return;
752
753#ifdef TCP_QUICKACK
754 setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &value, sizeof(value));
755#endif
756}
757
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200758/* Attaches a conn_stream to a data layer and sets the relevant callbacks */
759static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200760{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200761 cs->data_cb = data_cb;
762 cs->data = data;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200763}
764
Willy Tarreau144f84a2021-03-02 16:09:26 +0100765static 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 +0200766{
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200767 if (!wl->tasklet->process) {
768 wl->tasklet->process = cb;
769 wl->tasklet->context = ctx;
Willy Tarreau83188852018-07-18 08:18:20 +0200770 }
771 return wl;
772}
773
Willy Tarreau53a47662017-08-28 10:53:00 +0200774/* Installs the connection's mux layer for upper context <ctx>.
775 * Returns < 0 on error.
776 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200777static inline int conn_install_mux(struct connection *conn, const struct mux_ops *mux,
Olivier Houchardf502aca2018-12-14 19:42:40 +0100778 void *ctx, struct proxy *prx, struct session *sess)
Willy Tarreau53a47662017-08-28 10:53:00 +0200779{
Willy Tarreau762475e2019-01-10 10:33:32 +0100780 int ret;
781
Willy Tarreau53a47662017-08-28 10:53:00 +0200782 conn->mux = mux;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100783 conn->ctx = ctx;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200784 ret = mux->init ? mux->init(conn, prx, sess, &BUF_NULL) : 0;
Willy Tarreau762475e2019-01-10 10:33:32 +0100785 if (ret < 0) {
786 conn->mux = NULL;
787 conn->ctx = NULL;
788 }
789 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200790}
791
Willy Tarreau0af29122012-12-03 15:35:00 +0100792/* returns a human-readable error code for conn->err_code, or NULL if the code
793 * is unknown.
794 */
795static inline const char *conn_err_code_str(struct connection *c)
796{
797 switch (c->err_code) {
798 case CO_ER_NONE: return "Success";
Willy Tarreau45b34e82014-01-24 16:06:50 +0100799
800 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
801 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
802 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
803 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
804 case CO_ER_NOPROTO: return "Protocol or address family not supported";
805 case CO_ER_SOCK_ERR: return "General socket error";
806 case CO_ER_PORT_RANGE: return "Source port range exhausted";
807 case CO_ER_CANT_BIND: return "Can't bind to source address";
808 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
809 case CO_ER_ADDR_INUSE: return "Local source address already in use";
810
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100811 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
812 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
Willy Tarreau0af29122012-12-03 15:35:00 +0100813 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100814 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
815 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
816 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
817 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100818
819 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
820 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
Remi Tricot-Le Bretonabc2d3c2021-07-29 09:45:48 +0200821 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100822 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
823 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
824 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
825
Willy Tarreau20879a02012-12-03 16:32:10 +0100826 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
827 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
Willy Tarreau0af29122012-12-03 15:35:00 +0100828 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
Willy Tarreau20879a02012-12-03 16:32:10 +0100829 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
830 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500831 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
Willy Tarreau20879a02012-12-03 16:32:10 +0100832 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
833 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
Willy Tarreau71d058c2017-07-26 20:09:56 +0200834 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
835 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
Willy Tarreau20879a02012-12-03 16:32:10 +0100836 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
Willy Tarreaub3966372014-04-25 18:54:29 +0200837 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
Willy Tarreauf51c6982014-04-25 20:02:39 +0200838 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
Willy Tarreau830bf612013-12-01 20:29:58 +0100839 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 +0200840 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
Alexander Liu2a54bb72019-05-22 19:44:48 +0800841
842 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
843 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
844 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
845 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
Willy Tarreau0af29122012-12-03 15:35:00 +0100846 }
847 return NULL;
848}
849
Amaury Denoyelle92621012021-10-18 14:32:36 +0200850int conn_update_alpn(struct connection *conn, const struct ist alpn, int force);
851
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100852static inline const char *conn_get_ctrl_name(const struct connection *conn)
853{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200854 if (!conn || !conn_ctrl_ready(conn))
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100855 return "NONE";
856 return conn->ctrl->name;
857}
858
859static inline const char *conn_get_xprt_name(const struct connection *conn)
860{
Olivier Houchard1b3c9312021-03-05 23:37:48 +0100861 if (!conn || !conn->xprt)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100862 return "NONE";
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +0100863 return conn->xprt->name;
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100864}
865
Willy Tarreau53a47662017-08-28 10:53:00 +0200866static inline const char *conn_get_mux_name(const struct connection *conn)
867{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200868 if (!conn || !conn->mux)
Willy Tarreau53a47662017-08-28 10:53:00 +0200869 return "NONE";
870 return conn->mux->name;
871}
872
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200873static inline const char *cs_get_data_name(const struct conn_stream *cs)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100874{
Willy Tarreau5e6a5b32019-04-25 18:35:49 +0200875 if (!cs || !cs->data_cb)
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100876 return "NONE";
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200877 return cs->data_cb->name;
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100878}
879
Willy Tarreau13e14102016-12-22 20:25:26 +0100880/* registers pointer to transport layer <id> (XPRT_*) */
881static inline void xprt_register(int id, struct xprt_ops *xprt)
882{
883 if (id >= XPRT_ENTRIES)
884 return;
885 registered_xprt[id] = xprt;
886}
887
888/* returns pointer to transport layer <id> (XPRT_*) or NULL if not registered */
889static inline struct xprt_ops *xprt_get(int id)
890{
891 if (id >= XPRT_ENTRIES)
892 return NULL;
893 return registered_xprt[id];
894}
Willy Tarreau2dc770c2016-11-23 18:00:08 +0100895
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200896/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
897 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
898 * call the xprt's add_xprt() method.
899 * Returns 0 on success, or non-zero on failure.
900 */
901static inline int xprt_add_hs(struct connection *conn)
902{
903 void *xprt_ctx = NULL;
904 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
905 void *nextxprt_ctx = NULL;
906 const struct xprt_ops *nextxprt_ops = NULL;
907
908 if (conn->flags & CO_FL_ERROR)
909 return -1;
910 if (ops->init(conn, &xprt_ctx) < 0)
911 return -1;
912 if (conn->xprt == xprt_get(XPRT_RAW)) {
913 nextxprt_ctx = conn->xprt_ctx;
914 nextxprt_ops = conn->xprt;
915 conn->xprt_ctx = xprt_ctx;
916 conn->xprt = ops;
917 } else {
918 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
919 &nextxprt_ctx, &nextxprt_ops) != 0) {
920 ops->close(conn, xprt_ctx);
921 return -1;
922 }
923 }
924 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
925 ops->close(conn, xprt_ctx);
926 return -1;
927 }
928 return 0;
929}
930
Willy Tarreau4f8cd432021-03-02 17:27:58 +0100931/* notify the next xprt that the connection is about to become idle and that it
932 * may be stolen at any time after the function returns and that any tasklet in
933 * the chain must be careful before dereferencing its context.
934 */
935static inline void xprt_set_idle(struct connection *conn, const struct xprt_ops *xprt, void *xprt_ctx)
936{
937 if (xprt->set_idle)
938 xprt->set_idle(conn, conn->xprt_ctx);
939}
940
941/* notify the next xprt that the connection is not idle anymore and that it may
942 * not be stolen before the next xprt_set_idle().
943 */
944static inline void xprt_set_used(struct connection *conn, const struct xprt_ops *xprt, void *xprt_ctx)
945{
946 if (xprt->set_used)
947 xprt->set_used(conn, conn->xprt_ctx);
948}
949
Willy Tarreaua9c17412016-12-04 18:42:09 +0100950static inline int conn_get_alpn(const struct connection *conn, const char **str, int *len)
951{
952 if (!conn_xprt_ready(conn) || !conn->xprt->get_alpn)
953 return 0;
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100954 return conn->xprt->get_alpn(conn, conn->xprt_ctx, str, len);
Willy Tarreaua9c17412016-12-04 18:42:09 +0100955}
956
Christopher Faulet32f61c02018-04-10 14:33:41 +0200957/* registers proto mux list <list>. Modifies the list element! */
958static inline void register_mux_proto(struct mux_proto_list *list)
Willy Tarreau2386be62017-09-21 19:40:52 +0200959{
Willy Tarreau2b718102021-04-21 07:32:39 +0200960 LIST_APPEND(&mux_proto_list.list, &list->list);
Willy Tarreau2386be62017-09-21 19:40:52 +0200961}
962
Christopher Faulet32f61c02018-04-10 14:33:41 +0200963/* unregisters proto mux list <list> */
964static inline void unregister_mux_proto(struct mux_proto_list *list)
Willy Tarreau2386be62017-09-21 19:40:52 +0200965{
Willy Tarreau2b718102021-04-21 07:32:39 +0200966 LIST_DELETE(&list->list);
Willy Tarreau2386be62017-09-21 19:40:52 +0200967 LIST_INIT(&list->list);
968}
969
Christopher Faulete15c6c42018-08-08 10:21:56 +0200970static inline struct mux_proto_list *get_mux_proto(const struct ist proto)
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200971{
972 struct mux_proto_list *item;
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200973
974 list_for_each_entry(item, &mux_proto_list.list, list) {
975 if (isteq(proto, item->token))
976 return item;
977 }
978 return NULL;
979}
980
Willy Tarreaue59b5162021-05-08 14:06:09 +0200981void list_mux_proto(FILE *out);
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100982/* returns the first mux entry in the list matching the exact same <mux_proto>
983 * and compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200984 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
985 * with exactly the same <proto_mode> or with an empty name. May return
986 * null if the code improperly registered the default mux to use as a fallback.
Willy Tarreau2386be62017-09-21 19:40:52 +0200987 */
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100988static inline const struct mux_proto_list *conn_get_best_mux_entry(
989 const struct ist mux_proto,
990 int proto_side, int proto_mode)
Willy Tarreau2386be62017-09-21 19:40:52 +0200991{
Christopher Faulet32f61c02018-04-10 14:33:41 +0200992 struct mux_proto_list *item;
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200993 struct mux_proto_list *fallback = NULL;
Willy Tarreau2386be62017-09-21 19:40:52 +0200994
Christopher Faulet32f61c02018-04-10 14:33:41 +0200995 list_for_each_entry(item, &mux_proto_list.list, list) {
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200996 if (!(item->side & proto_side) || !(item->mode & proto_mode))
Willy Tarreau2386be62017-09-21 19:40:52 +0200997 continue;
Christopher Faulet7ce0c892018-04-10 15:01:45 +0200998 if (istlen(mux_proto) && isteq(mux_proto, item->token))
Willy Tarreau5fc311c2018-12-02 13:04:43 +0100999 return item;
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001000 else if (!istlen(item->token)) {
1001 if (!fallback || (item->mode == proto_mode && fallback->mode != proto_mode))
1002 fallback = item;
1003 }
Willy Tarreau2386be62017-09-21 19:40:52 +02001004 }
Willy Tarreau5fc311c2018-12-02 13:04:43 +01001005 return fallback;
1006
1007}
1008
1009/* returns the first mux in the list matching the exact same <mux_proto> and
1010 * compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
1011 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
1012 * with exactly the same <proto_mode> or with an empty name. May return
1013 * null if the code improperly registered the default mux to use as a fallback.
1014 */
1015static inline const struct mux_ops *conn_get_best_mux(struct connection *conn,
1016 const struct ist mux_proto,
1017 int proto_side, int proto_mode)
1018{
1019 const struct mux_proto_list *item;
1020
1021 item = conn_get_best_mux_entry(mux_proto, proto_side, proto_mode);
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001022
Willy Tarreau5fc311c2018-12-02 13:04:43 +01001023 return item ? item->mux : NULL;
Willy Tarreau2386be62017-09-21 19:40:52 +02001024}
1025
Willy Tarreau6ac98ac2018-09-06 11:48:44 +02001026/* returns a pointer to the proxy associated with this connection. For a front
1027 * connection it returns a pointer to the frontend ; for a back connection, it
1028 * returns a pointer to the backend.
1029 */
1030static inline struct proxy *conn_get_proxy(const struct connection *conn)
1031{
1032 struct listener *l;
1033 struct server *s;
1034
1035 /* check if it's a frontend connection */
1036 l = objt_listener(conn->target);
1037 if (l)
1038 return l->bind_conf->frontend;
1039
1040 /* check if it's a backend connection */
1041 s = objt_server(conn->target);
1042 if (s)
1043 return s->proxy;
1044
1045 return objt_proxy(conn->target);
1046}
1047
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001048/* installs the best mux for incoming connection <conn> using the upper context
1049 * <ctx>. If the mux protocol is forced, we use it to find the best
1050 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
Willy Tarreau2386be62017-09-21 19:40:52 +02001051 */
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001052static inline int conn_install_mux_fe(struct connection *conn, void *ctx)
Willy Tarreau2386be62017-09-21 19:40:52 +02001053{
Willy Tarreau55e0da62018-09-20 11:26:52 +02001054 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001055 const struct mux_ops *mux_ops;
Willy Tarreau2386be62017-09-21 19:40:52 +02001056
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001057 if (bind_conf->mux_proto)
1058 mux_ops = bind_conf->mux_proto->mux;
1059 else {
1060 struct ist mux_proto;
1061 const char *alpn_str = NULL;
1062 int alpn_len = 0;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001063 int mode;
1064
Christopher Fauletc1918d12019-04-24 15:01:22 +02001065 if (bind_conf->frontend->mode == PR_MODE_HTTP)
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001066 mode = PROTO_MODE_HTTP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001067 else
Christopher Fauletc1918d12019-04-24 15:01:22 +02001068 mode = PROTO_MODE_TCP;
Willy Tarreau2386be62017-09-21 19:40:52 +02001069
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001070 conn_get_alpn(conn, &alpn_str, &alpn_len);
1071 mux_proto = ist2(alpn_str, alpn_len);
1072 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
1073 if (!mux_ops)
1074 return -1;
1075 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01001076 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
Willy Tarreau2386be62017-09-21 19:40:52 +02001077}
1078
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001079/* installs the best mux for outgoing connection <conn> using the upper context
1080 * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns
Willy Tarreau2e0b2b52017-09-15 06:59:55 +02001081 * < 0 on error.
Willy Tarreau2386be62017-09-21 19:40:52 +02001082 */
Amaury Denoyelle60378872021-10-28 16:36:11 +02001083static inline int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess,
1084 const struct mux_ops *force_mux_ops)
Willy Tarreau2386be62017-09-21 19:40:52 +02001085{
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001086 struct server *srv = objt_server(conn->target);
Willy Tarreau287527a2018-08-08 18:40:44 +02001087 struct proxy *prx = objt_proxy(conn->target);
Willy Tarreau2386be62017-09-21 19:40:52 +02001088 const struct mux_ops *mux_ops;
1089
Willy Tarreau287527a2018-08-08 18:40:44 +02001090 if (srv)
1091 prx = srv->proxy;
1092
1093 if (!prx) // target must be either proxy or server
1094 return -1;
1095
Amaury Denoyelle60378872021-10-28 16:36:11 +02001096 if (srv && srv->mux_proto && likely(!force_mux_ops)) {
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001097 mux_ops = srv->mux_proto->mux;
Amaury Denoyelle60378872021-10-28 16:36:11 +02001098 }
1099 else if (srv && unlikely(force_mux_ops)) {
1100 mux_ops = force_mux_ops;
1101 }
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001102 else {
Olivier Houchard201b9f42018-11-21 00:16:29 +01001103 struct ist mux_proto;
1104 const char *alpn_str = NULL;
1105 int alpn_len = 0;
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001106 int mode;
1107
Christopher Fauletc1918d12019-04-24 15:01:22 +02001108 if (prx->mode == PR_MODE_HTTP)
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001109 mode = PROTO_MODE_HTTP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001110 else
Christopher Fauletc1918d12019-04-24 15:01:22 +02001111 mode = PROTO_MODE_TCP;
Willy Tarreau68ad3a42018-10-22 11:49:15 +02001112
Olivier Houchard201b9f42018-11-21 00:16:29 +01001113 conn_get_alpn(conn, &alpn_str, &alpn_len);
1114 mux_proto = ist2(alpn_str, alpn_len);
1115
1116 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
Christopher Faulet7ce0c892018-04-10 15:01:45 +02001117 if (!mux_ops)
1118 return -1;
1119 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01001120 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
Willy Tarreau2386be62017-09-21 19:40:52 +02001121}
1122
Christopher Fauleta9e1c4c2020-04-21 12:18:05 +02001123/* installs the best mux for outgoing connection <conn> for a check using the
1124 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
1125 * find the best mux. Returns < 0 on error.
1126 */
1127static inline int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
1128{
1129 struct check *check = objt_check(sess->origin);
1130 struct server *srv = objt_server(conn->target);
1131 struct proxy *prx = objt_proxy(conn->target);
1132 const struct mux_ops *mux_ops;
1133
1134 if (!check) // Check must be defined
1135 return -1;
1136
1137 if (srv)
1138 prx = srv->proxy;
1139
1140 if (!prx) // target must be either proxy or server
1141 return -1;
1142
1143 if (check->mux_proto)
1144 mux_ops = check->mux_proto->mux;
1145 else {
1146 struct ist mux_proto;
1147 const char *alpn_str = NULL;
1148 int alpn_len = 0;
1149 int mode;
1150
1151 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
1152 mode = PROTO_MODE_HTTP;
1153 else
1154 mode = PROTO_MODE_TCP;
1155
1156 conn_get_alpn(conn, &alpn_str, &alpn_len);
1157 mux_proto = ist2(alpn_str, alpn_len);
1158
1159 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
1160 if (!mux_ops)
1161 return -1;
1162 }
1163 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
1164}
1165
Olivier Houchard2ab3dad2019-07-03 13:08:18 +02001166/* Change the mux for the connection.
1167 * The caller should make sure he's not subscribed to the underlying XPRT.
1168 */
Christopher Fauletc0016d82019-04-08 10:42:41 +02001169static inline int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
1170 struct ist mux_proto, int mode)
1171{
1172 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
1173 const struct mux_ops *old_mux, *new_mux;
1174 void *old_mux_ctx;
1175 const char *alpn_str = NULL;
1176 int alpn_len = 0;
1177
1178 if (!mux_proto.len) {
1179 conn_get_alpn(conn, &alpn_str, &alpn_len);
1180 mux_proto = ist2(alpn_str, alpn_len);
1181 }
1182 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
1183 old_mux = conn->mux;
1184
1185 /* No mux found */
1186 if (!new_mux)
1187 return -1;
1188
1189 /* Same mux, nothing to do */
1190 if (old_mux == new_mux)
1191 return 0;
1192
1193 old_mux_ctx = conn->ctx;
1194 conn->mux = new_mux;
1195 conn->ctx = ctx;
Christopher Fauletc0016d82019-04-08 10:42:41 +02001196 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
1197 /* The mux upgrade failed, so restore the old mux */
1198 conn->ctx = old_mux_ctx;
1199 conn->mux = old_mux;
1200 return -1;
1201 }
1202
1203 /* The mux was upgraded, destroy the old one */
1204 *buf = BUF_NULL;
1205 old_mux->destroy(old_mux_ctx);
1206 return 0;
1207}
1208
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001209/* Generate the hash of a connection with params as input
1210 * Each non-null field of params is taken into account for the hash calcul.
1211 */
1212XXH64_hash_t conn_calculate_hash(const struct conn_hash_params *params);
1213
1214static inline XXH64_hash_t conn_hash_prehash(char *buf, size_t size)
1215{
1216 return XXH64(buf, size, 0);
1217}
1218
1219/* Append <data> into <buf> at <idx> offset in preparation for connection hash
1220 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
1221 * are updated with <type> for the hash header.
1222 */
1223static inline void conn_hash_update(char *buf, size_t *idx,
1224 const void *data, size_t size,
1225 enum conn_hash_params_t *flags,
1226 enum conn_hash_params_t type)
1227{
1228 memcpy(&buf[*idx], data, size);
1229 *idx += size;
1230 *flags |= type;
1231}
1232
1233static inline XXH64_hash_t conn_hash_digest(char *buf, size_t bufsize,
1234 enum conn_hash_params_t flags)
1235{
1236 const uint64_t flags_u64 = (uint64_t)flags;
1237 const XXH64_hash_t hash = XXH64(buf, bufsize, 0);
1238
1239 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
1240}
1241
Willy Tarreau7ea393d2020-06-04 18:02:10 +02001242#endif /* _HAPROXY_CONNECTION_H */
Willy Tarreau59f98392012-07-06 14:13:49 +02001243
1244/*
1245 * Local variables:
1246 * c-indent-level: 8
1247 * c-basic-offset: 8
1248 * End:
1249 */