|
|
@ -8,11 +8,14 @@ package resolver
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
@ -20,12 +23,15 @@ import (
|
|
|
|
"sync/atomic"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"go4.org/mem"
|
|
|
|
dns "golang.org/x/net/dns/dnsmessage"
|
|
|
|
dns "golang.org/x/net/dns/dnsmessage"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
|
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
"tailscale.com/types/dnstype"
|
|
|
|
"tailscale.com/types/dnstype"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/util/clientmetric"
|
|
|
|
"tailscale.com/util/clientmetric"
|
|
|
|
"tailscale.com/util/dnsname"
|
|
|
|
"tailscale.com/util/dnsname"
|
|
|
|
|
|
|
|
"tailscale.com/util/lineread"
|
|
|
|
"tailscale.com/wgengine/monitor"
|
|
|
|
"tailscale.com/wgengine/monitor"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -303,48 +309,83 @@ func (r *Resolver) NextResponse() (packet []byte, to netaddr.IPPort, err error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// parseExitNodeQuery parses a DNS request packet.
|
|
|
|
|
|
|
|
// It returns nil if it's malformed or lacking a question.
|
|
|
|
|
|
|
|
func parseExitNodeQuery(q []byte) *response {
|
|
|
|
|
|
|
|
p := dnsParserPool.Get().(*dnsParser)
|
|
|
|
|
|
|
|
defer dnsParserPool.Put(p)
|
|
|
|
|
|
|
|
p.zeroParser()
|
|
|
|
|
|
|
|
defer p.zeroParser()
|
|
|
|
|
|
|
|
if err := p.parseQuery(q); err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.response()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// HandleExitNodeDNSQuery handles a DNS query that arrived from a peer
|
|
|
|
// HandleExitNodeDNSQuery handles a DNS query that arrived from a peer
|
|
|
|
// via the peerapi's DoH server. This is only used when the local
|
|
|
|
// via the peerapi's DoH server. This is only used when the local
|
|
|
|
// node is being an exit node.
|
|
|
|
// node is being an exit node.
|
|
|
|
func (r *Resolver) HandleExitNodeDNSQuery(ctx context.Context, q []byte, from netaddr.IPPort) (res []byte, err error) {
|
|
|
|
//
|
|
|
|
metricDNSQueryForPeer.Add(1)
|
|
|
|
// The provided allowName callback is whether a DNS query for a name
|
|
|
|
|
|
|
|
// (as found by parsing q) is allowed.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// In most (all?) cases, err will be nil. A bogus DNS query q will
|
|
|
|
|
|
|
|
// still result in a response DNS packet (saying there's a failure)
|
|
|
|
|
|
|
|
// and a nil error.
|
|
|
|
|
|
|
|
// TODO: figure out if we even need an error result.
|
|
|
|
|
|
|
|
func (r *Resolver) HandleExitNodeDNSQuery(ctx context.Context, q []byte, from netaddr.IPPort, allowName func(name string) bool) (res []byte, err error) {
|
|
|
|
|
|
|
|
metricDNSExitProxyQuery.Add(1)
|
|
|
|
ch := make(chan packet, 1)
|
|
|
|
ch := make(chan packet, 1)
|
|
|
|
|
|
|
|
|
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, from}, ch)
|
|
|
|
resp := parseExitNodeQuery(q)
|
|
|
|
if err == errNoUpstreams {
|
|
|
|
if resp == nil {
|
|
|
|
// Handle to the system resolver.
|
|
|
|
return nil, errors.New("bad query")
|
|
|
|
switch runtime.GOOS {
|
|
|
|
}
|
|
|
|
case "linux":
|
|
|
|
name := resp.Question.Name.String()
|
|
|
|
// Assume for now that we don't have an upstream because
|
|
|
|
if !allowName(name) {
|
|
|
|
// they're using systemd-resolved and we're in Split DNS mode
|
|
|
|
metricDNSExitProxyErrorName.Add(1)
|
|
|
|
// where we don't know the base config.
|
|
|
|
resp.Header.RCode = dns.RCodeRefused
|
|
|
|
//
|
|
|
|
return marshalResponse(resp)
|
|
|
|
// TODO(bradfitz): this is a lazy assumption. Do better, and
|
|
|
|
}
|
|
|
|
// maybe move the HandleExitNodeDNSQuery method to the dns.Manager
|
|
|
|
|
|
|
|
// instead? But this works for now.
|
|
|
|
switch runtime.GOOS {
|
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, from}, ch, resolverAndDelay{
|
|
|
|
default:
|
|
|
|
name: dnstype.Resolver{
|
|
|
|
return nil, errors.New("unsupported exit node OS")
|
|
|
|
Addr: "127.0.0.1:53",
|
|
|
|
case "windows":
|
|
|
|
},
|
|
|
|
// TODO: use DnsQueryEx and write to ch.
|
|
|
|
})
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsqueryex.
|
|
|
|
default:
|
|
|
|
return nil, errors.New("TODO: windows exit node suport")
|
|
|
|
// TODO(bradfitz): if we're on an exit node
|
|
|
|
case "darwin":
|
|
|
|
// on, say, Windows, we need to parse the DNS
|
|
|
|
// /etc/resolv.conf is a lie and only says one upstream DNS
|
|
|
|
// packet in q and call OS-native APIs for
|
|
|
|
// but for now that's probably good enough. Later we'll
|
|
|
|
// each question. But we'll want to strip out
|
|
|
|
// want to blend in everything from scutil --dns.
|
|
|
|
// questions for MagicDNS names probably, so
|
|
|
|
fallthrough
|
|
|
|
// they don't loop back into
|
|
|
|
case "linux", "freebsd", "openbsd", "illumos":
|
|
|
|
// 100.100.100.100. We don't want to resolve
|
|
|
|
nameserver, err := stubResolverForOS()
|
|
|
|
// MagicDNS names across Tailnets once we
|
|
|
|
if err != nil {
|
|
|
|
// permit sharing exit nodes.
|
|
|
|
r.logf("stubResolverForOS: %v", err)
|
|
|
|
//
|
|
|
|
metricDNSExitProxyErrorResolvConf.Add(1)
|
|
|
|
// For now, just return an error.
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: more than 1 resolver from /etc/resolv.conf?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var resolvers []resolverAndDelay
|
|
|
|
|
|
|
|
if nameserver == tsaddr.TailscaleServiceIP() {
|
|
|
|
|
|
|
|
// If resolv.conf says 100.100.100.100, it's coming right back to us anyway
|
|
|
|
|
|
|
|
// so avoid the loop through the kernel and just do what we
|
|
|
|
|
|
|
|
// would've done anyway. By not passing any resolvers, the forwarder
|
|
|
|
|
|
|
|
// will use its default ones from our DNS config.
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
resolvers = []resolverAndDelay{{
|
|
|
|
|
|
|
|
name: dnstype.Resolver{Addr: net.JoinHostPort(nameserver.String(), "53")},
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, from}, ch, resolvers...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
metricDNSExitProxyErrorForward.Add(1)
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
metricDNSQueryForPeerError.Add(1)
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case p, ok := <-ch:
|
|
|
|
case p, ok := <-ch:
|
|
|
@ -357,6 +398,59 @@ func (r *Resolver) HandleExitNodeDNSQuery(ctx context.Context, q []byte, from ne
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type resolvConfCache struct {
|
|
|
|
|
|
|
|
mod time.Time
|
|
|
|
|
|
|
|
size int64
|
|
|
|
|
|
|
|
ip netaddr.IP
|
|
|
|
|
|
|
|
// TODO: inode/dev?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// resolvConfCacheValue contains the most recent stat metadata and parsed
|
|
|
|
|
|
|
|
// version of /etc/resolv.conf.
|
|
|
|
|
|
|
|
var resolvConfCacheValue atomic.Value // of resolvConfCache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var errEmptyResolvConf = errors.New("resolv.conf has no nameservers")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// stubResolverForOS returns the IP address of the first nameserver in
|
|
|
|
|
|
|
|
// /etc/resolv.conf.
|
|
|
|
|
|
|
|
func stubResolverForOS() (ip netaddr.IP, err error) {
|
|
|
|
|
|
|
|
fi, err := os.Stat("/etc/resolv.conf")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return netaddr.IP{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cur := resolvConfCache{
|
|
|
|
|
|
|
|
mod: fi.ModTime(),
|
|
|
|
|
|
|
|
size: fi.Size(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, ok := resolvConfCacheValue.Load().(resolvConfCache); ok && c.mod == cur.mod && c.size == cur.size {
|
|
|
|
|
|
|
|
return c.ip, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lineread.File("/etc/resolv.conf", func(line []byte) error {
|
|
|
|
|
|
|
|
if !ip.IsZero() {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
line = bytes.TrimSpace(line)
|
|
|
|
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if mem.HasPrefix(mem.B(line), mem.S("nameserver ")) {
|
|
|
|
|
|
|
|
s := strings.TrimSpace(strings.TrimPrefix(string(line), "nameserver "))
|
|
|
|
|
|
|
|
ip, err = netaddr.ParseIP(s)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return netaddr.IP{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ip.IsValid() {
|
|
|
|
|
|
|
|
return netaddr.IP{}, errEmptyResolvConf
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.ip = ip
|
|
|
|
|
|
|
|
resolvConfCacheValue.Store(cur)
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// resolveLocal returns an IP for the given domain, if domain is in
|
|
|
|
// resolveLocal returns an IP for the given domain, if domain is in
|
|
|
|
// the local hosts map and has an IP corresponding to the requested
|
|
|
|
// the local hosts map and has an IP corresponding to the requested
|
|
|
|
// typ (A, AAAA, ALL).
|
|
|
|
// typ (A, AAAA, ALL).
|
|
|
@ -538,6 +632,7 @@ func (p *dnsParser) zeroParser() { p.parser = dns.Parser{} }
|
|
|
|
// p.Question.
|
|
|
|
// p.Question.
|
|
|
|
func (p *dnsParser) parseQuery(query []byte) error {
|
|
|
|
func (p *dnsParser) parseQuery(query []byte) error {
|
|
|
|
defer p.zeroParser()
|
|
|
|
defer p.zeroParser()
|
|
|
|
|
|
|
|
p.zeroParser()
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
p.Header, err = p.parser.Start(query)
|
|
|
|
p.Header, err = p.parser.Start(query)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -837,8 +932,10 @@ var (
|
|
|
|
metricDNSMagicDNSSuccessName = clientmetric.NewCounter("dns_query_magic_success_name")
|
|
|
|
metricDNSMagicDNSSuccessName = clientmetric.NewCounter("dns_query_magic_success_name")
|
|
|
|
metricDNSMagicDNSSuccessReverse = clientmetric.NewCounter("dns_query_magic_success_reverse")
|
|
|
|
metricDNSMagicDNSSuccessReverse = clientmetric.NewCounter("dns_query_magic_success_reverse")
|
|
|
|
|
|
|
|
|
|
|
|
metricDNSQueryForPeer = clientmetric.NewCounter("dns_query_peerapi")
|
|
|
|
metricDNSExitProxyQuery = clientmetric.NewCounter("dns_exit_node_query")
|
|
|
|
metricDNSQueryForPeerError = clientmetric.NewCounter("dns_query_peerapi_error")
|
|
|
|
metricDNSExitProxyErrorName = clientmetric.NewCounter("dns_exit_node_error_name")
|
|
|
|
|
|
|
|
metricDNSExitProxyErrorForward = clientmetric.NewCounter("dns_exit_node_error_forward")
|
|
|
|
|
|
|
|
metricDNSExitProxyErrorResolvConf = clientmetric.NewCounter("dns_exit_node_error_resolvconf")
|
|
|
|
|
|
|
|
|
|
|
|
metricDNSFwd = clientmetric.NewCounter("dns_query_fwd")
|
|
|
|
metricDNSFwd = clientmetric.NewCounter("dns_query_fwd")
|
|
|
|
metricDNSFwdDropBonjour = clientmetric.NewCounter("dns_query_fwd_drop_bonjour")
|
|
|
|
metricDNSFwdDropBonjour = clientmetric.NewCounter("dns_query_fwd_drop_bonjour")
|
|
|
|