ipn/ipnlocal,net/dns/resolver: serve MagicDNS names from live indexes

Every netmap change, including an incremental delta of a single peer,
rebuilt the full MagicDNS state twice: dnsConfigForNetmap walked all
peers to build the dns.Config.Hosts map, and resolver.SetConfig then
walked that map again to build its reverse (PTR) index. On a tailnet
with 10k peers that is a lot of garbage per delta.

Instead, add a resolver.MagicDNSHosts hook, installed once by
LocalBackend, that the quad-100 resolver consults on demand at query
time. It is backed by nodeBackend's nodeByName, nodeByAddr, and peers
indexes, which are already maintained incrementally as netmap deltas
arrive. The subdomain-resolve capability check also moves to the hook
(checking the node's CapMap at query time), so dns.Config's
SubdomainHosts is no longer populated.

dns.Config.Hosts remains for control's DNS.ExtraRecords, which are
few and which feed the split-DNS decisions in dns.Manager's
compileConfig, and on Windows it still carries every node's records
because the hosts-file fallback path (compileHostEntries) needs the
complete enumerable set. Those compileConfig decisions also consulted
the per-node Hosts entries (hasHostsWithoutSplitDNSRoutes), so a new
Config.MagicDNSHostsUnrouted bit preserves that signal now that node
records are not listed: with MagicDNS names present but MagicDNS
domain routing off, quad-100 stays in the OS resolver path.

One small behavior change: reverse (PTR) lookups now also answer for
node addresses whose forward records are filtered out by the
IPv6-suppression rule (issue #1152), since nodeByAddr indexes all node
addresses. Previously such addresses were absent from the pushed
Hosts map and thus from the reverse index.

Updates #12542
Updates tailscale/corp#43949

Change-Id: I63b99199c2b3b124c08cb8bbaea1f63165095294
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2026-07-14 07:26:41 -07:00
committed by Brad Fitzpatrick
parent 6a635c4e55
commit 7e609b2581
11 changed files with 511 additions and 125 deletions
+105
View File
@@ -476,6 +476,111 @@ func TestResolveLocalSubdomain(t *testing.T) {
}
}
// fakeMagicDNSHosts is a MagicDNSHosts for tests, serving from fixed maps.
type fakeMagicDNSHosts struct {
hosts map[dnsname.FQDN][]netip.Addr
subdomain set.Set[dnsname.FQDN]
ptr map[netip.Addr]dnsname.FQDN
}
func (f fakeMagicDNSHosts) LookupHost(fqdn dnsname.FQDN) (ips []netip.Addr, ok bool) {
ips, ok = f.hosts[fqdn]
return ips, ok
}
func (f fakeMagicDNSHosts) LookupPTR(ip netip.Addr) (_ dnsname.FQDN, ok bool) {
name, ok := f.ptr[ip]
return name, ok
}
func (f fakeMagicDNSHosts) SubdomainHost(fqdn dnsname.FQDN) bool {
return f.subdomain.Contains(fqdn)
}
// Tests forward, subdomain, and reverse resolution served on demand
// via the MagicDNSHosts hook, and that entries pushed via Config.Hosts
// take precedence over the hook.
func TestResolveLocalMagicDNSHosts(t *testing.T) {
r := newResolver(t)
defer r.Close()
r.SetConfig(Config{
Hosts: map[dnsname.FQDN][]netip.Addr{
"extra.ipn.dev.": {netip.MustParseAddr("100.100.1.1")},
"both.ipn.dev.": {netip.MustParseAddr("100.100.2.2")},
},
LocalDomains: []dnsname.FQDN{"ipn.dev.", "64.100.in-addr.arpa."},
})
node4 := netip.MustParseAddr("100.64.0.7")
node6 := netip.MustParseAddr("fd7a:115c:a1e0::7")
r.SetMagicDNSHosts(fakeMagicDNSHosts{
hosts: map[dnsname.FQDN][]netip.Addr{
"node.ipn.dev.": {node4, node6},
"v4only.ipn.dev.": {node4},
"subber.ipn.dev.": {node4},
"both.ipn.dev.": {netip.MustParseAddr("100.100.9.9")}, // masked by Config.Hosts
},
subdomain: set.Of[dnsname.FQDN]("subber.ipn.dev."),
ptr: map[netip.Addr]dnsname.FQDN{node4: "node.ipn.dev."},
})
tests := []struct {
name string
qname dnsname.FQDN
qtype dns.Type
ip netip.Addr
code dns.RCode
}{
{"hook-ipv4", "node.ipn.dev.", dns.TypeA, node4, dns.RCodeSuccess},
{"hook-ipv6", "node.ipn.dev.", dns.TypeAAAA, node6, dns.RCodeSuccess},
// A known name with no records of the queried family is
// "name exists, no records", not NXDOMAIN.
{"hook-no-ipv6", "v4only.ipn.dev.", dns.TypeAAAA, netip.Addr{}, dns.RCodeSuccess},
{"hook-nxdomain", "gone.ipn.dev.", dns.TypeA, netip.Addr{}, dns.RCodeNameError},
{"hook-foreign", "google.com.", dns.TypeA, netip.Addr{}, dns.RCodeRefused},
{"hook-subdomain", "foo.subber.ipn.dev.", dns.TypeA, node4, dns.RCodeSuccess},
{"hook-subdomain-deep", "bar.foo.subber.ipn.dev.", dns.TypeA, node4, dns.RCodeSuccess},
{"hook-subdomain-no-cap", "foo.node.ipn.dev.", dns.TypeA, netip.Addr{}, dns.RCodeNameError},
// Config.Hosts entries (control's ExtraRecords) are
// consulted before the hook.
{"config-hosts", "extra.ipn.dev.", dns.TypeA, netip.MustParseAddr("100.100.1.1"), dns.RCodeSuccess},
{"config-hosts-precedence", "both.ipn.dev.", dns.TypeA, netip.MustParseAddr("100.100.2.2"), dns.RCodeSuccess},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ip, code := r.resolveLocal(tt.qname, tt.qtype)
if code != tt.code {
t.Errorf("code = %v; want %v", code, tt.code)
}
if ip != tt.ip {
t.Errorf("ip = %v; want %v", ip, tt.ip)
}
})
}
revTests := []struct {
name string
q dnsname.FQDN
want dnsname.FQDN
code dns.RCode
}{
{"hook-ptr", "7.0.64.100.in-addr.arpa.", "node.ipn.dev.", dns.RCodeSuccess},
{"hook-ptr-nxdomain", "8.0.64.100.in-addr.arpa.", "", dns.RCodeNameError},
{"hook-ptr-foreign", "5.4.3.2.in-addr.arpa.", "", dns.RCodeRefused},
}
for _, tt := range revTests {
t.Run(tt.name, func(t *testing.T) {
name, code := r.resolveLocalReverse(tt.q)
if code != tt.code {
t.Errorf("code = %v; want %v", code, tt.code)
}
if name != tt.want {
t.Errorf("name = %v; want %v", name, tt.want)
}
})
}
}
func TestResolveLocalReverse(t *testing.T) {
r := newResolver(t)
defer r.Close()