From 12100320d2510ce7a63f50483ce7fe53574e49f2 Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Fri, 3 Mar 2023 17:19:02 -0500 Subject: [PATCH] net/interfaces: always return an IPv4 LikelyHomeRouterIP We weren't filtering out IPv6 addresses from this function, so we could be returning an IPv4 gateway IP and an IPv6 self IP. Per the function comments, only return IPv4 addresses for the self IP. Signed-off-by: Andrew Dunham Change-Id: If19a4aadc343fbd4383fc5290befa0eff006799e --- net/interfaces/interfaces.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/net/interfaces/interfaces.go b/net/interfaces/interfaces.go index ba7756b1c..f8f815bd8 100644 --- a/net/interfaces/interfaces.go +++ b/net/interfaces/interfaces.go @@ -613,8 +613,18 @@ func LikelyHomeRouterIP() (gateway, myIP netip.Addr, ok bool) { return } ForeachInterfaceAddress(func(i Interface, pfx netip.Prefix) { + if !i.IsUp() { + // Skip interfaces that aren't up. + return + } else if myIP.IsValid() { + // We already have a valid self IP; skip this one. + return + } + ip := pfx.Addr() - if !i.IsUp() || !ip.IsValid() || myIP.IsValid() { + if !ip.IsValid() || !ip.Is4() { + // Skip IPs that aren't valid or aren't IPv4, since we + // always return an IPv4 address. return } if gateway.IsPrivate() && ip.IsPrivate() {