2020-07-31 20:27:09 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2022-08-19 17:19:50 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-04-06 19:59:06 +00:00
|
|
|
"errors"
|
2020-07-31 20:27:09 +00:00
|
|
|
"fmt"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-08-19 17:19:50 +00:00
|
|
|
"os"
|
2020-10-30 03:01:31 +00:00
|
|
|
"os/exec"
|
2022-08-19 17:19:50 +00:00
|
|
|
"path/filepath"
|
2021-04-28 01:24:03 +00:00
|
|
|
"sort"
|
2020-07-31 20:27:09 +00:00
|
|
|
"strings"
|
2020-10-30 03:01:31 +00:00
|
|
|
"syscall"
|
2020-11-03 17:59:50 +00:00
|
|
|
"time"
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-06 19:59:06 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2020-07-31 20:27:09 +00:00
|
|
|
"golang.org/x/sys/windows/registry"
|
2021-04-06 19:59:06 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2022-08-19 17:19:50 +00:00
|
|
|
"tailscale.com/atomicfile"
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2020-07-31 20:27:09 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-04-09 22:24:47 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2022-08-12 21:55:01 +00:00
|
|
|
"tailscale.com/util/winutil"
|
2020-07-31 20:27:09 +00:00
|
|
|
)
|
|
|
|
|
2020-08-19 18:16:57 +00:00
|
|
|
const (
|
2021-04-06 19:59:06 +00:00
|
|
|
versionKey = `SOFTWARE\Microsoft\Windows NT\CurrentVersion`
|
2020-08-19 18:16:57 +00:00
|
|
|
)
|
|
|
|
|
2022-01-24 18:52:57 +00:00
|
|
|
var configureWSL = envknob.Bool("TS_DEBUG_CONFIGURE_WSL")
|
2022-01-10 05:14:50 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
type windowsManager struct {
|
2021-06-25 23:36:36 +00:00
|
|
|
logf logger.Logf
|
|
|
|
guid string
|
2022-06-01 20:41:11 +00:00
|
|
|
nrptDB *nrptRuleDatabase
|
2021-06-25 23:36:36 +00:00
|
|
|
wslManager *wslManager
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func NewOSConfigurator(logf logger.Logf, interfaceName string) (OSConfigurator, error) {
|
2021-04-06 04:51:57 +00:00
|
|
|
ret := windowsManager{
|
2021-06-30 00:33:17 +00:00
|
|
|
logf: logf,
|
|
|
|
guid: interfaceName,
|
|
|
|
wslManager: newWSLManager(logf),
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
|
2022-06-01 20:41:11 +00:00
|
|
|
if isWindows10OrBetter() {
|
|
|
|
ret.nrptDB = newNRPTRuleDatabase(logf)
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
|
2021-06-30 00:33:17 +00:00
|
|
|
// Log WSL status once at startup.
|
|
|
|
if distros, err := wslDistros(); err != nil {
|
|
|
|
logf("WSL: could not list distributions: %v", err)
|
|
|
|
} else {
|
|
|
|
logf("WSL: found %d distributions", len(distros))
|
2021-06-25 23:36:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
return ret, nil
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 21:55:01 +00:00
|
|
|
func (m windowsManager) openInterfaceKey(pfx winutil.RegistryPathPrefix) (registry.Key, error) {
|
|
|
|
path := pfx.WithSuffix(m.guid)
|
|
|
|
key, err := winutil.OpenKeyWait(registry.LOCAL_MACHINE, path, registry.SET_VALUE)
|
2020-07-31 20:27:09 +00:00
|
|
|
if err != nil {
|
2021-04-06 04:51:57 +00:00
|
|
|
return 0, fmt.Errorf("opening %s: %w", path, err)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
return key, nil
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-04-06 04:51:57 +00:00
|
|
|
func delValue(key registry.Key, name string) error {
|
|
|
|
if err := key.DeleteValue(name); err != nil && err != registry.ErrNotExist {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-19 18:16:57 +00:00
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2022-05-25 21:51:54 +00:00
|
|
|
// setSplitDNS configures one or more NRPT (Name Resolution Policy Table) rules
|
2021-04-06 04:51:57 +00:00
|
|
|
// to resolve queries for domains using resolvers, rather than the
|
|
|
|
// system's "primary" resolver.
|
|
|
|
//
|
2022-05-25 21:51:54 +00:00
|
|
|
// If no resolvers are provided, the Tailscale NRPT rules are deleted.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (m windowsManager) setSplitDNS(resolvers []netip.Addr, domains []dnsname.FQDN) error {
|
2022-06-01 20:41:11 +00:00
|
|
|
if m.nrptDB == nil {
|
|
|
|
if resolvers == nil {
|
|
|
|
// Just a no-op in this case.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Split DNS unsupported on this Windows version")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer m.nrptDB.Refresh()
|
2021-04-06 04:51:57 +00:00
|
|
|
if len(resolvers) == 0 {
|
2022-06-01 20:41:11 +00:00
|
|
|
return m.nrptDB.DelAllRuleKeys()
|
2021-04-06 04:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
servers := make([]string, 0, len(resolvers))
|
|
|
|
for _, resolver := range resolvers {
|
|
|
|
servers = append(servers, resolver.String())
|
|
|
|
}
|
2022-05-25 21:51:54 +00:00
|
|
|
|
2022-06-01 20:41:11 +00:00
|
|
|
return m.nrptDB.WriteSplitDNSConfig(servers, domains)
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 17:19:50 +00:00
|
|
|
func setTailscaleHosts(prevHostsFile []byte, hosts []*HostEntry) ([]byte, error) {
|
|
|
|
b := bytes.ReplaceAll(prevHostsFile, []byte("\r\n"), []byte("\n"))
|
|
|
|
sc := bufio.NewScanner(bytes.NewReader(b))
|
|
|
|
const (
|
|
|
|
header = "# TailscaleHostsSectionStart"
|
|
|
|
footer = "# TailscaleHostsSectionEnd"
|
|
|
|
)
|
|
|
|
var comments = []string{
|
|
|
|
"# This section contains MagicDNS entries for Tailscale.",
|
|
|
|
"# Do not edit this section manually.",
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
var inSection bool
|
|
|
|
for sc.Scan() {
|
|
|
|
line := strings.TrimSpace(sc.Text())
|
|
|
|
if line == header {
|
|
|
|
inSection = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if line == footer {
|
|
|
|
inSection = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if inSection {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&out, line)
|
|
|
|
}
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(hosts) > 0 {
|
|
|
|
fmt.Fprintln(&out, header)
|
|
|
|
for _, c := range comments {
|
|
|
|
fmt.Fprintln(&out, c)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&out)
|
|
|
|
for _, he := range hosts {
|
|
|
|
fmt.Fprintf(&out, "%s %s\n", he.Addr, strings.Join(he.Hosts, " "))
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&out)
|
|
|
|
fmt.Fprintln(&out, footer)
|
|
|
|
}
|
|
|
|
return bytes.ReplaceAll(out.Bytes(), []byte("\n"), []byte("\r\n")), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setHosts sets the hosts file to contain the given host entries.
|
|
|
|
func (m windowsManager) setHosts(hosts []*HostEntry) error {
|
|
|
|
systemDir, err := windows.GetSystemDirectory()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hostsFile := filepath.Join(systemDir, "drivers", "etc", "hosts")
|
|
|
|
b, err := os.ReadFile(hostsFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outB, err := setTailscaleHosts(b, hosts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
const fileMode = 0 // ignored on windows.
|
|
|
|
return atomicfile.WriteFile(hostsFile, outB, fileMode)
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:51:57 +00:00
|
|
|
// setPrimaryDNS sets the given resolvers and domains as the Tailscale
|
|
|
|
// interface's DNS configuration.
|
|
|
|
// If resolvers is non-empty, those resolvers become the system's
|
|
|
|
// "primary" resolvers.
|
|
|
|
// domains can be set without resolvers, which just contributes new
|
|
|
|
// paths to the global DNS search list.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (m windowsManager) setPrimaryDNS(resolvers []netip.Addr, domains []dnsname.FQDN) error {
|
2020-07-31 20:27:09 +00:00
|
|
|
var ipsv4 []string
|
|
|
|
var ipsv6 []string
|
2020-08-19 18:16:57 +00:00
|
|
|
|
2021-04-06 04:51:57 +00:00
|
|
|
for _, ip := range resolvers {
|
2020-07-31 20:27:09 +00:00
|
|
|
if ip.Is4() {
|
|
|
|
ipsv4 = append(ipsv4, ip.String())
|
|
|
|
} else {
|
|
|
|
ipsv6 = append(ipsv6, ip.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 22:24:47 +00:00
|
|
|
domStrs := make([]string, 0, len(domains))
|
|
|
|
for _, dom := range domains {
|
|
|
|
domStrs = append(domStrs, dom.WithoutTrailingDot())
|
|
|
|
}
|
|
|
|
|
2022-08-12 21:55:01 +00:00
|
|
|
key4, err := m.openInterfaceKey(winutil.IPv4TCPIPInterfacePrefix)
|
2021-04-06 04:51:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer key4.Close()
|
|
|
|
|
|
|
|
if len(ipsv4) == 0 {
|
|
|
|
if err := delValue(key4, "NameServer"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err := key4.SetStringValue("NameServer", strings.Join(ipsv4, ",")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(domains) == 0 {
|
|
|
|
if err := delValue(key4, "SearchList"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-09 22:24:47 +00:00
|
|
|
} else if err := key4.SetStringValue("SearchList", strings.Join(domStrs, ",")); err != nil {
|
2020-08-19 18:16:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
|
2022-08-12 21:55:01 +00:00
|
|
|
key6, err := m.openInterfaceKey(winutil.IPv6TCPIPInterfacePrefix)
|
2021-04-06 04:51:57 +00:00
|
|
|
if err != nil {
|
2020-07-31 20:27:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
defer key6.Close()
|
2020-08-19 18:16:57 +00:00
|
|
|
|
2021-04-06 04:51:57 +00:00
|
|
|
if len(ipsv6) == 0 {
|
|
|
|
if err := delValue(key6, "NameServer"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err := key6.SetStringValue("NameServer", strings.Join(ipsv6, ",")); err != nil {
|
2020-08-19 18:16:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
|
|
|
|
if len(domains) == 0 {
|
|
|
|
if err := delValue(key6, "SearchList"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-09 22:24:47 +00:00
|
|
|
} else if err := key6.SetStringValue("SearchList", strings.Join(domStrs, ",")); err != nil {
|
2021-04-06 04:51:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:19:39 +00:00
|
|
|
// Disable LLMNR on the Tailscale interface. We don't do multicast, and we
|
|
|
|
// certainly don't do LLMNR, so it's pointless to make Windows try it. It is
|
|
|
|
// being deprecated.
|
2021-04-06 04:51:57 +00:00
|
|
|
if err := key4.SetDWordValue("EnableMulticast", 0); err != nil {
|
2020-08-19 18:16:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
if err := key6.SetDWordValue("EnableMulticast", 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m windowsManager) SetDNS(cfg OSConfig) error {
|
|
|
|
// We can configure Windows DNS in one of two ways:
|
|
|
|
//
|
|
|
|
// - In primary DNS mode, we set the NameServer and SearchList
|
|
|
|
// registry keys on our interface. Because our interface metric
|
|
|
|
// is very low, this turns us into the one and only "primary"
|
|
|
|
// resolver for the OS, i.e. all queries flow to the
|
|
|
|
// resolver(s) we specify.
|
|
|
|
// - In split DNS mode, we set the Domain registry key on our
|
|
|
|
// interface (which adds that domain to the global search list,
|
|
|
|
// but does not contribute other DNS configuration from the
|
|
|
|
// interface), and configure an NRPT (Name Resolution Policy
|
|
|
|
// Table) rule to route queries for our suffixes to the
|
|
|
|
// provided resolver.
|
|
|
|
//
|
|
|
|
// When switching modes, we delete all the configuration related
|
|
|
|
// to the other mode, so these two are an XOR.
|
|
|
|
//
|
|
|
|
// Windows actually supports much more advanced configurations as
|
|
|
|
// well, with arbitrary routing of hosts and suffixes to arbitrary
|
|
|
|
// resolvers. However, we use it in a "simple" split domain
|
|
|
|
// configuration only, routing one set of things to the "split"
|
|
|
|
// resolver and the rest to the primary.
|
|
|
|
|
2022-08-12 22:19:39 +00:00
|
|
|
// Unconditionally disable dynamic DNS updates and NetBIOS on our
|
|
|
|
// interfaces.
|
2022-06-23 22:15:11 +00:00
|
|
|
if err := m.disableDynamicUpdates(); err != nil {
|
|
|
|
m.logf("disableDynamicUpdates error: %v\n", err)
|
|
|
|
}
|
2022-08-12 22:19:39 +00:00
|
|
|
if err := m.disableNetBIOS(); err != nil {
|
|
|
|
m.logf("disableNetBIOS error: %v\n", err)
|
|
|
|
}
|
2022-06-23 22:15:11 +00:00
|
|
|
|
2021-04-06 22:21:32 +00:00
|
|
|
if len(cfg.MatchDomains) == 0 {
|
2021-04-06 04:51:57 +00:00
|
|
|
if err := m.setSplitDNS(nil, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-19 17:19:50 +00:00
|
|
|
if err := m.setHosts(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 22:21:32 +00:00
|
|
|
if err := m.setPrimaryDNS(cfg.Nameservers, cfg.SearchDomains); err != nil {
|
2021-04-06 04:51:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-01 20:41:11 +00:00
|
|
|
} else if m.nrptDB == nil {
|
2021-04-06 19:59:06 +00:00
|
|
|
return errors.New("cannot set per-domain resolvers on Windows 7")
|
2021-04-06 04:51:57 +00:00
|
|
|
} else {
|
2021-04-06 22:21:32 +00:00
|
|
|
if err := m.setSplitDNS(cfg.Nameservers, cfg.MatchDomains); err != nil {
|
2021-04-06 04:51:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-19 17:19:50 +00:00
|
|
|
// Unset the resolver on the interface to ensure that we do not become
|
|
|
|
// the primary resolver. Although this is what we want, at the moment
|
|
|
|
// (2022-08-13) it causes single label resolutions from the OS resolver
|
|
|
|
// to wait for a MDNS response from the Tailscale interface.
|
|
|
|
// See #1659 and #5366 for more details.
|
|
|
|
//
|
|
|
|
// Still set search domains on the interface, since NRPT only handles
|
|
|
|
// query routing and not search domain expansion.
|
2021-04-06 22:21:32 +00:00
|
|
|
if err := m.setPrimaryDNS(nil, cfg.SearchDomains); err != nil {
|
2021-04-06 04:51:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-19 17:19:50 +00:00
|
|
|
|
|
|
|
// As we are not the primary resolver in this setup, we need to
|
|
|
|
// explicitly set some single name hosts to ensure that we can resolve
|
|
|
|
// them quickly and get around the 2.3s delay that otherwise occurs due
|
|
|
|
// to multicast timeouts.
|
|
|
|
if err := m.setHosts(cfg.Hosts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
}
|
2020-08-19 18:16:57 +00:00
|
|
|
|
2020-10-30 03:01:31 +00:00
|
|
|
// Force DNS re-registration in Active Directory. What we actually
|
|
|
|
// care about is that this command invokes the undocumented hidden
|
|
|
|
// function that forces Windows to notice that adapter settings
|
|
|
|
// have changed, which makes the DNS settings actually take
|
|
|
|
// effect.
|
|
|
|
//
|
2020-11-03 17:59:50 +00:00
|
|
|
// This command can take a few seconds to run, so run it async, best effort.
|
2021-04-06 04:51:57 +00:00
|
|
|
//
|
|
|
|
// After re-registering DNS, also flush the DNS cache to clear out
|
|
|
|
// any cached split-horizon queries that are no longer the correct
|
|
|
|
// answer.
|
2020-11-03 17:59:50 +00:00
|
|
|
go func() {
|
|
|
|
t0 := time.Now()
|
|
|
|
m.logf("running ipconfig /registerdns ...")
|
|
|
|
cmd := exec.Command("ipconfig", "/registerdns")
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
2021-04-06 04:51:57 +00:00
|
|
|
err := cmd.Run()
|
2020-11-03 17:59:50 +00:00
|
|
|
d := time.Since(t0).Round(time.Millisecond)
|
2021-04-06 04:51:57 +00:00
|
|
|
if err != nil {
|
2020-11-03 17:59:50 +00:00
|
|
|
m.logf("error running ipconfig /registerdns after %v: %v", d, err)
|
|
|
|
} else {
|
|
|
|
m.logf("ran ipconfig /registerdns in %v", d)
|
|
|
|
}
|
2021-04-06 04:51:57 +00:00
|
|
|
|
|
|
|
t0 = time.Now()
|
2021-08-03 19:55:51 +00:00
|
|
|
m.logf("running ipconfig /flushdns ...")
|
2021-04-06 04:51:57 +00:00
|
|
|
cmd = exec.Command("ipconfig", "/flushdns")
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
|
|
err = cmd.Run()
|
|
|
|
d = time.Since(t0).Round(time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
m.logf("error running ipconfig /flushdns after %v: %v", d, err)
|
|
|
|
} else {
|
|
|
|
m.logf("ran ipconfig /flushdns in %v", d)
|
|
|
|
}
|
2020-11-03 17:59:50 +00:00
|
|
|
}()
|
2020-10-30 03:01:31 +00:00
|
|
|
|
2021-06-25 23:36:36 +00:00
|
|
|
// On initial setup of WSL, the restart caused by --shutdown is slow,
|
|
|
|
// so we do it out-of-line.
|
2022-01-10 05:14:50 +00:00
|
|
|
if configureWSL {
|
|
|
|
go func() {
|
|
|
|
if err := m.wslManager.SetDNS(cfg); err != nil {
|
|
|
|
m.logf("WSL SetDNS: %v", err) // continue
|
|
|
|
} else {
|
|
|
|
m.logf("WSL SetDNS: success")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-06-25 23:36:36 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:05:47 +00:00
|
|
|
func (m windowsManager) SupportsSplitDNS() bool {
|
2022-06-01 20:41:11 +00:00
|
|
|
return m.nrptDB != nil
|
2021-04-02 09:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m windowsManager) Close() error {
|
2022-06-29 21:02:23 +00:00
|
|
|
err := m.SetDNS(OSConfig{})
|
|
|
|
if m.nrptDB != nil {
|
|
|
|
m.nrptDB.Close()
|
|
|
|
}
|
|
|
|
return err
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 22:15:11 +00:00
|
|
|
// disableDynamicUpdates sets the appropriate registry values to prevent the
|
|
|
|
// Windows DHCP client from sending dynamic DNS updates for our interface to
|
|
|
|
// AD domain controllers.
|
|
|
|
func (m windowsManager) disableDynamicUpdates() error {
|
2022-08-29 22:16:34 +00:00
|
|
|
if err := m.setSingleDWORD(winutil.IPv4TCPIPInterfacePrefix, "DisableDynamicUpdate", 1); err != nil {
|
2022-08-12 21:55:01 +00:00
|
|
|
return err
|
2022-06-23 22:15:11 +00:00
|
|
|
}
|
2022-08-29 22:16:34 +00:00
|
|
|
if err := m.setSingleDWORD(winutil.IPv6TCPIPInterfacePrefix, "DisableDynamicUpdate", 1); err != nil {
|
2022-08-12 21:55:01 +00:00
|
|
|
return err
|
2022-06-23 22:15:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 21:55:01 +00:00
|
|
|
// setSingleDWORD opens the Registry Key in HKLM for the interface associated
|
|
|
|
// with the windowsManager and sets the "keyPrefix\value" to data.
|
|
|
|
func (m windowsManager) setSingleDWORD(prefix winutil.RegistryPathPrefix, value string, data uint32) error {
|
|
|
|
k, err := m.openInterfaceKey(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer k.Close()
|
|
|
|
return k.SetDWordValue(value, data)
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:19:39 +00:00
|
|
|
// disableNetBIOS sets the appropriate registry values to prevent Windows from
|
|
|
|
// sending NetBIOS name resolution requests for our interface which we do not
|
|
|
|
// handle nor want to. By leaving it enabled and not handling it we introduce
|
|
|
|
// short-name resolution delays in certain conditions as Windows waits for
|
|
|
|
// NetBIOS responses from our interface (#1659).
|
|
|
|
//
|
|
|
|
// Further, LLMNR and NetBIOS are being deprecated anyway in favor of MDNS.
|
|
|
|
// https://techcommunity.microsoft.com/t5/networking-blog/aligning-on-mdns-ramping-down-netbios-name-resolution-and-llmnr/ba-p/3290816
|
|
|
|
func (m windowsManager) disableNetBIOS() error {
|
|
|
|
return m.setSingleDWORD(winutil.NetBTInterfacePrefix, "NetbiosOptions", 2)
|
|
|
|
}
|
|
|
|
|
2021-04-07 07:31:31 +00:00
|
|
|
func (m windowsManager) GetBaseConfig() (OSConfig, error) {
|
|
|
|
resolvers, err := m.getBasePrimaryResolver()
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
return OSConfig{
|
|
|
|
Nameservers: resolvers,
|
|
|
|
// Don't return any search domains here, because even Windows
|
|
|
|
// 7 correctly handles blending search domains from multiple
|
|
|
|
// sources, and any search domains we add here will get tacked
|
|
|
|
// onto the Tailscale config unnecessarily.
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:59:06 +00:00
|
|
|
// getBasePrimaryResolver returns a guess of the non-Tailscale primary
|
|
|
|
// resolver on the system.
|
|
|
|
// It's used on Windows 7 to emulate split DNS by trying to figure out
|
|
|
|
// what the "previous" primary resolver was. It might be wrong, or
|
|
|
|
// incomplete.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (m windowsManager) getBasePrimaryResolver() (resolvers []netip.Addr, err error) {
|
2021-04-06 19:59:06 +00:00
|
|
|
tsGUID, err := windows.GUIDFromString(m.guid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tsLUID, err := winipcfg.LUIDFromGUID(&tsGUID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ifrows, err := winipcfg.GetIPInterfaceTable(windows.AF_INET)
|
|
|
|
if err == windows.ERROR_NOT_FOUND {
|
|
|
|
// IPv4 seems disabled, try to get interface metrics from IPv6 instead.
|
|
|
|
ifrows, err = winipcfg.GetIPInterfaceTable(windows.AF_INET6)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-28 01:24:03 +00:00
|
|
|
type candidate struct {
|
|
|
|
id winipcfg.LUID
|
|
|
|
metric uint32
|
|
|
|
}
|
|
|
|
var candidates []candidate
|
2021-04-06 19:59:06 +00:00
|
|
|
for _, row := range ifrows {
|
|
|
|
if !row.Connected {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if row.InterfaceLUID == tsLUID {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-28 01:24:03 +00:00
|
|
|
candidates = append(candidates, candidate{row.InterfaceLUID, row.Metric})
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
2021-04-28 01:24:03 +00:00
|
|
|
if len(candidates) == 0 {
|
2021-04-06 19:59:06 +00:00
|
|
|
// No resolvers set outside of Tailscale.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-04-28 01:24:03 +00:00
|
|
|
sort.Slice(candidates, func(i, j int) bool { return candidates[i].metric < candidates[j].metric })
|
|
|
|
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
ips, err := candidate.id.DNS()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ipLoop:
|
2022-08-11 17:23:35 +00:00
|
|
|
for _, ip := range ips {
|
2022-08-02 20:38:11 +00:00
|
|
|
ip = ip.Unmap()
|
2021-04-28 01:24:03 +00:00
|
|
|
// Skip IPv6 site-local resolvers. These are an ancient
|
|
|
|
// and obsolete IPv6 RFC, which Windows still faithfully
|
|
|
|
// implements. The net result is that some low-metric
|
|
|
|
// interfaces can "have" DNS resolvers, but they're just
|
|
|
|
// site-local resolver IPs that don't go anywhere. So, we
|
|
|
|
// skip the site-local resolvers in order to find the
|
|
|
|
// first interface that has real DNS servers configured.
|
|
|
|
for _, sl := range siteLocalResolvers {
|
|
|
|
if ip.WithZone("") == sl {
|
|
|
|
continue ipLoop
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 19:59:06 +00:00
|
|
|
resolvers = append(resolvers, ip)
|
|
|
|
}
|
2021-04-28 01:24:03 +00:00
|
|
|
|
|
|
|
if len(resolvers) > 0 {
|
|
|
|
// Found some resolvers, we're done.
|
|
|
|
break
|
|
|
|
}
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolvers, nil
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
var siteLocalResolvers = []netip.Addr{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParseAddr("fec0:0:0:ffff::1"),
|
|
|
|
netip.MustParseAddr("fec0:0:0:ffff::2"),
|
|
|
|
netip.MustParseAddr("fec0:0:0:ffff::3"),
|
2021-04-28 01:24:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 00:48:47 +00:00
|
|
|
func isWindows10OrBetter() bool {
|
2021-04-06 19:59:06 +00:00
|
|
|
key, err := registry.OpenKey(registry.LOCAL_MACHINE, versionKey, registry.READ)
|
|
|
|
if err != nil {
|
2021-06-24 00:48:47 +00:00
|
|
|
// Fail safe, assume old Windows.
|
|
|
|
return false
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
2021-06-24 00:48:47 +00:00
|
|
|
// This key above only exists in Windows 10 and above. Its mere
|
|
|
|
// presence is good enough.
|
|
|
|
if _, _, err := key.GetIntegerValue("CurrentMajorVersionNumber"); err != nil {
|
|
|
|
return false
|
2021-04-06 19:59:06 +00:00
|
|
|
}
|
2021-06-24 00:48:47 +00:00
|
|
|
return true
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|