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 <[email protected]>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-11-07 12:29:01 -08:00
1 parent dc46358ecf
commit 503010831e
5 files changed
+42 -34

No files matched your search

+1
View File
@@ -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+
+35 -32
View File
@@ -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
}
+1 -1
View File
@@ -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()
+2 -1
View File
@@ -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)
}
+3
View File
@@ -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"