blob: b666d18760a606fdc5af64187cce7c3eff6e4b87 [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>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010020#include <haproxy/conn_stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Willy Tarreauaac777f2021-10-06 18:48:28 +020024#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <haproxy/log-t.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020026#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020027#include <haproxy/net_helper.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020028#include <haproxy/proto_tcp.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020029#include <haproxy/sample.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010030#include <haproxy/session.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020031#include <haproxy/stream_interface.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010032#include <haproxy/ssl_sock.h>
Willy Tarreau908908e2021-05-08 13:07:31 +020033#include <haproxy/tools.h>
Willy Tarreaue5983ff2021-10-06 17:14:49 +020034#include <haproxy/xxhash.h>
Emeric Brun46591952012-05-18 15:47:34 +020035
Alexander Liu2a54bb72019-05-22 19:44:48 +080036
Amaury Denoyelle8990b012021-02-19 15:29:16 +010037DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
Amaury Denoyelle8990b012021-02-19 15:29:16 +010038DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node));
39DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
40DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010041
Willy Tarreau4d82bf52020-06-28 00:19:17 +020042struct idle_conns idle_conns[MAX_THREADS] = { };
Willy Tarreau13e14102016-12-22 20:25:26 +010043struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020044
Christopher Faulet32f61c02018-04-10 14:33:41 +020045/* List head of all known muxes for PROTO */
46struct mux_proto_list mux_proto_list = {
47 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020048};
49
Amaury Denoyelled3a88c12021-05-03 10:47:51 +020050struct mux_stopping_data mux_stopping_data[MAX_THREADS];
51
Willy Tarreau119e50e2020-05-22 13:53:29 +020052/* disables sending of proxy-protocol-v2's LOCAL command */
53static int pp2_never_send_local;
54
Willy Tarreau930428c2021-10-06 18:27:28 +020055void conn_delete_from_tree(struct ebmb_node *node)
56{
57 ebmb_delete(node);
Willy Tarreau930428c2021-10-06 18:27:28 +020058}
59
Olivier Houchard477902b2020-01-22 18:08:48 +010060int conn_create_mux(struct connection *conn)
61{
Olivier Houchard477902b2020-01-22 18:08:48 +010062 if (conn_is_back(conn)) {
63 struct server *srv;
64 struct conn_stream *cs = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020065 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010066
67 if (conn->flags & CO_FL_ERROR)
68 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010069
Christopher Faulet14cd3162020-04-16 14:50:06 +020070 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010071 if (conn_install_mux_chk(conn, conn->ctx, sess) < 0)
Christopher Faulet14cd3162020-04-16 14:50:06 +020072 goto fail;
73 }
Amaury Denoyelleac03ef22021-10-28 16:36:11 +020074 else if (conn_install_mux_be(conn, conn->ctx, sess, NULL) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010075 goto fail;
76 srv = objt_server(conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020077
78 /* If we're doing http-reuse always, and the connection is not
79 * private with available streams (an http2 connection), add it
80 * to the available list, so that others can use it right
81 * away. If the connection is private, add it in the session
82 * server list.
83 */
Christopher Faulet2883fcf2020-07-01 14:59:43 +020084 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) &&
Christopher Fauletaa278532020-06-30 14:47:46 +020085 !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0)
Willy Tarreau430bf4a2021-03-04 09:45:32 +010086 ebmb_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Christopher Faulet08016ab2020-07-01 16:10:06 +020087 else if (conn->flags & CO_FL_PRIVATE) {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +050088 /* If it fail now, the same will be done in mux->detach() callback */
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010089 session_add_conn(sess, conn, conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020090 }
Olivier Houchard477902b2020-01-22 18:08:48 +010091 return 0;
92fail:
93 /* let the upper layer know the connection failed */
94 cs->data_cb->wake(cs);
95 return -1;
96 } else
97 return conn_complete_session(conn);
98
Willy Tarreau930428c2021-10-06 18:27:28 +020099}
100
101/* This is used at the end of the socket IOCB to possibly create the mux if it
102 * was not done yet, or wake it up if flags changed compared to old_flags or if
103 * need_wake insists on this. It returns <0 if the connection was destroyed and
104 * must not be used, >=0 otherwise.
105 */
106int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake)
107{
108 int ret = 0;
109
110 /* If we don't yet have a mux, that means we were waiting for
111 * information to create one, typically from the ALPN. If we're
112 * done with the handshake, attempt to create one.
113 */
114 if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) {
115 ret = conn_create_mux(conn);
116 if (ret < 0)
117 goto done;
118 }
119
120 /* The wake callback is normally used to notify the data layer about
121 * data layer activity (successful send/recv), connection establishment,
122 * shutdown and fatal errors. We need to consider the following
123 * situations to wake up the data layer :
124 * - change among the CO_FL_NOTIFY_DONE flags :
125 * SOCK_{RD,WR}_SH, ERROR,
126 * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the
127 * end of handshake and transition to CONNECTED
128 * - raise of CONNECTED with HANDSHAKE down
129 * - end of HANDSHAKE with CONNECTED set
130 * - regular data layer activity
131 *
132 * One tricky case is the wake up on read0 or error on an idle
133 * backend connection, that can happen on a connection that is still
134 * polled while at the same moment another thread is about to perform a
135 * takeover. The solution against this is to remove the connection from
136 * the idle list if it was in it, and possibly reinsert it at the end
137 * if the connection remains valid. The cost is non-null (locked tree
138 * removal) but remains low given that this is extremely rarely called.
139 * In any case it's guaranteed by the FD's thread_mask that we're
140 * called from the same thread the connection is queued in.
141 *
142 * Note that the wake callback is allowed to release the connection and
143 * the fd (and return < 0 in this case).
144 */
145 if ((forced_wake ||
146 ((conn->flags ^ old_flags) & CO_FL_NOTIFY_DONE) ||
147 ((old_flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) &&
148 conn->mux && conn->mux->wake) {
149 uint conn_in_list = conn->flags & CO_FL_LIST_MASK;
150 struct server *srv = objt_server(conn->target);
151
152 if (conn_in_list) {
153 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
154 conn_delete_from_tree(&conn->hash_node->node);
155 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
156 }
157
158 ret = conn->mux->wake(conn);
159 if (ret < 0)
160 goto done;
161
162 if (conn_in_list) {
163 struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ?
164 &srv->per_thr[tid].safe_conns :
165 &srv->per_thr[tid].idle_conns;
166
167 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
168 ebmb_insert(root, &conn->hash_node->node, sizeof(conn->hash_node->hash));
169 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
170 }
171 }
172 done:
173 return ret;
Olivier Houchard477902b2020-01-22 18:08:48 +0100174}
175
Willy Tarreauaac777f2021-10-06 18:48:28 +0200176/* Change the mux for the connection.
177 * The caller should make sure he's not subscribed to the underlying XPRT.
178 */
179int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
180 struct ist mux_proto, int mode)
181{
182 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
183 const struct mux_ops *old_mux, *new_mux;
184 void *old_mux_ctx;
185 const char *alpn_str = NULL;
186 int alpn_len = 0;
187
188 if (!mux_proto.len) {
189 conn_get_alpn(conn, &alpn_str, &alpn_len);
190 mux_proto = ist2(alpn_str, alpn_len);
191 }
192 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
193 old_mux = conn->mux;
194
195 /* No mux found */
196 if (!new_mux)
197 return -1;
198
199 /* Same mux, nothing to do */
200 if (old_mux == new_mux)
201 return 0;
202
203 old_mux_ctx = conn->ctx;
204 conn->mux = new_mux;
205 conn->ctx = ctx;
206 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
207 /* The mux upgrade failed, so restore the old mux */
208 conn->ctx = old_mux_ctx;
209 conn->mux = old_mux;
210 return -1;
211 }
212
213 /* The mux was upgraded, destroy the old one */
214 *buf = BUF_NULL;
215 old_mux->destroy(old_mux_ctx);
216 return 0;
217}
218
219/* installs the best mux for incoming connection <conn> using the upper context
220 * <ctx>. If the mux protocol is forced, we use it to find the best
221 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
222 */
223int conn_install_mux_fe(struct connection *conn, void *ctx)
224{
225 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
226 const struct mux_ops *mux_ops;
227
228 if (bind_conf->mux_proto)
229 mux_ops = bind_conf->mux_proto->mux;
230 else {
231 struct ist mux_proto;
232 const char *alpn_str = NULL;
233 int alpn_len = 0;
234 int mode;
235
236 if (bind_conf->frontend->mode == PR_MODE_HTTP)
237 mode = PROTO_MODE_HTTP;
238 else
239 mode = PROTO_MODE_TCP;
240
241 conn_get_alpn(conn, &alpn_str, &alpn_len);
242 mux_proto = ist2(alpn_str, alpn_len);
243 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
244 if (!mux_ops)
245 return -1;
246 }
247 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
248}
249
250/* installs the best mux for outgoing connection <conn> using the upper context
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200251 * <ctx>. If the server mux protocol is forced, we use it to find the best mux.
252 * It's also possible to specify an alternative mux protocol <force_mux_ops>,
253 * in which case it will be used instead of the default server mux protocol.
254 *
255 * Returns < 0 on error.
Willy Tarreauaac777f2021-10-06 18:48:28 +0200256 */
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200257int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess,
258 const struct mux_ops *force_mux_ops)
Willy Tarreauaac777f2021-10-06 18:48:28 +0200259{
260 struct server *srv = objt_server(conn->target);
261 struct proxy *prx = objt_proxy(conn->target);
262 const struct mux_ops *mux_ops;
263
264 if (srv)
265 prx = srv->proxy;
266
267 if (!prx) // target must be either proxy or server
268 return -1;
269
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200270 if (srv && srv->mux_proto && likely(!force_mux_ops)) {
Willy Tarreauaac777f2021-10-06 18:48:28 +0200271 mux_ops = srv->mux_proto->mux;
Amaury Denoyelleac03ef22021-10-28 16:36:11 +0200272 }
273 else if (srv && unlikely(force_mux_ops)) {
274 mux_ops = force_mux_ops;
275 }
Willy Tarreauaac777f2021-10-06 18:48:28 +0200276 else {
277 struct ist mux_proto;
278 const char *alpn_str = NULL;
279 int alpn_len = 0;
280 int mode;
281
282 if (prx->mode == PR_MODE_HTTP)
283 mode = PROTO_MODE_HTTP;
284 else
285 mode = PROTO_MODE_TCP;
286
287 conn_get_alpn(conn, &alpn_str, &alpn_len);
288 mux_proto = ist2(alpn_str, alpn_len);
289
290 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
291 if (!mux_ops)
292 return -1;
293 }
294 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
295}
296
297/* installs the best mux for outgoing connection <conn> for a check using the
298 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
299 * find the best mux. Returns < 0 on error.
300 */
301int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
302{
303 struct check *check = objt_check(sess->origin);
304 struct server *srv = objt_server(conn->target);
305 struct proxy *prx = objt_proxy(conn->target);
306 const struct mux_ops *mux_ops;
307
308 if (!check) // Check must be defined
309 return -1;
310
311 if (srv)
312 prx = srv->proxy;
313
314 if (!prx) // target must be either proxy or server
315 return -1;
316
317 if (check->mux_proto)
318 mux_ops = check->mux_proto->mux;
319 else {
320 struct ist mux_proto;
321 const char *alpn_str = NULL;
322 int alpn_len = 0;
323 int mode;
324
325 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
326 mode = PROTO_MODE_HTTP;
327 else
328 mode = PROTO_MODE_TCP;
329
330 conn_get_alpn(conn, &alpn_str, &alpn_len);
331 mux_proto = ist2(alpn_str, alpn_len);
332
333 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
334 if (!mux_ops)
335 return -1;
336 }
337 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
338}
339
Amaury Denoyelle2454bda2021-10-18 14:32:36 +0200340/* Set the ALPN of connection <conn> to <alpn>. If force is false, <alpn> must
341 * be a subset or identical to the registered protos for the parent SSL_CTX.
342 * In this case <alpn> must be a single protocol value, not a list.
343 *
344 * Returns 0 if ALPN is updated else -1.
345 */
346int conn_update_alpn(struct connection *conn, const struct ist alpn, int force)
347{
348#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
349 size_t alpn_len = istlen(alpn);
350 char *ctx_alpn_str = NULL;
351 int ctx_alpn_len = 0, found = 0;
352
353 /* if not force, first search if alpn is a subset or identical to the
354 * parent SSL_CTX.
355 */
356 if (!force) {
357 /* retrieve the SSL_CTX according to the connection side. */
358 if (conn_is_back(conn)) {
359 if (obj_type(conn->target) == OBJ_TYPE_SERVER) {
360 struct server *srv = __objt_server(conn->target);
361 ctx_alpn_str = srv->ssl_ctx.alpn_str;
362 ctx_alpn_len = srv->ssl_ctx.alpn_len;
363 }
364 }
365 else {
366 struct session *sess = conn->owner;
367 struct listener *li = sess->listener;
368
369 if (li->bind_conf && li->bind_conf->is_ssl) {
370 ctx_alpn_str = li->bind_conf->ssl_conf.alpn_str;
371 ctx_alpn_len = li->bind_conf->ssl_conf.alpn_len;
372 }
373 }
374
375 if (ctx_alpn_str) {
376 /* search if ALPN is present in SSL_CTX ALPN before
377 * using it.
378 */
379 while (ctx_alpn_len) {
380 /* skip ALPN whose size is not 8 */
381 if (*ctx_alpn_str != alpn_len - 1) {
382 ctx_alpn_len -= *ctx_alpn_str + 1;
383 }
384 else {
385 if (isteqi(ist2(ctx_alpn_str, alpn_len), alpn)) {
386 found = 1;
387 break;
388 }
389 }
390 ctx_alpn_str += *ctx_alpn_str + 1;
391
392 /* This indicates an invalid ALPN formatted
393 * string and should never happen. */
394 BUG_ON(ctx_alpn_len < 0);
395 }
396 }
397 }
398
399 if (found || force) {
400 ssl_sock_set_alpn(conn, (const uchar *)istptr(alpn), istlen(alpn));
401 return 0;
402 }
403
404#endif
405 return -1;
406}
407
Willy Tarreauaac777f2021-10-06 18:48:28 +0200408/* Initializes all required fields for a new connection. Note that it does the
409 * minimum acceptable initialization for a connection that already exists and
410 * is about to be reused. It also leaves the addresses untouched, which makes
411 * it usable across connection retries to reset a connection to a known state.
412 */
413void conn_init(struct connection *conn, void *target)
414{
415 conn->obj_type = OBJ_TYPE_CONN;
416 conn->flags = CO_FL_NONE;
417 conn->mux = NULL;
418 conn->ctx = NULL;
419 conn->owner = NULL;
420 conn->send_proxy_ofs = 0;
421 conn->handle.fd = DEAD_FD_MAGIC;
422 conn->err_code = CO_ER_NONE;
423 conn->target = target;
424 conn->destroy_cb = NULL;
425 conn->proxy_netns = NULL;
426 MT_LIST_INIT(&conn->toremove_list);
427 if (conn_is_back(conn))
428 LIST_INIT(&conn->session_list);
429 else
430 LIST_INIT(&conn->stopping_list);
431 conn->subs = NULL;
432 conn->src = NULL;
433 conn->dst = NULL;
434 conn->proxy_authority = IST_NULL;
435 conn->proxy_unique_id = IST_NULL;
436 conn->qc = NULL;
437 conn->hash_node = NULL;
438 conn->xprt = NULL;
439}
440
441/* Tries to allocate a new connection and initialized its main fields. The
442 * connection is returned on success, NULL on failure. The connection must
443 * be released using pool_free() or conn_free().
444 */
445struct connection *conn_new(void *target)
446{
447 struct connection *conn;
448 struct conn_hash_node *hash_node;
449
450 conn = pool_alloc(pool_head_connection);
451 if (unlikely(!conn))
452 return NULL;
453
454 conn_init(conn, target);
455
456 if (conn_is_back(conn)) {
457 if (obj_type(target) == OBJ_TYPE_SERVER)
458 srv_use_conn(__objt_server(target), conn);
459
460 hash_node = conn_alloc_hash_node(conn);
461 if (unlikely(!hash_node)) {
462 pool_free(pool_head_connection, conn);
463 return NULL;
464 }
465
466 conn->hash_node = hash_node;
467 }
468
469 return conn;
470}
471
472/* Releases a connection previously allocated by conn_new() */
473void conn_free(struct connection *conn)
474{
475 /* If the connection is owned by the session, remove it from its list
476 */
477 if (conn_is_back(conn) && LIST_INLIST(&conn->session_list)) {
478 session_unown_conn(conn->owner, conn);
479 }
480 else if (!(conn->flags & CO_FL_PRIVATE)) {
481 if (obj_type(conn->target) == OBJ_TYPE_SERVER)
482 srv_release_conn(__objt_server(conn->target), conn);
483 }
484
485 /* Remove the conn from toremove_list.
486 *
487 * This is needed to prevent a double-free in case the connection was
488 * already scheduled from cleaning but is freed before via another
489 * call.
490 */
491 MT_LIST_DELETE(&conn->toremove_list);
492
493 sockaddr_free(&conn->src);
494 sockaddr_free(&conn->dst);
495
496 pool_free(pool_head_authority, istptr(conn->proxy_authority));
497 conn->proxy_authority = IST_NULL;
498
499 pool_free(pool_head_uniqueid, istptr(conn->proxy_unique_id));
500 conn->proxy_unique_id = IST_NULL;
501
502 pool_free(pool_head_conn_hash_node, conn->hash_node);
503 conn->hash_node = NULL;
504
505 /* By convention we always place a NULL where the ctx points to if the
506 * mux is null. It may have been used to store the connection as a
507 * stream_interface's end point for example.
508 */
509 if (conn->ctx != NULL && conn->mux == NULL)
510 *(void **)conn->ctx = NULL;
511
512 conn_force_unsubscribe(conn);
513 pool_free(pool_head_connection, conn);
514}
515
Willy Tarreau8de90c72021-10-06 19:11:10 +0200516struct conn_hash_node *conn_alloc_hash_node(struct connection *conn)
517{
518 struct conn_hash_node *hash_node = NULL;
519
520 hash_node = pool_zalloc(pool_head_conn_hash_node);
521 if (unlikely(!hash_node))
522 return NULL;
523
524 hash_node->conn = conn;
525
526 return hash_node;
527}
528
529/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
530 * returns it. If <sap> is NULL, the address is always allocated and returned.
531 * if <sap> is non-null, an address will only be allocated if it points to a
532 * non-null pointer. In this case the allocated address will be assigned there.
533 * If <orig> is non-null and <len> positive, the address in <sa> will be copied
534 * into the allocated address. In both situations the new pointer is returned.
535 */
536struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
537{
538 struct sockaddr_storage *sa;
539
540 if (sap && *sap)
541 return *sap;
542
543 sa = pool_alloc(pool_head_sockaddr);
544 if (sa && orig && len > 0)
545 memcpy(sa, orig, len);
546 if (sap)
547 *sap = sa;
548 return sa;
549}
550
551/* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
552 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
553 * there.
554 */
555void sockaddr_free(struct sockaddr_storage **sap)
556{
557 if (!sap)
558 return;
559 pool_free(pool_head_sockaddr, *sap);
560 *sap = NULL;
561}
562
Willy Tarreauaac777f2021-10-06 18:48:28 +0200563/* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
564 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
565 * call the xprt's add_xprt() method.
566 * Returns 0 on success, or non-zero on failure.
567 */
568int xprt_add_hs(struct connection *conn)
569{
570 void *xprt_ctx = NULL;
571 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
572 void *nextxprt_ctx = NULL;
573 const struct xprt_ops *nextxprt_ops = NULL;
574
575 if (conn->flags & CO_FL_ERROR)
576 return -1;
577 if (ops->init(conn, &xprt_ctx) < 0)
578 return -1;
579 if (conn->xprt == xprt_get(XPRT_RAW)) {
580 nextxprt_ctx = conn->xprt_ctx;
581 nextxprt_ops = conn->xprt;
582 conn->xprt_ctx = xprt_ctx;
583 conn->xprt = ops;
584 } else {
585 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
586 &nextxprt_ctx, &nextxprt_ops) != 0) {
587 ops->close(conn, xprt_ctx);
588 return -1;
589 }
590 }
591 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
592 ops->close(conn, xprt_ctx);
593 return -1;
594 }
595 return 0;
596}
597
598/* returns a human-readable error code for conn->err_code, or NULL if the code
599 * is unknown.
600 */
601const char *conn_err_code_str(struct connection *c)
602{
603 switch (c->err_code) {
604 case CO_ER_NONE: return "Success";
605
606 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
607 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
608 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
609 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
610 case CO_ER_NOPROTO: return "Protocol or address family not supported";
611 case CO_ER_SOCK_ERR: return "General socket error";
612 case CO_ER_PORT_RANGE: return "Source port range exhausted";
613 case CO_ER_CANT_BIND: return "Can't bind to source address";
614 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
615 case CO_ER_ADDR_INUSE: return "Local source address already in use";
616
617 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
618 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
619 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
620 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
621 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
622 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
623 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
624
625 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
626 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
627 case CO_ER_CIP_TIMEOUT: return "Timeout while waiting for a NetScaler Client IP header";
628 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
629 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
630 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
631
632 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
633 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
634 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
635 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
636 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
637 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
638 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
639 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
640 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
641 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
642 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
643 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
644 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
645 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
646 case CO_ER_SSL_EARLY_FAILED: return "Server refused early data";
647
648 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
649 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
650 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
651 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
652
653 case CO_ERR_SSL_FATAL: return "SSL fatal error";
654 }
655 return NULL;
656}
657
Willy Tarreauff3e6482015-03-12 23:56:52 +0100658/* Send a message over an established connection. It makes use of send() and
659 * returns the same return code and errno. If the socket layer is not ready yet
660 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
661 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
662 * EMSGSIZE if called with a zero length message. The purpose is to simplify
663 * some rare attempts to directly write on the socket from above the connection
664 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
665 * It automatically retries on EINTR. Other errors cause the connection to be
666 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +0100667 * first one which is the connection instead of the file descriptor. <flags>
668 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100669 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100670int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100671{
Willy Tarreau827fee72020-12-11 15:26:55 +0100672 const struct buffer buffer = b_make((char*)buf, len, 0, len);
673 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100674 int ret;
675
676 ret = -1;
677 errno = ENOTSOCK;
678
679 if (conn->flags & CO_FL_SOCK_WR_SH)
680 goto fail;
681
682 if (!conn_ctrl_ready(conn))
683 goto fail;
684
685 errno = EMSGSIZE;
686 if (!len)
687 goto fail;
688
Willy Tarreau827fee72020-12-11 15:26:55 +0100689 /* snd_buf() already takes care of updating conn->flags and handling
690 * the FD polling status.
691 */
692 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
693 if (conn->flags & CO_FL_ERROR)
694 ret = -1;
695 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100696 fail:
697 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
698 return ret;
699}
700
Willy Tarreau746b0512020-12-11 17:06:11 +0100701/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
702 * The event subscriber <es> is not allowed to change from a previous call as
703 * long as at least one event is still subscribed. The <event_type> must only
704 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100705 */
706int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200707{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100708 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100709 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100710
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100711 es->events &= ~event_type;
712 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100713 conn->subs = NULL;
714
Willy Tarreau746b0512020-12-11 17:06:11 +0100715 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
716 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100717
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200718 return 0;
719}
720
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100721/* Called from the upper layer, to subscribe <es> to events <event_type>.
722 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100723 * previous call to subscribe(). If the connection's ctrl layer is ready,
724 * the wait_event is immediately woken up and the subcription is cancelled.
725 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100726 */
727int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200728{
Willy Tarreau746b0512020-12-11 17:06:11 +0100729 int ret = 0;
730
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100731 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100732 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100733
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100734 if (conn->subs && (conn->subs->events & event_type) == event_type)
735 return 0;
736
Willy Tarreau746b0512020-12-11 17:06:11 +0100737 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
738 ret = conn->ctrl->check_events(conn, event_type);
739 if (ret)
740 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100741 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100742
743 es->events = (es->events | event_type) & ~ret;
744 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200745 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200746}
747
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100748/* Drains possibly pending incoming data on the connection and update the flags
749 * accordingly. This is used to know whether we need to disable lingering on
750 * close. Returns non-zero if it is safe to close without disabling lingering,
751 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
752 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100753 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100754int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100755{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100756 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200757
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100758 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
759 ret = 1;
760 else if (conn->ctrl->drain) {
761 ret = conn->ctrl->drain(conn);
762 if (ret)
763 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100764 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100765 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100766}
767
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100768/*
769 * Get data length from tlv
770 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100771static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100772{
773 return (src->length_hi << 8) | src->length_lo;
774}
775
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200776/* This handshake handler waits a PROXY protocol header at the beginning of the
777 * raw data stream. The header looks like this :
778 *
779 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
780 *
781 * There must be exactly one space between each field. Fields are :
782 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
783 * - SRC3 : layer 3 (eg: IP) source address in standard text form
784 * - DST3 : layer 3 (eg: IP) destination address in standard text form
785 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
786 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
787 *
788 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
789 *
790 * The header line is small and in all cases smaller than the smallest normal
791 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
792 * can safely use MSG_PEEK and avoid buffering.
793 *
794 * Once the data is fetched, the values are set in the connection's address
795 * fields, and data are removed from the socket's buffer. The function returns
796 * zero if it needs to wait for more data or if it fails, or 1 if it completed
797 * and removed itself.
798 */
799int conn_recv_proxy(struct connection *conn, int flag)
800{
Christopher Fauletc105c922021-10-25 08:17:11 +0200801 struct session *sess = conn->owner;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200802 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200803 struct proxy_hdr_v2 *hdr_v2;
804 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100805 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100806 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200807 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200808
Willy Tarreau3c728722014-01-23 13:50:42 +0100809 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200810 goto fail;
811
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200812 BUG_ON(conn->flags & CO_FL_FDLESS);
813
Willy Tarreau585744b2017-08-24 14:31:19 +0200814 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200815 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100816
Willy Tarreau157788c2020-02-11 10:08:05 +0100817 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200818 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
819 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200820 if (errno == EINTR)
821 continue;
822 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200823 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200824 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200825 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100826 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200827 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200828 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100829 break;
830 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200831
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100833 /* client shutdown */
834 conn->err_code = CO_ER_PRX_EMPTY;
835 goto fail;
836 }
837
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100838 conn->flags &= ~CO_FL_WAIT_L4_CONN;
839
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200840 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200841 goto missing;
842
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200843 line = trash.area;
844 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200845
846 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200847 if (strncmp(line, "PROXY ", 6) != 0)
848 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200849
850 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200851 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200852 goto missing;
853
David CARLIER42ff05e2016-03-24 09:22:36 +0000854 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200855 u32 src3, dst3, sport, dport;
856
857 line += 5;
858
859 src3 = inetaddr_host_lim_ret(line, end, &line);
860 if (line == end)
861 goto missing;
862 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100863 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200864
865 dst3 = inetaddr_host_lim_ret(line, end, &line);
866 if (line == end)
867 goto missing;
868 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100869 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200870
871 sport = read_uint((const char **)&line, end);
872 if (line == end)
873 goto missing;
874 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100875 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200876
877 dport = read_uint((const char **)&line, end);
878 if (line > end - 2)
879 goto missing;
880 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100881 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200882 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100883 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200884
Christopher Fauletc105c922021-10-25 08:17:11 +0200885 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
886 goto fail;
887
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200888 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200889 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
890 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = htonl(src3);
891 ((struct sockaddr_in *)sess->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200892
Christopher Fauletc105c922021-10-25 08:17:11 +0200893 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
894 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = htonl(dst3);
895 ((struct sockaddr_in *)sess->dst)->sin_port = htons(dport);
896 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200897 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000898 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200899 u32 sport, dport;
900 char *src_s;
901 char *dst_s, *sport_s, *dport_s;
902 struct in6_addr src3, dst3;
903
904 line += 5;
905
906 src_s = line;
907 dst_s = sport_s = dport_s = NULL;
908 while (1) {
909 if (line > end - 2) {
910 goto missing;
911 }
912 else if (*line == '\r') {
913 *line = 0;
914 line++;
915 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100916 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200917 break;
918 }
919
920 if (*line == ' ') {
921 *line = 0;
922 if (!dst_s)
923 dst_s = line + 1;
924 else if (!sport_s)
925 sport_s = line + 1;
926 else if (!dport_s)
927 dport_s = line + 1;
928 }
929 line++;
930 }
931
932 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100933 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200934
935 sport = read_uint((const char **)&sport_s,dport_s - 1);
936 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100937 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200938
939 dport = read_uint((const char **)&dport_s,line - 2);
940 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100941 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200942
943 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100944 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200945
946 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100947 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200948
Christopher Fauletc105c922021-10-25 08:17:11 +0200949 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
950 goto fail;
951
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200952 /* update the session's addresses and mark them set */
Christopher Fauletc105c922021-10-25 08:17:11 +0200953 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
954 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, &src3, sizeof(struct in6_addr));
955 ((struct sockaddr_in6 *)sess->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200956
Christopher Fauletc105c922021-10-25 08:17:11 +0200957 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
958 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
959 ((struct sockaddr_in6 *)sess->dst)->sin6_port = htons(dport);
960 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200961 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200962 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
963 /* This can be a UNIX socket forwarded by an haproxy upstream */
964 line += 9;
965 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200966 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200967 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100968 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200969 goto fail;
970 }
971
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200972 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200973 goto eat_header;
974
975 not_v1:
976 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200977 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200978 goto missing;
979
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200980 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200981
982 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
983 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
984 conn->err_code = CO_ER_PRX_NOT_HDR;
985 goto fail;
986 }
987
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100988 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
989 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200990 goto missing;
991
992 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
993 case 0x01: /* PROXY command */
994 switch (hdr_v2->fam) {
995 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100996 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
997 goto bad_header;
998
Christopher Fauletc105c922021-10-25 08:17:11 +0200999 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1000 goto fail;
1001
1002 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
1003 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
1004 ((struct sockaddr_in *)sess->src)->sin_port = hdr_v2->addr.ip4.src_port;
1005 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1006 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
1007 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
1008 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001009 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +02001010 break;
1011 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +01001012 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
1013 goto bad_header;
1014
Christopher Fauletc105c922021-10-25 08:17:11 +02001015 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1016 goto fail;
1017
1018 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1019 memcpy(&((struct sockaddr_in6 *)sess->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
1020 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_v2->addr.ip6.src_port;
1021 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1022 memcpy(&((struct sockaddr_in6 *)sess->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
1023 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
1024 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +02001025 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +02001026 break;
1027 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001028
1029 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001030 while (tlv_offset < total_v2_len) {
1031 struct tlv *tlv_packet;
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001032 struct ist tlv;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001033
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001034 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
1035 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
1036 goto bad_header;
1037
1038 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001039 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
1040 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001041
1042 /* Verify that the TLV length does not exceed the total PROXYv2 length */
1043 if (tlv_offset > total_v2_len)
1044 goto bad_header;
1045
1046 switch (tlv_packet->type) {
1047 case PP2_TYPE_CRC32C: {
1048 uint32_t n_crc32c;
1049
1050 /* Verify that this TLV is exactly 4 bytes long */
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001051 if (istlen(tlv) != 4)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001052 goto bad_header;
1053
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001054 n_crc32c = read_n32(istptr(tlv));
1055 write_n32(istptr(tlv), 0); // compute with CRC==0
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001056
1057 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
1058 goto bad_header;
1059 break;
1060 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001061#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001062 case PP2_TYPE_NETNS: {
1063 const struct netns_entry *ns;
1064
Tim Duesterhus56c176a2021-03-06 20:06:51 +01001065 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001066 if (ns)
1067 conn->proxy_netns = ns;
1068 break;
1069 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001070#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001071 case PP2_TYPE_AUTHORITY: {
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001072 if (istlen(tlv) > PP2_AUTHORITY_MAX)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001073 goto bad_header;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001074 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
1075 if (!isttest(conn->proxy_authority))
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001076 goto fail;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001077 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
Tim Duesterhusf09af572022-02-25 21:44:26 +01001078 /* This is impossible, because we verified that the TLV value fits. */
1079 my_unreachable();
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001080 goto fail;
1081 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001082 break;
1083 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001084 case PP2_TYPE_UNIQUE_ID: {
Tim Duesterhus002bd772021-03-06 20:06:49 +01001085 if (istlen(tlv) > UNIQUEID_LEN)
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001086 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +01001087 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001088 if (!isttest(conn->proxy_unique_id))
1089 goto fail;
1090 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
Tim Duesterhusf09af572022-02-25 21:44:26 +01001091 /* This is impossible, because we verified that the TLV value fits. */
1092 my_unreachable();
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001093 goto fail;
1094 }
1095 break;
1096 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001097 default:
1098 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001099 }
1100 }
1101
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001102 /* Verify that the PROXYv2 header ends at a TLV boundary.
Tim Duesterhus17e6b732022-02-25 21:44:27 +01001103 * This is can not be true, because the TLV parsing already
1104 * verifies that a TLV does not exceed the total length and
1105 * also that there is space for a TLV header.
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001106 */
Tim Duesterhus17e6b732022-02-25 21:44:27 +01001107 BUG_ON(tlv_offset != total_v2_len);
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001108
Willy Tarreau77992672014-06-14 11:06:17 +02001109 /* unsupported protocol, keep local connection address */
1110 break;
1111 case 0x00: /* LOCAL command */
1112 /* keep local connection address for LOCAL */
1113 break;
1114 default:
1115 goto bad_header; /* not a supported command */
1116 }
1117
Tim Duesterhus488ee7f2020-03-05 22:55:20 +01001118 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +02001119 goto eat_header;
1120
1121 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001122 /* remove the PROXY line from the request. For this we re-read the
1123 * exact line at once. If we don't get the exact same result, we
1124 * fail.
1125 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001126 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001127 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
1128
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001129 if (len2 < 0 && errno == EINTR)
1130 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001131 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001132 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001133 break;
1134 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001135
1136 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +01001137 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001138 return 1;
1139
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001140 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001141 return 0;
1142
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001143 missing:
1144 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1145 * we have not read anything. Otherwise we need to fail because we won't
1146 * be able to poll anymore.
1147 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001148 conn->err_code = CO_ER_PRX_TRUNCATED;
1149 goto fail;
1150
1151 bad_header:
1152 /* This is not a valid proxy protocol header */
1153 conn->err_code = CO_ER_PRX_BAD_HDR;
1154 goto fail;
1155
1156 recv_abort:
1157 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +01001158 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +01001159 goto fail;
1160
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001161 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001162 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001163 return 0;
1164}
1165
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001166/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001167 * at the beginning of the raw data stream. The header format is
1168 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001169 *
1170 * This line MUST be at the beginning of the buffer and MUST NOT be
1171 * fragmented.
1172 *
1173 * The header line is small and in all cases smaller than the smallest normal
1174 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
1175 * can safely use MSG_PEEK and avoid buffering.
1176 *
1177 * Once the data is fetched, the values are set in the connection's address
1178 * fields, and data are removed from the socket's buffer. The function returns
1179 * zero if it needs to wait for more data or if it fails, or 1 if it completed
1180 * and removed itself.
1181 */
1182int conn_recv_netscaler_cip(struct connection *conn, int flag)
1183{
Christopher Faulete83e8822021-10-25 08:23:22 +02001184 struct session *sess = conn->owner;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001185 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001186 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001187 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +02001188 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001189
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001190 if (!conn_ctrl_ready(conn))
1191 goto fail;
1192
Willy Tarreau07ecfc52022-04-11 18:07:03 +02001193 BUG_ON(conn->flags & CO_FL_FDLESS);
1194
Willy Tarreau585744b2017-08-24 14:31:19 +02001195 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001196 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001197
Willy Tarreau157788c2020-02-11 10:08:05 +01001198 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +02001199 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
1200 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001201 if (errno == EINTR)
1202 continue;
1203 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +02001204 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001205 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001206 }
1207 goto recv_abort;
1208 }
Willy Tarreaub406b872018-08-22 05:20:32 +02001209 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +01001210 break;
1211 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001212
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001213 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1214
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001215 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001216 /* client shutdown */
1217 conn->err_code = CO_ER_CIP_EMPTY;
1218 goto fail;
1219 }
1220
1221 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001222 * CIP magic, header length or
1223 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001224 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001225 goto missing;
1226
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001227 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001228
1229 /* Decode a possible NetScaler Client IP request, fail early if
1230 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001231 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001232 goto bad_magic;
1233
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001234 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001235 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001236 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001237 line += 8;
1238 }
1239 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001240 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +01001241 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001242 line += 12;
1243 }
1244 /* Unknown CIP protocol */
1245 else {
1246 conn->err_code = CO_ER_CIP_BAD_PROTO;
1247 goto fail;
1248 }
1249
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001250 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +00001251 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001252 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001253 goto missing;
1254
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001255 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001256 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001257
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001258 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001259 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +01001260 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001261
1262 hdr_ip4 = (struct ip *)line;
1263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001264 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001265 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001266 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001267 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001268 }
1269 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001270 /* The protocol does not include a TCP header */
1271 conn->err_code = CO_ER_CIP_BAD_PROTO;
1272 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001273 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001274
David Carlier3015a2e2016-07-04 22:51:33 +01001275 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001276
Christopher Faulete83e8822021-10-25 08:23:22 +02001277 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1278 goto fail;
1279
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001280 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001281 ((struct sockaddr_in *)sess->src)->sin_family = AF_INET;
1282 ((struct sockaddr_in *)sess->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
1283 ((struct sockaddr_in *)sess->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001284
Christopher Faulete83e8822021-10-25 08:23:22 +02001285 ((struct sockaddr_in *)sess->dst)->sin_family = AF_INET;
1286 ((struct sockaddr_in *)sess->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
1287 ((struct sockaddr_in *)sess->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001288
Christopher Faulete83e8822021-10-25 08:23:22 +02001289 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001290 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +01001291 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001292 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +01001293 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001294
1295 hdr_ip6 = (struct ip6_hdr *)line;
1296
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001297 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001298 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +00001299 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001300 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001301 }
1302 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001303 /* The protocol does not include a TCP header */
1304 conn->err_code = CO_ER_CIP_BAD_PROTO;
1305 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +00001306 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001307
David Carlier3015a2e2016-07-04 22:51:33 +01001308 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001309
Christopher Faulete83e8822021-10-25 08:23:22 +02001310 if (!sess || !sockaddr_alloc(&sess->src, NULL, 0) || !sockaddr_alloc(&sess->dst, NULL, 0))
1311 goto fail;
1312
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001313 /* update the session's addresses and mark them set */
Christopher Faulete83e8822021-10-25 08:23:22 +02001314 ((struct sockaddr_in6 *)sess->src)->sin6_family = AF_INET6;
1315 ((struct sockaddr_in6 *)sess->src)->sin6_addr = hdr_ip6->ip6_src;
1316 ((struct sockaddr_in6 *)sess->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001317
Christopher Faulete83e8822021-10-25 08:23:22 +02001318 ((struct sockaddr_in6 *)sess->dst)->sin6_family = AF_INET6;
1319 ((struct sockaddr_in6 *)sess->dst)->sin6_addr = hdr_ip6->ip6_dst;
1320 ((struct sockaddr_in6 *)sess->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001321
Christopher Faulete83e8822021-10-25 08:23:22 +02001322 sess->flags |= SESS_FL_ADDR_FROM_SET | SESS_FL_ADDR_TO_SET;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001323 }
1324 else {
1325 /* The protocol does not match something known (IPv4/IPv6) */
1326 conn->err_code = CO_ER_CIP_BAD_PROTO;
1327 goto fail;
1328 }
1329
Bertrand Jacquin7d668f92017-12-13 01:23:39 +00001330 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001332
1333 /* remove the NetScaler Client IP header from the request. For this
1334 * we re-read the exact line at once. If we don't get the exact same
1335 * result, we fail.
1336 */
Willy Tarreau157788c2020-02-11 10:08:05 +01001337 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001338 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001339 if (len2 < 0 && errno == EINTR)
1340 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001341 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001342 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +01001343 break;
1344 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001345
1346 conn->flags &= ~flag;
1347 return 1;
1348
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001349 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001350 return 0;
1351
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001352 missing:
1353 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1354 * we have not read anything. Otherwise we need to fail because we won't
1355 * be able to poll anymore.
1356 */
1357 conn->err_code = CO_ER_CIP_TRUNCATED;
1358 goto fail;
1359
1360 bad_magic:
1361 conn->err_code = CO_ER_CIP_BAD_MAGIC;
1362 goto fail;
1363
1364 recv_abort:
1365 conn->err_code = CO_ER_CIP_ABORT;
1366 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
1367 goto fail;
1368
1369 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +01001370 conn->flags |= CO_FL_ERROR;
1371 return 0;
1372}
1373
Alexander Liu2a54bb72019-05-22 19:44:48 +08001374
1375int conn_send_socks4_proxy_request(struct connection *conn)
1376{
1377 struct socks4_request req_line;
1378
Alexander Liu2a54bb72019-05-22 19:44:48 +08001379 if (!conn_ctrl_ready(conn))
1380 goto out_error;
1381
Willy Tarreau226572f2019-07-17 14:46:00 +02001382 if (!conn_get_dst(conn))
1383 goto out_error;
1384
Alexander Liu2a54bb72019-05-22 19:44:48 +08001385 req_line.version = 0x04;
1386 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +02001387 req_line.port = get_net_port(conn->dst);
1388 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001389 memcpy(req_line.user_id, "HAProxy\0", 8);
1390
1391 if (conn->send_proxy_ofs > 0) {
1392 /*
1393 * This is the first call to send the request
1394 */
1395 conn->send_proxy_ofs = -(int)sizeof(req_line);
1396 }
1397
1398 if (conn->send_proxy_ofs < 0) {
1399 int ret = 0;
1400
1401 /* we are sending the socks4_req_line here. If the data layer
1402 * has a pending write, we'll also set MSG_MORE.
1403 */
Willy Tarreau827fee72020-12-11 15:26:55 +01001404 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +08001405 conn,
1406 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
1407 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +01001408 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001409
1410 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
Willy Tarreau0e9c2642022-04-11 18:01:28 +02001411 conn_fd(conn), -conn->send_proxy_ofs, ret);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001412
1413 if (ret < 0) {
1414 goto out_error;
1415 }
1416
1417 conn->send_proxy_ofs += ret; /* becomes zero once complete */
1418 if (conn->send_proxy_ofs != 0) {
1419 goto out_wait;
1420 }
1421 }
1422
1423 /* OK we've the whole request sent */
1424 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001425
1426 /* The connection is ready now, simply return and let the connection
1427 * handler notify upper layers if needed.
1428 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001429 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001430
1431 if (conn->flags & CO_FL_SEND_PROXY) {
1432 /*
1433 * Get the send_proxy_ofs ready for the send_proxy due to we are
1434 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
1435 * before sending PROXY Protocol.
1436 */
1437 conn->send_proxy_ofs = 1;
1438 }
1439 return 1;
1440
1441 out_error:
1442 /* Write error on the file descriptor */
1443 conn->flags |= CO_FL_ERROR;
1444 if (conn->err_code == CO_ER_NONE) {
1445 conn->err_code = CO_ER_SOCKS4_SEND;
1446 }
1447 return 0;
1448
1449 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001450 return 0;
1451}
1452
1453int conn_recv_socks4_proxy_response(struct connection *conn)
1454{
1455 char line[SOCKS4_HS_RSP_LEN];
1456 int ret;
1457
Alexander Liu2a54bb72019-05-22 19:44:48 +08001458 if (!conn_ctrl_ready(conn))
1459 goto fail;
1460
Willy Tarreau07ecfc52022-04-11 18:07:03 +02001461 BUG_ON(conn->flags & CO_FL_FDLESS);
1462
Alexander Liu2a54bb72019-05-22 19:44:48 +08001463 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001464 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001465
Willy Tarreau157788c2020-02-11 10:08:05 +01001466 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001467 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
1468 * Try to peek into it, before all 8 bytes ready.
1469 */
1470 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
1471
1472 if (ret == 0) {
1473 /* the socket has been closed or shutdown for send */
1474 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
1475 conn->handle.fd, ret, errno);
1476 if (conn->err_code == CO_ER_NONE) {
1477 conn->err_code = CO_ER_SOCKS4_RECV;
1478 }
1479 goto fail;
1480 }
1481
1482 if (ret > 0) {
1483 if (ret == SOCKS4_HS_RSP_LEN) {
1484 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1485 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1486 }else{
1487 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]);
1488 }
1489 } else {
1490 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
1491 }
1492
1493 if (ret < 0) {
1494 if (errno == EINTR) {
1495 continue;
1496 }
1497 if (errno == EAGAIN) {
1498 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001499 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001500 }
1501 goto recv_abort;
1502 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001503 break;
1504 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001505
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001506 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1507
Alexander Liu2a54bb72019-05-22 19:44:48 +08001508 if (ret < SOCKS4_HS_RSP_LEN) {
1509 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
1510 * we are not able to read enough data.
1511 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001512 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001513 }
1514
1515 /*
1516 * Base on the SOCSK4 protocol:
1517 *
1518 * +----+----+----+----+----+----+----+----+
1519 * | VN | CD | DSTPORT | DSTIP |
1520 * +----+----+----+----+----+----+----+----+
1521 * # of bytes: 1 1 2 4
1522 * VN is the version of the reply code and should be 0. CD is the result
1523 * code with one of the following values:
1524 * 90: request granted
1525 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001526 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +08001527 * 93: request rejected because the client program and identd report different user-ids
1528 * The remaining fields are ignored.
1529 */
1530 if (line[1] != 90) {
1531 conn->flags &= ~CO_FL_SOCKS4_RECV;
1532
1533 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
1534 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
1535 if (conn->err_code == CO_ER_NONE) {
1536 conn->err_code = CO_ER_SOCKS4_DENY;
1537 }
1538 goto fail;
1539 }
1540
1541 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +01001542 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001543 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
1544 if (ret < 0 && errno == EINTR) {
1545 continue;
1546 }
1547 if (ret != SOCKS4_HS_RSP_LEN) {
1548 if (conn->err_code == CO_ER_NONE) {
1549 conn->err_code = CO_ER_SOCKS4_RECV;
1550 }
1551 goto fail;
1552 }
Willy Tarreau157788c2020-02-11 10:08:05 +01001553 break;
1554 }
Alexander Liu2a54bb72019-05-22 19:44:48 +08001555
1556 conn->flags &= ~CO_FL_SOCKS4_RECV;
1557 return 1;
1558
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001559 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +02001560 return 0;
1561
Alexander Liu2a54bb72019-05-22 19:44:48 +08001562 recv_abort:
1563 if (conn->err_code == CO_ER_NONE) {
1564 conn->err_code = CO_ER_SOCKS4_ABORT;
1565 }
1566 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
1567 goto fail;
1568
1569 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +08001570 conn->flags |= CO_FL_ERROR;
1571 return 0;
1572}
1573
Willy Tarreaud318e4e2022-03-02 14:46:45 +01001574/* registers proto mux list <list>. Modifies the list element! */
1575void register_mux_proto(struct mux_proto_list *list)
1576{
1577 LIST_APPEND(&mux_proto_list.list, &list->list);
1578}
1579
Willy Tarreauadd43062022-02-18 11:07:40 +01001580/* Lists the known proto mux on <out>. This function is used by "haproxy -vv"
1581 * and is suitable for early boot just after the "REGISTER" stage because it
1582 * doesn't depend on anything to be already allocated.
1583 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001584void list_mux_proto(FILE *out)
1585{
1586 struct mux_proto_list *item;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001587 struct ist proto;
1588 char *mode, *side;
Willy Tarreauadd43062022-02-18 11:07:40 +01001589 int done;
Willy Tarreaue59b5162021-05-08 14:06:09 +02001590
1591 fprintf(out, "Available multiplexer protocols :\n"
1592 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
1593 list_for_each_entry(item, &mux_proto_list.list, list) {
1594 proto = item->token;
1595
1596 if (item->mode == PROTO_MODE_ANY)
1597 mode = "TCP|HTTP";
1598 else if (item->mode == PROTO_MODE_TCP)
1599 mode = "TCP";
1600 else if (item->mode == PROTO_MODE_HTTP)
1601 mode = "HTTP";
1602 else
1603 mode = "NONE";
1604
1605 if (item->side == PROTO_SIDE_BOTH)
1606 side = "FE|BE";
1607 else if (item->side == PROTO_SIDE_FE)
1608 side = "FE";
1609 else if (item->side == PROTO_SIDE_BE)
1610 side = "BE";
1611 else
1612 side = "NONE";
1613
Willy Tarreaudddfec52022-04-09 11:37:35 +02001614 fprintf(out, " %10s : mode=%-5s side=%-6s mux=%-5s flags=",
Willy Tarreauadd43062022-02-18 11:07:40 +01001615 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name);
1616
1617 done = 0;
1618
1619 /* note: the block below could be simplied using macros but for only
1620 * 4 flags it's not worth it.
1621 */
Willy Tarreaue59b5162021-05-08 14:06:09 +02001622 if (item->mux->flags & MX_FL_HTX)
Willy Tarreauadd43062022-02-18 11:07:40 +01001623 done |= fprintf(out, "%sHTX", done ? "|" : "");
1624
Willy Tarreaue59b5162021-05-08 14:06:09 +02001625 if (item->mux->flags & MX_FL_CLEAN_ABRT)
Willy Tarreauadd43062022-02-18 11:07:40 +01001626 done |= fprintf(out, "%sCLEAN_ABRT", done ? "|" : "");
1627
Willy Tarreaue59b5162021-05-08 14:06:09 +02001628 if (item->mux->flags & MX_FL_HOL_RISK)
Willy Tarreauadd43062022-02-18 11:07:40 +01001629 done |= fprintf(out, "%sHOL_RISK", done ? "|" : "");
1630
Willy Tarreaue59b5162021-05-08 14:06:09 +02001631 if (item->mux->flags & MX_FL_NO_UPG)
Willy Tarreauadd43062022-02-18 11:07:40 +01001632 done |= fprintf(out, "%sNO_UPG", done ? "|" : "");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001633
Willy Tarreauadd43062022-02-18 11:07:40 +01001634 fprintf(out, "\n");
Willy Tarreaue59b5162021-05-08 14:06:09 +02001635 }
1636}
1637
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001638/* Makes a PROXY protocol line from the two addresses. The output is sent to
1639 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1640 * It returns the number of bytes composing this line (including the trailing
1641 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001642 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1643 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001644 */
Christopher Fauletb097aef2021-10-25 08:05:28 +02001645static 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 +02001646{
1647 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001648 char * protocol;
1649 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1650 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1651 in_port_t src_port;
1652 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001653
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001654 if ( !src
1655 || !dst
1656 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1657 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1658 /* unknown family combination */
1659 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001660 if (ret >= buf_len)
1661 return 0;
1662
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001663 return ret;
1664 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001665
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001666 /* IPv4 for both src and dst */
1667 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1668 protocol = "TCP4";
1669 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001670 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001671 src_port = ((struct sockaddr_in *)src)->sin_port;
1672 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001673 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001674 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001675 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001676 /* IPv6 for at least one of src and dst */
1677 else {
1678 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001679
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001680 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001681
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001682 if (src->ss_family == AF_INET) {
1683 /* Convert src to IPv6 */
1684 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1685 src_port = ((struct sockaddr_in *)src)->sin_port;
1686 }
1687 else {
1688 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1689 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1690 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001691
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001692 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001693 return 0;
1694
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001695 if (dst->ss_family == AF_INET) {
1696 /* Convert dst to IPv6 */
1697 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1698 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1699 }
1700 else {
1701 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1702 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1703 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001704
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001705 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001706 return 0;
1707 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001708
1709 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1710 if (ret >= buf_len)
1711 return 0;
1712
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001713 return ret;
1714}
David Safb76832014-05-08 23:42:08 -04001715
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001716static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001717{
1718 struct tlv *tlv;
1719
1720 if (!dest || (length + sizeof(*tlv) > dest_len))
1721 return 0;
1722
1723 tlv = (struct tlv *)dest;
1724
1725 tlv->type = type;
1726 tlv->length_hi = length >> 8;
1727 tlv->length_lo = length & 0x00ff;
1728 memcpy(tlv->value, value, length);
1729 return length + sizeof(*tlv);
1730}
David Safb76832014-05-08 23:42:08 -04001731
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001732/* Note: <remote> is explicitly allowed to be NULL */
Christopher Faulet4bfce392021-10-22 14:33:59 +02001733static 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 -04001734{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001735 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001736 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001737 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001738 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001739 struct sockaddr_storage null_addr = { .ss_family = 0 };
Christopher Fauletb097aef2021-10-25 08:05:28 +02001740 const struct sockaddr_storage *src = &null_addr;
1741 const struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001742 const char *value;
1743 int value_len;
David Safb76832014-05-08 23:42:08 -04001744
1745 if (buf_len < PP2_HEADER_LEN)
1746 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001747 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001748
Christopher Fauletb097aef2021-10-25 08:05:28 +02001749 if (strm) {
Christopher Fauletcda94ac2021-12-23 17:28:17 +01001750 src = si_src(strm->csf->si);
1751 dst = si_dst(strm->csf->si);
Christopher Fauletb097aef2021-10-25 08:05:28 +02001752 }
1753 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1754 src = conn_src(remote);
1755 dst = conn_dst(remote);
David Safb76832014-05-08 23:42:08 -04001756 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001757
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001758 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1759 if ( !src
1760 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001761 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001762 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1763 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001764 if (buf_len < PP2_HDR_LEN_UNSPEC)
1765 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001766 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1767 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001768 ret = PP2_HDR_LEN_UNSPEC;
1769 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001770 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001771 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001772 /* IPv4 for both src and dst */
1773 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1774 if (buf_len < PP2_HDR_LEN_INET)
1775 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001776 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1777 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1778 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1779 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1780 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1781 ret = PP2_HDR_LEN_INET;
1782 }
1783 /* IPv6 for at least one of src and dst */
1784 else {
1785 struct in6_addr tmp;
1786
1787 if (buf_len < PP2_HDR_LEN_INET6)
1788 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001789 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1790 if (src->ss_family == AF_INET) {
1791 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1792 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1793 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1794 }
1795 else {
1796 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1797 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1798 }
1799 if (dst->ss_family == AF_INET) {
1800 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1801 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001802 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001803 }
1804 else {
1805 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1806 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1807 }
1808
1809 ret = PP2_HDR_LEN_INET6;
1810 }
1811 }
David Safb76832014-05-08 23:42:08 -04001812
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001813 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1814 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001815
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001816 if ((buf_len - ret) < sizeof(struct tlv))
1817 return 0;
1818 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1819 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1820 }
1821
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001822 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001823 if ((buf_len - ret) < sizeof(struct tlv))
1824 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001825 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001826 }
1827
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001828 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001829 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001830 if (remote && isttest(remote->proxy_authority)) {
1831 value = istptr(remote->proxy_authority);
1832 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001833 }
1834#ifdef USE_OPENSSL
1835 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001836 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001837 value_len = strlen(value);
1838 }
1839#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001840 if (value) {
1841 if ((buf_len - ret) < sizeof(struct tlv))
1842 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001843 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001844 }
1845 }
1846
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001847 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001848 struct session* sess = strm_sess(strm);
1849 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1850
1851 value = unique_id.ptr;
1852 value_len = unique_id.len;
1853
1854 if (value_len >= 0) {
1855 if ((buf_len - ret) < sizeof(struct tlv))
1856 return 0;
1857 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1858 }
1859 }
1860
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001861#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001862 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001863 struct tlv_ssl *tlv;
1864 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001865
David Safb76832014-05-08 23:42:08 -04001866 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1867 return 0;
1868 tlv = (struct tlv_ssl *)&buf[ret];
1869 memset(tlv, 0, sizeof(struct tlv_ssl));
1870 ssl_tlv_len += sizeof(struct tlv_ssl);
1871 tlv->tlv.type = PP2_TYPE_SSL;
Willy Tarreau1057bee2021-10-06 11:38:44 +02001872 if (conn_is_ssl(remote)) {
David Safb76832014-05-08 23:42:08 -04001873 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001874 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001875 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001876 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 -04001877 }
Dave McCowan328fb582014-07-30 10:39:13 -04001878 if (ssl_sock_get_cert_used_sess(remote)) {
1879 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001880 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001881 if (ssl_sock_get_cert_used_conn(remote))
1882 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001883 }
1884 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001885 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001886 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001887 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1888 cn_trash->data,
1889 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001890 }
1891 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001892 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001893 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001894 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001895 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1896 pkey_trash->data,
1897 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001898 }
1899 }
1900 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1901 value = ssl_sock_get_cert_sig(remote);
1902 if (value) {
1903 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1904 }
1905 }
1906 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1907 value = ssl_sock_get_cipher_name(remote);
1908 if (value) {
1909 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1910 }
1911 }
David Safb76832014-05-08 23:42:08 -04001912 }
1913 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1914 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1915 ret += ssl_tlv_len;
1916 }
1917#endif
1918
Willy Tarreaue5733232019-05-22 19:24:06 +02001919#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001920 if (remote && (remote->proxy_netns)) {
1921 if ((buf_len - ret) < sizeof(struct tlv))
1922 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001923 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 +01001924 }
1925#endif
1926
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001927 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001928
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001929 if (tlv_crc32c_p) {
1930 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1931 }
1932
David Safb76832014-05-08 23:42:08 -04001933 return ret;
1934}
Emeric Brun4f603012017-01-05 15:11:44 +01001935
Christopher Faulet4bfce392021-10-22 14:33:59 +02001936/* Note: <remote> is explicitly allowed to be NULL */
1937int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
1938{
1939 int ret = 0;
1940
1941 if (srv && (srv->pp_opts & SRV_PP_V2)) {
1942 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
1943 }
1944 else {
Christopher Fauletb097aef2021-10-25 08:05:28 +02001945 const struct sockaddr_storage *src = NULL;
1946 const struct sockaddr_storage *dst = NULL;
1947
1948 if (strm) {
Christopher Fauletcda94ac2021-12-23 17:28:17 +01001949 src = si_src(strm->csf->si);
1950 dst = si_dst(strm->csf->si);
Christopher Fauletb097aef2021-10-25 08:05:28 +02001951 }
1952 else if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1953 src = conn_src(remote);
1954 dst = conn_dst(remote);
1955 }
1956
1957 if (src && dst)
1958 ret = make_proxy_line_v1(buf, buf_len, src, dst);
Christopher Faulet4bfce392021-10-22 14:33:59 +02001959 else
1960 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1961 }
1962
1963 return ret;
1964}
1965
Willy Tarreau119e50e2020-05-22 13:53:29 +02001966/* returns 0 on success */
1967static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001968 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02001969 char **err)
1970{
1971 if (too_many_args(0, args, err, NULL))
1972 return -1;
1973 pp2_never_send_local = 1;
1974 return 0;
1975}
1976
Willy Tarreaud943a042021-06-16 17:35:20 +02001977/* extracts some info from the connection and appends them to buffer <buf>. The
1978 * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source
1979 * when set, destination when set, are printed in a compact human-readable format
1980 * fitting on a single line. This is handy to complete traces or debug output.
1981 * It is permitted to pass a NULL conn pointer. The number of characters emitted
1982 * is returned. A prefix <pfx> might be prepended before the first field if not
1983 * NULL.
1984 */
1985int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx)
1986{
1987 const struct listener *li;
1988 const struct server *sv;
1989 const struct proxy *px;
1990 char addr[40];
1991 int old_len = buf->data;
1992
1993 if (!conn)
1994 return 0;
1995
1996 chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN");
1997
1998 if ((li = objt_listener(conn->target)))
1999 chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id);
2000 else if ((sv = objt_server(conn->target)))
2001 chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id);
2002 else if ((px = objt_proxy(conn->target)))
2003 chunk_appendf(buf, " be=%s", px->id);
2004
2005 chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn));
2006
2007 if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr)))
2008 chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src));
2009
2010 if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr)))
2011 chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst));
2012
2013 return buf->data - old_len;
2014}
2015
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002016/* return the major HTTP version as 1 or 2 depending on how the request arrived
2017 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002018 *
2019 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002020 */
2021static int
2022smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
2023{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02002024 struct connection *conn = NULL;
2025
2026 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2027 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2028 else
2029 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Christopher Faulet95a61e82021-12-22 14:22:03 +01002030 smp->strm ? cs_conn(smp->strm->csb) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002031
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002032 /* No connection or a connection with a RAW muxx */
2033 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
2034 return 0;
2035
2036 /* No mux install, this may change */
2037 if (!conn->mux) {
2038 smp->flags |= SMP_F_MAY_CHANGE;
2039 return 0;
2040 }
2041
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002042 smp->data.type = SMP_T_SINT;
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02002043 smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02002044 return 1;
2045}
2046
Emeric Brun4f603012017-01-05 15:11:44 +01002047/* fetch if the received connection used a PROXY protocol header */
2048int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
2049{
2050 struct connection *conn;
2051
2052 conn = objt_conn(smp->sess->origin);
2053 if (!conn)
2054 return 0;
2055
Willy Tarreau911db9b2020-01-23 16:27:54 +01002056 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01002057 smp->flags |= SMP_F_MAY_CHANGE;
2058 return 0;
2059 }
2060
2061 smp->flags = 0;
2062 smp->data.type = SMP_T_BOOL;
2063 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
2064
2065 return 1;
2066}
2067
Geoff Simmons7185b782019-08-27 18:31:16 +02002068/* fetch the authority TLV from a PROXY protocol header */
2069int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
2070{
2071 struct connection *conn;
2072
2073 conn = objt_conn(smp->sess->origin);
2074 if (!conn)
2075 return 0;
2076
Willy Tarreau911db9b2020-01-23 16:27:54 +01002077 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02002078 smp->flags |= SMP_F_MAY_CHANGE;
2079 return 0;
2080 }
2081
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002082 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02002083 return 0;
2084
2085 smp->flags = 0;
2086 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01002087 smp->data.u.str.area = istptr(conn->proxy_authority);
2088 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02002089
2090 return 1;
2091}
2092
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002093/* fetch the unique ID TLV from a PROXY protocol header */
2094int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2095{
2096 struct connection *conn;
2097
2098 conn = objt_conn(smp->sess->origin);
2099 if (!conn)
2100 return 0;
2101
2102 if (conn->flags & CO_FL_WAIT_XPRT) {
2103 smp->flags |= SMP_F_MAY_CHANGE;
2104 return 0;
2105 }
2106
2107 if (!isttest(conn->proxy_unique_id))
2108 return 0;
2109
2110 smp->flags = 0;
2111 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01002112 smp->data.u.str.area = istptr(conn->proxy_unique_id);
2113 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002114
2115 return 1;
2116}
2117
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002118/* fetch the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002119int 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 +02002120{
2121 struct connection *conn;
2122
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002123 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2124 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2125 else
2126 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Christopher Faulet95a61e82021-12-22 14:22:03 +01002127 smp->strm ? cs_conn(smp->strm->csb) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002128
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002129 if (!conn)
2130 return 0;
2131
2132 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2133 smp->flags |= SMP_F_MAY_CHANGE;
2134 return 0;
2135 }
2136
2137 smp->flags = 0;
2138 smp->data.type = SMP_T_SINT;
2139 smp->data.u.sint = (unsigned long long int)conn->err_code;
2140
2141 return 1;
2142}
2143
2144/* fetch a string representation of the error code of a connection */
Willy Tarreau6f749762021-11-05 17:07:03 +01002145int 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 +02002146{
2147 struct connection *conn;
2148 const char *err_code_str;
2149
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002150 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
2151 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
2152 else
2153 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
Christopher Faulet95a61e82021-12-22 14:22:03 +01002154 smp->strm ? cs_conn(smp->strm->csb) : NULL;
Remi Tricot-Le Breton942c1672021-09-01 15:52:15 +02002155
Remi Tricot-Le Breton3d2093a2021-07-29 09:45:49 +02002156 if (!conn)
2157 return 0;
2158
2159 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
2160 smp->flags |= SMP_F_MAY_CHANGE;
2161 return 0;
2162 }
2163
2164 err_code_str = conn_err_code_str(conn);
2165
2166 if (!err_code_str)
2167 return 0;
2168
2169 smp->flags = 0;
2170 smp->data.type = SMP_T_STR;
2171 smp->data.u.str.area = (char*)err_code_str;
2172 smp->data.u.str.data = strlen(err_code_str);
2173
2174 return 1;
2175}
2176
Emeric Brun4f603012017-01-05 15:11:44 +01002177/* Note: must not be declared <const> as its list will be overwritten.
2178 * Note: fetches that may return multiple types must be declared as the lowest
2179 * common denominator, the type that can be casted into all other ones. For
2180 * instance v4/v6 must be declared v4.
2181 */
2182static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau6f749762021-11-05 17:07:03 +01002183 { "bc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
2184 { "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 +01002185 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Willy Tarreau6f749762021-11-05 17:07:03 +01002186 { "fc_err", smp_fetch_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
2187 { "fc_err_str", smp_fetch_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
2188 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01002189 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02002190 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01002191 { "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 +01002192 { /* END */ },
2193}};
2194
Willy Tarreau0108d902018-11-25 19:14:37 +01002195INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02002196
2197static struct cfg_kw_list cfg_kws = {ILH, {
2198 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
2199 { /* END */ },
2200}};
2201
2202INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002203
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002204/* private function to handle sockaddr as input for connection hash */
2205static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
2206 char *buf, size_t *idx,
2207 enum conn_hash_params_t *hash_flags,
2208 enum conn_hash_params_t param_type_addr,
2209 enum conn_hash_params_t param_type_port)
2210{
2211 struct sockaddr_in *addr;
2212 struct sockaddr_in6 *addr6;
2213
2214 switch (ss->ss_family) {
2215 case AF_INET:
2216 addr = (struct sockaddr_in *)ss;
2217
2218 conn_hash_update(buf, idx,
2219 &addr->sin_addr, sizeof(addr->sin_addr),
2220 hash_flags, param_type_addr);
2221
2222 if (addr->sin_port) {
2223 conn_hash_update(buf, idx,
2224 &addr->sin_port, sizeof(addr->sin_port),
2225 hash_flags, param_type_port);
2226 }
2227
2228 break;
2229
2230 case AF_INET6:
2231 addr6 = (struct sockaddr_in6 *)ss;
2232
2233 conn_hash_update(buf, idx,
2234 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
2235 hash_flags, param_type_addr);
2236
2237 if (addr6->sin6_port) {
2238 conn_hash_update(buf, idx,
2239 &addr6->sin6_port, sizeof(addr6->sin6_port),
2240 hash_flags, param_type_port);
2241 }
2242
2243 break;
2244 }
2245}
2246
Willy Tarreaue5983ff2021-10-06 17:14:49 +02002247/* Generate the hash of a connection with params as input
2248 * Each non-null field of params is taken into account for the hash calcul.
2249 */
2250uint64_t conn_hash_prehash(char *buf, size_t size)
2251{
2252 return XXH64(buf, size, 0);
2253}
2254
2255/* Append <data> into <buf> at <idx> offset in preparation for connection hash
2256 * calcul. <idx> is incremented beyond data <size>. In the same time, <flags>
2257 * are updated with <type> for the hash header.
2258 */
2259void conn_hash_update(char *buf, size_t *idx,
2260 const void *data, size_t size,
2261 enum conn_hash_params_t *flags,
2262 enum conn_hash_params_t type)
2263{
2264 memcpy(&buf[*idx], data, size);
2265 *idx += size;
2266 *flags |= type;
2267}
2268
2269uint64_t conn_hash_digest(char *buf, size_t bufsize,
2270 enum conn_hash_params_t flags)
2271{
2272 const uint64_t flags_u64 = (uint64_t)flags;
2273 const uint64_t hash = XXH64(buf, bufsize, 0);
2274
2275 return (flags_u64 << CONN_HASH_PAYLOAD_LEN) | CONN_HASH_GET_PAYLOAD(hash);
2276}
2277
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002278uint64_t conn_calculate_hash(const struct conn_hash_params *params)
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002279{
2280 char *buf;
2281 size_t idx = 0;
Willy Tarreaufd21c6c2021-10-06 17:09:41 +02002282 uint64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002283 enum conn_hash_params_t hash_flags = 0;
2284
2285 buf = trash.area;
2286
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01002287 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01002288
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002289 if (params->sni_prehash) {
2290 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002291 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01002292 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
2293 }
2294
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002295 if (params->dst_addr) {
2296 conn_calculate_hash_sockaddr(params->dst_addr,
2297 buf, &idx, &hash_flags,
2298 CONN_HASH_PARAMS_TYPE_DST_ADDR,
2299 CONN_HASH_PARAMS_TYPE_DST_PORT);
2300 }
2301
Amaury Denoyelled10a2002021-02-11 19:45:19 +01002302 if (params->src_addr) {
2303 conn_calculate_hash_sockaddr(params->src_addr,
2304 buf, &idx, &hash_flags,
2305 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
2306 CONN_HASH_PARAMS_TYPE_SRC_PORT);
2307 }
2308
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002309 if (params->proxy_prehash) {
2310 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01002311 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002312 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
2313 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01002314
Amaury Denoyelle1921d202021-01-14 10:15:29 +01002315 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01002316 return hash;
2317}
Amaury Denoyelle48372932021-09-16 12:15:12 +02002318
2319/* Handler of the task of mux_stopping_data.
2320 * Called on soft-stop.
2321 */
2322static struct task *mux_stopping_process(struct task *t, void *ctx, unsigned int state)
2323{
2324 struct connection *conn, *back;
2325
2326 list_for_each_entry_safe(conn, back, &mux_stopping_data[tid].list, stopping_list) {
2327 if (conn->mux && conn->mux->wake)
2328 conn->mux->wake(conn);
2329 }
2330
2331 return t;
2332}
2333
2334static int allocate_mux_cleanup(void)
2335{
2336 /* allocates the thread bound mux_stopping_data task */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002337 mux_stopping_data[tid].task = task_new_here();
Amaury Denoyelle48372932021-09-16 12:15:12 +02002338 if (!mux_stopping_data[tid].task) {
2339 ha_alert("Failed to allocate the task for connection cleanup on thread %d.\n", tid);
2340 return 0;
2341 }
2342
2343 mux_stopping_data[tid].task->process = mux_stopping_process;
2344 LIST_INIT(&mux_stopping_data[tid].list);
2345
2346 return 1;
2347}
2348REGISTER_PER_THREAD_ALLOC(allocate_mux_cleanup);
2349
2350static int deallocate_mux_cleanup(void)
2351{
2352 task_destroy(mux_stopping_data[tid].task);
2353 return 1;
2354}
2355REGISTER_PER_THREAD_FREE(deallocate_mux_cleanup);