From: Willy Tarreau Date: Wed, 9 Apr 2025 08:57:54 +0000 (+0200) Subject: BUG/MINOR: backend: do not use the source port when hashing clientip X-Git-Tag: v3.2-dev10~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7b6df86a834883f27f6f7d18d0caa8a6ea128c14;p=thirdparty%2Fhaproxy.git BUG/MINOR: backend: do not use the source port when hashing clientip The server's "usesrc" keyword supports among other options "client" and "clientip". The former means we bind to the client's IP and port to connect to the server, while the latter means we bind to its IP only. It's done in two steps, first alloc_bind_address() retrieves the IP address and port, and second, tcp_connect_server() decides to either bind to the IP only or IP+port. The problem comes with idle connection pools, which hash all the parameters: the hash is calculated before (and ideally withouy) calling tcp_connect_server(), and it considers the whole struct sockaddr_storage for the hash, except that both client and clientip entirely fill it with the client's address. This means that both client and clientip make use of the source port in the hash calculation, making idle connections almost not reusable when using "usesrc clientip" while they should for clients coming from the same source. A work-around is to force the source port to zero using "tcp-request session set-src-port int(0)" but it's ugly. Let's fix this by properly zeroing the port for AF_INET/AF_INET6 addresses. This can be backported to 2.4. Thanks to Sebastien Gross for providing a reproducer for this problem. --- diff --git a/src/backend.c b/src/backend.c index 6ae61fa51..451686b6a 100644 --- a/src/backend.c +++ b/src/backend.c @@ -1231,6 +1231,15 @@ int alloc_bind_address(struct sockaddr_storage **ss, return SRV_STATUS_INTERNAL; **ss = *addr; + if ((src->opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_CIP) { + /* always set port to zero when using "clientip", or + * the idle connection hash will include the port part. + */ + if (addr->ss_family == AF_INET) + ((struct sockaddr_in *)*ss)->sin_port = 0; + else if (addr->ss_family == AF_INET6) + ((struct sockaddr_in6 *)*ss)->sin6_port = 0; + } break; case CO_SRC_TPROXY_DYN: