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
|
|
|
|
|
2021-04-02 09:25:37 +00:00
|
|
|
//lint:file-ignore U1000 refactoring, temporarily unused code.
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
package dns
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-04-12 08:30:42 +00:00
|
|
|
"sort"
|
2021-04-11 10:37:14 +00:00
|
|
|
"time"
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
"github.com/godbus/dbus/v5"
|
2021-04-12 08:30:42 +00:00
|
|
|
"inet.af/netaddr"
|
2021-04-29 19:24:24 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2021-04-12 08:30:42 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-12-21 21:11:09 +00:00
|
|
|
"tailscale.com/util/endian"
|
2020-07-14 13:12:00 +00:00
|
|
|
)
|
|
|
|
|
2021-04-12 05:23:09 +00:00
|
|
|
const (
|
|
|
|
highestPriority = int32(-1 << 31)
|
2021-04-14 00:10:30 +00:00
|
|
|
mediumPriority = int32(1) // Highest priority that doesn't hard-override
|
2021-04-12 05:23:09 +00:00
|
|
|
lowerPriority = int32(200) // lower than all builtin auto priorities
|
|
|
|
)
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// nmManager uses the NetworkManager DBus API.
|
|
|
|
type nmManager struct {
|
|
|
|
interfaceName string
|
2021-04-12 22:51:37 +00:00
|
|
|
manager dbus.BusObject
|
|
|
|
dnsManager dbus.BusObject
|
2021-04-12 08:30:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func newNMManager(interfaceName string) (*nmManager, error) {
|
2021-04-12 08:30:42 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
2021-04-12 22:51:37 +00:00
|
|
|
return nil, err
|
2021-04-12 08:30:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
return &nmManager{
|
2021-04-02 06:26:52 +00:00
|
|
|
interfaceName: interfaceName,
|
2021-04-12 22:51:37 +00:00
|
|
|
manager: conn.Object("org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager")),
|
|
|
|
dnsManager: conn.Object("org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager/DnsManager")),
|
|
|
|
}, nil
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 20:02:52 +00:00
|
|
|
type nmConnectionSettings map[string]map[string]dbus.Variant
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func (m *nmManager) SetDNS(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()
|
|
|
|
|
2021-04-11 10:37:14 +00:00
|
|
|
// NetworkManager only lets you set DNS settings on "active"
|
|
|
|
// connections, which requires an assigned IP address. This got
|
|
|
|
// configured before the DNS manager was invoked, but it might
|
|
|
|
// take a little time for the netlink notifications to propagate
|
|
|
|
// up. So, keep retrying for the duration of the reconfigTimeout.
|
|
|
|
var err error
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
err = m.trySet(ctx, config)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func (m *nmManager) trySet(ctx context.Context, config OSConfig) error {
|
2020-07-14 13:12:00 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connecting to system bus: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is how we get at the DNS settings:
|
2020-07-18 06:58:12 +00:00
|
|
|
//
|
2020-07-14 13:12:00 +00:00
|
|
|
// org.freedesktop.NetworkManager
|
2020-07-18 06:58:12 +00:00
|
|
|
// |
|
|
|
|
// [GetDeviceByIpIface]
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// org.freedesktop.NetworkManager.Device <--------\
|
|
|
|
// (describes a network interface) |
|
|
|
|
// | |
|
|
|
|
// [GetAppliedConnection] [Reapply]
|
|
|
|
// | |
|
|
|
|
// v |
|
|
|
|
// org.freedesktop.NetworkManager.Connection |
|
|
|
|
// (connection settings) ------/
|
2020-07-14 13:12:00 +00:00
|
|
|
// contains {dns, dns-priority, dns-search}
|
|
|
|
//
|
|
|
|
// Ref: https://developer.gnome.org/NetworkManager/stable/settings-ipv4.html.
|
|
|
|
|
|
|
|
nm := conn.Object(
|
|
|
|
"org.freedesktop.NetworkManager",
|
|
|
|
dbus.ObjectPath("/org/freedesktop/NetworkManager"),
|
|
|
|
)
|
|
|
|
|
|
|
|
var devicePath dbus.ObjectPath
|
|
|
|
err = nm.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.NetworkManager.GetDeviceByIpIface", 0,
|
2020-07-31 20:27:09 +00:00
|
|
|
m.interfaceName,
|
2020-07-14 13:12:00 +00:00
|
|
|
).Store(&devicePath)
|
|
|
|
if err != nil {
|
2020-07-18 06:58:12 +00:00
|
|
|
return fmt.Errorf("getDeviceByIpIface: %w", err)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
device := conn.Object("org.freedesktop.NetworkManager", devicePath)
|
|
|
|
|
2020-07-18 06:58:12 +00:00
|
|
|
var (
|
|
|
|
settings nmConnectionSettings
|
|
|
|
version uint64
|
|
|
|
)
|
2020-07-14 13:12:00 +00:00
|
|
|
err = device.CallWithContext(
|
2020-07-18 06:58:12 +00:00
|
|
|
ctx, "org.freedesktop.NetworkManager.Device.GetAppliedConnection", 0,
|
|
|
|
uint32(0),
|
|
|
|
).Store(&settings, &version)
|
2020-07-14 13:12:00 +00:00
|
|
|
if err != nil {
|
2020-07-18 06:58:12 +00:00
|
|
|
return fmt.Errorf("getAppliedConnection: %w", err)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Frustratingly, NetworkManager represents IPv4 addresses as uint32s,
|
|
|
|
// although IPv6 addresses are represented as byte arrays.
|
|
|
|
// Perform the conversion here.
|
|
|
|
var (
|
|
|
|
dnsv4 []uint32
|
|
|
|
dnsv6 [][]byte
|
|
|
|
)
|
|
|
|
for _, ip := range config.Nameservers {
|
|
|
|
b := ip.As16()
|
|
|
|
if ip.Is4() {
|
2020-12-21 21:11:09 +00:00
|
|
|
dnsv4 = append(dnsv4, endian.Native.Uint32(b[12:]))
|
2020-07-14 13:12:00 +00:00
|
|
|
} else {
|
|
|
|
dnsv6 = append(dnsv6, b[:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:24:24 +00:00
|
|
|
// NetworkManager wipes out IPv6 address configuration unless we
|
|
|
|
// tell it explicitly to keep it. Read out the current interface
|
|
|
|
// settings and mirror them out to NetworkManager.
|
|
|
|
var addrs6 []map[string]interface{}
|
|
|
|
addrs, _, err := interfaces.Tailscale()
|
|
|
|
if err == nil {
|
|
|
|
for _, a := range addrs {
|
|
|
|
if a.Is6() {
|
|
|
|
addrs6 = append(addrs6, map[string]interface{}{
|
|
|
|
"address": a.String(),
|
|
|
|
"prefix": uint32(128),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:57:46 +00:00
|
|
|
seen := map[dnsname.FQDN]bool{}
|
|
|
|
var search []string
|
|
|
|
for _, dom := range config.SearchDomains {
|
|
|
|
if seen[dom] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[dom] = true
|
|
|
|
search = append(search, dom.WithTrailingDot())
|
|
|
|
}
|
|
|
|
for _, dom := range config.MatchDomains {
|
|
|
|
if seen[dom] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[dom] = true
|
|
|
|
search = append(search, "~"+dom.WithTrailingDot())
|
|
|
|
}
|
|
|
|
if len(config.MatchDomains) == 0 {
|
|
|
|
// Non-split routing requested, add an all-domains match.
|
|
|
|
search = append(search, "~.")
|
|
|
|
}
|
|
|
|
|
2021-05-06 21:38:48 +00:00
|
|
|
// Ideally we would like to disable LLMNR and mdns on the
|
|
|
|
// interface here, but older NetworkManagers don't understand
|
|
|
|
// those settings and choke on them, so we don't. Both LLMNR and
|
|
|
|
// mdns will fail since tailscale0 doesn't do multicast, so it's
|
|
|
|
// effectively fine. We used to try and enforce LLMNR and mdns
|
|
|
|
// settings here, but that led to #1870.
|
2021-04-12 05:23:09 +00:00
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
ipv4Map := settings["ipv4"]
|
2021-05-06 21:43:04 +00:00
|
|
|
// In some versions of NM, you can only provide DNS configuration
|
|
|
|
// if the interface is in "auto" or "manual" mode (defaults to
|
|
|
|
// "disabled").
|
|
|
|
//
|
|
|
|
// In "manual" mode we'd have to also give NM all our IP and
|
|
|
|
// routing configuration, which we don't want to do. So instead,
|
|
|
|
// we set the ipv4 mode to "auto", and then tell it to ignore any
|
|
|
|
// information it might get from DHCP (which is none, since we
|
|
|
|
// don't do DHCP on tailscale0). This is the documented way to
|
|
|
|
// provide only DNS configuration to NetworkManager.
|
|
|
|
//
|
|
|
|
// Some versions of NM don't need this, and will accept DNS
|
|
|
|
// configuration even for disabled interfaces, but there's no harm
|
|
|
|
// in telling those versions to do DHCP and ignore all results,
|
|
|
|
// hence why we have a single codepath here.
|
|
|
|
ipv4Map["method"] = dbus.MakeVariant("auto")
|
|
|
|
ipv4Map["ignore-auto-routes"] = dbus.MakeVariant(true)
|
|
|
|
ipv4Map["ignore-auto-dns"] = dbus.MakeVariant(true)
|
|
|
|
ipv4Map["never-default"] = dbus.MakeVariant(true)
|
2020-07-14 13:12:00 +00:00
|
|
|
ipv4Map["dns"] = dbus.MakeVariant(dnsv4)
|
2021-04-20 23:57:46 +00:00
|
|
|
ipv4Map["dns-search"] = dbus.MakeVariant(search)
|
2020-07-18 06:58:12 +00:00
|
|
|
// We should only request priority if we have nameservers to set.
|
|
|
|
if len(dnsv4) == 0 {
|
2021-04-12 05:23:09 +00:00
|
|
|
ipv4Map["dns-priority"] = dbus.MakeVariant(lowerPriority)
|
2021-04-14 00:10:30 +00:00
|
|
|
} else if len(config.MatchDomains) > 0 {
|
|
|
|
// Set a fairly high priority, but don't override all other
|
|
|
|
// configs when in split-DNS mode.
|
|
|
|
ipv4Map["dns-priority"] = dbus.MakeVariant(mediumPriority)
|
2020-07-18 06:58:12 +00:00
|
|
|
} else {
|
2021-04-12 05:23:09 +00:00
|
|
|
// Negative priority means only the settings from the most
|
|
|
|
// negative connection get used. The way this mixes with
|
|
|
|
// per-domain routing is unclear, but it _seems_ that the
|
|
|
|
// priority applies after routing has found possible
|
|
|
|
// candidates for a resolution.
|
|
|
|
ipv4Map["dns-priority"] = dbus.MakeVariant(highestPriority)
|
2020-07-18 06:58:12 +00:00
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
ipv6Map := settings["ipv6"]
|
2021-05-06 21:43:04 +00:00
|
|
|
// Same logic as above for IPv4 - request autoconfiguration, tell
|
|
|
|
// NM to ignore all autoconfiguration results, and provide manual
|
|
|
|
// DNS settings.
|
2020-07-14 13:12:00 +00:00
|
|
|
ipv6Map["method"] = dbus.MakeVariant("auto")
|
2021-04-29 19:24:24 +00:00
|
|
|
if len(addrs6) > 0 {
|
|
|
|
ipv6Map["address-data"] = dbus.MakeVariant(addrs6)
|
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
ipv6Map["ignore-auto-routes"] = dbus.MakeVariant(true)
|
2021-04-12 05:23:09 +00:00
|
|
|
ipv6Map["ignore-auto-dns"] = dbus.MakeVariant(true)
|
|
|
|
ipv6Map["never-default"] = dbus.MakeVariant(true)
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
ipv6Map["dns"] = dbus.MakeVariant(dnsv6)
|
2021-04-20 23:57:46 +00:00
|
|
|
ipv6Map["dns-search"] = dbus.MakeVariant(search)
|
2020-07-18 06:58:12 +00:00
|
|
|
if len(dnsv6) == 0 {
|
2021-04-12 05:23:09 +00:00
|
|
|
ipv6Map["dns-priority"] = dbus.MakeVariant(lowerPriority)
|
2021-04-14 00:10:30 +00:00
|
|
|
} else if len(config.MatchDomains) > 0 {
|
|
|
|
// Set a fairly high priority, but don't override all other
|
|
|
|
// configs when in split-DNS mode.
|
|
|
|
ipv6Map["dns-priority"] = dbus.MakeVariant(mediumPriority)
|
2020-07-18 06:58:12 +00:00
|
|
|
} else {
|
2021-04-12 05:23:09 +00:00
|
|
|
ipv6Map["dns-priority"] = dbus.MakeVariant(highestPriority)
|
2020-07-18 06:58:12 +00:00
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
// deprecatedProperties are the properties in interface settings
|
|
|
|
// that are deprecated by NetworkManager.
|
|
|
|
//
|
|
|
|
// In practice, this means that they are returned for reading,
|
|
|
|
// but submitting a settings object with them present fails
|
|
|
|
// with hard-to-diagnose errors. They must be removed.
|
|
|
|
deprecatedProperties := []string{
|
|
|
|
"addresses", "routes",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, property := range deprecatedProperties {
|
|
|
|
delete(ipv4Map, property)
|
|
|
|
delete(ipv6Map, property)
|
|
|
|
}
|
|
|
|
|
2021-04-12 05:23:09 +00:00
|
|
|
if call := device.CallWithContext(ctx, "org.freedesktop.NetworkManager.Device.Reapply", 0, settings, version, uint32(0)); call.Err != nil {
|
2021-05-06 21:46:56 +00:00
|
|
|
return fmt.Errorf("reapply: %w", call.Err)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func (m *nmManager) SupportsSplitDNS() bool {
|
|
|
|
var mode string
|
|
|
|
v, err := m.dnsManager.GetProperty("org.freedesktop.NetworkManager.DnsManager.Mode")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mode, ok := v.Value().(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Per NM's documentation, it only does split-DNS when it's
|
|
|
|
// programming dnsmasq or systemd-resolved. All other modes are
|
|
|
|
// primary-only.
|
|
|
|
return mode == "dnsmasq" || mode == "systemd-resolved"
|
|
|
|
}
|
2021-04-02 09:17:50 +00:00
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func (m *nmManager) GetBaseConfig() (OSConfig, error) {
|
2021-04-12 08:30:42 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := conn.Object("org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager/DnsManager"))
|
|
|
|
v, err := nm.GetProperty("org.freedesktop.NetworkManager.DnsManager.Configuration")
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
cfgs, ok := v.Value().([]map[string]dbus.Variant)
|
|
|
|
if !ok {
|
|
|
|
return OSConfig{}, fmt.Errorf("unexpected NM config type %T", v.Value())
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:14:43 +00:00
|
|
|
if len(cfgs) == 0 {
|
|
|
|
return OSConfig{}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:30:42 +00:00
|
|
|
type dnsPrio struct {
|
|
|
|
resolvers []netaddr.IP
|
|
|
|
domains []string
|
|
|
|
priority int32
|
|
|
|
}
|
|
|
|
order := make([]dnsPrio, 0, len(cfgs)-1)
|
|
|
|
|
|
|
|
for _, cfg := range cfgs {
|
|
|
|
if name, ok := cfg["interface"]; ok {
|
|
|
|
if s, ok := name.Value().(string); ok && s == m.interfaceName {
|
|
|
|
// Config for the taislcale interface, skip.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var p dnsPrio
|
|
|
|
|
|
|
|
if v, ok := cfg["nameservers"]; ok {
|
|
|
|
if ips, ok := v.Value().([]string); ok {
|
|
|
|
for _, s := range ips {
|
|
|
|
ip, err := netaddr.ParseIP(s)
|
|
|
|
if err != nil {
|
|
|
|
// hmm, what do? Shouldn't really happen.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.resolvers = append(p.resolvers, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := cfg["domains"]; ok {
|
|
|
|
if domains, ok := v.Value().([]string); ok {
|
|
|
|
p.domains = domains
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := cfg["priority"]; ok {
|
|
|
|
if prio, ok := v.Value().(int32); ok {
|
|
|
|
p.priority = prio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
order = append(order, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(order, func(i, j int) bool {
|
|
|
|
return order[i].priority < order[j].priority
|
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
ret OSConfig
|
|
|
|
seenResolvers = map[netaddr.IP]bool{}
|
|
|
|
seenSearch = map[string]bool{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, cfg := range order {
|
|
|
|
for _, resolver := range cfg.resolvers {
|
|
|
|
if seenResolvers[resolver] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Nameservers = append(ret.Nameservers, resolver)
|
|
|
|
seenResolvers[resolver] = true
|
|
|
|
}
|
|
|
|
for _, dom := range cfg.domains {
|
|
|
|
if seenSearch[dom] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fqdn, err := dnsname.ToFQDN(dom)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.SearchDomains = append(ret.SearchDomains, fqdn)
|
|
|
|
seenSearch[dom] = true
|
|
|
|
}
|
|
|
|
if cfg.priority < 0 {
|
|
|
|
// exclusive configurations preempt all other
|
|
|
|
// configurations, so we're done.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2021-04-07 07:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:51:37 +00:00
|
|
|
func (m *nmManager) Close() error {
|
2021-04-12 05:23:09 +00:00
|
|
|
// No need to do anything on close, NetworkManager will delete our
|
|
|
|
// settings when the tailscale interface goes away.
|
|
|
|
return nil
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|