blob: ffe55950468095497aead3a908f9b76750a40ce2 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * Connection management functions
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaue1e4a612012-10-05 00:10:55 +020013#include <errno.h>
14
Willy Tarreau930428c2021-10-06 18:27:28 +020015#include <import/ebmbtree.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020019#include <haproxy/connection.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020021#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/hash.h>
Willy Tarreauaac777f2021-10-06 18:48:28 +020023#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/log-t.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020025#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020026#include <haproxy/net_helper.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020027#include <haproxy/proto_tcp.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020028#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020029#include <haproxy/sc_strm.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010030#include <haproxy/session.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010031#include <haproxy/ssl_sock.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020032#include <haproxy/stconn.h>
Willy Tarreau908908e2021-05-08 13:07:31 +020033#include <haproxy/tools.h>
Willy Tarreaue5983ff2021-10-06 17:14:49 +020034#include <haproxy/xxhash.h>
Emeric Brun46591952012-05-18 15:47:34 +020035
Alexander Liu2a54bb72019-05-22 19:44:48 +080036
Amaury Denoyelle8990b012021-02-19 15:29:16 +010037DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
Amaury Denoyelle8990b012021-02-19 15:29:16 +010038DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node));
39DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
40DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010041
Willy Tarreau4d82bf52020-06-28 00:19:17 +020042struct idle_conns idle_conns[MAX_THREADS] = { };
Willy Tarreau13e14102016-12-22 20:25:26 +010043struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020044
Christopher Faulet32f61c02018-04-10 14:33:41 +020045/* List head of all known muxes for PROTO */
46struct mux_proto_list mux_proto_list = {
47 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020048};
49
Amaury Denoyelled3a88c12021-05-03 10:47:51 +020050struct mux_stopping_data mux_stopping_data[MAX_THREADS];
51
Willy Tarreau119e50e2020-05-22 13:53:29 +020052/* disables sending of proxy-protocol-v2's LOCAL command */
53static int pp2_never_send_local;
54
Willy Tarreau85223482022-09-29 20:32:43 +020055void conn_delete_from_tree(struct eb64_node *node)
Willy Tarreau930428c2021-10-06 18:27:28 +020056{
Willy Tarreau85223482022-09-29 20:32:43 +020057 eb64_delete(node);
Willy Tarreau930428c2021-10-06 18:27:28 +020058}
59
Olivier Houchard477902b2020-01-22 18:08:48 +010060int conn_create_mux(struct connection *conn)
61{
Olivier Houchard477902b2020-01-22 18:08:48 +010062 if (conn_is_back(conn)) {
63 struct server *srv;
Willy Tarreau61f56752022-05-27 10:00:13 +020064 struct stconn *sc = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020065 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010066
67 if (conn->flags & CO_FL_ERROR)
68 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010069
Christopher Faulet14cd3162020-04-16 14:50:06 +020070 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010071 if (conn_install_mux_chk(conn, conn->ctx, sess) < 0)
Christopher Faulet14cd3162020-04-16 14:50:06 +020072 goto fail;
73 }
Amaury Denoyelleac03ef22021-10-28 16:36:11 +020074 else if (conn_install_mux_be(conn, conn->ctx, sess, NULL) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010075 goto fail;
76 srv = objt_server(conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020077
78 /* If we're doing http-reuse always, and the connection is not
79 * private with available streams (an http2 connection), add it
80 * to the available list, so that others can use it right
81 * away. If the connection is private, add it in the session
82 * server list.
83 */
Christopher Faulet2883fcf2020-07-01 14:59:43 +020084 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) &&
Christopher Fauletaa278532020-06-30 14:47:46 +020085 !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0)
Willy Tarreau85223482022-09-29 20:32:43 +020086 eb64_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node);
Christopher Faulet08016ab2020-07-01 16:10:06 +020087 else if (conn->flags & CO_FL_PRIVATE) {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +050088 /* If it fail now, the same will be done in mux->detach() callback */
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010089 session_add_conn(sess, conn, conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020090 }
Olivier Houchard477902b2020-01-22 18:08:48 +010091 return 0;
92fail:
93 /* let the upper layer know the connection failed */
Willy Tarreau61f56752022-05-27 10:00:13 +020094 sc->app_ops->wake(sc);
Olivier Houchard477902b2020-01-22 18:08:48 +010095 return -1;
96 } else
97 return conn_complete_session(conn);
98
Willy Tarreau930428c2021-10-06 18:27:28 +020099}
100
101/* This is used at the end of the socket IOCB to possibly create the mux if it
102 * was not done yet, or wake it up if flags changed compared to old_flags or if
103 * need_wake insists on this. It returns <0 if the connection was destroyed and
104 * must not be used, >=0 otherwise.
105 */
106int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake)
107{
108 int ret = 0;
109
110 /* If we don't yet have a mux, that means we were waiting for
111 * information to create one, typically from the ALPN. If we're
112 * done with the handshake, attempt to create one.
113 */
114 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) {
115 ret = conn_create_mux(conn);
116 if (ret < 0)
117 goto done;
118 }
119
120 /* The wake callback is normally used to notify the data layer about
121 * data layer activity (successful send/recv), connection establishment,
122 * shutdown and fatal errors. We need to consider the following
123 * situations to wake up the data layer :
124 * - change among the CO_FL_NOTIFY_DONE flags :
125 * SOCK_{RD,WR}_SH, ERROR,
126 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
127 * end of handshake and transition to CONNECTED
128 * - raise of CONNECTED with HANDSHAKE down
129 * - end of HANDSHAKE with CONNECTED set
130 * - regular data layer activity
131 *
132 * One tricky case is the wake up on read0 or error on an idle
133 * backend connection, that can happen on a connection that is still
134 * polled while at the same moment another thread is about to perform a
135 * takeover. The solution against this is to remove the connection from
136 * the idle list if it was in it, and possibly reinsert it at the end
137 * if the connection remains valid. The cost is non-null (locked tree
138 * removal) but remains low given that this is extremely rarely called.
139 * In any case it's guaranteed by the FD's thread_mask that we're
140 * called from the same thread the connection is queued in.
141 *
142 * Note that the wake callback is allowed to release the connection and
143 * the fd (and return < 0 in this case).
144 */
145 if ((forced_wake ||
146 ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) ||
147 ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
148 conn->mux && conn->mux->wake) {
Christopher Faulet3a7b5392023-03-16 11:43:05 +0100149 uint conn_in_list = conn_get_idle_flag(conn);
Willy Tarreau930428c2021-10-06 18:27:28 +0200150 struct server *srv = objt_server(conn->target);
151
152 if (conn_in_list) {
153 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
154 conn_delete_from_tree(&conn->hash_node->node);
155 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
156 }
157
158 ret = conn->mux->wake(conn);
159 if (ret < 0)
160 goto done;
161
162 if (conn_in_list) {
163 struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ?
164 &srv->per_thr[tid].safe_conns :
165 &srv->per_thr[tid].idle_conns;
166
167 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau85223482022-09-29 20:32:43 +0200168 eb64_insert(root, &conn->hash_node->node);
Willy Tarreau930428c2021-10-06 18:27:28 +0200169 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
170 }
171 }
172 done:
173 return ret;
Olivier Houchard477902b2020-01-22 18:08:48 +0100174}
175
Willy Tarreauaac777f2021-10-06 18:48:28 +0200176/* Change the mux for the connection.
177 * The caller should make sure he's not subscribed to the underlying XPRT.
178 */
179int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
180 struct ist mux_proto, int mode)
181{
182 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
183 const struct mux_ops *old_mux, *new_mux;
184 void *old_mux_ctx;
185 const char *alpn_str = NULL;
186 int alpn_len = 0;
187
188 if (!mux_proto.len) {
189 conn_get_alpn(conn, &alpn_str, &alpn_len);
190 mux_proto = ist2(alpn_str, alpn_len);
191 }
192 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
193 old_mux = conn->mux;
194
195 /* No mux found */
196 if (!new_mux)
197 return -1;
198
199 /* Same mux, nothing to do */
200 if (old_mux == new_mux)
201 return 0;
202
203 old_mux_ctx = conn->ctx;
204 conn->mux = new_mux;
205 conn->ctx = ctx;
206 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
207 /* The mux upgrade failed, so restore the old mux */
208 conn->ctx = old_mux_ctx;
209 conn->mux = old_mux;
210 return -1;
211 }
212
213 /* The mux was upgraded, destroy the old one */
214 *buf = BUF_NULL;
215 old_mux->destroy(old_mux_ctx);
216 return 0;
217}
218
219/* installs the best mux for incoming connection <conn> using the upper context
220 * <ctx>. If the mux protocol is forced, we use it to find the best
221 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
222 */
223int conn_install_mux_fe(struct connection *conn, void *ctx)
224{
225 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
226 const struct mux_ops *mux_ops;
227
228 if (bind_conf->mux_proto)
229 mux_ops = bind_conf->mux_proto->mux;
230 else {
231 struct ist mux_proto;
232 const char *alpn_str = NULL;
233 int alpn_len = 0;
234 int mode;
235
236 if (bind_conf->frontend->mode == PR_MODE_HTTP)
237 mode = PROTO_MODE_HTTP;
238 else
239 mode = PROTO_MODE_TCP;
240
241 conn_get_alpn(conn, &alpn_str, &alpn_len);
242 mux_proto = ist2(alpn_str, alpn_len);
243 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
244 if (!mux_ops)
245 return -1;
246 }
247 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
248}
249
250/* installs the best mux for outgoing connection <conn> using the upper context
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200251 * <ctx>. If the server mux protocol is forced, we use it to find the best mux.
252 * It's also possible to specify an alternative mux protocol <force_mux_ops>,
253 * in which case it will be used instead of the default server mux protocol.
254 *
255 * Returns < 0 on error.
Willy Tarreauaac777f2021-10-06 18:48:28 +0200256 */
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200257int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess,
258 const struct mux_ops *force_mux_ops)
Willy Tarreauaac777f2021-10-06 18:48:28 +0200259{
260 struct server *srv = objt_server(conn->target);
261 struct proxy *prx = objt_proxy(conn->target);
262 const struct mux_ops *mux_ops;
263
264 if (srv)
265 prx = srv->proxy;
266
267 if (!prx) // target must be either proxy or server
268 return -1;
269
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200270 if (srv && srv->mux_proto && likely(!force_mux_ops)) {
Willy Tarreauaac777f2021-10-06 18:48:28 +0200271 mux_ops = srv->mux_proto->mux;
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200272 }
273 else if (srv && unlikely(force_mux_ops)) {
274 mux_ops = force_mux_ops;
275 }
Willy Tarreauaac777f2021-10-06 18:48:28 +0200276 else {
277 struct ist mux_proto;
278 const char *alpn_str = NULL;
279 int alpn_len = 0;
280 int mode;
281
282 if (prx->mode == PR_MODE_HTTP)
283 mode = PROTO_MODE_HTTP;
284 else
285 mode = PROTO_MODE_TCP;
286
287 conn_get_alpn(conn, &alpn_str, &alpn_len);
288 mux_proto = ist2(alpn_str, alpn_len);
289
290 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
291 if (!mux_ops)
292 return -1;
293 }
294 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
295}
296
297/* installs the best mux for outgoing connection <conn> for a check using the
298 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
299 * find the best mux. Returns < 0 on error.
300 */
301int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
302{
303 struct check *check = objt_check(sess->origin);
304 struct server *srv = objt_server(conn->target);
305 struct proxy *prx = objt_proxy(conn->target);
306 const struct mux_ops *mux_ops;
307
308 if (!check) // Check must be defined
309 return -1;
310
311 if (srv)
312 prx = srv->proxy;
313
314 if (!prx) // target must be either proxy or server
315 return -1;
316
317 if (check->mux_proto)
318 mux_ops = check->mux_proto->mux;
319 else {
320 struct ist mux_proto;
321 const char *alpn_str = NULL;
322 int alpn_len = 0;
323 int mode;
324
325 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
326 mode = PROTO_MODE_HTTP;
327 else
328 mode = PROTO_MODE_TCP;
329
330 conn_get_alpn(conn, &alpn_str, &alpn_len);
331 mux_proto = ist2(alpn_str, alpn_len);
332
333 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
334 if (!mux_ops)
335 return -1;
336 }
337 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
338}
339
Amaury Denoyelle2454bda2021-10-18 14:32:36 +0200340/* Set the ALPN of connection <conn> to <alpn>. If force is false, <alpn> must
341 * be a subset or identical to the registered protos for the parent SSL_CTX.
342 * In this case <alpn> must be a single protocol value, not a list.
343 *
344 * Returns 0 if ALPN is updated else -1.
345 */
346int conn_update_alpn(struct connection *conn, const struct ist alpn, int force)
347{
348#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
349 size_t alpn_len = istlen(alpn);
350 char *ctx_alpn_str = NULL;
351 int ctx_alpn_len = 0, found = 0;
352
353 /* if not force, first search if alpn is a subset or identical to the
354 * parent SSL_CTX.
355 */
356 if (!force) {
357 /* retrieve the SSL_CTX according to the connection side. */
358 if (conn_is_back(conn)) {
359 if (obj_type(conn->target) == OBJ_TYPE_SERVER) {
360 struct server *srv = __objt_server(conn->target);
361 ctx_alpn_str = srv->ssl_ctx.alpn_str;
362 ctx_alpn_len = srv->ssl_ctx.alpn_len;
363 }
364 }
365 else {
366 struct session *sess = conn->owner;
367 struct listener *li = sess->listener;
368
Willy Tarreau11ba4042022-05-20 15:56:32 +0200369 if (li->bind_conf && li->bind_conf->options & BC_O_USE_SSL) {
Amaury Denoyelle2454bda2021-10-18 14:32:36 +0200370 ctx_alpn_str = li->bind_conf->ssl_conf.alpn_str;
371 ctx_alpn_len = li->bind_conf->ssl_conf.alpn_len;
372 }
373 }
374
375 if (ctx_alpn_str) {
376 /* search if ALPN is present in SSL_CTX ALPN before
377 * using it.
378 */
379 while (ctx_alpn_len) {
380 /* skip ALPN whose size is not 8 */
381 if (*ctx_alpn_str != alpn_len - 1) {
382 ctx_alpn_len -= *ctx_alpn_str + 1;
383 }
384 else {
385 if (isteqi(ist2(ctx_alpn_str, alpn_len), alpn)) {
386 found = 1;
387 break;
388 }
389 }
390 ctx_alpn_str += *ctx_alpn_str + 1;
391
392 /* This indicates an invalid ALPN formatted
393 * string and should never happen. */
394 BUG_ON(ctx_alpn_len < 0);
395 }
396 }
397 }
398
399 if (found || force) {
400 ssl_sock_set_alpn(conn, (const uchar *)istptr(alpn), istlen(alpn));
401 return 0;
402 }
403
404#endif
405 return -1;
406}
407
Willy Tarreauaac777f2021-10-06 18:48:28 +0200408/* Initializes all required fields for a new connection. Note that it does the
409 * minimum acceptable initialization for a connection that already exists and
410 * is about to be reused. It also leaves the addresses untouched, which makes
411 * it usable across connection retries to reset a connection to a known state.
412 */
413void conn_init(struct connection *conn, void *target)
414{
415 conn->obj_type = OBJ_TYPE_CONN;
416 conn->flags = CO_FL_NONE;
417 conn->mux = NULL;
418 conn->ctx = NULL;
419 conn->owner = NULL;
420 conn->send_proxy_ofs = 0;
421 conn->handle.fd = DEAD_FD_MAGIC;
422 conn->err_code = CO_ER_NONE;
423 conn->target = target;
424 conn->destroy_cb = NULL;
425 conn->proxy_netns = NULL;
426 MT_LIST_INIT(&conn->toremove_list);
427 if (conn_is_back(conn))
Amaury Denoyelle926a5612024-03-14 11:24:10 +0100428 LIST_INIT(&conn->sess_el);
Willy Tarreauaac777f2021-10-06 18:48:28 +0200429 else
430 LIST_INIT(&conn->stopping_list);
431 conn->subs = NULL;
432 conn->src = NULL;
433 conn->dst = NULL;
434 conn->proxy_authority = IST_NULL;
435 conn->proxy_unique_id = IST_NULL;
Willy Tarreauaac777f2021-10-06 18:48:28 +0200436 conn->hash_node = NULL;
437 conn->xprt = NULL;
438}
439
440/* Tries to allocate a new connection and initialized its main fields. The
441 * connection is returned on success, NULL on failure. The connection must
442 * be released using pool_free() or conn_free().
443 */
444struct connection *conn_new(void *target)
445{
446 struct connection *conn;
447 struct conn_hash_node *hash_node;
448
449 conn = pool_alloc(pool_head_connection);
450 if (unlikely(!conn))
451 return NULL;
452
453 conn_init(conn, target);
454
455 if (conn_is_back(conn)) {
456 if (obj_type(target) == OBJ_TYPE_SERVER)
457 srv_use_conn(__objt_server(target), conn);
458
459 hash_node = conn_alloc_hash_node(conn);
460 if (unlikely(!hash_node)) {
461 pool_free(pool_head_connection, conn);
462 return NULL;
463 }
464
465 conn->hash_node = hash_node;
466 }
467
468 return conn;
469}
470
471/* Releases a connection previously allocated by conn_new() */
472void conn_free(struct connection *conn)
473{
474 /* If the connection is owned by the session, remove it from its list
475 */
Amaury Denoyelle926a5612024-03-14 11:24:10 +0100476 if (conn_is_back(conn) && LIST_INLIST(&conn->sess_el)) {
Willy Tarreauaac777f2021-10-06 18:48:28 +0200477 session_unown_conn(conn->owner, conn);
478 }
479 else if (!(conn->flags & CO_FL_PRIVATE)) {
480 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
481 srv_release_conn(__objt_server(conn->target), conn);
482 }
483
484 /* Remove the conn from toremove_list.
485 *
486 * This is needed to prevent a double-free in case the connection was
487 * already scheduled from cleaning but is freed before via another
488 * call.
489 */
490 MT_LIST_DELETE(&conn->toremove_list);
491
492 sockaddr_free(&conn->src);
493 sockaddr_free(&conn->dst);
494
495 pool_free(pool_head_authority, istptr(conn->proxy_authority));
496 conn->proxy_authority = IST_NULL;
497
498 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
499 conn->proxy_unique_id = IST_NULL;
500
Olivier Houchard9a0f8ba2023-01-24 23:59:32 +0100501 /* Make sure the connection is not left in the idle connection tree */
502 if (conn->hash_node != NULL)
503 BUG_ON(conn->hash_node->node.node.leaf_p != NULL);
504
Willy Tarreauaac777f2021-10-06 18:48:28 +0200505 pool_free(pool_head_conn_hash_node, conn->hash_node);
506 conn->hash_node = NULL;
507
Willy Tarreauaac777f2021-10-06 18:48:28 +0200508 conn_force_unsubscribe(conn);
509 pool_free(pool_head_connection, conn);
510}
511
Willy Tarreau8de90c72021-10-06 19:11:10 +0200512struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
513{
514 struct conn_hash_node *hash_node = NULL;
515
516 hash_node = pool_zalloc(pool_head_conn_hash_node);
517 if (unlikely(!hash_node))
518 return NULL;
519
520 hash_node->conn = conn;
521
522 return hash_node;
523}
524
525/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
526 * returns it. If <sap> is NULL, the address is always allocated and returned.
527 * if <sap> is non-null, an address will only be allocated if it points to a
528 * non-null pointer. In this case the allocated address will be assigned there.
529 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
530 * into the allocated address. In both situations the new pointer is returned.
531 */
532struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
533{
534 struct sockaddr_storage *sa;
535
536 if (sap && *sap)
537 return *sap;
538
539 sa = pool_alloc(pool_head_sockaddr);
540 if (sa && orig && len > 0)
541 memcpy(sa, orig, len);
542 if (sap)
543 *sap = sa;
544 return sa;
545}
546
547/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
548 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
549 * there.
550 */
551void sockaddr_free(struct sockaddr_storage **sap)
552{
553 if (!sap)
554 return;
555 pool_free(pool_head_sockaddr, *sap);
556 *sap = NULL;
557}
558
Willy Tarreauaac777f2021-10-06 18:48:28 +0200559/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
560 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
561 * call the xprt's add_xprt() method.
562 * Returns 0 on success, or non-zero on failure.
563 */
564int xprt_add_hs(struct connection *conn)
565{
566 void *xprt_ctx = NULL;
567 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
568 void *nextxprt_ctx = NULL;
569 const struct xprt_ops *nextxprt_ops = NULL;
570
571 if (conn->flags & CO_FL_ERROR)
572 return -1;
573 if (ops->init(conn, &xprt_ctx) < 0)
574 return -1;
575 if (conn->xprt == xprt_get(XPRT_RAW)) {
576 nextxprt_ctx = conn->xprt_ctx;
577 nextxprt_ops = conn->xprt;
578 conn->xprt_ctx = xprt_ctx;
579 conn->xprt = ops;
580 } else {
581 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
582 &nextxprt_ctx, &nextxprt_ops) != 0) {
583 ops->close(conn, xprt_ctx);
584 return -1;
585 }
586 }
587 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
588 ops->close(conn, xprt_ctx);
589 return -1;
590 }
591 return 0;
592}
593
594/* returns a human-readable error code for conn->err_code, or NULL if the code
595 * is unknown.
596 */
597const char *conn_err_code_str(struct connection *c)
598{
599 switch (c->err_code) {
600 case CO_ER_NONE: return "Success";
601
602 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
603 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
604 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
605 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
606 case CO_ER_NOPROTO: return "Protocol or address family not supported";
607 case CO_ER_SOCK_ERR: return "General socket error";
608 case CO_ER_PORT_RANGE: return "Source port range exhausted";
609 case CO_ER_CANT_BIND: return "Can't bind to source address";
610 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
611 case CO_ER_ADDR_INUSE: return "Local source address already in use";
612
613 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
614 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
615 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
616 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
617 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
618 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
619 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
620
621 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
622 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
623 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
624 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
625 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
626 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
627
628 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
629 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
630 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
631 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
632 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
633 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
634 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
635 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
636 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
637 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
638 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
639 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
640 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
641 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
642 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
643
644 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
645 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
646 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
647 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
648
649 case CO_ERR_SSL_FATAL: return "SSL fatal error";
650 }
651 return NULL;
652}
653
Willy Tarreauff3e6482015-03-12 23:56:52 +0100654/* Send a message over an established connection. It makes use of send() and
655 * returns the same return code and errno. If the socket layer is not ready yet
656 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
657 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
658 * EMSGSIZE if called with a zero length message. The purpose is to simplify
659 * some rare attempts to directly write on the socket from above the connection
660 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
661 * It automatically retries on EINTR. Other errors cause the connection to be
662 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +0100663 * first one which is the connection instead of the file descriptor. <flags>
664 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100665 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100666int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100667{
Willy Tarreau827fee72020-12-11 15:26:55 +0100668 const struct buffer buffer = b_make((char*)buf, len, 0, len);
669 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100670 int ret;
671
672 ret = -1;
673 errno = ENOTSOCK;
674
675 if (conn->flags & CO_FL_SOCK_WR_SH)
676 goto fail;
677
678 if (!conn_ctrl_ready(conn))
679 goto fail;
680
681 errno = EMSGSIZE;
682 if (!len)
683 goto fail;
684
Willy Tarreau827fee72020-12-11 15:26:55 +0100685 /* snd_buf() already takes care of updating conn->flags and handling
686 * the FD polling status.
687 */
688 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
689 if (conn->flags & CO_FL_ERROR)
690 ret = -1;
691 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100692 fail:
693 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
694 return ret;
695}
696
Willy Tarreau746b0512020-12-11 17:06:11 +0100697/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
698 * The event subscriber <es> is not allowed to change from a previous call as
699 * long as at least one event is still subscribed. The <event_type> must only
700 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100701 */
702int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200703{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100704 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100705 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100706
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100707 es->events &= ~event_type;
708 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100709 conn->subs = NULL;
710
Willy Tarreau746b0512020-12-11 17:06:11 +0100711 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
712 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100713
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200714 return 0;
715}
716
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100717/* Called from the upper layer, to subscribe <es> to events <event_type>.
718 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100719 * previous call to subscribe(). If the connection's ctrl layer is ready,
720 * the wait_event is immediately woken up and the subcription is cancelled.
721 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100722 */
723int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200724{
Willy Tarreau746b0512020-12-11 17:06:11 +0100725 int ret = 0;
726
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100727 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100728 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100729
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100730 if (conn->subs && (conn->subs->events & event_type) == event_type)
731 return 0;
732
Willy Tarreau746b0512020-12-11 17:06:11 +0100733 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
734 ret = conn->ctrl->check_events(conn, event_type);
735 if (ret)
736 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100737 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100738
739 es->events = (es->events | event_type) & ~ret;
740 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200741 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200742}
743
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100744/* Drains possibly pending incoming data on the connection and update the flags
745 * accordingly. This is used to know whether we need to disable lingering on
746 * close. Returns non-zero if it is safe to close without disabling lingering,
747 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
748 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100749 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100750int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100751{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100752 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200753
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100754 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
755 ret = 1;
756 else if (conn->ctrl->drain) {
757 ret = conn->ctrl->drain(conn);
758 if (ret)
759 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100760 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100761 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100762}
763
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100764/*
765 * Get data length from tlv
766 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100767static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100768{
769 return (src->length_hi << 8) | src->length_lo;
770}
771
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200772/* This handshake handler waits a PROXY protocol header at the beginning of the
773 * raw data stream. The header looks like this :
774 *
775 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
776 *
777 * There must be exactly one space between each field. Fields are :
778 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
779 * - SRC3 : layer 3 (eg: IP) source address in standard text form
780 * - DST3 : layer 3 (eg: IP) destination address in standard text form
781 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
782 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
783 *
784 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
785 *
786 * The header line is small and in all cases smaller than the smallest normal
787 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
788 * can safely use MSG_PEEK and avoid buffering.
789 *
790 * Once the data is fetched, the values are set in the connection's address
791 * fields, and data are removed from the socket's buffer. The function returns
792 * zero if it needs to wait for more data or if it fails, or 1 if it completed
793 * and removed itself.
794 */
795int conn_recv_proxy(struct connection *conn, int flag)
796{
Christopher Fauletc105c922021-10-25 08:17:11 +0200797 struct session *sess = conn->owner;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200798 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200799 struct proxy_hdr_v2 *hdr_v2;
800 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100801 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100802 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200803 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200804
Willy Tarreau3c728722014-01-23 13:50:42 +0100805 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200806 goto fail;
807
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200808 BUG_ON(conn->flags & CO_FL_FDLESS);
809
Willy Tarreau585744b2017-08-24 14:31:19 +0200810 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200811 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100812
Willy Tarreau157788c2020-02-11 10:08:05 +0100813 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200814 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
815 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200816 if (errno == EINTR)
817 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200818 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200819 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200820 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200821 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100822 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200823 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200824 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100825 break;
826 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200827
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200828 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100829 /* client shutdown */
830 conn->err_code = CO_ER_PRX_EMPTY;
831 goto fail;
832 }
833
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100834 conn->flags &= ~CO_FL_WAIT_L4_CONN;
835
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200836 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200837 goto missing;
838
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200839 line = trash.area;
840 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200841
842 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200843 if (strncmp(line, "PROXY ", 6) != 0)
844 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200845
846 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200848 goto missing;
849
David CARLIER42ff05e2016-03-24 09:22:36 +0000850 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200851 u32 src3, dst3, sport, dport;
852
853 line += 5;
854
855 src3 = inetaddr_host_lim_ret(line, end, &line);
856 if (line == end)
857 goto missing;
858 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100859 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200860
861 dst3 = inetaddr_host_lim_ret(line, end, &line);
862 if (line == end)
863 goto missing;
864 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100865 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200866
867 sport = read_uint((const char **)&line, end);
868 if (line == end)
869 goto missing;
870 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100871 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200872
873 dport = read_uint((const char **)&line, end);
874 if (line > end - 2)
875 goto missing;
876 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100877 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200878 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100879 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200880
Christopher Fauletc105c922021-10-25 08:17:11 +0200881 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
882 goto fail;
883
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200884 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200885 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
886 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = htonl(src3);
887 ((struct sockaddr_in *)sess->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200888
Christopher Fauletc105c922021-10-25 08:17:11 +0200889 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
890 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = htonl(dst3);
891 ((struct sockaddr_in *)sess->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200892 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000893 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200894 u32 sport, dport;
895 char *src_s;
896 char *dst_s, *sport_s, *dport_s;
897 struct in6_addr src3, dst3;
898
899 line += 5;
900
901 src_s = line;
902 dst_s = sport_s = dport_s = NULL;
903 while (1) {
904 if (line > end - 2) {
905 goto missing;
906 }
907 else if (*line == '\r') {
908 *line = 0;
909 line++;
910 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100911 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200912 break;
913 }
914
915 if (*line == ' ') {
916 *line = 0;
917 if (!dst_s)
918 dst_s = line + 1;
919 else if (!sport_s)
920 sport_s = line + 1;
921 else if (!dport_s)
922 dport_s = line + 1;
923 }
924 line++;
925 }
926
927 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100928 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200929
930 sport = read_uint((const char **)&sport_s,dport_s - 1);
931 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100932 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200933
934 dport = read_uint((const char **)&dport_s,line - 2);
935 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100936 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200937
938 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100939 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200940
941 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100942 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200943
Christopher Fauletc105c922021-10-25 08:17:11 +0200944 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
945 goto fail;
946
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200947 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200948 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
949 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, &src3, sizeof(struct in6_addr));
950 ((struct sockaddr_in6 *)sess->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200951
Christopher Fauletc105c922021-10-25 08:17:11 +0200952 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
953 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
954 ((struct sockaddr_in6 *)sess->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200955 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200956 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
957 /* This can be a UNIX socket forwarded by an haproxy upstream */
958 line += 9;
959 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200960 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200961 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100962 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200963 goto fail;
964 }
965
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200966 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200967 goto eat_header;
968
969 not_v1:
970 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200971 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200972 goto missing;
973
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200974 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200975
976 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
977 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
978 conn->err_code = CO_ER_PRX_NOT_HDR;
979 goto fail;
980 }
981
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100982 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
983 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200984 goto missing;
985
986 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
987 case 0x01: /* PROXY command */
988 switch (hdr_v2->fam) {
989 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100990 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
991 goto bad_header;
992
Christopher Fauletc105c922021-10-25 08:17:11 +0200993 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
994 goto fail;
995
996 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
997 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
998 ((struct sockaddr_in *)sess->src)->sin_port = hdr_v2->addr.ip4.src_port;
999 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1000 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
1001 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001002 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +02001003 break;
1004 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +01001005 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
1006 goto bad_header;
1007
Christopher Fauletc105c922021-10-25 08:17:11 +02001008 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1009 goto fail;
1010
1011 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1012 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
1013 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_v2->addr.ip6.src_port;
1014 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1015 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
1016 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001017 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +02001018 break;
1019 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001020
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001021 /* unsupported protocol, keep local connection address */
1022 break;
1023 case 0x00: /* LOCAL command */
1024 /* keep local connection address for LOCAL */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001025
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001026 tlv_offset = PP2_HEADER_LEN;
1027 break;
1028 default:
1029 goto bad_header; /* not a supported command */
1030 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001031
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001032 /* TLV parsing */
1033 while (tlv_offset < total_v2_len) {
1034 struct tlv *tlv_packet;
1035 struct ist tlv;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001036
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001037 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
1038 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
1039 goto bad_header;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001040
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001041 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
1042 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
1043 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001044
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001045 /* Verify that the TLV length does not exceed the total PROXYv2 length */
1046 if (tlv_offset > total_v2_len)
1047 goto bad_header;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001048
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001049 switch (tlv_packet->type) {
1050 case PP2_TYPE_CRC32C: {
1051 uint32_t n_crc32c;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001052
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001053 /* Verify that this TLV is exactly 4 bytes long */
1054 if (istlen(tlv) != 4)
1055 goto bad_header;
1056
1057 n_crc32c = read_n32(istptr(tlv));
1058 write_n32(istptr(tlv), 0); // compute with CRC==0
1059
1060 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
1061 goto bad_header;
1062 break;
1063 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001064#ifdef USE_NS
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001065 case PP2_TYPE_NETNS: {
1066 const struct netns_entry *ns;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001067
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001068 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
1069 if (ns)
1070 conn->proxy_netns = ns;
1071 break;
1072 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001073#endif
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001074 case PP2_TYPE_AUTHORITY: {
1075 if (istlen(tlv) > PP2_AUTHORITY_MAX)
1076 goto bad_header;
1077 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
1078 if (!isttest(conn->proxy_authority))
1079 goto fail;
1080 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
1081 /* This is impossible, because we verified that the TLV value fits. */
1082 my_unreachable();
1083 goto fail;
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001084 }
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001085 break;
1086 }
1087 case PP2_TYPE_UNIQUE_ID: {
1088 if (istlen(tlv) > UNIQUEID_LEN)
1089 goto bad_header;
1090 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
1091 if (!isttest(conn->proxy_unique_id))
1092 goto fail;
1093 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
1094 /* This is impossible, because we verified that the TLV value fits. */
1095 my_unreachable();
1096 goto fail;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001097 }
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001098 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001099 }
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001100 default:
1101 break;
1102 }
Willy Tarreau77992672014-06-14 11:06:17 +02001103 }
1104
Amaury Denoyelle5c6f20b2024-05-14 16:36:59 +02001105 /* Verify that the PROXYv2 header ends at a TLV boundary.
1106 * This is can not be true, because the TLV parsing already
1107 * verifies that a TLV does not exceed the total length and
1108 * also that there is space for a TLV header.
1109 */
1110 BUG_ON(tlv_offset != total_v2_len);
1111
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001112 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +02001113 goto eat_header;
1114
1115 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001116 /* remove the PROXY line from the request. For this we re-read the
1117 * exact line at once. If we don't get the exact same result, we
1118 * fail.
1119 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001120 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001121 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
1122
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001123 if (len2 < 0 && errno == EINTR)
1124 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001125 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001126 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001127 break;
1128 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001129
1130 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +01001131 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001132 return 1;
1133
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001134 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001135 return 0;
1136
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001137 missing:
1138 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1139 * we have not read anything. Otherwise we need to fail because we won't
1140 * be able to poll anymore.
1141 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001142 conn->err_code = CO_ER_PRX_TRUNCATED;
1143 goto fail;
1144
1145 bad_header:
1146 /* This is not a valid proxy protocol header */
1147 conn->err_code = CO_ER_PRX_BAD_HDR;
1148 goto fail;
1149
1150 recv_abort:
1151 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +01001152 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001153 goto fail;
1154
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001155 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001156 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001157 return 0;
1158}
1159
Christopher Fauletaa91d622022-04-01 13:22:50 +02001160/* This callback is used to send a valid PROXY protocol line to a socket being
1161 * established. It returns 0 if it fails in a fatal way or needs to poll to go
1162 * further, otherwise it returns non-zero and removes itself from the connection's
1163 * flags (the bit is provided in <flag> by the caller). It is designed to be
1164 * called by the connection handler and relies on it to commit polling changes.
1165 * Note that it can emit a PROXY line by relying on the other end's address
Willy Tarreau4596fe22022-05-17 19:07:51 +02001166 * when the connection is attached to a stream connector, or by resolving the
Christopher Fauletaa91d622022-04-01 13:22:50 +02001167 * local address otherwise (also called a LOCAL line).
1168 */
1169int conn_send_proxy(struct connection *conn, unsigned int flag)
1170{
1171 if (!conn_ctrl_ready(conn))
1172 goto out_error;
1173
1174 /* If we have a PROXY line to send, we'll use this to validate the
1175 * connection, in which case the connection is validated only once
1176 * we've sent the whole proxy line. Otherwise we use connect().
1177 */
1178 if (conn->send_proxy_ofs) {
Willy Tarreau61f56752022-05-27 10:00:13 +02001179 struct stconn *sc;
Christopher Fauletaa91d622022-04-01 13:22:50 +02001180 int ret;
1181
1182 /* If there is no mux attached to the connection, it means the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001183 * connection context is a stream connector.
Christopher Fauletaa91d622022-04-01 13:22:50 +02001184 */
Willy Tarreau61f56752022-05-27 10:00:13 +02001185 sc = conn->mux ? conn_get_first_sc(conn) : conn->ctx;
Christopher Fauletaa91d622022-04-01 13:22:50 +02001186
1187 /* The target server expects a PROXY line to be sent first.
1188 * If the send_proxy_ofs is negative, it corresponds to the
1189 * offset to start sending from then end of the proxy string
1190 * (which is recomputed every time since it's constant). If
1191 * it is positive, it means we have to send from the start.
1192 * We can only send a "normal" PROXY line when the connection
Willy Tarreau4596fe22022-05-17 19:07:51 +02001193 * is attached to a stream connector. Otherwise we can only
Christopher Fauletaa91d622022-04-01 13:22:50 +02001194 * send a LOCAL line (eg: for use with health checks).
1195 */
1196
Willy Tarreau61f56752022-05-27 10:00:13 +02001197 if (sc && sc_strm(sc)) {
Christopher Fauletaa91d622022-04-01 13:22:50 +02001198 ret = make_proxy_line(trash.area, trash.size,
1199 objt_server(conn->target),
Willy Tarreau61f56752022-05-27 10:00:13 +02001200 sc_conn(sc_opposite(sc)),
1201 __sc_strm(sc));
Christopher Fauletaa91d622022-04-01 13:22:50 +02001202 }
1203 else {
1204 /* The target server expects a LOCAL line to be sent first. Retrieving
1205 * local or remote addresses may fail until the connection is established.
1206 */
1207 if (!conn_get_src(conn) || !conn_get_dst(conn))
1208 goto out_wait;
1209
1210 ret = make_proxy_line(trash.area, trash.size,
1211 objt_server(conn->target), conn,
1212 NULL);
1213 }
1214
1215 if (!ret)
1216 goto out_error;
1217
1218 if (conn->send_proxy_ofs > 0)
1219 conn->send_proxy_ofs = -ret; /* first call */
1220
1221 /* we have to send trash from (ret+sp for -sp bytes). If the
1222 * data layer has a pending write, we'll also set MSG_MORE.
1223 */
1224 ret = conn_ctrl_send(conn,
1225 trash.area + ret + conn->send_proxy_ofs,
1226 -conn->send_proxy_ofs,
1227 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
1228
1229 if (ret < 0)
1230 goto out_error;
1231
1232 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1233 if (conn->send_proxy_ofs != 0)
1234 goto out_wait;
1235
1236 /* OK we've sent the whole line, we're connected */
1237 }
1238
1239 /* The connection is ready now, simply return and let the connection
1240 * handler notify upper layers if needed.
1241 */
1242 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1243 conn->flags &= ~flag;
1244 return 1;
1245
1246 out_error:
1247 /* Write error on the file descriptor */
1248 conn->flags |= CO_FL_ERROR;
1249 return 0;
1250
1251 out_wait:
1252 return 0;
1253}
1254
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001255/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001256 * at the beginning of the raw data stream. The header format is
1257 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001258 *
1259 * This line MUST be at the beginning of the buffer and MUST NOT be
1260 * fragmented.
1261 *
1262 * The header line is small and in all cases smaller than the smallest normal
1263 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
1264 * can safely use MSG_PEEK and avoid buffering.
1265 *
1266 * Once the data is fetched, the values are set in the connection's address
1267 * fields, and data are removed from the socket's buffer. The function returns
1268 * zero if it needs to wait for more data or if it fails, or 1 if it completed
1269 * and removed itself.
1270 */
1271int conn_recv_netscaler_cip(struct connection *conn, int flag)
1272{
Christopher Faulete83e8822021-10-25 08:23:22 +02001273 struct session *sess = conn->owner;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001274 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001275 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001276 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +02001277 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001278
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001279 if (!conn_ctrl_ready(conn))
1280 goto fail;
1281
Willy Tarreau07ecfc52022-04-11 18:07:03 +02001282 BUG_ON(conn->flags & CO_FL_FDLESS);
1283
Willy Tarreau585744b2017-08-24 14:31:19 +02001284 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001285 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001286
Willy Tarreau157788c2020-02-11 10:08:05 +01001287 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +02001288 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
1289 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001290 if (errno == EINTR)
1291 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +02001292 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Willy Tarreau585744b2017-08-24 14:31:19 +02001293 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001294 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001295 }
1296 goto recv_abort;
1297 }
Willy Tarreaub406b872018-08-22 05:20:32 +02001298 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +01001299 break;
1300 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001301
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001302 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001304 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001305 /* client shutdown */
1306 conn->err_code = CO_ER_CIP_EMPTY;
1307 goto fail;
1308 }
1309
1310 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001311 * CIP magic, header length or
1312 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001313 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001314 goto missing;
1315
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001316 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001317
1318 /* Decode a possible NetScaler Client IP request, fail early if
1319 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001320 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001321 goto bad_magic;
1322
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001323 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001324 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001325 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001326 line += 8;
1327 }
1328 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001329 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001330 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001331 line += 12;
1332 }
1333 /* Unknown CIP protocol */
1334 else {
1335 conn->err_code = CO_ER_CIP_BAD_PROTO;
1336 goto fail;
1337 }
1338
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001339 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001340 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001341 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001342 goto missing;
1343
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001344 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001345 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001346
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001347 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001348 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +01001349 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001350
1351 hdr_ip4 = (struct ip *)line;
1352
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001353 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001354 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001355 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001356 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001357 }
1358 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001359 /* The protocol does not include a TCP header */
1360 conn->err_code = CO_ER_CIP_BAD_PROTO;
1361 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001362 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001363
David Carlier3015a2e2016-07-04 22:51:33 +01001364 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001365
Christopher Faulete83e8822021-10-25 08:23:22 +02001366 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1367 goto fail;
1368
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001369 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001370 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
1371 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
1372 ((struct sockaddr_in *)sess->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001373
Christopher Faulete83e8822021-10-25 08:23:22 +02001374 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1375 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
1376 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001377 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001378 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001379 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +01001380 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001381
1382 hdr_ip6 = (struct ip6_hdr *)line;
1383
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001384 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001385 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001386 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001387 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001388 }
1389 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001390 /* The protocol does not include a TCP header */
1391 conn->err_code = CO_ER_CIP_BAD_PROTO;
1392 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001393 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001394
David Carlier3015a2e2016-07-04 22:51:33 +01001395 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001396
Christopher Faulete83e8822021-10-25 08:23:22 +02001397 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1398 goto fail;
1399
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001400 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001401 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1402 ((struct sockaddr_in6 *)sess->src)->sin6_addr = hdr_ip6->ip6_src;
1403 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001404
Christopher Faulete83e8822021-10-25 08:23:22 +02001405 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1406 ((struct sockaddr_in6 *)sess->dst)->sin6_addr = hdr_ip6->ip6_dst;
1407 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001408 }
1409 else {
1410 /* The protocol does not match something known (IPv4/IPv6) */
1411 conn->err_code = CO_ER_CIP_BAD_PROTO;
1412 goto fail;
1413 }
1414
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001415 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001416 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001417
1418 /* remove the NetScaler Client IP header from the request. For this
1419 * we re-read the exact line at once. If we don't get the exact same
1420 * result, we fail.
1421 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001422 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001423 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001424 if (len2 < 0 && errno == EINTR)
1425 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001426 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001427 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001428 break;
1429 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001430
1431 conn->flags &= ~flag;
1432 return 1;
1433
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001434 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001435 return 0;
1436
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001437 missing:
1438 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1439 * we have not read anything. Otherwise we need to fail because we won't
1440 * be able to poll anymore.
1441 */
1442 conn->err_code = CO_ER_CIP_TRUNCATED;
1443 goto fail;
1444
1445 bad_magic:
1446 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1447 goto fail;
1448
1449 recv_abort:
1450 conn->err_code = CO_ER_CIP_ABORT;
1451 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1452 goto fail;
1453
1454 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001455 conn->flags |= CO_FL_ERROR;
1456 return 0;
1457}
1458
Alexander Liu2a54bb72019-05-22 19:44:48 +08001459
1460int conn_send_socks4_proxy_request(struct connection *conn)
1461{
1462 struct socks4_request req_line;
1463
Alexander Liu2a54bb72019-05-22 19:44:48 +08001464 if (!conn_ctrl_ready(conn))
1465 goto out_error;
1466
Willy Tarreau226572f2019-07-17 14:46:00 +02001467 if (!conn_get_dst(conn))
1468 goto out_error;
1469
Alexander Liu2a54bb72019-05-22 19:44:48 +08001470 req_line.version = 0x04;
1471 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001472 req_line.port = get_net_port(conn->dst);
1473 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001474 memcpy(req_line.user_id, "HAProxy\0", 8);
1475
1476 if (conn->send_proxy_ofs > 0) {
1477 /*
1478 * This is the first call to send the request
1479 */
1480 conn->send_proxy_ofs = -(int)sizeof(req_line);
1481 }
1482
1483 if (conn->send_proxy_ofs < 0) {
1484 int ret = 0;
1485
1486 /* we are sending the socks4_req_line here. If the data layer
1487 * has a pending write, we'll also set MSG_MORE.
1488 */
Willy Tarreau827fee72020-12-11 15:26:55 +01001489 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +08001490 conn,
1491 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1492 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +01001493 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001494
1495 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
Willy Tarreau0e9c2642022-04-11 18:01:28 +02001496 conn_fd(conn), -conn->send_proxy_ofs, ret);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001497
1498 if (ret < 0) {
1499 goto out_error;
1500 }
1501
1502 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1503 if (conn->send_proxy_ofs != 0) {
1504 goto out_wait;
1505 }
1506 }
1507
1508 /* OK we've the whole request sent */
1509 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001510
1511 /* The connection is ready now, simply return and let the connection
1512 * handler notify upper layers if needed.
1513 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001514 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001515
1516 if (conn->flags & CO_FL_SEND_PROXY) {
1517 /*
1518 * Get the send_proxy_ofs ready for the send_proxy due to we are
1519 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1520 * before sending PROXY Protocol.
1521 */
1522 conn->send_proxy_ofs = 1;
1523 }
1524 return 1;
1525
1526 out_error:
1527 /* Write error on the file descriptor */
1528 conn->flags |= CO_FL_ERROR;
1529 if (conn->err_code == CO_ER_NONE) {
1530 conn->err_code = CO_ER_SOCKS4_SEND;
1531 }
1532 return 0;
1533
1534 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001535 return 0;
1536}
1537
1538int conn_recv_socks4_proxy_response(struct connection *conn)
1539{
1540 char line[SOCKS4_HS_RSP_LEN];
1541 int ret;
1542
Alexander Liu2a54bb72019-05-22 19:44:48 +08001543 if (!conn_ctrl_ready(conn))
1544 goto fail;
1545
Willy Tarreau07ecfc52022-04-11 18:07:03 +02001546 BUG_ON(conn->flags & CO_FL_FDLESS);
1547
Alexander Liu2a54bb72019-05-22 19:44:48 +08001548 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001549 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001550
Willy Tarreau157788c2020-02-11 10:08:05 +01001551 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001552 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1553 * Try to peek into it, before all 8 bytes ready.
1554 */
1555 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1556
1557 if (ret == 0) {
1558 /* the socket has been closed or shutdown for send */
1559 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1560 conn->handle.fd, ret, errno);
1561 if (conn->err_code == CO_ER_NONE) {
1562 conn->err_code = CO_ER_SOCKS4_RECV;
1563 }
1564 goto fail;
1565 }
1566
1567 if (ret > 0) {
1568 if (ret == SOCKS4_HS_RSP_LEN) {
1569 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1570 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1571 }else{
1572 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], first byte is [%02X], last bye is [%02X]\n", conn->handle.fd, ret, line[0], line[ret-1]);
1573 }
1574 } else {
1575 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1576 }
1577
1578 if (ret < 0) {
1579 if (errno == EINTR) {
1580 continue;
1581 }
Willy Tarreauacef5e22022-04-25 20:32:15 +02001582 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001583 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001584 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001585 }
1586 goto recv_abort;
1587 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001588 break;
1589 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001590
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001591 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1592
Alexander Liu2a54bb72019-05-22 19:44:48 +08001593 if (ret < SOCKS4_HS_RSP_LEN) {
1594 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1595 * we are not able to read enough data.
1596 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001597 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001598 }
1599
1600 /*
1601 * Base on the SOCSK4 protocol:
1602 *
1603 * +----+----+----+----+----+----+----+----+
1604 * | VN | CD | DSTPORT | DSTIP |
1605 * +----+----+----+----+----+----+----+----+
1606 * # of bytes: 1 1 2 4
1607 * VN is the version of the reply code and should be 0. CD is the result
1608 * code with one of the following values:
1609 * 90: request granted
1610 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001611 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001612 * 93: request rejected because the client program and identd report different user-ids
1613 * The remaining fields are ignored.
1614 */
1615 if (line[1] != 90) {
1616 conn->flags &= ~CO_FL_SOCKS4_RECV;
1617
1618 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1619 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1620 if (conn->err_code == CO_ER_NONE) {
1621 conn->err_code = CO_ER_SOCKS4_DENY;
1622 }
1623 goto fail;
1624 }
1625
1626 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001627 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001628 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1629 if (ret < 0 && errno == EINTR) {
1630 continue;
1631 }
1632 if (ret != SOCKS4_HS_RSP_LEN) {
1633 if (conn->err_code == CO_ER_NONE) {
1634 conn->err_code = CO_ER_SOCKS4_RECV;
1635 }
1636 goto fail;
1637 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001638 break;
1639 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001640
1641 conn->flags &= ~CO_FL_SOCKS4_RECV;
1642 return 1;
1643
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001644 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001645 return 0;
1646
Alexander Liu2a54bb72019-05-22 19:44:48 +08001647 recv_abort:
1648 if (conn->err_code == CO_ER_NONE) {
1649 conn->err_code = CO_ER_SOCKS4_ABORT;
1650 }
1651 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1652 goto fail;
1653
1654 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001655 conn->flags |= CO_FL_ERROR;
1656 return 0;
1657}
1658
Willy Tarreaud318e4e2022-03-02 14:46:45 +01001659/* registers proto mux list <list>. Modifies the list element! */
1660void register_mux_proto(struct mux_proto_list *list)
1661{
1662 LIST_APPEND(&mux_proto_list.list, &list->list);
1663}
1664
Willy Tarreauadd43062022-02-18 11:07:40 +01001665/* Lists the known proto mux on <out>. This function is used by "haproxy -vv"
1666 * and is suitable for early boot just after the "REGISTER" stage because it
1667 * doesn't depend on anything to be already allocated.
1668 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001669void list_mux_proto(FILE *out)
1670{
1671 struct mux_proto_list *item;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001672 struct ist proto;
1673 char *mode, *side;
Willy Tarreauadd43062022-02-18 11:07:40 +01001674 int done;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001675
1676 fprintf(out, "Available multiplexer protocols :\n"
1677 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
1678 list_for_each_entry(item, &mux_proto_list.list, list) {
1679 proto = item->token;
1680
1681 if (item->mode == PROTO_MODE_ANY)
1682 mode = "TCP|HTTP";
1683 else if (item->mode == PROTO_MODE_TCP)
1684 mode = "TCP";
1685 else if (item->mode == PROTO_MODE_HTTP)
1686 mode = "HTTP";
1687 else
1688 mode = "NONE";
1689
1690 if (item->side == PROTO_SIDE_BOTH)
1691 side = "FE|BE";
1692 else if (item->side == PROTO_SIDE_FE)
1693 side = "FE";
1694 else if (item->side == PROTO_SIDE_BE)
1695 side = "BE";
1696 else
1697 side = "NONE";
1698
Willy Tarreaudddfec52022-04-09 11:37:35 +02001699 fprintf(out, " %10s : mode=%-5s side=%-6s mux=%-5s flags=",
Willy Tarreauadd43062022-02-18 11:07:40 +01001700 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name);
1701
1702 done = 0;
1703
1704 /* note: the block below could be simplied using macros but for only
1705 * 4 flags it's not worth it.
1706 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001707 if (item->mux->flags & MX_FL_HTX)
Willy Tarreauadd43062022-02-18 11:07:40 +01001708 done |= fprintf(out, "%sHTX", done ? "|" : "");
1709
Willy Tarreaue59b5162021-05-08 14:06:09 +02001710 if (item->mux->flags & MX_FL_HOL_RISK)
Willy Tarreauadd43062022-02-18 11:07:40 +01001711 done |= fprintf(out, "%sHOL_RISK", done ? "|" : "");
1712
Willy Tarreaue59b5162021-05-08 14:06:09 +02001713 if (item->mux->flags & MX_FL_NO_UPG)
Willy Tarreauadd43062022-02-18 11:07:40 +01001714 done |= fprintf(out, "%sNO_UPG", done ? "|" : "");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001715
Willy Tarreaub5821e12022-04-26 11:54:08 +02001716 if (item->mux->flags & MX_FL_FRAMED)
1717 done |= fprintf(out, "%sFRAMED", done ? "|" : "");
1718
Willy Tarreauadd43062022-02-18 11:07:40 +01001719 fprintf(out, "\n");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001720 }
1721}
1722
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001723/* Makes a PROXY protocol line from the two addresses. The output is sent to
1724 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1725 * It returns the number of bytes composing this line (including the trailing
1726 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001727 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1728 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001729 */
Christopher Fauletb097aef2021-10-25 08:05:28 +02001730static int make_proxy_line_v1(char *buf, int buf_len, const struct sockaddr_storage *src, const struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001731{
1732 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001733 char * protocol;
1734 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1735 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1736 in_port_t src_port;
1737 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001738
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001739 if ( !src
1740 || !dst
1741 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1742 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1743 /* unknown family combination */
1744 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001745 if (ret >= buf_len)
1746 return 0;
1747
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001748 return ret;
1749 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001750
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001751 /* IPv4 for both src and dst */
1752 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1753 protocol = "TCP4";
1754 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001755 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001756 src_port = ((struct sockaddr_in *)src)->sin_port;
1757 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001758 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001759 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001760 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001761 /* IPv6 for at least one of src and dst */
1762 else {
1763 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001764
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001765 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001766
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001767 if (src->ss_family == AF_INET) {
1768 /* Convert src to IPv6 */
1769 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1770 src_port = ((struct sockaddr_in *)src)->sin_port;
1771 }
1772 else {
1773 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1774 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1775 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001776
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001777 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001778 return 0;
1779
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001780 if (dst->ss_family == AF_INET) {
1781 /* Convert dst to IPv6 */
1782 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1783 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1784 }
1785 else {
1786 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1787 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1788 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001789
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001790 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001791 return 0;
1792 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001793
1794 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1795 if (ret >= buf_len)
1796 return 0;
1797
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001798 return ret;
1799}
David Safb76832014-05-08 23:42:08 -04001800
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001801static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001802{
1803 struct tlv *tlv;
1804
1805 if (!dest || (length + sizeof(*tlv) > dest_len))
1806 return 0;
1807
1808 tlv = (struct tlv *)dest;
1809
1810 tlv->type = type;
1811 tlv->length_hi = length >> 8;
1812 tlv->length_lo = length & 0x00ff;
1813 memcpy(tlv->value, value, length);
1814 return length + sizeof(*tlv);
1815}
David Safb76832014-05-08 23:42:08 -04001816
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001817/* Note: <remote> is explicitly allowed to be NULL */
Christopher Faulet4bfce392021-10-22 14:33:59 +02001818static int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001819{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001820 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001821 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001822 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001823 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001824 struct sockaddr_storage null_addr = { .ss_family = 0 };
Christopher Fauletb097aef2021-10-25 08:05:28 +02001825 const struct sockaddr_storage *src = &null_addr;
1826 const struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001827 const char *value;
1828 int value_len;
David Safb76832014-05-08 23:42:08 -04001829
1830 if (buf_len < PP2_HEADER_LEN)
1831 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001832 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001833
Christopher Fauletb097aef2021-10-25 08:05:28 +02001834 if (strm) {
Willy Tarreaud68ff012022-05-27 08:57:21 +02001835 src = sc_src(strm->scf);
1836 dst = sc_dst(strm->scf);
Christopher Fauletb097aef2021-10-25 08:05:28 +02001837 }
1838 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1839 src = conn_src(remote);
1840 dst = conn_dst(remote);
David Safb76832014-05-08 23:42:08 -04001841 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001842
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001843 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1844 if ( !src
1845 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001846 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001847 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1848 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001849 if (buf_len < PP2_HDR_LEN_UNSPEC)
1850 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001851 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1852 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001853 ret = PP2_HDR_LEN_UNSPEC;
1854 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001855 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001856 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001857 /* IPv4 for both src and dst */
1858 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1859 if (buf_len < PP2_HDR_LEN_INET)
1860 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001861 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1862 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1863 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1864 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1865 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1866 ret = PP2_HDR_LEN_INET;
1867 }
1868 /* IPv6 for at least one of src and dst */
1869 else {
1870 struct in6_addr tmp;
1871
1872 if (buf_len < PP2_HDR_LEN_INET6)
1873 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001874 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1875 if (src->ss_family == AF_INET) {
1876 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1877 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1878 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1879 }
1880 else {
1881 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1882 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1883 }
1884 if (dst->ss_family == AF_INET) {
1885 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1886 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001887 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001888 }
1889 else {
1890 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1891 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1892 }
1893
1894 ret = PP2_HDR_LEN_INET6;
1895 }
1896 }
David Safb76832014-05-08 23:42:08 -04001897
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001898 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1899 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001900
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001901 if ((buf_len - ret) < sizeof(struct tlv))
1902 return 0;
1903 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1904 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1905 }
1906
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001907 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001908 if ((buf_len - ret) < sizeof(struct tlv))
1909 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001910 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001911 }
1912
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001913 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001914 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001915 if (remote && isttest(remote->proxy_authority)) {
1916 value = istptr(remote->proxy_authority);
1917 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001918 }
1919#ifdef USE_OPENSSL
1920 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001921 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001922 value_len = strlen(value);
1923 }
1924#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001925 if (value) {
1926 if ((buf_len - ret) < sizeof(struct tlv))
1927 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001928 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001929 }
1930 }
1931
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001932 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001933 struct session* sess = strm_sess(strm);
1934 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1935
1936 value = unique_id.ptr;
1937 value_len = unique_id.len;
1938
1939 if (value_len >= 0) {
1940 if ((buf_len - ret) < sizeof(struct tlv))
1941 return 0;
1942 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1943 }
1944 }
1945
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001946#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001947 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001948 struct tlv_ssl *tlv;
1949 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001950
David Safb76832014-05-08 23:42:08 -04001951 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1952 return 0;
1953 tlv = (struct tlv_ssl *)&buf[ret];
1954 memset(tlv, 0, sizeof(struct tlv_ssl));
1955 ssl_tlv_len += sizeof(struct tlv_ssl);
1956 tlv->tlv.type = PP2_TYPE_SSL;
Willy Tarreau1057bee2021-10-06 11:38:44 +02001957 if (conn_is_ssl(remote)) {
David Safb76832014-05-08 23:42:08 -04001958 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001959 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001960 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001961 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001962 }
Dave McCowan328fb582014-07-30 10:39:13 -04001963 if (ssl_sock_get_cert_used_sess(remote)) {
1964 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001965 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001966 if (ssl_sock_get_cert_used_conn(remote))
1967 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001968 }
1969 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001970 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001971 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001972 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1973 cn_trash->data,
1974 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001975 }
1976 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001977 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001978 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001979 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001980 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1981 pkey_trash->data,
1982 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001983 }
1984 }
1985 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1986 value = ssl_sock_get_cert_sig(remote);
1987 if (value) {
1988 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1989 }
1990 }
1991 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1992 value = ssl_sock_get_cipher_name(remote);
1993 if (value) {
1994 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1995 }
1996 }
David Safb76832014-05-08 23:42:08 -04001997 }
1998 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1999 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
2000 ret += ssl_tlv_len;
2001 }
2002#endif
2003
Willy Tarreaue5733232019-05-22 19:24:06 +02002004#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002005 if (remote && (remote->proxy_netns)) {
2006 if ((buf_len - ret) < sizeof(struct tlv))
2007 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01002008 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002009 }
2010#endif
2011
Willy Tarreau8fccfa22014-06-14 08:28:06 +02002012 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04002013
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01002014 if (tlv_crc32c_p) {
2015 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
2016 }
2017
David Safb76832014-05-08 23:42:08 -04002018 return ret;
2019}
Emeric Brun4f603012017-01-05 15:11:44 +01002020
Christopher Faulet4bfce392021-10-22 14:33:59 +02002021/* Note: <remote> is explicitly allowed to be NULL */
2022int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
2023{
2024 int ret = 0;
2025
2026 if (srv && (srv->pp_opts & SRV_PP_V2)) {
2027 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
2028 }
2029 else {
Christopher Fauletb097aef2021-10-25 08:05:28 +02002030 const struct sockaddr_storage *src = NULL;
2031 const struct sockaddr_storage *dst = NULL;
2032
2033 if (strm) {
Willy Tarreaud68ff012022-05-27 08:57:21 +02002034 src = sc_src(strm->scf);
2035 dst = sc_dst(strm->scf);
Christopher Fauletb097aef2021-10-25 08:05:28 +02002036 }
2037 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
2038 src = conn_src(remote);
2039 dst = conn_dst(remote);
2040 }
2041
2042 if (src && dst)
2043 ret = make_proxy_line_v1(buf, buf_len, src, dst);
Christopher Faulet4bfce392021-10-22 14:33:59 +02002044 else
2045 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
2046 }
2047
2048 return ret;
2049}
2050
Willy Tarreau119e50e2020-05-22 13:53:29 +02002051/* returns 0 on success */
2052static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002053 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02002054 char **err)
2055{
2056 if (too_many_args(0, args, err, NULL))
2057 return -1;
2058 pp2_never_send_local = 1;
2059 return 0;
2060}
2061
Willy Tarreaud943a042021-06-16 17:35:20 +02002062/* extracts some info from the connection and appends them to buffer <buf>. The
2063 * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source
2064 * when set, destination when set, are printed in a compact human-readable format
2065 * fitting on a single line. This is handy to complete traces or debug output.
2066 * It is permitted to pass a NULL conn pointer. The number of characters emitted
2067 * is returned. A prefix <pfx> might be prepended before the first field if not
2068 * NULL.
2069 */
2070int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx)
2071{
2072 const struct listener *li;
2073 const struct server *sv;
2074 const struct proxy *px;
2075 char addr[40];
2076 int old_len = buf->data;
2077
2078 if (!conn)
2079 return 0;
2080
2081 chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN");
2082
2083 if ((li = objt_listener(conn->target)))
2084 chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id);
2085 else if ((sv = objt_server(conn->target)))
2086 chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id);
2087 else if ((px = objt_proxy(conn->target)))
2088 chunk_appendf(buf, " be=%s", px->id);
2089
2090 chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn));
2091
Willy Tarreau030b3e62022-05-02 17:47:46 +02002092 if (conn->src && addr_to_str(conn->src, addr, sizeof(addr)))
Willy Tarreaud943a042021-06-16 17:35:20 +02002093 chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src));
2094
Willy Tarreau030b3e62022-05-02 17:47:46 +02002095 if (conn->dst && addr_to_str(conn->dst, addr, sizeof(addr)))
Willy Tarreaud943a042021-06-16 17:35:20 +02002096 chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst));
2097
2098 return buf->data - old_len;
2099}
2100
Willy Tarreau0d953302024-01-17 18:00:21 +01002101/* return the number of glitches experienced on the mux connection. */
2102static int
2103smp_fetch_fc_glitches(const struct arg *args, struct sample *smp, const char *kw, void *private)
2104{
2105 struct connection *conn = NULL;
2106 int ret;
2107
2108 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2109 conn = (kw[0] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
2110 else
2111 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2112 smp->strm ? sc_conn(smp->strm->scb) : NULL;
2113
2114 /* No connection or a connection with an unsupported mux */
2115 if (!conn || (conn->mux && !conn->mux->ctl))
2116 return 0;
2117
2118 /* Mux not installed yet, this may change */
2119 if (!conn->mux) {
2120 smp->flags |= SMP_F_MAY_CHANGE;
2121 return 0;
2122 }
2123
2124 ret = conn->mux->ctl(conn, MUX_CTL_GET_GLITCHES, NULL);
2125 if (ret < 0) {
2126 /* not supported by the mux */
2127 return 0;
2128 }
2129
2130 smp->data.type = SMP_T_SINT;
2131 smp->data.u.sint = ret;
2132 return 1;
2133}
2134
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002135/* return the major HTTP version as 1 or 2 depending on how the request arrived
2136 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002137 *
2138 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002139 */
2140static int
2141smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
2142{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02002143 struct connection *conn = NULL;
Amaury Denoyelle16f3da42022-06-07 11:57:20 +02002144 const char *mux_name = NULL;
Christopher Faulet242f8ce2021-04-14 15:46:49 +02002145
2146 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002147 conn = (kw[0] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Christopher Faulet242f8ce2021-04-14 15:46:49 +02002148 else
2149 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002150 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002151
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002152 /* No connection or a connection with a RAW muxx */
2153 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
2154 return 0;
2155
2156 /* No mux install, this may change */
2157 if (!conn->mux) {
2158 smp->flags |= SMP_F_MAY_CHANGE;
2159 return 0;
2160 }
2161
Amaury Denoyelle16f3da42022-06-07 11:57:20 +02002162 mux_name = conn_get_mux_name(conn);
2163
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002164 smp->data.type = SMP_T_SINT;
Amaury Denoyelle16f3da42022-06-07 11:57:20 +02002165 if (strcmp(mux_name, "QUIC") == 0)
2166 smp->data.u.sint = 3;
2167 else if (strcmp(mux_name, "H2") == 0)
2168 smp->data.u.sint = 2;
2169 else
2170 smp->data.u.sint = 1;
2171
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002172 return 1;
2173}
2174
Emeric Brun4f603012017-01-05 15:11:44 +01002175/* fetch if the received connection used a PROXY protocol header */
2176int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
2177{
2178 struct connection *conn;
2179
2180 conn = objt_conn(smp->sess->origin);
2181 if (!conn)
2182 return 0;
2183
Willy Tarreau911db9b2020-01-23 16:27:54 +01002184 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01002185 smp->flags |= SMP_F_MAY_CHANGE;
2186 return 0;
2187 }
2188
2189 smp->flags = 0;
2190 smp->data.type = SMP_T_BOOL;
2191 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
2192
2193 return 1;
2194}
2195
Geoff Simmons7185b782019-08-27 18:31:16 +02002196/* fetch the authority TLV from a PROXY protocol header */
2197int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
2198{
2199 struct connection *conn;
2200
2201 conn = objt_conn(smp->sess->origin);
2202 if (!conn)
2203 return 0;
2204
Willy Tarreau911db9b2020-01-23 16:27:54 +01002205 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02002206 smp->flags |= SMP_F_MAY_CHANGE;
2207 return 0;
2208 }
2209
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002210 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02002211 return 0;
2212
2213 smp->flags = 0;
2214 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002215 smp->data.u.str.area = istptr(conn->proxy_authority);
2216 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02002217
2218 return 1;
2219}
2220
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002221/* fetch the unique ID TLV from a PROXY protocol header */
2222int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2223{
2224 struct connection *conn;
2225
2226 conn = objt_conn(smp->sess->origin);
2227 if (!conn)
2228 return 0;
2229
2230 if (conn->flags & CO_FL_WAIT_XPRT) {
2231 smp->flags |= SMP_F_MAY_CHANGE;
2232 return 0;
2233 }
2234
2235 if (!isttest(conn->proxy_unique_id))
2236 return 0;
2237
2238 smp->flags = 0;
2239 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01002240 smp->data.u.str.area = istptr(conn->proxy_unique_id);
2241 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002242
2243 return 1;
2244}
2245
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002246/* fetch the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002247int smp_fetch_fc_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002248{
2249 struct connection *conn;
2250
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002251 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002252 conn = (kw[0] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002253 else
2254 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002255 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002256
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002257 if (!conn)
2258 return 0;
2259
2260 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2261 smp->flags |= SMP_F_MAY_CHANGE;
2262 return 0;
2263 }
2264
2265 smp->flags = 0;
2266 smp->data.type = SMP_T_SINT;
2267 smp->data.u.sint = (unsigned long long int)conn->err_code;
2268
2269 return 1;
2270}
2271
2272/* fetch a string representation of the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002273int smp_fetch_fc_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002274{
2275 struct connection *conn;
2276 const char *err_code_str;
2277
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002278 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002279 conn = (kw[0] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002280 else
2281 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002282 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002283
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002284 if (!conn)
2285 return 0;
2286
2287 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2288 smp->flags |= SMP_F_MAY_CHANGE;
2289 return 0;
2290 }
2291
2292 err_code_str = conn_err_code_str(conn);
2293
2294 if (!err_code_str)
2295 return 0;
2296
2297 smp->flags = 0;
2298 smp->data.type = SMP_T_STR;
2299 smp->data.u.str.area = (char*)err_code_str;
2300 smp->data.u.str.data = strlen(err_code_str);
2301
2302 return 1;
2303}
2304
Emeric Brun4f603012017-01-05 15:11:44 +01002305/* Note: must not be declared <const> as its list will be overwritten.
2306 * Note: fetches that may return multiple types must be declared as the lowest
2307 * common denominator, the type that can be casted into all other ones. For
2308 * instance v4/v6 must be declared v4.
2309 */
2310static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau6f749762021-11-05 17:07:03 +01002311 { "bc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
2312 { "bc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4SRV },
Willy Tarreau0d953302024-01-17 18:00:21 +01002313 { "bc_glitches", smp_fetch_fc_glitches, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Jérôme Magnin86577422018-12-07 09:03:11 +01002314 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Willy Tarreau6f749762021-11-05 17:07:03 +01002315 { "fc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
2316 { "fc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Willy Tarreau0d953302024-01-17 18:00:21 +01002317 { "fc_glitches", smp_fetch_fc_glitches, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreau6f749762021-11-05 17:07:03 +01002318 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01002319 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02002320 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002321 { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01002322 { /* END */ },
2323}};
2324
Willy Tarreau0108d902018-11-25 19:14:37 +01002325INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02002326
2327static struct cfg_kw_list cfg_kws = {ILH, {
2328 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
2329 { /* END */ },
2330}};
2331
2332INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002333
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002334/* private function to handle sockaddr as input for connection hash */
2335static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
2336 char *buf, size_t *idx,
2337 enum conn_hash_params_t *hash_flags,
2338 enum conn_hash_params_t param_type_addr,
2339 enum conn_hash_params_t param_type_port)
2340{
2341 struct sockaddr_in *addr;
2342 struct sockaddr_in6 *addr6;
2343
2344 switch (ss->ss_family) {
2345 case AF_INET:
2346 addr = (struct sockaddr_in *)ss;
2347
2348 conn_hash_update(buf, idx,
2349 &addr->sin_addr, sizeof(addr->sin_addr),
2350 hash_flags, param_type_addr);
2351
2352 if (addr->sin_port) {
2353 conn_hash_update(buf, idx,
2354 &addr->sin_port, sizeof(addr->sin_port),
2355 hash_flags, param_type_port);
2356 }
2357
2358 break;
2359
2360 case AF_INET6:
2361 addr6 = (struct sockaddr_in6 *)ss;
2362
2363 conn_hash_update(buf, idx,
2364 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
2365 hash_flags, param_type_addr);
2366
2367 if (addr6->sin6_port) {
2368 conn_hash_update(buf, idx,
2369 &addr6->sin6_port, sizeof(addr6->sin6_port),
2370 hash_flags, param_type_port);
2371 }
2372
2373 break;
2374 }
2375}
2376
Willy Tarreaue5983ff2021-10-06 17:14:49 +02002377/* Generate the hash of a connection with params as input
2378 * Each non-null field of params is taken into account for the hash calcul.
2379 */
2380uint64_t conn_hash_prehash(char *buf, size_t size)
2381{
2382 return XXH64(buf, size, 0);
2383}
2384
2385/* Append <data> into <buf> at <idx> offset in preparation for connection hash
2386 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
2387 * are updated with <type> for the hash header.
2388 */
2389void conn_hash_update(char *buf, size_t *idx,
2390 const void *data, size_t size,
2391 enum conn_hash_params_t *flags,
2392 enum conn_hash_params_t type)
2393{
2394 memcpy(&buf[*idx], data, size);
2395 *idx += size;
2396 *flags |= type;
2397}
2398
2399uint64_t conn_hash_digest(char *buf, size_t bufsize,
2400 enum conn_hash_params_t flags)
2401{
2402 const uint64_t flags_u64 = (uint64_t)flags;
2403 const uint64_t hash = XXH64(buf, bufsize, 0);
2404
2405 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
2406}
2407
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002408uint64_t conn_calculate_hash(const struct conn_hash_params *params)
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002409{
2410 char *buf;
2411 size_t idx = 0;
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002412 uint64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002413 enum conn_hash_params_t hash_flags = 0;
2414
2415 buf = trash.area;
2416
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01002417 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01002418
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002419 if (params->sni_prehash) {
2420 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002421 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002422 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
2423 }
2424
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002425 if (params->dst_addr) {
2426 conn_calculate_hash_sockaddr(params->dst_addr,
2427 buf, &idx, &hash_flags,
2428 CONN_HASH_PARAMS_TYPE_DST_ADDR,
2429 CONN_HASH_PARAMS_TYPE_DST_PORT);
2430 }
2431
Amaury Denoyelled10a2002021-02-11 19:45:19 +01002432 if (params->src_addr) {
2433 conn_calculate_hash_sockaddr(params->src_addr,
2434 buf, &idx, &hash_flags,
2435 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
2436 CONN_HASH_PARAMS_TYPE_SRC_PORT);
2437 }
2438
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002439 if (params->proxy_prehash) {
2440 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002441 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002442 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
2443 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002444
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002445 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002446 return hash;
2447}
Amaury Denoyelle48372932021-09-16 12:15:12 +02002448
2449/* Handler of the task of mux_stopping_data.
2450 * Called on soft-stop.
2451 */
2452static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state)
2453{
2454 struct connection *conn, *back;
2455
2456 list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) {
2457 if (conn->mux && conn->mux->wake)
2458 conn->mux->wake(conn);
2459 }
2460
2461 return t;
2462}
2463
2464static int allocate_mux_cleanup(void)
2465{
2466 /* allocates the thread bound mux_stopping_data task */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002467 mux_stopping_data[tid].task = task_new_here();
Amaury Denoyelle48372932021-09-16 12:15:12 +02002468 if (!mux_stopping_data[tid].task) {
2469 ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid);
2470 return 0;
2471 }
2472
2473 mux_stopping_data[tid].task->process = mux_stopping_process;
2474 LIST_INIT(&mux_stopping_data[tid].list);
2475
2476 return 1;
2477}
2478REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup);
2479
2480static int deallocate_mux_cleanup(void)
2481{
2482 task_destroy(mux_stopping_data[tid].task);
2483 return 1;
2484}
2485REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup);
Willy Tarreaufaafe4b2022-04-27 18:53:07 +02002486
2487static void deinit_idle_conns(void)
2488{
2489 int i;
2490
2491 for (i = 0; i < global.nbthread; i++) {
Tim Duesterhusfe83f582023-04-22 17:47:34 +02002492 task_destroy(idle_conns[i].cleanup_task);
Willy Tarreaufaafe4b2022-04-27 18:53:07 +02002493 }
2494}
2495REGISTER_POST_DEINIT(deinit_idle_conns);