From 30f85c07c6dae45b89967d707f6067537164ac19 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 6 Feb 2026 22:27:05 +0000 Subject: [PATCH] Only use get_srcaddr if SO_BINDTODEVICE is defined; otherwise use route address --- libnetutil/netutil.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index 4ae62618f..01fd7c03d 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -3382,7 +3382,7 @@ static struct interface_info *find_loopback_iface(struct interface_info *ifaces, /* Get the source address for routing to dst by creating a socket and asking the operating system for the local address. */ static int get_srcaddr(const struct sockaddr_storage *dst, - struct sockaddr_storage *src) + struct sockaddr_storage *src, const char *device) { static const unsigned short DUMMY_PORT = 1234; struct sockaddr_storage dst_dummy; @@ -3407,6 +3407,10 @@ static int get_srcaddr(const struct sockaddr_storage *dst, goto bail; } + if (device) { + socket_bindtodevice(fd, device); + } + rc = connect(fd, (struct sockaddr *) &dst_dummy, dst_dummy_len); if (rc == -1) { netutil_error("%s: can't connect socket: %s", __func__, socket_strerror(socket_errno())); @@ -3524,7 +3528,7 @@ static int route_dst_generic(const struct sockaddr_storage *dst, rnfo->direct_connect = 1; /* But the source address we want to use is the target address. */ if (!spoofss) { - if (get_srcaddr(dst, &rnfo->srcaddr) == -1) + if (get_srcaddr(dst, &rnfo->srcaddr, device) == -1) rnfo->srcaddr = rnfo->ii.addr; } @@ -3552,7 +3556,9 @@ static int route_dst_generic(const struct sockaddr_storage *dst, sockaddr_equal(&routes[i].gw, &routes[i].device->addr) || sockaddr_equal(&routes[i].gw, dst)); if (!spoofss) { - if (get_srcaddr(dst, &rnfo->srcaddr) == -1) +#ifdef SO_BINDTODEVICE + if (get_srcaddr(dst, &rnfo->srcaddr, device) == -1) +#endif rnfo->srcaddr = rnfo->ii.addr; } rnfo->nexthop = routes[i].gw; @@ -3573,7 +3579,9 @@ static int route_dst_generic(const struct sockaddr_storage *dst, rnfo->ii = ifaces[i]; rnfo->direct_connect = 1; if (!spoofss) { - if (get_srcaddr(dst, &rnfo->srcaddr) == -1) +#ifdef SO_BINDTODEVICE + if (get_srcaddr(dst, &rnfo->srcaddr, device) == -1) +#endif rnfo->srcaddr = rnfo->ii.addr; }