util: mask the prefix and cover /0 in GenerateIPv4DNSRootDomain

Host bits shifted the range: 100.65.0.0/10 gave 65..128.

Updates #3482
This commit is contained in:
Kristoffer Dalby committed 2026-11-04 16:25:09 +01:00
1 parent 8f49f4713d
commit aa3356910a
2 files changed
+32 -36

No files matched your search

+13 -34
View File
@@ -72,57 +72,36 @@ func ValidateUsername(username string) error {
return nil
}
// generateMagicDNSRootDomains generates a list of DNS entries to be included in [tailcfg.DNSConfig.Routes] in [tailcfg.MapResponse].
// This list of reverse DNS entries instructs the OS on what subnets and domains the Tailscale embedded DNS
// server (listening in 100.100.100.100 udp/53) should be used for.
//
// Tailscale.com includes in the list:
// - the [types.DNSConfig.BaseDomain] of the user
// - the reverse DNS entry for IPv6 (0.e.1.a.c.5.1.1.a.7.d.f.ip6.arpa., see below more on IPv6)
// - the reverse DNS entries for the IPv4 subnets covered by the user's `IPPrefix`.
// In the public SaaS this is [64-127].100.in-addr.arpa.
//
// The main purpose of this function is then generating the list of IPv4 entries. For the 100.64.0.0/10, this
// is clear, and could be hardcoded. But we are allowing any range as `IPPrefix`, so we need to find out the
// subnets when we have 172.16.0.0/16 (i.e., 16.172.in-addr.arpa.), or any other subnet.
//
// How IN-ADDR.ARPA domains work is defined in RFC1035 (section 3.5). Tailscale.com seems to adhere to this,
// and do not make use of RFC2317 ("Classless IN-ADDR.ARPA delegation") - hence generating the entries for the next
// class block only.
// GenerateIPv4DNSRootDomain generates the IPv4 reverse DNS root domains.
// From the netmask we can find out the wildcard bits (the bits that are not set in the netmask).
// This allows us to then calculate the subnets included in the subsequent class block and generate the entries.
// GenerateIPv4DNSRootDomain returns the in-addr.arpa. names that route reverse
// lookups for ipPrefix to MagicDNS ([tailcfg.DNSConfig.Routes]). Reverse zones
// split on octets (RFC 1035 section 3.5) and Tailscale does not delegate
// classlessly (RFC 2317), so a prefix ending inside an octet yields one name
// per value of that octet, and an octet-aligned prefix yields one name.
func GenerateIPv4DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
// Conversion to the std lib [net.IPNet], a bit easier to operate
netRange := netipx.PrefixIPNet(ipPrefix)
// Config prefixes keep their host bits, which would shift the range.
netRange := netipx.PrefixIPNet(ipPrefix.Masked())
maskBits, _ := netRange.Mask.Size()
// lastOctet is the last IP byte covered by the mask
// lastOctet indexes the octet the mask ends in.
lastOctet := maskBits / ByteSize
// wildcardBits is the number of bits not under the mask in the lastOctet
wildcardBits := ByteSize - maskBits%ByteSize
// An octet-aligned mask has no wildcard bits: the prefix boundary falls on
// an octet boundary, so the wildcard octet is the last one *inside* the
// mask and it takes a single value. Without this, ByteSize - 0 yields 8
// wildcard bits and all 256 children of the name that should have been
// emitted alone are generated instead. It also keeps lastOctet from
// indexing past the address on an IPv4 /32.
// An aligned mask ends on an octet boundary: one name for the last masked
// octet covers all 256 children. A /0 masks no octet.
if maskBits%ByteSize == 0 {
if maskBits == 0 {
return nil
return []dnsname.FQDN{"in-addr.arpa."}
}
lastOctet--
wildcardBits = 0
}
// minVal is the value in the lastOctet byte of the IP
// maxVal is basically 2^wildcardBits - i.e., the value when all the wildcardBits are set to 1
// maxVal is minVal with every wildcard bit set.
minVal := uint(netRange.IP[lastOctet])
maxVal := (minVal + 1<<uint(wildcardBits)) - 1 //nolint:gosec // wildcardBits is always < 8, no overflow
maxVal := (minVal + 1<<uint(wildcardBits)) - 1 //nolint:gosec // wildcardBits is in [0, 7]
// here we generate the base domain (e.g., 100.in-addr.arpa., 16.172.in-addr.arpa., etc.)
rdnsSlice := []string{}
+19 -2
View File
@@ -26,13 +26,15 @@ func TestMagicDNSRootDomains172(t *testing.T) {
// An octet-aligned mask has no wildcard bits, so it must yield exactly one
// reverse-DNS name. Enumerating the next octet instead emits all 256 children
// of that name, which overruns the search-domain limits of stub resolvers.
// of that name, which can overflow a resolver's per-link domain limit.
func TestMagicDNSRootDomainsOctetAligned(t *testing.T) {
for _, tt := range []struct {
prefix string
want string
}{
{prefix: "0.0.0.0/0", want: "in-addr.arpa."},
{prefix: "100.0.0.0/8", want: "100.in-addr.arpa."},
{prefix: "100.120.0.5/24", want: "0.120.100.in-addr.arpa."},
{prefix: "100.120.0.0/16", want: "120.100.in-addr.arpa."},
{prefix: "100.120.0.0/24", want: "0.120.100.in-addr.arpa."},
{prefix: "100.120.0.5/32", want: "5.0.120.100.in-addr.arpa."},
@@ -50,7 +52,7 @@ func TestMagicDNSRootDomainsOctetAligned(t *testing.T) {
// enumerate every value that octet takes within the prefix. RFC1035 reverse
// delegation is per-octet, so a mask longer than /24 enumerates host names
// rather than delegating classlessly (RFC2317), which is what Tailscale
// expects. These counts and boundaries are unchanged by the aligned-mask case.
// expects.
func TestMagicDNSRootDomainsUnaligned(t *testing.T) {
for _, tt := range []struct {
prefix string
@@ -59,6 +61,21 @@ func TestMagicDNSRootDomainsUnaligned(t *testing.T) {
last string
absent []string
}{
{
prefix: "10.0.0.0/7",
count: 2,
first: "10.in-addr.arpa.",
last: "11.in-addr.arpa.",
absent: []string{"12.in-addr.arpa."},
},
{
// Host bits set: the range starts at the masked octet.
prefix: "100.65.0.0/10",
count: 64,
first: "64.100.in-addr.arpa.",
last: "127.100.in-addr.arpa.",
absent: []string{"128.100.in-addr.arpa."},
},
{
prefix: "10.0.0.0/9",
count: 128,