blob: f8b704b67799199d5ffeae5c12ffd9150c1ae4a4 [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 Tarreau209108d2020-06-04 20:30:20 +020029#include <haproxy/ssl_sock.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020030#include <haproxy/stream_interface.h>
Willy Tarreau908908e2021-05-08 13:07:31 +020031#include <haproxy/tools.h>
Willy Tarreaue5983ff2021-10-06 17:14:49 +020032#include <haproxy/xxhash.h>
Emeric Brun46591952012-05-18 15:47:34 +020033
Alexander Liu2a54bb72019-05-22 19:44:48 +080034
Amaury Denoyelle8990b012021-02-19 15:29:16 +010035DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
36DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
37DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node));
38DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
39DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010040
Willy Tarreau4d82bf52020-06-28 00:19:17 +020041struct idle_conns idle_conns[MAX_THREADS] = { };
Willy Tarreau13e14102016-12-22 20:25:26 +010042struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020043
Christopher Faulet32f61c02018-04-10 14:33:41 +020044/* List head of all known muxes for PROTO */
45struct mux_proto_list mux_proto_list = {
46 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020047};
48
Amaury Denoyelled3a88c12021-05-03 10:47:51 +020049struct mux_stopping_data mux_stopping_data[MAX_THREADS];
50
Willy Tarreau119e50e2020-05-22 13:53:29 +020051/* disables sending of proxy-protocol-v2's LOCAL command */
52static int pp2_never_send_local;
53
Willy Tarreau930428c2021-10-06 18:27:28 +020054void conn_delete_from_tree(struct ebmb_node *node)
55{
56 ebmb_delete(node);
57 memset(node, 0, sizeof(*node));
58}
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;
64 struct conn_stream *cs = 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 }
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010074 else if (conn_install_mux_be(conn, conn->ctx, sess) < 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 Tarreau430bf4a2021-03-04 09:45:32 +010086 ebmb_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
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 */
94 cs->data_cb->wake(cs);
95 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) {
149 uint conn_in_list = conn->flags & CO_FL_LIST_MASK;
150 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);
168 ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash));
169 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
251 * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns
252 * < 0 on error.
253 */
254int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess)
255{
256 struct server *srv = objt_server(conn->target);
257 struct proxy *prx = objt_proxy(conn->target);
258 const struct mux_ops *mux_ops;
259
260 if (srv)
261 prx = srv->proxy;
262
263 if (!prx) // target must be either proxy or server
264 return -1;
265
266 if (srv && srv->mux_proto)
267 mux_ops = srv->mux_proto->mux;
268 else {
269 struct ist mux_proto;
270 const char *alpn_str = NULL;
271 int alpn_len = 0;
272 int mode;
273
274 if (prx->mode == PR_MODE_HTTP)
275 mode = PROTO_MODE_HTTP;
276 else
277 mode = PROTO_MODE_TCP;
278
279 conn_get_alpn(conn, &alpn_str, &alpn_len);
280 mux_proto = ist2(alpn_str, alpn_len);
281
282 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
283 if (!mux_ops)
284 return -1;
285 }
286 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
287}
288
289/* installs the best mux for outgoing connection <conn> for a check using the
290 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
291 * find the best mux. Returns < 0 on error.
292 */
293int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
294{
295 struct check *check = objt_check(sess->origin);
296 struct server *srv = objt_server(conn->target);
297 struct proxy *prx = objt_proxy(conn->target);
298 const struct mux_ops *mux_ops;
299
300 if (!check) // Check must be defined
301 return -1;
302
303 if (srv)
304 prx = srv->proxy;
305
306 if (!prx) // target must be either proxy or server
307 return -1;
308
309 if (check->mux_proto)
310 mux_ops = check->mux_proto->mux;
311 else {
312 struct ist mux_proto;
313 const char *alpn_str = NULL;
314 int alpn_len = 0;
315 int mode;
316
317 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
318 mode = PROTO_MODE_HTTP;
319 else
320 mode = PROTO_MODE_TCP;
321
322 conn_get_alpn(conn, &alpn_str, &alpn_len);
323 mux_proto = ist2(alpn_str, alpn_len);
324
325 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
326 if (!mux_ops)
327 return -1;
328 }
329 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
330}
331
332/* Initializes all required fields for a new connection. Note that it does the
333 * minimum acceptable initialization for a connection that already exists and
334 * is about to be reused. It also leaves the addresses untouched, which makes
335 * it usable across connection retries to reset a connection to a known state.
336 */
337void conn_init(struct connection *conn, void *target)
338{
339 conn->obj_type = OBJ_TYPE_CONN;
340 conn->flags = CO_FL_NONE;
341 conn->mux = NULL;
342 conn->ctx = NULL;
343 conn->owner = NULL;
344 conn->send_proxy_ofs = 0;
345 conn->handle.fd = DEAD_FD_MAGIC;
346 conn->err_code = CO_ER_NONE;
347 conn->target = target;
348 conn->destroy_cb = NULL;
349 conn->proxy_netns = NULL;
350 MT_LIST_INIT(&conn->toremove_list);
351 if (conn_is_back(conn))
352 LIST_INIT(&conn->session_list);
353 else
354 LIST_INIT(&conn->stopping_list);
355 conn->subs = NULL;
356 conn->src = NULL;
357 conn->dst = NULL;
358 conn->proxy_authority = IST_NULL;
359 conn->proxy_unique_id = IST_NULL;
360 conn->qc = NULL;
361 conn->hash_node = NULL;
362 conn->xprt = NULL;
363}
364
365/* Tries to allocate a new connection and initialized its main fields. The
366 * connection is returned on success, NULL on failure. The connection must
367 * be released using pool_free() or conn_free().
368 */
369struct connection *conn_new(void *target)
370{
371 struct connection *conn;
372 struct conn_hash_node *hash_node;
373
374 conn = pool_alloc(pool_head_connection);
375 if (unlikely(!conn))
376 return NULL;
377
378 conn_init(conn, target);
379
380 if (conn_is_back(conn)) {
381 if (obj_type(target) == OBJ_TYPE_SERVER)
382 srv_use_conn(__objt_server(target), conn);
383
384 hash_node = conn_alloc_hash_node(conn);
385 if (unlikely(!hash_node)) {
386 pool_free(pool_head_connection, conn);
387 return NULL;
388 }
389
390 conn->hash_node = hash_node;
391 }
392
393 return conn;
394}
395
396/* Releases a connection previously allocated by conn_new() */
397void conn_free(struct connection *conn)
398{
399 /* If the connection is owned by the session, remove it from its list
400 */
401 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
402 session_unown_conn(conn->owner, conn);
403 }
404 else if (!(conn->flags & CO_FL_PRIVATE)) {
405 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
406 srv_release_conn(__objt_server(conn->target), conn);
407 }
408
409 /* Remove the conn from toremove_list.
410 *
411 * This is needed to prevent a double-free in case the connection was
412 * already scheduled from cleaning but is freed before via another
413 * call.
414 */
415 MT_LIST_DELETE(&conn->toremove_list);
416
417 sockaddr_free(&conn->src);
418 sockaddr_free(&conn->dst);
419
420 pool_free(pool_head_authority, istptr(conn->proxy_authority));
421 conn->proxy_authority = IST_NULL;
422
423 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
424 conn->proxy_unique_id = IST_NULL;
425
426 pool_free(pool_head_conn_hash_node, conn->hash_node);
427 conn->hash_node = NULL;
428
429 /* By convention we always place a NULL where the ctx points to if the
430 * mux is null. It may have been used to store the connection as a
431 * stream_interface's end point for example.
432 */
433 if (conn->ctx != NULL && conn->mux == NULL)
434 *(void **)conn->ctx = NULL;
435
436 conn_force_unsubscribe(conn);
437 pool_free(pool_head_connection, conn);
438}
439
Willy Tarreau8de90c72021-10-06 19:11:10 +0200440struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
441{
442 struct conn_hash_node *hash_node = NULL;
443
444 hash_node = pool_zalloc(pool_head_conn_hash_node);
445 if (unlikely(!hash_node))
446 return NULL;
447
448 hash_node->conn = conn;
449
450 return hash_node;
451}
452
453/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
454 * returns it. If <sap> is NULL, the address is always allocated and returned.
455 * if <sap> is non-null, an address will only be allocated if it points to a
456 * non-null pointer. In this case the allocated address will be assigned there.
457 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
458 * into the allocated address. In both situations the new pointer is returned.
459 */
460struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
461{
462 struct sockaddr_storage *sa;
463
464 if (sap && *sap)
465 return *sap;
466
467 sa = pool_alloc(pool_head_sockaddr);
468 if (sa && orig && len > 0)
469 memcpy(sa, orig, len);
470 if (sap)
471 *sap = sa;
472 return sa;
473}
474
475/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
476 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
477 * there.
478 */
479void sockaddr_free(struct sockaddr_storage **sap)
480{
481 if (!sap)
482 return;
483 pool_free(pool_head_sockaddr, *sap);
484 *sap = NULL;
485}
486
487/* Releases a conn_stream previously allocated by cs_new(), as well as any
488 * buffer it would still hold.
489 */
490void cs_free(struct conn_stream *cs)
491{
492
493 pool_free(pool_head_connstream, cs);
494}
495
496/* Tries to allocate a new conn_stream and initialize its main fields. If
497 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
498 * and assigned to cs->conn ; this connection will then have to be released
499 * using pool_free() or conn_free(). The conn_stream is initialized and added
500 * to the mux's stream list on success, then returned. On failure, nothing is
501 * allocated and NULL is returned.
502 */
503struct conn_stream *cs_new(struct connection *conn, void *target)
504{
505 struct conn_stream *cs;
506
507 cs = pool_alloc(pool_head_connstream);
508 if (unlikely(!cs))
509 return NULL;
510
511 if (!conn) {
512 conn = conn_new(target);
513 if (unlikely(!conn)) {
514 cs_free(cs);
515 return NULL;
516 }
517 }
518
519 cs_init(cs, conn);
520 return cs;
521}
522
Willy Tarreauaac777f2021-10-06 18:48:28 +0200523/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
524 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
525 * call the xprt's add_xprt() method.
526 * Returns 0 on success, or non-zero on failure.
527 */
528int xprt_add_hs(struct connection *conn)
529{
530 void *xprt_ctx = NULL;
531 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
532 void *nextxprt_ctx = NULL;
533 const struct xprt_ops *nextxprt_ops = NULL;
534
535 if (conn->flags & CO_FL_ERROR)
536 return -1;
537 if (ops->init(conn, &xprt_ctx) < 0)
538 return -1;
539 if (conn->xprt == xprt_get(XPRT_RAW)) {
540 nextxprt_ctx = conn->xprt_ctx;
541 nextxprt_ops = conn->xprt;
542 conn->xprt_ctx = xprt_ctx;
543 conn->xprt = ops;
544 } else {
545 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
546 &nextxprt_ctx, &nextxprt_ops) != 0) {
547 ops->close(conn, xprt_ctx);
548 return -1;
549 }
550 }
551 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
552 ops->close(conn, xprt_ctx);
553 return -1;
554 }
555 return 0;
556}
557
558/* returns a human-readable error code for conn->err_code, or NULL if the code
559 * is unknown.
560 */
561const char *conn_err_code_str(struct connection *c)
562{
563 switch (c->err_code) {
564 case CO_ER_NONE: return "Success";
565
566 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
567 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
568 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
569 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
570 case CO_ER_NOPROTO: return "Protocol or address family not supported";
571 case CO_ER_SOCK_ERR: return "General socket error";
572 case CO_ER_PORT_RANGE: return "Source port range exhausted";
573 case CO_ER_CANT_BIND: return "Can't bind to source address";
574 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
575 case CO_ER_ADDR_INUSE: return "Local source address already in use";
576
577 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
578 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
579 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
580 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
581 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
582 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
583 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
584
585 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
586 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
587 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
588 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
589 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
590 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
591
592 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
593 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
594 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
595 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
596 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
597 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
598 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
599 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
600 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
601 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
602 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
603 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
604 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
605 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
606 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
607
608 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
609 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
610 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
611 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
612
613 case CO_ERR_SSL_FATAL: return "SSL fatal error";
614 }
615 return NULL;
616}
617
Willy Tarreauff3e6482015-03-12 23:56:52 +0100618/* Send a message over an established connection. It makes use of send() and
619 * returns the same return code and errno. If the socket layer is not ready yet
620 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
621 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
622 * EMSGSIZE if called with a zero length message. The purpose is to simplify
623 * some rare attempts to directly write on the socket from above the connection
624 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
625 * It automatically retries on EINTR. Other errors cause the connection to be
626 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +0100627 * first one which is the connection instead of the file descriptor. <flags>
628 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100629 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100630int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100631{
Willy Tarreau827fee72020-12-11 15:26:55 +0100632 const struct buffer buffer = b_make((char*)buf, len, 0, len);
633 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100634 int ret;
635
636 ret = -1;
637 errno = ENOTSOCK;
638
639 if (conn->flags & CO_FL_SOCK_WR_SH)
640 goto fail;
641
642 if (!conn_ctrl_ready(conn))
643 goto fail;
644
645 errno = EMSGSIZE;
646 if (!len)
647 goto fail;
648
Willy Tarreau827fee72020-12-11 15:26:55 +0100649 /* snd_buf() already takes care of updating conn->flags and handling
650 * the FD polling status.
651 */
652 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
653 if (conn->flags & CO_FL_ERROR)
654 ret = -1;
655 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100656 fail:
657 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
658 return ret;
659}
660
Willy Tarreau746b0512020-12-11 17:06:11 +0100661/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
662 * The event subscriber <es> is not allowed to change from a previous call as
663 * long as at least one event is still subscribed. The <event_type> must only
664 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100665 */
666int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200667{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100668 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100669 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100670
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100671 es->events &= ~event_type;
672 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100673 conn->subs = NULL;
674
Willy Tarreau746b0512020-12-11 17:06:11 +0100675 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
676 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100677
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200678 return 0;
679}
680
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100681/* Called from the upper layer, to subscribe <es> to events <event_type>.
682 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100683 * previous call to subscribe(). If the connection's ctrl layer is ready,
684 * the wait_event is immediately woken up and the subcription is cancelled.
685 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100686 */
687int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200688{
Willy Tarreau746b0512020-12-11 17:06:11 +0100689 int ret = 0;
690
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100691 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100692 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100693
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100694 if (conn->subs && (conn->subs->events & event_type) == event_type)
695 return 0;
696
Willy Tarreau746b0512020-12-11 17:06:11 +0100697 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
698 ret = conn->ctrl->check_events(conn, event_type);
699 if (ret)
700 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100701 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100702
703 es->events = (es->events | event_type) & ~ret;
704 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200705 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200706}
707
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100708/* Drains possibly pending incoming data on the connection and update the flags
709 * accordingly. This is used to know whether we need to disable lingering on
710 * close. Returns non-zero if it is safe to close without disabling lingering,
711 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
712 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100713 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100714int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100715{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100716 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200717
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100718 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
719 ret = 1;
720 else if (conn->ctrl->drain) {
721 ret = conn->ctrl->drain(conn);
722 if (ret)
723 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100724 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100725 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100726}
727
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100728/*
729 * Get data length from tlv
730 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100731static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100732{
733 return (src->length_hi << 8) | src->length_lo;
734}
735
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200736/* This handshake handler waits a PROXY protocol header at the beginning of the
737 * raw data stream. The header looks like this :
738 *
739 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
740 *
741 * There must be exactly one space between each field. Fields are :
742 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
743 * - SRC3 : layer 3 (eg: IP) source address in standard text form
744 * - DST3 : layer 3 (eg: IP) destination address in standard text form
745 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
746 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
747 *
748 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
749 *
750 * The header line is small and in all cases smaller than the smallest normal
751 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
752 * can safely use MSG_PEEK and avoid buffering.
753 *
754 * Once the data is fetched, the values are set in the connection's address
755 * fields, and data are removed from the socket's buffer. The function returns
756 * zero if it needs to wait for more data or if it fails, or 1 if it completed
757 * and removed itself.
758 */
759int conn_recv_proxy(struct connection *conn, int flag)
760{
761 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200762 struct proxy_hdr_v2 *hdr_v2;
763 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100764 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100765 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200766 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200767
Willy Tarreau3c728722014-01-23 13:50:42 +0100768 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200769 goto fail;
770
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200771 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200772 goto fail;
773
Willy Tarreau585744b2017-08-24 14:31:19 +0200774 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200775 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100776
Willy Tarreau157788c2020-02-11 10:08:05 +0100777 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200778 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
779 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200780 if (errno == EINTR)
781 continue;
782 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200783 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200784 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200785 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100786 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200787 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200788 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100789 break;
790 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200791
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200792 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100793 /* client shutdown */
794 conn->err_code = CO_ER_PRX_EMPTY;
795 goto fail;
796 }
797
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100798 conn->flags &= ~CO_FL_WAIT_L4_CONN;
799
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200800 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200801 goto missing;
802
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200803 line = trash.area;
804 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200805
806 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200807 if (strncmp(line, "PROXY ", 6) != 0)
808 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200809
810 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200811 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200812 goto missing;
813
David CARLIER42ff05e2016-03-24 09:22:36 +0000814 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200815 u32 src3, dst3, sport, dport;
816
817 line += 5;
818
819 src3 = inetaddr_host_lim_ret(line, end, &line);
820 if (line == end)
821 goto missing;
822 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100823 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200824
825 dst3 = inetaddr_host_lim_ret(line, end, &line);
826 if (line == end)
827 goto missing;
828 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100829 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200830
831 sport = read_uint((const char **)&line, end);
832 if (line == end)
833 goto missing;
834 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100835 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200836
837 dport = read_uint((const char **)&line, end);
838 if (line > end - 2)
839 goto missing;
840 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100841 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200842 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100843 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200844
845 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200846 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
847 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
848 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200849
Willy Tarreau226572f2019-07-17 14:46:00 +0200850 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
851 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
852 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200853 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
854 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000855 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200856 u32 sport, dport;
857 char *src_s;
858 char *dst_s, *sport_s, *dport_s;
859 struct in6_addr src3, dst3;
860
861 line += 5;
862
863 src_s = line;
864 dst_s = sport_s = dport_s = NULL;
865 while (1) {
866 if (line > end - 2) {
867 goto missing;
868 }
869 else if (*line == '\r') {
870 *line = 0;
871 line++;
872 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100873 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200874 break;
875 }
876
877 if (*line == ' ') {
878 *line = 0;
879 if (!dst_s)
880 dst_s = line + 1;
881 else if (!sport_s)
882 sport_s = line + 1;
883 else if (!dport_s)
884 dport_s = line + 1;
885 }
886 line++;
887 }
888
889 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100890 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200891
892 sport = read_uint((const char **)&sport_s,dport_s - 1);
893 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100894 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200895
896 dport = read_uint((const char **)&dport_s,line - 2);
897 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100898 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200899
900 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100901 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200902
903 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100904 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200905
906 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200907 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
908 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
909 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200910
Willy Tarreau226572f2019-07-17 14:46:00 +0200911 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
912 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
913 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200914 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
915 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200916 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
917 /* This can be a UNIX socket forwarded by an haproxy upstream */
918 line += 9;
919 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200920 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200921 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100922 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200923 goto fail;
924 }
925
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200926 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200927 goto eat_header;
928
929 not_v1:
930 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200931 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200932 goto missing;
933
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200934 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200935
936 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
937 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
938 conn->err_code = CO_ER_PRX_NOT_HDR;
939 goto fail;
940 }
941
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100942 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
943 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200944 goto missing;
945
946 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
947 case 0x01: /* PROXY command */
948 switch (hdr_v2->fam) {
949 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100950 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
951 goto bad_header;
952
Willy Tarreau226572f2019-07-17 14:46:00 +0200953 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
954 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
955 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
956 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
957 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
958 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200959 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200960 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200961 break;
962 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100963 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
964 goto bad_header;
965
Willy Tarreau226572f2019-07-17 14:46:00 +0200966 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
967 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
968 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
969 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
970 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
971 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200972 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200973 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200974 break;
975 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100976
977 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100978 while (tlv_offset < total_v2_len) {
979 struct tlv *tlv_packet;
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100980 struct ist tlv;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100981
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100982 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
983 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
984 goto bad_header;
985
986 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100987 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
988 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100989
990 /* Verify that the TLV length does not exceed the total PROXYv2 length */
991 if (tlv_offset > total_v2_len)
992 goto bad_header;
993
994 switch (tlv_packet->type) {
995 case PP2_TYPE_CRC32C: {
996 uint32_t n_crc32c;
997
998 /* Verify that this TLV is exactly 4 bytes long */
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100999 if (istlen(tlv) != 4)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001000 goto bad_header;
1001
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001002 n_crc32c = read_n32(istptr(tlv));
1003 write_n32(istptr(tlv), 0); // compute with CRC==0
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001004
1005 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
1006 goto bad_header;
1007 break;
1008 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001009#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001010 case PP2_TYPE_NETNS: {
1011 const struct netns_entry *ns;
1012
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001013 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001014 if (ns)
1015 conn->proxy_netns = ns;
1016 break;
1017 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001018#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001019 case PP2_TYPE_AUTHORITY: {
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001020 if (istlen(tlv) > PP2_AUTHORITY_MAX)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001021 goto bad_header;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001022 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
1023 if (!isttest(conn->proxy_authority))
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001024 goto fail;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001025 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
1026 /* This is technically unreachable, because we verified above
1027 * that the TLV value fits.
1028 */
1029 goto fail;
1030 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001031 break;
1032 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001033 case PP2_TYPE_UNIQUE_ID: {
Tim Duesterhus002bd772021-03-06 20:06:49 +01001034 if (istlen(tlv) > UNIQUEID_LEN)
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001035 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +01001036 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001037 if (!isttest(conn->proxy_unique_id))
1038 goto fail;
1039 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
1040 /* This is technically unreachable, because we verified above
1041 * that the TLV value fits.
1042 */
1043 goto fail;
1044 }
1045 break;
1046 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001047 default:
1048 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001049 }
1050 }
1051
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001052 /* Verify that the PROXYv2 header ends at a TLV boundary.
1053 * This is technically unreachable, because the TLV parsing already
1054 * verifies that a TLV does not exceed the total length and also
1055 * that there is space for a TLV header.
1056 */
1057 if (tlv_offset != total_v2_len)
1058 goto bad_header;
1059
Willy Tarreau77992672014-06-14 11:06:17 +02001060 /* unsupported protocol, keep local connection address */
1061 break;
1062 case 0x00: /* LOCAL command */
1063 /* keep local connection address for LOCAL */
1064 break;
1065 default:
1066 goto bad_header; /* not a supported command */
1067 }
1068
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001069 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +02001070 goto eat_header;
1071
1072 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001073 /* remove the PROXY line from the request. For this we re-read the
1074 * exact line at once. If we don't get the exact same result, we
1075 * fail.
1076 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001077 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001078 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
1079
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001080 if (len2 < 0 && errno == EINTR)
1081 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001082 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001083 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001084 break;
1085 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001086
1087 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +01001088 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001089 return 1;
1090
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001091 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001092 return 0;
1093
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001094 missing:
1095 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1096 * we have not read anything. Otherwise we need to fail because we won't
1097 * be able to poll anymore.
1098 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001099 conn->err_code = CO_ER_PRX_TRUNCATED;
1100 goto fail;
1101
1102 bad_header:
1103 /* This is not a valid proxy protocol header */
1104 conn->err_code = CO_ER_PRX_BAD_HDR;
1105 goto fail;
1106
1107 recv_abort:
1108 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +01001109 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001110 goto fail;
1111
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001112 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001113 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001114 return 0;
1115}
1116
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001117/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001118 * at the beginning of the raw data stream. The header format is
1119 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001120 *
1121 * This line MUST be at the beginning of the buffer and MUST NOT be
1122 * fragmented.
1123 *
1124 * The header line is small and in all cases smaller than the smallest normal
1125 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
1126 * can safely use MSG_PEEK and avoid buffering.
1127 *
1128 * Once the data is fetched, the values are set in the connection's address
1129 * fields, and data are removed from the socket's buffer. The function returns
1130 * zero if it needs to wait for more data or if it fails, or 1 if it completed
1131 * and removed itself.
1132 */
1133int conn_recv_netscaler_cip(struct connection *conn, int flag)
1134{
1135 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001136 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001137 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +02001138 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001139
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001140 if (!conn_ctrl_ready(conn))
1141 goto fail;
1142
Willy Tarreau9b7587a2020-10-15 07:32:10 +02001143 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Olivier Houchard1a9dbe52020-01-22 15:31:09 +01001144 goto fail;
1145
Willy Tarreau585744b2017-08-24 14:31:19 +02001146 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001147 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001148
Willy Tarreau157788c2020-02-11 10:08:05 +01001149 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +02001150 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
1151 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001152 if (errno == EINTR)
1153 continue;
1154 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +02001155 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001156 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001157 }
1158 goto recv_abort;
1159 }
Willy Tarreaub406b872018-08-22 05:20:32 +02001160 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +01001161 break;
1162 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001163
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001164 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1165
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001166 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001167 /* client shutdown */
1168 conn->err_code = CO_ER_CIP_EMPTY;
1169 goto fail;
1170 }
1171
1172 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001173 * CIP magic, header length or
1174 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001175 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001176 goto missing;
1177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001178 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001179
1180 /* Decode a possible NetScaler Client IP request, fail early if
1181 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001182 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001183 goto bad_magic;
1184
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001185 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001186 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001187 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001188 line += 8;
1189 }
1190 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001191 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001192 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001193 line += 12;
1194 }
1195 /* Unknown CIP protocol */
1196 else {
1197 conn->err_code = CO_ER_CIP_BAD_PROTO;
1198 goto fail;
1199 }
1200
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001201 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001202 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001203 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001204 goto missing;
1205
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001206 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001207 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001208
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001209 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001210 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +01001211 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001212
1213 hdr_ip4 = (struct ip *)line;
1214
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001215 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001216 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001217 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001218 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001219 }
1220 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001221 /* The protocol does not include a TCP header */
1222 conn->err_code = CO_ER_CIP_BAD_PROTO;
1223 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001224 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001225
David Carlier3015a2e2016-07-04 22:51:33 +01001226 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001227
1228 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +02001229 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
1230 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
1231 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001232
Willy Tarreau226572f2019-07-17 14:46:00 +02001233 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
1234 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
1235 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001236
1237 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
1238 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001239 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001240 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +01001241 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001242
1243 hdr_ip6 = (struct ip6_hdr *)line;
1244
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001245 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001246 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001247 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001248 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001249 }
1250 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001251 /* The protocol does not include a TCP header */
1252 conn->err_code = CO_ER_CIP_BAD_PROTO;
1253 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001254 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001255
David Carlier3015a2e2016-07-04 22:51:33 +01001256 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001257
1258 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +02001259 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
1260 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
1261 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001262
Willy Tarreau226572f2019-07-17 14:46:00 +02001263 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
1264 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
1265 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001266
1267 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
1268 }
1269 else {
1270 /* The protocol does not match something known (IPv4/IPv6) */
1271 conn->err_code = CO_ER_CIP_BAD_PROTO;
1272 goto fail;
1273 }
1274
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001275 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001276 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001277
1278 /* remove the NetScaler Client IP header from the request. For this
1279 * we re-read the exact line at once. If we don't get the exact same
1280 * result, we fail.
1281 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001282 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001283 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001284 if (len2 < 0 && errno == EINTR)
1285 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001286 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001287 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001288 break;
1289 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001290
1291 conn->flags &= ~flag;
1292 return 1;
1293
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001294 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001295 return 0;
1296
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001297 missing:
1298 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1299 * we have not read anything. Otherwise we need to fail because we won't
1300 * be able to poll anymore.
1301 */
1302 conn->err_code = CO_ER_CIP_TRUNCATED;
1303 goto fail;
1304
1305 bad_magic:
1306 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1307 goto fail;
1308
1309 recv_abort:
1310 conn->err_code = CO_ER_CIP_ABORT;
1311 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1312 goto fail;
1313
1314 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001315 conn->flags |= CO_FL_ERROR;
1316 return 0;
1317}
1318
Alexander Liu2a54bb72019-05-22 19:44:48 +08001319
1320int conn_send_socks4_proxy_request(struct connection *conn)
1321{
1322 struct socks4_request req_line;
1323
Alexander Liu2a54bb72019-05-22 19:44:48 +08001324 if (!conn_ctrl_ready(conn))
1325 goto out_error;
1326
Willy Tarreau226572f2019-07-17 14:46:00 +02001327 if (!conn_get_dst(conn))
1328 goto out_error;
1329
Alexander Liu2a54bb72019-05-22 19:44:48 +08001330 req_line.version = 0x04;
1331 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001332 req_line.port = get_net_port(conn->dst);
1333 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001334 memcpy(req_line.user_id, "HAProxy\0", 8);
1335
1336 if (conn->send_proxy_ofs > 0) {
1337 /*
1338 * This is the first call to send the request
1339 */
1340 conn->send_proxy_ofs = -(int)sizeof(req_line);
1341 }
1342
1343 if (conn->send_proxy_ofs < 0) {
1344 int ret = 0;
1345
1346 /* we are sending the socks4_req_line here. If the data layer
1347 * has a pending write, we'll also set MSG_MORE.
1348 */
Willy Tarreau827fee72020-12-11 15:26:55 +01001349 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +08001350 conn,
1351 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1352 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +01001353 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001354
1355 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1356 conn->handle.fd, -conn->send_proxy_ofs, ret);
1357
1358 if (ret < 0) {
1359 goto out_error;
1360 }
1361
1362 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1363 if (conn->send_proxy_ofs != 0) {
1364 goto out_wait;
1365 }
1366 }
1367
1368 /* OK we've the whole request sent */
1369 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001370
1371 /* The connection is ready now, simply return and let the connection
1372 * handler notify upper layers if needed.
1373 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001374 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001375
1376 if (conn->flags & CO_FL_SEND_PROXY) {
1377 /*
1378 * Get the send_proxy_ofs ready for the send_proxy due to we are
1379 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1380 * before sending PROXY Protocol.
1381 */
1382 conn->send_proxy_ofs = 1;
1383 }
1384 return 1;
1385
1386 out_error:
1387 /* Write error on the file descriptor */
1388 conn->flags |= CO_FL_ERROR;
1389 if (conn->err_code == CO_ER_NONE) {
1390 conn->err_code = CO_ER_SOCKS4_SEND;
1391 }
1392 return 0;
1393
1394 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001395 return 0;
1396}
1397
1398int conn_recv_socks4_proxy_response(struct connection *conn)
1399{
1400 char line[SOCKS4_HS_RSP_LEN];
1401 int ret;
1402
Alexander Liu2a54bb72019-05-22 19:44:48 +08001403 if (!conn_ctrl_ready(conn))
1404 goto fail;
1405
1406 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001407 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001408
Willy Tarreau157788c2020-02-11 10:08:05 +01001409 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001410 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1411 * Try to peek into it, before all 8 bytes ready.
1412 */
1413 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1414
1415 if (ret == 0) {
1416 /* the socket has been closed or shutdown for send */
1417 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1418 conn->handle.fd, ret, errno);
1419 if (conn->err_code == CO_ER_NONE) {
1420 conn->err_code = CO_ER_SOCKS4_RECV;
1421 }
1422 goto fail;
1423 }
1424
1425 if (ret > 0) {
1426 if (ret == SOCKS4_HS_RSP_LEN) {
1427 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1428 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1429 }else{
1430 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]);
1431 }
1432 } else {
1433 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1434 }
1435
1436 if (ret < 0) {
1437 if (errno == EINTR) {
1438 continue;
1439 }
1440 if (errno == EAGAIN) {
1441 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001442 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001443 }
1444 goto recv_abort;
1445 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001446 break;
1447 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001448
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001449 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1450
Alexander Liu2a54bb72019-05-22 19:44:48 +08001451 if (ret < SOCKS4_HS_RSP_LEN) {
1452 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1453 * we are not able to read enough data.
1454 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001455 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001456 }
1457
1458 /*
1459 * Base on the SOCSK4 protocol:
1460 *
1461 * +----+----+----+----+----+----+----+----+
1462 * | VN | CD | DSTPORT | DSTIP |
1463 * +----+----+----+----+----+----+----+----+
1464 * # of bytes: 1 1 2 4
1465 * VN is the version of the reply code and should be 0. CD is the result
1466 * code with one of the following values:
1467 * 90: request granted
1468 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001469 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001470 * 93: request rejected because the client program and identd report different user-ids
1471 * The remaining fields are ignored.
1472 */
1473 if (line[1] != 90) {
1474 conn->flags &= ~CO_FL_SOCKS4_RECV;
1475
1476 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1477 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1478 if (conn->err_code == CO_ER_NONE) {
1479 conn->err_code = CO_ER_SOCKS4_DENY;
1480 }
1481 goto fail;
1482 }
1483
1484 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001485 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001486 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1487 if (ret < 0 && errno == EINTR) {
1488 continue;
1489 }
1490 if (ret != SOCKS4_HS_RSP_LEN) {
1491 if (conn->err_code == CO_ER_NONE) {
1492 conn->err_code = CO_ER_SOCKS4_RECV;
1493 }
1494 goto fail;
1495 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001496 break;
1497 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001498
1499 conn->flags &= ~CO_FL_SOCKS4_RECV;
1500 return 1;
1501
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001502 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001503 return 0;
1504
Alexander Liu2a54bb72019-05-22 19:44:48 +08001505 recv_abort:
1506 if (conn->err_code == CO_ER_NONE) {
1507 conn->err_code = CO_ER_SOCKS4_ABORT;
1508 }
1509 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1510 goto fail;
1511
1512 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001513 conn->flags |= CO_FL_ERROR;
1514 return 0;
1515}
1516
Willy Tarreaue59b5162021-05-08 14:06:09 +02001517/* Lists the known proto mux on <out> */
1518void list_mux_proto(FILE *out)
1519{
1520 struct mux_proto_list *item;
1521 struct buffer *chk = get_trash_chunk();
1522 struct ist proto;
1523 char *mode, *side;
1524
1525 fprintf(out, "Available multiplexer protocols :\n"
1526 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
1527 list_for_each_entry(item, &mux_proto_list.list, list) {
1528 proto = item->token;
1529
1530 if (item->mode == PROTO_MODE_ANY)
1531 mode = "TCP|HTTP";
1532 else if (item->mode == PROTO_MODE_TCP)
1533 mode = "TCP";
1534 else if (item->mode == PROTO_MODE_HTTP)
1535 mode = "HTTP";
1536 else
1537 mode = "NONE";
1538
1539 if (item->side == PROTO_SIDE_BOTH)
1540 side = "FE|BE";
1541 else if (item->side == PROTO_SIDE_FE)
1542 side = "FE";
1543 else if (item->side == PROTO_SIDE_BE)
1544 side = "BE";
1545 else
1546 side = "NONE";
1547
1548 chunk_reset(chk);
1549 if (item->mux->flags & MX_FL_HTX)
1550 chunk_strcpy(chk, "HTX");
1551 if (item->mux->flags & MX_FL_CLEAN_ABRT)
1552 chunk_appendf(chk, "%sCLEAN_ABRT", (b_data(chk) ? "|": ""));
1553 if (item->mux->flags & MX_FL_HOL_RISK)
1554 chunk_appendf(chk, "%sHOL_RISK", (b_data(chk) ? "|": ""));
1555 if (item->mux->flags & MX_FL_NO_UPG)
1556 chunk_appendf(chk, "%sNO_UPG", (b_data(chk) ? "|": ""));
1557
1558 fprintf(out, " %15s : mode=%-10s side=%-8s mux=%-8s flags=%.*s\n",
1559 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name,
1560 (int)b_data(chk), b_orig(chk));
1561 }
1562}
1563
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001564/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001565int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001566{
1567 int ret = 0;
1568
1569 if (srv && (srv->pp_opts & SRV_PP_V2)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001570 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
David Safb76832014-05-08 23:42:08 -04001571 }
1572 else {
Willy Tarreau226572f2019-07-17 14:46:00 +02001573 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1574 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001575 else
1576 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1577 }
1578
1579 return ret;
1580}
1581
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001582/* Makes a PROXY protocol line from the two addresses. The output is sent to
1583 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1584 * It returns the number of bytes composing this line (including the trailing
1585 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001586 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1587 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001588 */
David Safb76832014-05-08 23:42:08 -04001589int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001590{
1591 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001592 char * protocol;
1593 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1594 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1595 in_port_t src_port;
1596 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001597
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001598 if ( !src
1599 || !dst
1600 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1601 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1602 /* unknown family combination */
1603 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001604 if (ret >= buf_len)
1605 return 0;
1606
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001607 return ret;
1608 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001609
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001610 /* IPv4 for both src and dst */
1611 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1612 protocol = "TCP4";
1613 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001614 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001615 src_port = ((struct sockaddr_in *)src)->sin_port;
1616 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001617 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001618 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001619 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001620 /* IPv6 for at least one of src and dst */
1621 else {
1622 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001623
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001624 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001625
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001626 if (src->ss_family == AF_INET) {
1627 /* Convert src to IPv6 */
1628 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1629 src_port = ((struct sockaddr_in *)src)->sin_port;
1630 }
1631 else {
1632 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1633 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1634 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001635
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001636 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001637 return 0;
1638
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001639 if (dst->ss_family == AF_INET) {
1640 /* Convert dst to IPv6 */
1641 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1642 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1643 }
1644 else {
1645 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1646 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1647 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001648
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001649 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001650 return 0;
1651 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001652
1653 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1654 if (ret >= buf_len)
1655 return 0;
1656
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001657 return ret;
1658}
David Safb76832014-05-08 23:42:08 -04001659
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001660static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001661{
1662 struct tlv *tlv;
1663
1664 if (!dest || (length + sizeof(*tlv) > dest_len))
1665 return 0;
1666
1667 tlv = (struct tlv *)dest;
1668
1669 tlv->type = type;
1670 tlv->length_hi = length >> 8;
1671 tlv->length_lo = length & 0x00ff;
1672 memcpy(tlv->value, value, length);
1673 return length + sizeof(*tlv);
1674}
David Safb76832014-05-08 23:42:08 -04001675
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001676/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001677int 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 -04001678{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001679 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001680 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001681 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001682 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001683 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001684 struct sockaddr_storage *src = &null_addr;
1685 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001686 const char *value;
1687 int value_len;
David Safb76832014-05-08 23:42:08 -04001688
1689 if (buf_len < PP2_HEADER_LEN)
1690 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001691 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001692
Willy Tarreau226572f2019-07-17 14:46:00 +02001693 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1694 src = remote->src;
1695 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001696 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001697
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001698 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1699 if ( !src
1700 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001701 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001702 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1703 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001704 if (buf_len < PP2_HDR_LEN_UNSPEC)
1705 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001706 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1707 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001708 ret = PP2_HDR_LEN_UNSPEC;
1709 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001710 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001711 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001712 /* IPv4 for both src and dst */
1713 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1714 if (buf_len < PP2_HDR_LEN_INET)
1715 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001716 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1717 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1718 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1719 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1720 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1721 ret = PP2_HDR_LEN_INET;
1722 }
1723 /* IPv6 for at least one of src and dst */
1724 else {
1725 struct in6_addr tmp;
1726
1727 if (buf_len < PP2_HDR_LEN_INET6)
1728 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001729 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1730 if (src->ss_family == AF_INET) {
1731 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1732 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1733 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1734 }
1735 else {
1736 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1737 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1738 }
1739 if (dst->ss_family == AF_INET) {
1740 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1741 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001742 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001743 }
1744 else {
1745 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1746 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1747 }
1748
1749 ret = PP2_HDR_LEN_INET6;
1750 }
1751 }
David Safb76832014-05-08 23:42:08 -04001752
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001753 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1754 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001755
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001756 if ((buf_len - ret) < sizeof(struct tlv))
1757 return 0;
1758 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1759 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1760 }
1761
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001762 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001763 if ((buf_len - ret) < sizeof(struct tlv))
1764 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001765 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001766 }
1767
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001768 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001769 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001770 if (remote && isttest(remote->proxy_authority)) {
1771 value = istptr(remote->proxy_authority);
1772 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001773 }
1774#ifdef USE_OPENSSL
1775 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001776 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001777 value_len = strlen(value);
1778 }
1779#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001780 if (value) {
1781 if ((buf_len - ret) < sizeof(struct tlv))
1782 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001783 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001784 }
1785 }
1786
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001787 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001788 struct session* sess = strm_sess(strm);
1789 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1790
1791 value = unique_id.ptr;
1792 value_len = unique_id.len;
1793
1794 if (value_len >= 0) {
1795 if ((buf_len - ret) < sizeof(struct tlv))
1796 return 0;
1797 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1798 }
1799 }
1800
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001801#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001802 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001803 struct tlv_ssl *tlv;
1804 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001805
David Safb76832014-05-08 23:42:08 -04001806 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1807 return 0;
1808 tlv = (struct tlv_ssl *)&buf[ret];
1809 memset(tlv, 0, sizeof(struct tlv_ssl));
1810 ssl_tlv_len += sizeof(struct tlv_ssl);
1811 tlv->tlv.type = PP2_TYPE_SSL;
Willy Tarreau1057bee2021-10-06 11:38:44 +02001812 if (conn_is_ssl(remote)) {
David Safb76832014-05-08 23:42:08 -04001813 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001814 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001815 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001816 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 -04001817 }
Dave McCowan328fb582014-07-30 10:39:13 -04001818 if (ssl_sock_get_cert_used_sess(remote)) {
1819 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001820 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001821 if (ssl_sock_get_cert_used_conn(remote))
1822 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001823 }
1824 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001825 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001826 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001827 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1828 cn_trash->data,
1829 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001830 }
1831 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001832 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001833 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001834 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1836 pkey_trash->data,
1837 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001838 }
1839 }
1840 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1841 value = ssl_sock_get_cert_sig(remote);
1842 if (value) {
1843 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1844 }
1845 }
1846 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1847 value = ssl_sock_get_cipher_name(remote);
1848 if (value) {
1849 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1850 }
1851 }
David Safb76832014-05-08 23:42:08 -04001852 }
1853 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1854 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1855 ret += ssl_tlv_len;
1856 }
1857#endif
1858
Willy Tarreaue5733232019-05-22 19:24:06 +02001859#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001860 if (remote && (remote->proxy_netns)) {
1861 if ((buf_len - ret) < sizeof(struct tlv))
1862 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001863 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 +01001864 }
1865#endif
1866
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001867 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001868
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001869 if (tlv_crc32c_p) {
1870 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1871 }
1872
David Safb76832014-05-08 23:42:08 -04001873 return ret;
1874}
Emeric Brun4f603012017-01-05 15:11:44 +01001875
Willy Tarreau119e50e2020-05-22 13:53:29 +02001876/* returns 0 on success */
1877static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001878 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02001879 char **err)
1880{
1881 if (too_many_args(0, args, err, NULL))
1882 return -1;
1883 pp2_never_send_local = 1;
1884 return 0;
1885}
1886
Willy Tarreaud943a042021-06-16 17:35:20 +02001887/* extracts some info from the connection and appends them to buffer <buf>. The
1888 * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source
1889 * when set, destination when set, are printed in a compact human-readable format
1890 * fitting on a single line. This is handy to complete traces or debug output.
1891 * It is permitted to pass a NULL conn pointer. The number of characters emitted
1892 * is returned. A prefix <pfx> might be prepended before the first field if not
1893 * NULL.
1894 */
1895int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx)
1896{
1897 const struct listener *li;
1898 const struct server *sv;
1899 const struct proxy *px;
1900 char addr[40];
1901 int old_len = buf->data;
1902
1903 if (!conn)
1904 return 0;
1905
1906 chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN");
1907
1908 if ((li = objt_listener(conn->target)))
1909 chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id);
1910 else if ((sv = objt_server(conn->target)))
1911 chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id);
1912 else if ((px = objt_proxy(conn->target)))
1913 chunk_appendf(buf, " be=%s", px->id);
1914
1915 chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn));
1916
1917 if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr)))
1918 chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src));
1919
1920 if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr)))
1921 chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst));
1922
1923 return buf->data - old_len;
1924}
1925
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001926/* return the major HTTP version as 1 or 2 depending on how the request arrived
1927 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001928 *
1929 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001930 */
1931static int
1932smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1933{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02001934 struct connection *conn = NULL;
1935
1936 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
1937 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
1938 else
1939 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1940 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001941
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001942 /* No connection or a connection with a RAW muxx */
1943 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
1944 return 0;
1945
1946 /* No mux install, this may change */
1947 if (!conn->mux) {
1948 smp->flags |= SMP_F_MAY_CHANGE;
1949 return 0;
1950 }
1951
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001952 smp->data.type = SMP_T_SINT;
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001953 smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001954 return 1;
1955}
1956
Emeric Brun4f603012017-01-05 15:11:44 +01001957/* fetch if the received connection used a PROXY protocol header */
1958int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1959{
1960 struct connection *conn;
1961
1962 conn = objt_conn(smp->sess->origin);
1963 if (!conn)
1964 return 0;
1965
Willy Tarreau911db9b2020-01-23 16:27:54 +01001966 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001967 smp->flags |= SMP_F_MAY_CHANGE;
1968 return 0;
1969 }
1970
1971 smp->flags = 0;
1972 smp->data.type = SMP_T_BOOL;
1973 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1974
1975 return 1;
1976}
1977
Geoff Simmons7185b782019-08-27 18:31:16 +02001978/* fetch the authority TLV from a PROXY protocol header */
1979int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1980{
1981 struct connection *conn;
1982
1983 conn = objt_conn(smp->sess->origin);
1984 if (!conn)
1985 return 0;
1986
Willy Tarreau911db9b2020-01-23 16:27:54 +01001987 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001988 smp->flags |= SMP_F_MAY_CHANGE;
1989 return 0;
1990 }
1991
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001992 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02001993 return 0;
1994
1995 smp->flags = 0;
1996 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001997 smp->data.u.str.area = istptr(conn->proxy_authority);
1998 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02001999
2000 return 1;
2001}
2002
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002003/* fetch the unique ID TLV from a PROXY protocol header */
2004int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2005{
2006 struct connection *conn;
2007
2008 conn = objt_conn(smp->sess->origin);
2009 if (!conn)
2010 return 0;
2011
2012 if (conn->flags & CO_FL_WAIT_XPRT) {
2013 smp->flags |= SMP_F_MAY_CHANGE;
2014 return 0;
2015 }
2016
2017 if (!isttest(conn->proxy_unique_id))
2018 return 0;
2019
2020 smp->flags = 0;
2021 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01002022 smp->data.u.str.area = istptr(conn->proxy_unique_id);
2023 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002024
2025 return 1;
2026}
2027
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002028/* fetch the error code of a connection */
2029int smp_fetch_fc_conn_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2030{
2031 struct connection *conn;
2032
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002033 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2034 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2035 else
2036 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2037 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2038
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002039 if (!conn)
2040 return 0;
2041
2042 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2043 smp->flags |= SMP_F_MAY_CHANGE;
2044 return 0;
2045 }
2046
2047 smp->flags = 0;
2048 smp->data.type = SMP_T_SINT;
2049 smp->data.u.sint = (unsigned long long int)conn->err_code;
2050
2051 return 1;
2052}
2053
2054/* fetch a string representation of the error code of a connection */
2055int smp_fetch_fc_conn_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2056{
2057 struct connection *conn;
2058 const char *err_code_str;
2059
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002060 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2061 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2062 else
2063 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2064 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2065
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002066 if (!conn)
2067 return 0;
2068
2069 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2070 smp->flags |= SMP_F_MAY_CHANGE;
2071 return 0;
2072 }
2073
2074 err_code_str = conn_err_code_str(conn);
2075
2076 if (!err_code_str)
2077 return 0;
2078
2079 smp->flags = 0;
2080 smp->data.type = SMP_T_STR;
2081 smp->data.u.str.area = (char*)err_code_str;
2082 smp->data.u.str.data = strlen(err_code_str);
2083
2084 return 1;
2085}
2086
Emeric Brun4f603012017-01-05 15:11:44 +01002087/* Note: must not be declared <const> as its list will be overwritten.
2088 * Note: fetches that may return multiple types must be declared as the lowest
2089 * common denominator, the type that can be casted into all other ones. For
2090 * instance v4/v6 must be declared v4.
2091 */
2092static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002093 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Jérôme Magnin86577422018-12-07 09:03:11 +01002094 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01002095 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02002096 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002097 { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002098 { "fc_conn_err", smp_fetch_fc_conn_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
2099 { "fc_conn_err_str", smp_fetch_fc_conn_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002100 { "bc_conn_err", smp_fetch_fc_conn_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
2101 { "bc_conn_err_str", smp_fetch_fc_conn_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01002102 { /* END */ },
2103}};
2104
Willy Tarreau0108d902018-11-25 19:14:37 +01002105INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02002106
2107static struct cfg_kw_list cfg_kws = {ILH, {
2108 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
2109 { /* END */ },
2110}};
2111
2112INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002113
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002114/* private function to handle sockaddr as input for connection hash */
2115static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
2116 char *buf, size_t *idx,
2117 enum conn_hash_params_t *hash_flags,
2118 enum conn_hash_params_t param_type_addr,
2119 enum conn_hash_params_t param_type_port)
2120{
2121 struct sockaddr_in *addr;
2122 struct sockaddr_in6 *addr6;
2123
2124 switch (ss->ss_family) {
2125 case AF_INET:
2126 addr = (struct sockaddr_in *)ss;
2127
2128 conn_hash_update(buf, idx,
2129 &addr->sin_addr, sizeof(addr->sin_addr),
2130 hash_flags, param_type_addr);
2131
2132 if (addr->sin_port) {
2133 conn_hash_update(buf, idx,
2134 &addr->sin_port, sizeof(addr->sin_port),
2135 hash_flags, param_type_port);
2136 }
2137
2138 break;
2139
2140 case AF_INET6:
2141 addr6 = (struct sockaddr_in6 *)ss;
2142
2143 conn_hash_update(buf, idx,
2144 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
2145 hash_flags, param_type_addr);
2146
2147 if (addr6->sin6_port) {
2148 conn_hash_update(buf, idx,
2149 &addr6->sin6_port, sizeof(addr6->sin6_port),
2150 hash_flags, param_type_port);
2151 }
2152
2153 break;
2154 }
2155}
2156
Willy Tarreaue5983ff2021-10-06 17:14:49 +02002157/* Generate the hash of a connection with params as input
2158 * Each non-null field of params is taken into account for the hash calcul.
2159 */
2160uint64_t conn_hash_prehash(char *buf, size_t size)
2161{
2162 return XXH64(buf, size, 0);
2163}
2164
2165/* Append <data> into <buf> at <idx> offset in preparation for connection hash
2166 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
2167 * are updated with <type> for the hash header.
2168 */
2169void conn_hash_update(char *buf, size_t *idx,
2170 const void *data, size_t size,
2171 enum conn_hash_params_t *flags,
2172 enum conn_hash_params_t type)
2173{
2174 memcpy(&buf[*idx], data, size);
2175 *idx += size;
2176 *flags |= type;
2177}
2178
2179uint64_t conn_hash_digest(char *buf, size_t bufsize,
2180 enum conn_hash_params_t flags)
2181{
2182 const uint64_t flags_u64 = (uint64_t)flags;
2183 const uint64_t hash = XXH64(buf, bufsize, 0);
2184
2185 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
2186}
2187
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002188uint64_t conn_calculate_hash(const struct conn_hash_params *params)
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002189{
2190 char *buf;
2191 size_t idx = 0;
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002192 uint64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002193 enum conn_hash_params_t hash_flags = 0;
2194
2195 buf = trash.area;
2196
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01002197 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01002198
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002199 if (params->sni_prehash) {
2200 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002201 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002202 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
2203 }
2204
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002205 if (params->dst_addr) {
2206 conn_calculate_hash_sockaddr(params->dst_addr,
2207 buf, &idx, &hash_flags,
2208 CONN_HASH_PARAMS_TYPE_DST_ADDR,
2209 CONN_HASH_PARAMS_TYPE_DST_PORT);
2210 }
2211
Amaury Denoyelled10a2002021-02-11 19:45:19 +01002212 if (params->src_addr) {
2213 conn_calculate_hash_sockaddr(params->src_addr,
2214 buf, &idx, &hash_flags,
2215 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
2216 CONN_HASH_PARAMS_TYPE_SRC_PORT);
2217 }
2218
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002219 if (params->proxy_prehash) {
2220 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002221 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002222 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
2223 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002224
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002225 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002226 return hash;
2227}
Amaury Denoyelle48372932021-09-16 12:15:12 +02002228
2229/* Handler of the task of mux_stopping_data.
2230 * Called on soft-stop.
2231 */
2232static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state)
2233{
2234 struct connection *conn, *back;
2235
2236 list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) {
2237 if (conn->mux && conn->mux->wake)
2238 conn->mux->wake(conn);
2239 }
2240
2241 return t;
2242}
2243
2244static int allocate_mux_cleanup(void)
2245{
2246 /* allocates the thread bound mux_stopping_data task */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002247 mux_stopping_data[tid].task = task_new_here();
Amaury Denoyelle48372932021-09-16 12:15:12 +02002248 if (!mux_stopping_data[tid].task) {
2249 ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid);
2250 return 0;
2251 }
2252
2253 mux_stopping_data[tid].task->process = mux_stopping_process;
2254 LIST_INIT(&mux_stopping_data[tid].list);
2255
2256 return 1;
2257}
2258REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup);
2259
2260static int deallocate_mux_cleanup(void)
2261{
2262 task_destroy(mux_stopping_data[tid].task);
2263 return 1;
2264}
2265REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup);