blob: bec639c8c5521ee1df1b8f615b2c6fda3e5914d3 [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);
Willy Tarreau930428c2021-10-06 18:27:28 +020057}
58
Olivier Houchard477902b2020-01-22 18:08:48 +010059int conn_create_mux(struct connection *conn)
60{
Olivier Houchard477902b2020-01-22 18:08:48 +010061 if (conn_is_back(conn)) {
62 struct server *srv;
63 struct conn_stream *cs = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020064 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010065
66 if (conn->flags & CO_FL_ERROR)
67 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010068
Christopher Faulet14cd3162020-04-16 14:50:06 +020069 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010070 if (conn_install_mux_chk(conn, conn->ctx, sess) < 0)
Christopher Faulet14cd3162020-04-16 14:50:06 +020071 goto fail;
72 }
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010073 else if (conn_install_mux_be(conn, conn->ctx, sess) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010074 goto fail;
75 srv = objt_server(conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020076
77 /* If we're doing http-reuse always, and the connection is not
78 * private with available streams (an http2 connection), add it
79 * to the available list, so that others can use it right
80 * away. If the connection is private, add it in the session
81 * server list.
82 */
Christopher Faulet2883fcf2020-07-01 14:59:43 +020083 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) &&
Christopher Fauletaa278532020-06-30 14:47:46 +020084 !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0)
Willy Tarreau430bf4a2021-03-04 09:45:32 +010085 ebmb_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Christopher Faulet08016ab2020-07-01 16:10:06 +020086 else if (conn->flags & CO_FL_PRIVATE) {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +050087 /* If it fail now, the same will be done in mux->detach() callback */
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010088 session_add_conn(sess, conn, conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020089 }
Olivier Houchard477902b2020-01-22 18:08:48 +010090 return 0;
91fail:
92 /* let the upper layer know the connection failed */
93 cs->data_cb->wake(cs);
94 return -1;
95 } else
96 return conn_complete_session(conn);
97
Willy Tarreau930428c2021-10-06 18:27:28 +020098}
99
100/* This is used at the end of the socket IOCB to possibly create the mux if it
101 * was not done yet, or wake it up if flags changed compared to old_flags or if
102 * need_wake insists on this. It returns <0 if the connection was destroyed and
103 * must not be used, >=0 otherwise.
104 */
105int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake)
106{
107 int ret = 0;
108
109 /* If we don't yet have a mux, that means we were waiting for
110 * information to create one, typically from the ALPN. If we're
111 * done with the handshake, attempt to create one.
112 */
113 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) {
114 ret = conn_create_mux(conn);
115 if (ret < 0)
116 goto done;
117 }
118
119 /* The wake callback is normally used to notify the data layer about
120 * data layer activity (successful send/recv), connection establishment,
121 * shutdown and fatal errors. We need to consider the following
122 * situations to wake up the data layer :
123 * - change among the CO_FL_NOTIFY_DONE flags :
124 * SOCK_{RD,WR}_SH, ERROR,
125 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
126 * end of handshake and transition to CONNECTED
127 * - raise of CONNECTED with HANDSHAKE down
128 * - end of HANDSHAKE with CONNECTED set
129 * - regular data layer activity
130 *
131 * One tricky case is the wake up on read0 or error on an idle
132 * backend connection, that can happen on a connection that is still
133 * polled while at the same moment another thread is about to perform a
134 * takeover. The solution against this is to remove the connection from
135 * the idle list if it was in it, and possibly reinsert it at the end
136 * if the connection remains valid. The cost is non-null (locked tree
137 * removal) but remains low given that this is extremely rarely called.
138 * In any case it's guaranteed by the FD's thread_mask that we're
139 * called from the same thread the connection is queued in.
140 *
141 * Note that the wake callback is allowed to release the connection and
142 * the fd (and return < 0 in this case).
143 */
144 if ((forced_wake ||
145 ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) ||
146 ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
147 conn->mux && conn->mux->wake) {
148 uint conn_in_list = conn->flags & CO_FL_LIST_MASK;
149 struct server *srv = objt_server(conn->target);
150
151 if (conn_in_list) {
152 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
153 conn_delete_from_tree(&conn->hash_node->node);
154 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
155 }
156
157 ret = conn->mux->wake(conn);
158 if (ret < 0)
159 goto done;
160
161 if (conn_in_list) {
162 struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ?
163 &srv->per_thr[tid].safe_conns :
164 &srv->per_thr[tid].idle_conns;
165
166 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
167 ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash));
168 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
169 }
170 }
171 done:
172 return ret;
Olivier Houchard477902b2020-01-22 18:08:48 +0100173}
174
Willy Tarreauaac777f2021-10-06 18:48:28 +0200175/* Change the mux for the connection.
176 * The caller should make sure he's not subscribed to the underlying XPRT.
177 */
178int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
179 struct ist mux_proto, int mode)
180{
181 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
182 const struct mux_ops *old_mux, *new_mux;
183 void *old_mux_ctx;
184 const char *alpn_str = NULL;
185 int alpn_len = 0;
186
187 if (!mux_proto.len) {
188 conn_get_alpn(conn, &alpn_str, &alpn_len);
189 mux_proto = ist2(alpn_str, alpn_len);
190 }
191 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
192 old_mux = conn->mux;
193
194 /* No mux found */
195 if (!new_mux)
196 return -1;
197
198 /* Same mux, nothing to do */
199 if (old_mux == new_mux)
200 return 0;
201
202 old_mux_ctx = conn->ctx;
203 conn->mux = new_mux;
204 conn->ctx = ctx;
205 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
206 /* The mux upgrade failed, so restore the old mux */
207 conn->ctx = old_mux_ctx;
208 conn->mux = old_mux;
209 return -1;
210 }
211
212 /* The mux was upgraded, destroy the old one */
213 *buf = BUF_NULL;
214 old_mux->destroy(old_mux_ctx);
215 return 0;
216}
217
218/* installs the best mux for incoming connection <conn> using the upper context
219 * <ctx>. If the mux protocol is forced, we use it to find the best
220 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
221 */
222int conn_install_mux_fe(struct connection *conn, void *ctx)
223{
224 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
225 const struct mux_ops *mux_ops;
226
227 if (bind_conf->mux_proto)
228 mux_ops = bind_conf->mux_proto->mux;
229 else {
230 struct ist mux_proto;
231 const char *alpn_str = NULL;
232 int alpn_len = 0;
233 int mode;
234
235 if (bind_conf->frontend->mode == PR_MODE_HTTP)
236 mode = PROTO_MODE_HTTP;
237 else
238 mode = PROTO_MODE_TCP;
239
240 conn_get_alpn(conn, &alpn_str, &alpn_len);
241 mux_proto = ist2(alpn_str, alpn_len);
242 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
243 if (!mux_ops)
244 return -1;
245 }
246 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
247}
248
249/* installs the best mux for outgoing connection <conn> using the upper context
250 * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns
251 * < 0 on error.
252 */
253int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess)
254{
255 struct server *srv = objt_server(conn->target);
256 struct proxy *prx = objt_proxy(conn->target);
257 const struct mux_ops *mux_ops;
258
259 if (srv)
260 prx = srv->proxy;
261
262 if (!prx) // target must be either proxy or server
263 return -1;
264
265 if (srv && srv->mux_proto)
266 mux_ops = srv->mux_proto->mux;
267 else {
268 struct ist mux_proto;
269 const char *alpn_str = NULL;
270 int alpn_len = 0;
271 int mode;
272
273 if (prx->mode == PR_MODE_HTTP)
274 mode = PROTO_MODE_HTTP;
275 else
276 mode = PROTO_MODE_TCP;
277
278 conn_get_alpn(conn, &alpn_str, &alpn_len);
279 mux_proto = ist2(alpn_str, alpn_len);
280
281 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
282 if (!mux_ops)
283 return -1;
284 }
285 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
286}
287
288/* installs the best mux for outgoing connection <conn> for a check using the
289 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
290 * find the best mux. Returns < 0 on error.
291 */
292int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
293{
294 struct check *check = objt_check(sess->origin);
295 struct server *srv = objt_server(conn->target);
296 struct proxy *prx = objt_proxy(conn->target);
297 const struct mux_ops *mux_ops;
298
299 if (!check) // Check must be defined
300 return -1;
301
302 if (srv)
303 prx = srv->proxy;
304
305 if (!prx) // target must be either proxy or server
306 return -1;
307
308 if (check->mux_proto)
309 mux_ops = check->mux_proto->mux;
310 else {
311 struct ist mux_proto;
312 const char *alpn_str = NULL;
313 int alpn_len = 0;
314 int mode;
315
316 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
317 mode = PROTO_MODE_HTTP;
318 else
319 mode = PROTO_MODE_TCP;
320
321 conn_get_alpn(conn, &alpn_str, &alpn_len);
322 mux_proto = ist2(alpn_str, alpn_len);
323
324 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
325 if (!mux_ops)
326 return -1;
327 }
328 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
329}
330
331/* Initializes all required fields for a new connection. Note that it does the
332 * minimum acceptable initialization for a connection that already exists and
333 * is about to be reused. It also leaves the addresses untouched, which makes
334 * it usable across connection retries to reset a connection to a known state.
335 */
336void conn_init(struct connection *conn, void *target)
337{
338 conn->obj_type = OBJ_TYPE_CONN;
339 conn->flags = CO_FL_NONE;
340 conn->mux = NULL;
341 conn->ctx = NULL;
342 conn->owner = NULL;
343 conn->send_proxy_ofs = 0;
344 conn->handle.fd = DEAD_FD_MAGIC;
345 conn->err_code = CO_ER_NONE;
346 conn->target = target;
347 conn->destroy_cb = NULL;
348 conn->proxy_netns = NULL;
349 MT_LIST_INIT(&conn->toremove_list);
350 if (conn_is_back(conn))
351 LIST_INIT(&conn->session_list);
352 else
353 LIST_INIT(&conn->stopping_list);
354 conn->subs = NULL;
355 conn->src = NULL;
356 conn->dst = NULL;
357 conn->proxy_authority = IST_NULL;
358 conn->proxy_unique_id = IST_NULL;
359 conn->qc = NULL;
360 conn->hash_node = NULL;
361 conn->xprt = NULL;
362}
363
364/* Tries to allocate a new connection and initialized its main fields. The
365 * connection is returned on success, NULL on failure. The connection must
366 * be released using pool_free() or conn_free().
367 */
368struct connection *conn_new(void *target)
369{
370 struct connection *conn;
371 struct conn_hash_node *hash_node;
372
373 conn = pool_alloc(pool_head_connection);
374 if (unlikely(!conn))
375 return NULL;
376
377 conn_init(conn, target);
378
379 if (conn_is_back(conn)) {
380 if (obj_type(target) == OBJ_TYPE_SERVER)
381 srv_use_conn(__objt_server(target), conn);
382
383 hash_node = conn_alloc_hash_node(conn);
384 if (unlikely(!hash_node)) {
385 pool_free(pool_head_connection, conn);
386 return NULL;
387 }
388
389 conn->hash_node = hash_node;
390 }
391
392 return conn;
393}
394
395/* Releases a connection previously allocated by conn_new() */
396void conn_free(struct connection *conn)
397{
398 /* If the connection is owned by the session, remove it from its list
399 */
400 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
401 session_unown_conn(conn->owner, conn);
402 }
403 else if (!(conn->flags & CO_FL_PRIVATE)) {
404 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
405 srv_release_conn(__objt_server(conn->target), conn);
406 }
407
408 /* Remove the conn from toremove_list.
409 *
410 * This is needed to prevent a double-free in case the connection was
411 * already scheduled from cleaning but is freed before via another
412 * call.
413 */
414 MT_LIST_DELETE(&conn->toremove_list);
415
416 sockaddr_free(&conn->src);
417 sockaddr_free(&conn->dst);
418
419 pool_free(pool_head_authority, istptr(conn->proxy_authority));
420 conn->proxy_authority = IST_NULL;
421
422 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
423 conn->proxy_unique_id = IST_NULL;
424
425 pool_free(pool_head_conn_hash_node, conn->hash_node);
426 conn->hash_node = NULL;
427
428 /* By convention we always place a NULL where the ctx points to if the
429 * mux is null. It may have been used to store the connection as a
430 * stream_interface's end point for example.
431 */
432 if (conn->ctx != NULL && conn->mux == NULL)
433 *(void **)conn->ctx = NULL;
434
435 conn_force_unsubscribe(conn);
436 pool_free(pool_head_connection, conn);
437}
438
Willy Tarreau8de90c72021-10-06 19:11:10 +0200439struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
440{
441 struct conn_hash_node *hash_node = NULL;
442
443 hash_node = pool_zalloc(pool_head_conn_hash_node);
444 if (unlikely(!hash_node))
445 return NULL;
446
447 hash_node->conn = conn;
448
449 return hash_node;
450}
451
452/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
453 * returns it. If <sap> is NULL, the address is always allocated and returned.
454 * if <sap> is non-null, an address will only be allocated if it points to a
455 * non-null pointer. In this case the allocated address will be assigned there.
456 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
457 * into the allocated address. In both situations the new pointer is returned.
458 */
459struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
460{
461 struct sockaddr_storage *sa;
462
463 if (sap && *sap)
464 return *sap;
465
466 sa = pool_alloc(pool_head_sockaddr);
467 if (sa && orig && len > 0)
468 memcpy(sa, orig, len);
469 if (sap)
470 *sap = sa;
471 return sa;
472}
473
474/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
475 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
476 * there.
477 */
478void sockaddr_free(struct sockaddr_storage **sap)
479{
480 if (!sap)
481 return;
482 pool_free(pool_head_sockaddr, *sap);
483 *sap = NULL;
484}
485
486/* Releases a conn_stream previously allocated by cs_new(), as well as any
487 * buffer it would still hold.
488 */
489void cs_free(struct conn_stream *cs)
490{
491
492 pool_free(pool_head_connstream, cs);
493}
494
495/* Tries to allocate a new conn_stream and initialize its main fields. If
496 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
497 * and assigned to cs->conn ; this connection will then have to be released
498 * using pool_free() or conn_free(). The conn_stream is initialized and added
499 * to the mux's stream list on success, then returned. On failure, nothing is
500 * allocated and NULL is returned.
501 */
502struct conn_stream *cs_new(struct connection *conn, void *target)
503{
504 struct conn_stream *cs;
505
506 cs = pool_alloc(pool_head_connstream);
507 if (unlikely(!cs))
508 return NULL;
509
510 if (!conn) {
511 conn = conn_new(target);
512 if (unlikely(!conn)) {
513 cs_free(cs);
514 return NULL;
515 }
516 }
517
518 cs_init(cs, conn);
519 return cs;
520}
521
Willy Tarreauaac777f2021-10-06 18:48:28 +0200522/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
523 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
524 * call the xprt's add_xprt() method.
525 * Returns 0 on success, or non-zero on failure.
526 */
527int xprt_add_hs(struct connection *conn)
528{
529 void *xprt_ctx = NULL;
530 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
531 void *nextxprt_ctx = NULL;
532 const struct xprt_ops *nextxprt_ops = NULL;
533
534 if (conn->flags & CO_FL_ERROR)
535 return -1;
536 if (ops->init(conn, &xprt_ctx) < 0)
537 return -1;
538 if (conn->xprt == xprt_get(XPRT_RAW)) {
539 nextxprt_ctx = conn->xprt_ctx;
540 nextxprt_ops = conn->xprt;
541 conn->xprt_ctx = xprt_ctx;
542 conn->xprt = ops;
543 } else {
544 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
545 &nextxprt_ctx, &nextxprt_ops) != 0) {
546 ops->close(conn, xprt_ctx);
547 return -1;
548 }
549 }
550 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
551 ops->close(conn, xprt_ctx);
552 return -1;
553 }
554 return 0;
555}
556
557/* returns a human-readable error code for conn->err_code, or NULL if the code
558 * is unknown.
559 */
560const char *conn_err_code_str(struct connection *c)
561{
562 switch (c->err_code) {
563 case CO_ER_NONE: return "Success";
564
565 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
566 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
567 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
568 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
569 case CO_ER_NOPROTO: return "Protocol or address family not supported";
570 case CO_ER_SOCK_ERR: return "General socket error";
571 case CO_ER_PORT_RANGE: return "Source port range exhausted";
572 case CO_ER_CANT_BIND: return "Can't bind to source address";
573 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
574 case CO_ER_ADDR_INUSE: return "Local source address already in use";
575
576 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
577 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
578 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
579 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
580 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
581 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
582 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
583
584 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
585 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
586 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
587 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
588 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
589 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
590
591 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
592 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
593 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
594 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
595 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
596 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
597 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
598 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
599 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
600 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
601 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
602 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
603 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
604 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
605 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
606
607 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
608 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
609 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
610 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
611
612 case CO_ERR_SSL_FATAL: return "SSL fatal error";
613 }
614 return NULL;
615}
616
Willy Tarreauff3e6482015-03-12 23:56:52 +0100617/* Send a message over an established connection. It makes use of send() and
618 * returns the same return code and errno. If the socket layer is not ready yet
619 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
620 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
621 * EMSGSIZE if called with a zero length message. The purpose is to simplify
622 * some rare attempts to directly write on the socket from above the connection
623 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
624 * It automatically retries on EINTR. Other errors cause the connection to be
625 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +0100626 * first one which is the connection instead of the file descriptor. <flags>
627 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100628 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100629int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100630{
Willy Tarreau827fee72020-12-11 15:26:55 +0100631 const struct buffer buffer = b_make((char*)buf, len, 0, len);
632 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100633 int ret;
634
635 ret = -1;
636 errno = ENOTSOCK;
637
638 if (conn->flags & CO_FL_SOCK_WR_SH)
639 goto fail;
640
641 if (!conn_ctrl_ready(conn))
642 goto fail;
643
644 errno = EMSGSIZE;
645 if (!len)
646 goto fail;
647
Willy Tarreau827fee72020-12-11 15:26:55 +0100648 /* snd_buf() already takes care of updating conn->flags and handling
649 * the FD polling status.
650 */
651 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
652 if (conn->flags & CO_FL_ERROR)
653 ret = -1;
654 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100655 fail:
656 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
657 return ret;
658}
659
Willy Tarreau746b0512020-12-11 17:06:11 +0100660/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
661 * The event subscriber <es> is not allowed to change from a previous call as
662 * long as at least one event is still subscribed. The <event_type> must only
663 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100664 */
665int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200666{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100667 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100668 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100669
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100670 es->events &= ~event_type;
671 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100672 conn->subs = NULL;
673
Willy Tarreau746b0512020-12-11 17:06:11 +0100674 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
675 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100676
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200677 return 0;
678}
679
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100680/* Called from the upper layer, to subscribe <es> to events <event_type>.
681 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100682 * previous call to subscribe(). If the connection's ctrl layer is ready,
683 * the wait_event is immediately woken up and the subcription is cancelled.
684 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100685 */
686int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200687{
Willy Tarreau746b0512020-12-11 17:06:11 +0100688 int ret = 0;
689
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100690 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100691 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100692
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100693 if (conn->subs && (conn->subs->events & event_type) == event_type)
694 return 0;
695
Willy Tarreau746b0512020-12-11 17:06:11 +0100696 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
697 ret = conn->ctrl->check_events(conn, event_type);
698 if (ret)
699 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100700 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100701
702 es->events = (es->events | event_type) & ~ret;
703 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200704 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200705}
706
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100707/* Drains possibly pending incoming data on the connection and update the flags
708 * accordingly. This is used to know whether we need to disable lingering on
709 * close. Returns non-zero if it is safe to close without disabling lingering,
710 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
711 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100712 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100713int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100714{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100715 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200716
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100717 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
718 ret = 1;
719 else if (conn->ctrl->drain) {
720 ret = conn->ctrl->drain(conn);
721 if (ret)
722 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100723 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100724 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100725}
726
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100727/*
728 * Get data length from tlv
729 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100730static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100731{
732 return (src->length_hi << 8) | src->length_lo;
733}
734
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200735/* This handshake handler waits a PROXY protocol header at the beginning of the
736 * raw data stream. The header looks like this :
737 *
738 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
739 *
740 * There must be exactly one space between each field. Fields are :
741 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
742 * - SRC3 : layer 3 (eg: IP) source address in standard text form
743 * - DST3 : layer 3 (eg: IP) destination address in standard text form
744 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
745 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
746 *
747 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
748 *
749 * The header line is small and in all cases smaller than the smallest normal
750 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
751 * can safely use MSG_PEEK and avoid buffering.
752 *
753 * Once the data is fetched, the values are set in the connection's address
754 * fields, and data are removed from the socket's buffer. The function returns
755 * zero if it needs to wait for more data or if it fails, or 1 if it completed
756 * and removed itself.
757 */
758int conn_recv_proxy(struct connection *conn, int flag)
759{
760 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200761 struct proxy_hdr_v2 *hdr_v2;
762 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100763 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100764 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200765 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200766
Willy Tarreau3c728722014-01-23 13:50:42 +0100767 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200768 goto fail;
769
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200770 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200771 goto fail;
772
Willy Tarreau585744b2017-08-24 14:31:19 +0200773 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200774 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100775
Willy Tarreau157788c2020-02-11 10:08:05 +0100776 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200777 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
778 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200779 if (errno == EINTR)
780 continue;
781 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200782 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200783 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200784 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100785 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200786 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200787 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100788 break;
789 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200790
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200791 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100792 /* client shutdown */
793 conn->err_code = CO_ER_PRX_EMPTY;
794 goto fail;
795 }
796
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100797 conn->flags &= ~CO_FL_WAIT_L4_CONN;
798
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200799 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200800 goto missing;
801
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200802 line = trash.area;
803 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200804
805 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200806 if (strncmp(line, "PROXY ", 6) != 0)
807 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200808
809 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200810 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200811 goto missing;
812
David CARLIER42ff05e2016-03-24 09:22:36 +0000813 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200814 u32 src3, dst3, sport, dport;
815
816 line += 5;
817
818 src3 = inetaddr_host_lim_ret(line, end, &line);
819 if (line == end)
820 goto missing;
821 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100822 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200823
824 dst3 = inetaddr_host_lim_ret(line, end, &line);
825 if (line == end)
826 goto missing;
827 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100828 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200829
830 sport = read_uint((const char **)&line, end);
831 if (line == end)
832 goto missing;
833 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100834 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200835
836 dport = read_uint((const char **)&line, end);
837 if (line > end - 2)
838 goto missing;
839 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100840 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200841 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100842 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200843
844 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200845 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
846 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
847 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200848
Willy Tarreau226572f2019-07-17 14:46:00 +0200849 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
850 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
851 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200852 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
853 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000854 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200855 u32 sport, dport;
856 char *src_s;
857 char *dst_s, *sport_s, *dport_s;
858 struct in6_addr src3, dst3;
859
860 line += 5;
861
862 src_s = line;
863 dst_s = sport_s = dport_s = NULL;
864 while (1) {
865 if (line > end - 2) {
866 goto missing;
867 }
868 else if (*line == '\r') {
869 *line = 0;
870 line++;
871 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100872 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200873 break;
874 }
875
876 if (*line == ' ') {
877 *line = 0;
878 if (!dst_s)
879 dst_s = line + 1;
880 else if (!sport_s)
881 sport_s = line + 1;
882 else if (!dport_s)
883 dport_s = line + 1;
884 }
885 line++;
886 }
887
888 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100889 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200890
891 sport = read_uint((const char **)&sport_s,dport_s - 1);
892 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100893 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200894
895 dport = read_uint((const char **)&dport_s,line - 2);
896 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100897 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200898
899 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100900 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200901
902 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100903 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200904
905 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200906 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
907 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
908 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200909
Willy Tarreau226572f2019-07-17 14:46:00 +0200910 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
911 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
912 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200913 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
914 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200915 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
916 /* This can be a UNIX socket forwarded by an haproxy upstream */
917 line += 9;
918 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200919 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200920 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100921 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200922 goto fail;
923 }
924
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200925 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200926 goto eat_header;
927
928 not_v1:
929 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200930 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200931 goto missing;
932
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200933 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200934
935 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
936 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
937 conn->err_code = CO_ER_PRX_NOT_HDR;
938 goto fail;
939 }
940
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100941 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
942 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200943 goto missing;
944
945 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
946 case 0x01: /* PROXY command */
947 switch (hdr_v2->fam) {
948 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100949 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
950 goto bad_header;
951
Willy Tarreau226572f2019-07-17 14:46:00 +0200952 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
953 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
954 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
955 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
956 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
957 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200958 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200959 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200960 break;
961 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100962 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
963 goto bad_header;
964
Willy Tarreau226572f2019-07-17 14:46:00 +0200965 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
966 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
967 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
968 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
969 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
970 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200971 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200972 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200973 break;
974 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100975
976 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100977 while (tlv_offset < total_v2_len) {
978 struct tlv *tlv_packet;
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100979 struct ist tlv;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100980
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100981 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
982 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
983 goto bad_header;
984
985 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100986 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
987 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100988
989 /* Verify that the TLV length does not exceed the total PROXYv2 length */
990 if (tlv_offset > total_v2_len)
991 goto bad_header;
992
993 switch (tlv_packet->type) {
994 case PP2_TYPE_CRC32C: {
995 uint32_t n_crc32c;
996
997 /* Verify that this TLV is exactly 4 bytes long */
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100998 if (istlen(tlv) != 4)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100999 goto bad_header;
1000
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001001 n_crc32c = read_n32(istptr(tlv));
1002 write_n32(istptr(tlv), 0); // compute with CRC==0
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001003
1004 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
1005 goto bad_header;
1006 break;
1007 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001008#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001009 case PP2_TYPE_NETNS: {
1010 const struct netns_entry *ns;
1011
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001012 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001013 if (ns)
1014 conn->proxy_netns = ns;
1015 break;
1016 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001017#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001018 case PP2_TYPE_AUTHORITY: {
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001019 if (istlen(tlv) > PP2_AUTHORITY_MAX)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001020 goto bad_header;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001021 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
1022 if (!isttest(conn->proxy_authority))
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001023 goto fail;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001024 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
1025 /* This is technically unreachable, because we verified above
1026 * that the TLV value fits.
1027 */
1028 goto fail;
1029 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001030 break;
1031 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001032 case PP2_TYPE_UNIQUE_ID: {
Tim Duesterhus002bd772021-03-06 20:06:49 +01001033 if (istlen(tlv) > UNIQUEID_LEN)
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001034 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +01001035 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001036 if (!isttest(conn->proxy_unique_id))
1037 goto fail;
1038 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
1039 /* This is technically unreachable, because we verified above
1040 * that the TLV value fits.
1041 */
1042 goto fail;
1043 }
1044 break;
1045 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001046 default:
1047 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001048 }
1049 }
1050
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001051 /* Verify that the PROXYv2 header ends at a TLV boundary.
1052 * This is technically unreachable, because the TLV parsing already
1053 * verifies that a TLV does not exceed the total length and also
1054 * that there is space for a TLV header.
1055 */
1056 if (tlv_offset != total_v2_len)
1057 goto bad_header;
1058
Willy Tarreau77992672014-06-14 11:06:17 +02001059 /* unsupported protocol, keep local connection address */
1060 break;
1061 case 0x00: /* LOCAL command */
1062 /* keep local connection address for LOCAL */
1063 break;
1064 default:
1065 goto bad_header; /* not a supported command */
1066 }
1067
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001068 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +02001069 goto eat_header;
1070
1071 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001072 /* remove the PROXY line from the request. For this we re-read the
1073 * exact line at once. If we don't get the exact same result, we
1074 * fail.
1075 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001076 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001077 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
1078
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001079 if (len2 < 0 && errno == EINTR)
1080 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001081 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001082 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001083 break;
1084 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001085
1086 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +01001087 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001088 return 1;
1089
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001090 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001091 return 0;
1092
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001093 missing:
1094 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1095 * we have not read anything. Otherwise we need to fail because we won't
1096 * be able to poll anymore.
1097 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001098 conn->err_code = CO_ER_PRX_TRUNCATED;
1099 goto fail;
1100
1101 bad_header:
1102 /* This is not a valid proxy protocol header */
1103 conn->err_code = CO_ER_PRX_BAD_HDR;
1104 goto fail;
1105
1106 recv_abort:
1107 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +01001108 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001109 goto fail;
1110
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001111 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001112 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001113 return 0;
1114}
1115
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001116/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001117 * at the beginning of the raw data stream. The header format is
1118 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001119 *
1120 * This line MUST be at the beginning of the buffer and MUST NOT be
1121 * fragmented.
1122 *
1123 * The header line is small and in all cases smaller than the smallest normal
1124 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
1125 * can safely use MSG_PEEK and avoid buffering.
1126 *
1127 * Once the data is fetched, the values are set in the connection's address
1128 * fields, and data are removed from the socket's buffer. The function returns
1129 * zero if it needs to wait for more data or if it fails, or 1 if it completed
1130 * and removed itself.
1131 */
1132int conn_recv_netscaler_cip(struct connection *conn, int flag)
1133{
1134 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001135 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001136 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +02001137 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001138
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001139 if (!conn_ctrl_ready(conn))
1140 goto fail;
1141
Willy Tarreau9b7587a2020-10-15 07:32:10 +02001142 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Olivier Houchard1a9dbe52020-01-22 15:31:09 +01001143 goto fail;
1144
Willy Tarreau585744b2017-08-24 14:31:19 +02001145 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001146 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001147
Willy Tarreau157788c2020-02-11 10:08:05 +01001148 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +02001149 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
1150 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001151 if (errno == EINTR)
1152 continue;
1153 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +02001154 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001155 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001156 }
1157 goto recv_abort;
1158 }
Willy Tarreaub406b872018-08-22 05:20:32 +02001159 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +01001160 break;
1161 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001162
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001163 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1164
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001165 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001166 /* client shutdown */
1167 conn->err_code = CO_ER_CIP_EMPTY;
1168 goto fail;
1169 }
1170
1171 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001172 * CIP magic, header length or
1173 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001174 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001175 goto missing;
1176
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001177 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001178
1179 /* Decode a possible NetScaler Client IP request, fail early if
1180 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001181 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001182 goto bad_magic;
1183
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001184 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001185 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001186 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001187 line += 8;
1188 }
1189 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001190 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001191 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001192 line += 12;
1193 }
1194 /* Unknown CIP protocol */
1195 else {
1196 conn->err_code = CO_ER_CIP_BAD_PROTO;
1197 goto fail;
1198 }
1199
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001200 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001201 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001202 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001203 goto missing;
1204
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001205 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001206 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001207
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001208 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001209 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +01001210 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001211
1212 hdr_ip4 = (struct ip *)line;
1213
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001214 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001215 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001216 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001217 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001218 }
1219 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001220 /* The protocol does not include a TCP header */
1221 conn->err_code = CO_ER_CIP_BAD_PROTO;
1222 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001223 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001224
David Carlier3015a2e2016-07-04 22:51:33 +01001225 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001226
1227 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +02001228 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
1229 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
1230 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001231
Willy Tarreau226572f2019-07-17 14:46:00 +02001232 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
1233 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
1234 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001235
1236 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
1237 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001238 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001239 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +01001240 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001241
1242 hdr_ip6 = (struct ip6_hdr *)line;
1243
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001244 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001245 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001246 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001247 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001248 }
1249 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001250 /* The protocol does not include a TCP header */
1251 conn->err_code = CO_ER_CIP_BAD_PROTO;
1252 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001253 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001254
David Carlier3015a2e2016-07-04 22:51:33 +01001255 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001256
1257 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +02001258 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
1259 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
1260 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001261
Willy Tarreau226572f2019-07-17 14:46:00 +02001262 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
1263 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
1264 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001265
1266 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
1267 }
1268 else {
1269 /* The protocol does not match something known (IPv4/IPv6) */
1270 conn->err_code = CO_ER_CIP_BAD_PROTO;
1271 goto fail;
1272 }
1273
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001274 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001275 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001276
1277 /* remove the NetScaler Client IP header from the request. For this
1278 * we re-read the exact line at once. If we don't get the exact same
1279 * result, we fail.
1280 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001281 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001282 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001283 if (len2 < 0 && errno == EINTR)
1284 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001285 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001286 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001287 break;
1288 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001289
1290 conn->flags &= ~flag;
1291 return 1;
1292
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001293 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001294 return 0;
1295
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001296 missing:
1297 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1298 * we have not read anything. Otherwise we need to fail because we won't
1299 * be able to poll anymore.
1300 */
1301 conn->err_code = CO_ER_CIP_TRUNCATED;
1302 goto fail;
1303
1304 bad_magic:
1305 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1306 goto fail;
1307
1308 recv_abort:
1309 conn->err_code = CO_ER_CIP_ABORT;
1310 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1311 goto fail;
1312
1313 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001314 conn->flags |= CO_FL_ERROR;
1315 return 0;
1316}
1317
Alexander Liu2a54bb72019-05-22 19:44:48 +08001318
1319int conn_send_socks4_proxy_request(struct connection *conn)
1320{
1321 struct socks4_request req_line;
1322
Alexander Liu2a54bb72019-05-22 19:44:48 +08001323 if (!conn_ctrl_ready(conn))
1324 goto out_error;
1325
Willy Tarreau226572f2019-07-17 14:46:00 +02001326 if (!conn_get_dst(conn))
1327 goto out_error;
1328
Alexander Liu2a54bb72019-05-22 19:44:48 +08001329 req_line.version = 0x04;
1330 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001331 req_line.port = get_net_port(conn->dst);
1332 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001333 memcpy(req_line.user_id, "HAProxy\0", 8);
1334
1335 if (conn->send_proxy_ofs > 0) {
1336 /*
1337 * This is the first call to send the request
1338 */
1339 conn->send_proxy_ofs = -(int)sizeof(req_line);
1340 }
1341
1342 if (conn->send_proxy_ofs < 0) {
1343 int ret = 0;
1344
1345 /* we are sending the socks4_req_line here. If the data layer
1346 * has a pending write, we'll also set MSG_MORE.
1347 */
Willy Tarreau827fee72020-12-11 15:26:55 +01001348 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +08001349 conn,
1350 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1351 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +01001352 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001353
1354 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1355 conn->handle.fd, -conn->send_proxy_ofs, ret);
1356
1357 if (ret < 0) {
1358 goto out_error;
1359 }
1360
1361 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1362 if (conn->send_proxy_ofs != 0) {
1363 goto out_wait;
1364 }
1365 }
1366
1367 /* OK we've the whole request sent */
1368 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001369
1370 /* The connection is ready now, simply return and let the connection
1371 * handler notify upper layers if needed.
1372 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001373 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001374
1375 if (conn->flags & CO_FL_SEND_PROXY) {
1376 /*
1377 * Get the send_proxy_ofs ready for the send_proxy due to we are
1378 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1379 * before sending PROXY Protocol.
1380 */
1381 conn->send_proxy_ofs = 1;
1382 }
1383 return 1;
1384
1385 out_error:
1386 /* Write error on the file descriptor */
1387 conn->flags |= CO_FL_ERROR;
1388 if (conn->err_code == CO_ER_NONE) {
1389 conn->err_code = CO_ER_SOCKS4_SEND;
1390 }
1391 return 0;
1392
1393 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001394 return 0;
1395}
1396
1397int conn_recv_socks4_proxy_response(struct connection *conn)
1398{
1399 char line[SOCKS4_HS_RSP_LEN];
1400 int ret;
1401
Alexander Liu2a54bb72019-05-22 19:44:48 +08001402 if (!conn_ctrl_ready(conn))
1403 goto fail;
1404
1405 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001406 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001407
Willy Tarreau157788c2020-02-11 10:08:05 +01001408 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001409 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1410 * Try to peek into it, before all 8 bytes ready.
1411 */
1412 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1413
1414 if (ret == 0) {
1415 /* the socket has been closed or shutdown for send */
1416 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1417 conn->handle.fd, ret, errno);
1418 if (conn->err_code == CO_ER_NONE) {
1419 conn->err_code = CO_ER_SOCKS4_RECV;
1420 }
1421 goto fail;
1422 }
1423
1424 if (ret > 0) {
1425 if (ret == SOCKS4_HS_RSP_LEN) {
1426 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1427 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1428 }else{
1429 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]);
1430 }
1431 } else {
1432 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1433 }
1434
1435 if (ret < 0) {
1436 if (errno == EINTR) {
1437 continue;
1438 }
1439 if (errno == EAGAIN) {
1440 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001441 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001442 }
1443 goto recv_abort;
1444 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001445 break;
1446 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001447
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001448 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1449
Alexander Liu2a54bb72019-05-22 19:44:48 +08001450 if (ret < SOCKS4_HS_RSP_LEN) {
1451 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1452 * we are not able to read enough data.
1453 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001454 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001455 }
1456
1457 /*
1458 * Base on the SOCSK4 protocol:
1459 *
1460 * +----+----+----+----+----+----+----+----+
1461 * | VN | CD | DSTPORT | DSTIP |
1462 * +----+----+----+----+----+----+----+----+
1463 * # of bytes: 1 1 2 4
1464 * VN is the version of the reply code and should be 0. CD is the result
1465 * code with one of the following values:
1466 * 90: request granted
1467 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001468 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001469 * 93: request rejected because the client program and identd report different user-ids
1470 * The remaining fields are ignored.
1471 */
1472 if (line[1] != 90) {
1473 conn->flags &= ~CO_FL_SOCKS4_RECV;
1474
1475 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1476 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1477 if (conn->err_code == CO_ER_NONE) {
1478 conn->err_code = CO_ER_SOCKS4_DENY;
1479 }
1480 goto fail;
1481 }
1482
1483 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001484 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001485 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1486 if (ret < 0 && errno == EINTR) {
1487 continue;
1488 }
1489 if (ret != SOCKS4_HS_RSP_LEN) {
1490 if (conn->err_code == CO_ER_NONE) {
1491 conn->err_code = CO_ER_SOCKS4_RECV;
1492 }
1493 goto fail;
1494 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001495 break;
1496 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001497
1498 conn->flags &= ~CO_FL_SOCKS4_RECV;
1499 return 1;
1500
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001501 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001502 return 0;
1503
Alexander Liu2a54bb72019-05-22 19:44:48 +08001504 recv_abort:
1505 if (conn->err_code == CO_ER_NONE) {
1506 conn->err_code = CO_ER_SOCKS4_ABORT;
1507 }
1508 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1509 goto fail;
1510
1511 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001512 conn->flags |= CO_FL_ERROR;
1513 return 0;
1514}
1515
Willy Tarreaue59b5162021-05-08 14:06:09 +02001516/* Lists the known proto mux on <out> */
1517void list_mux_proto(FILE *out)
1518{
1519 struct mux_proto_list *item;
1520 struct buffer *chk = get_trash_chunk();
1521 struct ist proto;
1522 char *mode, *side;
1523
1524 fprintf(out, "Available multiplexer protocols :\n"
1525 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
1526 list_for_each_entry(item, &mux_proto_list.list, list) {
1527 proto = item->token;
1528
1529 if (item->mode == PROTO_MODE_ANY)
1530 mode = "TCP|HTTP";
1531 else if (item->mode == PROTO_MODE_TCP)
1532 mode = "TCP";
1533 else if (item->mode == PROTO_MODE_HTTP)
1534 mode = "HTTP";
1535 else
1536 mode = "NONE";
1537
1538 if (item->side == PROTO_SIDE_BOTH)
1539 side = "FE|BE";
1540 else if (item->side == PROTO_SIDE_FE)
1541 side = "FE";
1542 else if (item->side == PROTO_SIDE_BE)
1543 side = "BE";
1544 else
1545 side = "NONE";
1546
1547 chunk_reset(chk);
1548 if (item->mux->flags & MX_FL_HTX)
1549 chunk_strcpy(chk, "HTX");
1550 if (item->mux->flags & MX_FL_CLEAN_ABRT)
1551 chunk_appendf(chk, "%sCLEAN_ABRT", (b_data(chk) ? "|": ""));
1552 if (item->mux->flags & MX_FL_HOL_RISK)
1553 chunk_appendf(chk, "%sHOL_RISK", (b_data(chk) ? "|": ""));
1554 if (item->mux->flags & MX_FL_NO_UPG)
1555 chunk_appendf(chk, "%sNO_UPG", (b_data(chk) ? "|": ""));
1556
1557 fprintf(out, " %15s : mode=%-10s side=%-8s mux=%-8s flags=%.*s\n",
1558 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name,
1559 (int)b_data(chk), b_orig(chk));
1560 }
1561}
1562
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001563/* Makes a PROXY protocol line from the two addresses. The output is sent to
1564 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1565 * It returns the number of bytes composing this line (including the trailing
1566 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001567 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1568 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001569 */
Christopher Faulet4bfce392021-10-22 14:33:59 +02001570static int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001571{
1572 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001573 char * protocol;
1574 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1575 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1576 in_port_t src_port;
1577 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001578
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001579 if ( !src
1580 || !dst
1581 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1582 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1583 /* unknown family combination */
1584 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001585 if (ret >= buf_len)
1586 return 0;
1587
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001588 return ret;
1589 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001590
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001591 /* IPv4 for both src and dst */
1592 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1593 protocol = "TCP4";
1594 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001595 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001596 src_port = ((struct sockaddr_in *)src)->sin_port;
1597 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001598 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001599 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001600 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001601 /* IPv6 for at least one of src and dst */
1602 else {
1603 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001604
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001605 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001606
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001607 if (src->ss_family == AF_INET) {
1608 /* Convert src to IPv6 */
1609 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1610 src_port = ((struct sockaddr_in *)src)->sin_port;
1611 }
1612 else {
1613 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1614 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1615 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001616
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001617 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001618 return 0;
1619
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001620 if (dst->ss_family == AF_INET) {
1621 /* Convert dst to IPv6 */
1622 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1623 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1624 }
1625 else {
1626 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1627 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1628 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001629
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001630 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001631 return 0;
1632 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001633
1634 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1635 if (ret >= buf_len)
1636 return 0;
1637
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001638 return ret;
1639}
David Safb76832014-05-08 23:42:08 -04001640
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001641static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001642{
1643 struct tlv *tlv;
1644
1645 if (!dest || (length + sizeof(*tlv) > dest_len))
1646 return 0;
1647
1648 tlv = (struct tlv *)dest;
1649
1650 tlv->type = type;
1651 tlv->length_hi = length >> 8;
1652 tlv->length_lo = length & 0x00ff;
1653 memcpy(tlv->value, value, length);
1654 return length + sizeof(*tlv);
1655}
David Safb76832014-05-08 23:42:08 -04001656
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001657/* Note: <remote> is explicitly allowed to be NULL */
Christopher Faulet4bfce392021-10-22 14:33:59 +02001658static int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001659{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001660 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001661 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001662 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001663 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001664 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001665 struct sockaddr_storage *src = &null_addr;
1666 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001667 const char *value;
1668 int value_len;
David Safb76832014-05-08 23:42:08 -04001669
1670 if (buf_len < PP2_HEADER_LEN)
1671 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001672 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001673
Willy Tarreau226572f2019-07-17 14:46:00 +02001674 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1675 src = remote->src;
1676 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001677 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001678
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001679 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1680 if ( !src
1681 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001682 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001683 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1684 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001685 if (buf_len < PP2_HDR_LEN_UNSPEC)
1686 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001687 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1688 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001689 ret = PP2_HDR_LEN_UNSPEC;
1690 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001691 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001692 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001693 /* IPv4 for both src and dst */
1694 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1695 if (buf_len < PP2_HDR_LEN_INET)
1696 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001697 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1698 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1699 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1700 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1701 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1702 ret = PP2_HDR_LEN_INET;
1703 }
1704 /* IPv6 for at least one of src and dst */
1705 else {
1706 struct in6_addr tmp;
1707
1708 if (buf_len < PP2_HDR_LEN_INET6)
1709 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001710 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1711 if (src->ss_family == AF_INET) {
1712 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1713 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1714 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1715 }
1716 else {
1717 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1718 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1719 }
1720 if (dst->ss_family == AF_INET) {
1721 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1722 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001723 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001724 }
1725 else {
1726 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1727 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1728 }
1729
1730 ret = PP2_HDR_LEN_INET6;
1731 }
1732 }
David Safb76832014-05-08 23:42:08 -04001733
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001734 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1735 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001736
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001737 if ((buf_len - ret) < sizeof(struct tlv))
1738 return 0;
1739 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1740 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1741 }
1742
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001743 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001744 if ((buf_len - ret) < sizeof(struct tlv))
1745 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001746 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001747 }
1748
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001749 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001750 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001751 if (remote && isttest(remote->proxy_authority)) {
1752 value = istptr(remote->proxy_authority);
1753 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001754 }
1755#ifdef USE_OPENSSL
1756 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001757 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001758 value_len = strlen(value);
1759 }
1760#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001761 if (value) {
1762 if ((buf_len - ret) < sizeof(struct tlv))
1763 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001764 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001765 }
1766 }
1767
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001768 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001769 struct session* sess = strm_sess(strm);
1770 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1771
1772 value = unique_id.ptr;
1773 value_len = unique_id.len;
1774
1775 if (value_len >= 0) {
1776 if ((buf_len - ret) < sizeof(struct tlv))
1777 return 0;
1778 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1779 }
1780 }
1781
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001782#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001783 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001784 struct tlv_ssl *tlv;
1785 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001786
David Safb76832014-05-08 23:42:08 -04001787 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1788 return 0;
1789 tlv = (struct tlv_ssl *)&buf[ret];
1790 memset(tlv, 0, sizeof(struct tlv_ssl));
1791 ssl_tlv_len += sizeof(struct tlv_ssl);
1792 tlv->tlv.type = PP2_TYPE_SSL;
Willy Tarreau1057bee2021-10-06 11:38:44 +02001793 if (conn_is_ssl(remote)) {
David Safb76832014-05-08 23:42:08 -04001794 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001795 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001796 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001797 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 -04001798 }
Dave McCowan328fb582014-07-30 10:39:13 -04001799 if (ssl_sock_get_cert_used_sess(remote)) {
1800 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001801 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001802 if (ssl_sock_get_cert_used_conn(remote))
1803 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001804 }
1805 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001806 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001807 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001808 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1809 cn_trash->data,
1810 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001811 }
1812 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001813 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001814 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001815 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001816 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1817 pkey_trash->data,
1818 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001819 }
1820 }
1821 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1822 value = ssl_sock_get_cert_sig(remote);
1823 if (value) {
1824 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1825 }
1826 }
1827 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1828 value = ssl_sock_get_cipher_name(remote);
1829 if (value) {
1830 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1831 }
1832 }
David Safb76832014-05-08 23:42:08 -04001833 }
1834 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1835 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1836 ret += ssl_tlv_len;
1837 }
1838#endif
1839
Willy Tarreaue5733232019-05-22 19:24:06 +02001840#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001841 if (remote && (remote->proxy_netns)) {
1842 if ((buf_len - ret) < sizeof(struct tlv))
1843 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001844 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 +01001845 }
1846#endif
1847
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001848 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001849
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001850 if (tlv_crc32c_p) {
1851 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1852 }
1853
David Safb76832014-05-08 23:42:08 -04001854 return ret;
1855}
Emeric Brun4f603012017-01-05 15:11:44 +01001856
Christopher Faulet4bfce392021-10-22 14:33:59 +02001857/* Note: <remote> is explicitly allowed to be NULL */
1858int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
1859{
1860 int ret = 0;
1861
1862 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1863 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
1864 }
1865 else {
1866 if (remote && conn_get_src(remote) && conn_get_dst(remote))
1867 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
1868 else
1869 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1870 }
1871
1872 return ret;
1873}
1874
Willy Tarreau119e50e2020-05-22 13:53:29 +02001875/* returns 0 on success */
1876static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001877 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02001878 char **err)
1879{
1880 if (too_many_args(0, args, err, NULL))
1881 return -1;
1882 pp2_never_send_local = 1;
1883 return 0;
1884}
1885
Willy Tarreaud943a042021-06-16 17:35:20 +02001886/* extracts some info from the connection and appends them to buffer <buf>. The
1887 * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source
1888 * when set, destination when set, are printed in a compact human-readable format
1889 * fitting on a single line. This is handy to complete traces or debug output.
1890 * It is permitted to pass a NULL conn pointer. The number of characters emitted
1891 * is returned. A prefix <pfx> might be prepended before the first field if not
1892 * NULL.
1893 */
1894int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx)
1895{
1896 const struct listener *li;
1897 const struct server *sv;
1898 const struct proxy *px;
1899 char addr[40];
1900 int old_len = buf->data;
1901
1902 if (!conn)
1903 return 0;
1904
1905 chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN");
1906
1907 if ((li = objt_listener(conn->target)))
1908 chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id);
1909 else if ((sv = objt_server(conn->target)))
1910 chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id);
1911 else if ((px = objt_proxy(conn->target)))
1912 chunk_appendf(buf, " be=%s", px->id);
1913
1914 chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn));
1915
1916 if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr)))
1917 chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src));
1918
1919 if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr)))
1920 chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst));
1921
1922 return buf->data - old_len;
1923}
1924
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001925/* return the major HTTP version as 1 or 2 depending on how the request arrived
1926 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001927 *
1928 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001929 */
1930static int
1931smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1932{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02001933 struct connection *conn = NULL;
1934
1935 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
1936 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
1937 else
1938 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1939 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001940
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001941 /* No connection or a connection with a RAW muxx */
1942 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
1943 return 0;
1944
1945 /* No mux install, this may change */
1946 if (!conn->mux) {
1947 smp->flags |= SMP_F_MAY_CHANGE;
1948 return 0;
1949 }
1950
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001951 smp->data.type = SMP_T_SINT;
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001952 smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001953 return 1;
1954}
1955
Emeric Brun4f603012017-01-05 15:11:44 +01001956/* fetch if the received connection used a PROXY protocol header */
1957int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1958{
1959 struct connection *conn;
1960
1961 conn = objt_conn(smp->sess->origin);
1962 if (!conn)
1963 return 0;
1964
Willy Tarreau911db9b2020-01-23 16:27:54 +01001965 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001966 smp->flags |= SMP_F_MAY_CHANGE;
1967 return 0;
1968 }
1969
1970 smp->flags = 0;
1971 smp->data.type = SMP_T_BOOL;
1972 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1973
1974 return 1;
1975}
1976
Geoff Simmons7185b782019-08-27 18:31:16 +02001977/* fetch the authority TLV from a PROXY protocol header */
1978int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1979{
1980 struct connection *conn;
1981
1982 conn = objt_conn(smp->sess->origin);
1983 if (!conn)
1984 return 0;
1985
Willy Tarreau911db9b2020-01-23 16:27:54 +01001986 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001987 smp->flags |= SMP_F_MAY_CHANGE;
1988 return 0;
1989 }
1990
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001991 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02001992 return 0;
1993
1994 smp->flags = 0;
1995 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001996 smp->data.u.str.area = istptr(conn->proxy_authority);
1997 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02001998
1999 return 1;
2000}
2001
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002002/* fetch the unique ID TLV from a PROXY protocol header */
2003int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2004{
2005 struct connection *conn;
2006
2007 conn = objt_conn(smp->sess->origin);
2008 if (!conn)
2009 return 0;
2010
2011 if (conn->flags & CO_FL_WAIT_XPRT) {
2012 smp->flags |= SMP_F_MAY_CHANGE;
2013 return 0;
2014 }
2015
2016 if (!isttest(conn->proxy_unique_id))
2017 return 0;
2018
2019 smp->flags = 0;
2020 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01002021 smp->data.u.str.area = istptr(conn->proxy_unique_id);
2022 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002023
2024 return 1;
2025}
2026
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002027/* fetch the error code of a connection */
2028int smp_fetch_fc_conn_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2029{
2030 struct connection *conn;
2031
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002032 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2033 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2034 else
2035 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2036 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2037
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002038 if (!conn)
2039 return 0;
2040
2041 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2042 smp->flags |= SMP_F_MAY_CHANGE;
2043 return 0;
2044 }
2045
2046 smp->flags = 0;
2047 smp->data.type = SMP_T_SINT;
2048 smp->data.u.sint = (unsigned long long int)conn->err_code;
2049
2050 return 1;
2051}
2052
2053/* fetch a string representation of the error code of a connection */
2054int smp_fetch_fc_conn_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2055{
2056 struct connection *conn;
2057 const char *err_code_str;
2058
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002059 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2060 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2061 else
2062 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2063 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2064
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002065 if (!conn)
2066 return 0;
2067
2068 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2069 smp->flags |= SMP_F_MAY_CHANGE;
2070 return 0;
2071 }
2072
2073 err_code_str = conn_err_code_str(conn);
2074
2075 if (!err_code_str)
2076 return 0;
2077
2078 smp->flags = 0;
2079 smp->data.type = SMP_T_STR;
2080 smp->data.u.str.area = (char*)err_code_str;
2081 smp->data.u.str.data = strlen(err_code_str);
2082
2083 return 1;
2084}
2085
Emeric Brun4f603012017-01-05 15:11:44 +01002086/* Note: must not be declared <const> as its list will be overwritten.
2087 * Note: fetches that may return multiple types must be declared as the lowest
2088 * common denominator, the type that can be casted into all other ones. For
2089 * instance v4/v6 must be declared v4.
2090 */
2091static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002092 { "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 +01002093 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01002094 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02002095 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002096 { "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 +02002097 { "fc_conn_err", smp_fetch_fc_conn_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
2098 { "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 +02002099 { "bc_conn_err", smp_fetch_fc_conn_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
2100 { "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 +01002101 { /* END */ },
2102}};
2103
Willy Tarreau0108d902018-11-25 19:14:37 +01002104INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02002105
2106static struct cfg_kw_list cfg_kws = {ILH, {
2107 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
2108 { /* END */ },
2109}};
2110
2111INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002112
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002113/* private function to handle sockaddr as input for connection hash */
2114static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
2115 char *buf, size_t *idx,
2116 enum conn_hash_params_t *hash_flags,
2117 enum conn_hash_params_t param_type_addr,
2118 enum conn_hash_params_t param_type_port)
2119{
2120 struct sockaddr_in *addr;
2121 struct sockaddr_in6 *addr6;
2122
2123 switch (ss->ss_family) {
2124 case AF_INET:
2125 addr = (struct sockaddr_in *)ss;
2126
2127 conn_hash_update(buf, idx,
2128 &addr->sin_addr, sizeof(addr->sin_addr),
2129 hash_flags, param_type_addr);
2130
2131 if (addr->sin_port) {
2132 conn_hash_update(buf, idx,
2133 &addr->sin_port, sizeof(addr->sin_port),
2134 hash_flags, param_type_port);
2135 }
2136
2137 break;
2138
2139 case AF_INET6:
2140 addr6 = (struct sockaddr_in6 *)ss;
2141
2142 conn_hash_update(buf, idx,
2143 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
2144 hash_flags, param_type_addr);
2145
2146 if (addr6->sin6_port) {
2147 conn_hash_update(buf, idx,
2148 &addr6->sin6_port, sizeof(addr6->sin6_port),
2149 hash_flags, param_type_port);
2150 }
2151
2152 break;
2153 }
2154}
2155
Willy Tarreaue5983ff2021-10-06 17:14:49 +02002156/* Generate the hash of a connection with params as input
2157 * Each non-null field of params is taken into account for the hash calcul.
2158 */
2159uint64_t conn_hash_prehash(char *buf, size_t size)
2160{
2161 return XXH64(buf, size, 0);
2162}
2163
2164/* Append <data> into <buf> at <idx> offset in preparation for connection hash
2165 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
2166 * are updated with <type> for the hash header.
2167 */
2168void conn_hash_update(char *buf, size_t *idx,
2169 const void *data, size_t size,
2170 enum conn_hash_params_t *flags,
2171 enum conn_hash_params_t type)
2172{
2173 memcpy(&buf[*idx], data, size);
2174 *idx += size;
2175 *flags |= type;
2176}
2177
2178uint64_t conn_hash_digest(char *buf, size_t bufsize,
2179 enum conn_hash_params_t flags)
2180{
2181 const uint64_t flags_u64 = (uint64_t)flags;
2182 const uint64_t hash = XXH64(buf, bufsize, 0);
2183
2184 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
2185}
2186
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002187uint64_t conn_calculate_hash(const struct conn_hash_params *params)
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002188{
2189 char *buf;
2190 size_t idx = 0;
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002191 uint64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002192 enum conn_hash_params_t hash_flags = 0;
2193
2194 buf = trash.area;
2195
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01002196 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01002197
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002198 if (params->sni_prehash) {
2199 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002200 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002201 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
2202 }
2203
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002204 if (params->dst_addr) {
2205 conn_calculate_hash_sockaddr(params->dst_addr,
2206 buf, &idx, &hash_flags,
2207 CONN_HASH_PARAMS_TYPE_DST_ADDR,
2208 CONN_HASH_PARAMS_TYPE_DST_PORT);
2209 }
2210
Amaury Denoyelled10a2002021-02-11 19:45:19 +01002211 if (params->src_addr) {
2212 conn_calculate_hash_sockaddr(params->src_addr,
2213 buf, &idx, &hash_flags,
2214 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
2215 CONN_HASH_PARAMS_TYPE_SRC_PORT);
2216 }
2217
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002218 if (params->proxy_prehash) {
2219 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002220 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002221 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
2222 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002223
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002224 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002225 return hash;
2226}
Amaury Denoyelle48372932021-09-16 12:15:12 +02002227
2228/* Handler of the task of mux_stopping_data.
2229 * Called on soft-stop.
2230 */
2231static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state)
2232{
2233 struct connection *conn, *back;
2234
2235 list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) {
2236 if (conn->mux && conn->mux->wake)
2237 conn->mux->wake(conn);
2238 }
2239
2240 return t;
2241}
2242
2243static int allocate_mux_cleanup(void)
2244{
2245 /* allocates the thread bound mux_stopping_data task */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002246 mux_stopping_data[tid].task = task_new_here();
Amaury Denoyelle48372932021-09-16 12:15:12 +02002247 if (!mux_stopping_data[tid].task) {
2248 ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid);
2249 return 0;
2250 }
2251
2252 mux_stopping_data[tid].task->process = mux_stopping_process;
2253 LIST_INIT(&mux_stopping_data[tid].list);
2254
2255 return 1;
2256}
2257REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup);
2258
2259static int deallocate_mux_cleanup(void)
2260{
2261 task_destroy(mux_stopping_data[tid].task);
2262 return 1;
2263}
2264REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup);