From 162488a77566398241c9165d42b410eb88572de3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 17 Apr 2023 09:40:37 -0700 Subject: [PATCH] net/interfaces: cache "home" router lookup on big Linux routers This is a continuation of the earlier 2a67beaacf but more aggressive; this now remembers that we failed to find the "home" router IP so we don't try again later on the next call. Updates #7621 Signed-off-by: Brad Fitzpatrick --- net/interfaces/interfaces_linux.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/net/interfaces/interfaces_linux.go b/net/interfaces/interfaces_linux.go index 41eb949df..b7d405828 100644 --- a/net/interfaces/interfaces_linux.go +++ b/net/interfaces/interfaces_linux.go @@ -98,7 +98,22 @@ func likelyHomeRouterIPLinux() (ret netip.Addr, ok bool) { } log.Printf("interfaces: failed to read /proc/net/route: %v", err) } - return ret, ret.IsValid() + if ret.IsValid() { + return ret, true + } + if lineNum >= maxProcNetRouteRead { + // If we went over our line limit without finding an answer, assume + // we're a big fancy Linux router (or at least not a home system) + // and set the error bit so we stop trying this in the future (and wasting CPU). + // See https://github.com/tailscale/tailscale/issues/7621. + // + // Remember that "likelyHomeRouterIP" exists purely to find the port + // mapping service (UPnP, PMP, PCP) often present on a home router. If we hit + // the route (line) limit without finding an answer, we're unlikely to ever + // find one in the future. + procNetRouteErr.Store(true) + } + return netip.Addr{}, false } // Android apps don't have permission to read /proc/net/route, at