2020-07-14 13:12:00 +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.
|
|
|
|
|
|
|
|
// +build linux
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
package dns
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/interfaces"
|
|
|
|
)
|
|
|
|
|
|
|
|
// resolvedListenAddr is the listen address of the resolved stub resolver.
|
|
|
|
//
|
|
|
|
// We only consider resolved to be the system resolver if the stub resolver is;
|
|
|
|
// that is, if this address is the sole nameserver in /etc/resolved.conf.
|
2020-07-31 20:27:09 +00:00
|
|
|
// In other cases, resolved may be managing the system DNS configuration directly.
|
2020-07-14 13:12:00 +00:00
|
|
|
// Then the nameserver list will be a concatenation of those for all
|
|
|
|
// the interfaces that register their interest in being a default resolver with
|
|
|
|
// SetLinkDomains([]{{"~.", true}, ...})
|
|
|
|
// which includes at least the interface with the default route, i.e. not us.
|
|
|
|
// This does not work for us: there is a possibility of getting NXDOMAIN
|
|
|
|
// from the other nameservers before we are asked or get a chance to respond.
|
|
|
|
// We consider this case as lacking resolved support and fall through to dnsDirect.
|
|
|
|
//
|
|
|
|
// While it may seem that we need to read a config option to get at this,
|
|
|
|
// this address is, in fact, hard-coded into resolved.
|
|
|
|
var resolvedListenAddr = netaddr.IPv4(127, 0, 0, 53)
|
|
|
|
|
|
|
|
var errNotReady = errors.New("interface not ready")
|
|
|
|
|
|
|
|
type resolvedLinkNameserver struct {
|
|
|
|
Family int32
|
|
|
|
Address []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type resolvedLinkDomain struct {
|
|
|
|
Domain string
|
|
|
|
RoutingOnly bool
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// isResolvedActive determines if resolved is currently managing system DNS settings.
|
|
|
|
func isResolvedActive() bool {
|
2020-07-14 13:12:00 +00:00
|
|
|
// systemd-resolved is never installed without systemd.
|
|
|
|
_, err := exec.LookPath("systemctl")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// is-active exits with code 3 if the service is not active.
|
|
|
|
err = exec.Command("systemctl", "is-active", "systemd-resolved").Run()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
config, err := readResolvConf()
|
2020-07-14 13:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sole nameserver must be the systemd-resolved stub.
|
|
|
|
if len(config.Nameservers) == 1 && config.Nameservers[0] == resolvedListenAddr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// resolvedManager uses the systemd-resolved DBus API.
|
|
|
|
type resolvedManager struct{}
|
|
|
|
|
2021-04-02 06:26:52 +00:00
|
|
|
func newResolvedManager() managerImpl {
|
2020-07-31 20:27:09 +00:00
|
|
|
return resolvedManager{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up implements managerImpl.
|
2021-04-02 06:42:58 +00:00
|
|
|
func (m resolvedManager) Up(config OSConfig) error {
|
2020-07-31 20:27:09 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), reconfigTimeout)
|
2020-07-14 13:12:00 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-07-18 06:58:12 +00:00
|
|
|
// conn is a shared connection whose lifecycle is managed by the dbus package.
|
|
|
|
// We should not interfere with that by closing it.
|
2020-07-14 13:12:00 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connecting to system bus: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved := conn.Object(
|
|
|
|
"org.freedesktop.resolve1",
|
|
|
|
dbus.ObjectPath("/org/freedesktop/resolve1"),
|
|
|
|
)
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// In principle, we could persist this in the manager struct
|
|
|
|
// if we knew that interface indices are persistent. This does not seem to be the case.
|
2020-07-14 13:12:00 +00:00
|
|
|
_, iface, err := interfaces.Tailscale()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting interface index: %w", err)
|
|
|
|
}
|
|
|
|
if iface == nil {
|
|
|
|
return errNotReady
|
|
|
|
}
|
|
|
|
|
|
|
|
var linkNameservers = make([]resolvedLinkNameserver, len(config.Nameservers))
|
|
|
|
for i, server := range config.Nameservers {
|
|
|
|
ip := server.As16()
|
|
|
|
if server.Is4() {
|
|
|
|
linkNameservers[i] = resolvedLinkNameserver{
|
|
|
|
Family: unix.AF_INET,
|
|
|
|
Address: ip[12:],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
linkNameservers[i] = resolvedLinkNameserver{
|
|
|
|
Family: unix.AF_INET6,
|
|
|
|
Address: ip[:],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resolved.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.resolve1.Manager.SetLinkDNS", 0,
|
|
|
|
iface.Index, linkNameservers,
|
|
|
|
).Store()
|
|
|
|
if err != nil {
|
2020-07-31 20:27:09 +00:00
|
|
|
return fmt.Errorf("setLinkDNS: %w", err)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var linkDomains = make([]resolvedLinkDomain, len(config.Domains))
|
|
|
|
for i, domain := range config.Domains {
|
|
|
|
linkDomains[i] = resolvedLinkDomain{
|
|
|
|
Domain: domain,
|
|
|
|
RoutingOnly: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resolved.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.resolve1.Manager.SetLinkDomains", 0,
|
|
|
|
iface.Index, linkDomains,
|
|
|
|
).Store()
|
|
|
|
if err != nil {
|
2020-07-31 20:27:09 +00:00
|
|
|
return fmt.Errorf("setLinkDomains: %w", err)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// Down implements managerImpl.
|
|
|
|
func (m resolvedManager) Down() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), reconfigTimeout)
|
2020-07-14 13:12:00 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-07-18 06:58:12 +00:00
|
|
|
// conn is a shared connection whose lifecycle is managed by the dbus package.
|
|
|
|
// We should not interfere with that by closing it.
|
2020-07-14 13:12:00 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connecting to system bus: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved := conn.Object(
|
|
|
|
"org.freedesktop.resolve1",
|
|
|
|
dbus.ObjectPath("/org/freedesktop/resolve1"),
|
|
|
|
)
|
|
|
|
|
|
|
|
_, iface, err := interfaces.Tailscale()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting interface index: %w", err)
|
|
|
|
}
|
|
|
|
if iface == nil {
|
|
|
|
return errNotReady
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resolved.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.resolve1.Manager.RevertLink", 0,
|
|
|
|
iface.Index,
|
|
|
|
).Store()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RevertLink: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|