blob: 76becfc2b2ad3a60dd69eeb940626eee6c1835c6 [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 }
Amaury Denoyelleac03ef22021-10-28 16:36:11 +020073 else if (conn_install_mux_be(conn, conn->ctx, sess, NULL) < 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
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200250 * <ctx>. If the server mux protocol is forced, we use it to find the best mux.
251 * It's also possible to specify an alternative mux protocol <force_mux_ops>,
252 * in which case it will be used instead of the default server mux protocol.
253 *
254 * Returns < 0 on error.
Willy Tarreauaac777f2021-10-06 18:48:28 +0200255 */
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200256int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess,
257 const struct mux_ops *force_mux_ops)
Willy Tarreauaac777f2021-10-06 18:48:28 +0200258{
259 struct server *srv = objt_server(conn->target);
260 struct proxy *prx = objt_proxy(conn->target);
261 const struct mux_ops *mux_ops;
262
263 if (srv)
264 prx = srv->proxy;
265
266 if (!prx) // target must be either proxy or server
267 return -1;
268
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200269 if (srv && srv->mux_proto && likely(!force_mux_ops)) {
Willy Tarreauaac777f2021-10-06 18:48:28 +0200270 mux_ops = srv->mux_proto->mux;
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200271 }
272 else if (srv && unlikely(force_mux_ops)) {
273 mux_ops = force_mux_ops;
274 }
Willy Tarreauaac777f2021-10-06 18:48:28 +0200275 else {
276 struct ist mux_proto;
277 const char *alpn_str = NULL;
278 int alpn_len = 0;
279 int mode;
280
281 if (prx->mode == PR_MODE_HTTP)
282 mode = PROTO_MODE_HTTP;
283 else
284 mode = PROTO_MODE_TCP;
285
286 conn_get_alpn(conn, &alpn_str, &alpn_len);
287 mux_proto = ist2(alpn_str, alpn_len);
288
289 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
290 if (!mux_ops)
291 return -1;
292 }
293 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
294}
295
296/* installs the best mux for outgoing connection <conn> for a check using the
297 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
298 * find the best mux. Returns < 0 on error.
299 */
300int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
301{
302 struct check *check = objt_check(sess->origin);
303 struct server *srv = objt_server(conn->target);
304 struct proxy *prx = objt_proxy(conn->target);
305 const struct mux_ops *mux_ops;
306
307 if (!check) // Check must be defined
308 return -1;
309
310 if (srv)
311 prx = srv->proxy;
312
313 if (!prx) // target must be either proxy or server
314 return -1;
315
316 if (check->mux_proto)
317 mux_ops = check->mux_proto->mux;
318 else {
319 struct ist mux_proto;
320 const char *alpn_str = NULL;
321 int alpn_len = 0;
322 int mode;
323
324 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
325 mode = PROTO_MODE_HTTP;
326 else
327 mode = PROTO_MODE_TCP;
328
329 conn_get_alpn(conn, &alpn_str, &alpn_len);
330 mux_proto = ist2(alpn_str, alpn_len);
331
332 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
333 if (!mux_ops)
334 return -1;
335 }
336 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
337}
338
Amaury Denoyelle2454bda2021-10-18 14:32:36 +0200339/* Set the ALPN of connection <conn> to <alpn>. If force is false, <alpn> must
340 * be a subset or identical to the registered protos for the parent SSL_CTX.
341 * In this case <alpn> must be a single protocol value, not a list.
342 *
343 * Returns 0 if ALPN is updated else -1.
344 */
345int conn_update_alpn(struct connection *conn, const struct ist alpn, int force)
346{
347#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
348 size_t alpn_len = istlen(alpn);
349 char *ctx_alpn_str = NULL;
350 int ctx_alpn_len = 0, found = 0;
351
352 /* if not force, first search if alpn is a subset or identical to the
353 * parent SSL_CTX.
354 */
355 if (!force) {
356 /* retrieve the SSL_CTX according to the connection side. */
357 if (conn_is_back(conn)) {
358 if (obj_type(conn->target) == OBJ_TYPE_SERVER) {
359 struct server *srv = __objt_server(conn->target);
360 ctx_alpn_str = srv->ssl_ctx.alpn_str;
361 ctx_alpn_len = srv->ssl_ctx.alpn_len;
362 }
363 }
364 else {
365 struct session *sess = conn->owner;
366 struct listener *li = sess->listener;
367
368 if (li->bind_conf && li->bind_conf->is_ssl) {
369 ctx_alpn_str = li->bind_conf->ssl_conf.alpn_str;
370 ctx_alpn_len = li->bind_conf->ssl_conf.alpn_len;
371 }
372 }
373
374 if (ctx_alpn_str) {
375 /* search if ALPN is present in SSL_CTX ALPN before
376 * using it.
377 */
378 while (ctx_alpn_len) {
379 /* skip ALPN whose size is not 8 */
380 if (*ctx_alpn_str != alpn_len - 1) {
381 ctx_alpn_len -= *ctx_alpn_str + 1;
382 }
383 else {
384 if (isteqi(ist2(ctx_alpn_str, alpn_len), alpn)) {
385 found = 1;
386 break;
387 }
388 }
389 ctx_alpn_str += *ctx_alpn_str + 1;
390
391 /* This indicates an invalid ALPN formatted
392 * string and should never happen. */
393 BUG_ON(ctx_alpn_len < 0);
394 }
395 }
396 }
397
398 if (found || force) {
399 ssl_sock_set_alpn(conn, (const uchar *)istptr(alpn), istlen(alpn));
400 return 0;
401 }
402
403#endif
404 return -1;
405}
406
Willy Tarreauaac777f2021-10-06 18:48:28 +0200407/* Initializes all required fields for a new connection. Note that it does the
408 * minimum acceptable initialization for a connection that already exists and
409 * is about to be reused. It also leaves the addresses untouched, which makes
410 * it usable across connection retries to reset a connection to a known state.
411 */
412void conn_init(struct connection *conn, void *target)
413{
414 conn->obj_type = OBJ_TYPE_CONN;
415 conn->flags = CO_FL_NONE;
416 conn->mux = NULL;
417 conn->ctx = NULL;
418 conn->owner = NULL;
419 conn->send_proxy_ofs = 0;
420 conn->handle.fd = DEAD_FD_MAGIC;
421 conn->err_code = CO_ER_NONE;
422 conn->target = target;
423 conn->destroy_cb = NULL;
424 conn->proxy_netns = NULL;
425 MT_LIST_INIT(&conn->toremove_list);
426 if (conn_is_back(conn))
427 LIST_INIT(&conn->session_list);
428 else
429 LIST_INIT(&conn->stopping_list);
430 conn->subs = NULL;
431 conn->src = NULL;
432 conn->dst = NULL;
433 conn->proxy_authority = IST_NULL;
434 conn->proxy_unique_id = IST_NULL;
435 conn->qc = NULL;
436 conn->hash_node = NULL;
437 conn->xprt = NULL;
438}
439
440/* Tries to allocate a new connection and initialized its main fields. The
441 * connection is returned on success, NULL on failure. The connection must
442 * be released using pool_free() or conn_free().
443 */
444struct connection *conn_new(void *target)
445{
446 struct connection *conn;
447 struct conn_hash_node *hash_node;
448
449 conn = pool_alloc(pool_head_connection);
450 if (unlikely(!conn))
451 return NULL;
452
453 conn_init(conn, target);
454
455 if (conn_is_back(conn)) {
456 if (obj_type(target) == OBJ_TYPE_SERVER)
457 srv_use_conn(__objt_server(target), conn);
458
459 hash_node = conn_alloc_hash_node(conn);
460 if (unlikely(!hash_node)) {
461 pool_free(pool_head_connection, conn);
462 return NULL;
463 }
464
465 conn->hash_node = hash_node;
466 }
467
468 return conn;
469}
470
471/* Releases a connection previously allocated by conn_new() */
472void conn_free(struct connection *conn)
473{
474 /* If the connection is owned by the session, remove it from its list
475 */
476 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
477 session_unown_conn(conn->owner, conn);
478 }
479 else if (!(conn->flags & CO_FL_PRIVATE)) {
480 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
481 srv_release_conn(__objt_server(conn->target), conn);
482 }
483
484 /* Remove the conn from toremove_list.
485 *
486 * This is needed to prevent a double-free in case the connection was
487 * already scheduled from cleaning but is freed before via another
488 * call.
489 */
490 MT_LIST_DELETE(&conn->toremove_list);
491
492 sockaddr_free(&conn->src);
493 sockaddr_free(&conn->dst);
494
495 pool_free(pool_head_authority, istptr(conn->proxy_authority));
496 conn->proxy_authority = IST_NULL;
497
498 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
499 conn->proxy_unique_id = IST_NULL;
500
501 pool_free(pool_head_conn_hash_node, conn->hash_node);
502 conn->hash_node = NULL;
503
504 /* By convention we always place a NULL where the ctx points to if the
505 * mux is null. It may have been used to store the connection as a
506 * stream_interface's end point for example.
507 */
508 if (conn->ctx != NULL && conn->mux == NULL)
509 *(void **)conn->ctx = NULL;
510
511 conn_force_unsubscribe(conn);
512 pool_free(pool_head_connection, conn);
513}
514
Willy Tarreau8de90c72021-10-06 19:11:10 +0200515struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
516{
517 struct conn_hash_node *hash_node = NULL;
518
519 hash_node = pool_zalloc(pool_head_conn_hash_node);
520 if (unlikely(!hash_node))
521 return NULL;
522
523 hash_node->conn = conn;
524
525 return hash_node;
526}
527
528/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
529 * returns it. If <sap> is NULL, the address is always allocated and returned.
530 * if <sap> is non-null, an address will only be allocated if it points to a
531 * non-null pointer. In this case the allocated address will be assigned there.
532 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
533 * into the allocated address. In both situations the new pointer is returned.
534 */
535struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
536{
537 struct sockaddr_storage *sa;
538
539 if (sap && *sap)
540 return *sap;
541
542 sa = pool_alloc(pool_head_sockaddr);
543 if (sa && orig && len > 0)
544 memcpy(sa, orig, len);
545 if (sap)
546 *sap = sa;
547 return sa;
548}
549
550/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
551 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
552 * there.
553 */
554void sockaddr_free(struct sockaddr_storage **sap)
555{
556 if (!sap)
557 return;
558 pool_free(pool_head_sockaddr, *sap);
559 *sap = NULL;
560}
561
562/* Releases a conn_stream previously allocated by cs_new(), as well as any
563 * buffer it would still hold.
564 */
565void cs_free(struct conn_stream *cs)
566{
567
568 pool_free(pool_head_connstream, cs);
569}
570
571/* Tries to allocate a new conn_stream and initialize its main fields. If
572 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
573 * and assigned to cs->conn ; this connection will then have to be released
574 * using pool_free() or conn_free(). The conn_stream is initialized and added
575 * to the mux's stream list on success, then returned. On failure, nothing is
576 * allocated and NULL is returned.
577 */
578struct conn_stream *cs_new(struct connection *conn, void *target)
579{
580 struct conn_stream *cs;
581
582 cs = pool_alloc(pool_head_connstream);
583 if (unlikely(!cs))
584 return NULL;
585
586 if (!conn) {
587 conn = conn_new(target);
588 if (unlikely(!conn)) {
589 cs_free(cs);
590 return NULL;
591 }
592 }
593
594 cs_init(cs, conn);
595 return cs;
596}
597
Willy Tarreauaac777f2021-10-06 18:48:28 +0200598/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
599 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
600 * call the xprt's add_xprt() method.
601 * Returns 0 on success, or non-zero on failure.
602 */
603int xprt_add_hs(struct connection *conn)
604{
605 void *xprt_ctx = NULL;
606 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
607 void *nextxprt_ctx = NULL;
608 const struct xprt_ops *nextxprt_ops = NULL;
609
610 if (conn->flags & CO_FL_ERROR)
611 return -1;
612 if (ops->init(conn, &xprt_ctx) < 0)
613 return -1;
614 if (conn->xprt == xprt_get(XPRT_RAW)) {
615 nextxprt_ctx = conn->xprt_ctx;
616 nextxprt_ops = conn->xprt;
617 conn->xprt_ctx = xprt_ctx;
618 conn->xprt = ops;
619 } else {
620 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
621 &nextxprt_ctx, &nextxprt_ops) != 0) {
622 ops->close(conn, xprt_ctx);
623 return -1;
624 }
625 }
626 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
627 ops->close(conn, xprt_ctx);
628 return -1;
629 }
630 return 0;
631}
632
633/* returns a human-readable error code for conn->err_code, or NULL if the code
634 * is unknown.
635 */
636const char *conn_err_code_str(struct connection *c)
637{
638 switch (c->err_code) {
639 case CO_ER_NONE: return "Success";
640
641 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
642 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
643 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
644 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
645 case CO_ER_NOPROTO: return "Protocol or address family not supported";
646 case CO_ER_SOCK_ERR: return "General socket error";
647 case CO_ER_PORT_RANGE: return "Source port range exhausted";
648 case CO_ER_CANT_BIND: return "Can't bind to source address";
649 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
650 case CO_ER_ADDR_INUSE: return "Local source address already in use";
651
652 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
653 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
654 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
655 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
656 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
657 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
658 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
659
660 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
661 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
662 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
663 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
664 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
665 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
666
667 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
668 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
669 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
670 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
671 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
672 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
673 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
674 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
675 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
676 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
677 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
678 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
679 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
680 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
681 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
682
683 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
684 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
685 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
686 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
687
688 case CO_ERR_SSL_FATAL: return "SSL fatal error";
689 }
690 return NULL;
691}
692
Willy Tarreauff3e6482015-03-12 23:56:52 +0100693/* Send a message over an established connection. It makes use of send() and
694 * returns the same return code and errno. If the socket layer is not ready yet
695 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
696 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
697 * EMSGSIZE if called with a zero length message. The purpose is to simplify
698 * some rare attempts to directly write on the socket from above the connection
699 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
700 * It automatically retries on EINTR. Other errors cause the connection to be
701 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +0100702 * first one which is the connection instead of the file descriptor. <flags>
703 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100704 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100705int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100706{
Willy Tarreau827fee72020-12-11 15:26:55 +0100707 const struct buffer buffer = b_make((char*)buf, len, 0, len);
708 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100709 int ret;
710
711 ret = -1;
712 errno = ENOTSOCK;
713
714 if (conn->flags & CO_FL_SOCK_WR_SH)
715 goto fail;
716
717 if (!conn_ctrl_ready(conn))
718 goto fail;
719
720 errno = EMSGSIZE;
721 if (!len)
722 goto fail;
723
Willy Tarreau827fee72020-12-11 15:26:55 +0100724 /* snd_buf() already takes care of updating conn->flags and handling
725 * the FD polling status.
726 */
727 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
728 if (conn->flags & CO_FL_ERROR)
729 ret = -1;
730 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100731 fail:
732 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
733 return ret;
734}
735
Willy Tarreau746b0512020-12-11 17:06:11 +0100736/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
737 * The event subscriber <es> is not allowed to change from a previous call as
738 * long as at least one event is still subscribed. The <event_type> must only
739 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100740 */
741int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200742{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100743 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100744 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100745
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100746 es->events &= ~event_type;
747 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100748 conn->subs = NULL;
749
Willy Tarreau746b0512020-12-11 17:06:11 +0100750 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
751 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100752
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200753 return 0;
754}
755
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100756/* Called from the upper layer, to subscribe <es> to events <event_type>.
757 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100758 * previous call to subscribe(). If the connection's ctrl layer is ready,
759 * the wait_event is immediately woken up and the subcription is cancelled.
760 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100761 */
762int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200763{
Willy Tarreau746b0512020-12-11 17:06:11 +0100764 int ret = 0;
765
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100766 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100767 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100768
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100769 if (conn->subs && (conn->subs->events & event_type) == event_type)
770 return 0;
771
Willy Tarreau746b0512020-12-11 17:06:11 +0100772 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
773 ret = conn->ctrl->check_events(conn, event_type);
774 if (ret)
775 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100776 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100777
778 es->events = (es->events | event_type) & ~ret;
779 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200780 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200781}
782
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100783/* Drains possibly pending incoming data on the connection and update the flags
784 * accordingly. This is used to know whether we need to disable lingering on
785 * close. Returns non-zero if it is safe to close without disabling lingering,
786 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
787 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100788 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100789int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100790{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100791 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200792
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100793 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
794 ret = 1;
795 else if (conn->ctrl->drain) {
796 ret = conn->ctrl->drain(conn);
797 if (ret)
798 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100799 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100800 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100801}
802
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100803/*
804 * Get data length from tlv
805 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100806static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100807{
808 return (src->length_hi << 8) | src->length_lo;
809}
810
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200811/* This handshake handler waits a PROXY protocol header at the beginning of the
812 * raw data stream. The header looks like this :
813 *
814 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
815 *
816 * There must be exactly one space between each field. Fields are :
817 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
818 * - SRC3 : layer 3 (eg: IP) source address in standard text form
819 * - DST3 : layer 3 (eg: IP) destination address in standard text form
820 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
821 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
822 *
823 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
824 *
825 * The header line is small and in all cases smaller than the smallest normal
826 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
827 * can safely use MSG_PEEK and avoid buffering.
828 *
829 * Once the data is fetched, the values are set in the connection's address
830 * fields, and data are removed from the socket's buffer. The function returns
831 * zero if it needs to wait for more data or if it fails, or 1 if it completed
832 * and removed itself.
833 */
834int conn_recv_proxy(struct connection *conn, int flag)
835{
Christopher Fauletc105c922021-10-25 08:17:11 +0200836 struct session *sess = conn->owner;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200837 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200838 struct proxy_hdr_v2 *hdr_v2;
839 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100840 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100841 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200842 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200843
Willy Tarreau3c728722014-01-23 13:50:42 +0100844 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200845 goto fail;
846
Willy Tarreau585744b2017-08-24 14:31:19 +0200847 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200848 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100849
Willy Tarreau157788c2020-02-11 10:08:05 +0100850 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200851 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
852 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200853 if (errno == EINTR)
854 continue;
855 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200856 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200857 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200858 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100859 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200860 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200861 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100862 break;
863 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200864
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200865 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100866 /* client shutdown */
867 conn->err_code = CO_ER_PRX_EMPTY;
868 goto fail;
869 }
870
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100871 conn->flags &= ~CO_FL_WAIT_L4_CONN;
872
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200873 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200874 goto missing;
875
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200876 line = trash.area;
877 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200878
879 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200880 if (strncmp(line, "PROXY ", 6) != 0)
881 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200882
883 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200884 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200885 goto missing;
886
David CARLIER42ff05e2016-03-24 09:22:36 +0000887 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200888 u32 src3, dst3, sport, dport;
889
890 line += 5;
891
892 src3 = inetaddr_host_lim_ret(line, end, &line);
893 if (line == end)
894 goto missing;
895 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100896 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200897
898 dst3 = inetaddr_host_lim_ret(line, end, &line);
899 if (line == end)
900 goto missing;
901 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100902 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200903
904 sport = read_uint((const char **)&line, end);
905 if (line == end)
906 goto missing;
907 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100908 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200909
910 dport = read_uint((const char **)&line, end);
911 if (line > end - 2)
912 goto missing;
913 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100914 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200915 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100916 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200917
Christopher Fauletc105c922021-10-25 08:17:11 +0200918 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
919 goto fail;
920
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200921 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200922 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
923 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = htonl(src3);
924 ((struct sockaddr_in *)sess->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200925
Christopher Fauletc105c922021-10-25 08:17:11 +0200926 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
927 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = htonl(dst3);
928 ((struct sockaddr_in *)sess->dst)->sin_port = htons(dport);
929 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200930 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000931 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200932 u32 sport, dport;
933 char *src_s;
934 char *dst_s, *sport_s, *dport_s;
935 struct in6_addr src3, dst3;
936
937 line += 5;
938
939 src_s = line;
940 dst_s = sport_s = dport_s = NULL;
941 while (1) {
942 if (line > end - 2) {
943 goto missing;
944 }
945 else if (*line == '\r') {
946 *line = 0;
947 line++;
948 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100949 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200950 break;
951 }
952
953 if (*line == ' ') {
954 *line = 0;
955 if (!dst_s)
956 dst_s = line + 1;
957 else if (!sport_s)
958 sport_s = line + 1;
959 else if (!dport_s)
960 dport_s = line + 1;
961 }
962 line++;
963 }
964
965 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100966 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200967
968 sport = read_uint((const char **)&sport_s,dport_s - 1);
969 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100970 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200971
972 dport = read_uint((const char **)&dport_s,line - 2);
973 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100974 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200975
976 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100977 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200978
979 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100980 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200981
Christopher Fauletc105c922021-10-25 08:17:11 +0200982 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
983 goto fail;
984
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200985 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200986 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
987 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, &src3, sizeof(struct in6_addr));
988 ((struct sockaddr_in6 *)sess->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200989
Christopher Fauletc105c922021-10-25 08:17:11 +0200990 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
991 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
992 ((struct sockaddr_in6 *)sess->dst)->sin6_port = htons(dport);
993 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200994 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200995 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
996 /* This can be a UNIX socket forwarded by an haproxy upstream */
997 line += 9;
998 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200999 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +02001000 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001001 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001002 goto fail;
1003 }
1004
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001005 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +02001006 goto eat_header;
1007
1008 not_v1:
1009 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001010 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +02001011 goto missing;
1012
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001013 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +02001014
1015 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
1016 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
1017 conn->err_code = CO_ER_PRX_NOT_HDR;
1018 goto fail;
1019 }
1020
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001021 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
1022 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +02001023 goto missing;
1024
1025 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
1026 case 0x01: /* PROXY command */
1027 switch (hdr_v2->fam) {
1028 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +01001029 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
1030 goto bad_header;
1031
Christopher Fauletc105c922021-10-25 08:17:11 +02001032 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1033 goto fail;
1034
1035 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
1036 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
1037 ((struct sockaddr_in *)sess->src)->sin_port = hdr_v2->addr.ip4.src_port;
1038 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1039 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
1040 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
1041 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001042 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +02001043 break;
1044 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +01001045 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
1046 goto bad_header;
1047
Christopher Fauletc105c922021-10-25 08:17:11 +02001048 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1049 goto fail;
1050
1051 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1052 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
1053 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_v2->addr.ip6.src_port;
1054 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1055 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
1056 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
1057 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001058 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +02001059 break;
1060 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001061
1062 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001063 while (tlv_offset < total_v2_len) {
1064 struct tlv *tlv_packet;
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001065 struct ist tlv;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001066
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001067 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
1068 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
1069 goto bad_header;
1070
1071 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001072 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
1073 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001074
1075 /* Verify that the TLV length does not exceed the total PROXYv2 length */
1076 if (tlv_offset > total_v2_len)
1077 goto bad_header;
1078
1079 switch (tlv_packet->type) {
1080 case PP2_TYPE_CRC32C: {
1081 uint32_t n_crc32c;
1082
1083 /* Verify that this TLV is exactly 4 bytes long */
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001084 if (istlen(tlv) != 4)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001085 goto bad_header;
1086
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001087 n_crc32c = read_n32(istptr(tlv));
1088 write_n32(istptr(tlv), 0); // compute with CRC==0
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001089
1090 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
1091 goto bad_header;
1092 break;
1093 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001094#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001095 case PP2_TYPE_NETNS: {
1096 const struct netns_entry *ns;
1097
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001098 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001099 if (ns)
1100 conn->proxy_netns = ns;
1101 break;
1102 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001103#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001104 case PP2_TYPE_AUTHORITY: {
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001105 if (istlen(tlv) > PP2_AUTHORITY_MAX)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001106 goto bad_header;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001107 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
1108 if (!isttest(conn->proxy_authority))
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001109 goto fail;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001110 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
1111 /* This is technically unreachable, because we verified above
1112 * that the TLV value fits.
1113 */
1114 goto fail;
1115 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001116 break;
1117 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001118 case PP2_TYPE_UNIQUE_ID: {
Tim Duesterhus002bd772021-03-06 20:06:49 +01001119 if (istlen(tlv) > UNIQUEID_LEN)
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001120 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +01001121 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001122 if (!isttest(conn->proxy_unique_id))
1123 goto fail;
1124 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
1125 /* This is technically unreachable, because we verified above
1126 * that the TLV value fits.
1127 */
1128 goto fail;
1129 }
1130 break;
1131 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001132 default:
1133 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001134 }
1135 }
1136
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001137 /* Verify that the PROXYv2 header ends at a TLV boundary.
1138 * This is technically unreachable, because the TLV parsing already
1139 * verifies that a TLV does not exceed the total length and also
1140 * that there is space for a TLV header.
1141 */
1142 if (tlv_offset != total_v2_len)
1143 goto bad_header;
1144
Willy Tarreau77992672014-06-14 11:06:17 +02001145 /* unsupported protocol, keep local connection address */
1146 break;
1147 case 0x00: /* LOCAL command */
1148 /* keep local connection address for LOCAL */
1149 break;
1150 default:
1151 goto bad_header; /* not a supported command */
1152 }
1153
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001154 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +02001155 goto eat_header;
1156
1157 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001158 /* remove the PROXY line from the request. For this we re-read the
1159 * exact line at once. If we don't get the exact same result, we
1160 * fail.
1161 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001162 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001163 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
1164
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001165 if (len2 < 0 && errno == EINTR)
1166 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001167 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001168 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001169 break;
1170 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001171
1172 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +01001173 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001174 return 1;
1175
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001176 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001177 return 0;
1178
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001179 missing:
1180 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1181 * we have not read anything. Otherwise we need to fail because we won't
1182 * be able to poll anymore.
1183 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001184 conn->err_code = CO_ER_PRX_TRUNCATED;
1185 goto fail;
1186
1187 bad_header:
1188 /* This is not a valid proxy protocol header */
1189 conn->err_code = CO_ER_PRX_BAD_HDR;
1190 goto fail;
1191
1192 recv_abort:
1193 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +01001194 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001195 goto fail;
1196
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001197 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001198 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001199 return 0;
1200}
1201
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001202/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001203 * at the beginning of the raw data stream. The header format is
1204 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001205 *
1206 * This line MUST be at the beginning of the buffer and MUST NOT be
1207 * fragmented.
1208 *
1209 * The header line is small and in all cases smaller than the smallest normal
1210 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
1211 * can safely use MSG_PEEK and avoid buffering.
1212 *
1213 * Once the data is fetched, the values are set in the connection's address
1214 * fields, and data are removed from the socket's buffer. The function returns
1215 * zero if it needs to wait for more data or if it fails, or 1 if it completed
1216 * and removed itself.
1217 */
1218int conn_recv_netscaler_cip(struct connection *conn, int flag)
1219{
Christopher Faulete83e8822021-10-25 08:23:22 +02001220 struct session *sess = conn->owner;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001221 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001222 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001223 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +02001224 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001225
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001226 if (!conn_ctrl_ready(conn))
1227 goto fail;
1228
Willy Tarreau585744b2017-08-24 14:31:19 +02001229 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001230 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001231
Willy Tarreau157788c2020-02-11 10:08:05 +01001232 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +02001233 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
1234 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001235 if (errno == EINTR)
1236 continue;
1237 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +02001238 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001239 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001240 }
1241 goto recv_abort;
1242 }
Willy Tarreaub406b872018-08-22 05:20:32 +02001243 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +01001244 break;
1245 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001246
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001247 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1248
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001249 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001250 /* client shutdown */
1251 conn->err_code = CO_ER_CIP_EMPTY;
1252 goto fail;
1253 }
1254
1255 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001256 * CIP magic, header length or
1257 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001258 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001259 goto missing;
1260
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001261 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001262
1263 /* Decode a possible NetScaler Client IP request, fail early if
1264 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001265 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001266 goto bad_magic;
1267
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001268 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001269 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001270 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001271 line += 8;
1272 }
1273 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001274 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001275 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001276 line += 12;
1277 }
1278 /* Unknown CIP protocol */
1279 else {
1280 conn->err_code = CO_ER_CIP_BAD_PROTO;
1281 goto fail;
1282 }
1283
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001284 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001285 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001286 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001287 goto missing;
1288
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001289 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001290 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001291
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001292 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001293 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +01001294 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001295
1296 hdr_ip4 = (struct ip *)line;
1297
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001298 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001299 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001300 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001301 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001302 }
1303 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001304 /* The protocol does not include a TCP header */
1305 conn->err_code = CO_ER_CIP_BAD_PROTO;
1306 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001307 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001308
David Carlier3015a2e2016-07-04 22:51:33 +01001309 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001310
Christopher Faulete83e8822021-10-25 08:23:22 +02001311 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1312 goto fail;
1313
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001314 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001315 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
1316 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
1317 ((struct sockaddr_in *)sess->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001318
Christopher Faulete83e8822021-10-25 08:23:22 +02001319 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1320 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
1321 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001322
Christopher Faulete83e8822021-10-25 08:23:22 +02001323 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001324 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001325 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001326 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +01001327 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001328
1329 hdr_ip6 = (struct ip6_hdr *)line;
1330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001332 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001333 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001334 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001335 }
1336 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001337 /* The protocol does not include a TCP header */
1338 conn->err_code = CO_ER_CIP_BAD_PROTO;
1339 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001340 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001341
David Carlier3015a2e2016-07-04 22:51:33 +01001342 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001343
Christopher Faulete83e8822021-10-25 08:23:22 +02001344 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1345 goto fail;
1346
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001347 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001348 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1349 ((struct sockaddr_in6 *)sess->src)->sin6_addr = hdr_ip6->ip6_src;
1350 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001351
Christopher Faulete83e8822021-10-25 08:23:22 +02001352 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1353 ((struct sockaddr_in6 *)sess->dst)->sin6_addr = hdr_ip6->ip6_dst;
1354 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001355
Christopher Faulete83e8822021-10-25 08:23:22 +02001356 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001357 }
1358 else {
1359 /* The protocol does not match something known (IPv4/IPv6) */
1360 conn->err_code = CO_ER_CIP_BAD_PROTO;
1361 goto fail;
1362 }
1363
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001364 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001365 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001366
1367 /* remove the NetScaler Client IP header from the request. For this
1368 * we re-read the exact line at once. If we don't get the exact same
1369 * result, we fail.
1370 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001371 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001372 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001373 if (len2 < 0 && errno == EINTR)
1374 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001376 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001377 break;
1378 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001379
1380 conn->flags &= ~flag;
1381 return 1;
1382
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001383 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001384 return 0;
1385
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001386 missing:
1387 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1388 * we have not read anything. Otherwise we need to fail because we won't
1389 * be able to poll anymore.
1390 */
1391 conn->err_code = CO_ER_CIP_TRUNCATED;
1392 goto fail;
1393
1394 bad_magic:
1395 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1396 goto fail;
1397
1398 recv_abort:
1399 conn->err_code = CO_ER_CIP_ABORT;
1400 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1401 goto fail;
1402
1403 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001404 conn->flags |= CO_FL_ERROR;
1405 return 0;
1406}
1407
Alexander Liu2a54bb72019-05-22 19:44:48 +08001408
1409int conn_send_socks4_proxy_request(struct connection *conn)
1410{
1411 struct socks4_request req_line;
1412
Alexander Liu2a54bb72019-05-22 19:44:48 +08001413 if (!conn_ctrl_ready(conn))
1414 goto out_error;
1415
Willy Tarreau226572f2019-07-17 14:46:00 +02001416 if (!conn_get_dst(conn))
1417 goto out_error;
1418
Alexander Liu2a54bb72019-05-22 19:44:48 +08001419 req_line.version = 0x04;
1420 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001421 req_line.port = get_net_port(conn->dst);
1422 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001423 memcpy(req_line.user_id, "HAProxy\0", 8);
1424
1425 if (conn->send_proxy_ofs > 0) {
1426 /*
1427 * This is the first call to send the request
1428 */
1429 conn->send_proxy_ofs = -(int)sizeof(req_line);
1430 }
1431
1432 if (conn->send_proxy_ofs < 0) {
1433 int ret = 0;
1434
1435 /* we are sending the socks4_req_line here. If the data layer
1436 * has a pending write, we'll also set MSG_MORE.
1437 */
Willy Tarreau827fee72020-12-11 15:26:55 +01001438 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +08001439 conn,
1440 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1441 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +01001442 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001443
1444 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
1445 conn->handle.fd, -conn->send_proxy_ofs, ret);
1446
1447 if (ret < 0) {
1448 goto out_error;
1449 }
1450
1451 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1452 if (conn->send_proxy_ofs != 0) {
1453 goto out_wait;
1454 }
1455 }
1456
1457 /* OK we've the whole request sent */
1458 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001459
1460 /* The connection is ready now, simply return and let the connection
1461 * handler notify upper layers if needed.
1462 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001463 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001464
1465 if (conn->flags & CO_FL_SEND_PROXY) {
1466 /*
1467 * Get the send_proxy_ofs ready for the send_proxy due to we are
1468 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1469 * before sending PROXY Protocol.
1470 */
1471 conn->send_proxy_ofs = 1;
1472 }
1473 return 1;
1474
1475 out_error:
1476 /* Write error on the file descriptor */
1477 conn->flags |= CO_FL_ERROR;
1478 if (conn->err_code == CO_ER_NONE) {
1479 conn->err_code = CO_ER_SOCKS4_SEND;
1480 }
1481 return 0;
1482
1483 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001484 return 0;
1485}
1486
1487int conn_recv_socks4_proxy_response(struct connection *conn)
1488{
1489 char line[SOCKS4_HS_RSP_LEN];
1490 int ret;
1491
Alexander Liu2a54bb72019-05-22 19:44:48 +08001492 if (!conn_ctrl_ready(conn))
1493 goto fail;
1494
1495 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001496 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001497
Willy Tarreau157788c2020-02-11 10:08:05 +01001498 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001499 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1500 * Try to peek into it, before all 8 bytes ready.
1501 */
1502 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1503
1504 if (ret == 0) {
1505 /* the socket has been closed or shutdown for send */
1506 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1507 conn->handle.fd, ret, errno);
1508 if (conn->err_code == CO_ER_NONE) {
1509 conn->err_code = CO_ER_SOCKS4_RECV;
1510 }
1511 goto fail;
1512 }
1513
1514 if (ret > 0) {
1515 if (ret == SOCKS4_HS_RSP_LEN) {
1516 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1517 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1518 }else{
1519 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]);
1520 }
1521 } else {
1522 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1523 }
1524
1525 if (ret < 0) {
1526 if (errno == EINTR) {
1527 continue;
1528 }
1529 if (errno == EAGAIN) {
1530 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001531 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001532 }
1533 goto recv_abort;
1534 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001535 break;
1536 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001537
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001538 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1539
Alexander Liu2a54bb72019-05-22 19:44:48 +08001540 if (ret < SOCKS4_HS_RSP_LEN) {
1541 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1542 * we are not able to read enough data.
1543 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001544 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001545 }
1546
1547 /*
1548 * Base on the SOCSK4 protocol:
1549 *
1550 * +----+----+----+----+----+----+----+----+
1551 * | VN | CD | DSTPORT | DSTIP |
1552 * +----+----+----+----+----+----+----+----+
1553 * # of bytes: 1 1 2 4
1554 * VN is the version of the reply code and should be 0. CD is the result
1555 * code with one of the following values:
1556 * 90: request granted
1557 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001558 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001559 * 93: request rejected because the client program and identd report different user-ids
1560 * The remaining fields are ignored.
1561 */
1562 if (line[1] != 90) {
1563 conn->flags &= ~CO_FL_SOCKS4_RECV;
1564
1565 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1566 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1567 if (conn->err_code == CO_ER_NONE) {
1568 conn->err_code = CO_ER_SOCKS4_DENY;
1569 }
1570 goto fail;
1571 }
1572
1573 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001574 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001575 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1576 if (ret < 0 && errno == EINTR) {
1577 continue;
1578 }
1579 if (ret != SOCKS4_HS_RSP_LEN) {
1580 if (conn->err_code == CO_ER_NONE) {
1581 conn->err_code = CO_ER_SOCKS4_RECV;
1582 }
1583 goto fail;
1584 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001585 break;
1586 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001587
1588 conn->flags &= ~CO_FL_SOCKS4_RECV;
1589 return 1;
1590
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001591 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001592 return 0;
1593
Alexander Liu2a54bb72019-05-22 19:44:48 +08001594 recv_abort:
1595 if (conn->err_code == CO_ER_NONE) {
1596 conn->err_code = CO_ER_SOCKS4_ABORT;
1597 }
1598 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1599 goto fail;
1600
1601 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001602 conn->flags |= CO_FL_ERROR;
1603 return 0;
1604}
1605
Willy Tarreauadd43062022-02-18 11:07:40 +01001606/* Lists the known proto mux on <out>. This function is used by "haproxy -vv"
1607 * and is suitable for early boot just after the "REGISTER" stage because it
1608 * doesn't depend on anything to be already allocated.
1609 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001610void list_mux_proto(FILE *out)
1611{
1612 struct mux_proto_list *item;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001613 struct ist proto;
1614 char *mode, *side;
Willy Tarreauadd43062022-02-18 11:07:40 +01001615 int done;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001616
1617 fprintf(out, "Available multiplexer protocols :\n"
1618 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
1619 list_for_each_entry(item, &mux_proto_list.list, list) {
1620 proto = item->token;
1621
1622 if (item->mode == PROTO_MODE_ANY)
1623 mode = "TCP|HTTP";
1624 else if (item->mode == PROTO_MODE_TCP)
1625 mode = "TCP";
1626 else if (item->mode == PROTO_MODE_HTTP)
1627 mode = "HTTP";
1628 else
1629 mode = "NONE";
1630
1631 if (item->side == PROTO_SIDE_BOTH)
1632 side = "FE|BE";
1633 else if (item->side == PROTO_SIDE_FE)
1634 side = "FE";
1635 else if (item->side == PROTO_SIDE_BE)
1636 side = "BE";
1637 else
1638 side = "NONE";
1639
Willy Tarreauadd43062022-02-18 11:07:40 +01001640 fprintf(out, " %15s : mode=%-10s side=%-8s mux=%-8s flags=",
1641 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name);
1642
1643 done = 0;
1644
1645 /* note: the block below could be simplied using macros but for only
1646 * 4 flags it's not worth it.
1647 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001648 if (item->mux->flags & MX_FL_HTX)
Willy Tarreauadd43062022-02-18 11:07:40 +01001649 done |= fprintf(out, "%sHTX", done ? "|" : "");
1650
Willy Tarreaue59b5162021-05-08 14:06:09 +02001651 if (item->mux->flags & MX_FL_CLEAN_ABRT)
Willy Tarreauadd43062022-02-18 11:07:40 +01001652 done |= fprintf(out, "%sCLEAN_ABRT", done ? "|" : "");
1653
Willy Tarreaue59b5162021-05-08 14:06:09 +02001654 if (item->mux->flags & MX_FL_HOL_RISK)
Willy Tarreauadd43062022-02-18 11:07:40 +01001655 done |= fprintf(out, "%sHOL_RISK", done ? "|" : "");
1656
Willy Tarreaue59b5162021-05-08 14:06:09 +02001657 if (item->mux->flags & MX_FL_NO_UPG)
Willy Tarreauadd43062022-02-18 11:07:40 +01001658 done |= fprintf(out, "%sNO_UPG", done ? "|" : "");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001659
Willy Tarreauadd43062022-02-18 11:07:40 +01001660 fprintf(out, "\n");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001661 }
1662}
1663
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001664/* Makes a PROXY protocol line from the two addresses. The output is sent to
1665 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1666 * It returns the number of bytes composing this line (including the trailing
1667 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001668 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1669 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001670 */
Christopher Fauletb097aef2021-10-25 08:05:28 +02001671static int make_proxy_line_v1(char *buf, int buf_len, const struct sockaddr_storage *src, const struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001672{
1673 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001674 char * protocol;
1675 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1676 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1677 in_port_t src_port;
1678 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001679
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001680 if ( !src
1681 || !dst
1682 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1683 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1684 /* unknown family combination */
1685 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001686 if (ret >= buf_len)
1687 return 0;
1688
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001689 return ret;
1690 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001691
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001692 /* IPv4 for both src and dst */
1693 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1694 protocol = "TCP4";
1695 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001696 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001697 src_port = ((struct sockaddr_in *)src)->sin_port;
1698 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001699 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001700 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001701 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001702 /* IPv6 for at least one of src and dst */
1703 else {
1704 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001705
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001706 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001707
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001708 if (src->ss_family == AF_INET) {
1709 /* Convert src to IPv6 */
1710 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1711 src_port = ((struct sockaddr_in *)src)->sin_port;
1712 }
1713 else {
1714 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1715 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1716 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001717
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001718 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001719 return 0;
1720
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001721 if (dst->ss_family == AF_INET) {
1722 /* Convert dst to IPv6 */
1723 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1724 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1725 }
1726 else {
1727 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1728 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1729 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001730
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001731 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001732 return 0;
1733 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001734
1735 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1736 if (ret >= buf_len)
1737 return 0;
1738
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001739 return ret;
1740}
David Safb76832014-05-08 23:42:08 -04001741
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001742static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001743{
1744 struct tlv *tlv;
1745
1746 if (!dest || (length + sizeof(*tlv) > dest_len))
1747 return 0;
1748
1749 tlv = (struct tlv *)dest;
1750
1751 tlv->type = type;
1752 tlv->length_hi = length >> 8;
1753 tlv->length_lo = length & 0x00ff;
1754 memcpy(tlv->value, value, length);
1755 return length + sizeof(*tlv);
1756}
David Safb76832014-05-08 23:42:08 -04001757
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001758/* Note: <remote> is explicitly allowed to be NULL */
Christopher Faulet4bfce392021-10-22 14:33:59 +02001759static 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 -04001760{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001761 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001762 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001763 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001764 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001765 struct sockaddr_storage null_addr = { .ss_family = 0 };
Christopher Fauletb097aef2021-10-25 08:05:28 +02001766 const struct sockaddr_storage *src = &null_addr;
1767 const struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001768 const char *value;
1769 int value_len;
David Safb76832014-05-08 23:42:08 -04001770
1771 if (buf_len < PP2_HEADER_LEN)
1772 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001773 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001774
Christopher Fauletb097aef2021-10-25 08:05:28 +02001775 if (strm) {
1776 src = si_src(&strm->si[0]);
1777 dst = si_dst(&strm->si[0]);
1778 }
1779 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1780 src = conn_src(remote);
1781 dst = conn_dst(remote);
David Safb76832014-05-08 23:42:08 -04001782 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001783
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001784 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1785 if ( !src
1786 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001787 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001788 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1789 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001790 if (buf_len < PP2_HDR_LEN_UNSPEC)
1791 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001792 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1793 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001794 ret = PP2_HDR_LEN_UNSPEC;
1795 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001796 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001797 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001798 /* IPv4 for both src and dst */
1799 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1800 if (buf_len < PP2_HDR_LEN_INET)
1801 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001802 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1803 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1804 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1805 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1806 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1807 ret = PP2_HDR_LEN_INET;
1808 }
1809 /* IPv6 for at least one of src and dst */
1810 else {
1811 struct in6_addr tmp;
1812
1813 if (buf_len < PP2_HDR_LEN_INET6)
1814 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001815 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1816 if (src->ss_family == AF_INET) {
1817 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1818 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1819 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1820 }
1821 else {
1822 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1823 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1824 }
1825 if (dst->ss_family == AF_INET) {
1826 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1827 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001828 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001829 }
1830 else {
1831 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1832 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1833 }
1834
1835 ret = PP2_HDR_LEN_INET6;
1836 }
1837 }
David Safb76832014-05-08 23:42:08 -04001838
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001839 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1840 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001841
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001842 if ((buf_len - ret) < sizeof(struct tlv))
1843 return 0;
1844 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1845 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1846 }
1847
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001848 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001849 if ((buf_len - ret) < sizeof(struct tlv))
1850 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001851 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001852 }
1853
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001854 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001855 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001856 if (remote && isttest(remote->proxy_authority)) {
1857 value = istptr(remote->proxy_authority);
1858 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001859 }
1860#ifdef USE_OPENSSL
1861 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001862 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001863 value_len = strlen(value);
1864 }
1865#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001866 if (value) {
1867 if ((buf_len - ret) < sizeof(struct tlv))
1868 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001869 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001870 }
1871 }
1872
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001873 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001874 struct session* sess = strm_sess(strm);
1875 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1876
1877 value = unique_id.ptr;
1878 value_len = unique_id.len;
1879
1880 if (value_len >= 0) {
1881 if ((buf_len - ret) < sizeof(struct tlv))
1882 return 0;
1883 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1884 }
1885 }
1886
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001887#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001888 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001889 struct tlv_ssl *tlv;
1890 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001891
David Safb76832014-05-08 23:42:08 -04001892 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1893 return 0;
1894 tlv = (struct tlv_ssl *)&buf[ret];
1895 memset(tlv, 0, sizeof(struct tlv_ssl));
1896 ssl_tlv_len += sizeof(struct tlv_ssl);
1897 tlv->tlv.type = PP2_TYPE_SSL;
Willy Tarreau1057bee2021-10-06 11:38:44 +02001898 if (conn_is_ssl(remote)) {
David Safb76832014-05-08 23:42:08 -04001899 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001900 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001901 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001902 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 -04001903 }
Dave McCowan328fb582014-07-30 10:39:13 -04001904 if (ssl_sock_get_cert_used_sess(remote)) {
1905 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001906 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001907 if (ssl_sock_get_cert_used_conn(remote))
1908 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001909 }
1910 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001911 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001912 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001913 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1914 cn_trash->data,
1915 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001916 }
1917 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001918 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001919 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001920 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001921 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1922 pkey_trash->data,
1923 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001924 }
1925 }
1926 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1927 value = ssl_sock_get_cert_sig(remote);
1928 if (value) {
1929 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1930 }
1931 }
1932 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1933 value = ssl_sock_get_cipher_name(remote);
1934 if (value) {
1935 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1936 }
1937 }
David Safb76832014-05-08 23:42:08 -04001938 }
1939 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1940 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1941 ret += ssl_tlv_len;
1942 }
1943#endif
1944
Willy Tarreaue5733232019-05-22 19:24:06 +02001945#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001946 if (remote && (remote->proxy_netns)) {
1947 if ((buf_len - ret) < sizeof(struct tlv))
1948 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001949 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 +01001950 }
1951#endif
1952
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001953 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001954
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001955 if (tlv_crc32c_p) {
1956 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1957 }
1958
David Safb76832014-05-08 23:42:08 -04001959 return ret;
1960}
Emeric Brun4f603012017-01-05 15:11:44 +01001961
Christopher Faulet4bfce392021-10-22 14:33:59 +02001962/* Note: <remote> is explicitly allowed to be NULL */
1963int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
1964{
1965 int ret = 0;
1966
1967 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1968 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
1969 }
1970 else {
Christopher Fauletb097aef2021-10-25 08:05:28 +02001971 const struct sockaddr_storage *src = NULL;
1972 const struct sockaddr_storage *dst = NULL;
1973
1974 if (strm) {
1975 src = si_src(&strm->si[0]);
1976 dst = si_dst(&strm->si[0]);
1977 }
1978 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1979 src = conn_src(remote);
1980 dst = conn_dst(remote);
1981 }
1982
1983 if (src && dst)
1984 ret = make_proxy_line_v1(buf, buf_len, src, dst);
Christopher Faulet4bfce392021-10-22 14:33:59 +02001985 else
1986 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1987 }
1988
1989 return ret;
1990}
1991
Willy Tarreau119e50e2020-05-22 13:53:29 +02001992/* returns 0 on success */
1993static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001994 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02001995 char **err)
1996{
1997 if (too_many_args(0, args, err, NULL))
1998 return -1;
1999 pp2_never_send_local = 1;
2000 return 0;
2001}
2002
Willy Tarreaud943a042021-06-16 17:35:20 +02002003/* extracts some info from the connection and appends them to buffer <buf>. The
2004 * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source
2005 * when set, destination when set, are printed in a compact human-readable format
2006 * fitting on a single line. This is handy to complete traces or debug output.
2007 * It is permitted to pass a NULL conn pointer. The number of characters emitted
2008 * is returned. A prefix <pfx> might be prepended before the first field if not
2009 * NULL.
2010 */
2011int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx)
2012{
2013 const struct listener *li;
2014 const struct server *sv;
2015 const struct proxy *px;
2016 char addr[40];
2017 int old_len = buf->data;
2018
2019 if (!conn)
2020 return 0;
2021
2022 chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN");
2023
2024 if ((li = objt_listener(conn->target)))
2025 chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id);
2026 else if ((sv = objt_server(conn->target)))
2027 chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id);
2028 else if ((px = objt_proxy(conn->target)))
2029 chunk_appendf(buf, " be=%s", px->id);
2030
2031 chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn));
2032
2033 if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr)))
2034 chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src));
2035
2036 if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr)))
2037 chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst));
2038
2039 return buf->data - old_len;
2040}
2041
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002042/* return the major HTTP version as 1 or 2 depending on how the request arrived
2043 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002044 *
2045 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002046 */
2047static int
2048smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
2049{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02002050 struct connection *conn = NULL;
2051
2052 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2053 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2054 else
2055 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2056 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002057
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002058 /* No connection or a connection with a RAW muxx */
2059 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
2060 return 0;
2061
2062 /* No mux install, this may change */
2063 if (!conn->mux) {
2064 smp->flags |= SMP_F_MAY_CHANGE;
2065 return 0;
2066 }
2067
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002068 smp->data.type = SMP_T_SINT;
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002069 smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002070 return 1;
2071}
2072
Emeric Brun4f603012017-01-05 15:11:44 +01002073/* fetch if the received connection used a PROXY protocol header */
2074int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
2075{
2076 struct connection *conn;
2077
2078 conn = objt_conn(smp->sess->origin);
2079 if (!conn)
2080 return 0;
2081
Willy Tarreau911db9b2020-01-23 16:27:54 +01002082 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01002083 smp->flags |= SMP_F_MAY_CHANGE;
2084 return 0;
2085 }
2086
2087 smp->flags = 0;
2088 smp->data.type = SMP_T_BOOL;
2089 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
2090
2091 return 1;
2092}
2093
Geoff Simmons7185b782019-08-27 18:31:16 +02002094/* fetch the authority TLV from a PROXY protocol header */
2095int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
2096{
2097 struct connection *conn;
2098
2099 conn = objt_conn(smp->sess->origin);
2100 if (!conn)
2101 return 0;
2102
Willy Tarreau911db9b2020-01-23 16:27:54 +01002103 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02002104 smp->flags |= SMP_F_MAY_CHANGE;
2105 return 0;
2106 }
2107
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002108 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02002109 return 0;
2110
2111 smp->flags = 0;
2112 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002113 smp->data.u.str.area = istptr(conn->proxy_authority);
2114 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02002115
2116 return 1;
2117}
2118
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002119/* fetch the unique ID TLV from a PROXY protocol header */
2120int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2121{
2122 struct connection *conn;
2123
2124 conn = objt_conn(smp->sess->origin);
2125 if (!conn)
2126 return 0;
2127
2128 if (conn->flags & CO_FL_WAIT_XPRT) {
2129 smp->flags |= SMP_F_MAY_CHANGE;
2130 return 0;
2131 }
2132
2133 if (!isttest(conn->proxy_unique_id))
2134 return 0;
2135
2136 smp->flags = 0;
2137 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01002138 smp->data.u.str.area = istptr(conn->proxy_unique_id);
2139 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002140
2141 return 1;
2142}
2143
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002144/* fetch the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002145int smp_fetch_fc_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002146{
2147 struct connection *conn;
2148
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002149 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2150 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2151 else
2152 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2153 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2154
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002155 if (!conn)
2156 return 0;
2157
2158 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2159 smp->flags |= SMP_F_MAY_CHANGE;
2160 return 0;
2161 }
2162
2163 smp->flags = 0;
2164 smp->data.type = SMP_T_SINT;
2165 smp->data.u.sint = (unsigned long long int)conn->err_code;
2166
2167 return 1;
2168}
2169
2170/* fetch a string representation of the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002171int smp_fetch_fc_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002172{
2173 struct connection *conn;
2174 const char *err_code_str;
2175
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002176 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2177 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2178 else
2179 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
2180 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
2181
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002182 if (!conn)
2183 return 0;
2184
2185 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2186 smp->flags |= SMP_F_MAY_CHANGE;
2187 return 0;
2188 }
2189
2190 err_code_str = conn_err_code_str(conn);
2191
2192 if (!err_code_str)
2193 return 0;
2194
2195 smp->flags = 0;
2196 smp->data.type = SMP_T_STR;
2197 smp->data.u.str.area = (char*)err_code_str;
2198 smp->data.u.str.data = strlen(err_code_str);
2199
2200 return 1;
2201}
2202
Emeric Brun4f603012017-01-05 15:11:44 +01002203/* Note: must not be declared <const> as its list will be overwritten.
2204 * Note: fetches that may return multiple types must be declared as the lowest
2205 * common denominator, the type that can be casted into all other ones. For
2206 * instance v4/v6 must be declared v4.
2207 */
2208static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau6f749762021-11-05 17:07:03 +01002209 { "bc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
2210 { "bc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4SRV },
Jérôme Magnin86577422018-12-07 09:03:11 +01002211 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Willy Tarreau6f749762021-11-05 17:07:03 +01002212 { "fc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
2213 { "fc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
2214 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01002215 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02002216 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002217 { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01002218 { /* END */ },
2219}};
2220
Willy Tarreau0108d902018-11-25 19:14:37 +01002221INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02002222
2223static struct cfg_kw_list cfg_kws = {ILH, {
2224 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
2225 { /* END */ },
2226}};
2227
2228INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002229
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002230/* private function to handle sockaddr as input for connection hash */
2231static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
2232 char *buf, size_t *idx,
2233 enum conn_hash_params_t *hash_flags,
2234 enum conn_hash_params_t param_type_addr,
2235 enum conn_hash_params_t param_type_port)
2236{
2237 struct sockaddr_in *addr;
2238 struct sockaddr_in6 *addr6;
2239
2240 switch (ss->ss_family) {
2241 case AF_INET:
2242 addr = (struct sockaddr_in *)ss;
2243
2244 conn_hash_update(buf, idx,
2245 &addr->sin_addr, sizeof(addr->sin_addr),
2246 hash_flags, param_type_addr);
2247
2248 if (addr->sin_port) {
2249 conn_hash_update(buf, idx,
2250 &addr->sin_port, sizeof(addr->sin_port),
2251 hash_flags, param_type_port);
2252 }
2253
2254 break;
2255
2256 case AF_INET6:
2257 addr6 = (struct sockaddr_in6 *)ss;
2258
2259 conn_hash_update(buf, idx,
2260 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
2261 hash_flags, param_type_addr);
2262
2263 if (addr6->sin6_port) {
2264 conn_hash_update(buf, idx,
2265 &addr6->sin6_port, sizeof(addr6->sin6_port),
2266 hash_flags, param_type_port);
2267 }
2268
2269 break;
2270 }
2271}
2272
Willy Tarreaue5983ff2021-10-06 17:14:49 +02002273/* Generate the hash of a connection with params as input
2274 * Each non-null field of params is taken into account for the hash calcul.
2275 */
2276uint64_t conn_hash_prehash(char *buf, size_t size)
2277{
2278 return XXH64(buf, size, 0);
2279}
2280
2281/* Append <data> into <buf> at <idx> offset in preparation for connection hash
2282 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
2283 * are updated with <type> for the hash header.
2284 */
2285void conn_hash_update(char *buf, size_t *idx,
2286 const void *data, size_t size,
2287 enum conn_hash_params_t *flags,
2288 enum conn_hash_params_t type)
2289{
2290 memcpy(&buf[*idx], data, size);
2291 *idx += size;
2292 *flags |= type;
2293}
2294
2295uint64_t conn_hash_digest(char *buf, size_t bufsize,
2296 enum conn_hash_params_t flags)
2297{
2298 const uint64_t flags_u64 = (uint64_t)flags;
2299 const uint64_t hash = XXH64(buf, bufsize, 0);
2300
2301 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
2302}
2303
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002304uint64_t conn_calculate_hash(const struct conn_hash_params *params)
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002305{
2306 char *buf;
2307 size_t idx = 0;
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002308 uint64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002309 enum conn_hash_params_t hash_flags = 0;
2310
2311 buf = trash.area;
2312
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01002313 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01002314
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002315 if (params->sni_prehash) {
2316 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002317 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002318 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
2319 }
2320
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002321 if (params->dst_addr) {
2322 conn_calculate_hash_sockaddr(params->dst_addr,
2323 buf, &idx, &hash_flags,
2324 CONN_HASH_PARAMS_TYPE_DST_ADDR,
2325 CONN_HASH_PARAMS_TYPE_DST_PORT);
2326 }
2327
Amaury Denoyelled10a2002021-02-11 19:45:19 +01002328 if (params->src_addr) {
2329 conn_calculate_hash_sockaddr(params->src_addr,
2330 buf, &idx, &hash_flags,
2331 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
2332 CONN_HASH_PARAMS_TYPE_SRC_PORT);
2333 }
2334
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002335 if (params->proxy_prehash) {
2336 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002337 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002338 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
2339 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002340
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002341 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002342 return hash;
2343}
Amaury Denoyelle48372932021-09-16 12:15:12 +02002344
2345/* Handler of the task of mux_stopping_data.
2346 * Called on soft-stop.
2347 */
2348static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state)
2349{
2350 struct connection *conn, *back;
2351
2352 list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) {
2353 if (conn->mux && conn->mux->wake)
2354 conn->mux->wake(conn);
2355 }
2356
2357 return t;
2358}
2359
2360static int allocate_mux_cleanup(void)
2361{
2362 /* allocates the thread bound mux_stopping_data task */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002363 mux_stopping_data[tid].task = task_new_here();
Amaury Denoyelle48372932021-09-16 12:15:12 +02002364 if (!mux_stopping_data[tid].task) {
2365 ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid);
2366 return 0;
2367 }
2368
2369 mux_stopping_data[tid].task->process = mux_stopping_process;
2370 LIST_INIT(&mux_stopping_data[tid].list);
2371
2372 return 1;
2373}
2374REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup);
2375
2376static int deallocate_mux_cleanup(void)
2377{
2378 task_destroy(mux_stopping_data[tid].task);
2379 return 1;
2380}
2381REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup);