From 503010831ecd44ab3d86864e5e65ff9f8b969d88 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sat, 7 Nov 2026 11:20:18 -0800 Subject: [PATCH] net/{netmon,tstun},tsconst: avoid enumerating all interfaces when retrieving the default GetWindowsDefault called GetAdaptersAddresses to build a map of every adapter before walking the routing table. Enumerating adapters is expensive on Windows and, since the call doesn't skip DNS info, it also goes through the DNS Client service, which on recent Windows builds enumerates every interface for each DNS server on each adapter. In this PR, we query only what we need: the routing table, the metric of each interface with a default route, and the interface row for routes that beat the current best. We also identify the Tailscale interface by its Wintun GUID rather than by its description, and move the GUID to tsconst so that net/tstun and net/netmon share it. Updates #17772 Updates #21885 Change-Id: I4c6a9d9ed791788fb0567154ee1dbe0f6a2c52a2 Signed-off-by: Nick Khyl --- cmd/derper/depaware.txt | 1 + net/netmon/interfaces_windows.go | 67 +++++++++++++++++--------------- net/netmon/netmon_windows.go | 2 +- net/tstun/tun_windows.go | 3 +- tsconst/tsconst.go | 3 ++ 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/cmd/derper/depaware.txt b/cmd/derper/depaware.txt index 962434166..b6917f417 100644 --- a/cmd/derper/depaware.txt +++ b/cmd/derper/depaware.txt @@ -162,6 +162,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa 💣 tailscale.com/util/hashx from tailscale.com/util/deephash tailscale.com/util/lineiter from tailscale.com/hostinfo+ tailscale.com/util/mak from tailscale.com/health+ + W tailscale.com/util/must from tailscale.com/net/netmon tailscale.com/util/nocasemaps from tailscale.com/types/ipproto tailscale.com/util/rands from tailscale.com/tsweb tailscale.com/util/set from tailscale.com/derp/derpserver+ diff --git a/net/netmon/interfaces_windows.go b/net/netmon/interfaces_windows.go index 070b08ba6..78e636b41 100644 --- a/net/netmon/interfaces_windows.go +++ b/net/netmon/interfaces_windows.go @@ -15,12 +15,16 @@ import ( "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" "tailscale.com/feature/buildfeatures" "tailscale.com/tsconst" + "tailscale.com/util/mak" + "tailscale.com/util/must" ) const ( fallbackInterfaceMetric = uint32(0) // Used if we cannot get the actual interface metric ) +var wintunInterfaceGUID = must.Get(windows.GUIDFromString(tsconst.WintunInterfaceGUID)) + func init() { likelyHomeRouterIP = likelyHomeRouterIPWindows if buildfeatures.HasUseProxy { @@ -155,26 +159,18 @@ func getInterfaces(family winipcfg.AddressFamily, flags winipcfg.GAAFlags, match // It returns (nil, nil) if no interface is found. // // The family must be one of AF_INET or AF_INET6. -func GetWindowsDefault(family winipcfg.AddressFamily) (*winipcfg.IPAdapterAddresses, error) { - ifs, err := getInterfaces(family, winipcfg.GAAFlagIncludeAllInterfaces, func(iface *winipcfg.IPAdapterAddresses) bool { - switch iface.IfType { - case winipcfg.IfTypeSoftwareLoopback: - return false +func GetWindowsDefault(family winipcfg.AddressFamily) (*winipcfg.MibIfRow2, error) { + var ipInterfaceCache map[winipcfg.LUID]*winipcfg.MibIPInterfaceRow + getIPInterface := func(luid winipcfg.LUID) (*winipcfg.MibIPInterfaceRow, error) { + if ipif, ok := ipInterfaceCache[luid]; ok { + return ipif, nil } - switch family { - case windows.AF_INET: - if iface.Flags&winipcfg.IPAAFlagIpv4Enabled == 0 { - return false - } - case windows.AF_INET6: - if iface.Flags&winipcfg.IPAAFlagIpv6Enabled == 0 { - return false - } + ipif, err := luid.IPInterface(family) + if err != nil { + return nil, err } - return iface.OperStatus == winipcfg.IfOperStatusUp && notTailscaleInterface(iface) - }) - if err != nil { - return nil, err + mak.Set(&ipInterfaceCache, luid, ipif) + return ipif, nil } routes, err := winipcfg.GetIPForwardTable2(family) @@ -183,14 +179,14 @@ func GetWindowsDefault(family winipcfg.AddressFamily) (*winipcfg.IPAdapterAddres } bestMetric := ^uint32(0) - var bestIface *winipcfg.IPAdapterAddresses + var bestIface *winipcfg.MibIfRow2 for _, route := range routes { if route.DestinationPrefix.PrefixLength != 0 { // Not a default route. continue } - iface := ifs[route.InterfaceLUID] - if iface == nil { + ipif, err := getIPInterface(route.InterfaceLUID) + if err != nil { continue } @@ -201,17 +197,24 @@ func GetWindowsDefault(family winipcfg.AddressFamily) (*winipcfg.IPAdapterAddres // metric offset specified in the Metric member of the // MIB_IPFORWARD_ROW2 structure and the interface // metric specified in this member for IPv4" - metric := route.Metric - switch family { - case windows.AF_INET: - metric += iface.Ipv4Metric - case windows.AF_INET6: - metric += iface.Ipv6Metric + metric := route.Metric + ipif.Metric + if metric >= bestMetric { + continue } - if metric < bestMetric { - bestMetric = metric - bestIface = iface + + iface, err := route.InterfaceLUID.Interface() + if err != nil || iface.OperStatus != winipcfg.IfOperStatusUp { + continue } + if iface.Type == winipcfg.IfTypeSoftwareLoopback { + continue + } + if iface.InterfaceGUID == wintunInterfaceGUID { + continue + } + + bestMetric = metric + bestIface = iface } return bestIface, nil @@ -226,9 +229,9 @@ func defaultRoute() (d DefaultRouteDetails, err error) { return d, err } if iface != nil { - d.InterfaceName = iface.FriendlyName() + d.InterfaceName = iface.Alias() d.InterfaceDesc = iface.Description() - d.InterfaceIndex = int(iface.IfIndex) + d.InterfaceIndex = int(iface.InterfaceIndex) } return d, nil } diff --git a/net/netmon/netmon_windows.go b/net/netmon/netmon_windows.go index 6a2013905..fa2fac04c 100644 --- a/net/netmon/netmon_windows.go +++ b/net/netmon/netmon_windows.go @@ -120,7 +120,7 @@ func (m *Monitor) WindowsDefaultInterfaceIndex(family winipcfg.AddressFamily) (u if err != nil || iface == nil { return 0, err } - return iface.IfIndex, nil + return iface.InterfaceIndex, nil } if cache == nil { return lookup() diff --git a/net/tstun/tun_windows.go b/net/tstun/tun_windows.go index 96721021b..235b2f78b 100644 --- a/net/tstun/tun_windows.go +++ b/net/tstun/tun_windows.go @@ -7,11 +7,12 @@ import ( "github.com/tailscale/wireguard-go/tun" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" + "tailscale.com/tsconst" ) func init() { tun.WintunTunnelType = "Tailscale" - guid, err := windows.GUIDFromString("{37217669-42da-4657-a55b-0d995d328250}") + guid, err := windows.GUIDFromString(tsconst.WintunInterfaceGUID) if err != nil { panic(err) } diff --git a/tsconst/tsconst.go b/tsconst/tsconst.go index 1477bafec..83a0a884c 100644 --- a/tsconst/tsconst.go +++ b/tsconst/tsconst.go @@ -10,6 +10,9 @@ package tsconst const WintunInterfaceDesc = "Tailscale Tunnel" const WintunInterfaceDesc0_14 = "Wintun Userspace Tunnel" +// WintunInterfaceGUID is the GUID of the Tailscale interface on Windows. +const WintunInterfaceGUID = "{37217669-42da-4657-a55b-0d995d328250}" + // TailnetLockNotTrustedMsg is the error message used by tailnet lock // and sniffed (via substring) out of an error sent over the network. const TailnetLockNotTrustedMsg = "this node is not trusted by Tailnet Lock"