diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a6d73df..b2a5b74a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -205,7 +205,6 @@ the ACME DNS-01 challenge through a built-in DNS provider: `acmedns`, - Lower CPU use on large tailnets when node tags, owners, IPs or routes change [#3501](https://github.com/juanfont/headscale/pull/3501) - `headscale nodes backfillips` now sends the new IPs to connected clients [#3501](https://github.com/juanfont/headscale/pull/3501) - A registration request from a client below the minimum supported version is now rejected before it can log a node out, use a pre-auth key or start a login [#3519](https://github.com/juanfont/headscale/pull/3519) -- Fix the MagicDNS search domain generation for octet-aligned subnets (/8, /16, /24) to output a single covering PTR [#3482](https://github.com/juanfont/headscale/pull/3482) - A node re-registering with a spent, expired or revoked pre-auth key is now rejected if it expired or changed node key while the re-registration was in flight [#3525](https://github.com/juanfont/headscale/pull/3525) - DERP client verification (the embedded DERP server and the `/verify` endpoint) looks up the node key directly instead of scanning every node on each connection [#3520](https://github.com/juanfont/headscale/pull/3520) @@ -234,6 +233,7 @@ the ACME DNS-01 challenge through a built-in DNS provider: `acmedns`, - Fix a `via` router that is also a source peering with others serving the route, and a router inside a subnet source missing `via` routes [#3637](https://github.com/juanfont/headscale/pull/3637) - With MagicDNS on, exit nodes refuse DNS queries for names under `dns.base_domain`, as on Tailscale SaaS. Clients cannot read the tailnet's names through an exit node. Reverse lookups still pass. [#3636](https://github.com/juanfont/headscale/pull/3636) - Peers get a node's last seen time when it goes offline, not at the next full map. [#3636](https://github.com/juanfont/headscale/pull/3636) +- With MagicDNS on, stop sending reverse DNS routes for `prefixes` inside 100.64.0.0/10 and fd7a:115c:a1e0::/48, as on Tailscale SaaS; clients add them. A `prefixes.v4` subset such as 100.64.0.0/24 sent 256, which overflowed systemd-resolved's domain limit and dropped reverse DNS. An octet-aligned prefix outside the ranges gets one route instead of 256 [#3482](https://github.com/juanfont/headscale/pull/3482) ## 0.29.7 (2026-10-30) diff --git a/hscontrol/app.go b/hscontrol/app.go index c37c7a47c..1f0d11bf7 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -13,6 +13,7 @@ import ( "math/big" "net" "net/http" + "net/netip" "os" "os/signal" "path/filepath" @@ -50,6 +51,7 @@ import ( "github.com/sasha-s/go-deadlock" "golang.org/x/sync/errgroup" "tailscale.com/envknob" + "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/dnstype" "tailscale.com/types/key" @@ -233,16 +235,23 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) { app.authProvider = authProvider if app.cfg.TailcfgDNSConfig != nil && app.cfg.TailcfgDNSConfig.Proxied { // if MagicDNS + // Clients route reverse lookups for the Tailscale ranges themselves + // (magicDNSRootDomains in tailscale.com/ipn/ipnlocal) and SaaS sends + // no reverse routes, so only a prefix outside those ranges needs them. + outside := func(p, r netip.Prefix) bool { + return p.Bits() < r.Bits() || !r.Contains(p.Addr()) + } + // TODO(kradalby): revisit why this takes a list. var magicDNSDomains []dnsname.FQDN - if cfg.PrefixV4 != nil { + if cfg.PrefixV4 != nil && outside(*cfg.PrefixV4, tsaddr.CGNATRange()) { magicDNSDomains = append( magicDNSDomains, util.GenerateIPv4DNSRootDomain(*cfg.PrefixV4)..., ) } - if cfg.PrefixV6 != nil { + if cfg.PrefixV6 != nil && outside(*cfg.PrefixV6, tsaddr.TailscaleULARange()) { magicDNSDomains = append( magicDNSDomains, util.GenerateIPv6DNSRootDomain(*cfg.PrefixV6)..., diff --git a/hscontrol/servertest/dns_compat_test.go b/hscontrol/servertest/dns_compat_test.go new file mode 100644 index 000000000..f5a5b4d0a --- /dev/null +++ b/hscontrol/servertest/dns_compat_test.go @@ -0,0 +1,150 @@ +// This file replays MagicDNS captures from Tailscale SaaS, one per ipPool, +// and compares the reverse-DNS routes headscale sends with those SaaS sent. +// +// Test data source: ../policy/v2/testdata/dns_results/dns-reverse-*.hujson +// Source format: github.com/juanfont/headscale/hscontrol/types/testcapture + +package servertest_test + +import ( + "encoding/json" + "maps" + "net/netip" + "path/filepath" + "slices" + "strings" + "testing" + "time" + + "github.com/juanfont/headscale/hscontrol/servertest" + "github.com/juanfont/headscale/hscontrol/types/testcapture" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "tailscale.com/net/tsaddr" + "tailscale.com/types/dnstype" + "tailscale.com/types/netmap" +) + +// reverseRoutes returns the reverse-DNS keys of routes as FQDNs; headscale +// sends them without the trailing dot, SaaS with it. +func reverseRoutes(routes map[string][]*dnstype.Resolver) []string { + var out []string + + for k := range routes { + if k = strings.TrimSuffix(k, ".") + "."; strings.HasSuffix(k, ".arpa.") { + out = append(out, k) + } + } + + slices.Sort(out) + + return out +} + +// dnsNetmap starts headscale with prefixV4 and returns a node's netmap once +// its DNS config has arrived. +func dnsNetmap(t *testing.T, prefixV4 netip.Prefix, magicDNS bool) *netmap.NetworkMap { + t.Helper() + + opts := []servertest.ServerOption{servertest.WithPrefixV4(prefixV4)} + if magicDNS { + opts = append(opts, servertest.WithMagicDNS("example.com")) + } + + srv := servertest.NewServer(t, opts...) + client := servertest.NewClient(t, srv, "node", servertest.WithUser(srv.CreateUser(t, "dns"))) + client.WaitForCondition(t, "dns config", 10*time.Second, func(nm *netmap.NetworkMap) bool { + return nm != nil && nm.SelfNode.Valid() && nm.DNS.Proxied == magicDNS + }) + + nm := client.Netmap() + require.True(t, slices.ContainsFunc(nm.GetAddresses().AsSlice(), func(p netip.Prefix) bool { + return prefixV4.Contains(p.Addr()) + }), "node address outside prefixes.v4 %s", prefixV4) + + return nm +} + +// TestMagicDNSReverseRoutesCompat: with an ipPool inside CGNAT, SaaS sends no +// reverse-DNS routes, whatever the pool's size or alignment; clients add the +// ones for the Tailscale ranges themselves. Headscale runs each pool as +// prefixes.v4 and must send the same. +func TestMagicDNSReverseRoutesCompat(t *testing.T) { + t.Parallel() + + files, err := filepath.Glob(filepath.Join("..", "policy", "v2", "testdata", "dns_results", "dns-reverse-*.hujson")) + require.NoError(t, err) + require.NotEmpty(t, files) + + for _, file := range files { + c, err := testcapture.Read(file) + require.NoError(t, err) + + // SaaS refuses an ipPool outside CGNAT; TestMagicDNSReverseRoutesOutsideCGNAT + // covers what headscale sends for one. + if c.Error { + assert.Contains(t, c.Input.APIResponseBody.Message, "must be within CGNAT range", c.TestID) + + continue + } + + var policy struct { + NodeAttrs []struct { + IPPool []netip.Prefix `json:"ipPool"` + } `json:"nodeAttrs"` + } + require.NoError(t, json.Unmarshal([]byte(c.Input.FullPolicy), &policy), c.TestID) + + var pools []netip.Prefix + for _, attr := range policy.NodeAttrs { + pools = append(pools, attr.IPPool...) + } + + if len(pools) == 0 { + pools = []netip.Prefix{tsaddr.CGNATRange()} + } + + // SaaS sends every node the same DNS config. + var want []string + + for i, name := range slices.Sorted(maps.Keys(c.Captures)) { + nm := c.Captures[name].Netmap + require.NotNil(t, nm, "%s/%s", c.TestID, name) + + got := reverseRoutes(nm.DNS.Routes) + if i == 0 { + want = got + } + + require.Equal(t, want, got, "%s: %s got other reverse routes", c.TestID, name) + } + + for _, pool := range pools { + t.Run(c.TestID+"/"+pool.String(), func(t *testing.T) { + t.Parallel() + + nm := dnsNetmap(t, pool, c.Input.Tailnet.DNS.MagicDNS) + assert.Equal(t, want, reverseRoutes(nm.DNS.Routes)) + }) + } + } +} + +// TestMagicDNSReverseRoutesOutsideCGNAT: SaaS has no prefix outside the +// Tailscale ranges (dns-reverse-e1/e2), so clients add no reverse routes for +// one and headscale must. An octet-aligned prefix gets one name, not 256. +func TestMagicDNSReverseRoutesOutsideCGNAT(t *testing.T) { + t.Parallel() + + for prefix, want := range map[string][]string{ + "172.16.0.0/16": {"16.172.in-addr.arpa."}, + "10.0.0.0/23": {"0.0.10.in-addr.arpa.", "1.0.10.in-addr.arpa."}, + } { + t.Run(prefix, func(t *testing.T) { + t.Parallel() + + nm := dnsNetmap(t, netip.MustParsePrefix(prefix), true) + assert.Equal(t, want, reverseRoutes(nm.DNS.Routes)) + }) + } +} diff --git a/hscontrol/servertest/server.go b/hscontrol/servertest/server.go index 969377325..11bd7ae26 100644 --- a/hscontrol/servertest/server.go +++ b/hscontrol/servertest/server.go @@ -58,6 +58,7 @@ type serverConfig struct { magicDNSDomain string dnsResolvers []string certProvider hsdns.Provider + prefixV4 netip.Prefix } func defaultServerConfig() *serverConfig { @@ -67,6 +68,7 @@ func defaultServerConfig() *serverConfig { batcherWorkers: 1, ephemeralTimeout: 30 * time.Second, taildropEnabled: true, + prefixV4: netip.MustParsePrefix("100.64.0.0/10"), } } @@ -125,6 +127,11 @@ func WithMagicDNS(domain string) ServerOption { return func(c *serverConfig) { c.magicDNSDomain = domain } } +// WithPrefixV4 sets prefixes.v4, the range node IPv4 addresses come from. +func WithPrefixV4(p netip.Prefix) ServerOption { + return func(c *serverConfig) { c.prefixV4 = p } +} + // WithCertificates enables node certificates (dns.certificates) with // provider publishing the challenges. It needs [WithMagicDNS]; the // propagation check is off because the provider is not real DNS. @@ -157,7 +164,7 @@ func NewServer(tb testing.TB, opts ...ServerOption) *TestServer { tmpDir := tb.TempDir() - prefixV4 := netip.MustParsePrefix("100.64.0.0/10") + prefixV4 := sc.prefixV4 prefixV6 := netip.MustParsePrefix("fd7a:115c:a1e0::/48") cfg := types.Config{