From aa3356910a01f9ca365bc4d496282dc96b2d9631 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 4 Nov 2026 13:31:34 +0000 Subject: [PATCH] util: mask the prefix and cover /0 in GenerateIPv4DNSRootDomain Host bits shifted the range: 100.65.0.0/10 gave 65..128. Updates #3482 --- hscontrol/util/dns.go | 47 +++++++++++--------------------------- hscontrol/util/dns_test.go | 21 +++++++++++++++-- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/hscontrol/util/dns.go b/hscontrol/util/dns.go index 9bbc01a9a..24b42e875 100644 --- a/hscontrol/util/dns.go +++ b/hscontrol/util/dns.go @@ -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<