]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: backend: do not use the source port when hashing clientip
authorWilly Tarreau <w@1wt.eu>
Wed, 9 Apr 2025 08:57:54 +0000 (10:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 9 Apr 2025 09:05:22 +0000 (11:05 +0200)
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.

src/backend.c

index 6ae61fa516f63cf9a2220d5e9bea803e92a290c9..451686b6ad53cd638f763220e8fb4aafa97da68f 100644 (file)
@@ -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: