mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
When quad-100 must be installed as the OS primary resolver (Apple Mode B), its catch-all forwarders were blended from base-config nameserver IPs alone: non-standard ports, search domains, and DoH/DoT endpoints were lost, and a base read that returned no resolvers silently installed a catch-all with no forwarders at all, breaking all public DNS while reporting a healthy DNS configuration. Introduce OSConfig.Resolvers, which OSConfigurators able to recover the underlying resolver configuration at full fidelity populate for the DNS manager only: it never reaches the OS, and Equal ignores it so configuration application is unaffected. Blend it into the catch-all route verbatim, falling back to plain IP:53 resolvers derived from Nameservers. Teach the forwarder to dial arbitrary https:// resolvers instead of only well-known public providers: at their BootstrapResolution addresses when present, or at the URL's own host when that is an IP literal, so enterprise DoH endpoints recovered from the OS can be forwarded to over DoH rather than plaintext DNS. Reject an empty base config on Apple Mode B: keep the previous configuration, use the upstream empty-base health warning and medium severity, and let an extension-triggered recompile retry instead of installing a catch-all that cannot forward. Expose the same missing- resolver error to OS configurators so bridge-reported absence uses this warning too, while genuine configuration-read failures remain distinct. Extend the Apple mode tests with empty-base and full-fidelity blend coverage, and update the iOS primary-mode cases to model the real NetworkExtension base read (LAN resolvers and search domains) rather than the silently empty catch-all. RELNOTE: Fix silent public DNS breakage when Tailscale must be the system DNS resolver on Apple clients. Updates #20341 Updates tailscale/corp#45534 Updates tailscale/corp#48693 Change-Id: Ie7e277b0fd39d2b91b1e770f8f01d688e02b4d49 Signed-off-by: James Tucker <[email protected]>
220 lines
6.5 KiB
Go
220 lines
6.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package dns
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"fmt"
|
|
"net/netip"
|
|
"slices"
|
|
"strings"
|
|
|
|
"tailscale.com/feature/buildfeatures"
|
|
"tailscale.com/types/dnstype"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/dnsname"
|
|
)
|
|
|
|
// An OSConfigurator applies DNS settings to the operating system.
|
|
type OSConfigurator interface {
|
|
// SetDNS updates the OS's DNS configuration to match cfg.
|
|
// If cfg is the zero value, all Tailscale-related DNS
|
|
// configuration is removed.
|
|
// SetDNS must not be called after Close.
|
|
// SetDNS takes ownership of cfg.
|
|
SetDNS(cfg OSConfig) error
|
|
// SupportsSplitDNS reports whether the configurator is capable of
|
|
// installing a resolver only for specific DNS suffixes. If false,
|
|
// the configurator can only set a global resolver.
|
|
SupportsSplitDNS() bool
|
|
// GetBaseConfig returns the OS's "base" configuration, i.e. the
|
|
// resolver settings the OS would use without Tailscale
|
|
// contributing any configuration.
|
|
// GetBaseConfig must return the tailscale-free base config even
|
|
// after SetDNS has been called to set a Tailscale configuration.
|
|
// Only works when SupportsSplitDNS=false.
|
|
|
|
// Implementations that don't support getting the base config must
|
|
// return ErrGetBaseConfigNotSupported. Implementations that can read the
|
|
// configuration but cannot recover any underlying resolvers may return
|
|
// ErrGetBaseConfigNoResolvers; other read failures should return their error.
|
|
GetBaseConfig() (OSConfig, error)
|
|
// Close removes Tailscale-related DNS configuration from the OS.
|
|
Close() error
|
|
}
|
|
|
|
// HostEntry represents a single line in the OS's hosts file.
|
|
type HostEntry struct {
|
|
Addr netip.Addr
|
|
Hosts []string
|
|
}
|
|
|
|
// OSConfig is an OS DNS configuration.
|
|
type OSConfig struct {
|
|
// Hosts is a map of DNS FQDNs to their IPs, which should be added to the
|
|
// OS's hosts file. Currently, (2022-08-12) it is only populated for Windows
|
|
// in SplitDNS mode and with Smart Name Resolution turned on.
|
|
Hosts []*HostEntry
|
|
// Nameservers are the IP addresses of the nameservers to use.
|
|
Nameservers []netip.Addr
|
|
// SearchDomains are the domain suffixes to use when expanding
|
|
// single-label name queries. SearchDomains is additive to
|
|
// whatever non-Tailscale search domains the OS has.
|
|
SearchDomains []dnsname.FQDN
|
|
// MatchDomains are the DNS suffixes for which Nameservers should
|
|
// be used. If empty, Nameservers is installed as the "primary" resolver.
|
|
// A non-empty MatchDomains requests a "split DNS" configuration
|
|
// from the OS, which will only work with OSConfigurators that
|
|
// report SupportsSplitDNS()=true.
|
|
MatchDomains []dnsname.FQDN
|
|
// Resolvers is the full-fidelity form of Nameservers, populated only by
|
|
// OSConfigurators whose [OSConfigurator.GetBaseConfig] can recover more
|
|
// of the underlying resolver configuration than plain IP:53 addresses
|
|
// (non-standard ports, DoH/DoT endpoints). It is input to the DNS
|
|
// manager only: the manager blends it into quad-100's catch-all route
|
|
// when the tunnel resolver must serve as the OS primary. It is never
|
|
// applied to the OS itself, and [OSConfig.Equal] deliberately ignores
|
|
// it, because it does not affect the configuration installed on the OS.
|
|
Resolvers []*dnstype.Resolver `json:",omitempty"`
|
|
}
|
|
|
|
func (o *OSConfig) WriteToBufioWriter(w *bufio.Writer) {
|
|
if o == nil {
|
|
w.WriteString("<nil>")
|
|
return
|
|
}
|
|
w.WriteString("{")
|
|
if len(o.Hosts) > 0 {
|
|
fmt.Fprintf(w, "Hosts:%v ", o.Hosts)
|
|
}
|
|
if len(o.Nameservers) > 0 {
|
|
fmt.Fprintf(w, "Nameservers:%v ", o.Nameservers)
|
|
}
|
|
if len(o.SearchDomains) > 0 {
|
|
fmt.Fprintf(w, "SearchDomains:%v ", o.SearchDomains)
|
|
}
|
|
if len(o.MatchDomains) > 0 {
|
|
w.WriteString("MatchDomains:[")
|
|
sp := ""
|
|
var numARPA int
|
|
for _, s := range o.MatchDomains {
|
|
if strings.HasSuffix(string(s), ".arpa.") {
|
|
numARPA++
|
|
continue
|
|
}
|
|
w.WriteString(sp)
|
|
w.WriteString(string(s))
|
|
sp = " "
|
|
}
|
|
w.WriteString("]")
|
|
if numARPA > 0 {
|
|
fmt.Fprintf(w, "+%darpa", numARPA)
|
|
}
|
|
}
|
|
w.WriteString("}")
|
|
}
|
|
|
|
func (o OSConfig) IsZero() bool {
|
|
return len(o.Hosts) == 0 &&
|
|
len(o.Nameservers) == 0 &&
|
|
len(o.SearchDomains) == 0 &&
|
|
len(o.MatchDomains) == 0 &&
|
|
len(o.Resolvers) == 0
|
|
}
|
|
|
|
func (a OSConfig) Equal(b OSConfig) bool {
|
|
if len(a.Hosts) != len(b.Hosts) {
|
|
return false
|
|
}
|
|
if len(a.Nameservers) != len(b.Nameservers) {
|
|
return false
|
|
}
|
|
if len(a.SearchDomains) != len(b.SearchDomains) {
|
|
return false
|
|
}
|
|
if len(a.MatchDomains) != len(b.MatchDomains) {
|
|
return false
|
|
}
|
|
|
|
for i := range a.Hosts {
|
|
ha, hb := a.Hosts[i], b.Hosts[i]
|
|
if ha.Addr != hb.Addr {
|
|
return false
|
|
}
|
|
if !slices.Equal(ha.Hosts, hb.Hosts) {
|
|
return false
|
|
}
|
|
}
|
|
for i := range a.Nameservers {
|
|
if a.Nameservers[i] != b.Nameservers[i] {
|
|
return false
|
|
}
|
|
}
|
|
for i := range a.SearchDomains {
|
|
if a.SearchDomains[i] != b.SearchDomains[i] {
|
|
return false
|
|
}
|
|
}
|
|
for i := range a.MatchDomains {
|
|
if a.MatchDomains[i] != b.MatchDomains[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Format implements the fmt.Formatter interface to ensure that Hosts is
|
|
// printed correctly (i.e. not as a bunch of pointers).
|
|
//
|
|
// Fixes https://github.com/tailscale/tailscale/issues/5669
|
|
func (a OSConfig) Format(f fmt.State, verb rune) {
|
|
logger.ArgWriter(func(w *bufio.Writer) {
|
|
if !buildfeatures.HasDNS {
|
|
w.WriteString(`{DNS-unlinked}`)
|
|
return
|
|
}
|
|
w.WriteString(`{Nameservers:[`)
|
|
for i, ns := range a.Nameservers {
|
|
if i != 0 {
|
|
w.WriteString(" ")
|
|
}
|
|
fmt.Fprintf(w, "%+v", ns)
|
|
}
|
|
w.WriteString(`] SearchDomains:[`)
|
|
for i, domain := range a.SearchDomains {
|
|
if i != 0 {
|
|
w.WriteString(" ")
|
|
}
|
|
fmt.Fprintf(w, "%+v", domain)
|
|
}
|
|
w.WriteString(`] MatchDomains:[`)
|
|
for i, domain := range a.MatchDomains {
|
|
if i != 0 {
|
|
w.WriteString(" ")
|
|
}
|
|
fmt.Fprintf(w, "%+v", domain)
|
|
}
|
|
w.WriteString(`] Hosts:[`)
|
|
for i, host := range a.Hosts {
|
|
if i != 0 {
|
|
w.WriteString(" ")
|
|
}
|
|
fmt.Fprintf(w, "%+v", host)
|
|
}
|
|
w.WriteString(`]}`)
|
|
}).Format(f, verb)
|
|
}
|
|
|
|
// ErrGetBaseConfigNotSupported is the error
|
|
// OSConfigurator.GetBaseConfig returns when the OSConfigurator
|
|
// doesn't support reading the underlying configuration out of the OS.
|
|
var ErrGetBaseConfigNotSupported = errors.New("getting OS base config is not supported")
|
|
|
|
// ErrGetBaseConfigNoResolvers is returned by [OSConfigurator.GetBaseConfig]
|
|
// when neither a fresh read nor a retained configuration supplies an underlying
|
|
// resolver. It produces the same health warning as a successful but empty read.
|
|
var ErrGetBaseConfigNoResolvers = errEmptyBaseConfig
|