2020-06-08 22:19:26 +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.
|
|
|
|
|
2021-04-01 06:42:28 +00:00
|
|
|
// Package resolver implements a stub DNS resolver that can also serve
|
|
|
|
// records out of an internal local zone.
|
2021-04-01 04:54:38 +00:00
|
|
|
package resolver
|
2020-06-08 22:19:26 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-20 05:24:43 +00:00
|
|
|
"bufio"
|
2021-11-23 17:58:34 +00:00
|
|
|
"context"
|
2020-08-06 18:25:28 +00:00
|
|
|
"encoding/hex"
|
2020-06-08 22:19:26 +00:00
|
|
|
"errors"
|
2021-07-20 05:24:43 +00:00
|
|
|
"fmt"
|
2021-08-03 13:56:31 +00:00
|
|
|
"io"
|
2021-06-23 04:53:43 +00:00
|
|
|
"runtime"
|
2021-04-01 08:33:58 +00:00
|
|
|
"sort"
|
2020-08-20 22:54:18 +00:00
|
|
|
"strings"
|
2020-06-08 22:19:26 +00:00
|
|
|
"sync"
|
2021-06-23 04:53:43 +00:00
|
|
|
"sync/atomic"
|
2020-06-09 17:09:43 +00:00
|
|
|
"time"
|
2020-06-08 22:19:26 +00:00
|
|
|
|
|
|
|
dns "golang.org/x/net/dns/dnsmessage"
|
|
|
|
"inet.af/netaddr"
|
2021-08-03 13:56:31 +00:00
|
|
|
"tailscale.com/types/dnstype"
|
2020-06-08 22:19:26 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-11-26 22:43:38 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
2021-01-10 20:03:01 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2021-03-12 16:39:43 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-06-08 22:19:26 +00:00
|
|
|
)
|
|
|
|
|
2021-06-07 21:16:07 +00:00
|
|
|
// maxResponseBytes is the maximum size of a response from a Resolver. The
|
|
|
|
// actual buffer size will be one larger than this so that we can detect
|
|
|
|
// truncation in a platform-agnostic way.
|
|
|
|
const maxResponseBytes = 4095
|
2020-07-07 19:25:32 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
// maxActiveQueries returns the maximal number of DNS requests that be
|
|
|
|
// can running.
|
2020-07-07 19:25:32 +00:00
|
|
|
// If EnqueueRequest is called when this many requests are already pending,
|
|
|
|
// the request will be dropped to avoid blocking the caller.
|
2021-06-23 04:53:43 +00:00
|
|
|
func maxActiveQueries() int32 {
|
|
|
|
if runtime.GOOS == "ios" {
|
|
|
|
// For memory paranoia reasons on iOS, match the
|
|
|
|
// historical Tailscale 1.x..1.8 behavior for now
|
|
|
|
// (just before the 1.10 release).
|
|
|
|
return 64
|
|
|
|
}
|
|
|
|
// But for other platforms, allow more burstiness:
|
|
|
|
return 256
|
|
|
|
}
|
2020-07-07 19:25:32 +00:00
|
|
|
|
2020-06-09 17:09:43 +00:00
|
|
|
// defaultTTL is the TTL of all responses from Resolver.
|
|
|
|
const defaultTTL = 600 * time.Second
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// ErrClosed indicates that the resolver has been closed and readers should exit.
|
|
|
|
var ErrClosed = errors.New("closed")
|
|
|
|
|
2020-06-08 22:19:26 +00:00
|
|
|
var (
|
2021-04-01 08:33:58 +00:00
|
|
|
errFullQueue = errors.New("request queue full")
|
|
|
|
errNotQuery = errors.New("not a DNS query")
|
|
|
|
errNotOurName = errors.New("not a Tailscale DNS name")
|
2020-06-08 22:19:26 +00:00
|
|
|
)
|
|
|
|
|
2021-04-01 06:06:47 +00:00
|
|
|
type packet struct {
|
|
|
|
bs []byte
|
|
|
|
addr netaddr.IPPort // src for a request, dst for a response
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 08:33:58 +00:00
|
|
|
// Config is a resolver configuration.
|
|
|
|
// Given a Config, queries are resolved in the following order:
|
|
|
|
// If the query is an exact match for an entry in LocalHosts, return that.
|
|
|
|
// Else if the query suffix matches an entry in LocalDomains, return NXDOMAIN.
|
|
|
|
// Else forward the query to the most specific matching entry in Routes.
|
|
|
|
// Else return SERVFAIL.
|
|
|
|
type Config struct {
|
|
|
|
// Routes is a map of DNS name suffix to the resolvers to use for
|
|
|
|
// queries within that suffix.
|
|
|
|
// Queries only match the most specific suffix.
|
|
|
|
// To register a "default route", add an entry for ".".
|
2021-08-03 13:56:31 +00:00
|
|
|
Routes map[dnsname.FQDN][]dnstype.Resolver
|
2021-04-01 08:33:58 +00:00
|
|
|
// LocalHosts is a map of FQDNs to corresponding IPs.
|
2021-04-09 22:24:47 +00:00
|
|
|
Hosts map[dnsname.FQDN][]netaddr.IP
|
2021-04-01 08:33:58 +00:00
|
|
|
// LocalDomains is a list of DNS name suffixes that should not be
|
|
|
|
// routed to upstream resolvers.
|
2021-04-09 22:24:47 +00:00
|
|
|
LocalDomains []dnsname.FQDN
|
2021-04-01 08:33:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:24:43 +00:00
|
|
|
// WriteToBufioWriter write a debug version of c for logs to w, omitting
|
|
|
|
// spammy stuff like *.arpa entries and replacing it with a total count.
|
|
|
|
func (c *Config) WriteToBufioWriter(w *bufio.Writer) {
|
|
|
|
w.WriteString("{Routes:")
|
|
|
|
WriteRoutes(w, c.Routes)
|
|
|
|
fmt.Fprintf(w, " Hosts:%v LocalDomains:[", len(c.Hosts))
|
|
|
|
space := false
|
|
|
|
arpa := 0
|
|
|
|
for _, d := range c.LocalDomains {
|
|
|
|
if strings.HasSuffix(string(d), ".arpa.") {
|
|
|
|
arpa++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if space {
|
|
|
|
w.WriteByte(' ')
|
|
|
|
}
|
|
|
|
w.WriteString(string(d))
|
|
|
|
space = true
|
|
|
|
}
|
|
|
|
w.WriteString("]")
|
|
|
|
if arpa > 0 {
|
|
|
|
fmt.Fprintf(w, "+%darpa", arpa)
|
|
|
|
}
|
|
|
|
w.WriteString("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteIPPorts writes vv to w.
|
|
|
|
func WriteIPPorts(w *bufio.Writer, vv []netaddr.IPPort) {
|
|
|
|
w.WriteByte('[')
|
|
|
|
var b []byte
|
|
|
|
for i, v := range vv {
|
|
|
|
if i > 0 {
|
|
|
|
w.WriteByte(' ')
|
|
|
|
}
|
|
|
|
b = v.AppendTo(b[:0])
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
w.WriteByte(']')
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:56:31 +00:00
|
|
|
// WriteDNSResolver writes r to w.
|
|
|
|
func WriteDNSResolver(w *bufio.Writer, r dnstype.Resolver) {
|
|
|
|
io.WriteString(w, r.Addr)
|
|
|
|
if len(r.BootstrapResolution) > 0 {
|
|
|
|
w.WriteByte('(')
|
|
|
|
var b []byte
|
|
|
|
for _, ip := range r.BootstrapResolution {
|
|
|
|
ip.AppendTo(b[:0])
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
w.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDNSResolvers writes resolvers to w.
|
|
|
|
func WriteDNSResolvers(w *bufio.Writer, resolvers []dnstype.Resolver) {
|
|
|
|
w.WriteByte('[')
|
|
|
|
for i, r := range resolvers {
|
|
|
|
if i > 0 {
|
|
|
|
w.WriteByte(' ')
|
|
|
|
}
|
|
|
|
WriteDNSResolver(w, r)
|
|
|
|
}
|
|
|
|
w.WriteByte(']')
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:24:43 +00:00
|
|
|
// WriteRoutes writes routes to w, omitting *.arpa routes and instead
|
|
|
|
// summarizing how many of them there were.
|
2021-08-03 13:56:31 +00:00
|
|
|
func WriteRoutes(w *bufio.Writer, routes map[dnsname.FQDN][]dnstype.Resolver) {
|
2021-07-20 05:24:43 +00:00
|
|
|
var kk []dnsname.FQDN
|
|
|
|
arpa := 0
|
|
|
|
for k := range routes {
|
|
|
|
if strings.HasSuffix(string(k), ".arpa.") {
|
|
|
|
arpa++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
kk = append(kk, k)
|
|
|
|
}
|
|
|
|
sort.Slice(kk, func(i, j int) bool { return kk[i] < kk[j] })
|
|
|
|
w.WriteByte('{')
|
|
|
|
for i, k := range kk {
|
|
|
|
if i > 0 {
|
|
|
|
w.WriteByte(' ')
|
|
|
|
}
|
|
|
|
w.WriteString(string(k))
|
|
|
|
w.WriteByte(':')
|
2021-08-03 13:56:31 +00:00
|
|
|
WriteDNSResolvers(w, routes[k])
|
2021-07-20 05:24:43 +00:00
|
|
|
}
|
|
|
|
w.WriteByte('}')
|
|
|
|
if arpa > 0 {
|
|
|
|
fmt.Fprintf(w, "+%darpa", arpa)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// Resolver is a DNS resolver for nodes on the Tailscale network,
|
|
|
|
// associating them with domain names of the form <mynode>.<mydomain>.<root>.
|
|
|
|
// If it is asked to resolve a domain that is not of that form,
|
|
|
|
// it delegates to upstream nameservers if any are set.
|
2020-06-08 22:19:26 +00:00
|
|
|
type Resolver struct {
|
2021-04-07 05:09:55 +00:00
|
|
|
logf logger.Logf
|
|
|
|
linkMon *monitor.Mon // or nil
|
|
|
|
saveConfigForTests func(cfg Config) // used in tests to capture resolver config
|
2020-08-24 21:27:21 +00:00
|
|
|
// forwarder forwards requests to upstream nameservers.
|
2020-08-19 19:39:25 +00:00
|
|
|
forwarder *forwarder
|
2020-07-07 19:25:32 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
activeQueriesAtomic int32 // number of DNS queries in flight
|
|
|
|
|
2020-08-19 19:39:25 +00:00
|
|
|
// responses is an unbuffered channel to which responses are returned.
|
2021-04-01 06:06:47 +00:00
|
|
|
responses chan packet
|
2020-08-19 19:39:25 +00:00
|
|
|
// errors is an unbuffered channel to which errors are returned.
|
2020-07-07 19:25:32 +00:00
|
|
|
errors chan error
|
2020-08-19 19:39:25 +00:00
|
|
|
// closed signals all goroutines to stop.
|
2020-07-07 19:25:32 +00:00
|
|
|
closed chan struct{}
|
2020-08-19 19:39:25 +00:00
|
|
|
// wg signals when all goroutines have stopped.
|
|
|
|
wg sync.WaitGroup
|
2020-06-08 22:19:26 +00:00
|
|
|
|
|
|
|
// mu guards the following fields from being updated while used.
|
2021-04-01 08:33:58 +00:00
|
|
|
mu sync.Mutex
|
2021-04-09 22:24:47 +00:00
|
|
|
localDomains []dnsname.FQDN
|
|
|
|
hostToIP map[dnsname.FQDN][]netaddr.IP
|
|
|
|
ipToHost map[netaddr.IP]dnsname.FQDN
|
2020-08-19 19:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
type ForwardLinkSelector interface {
|
|
|
|
// PickLink returns which network device should be used to query
|
|
|
|
// the DNS server at the given IP.
|
|
|
|
// The empty string means to use an unspecified default.
|
|
|
|
PickLink(netaddr.IP) (linkName string)
|
|
|
|
}
|
|
|
|
|
2021-04-01 05:13:23 +00:00
|
|
|
// New returns a new resolver.
|
|
|
|
// linkMon optionally specifies a link monitor to use for socket rebinding.
|
2021-06-23 04:53:43 +00:00
|
|
|
func New(logf logger.Logf, linkMon *monitor.Mon, linkSel ForwardLinkSelector) *Resolver {
|
2020-06-08 22:19:26 +00:00
|
|
|
r := &Resolver{
|
2021-10-07 19:19:39 +00:00
|
|
|
logf: logger.WithPrefix(logf, "resolver: "),
|
2021-04-01 05:13:23 +00:00
|
|
|
linkMon: linkMon,
|
2021-04-01 06:06:47 +00:00
|
|
|
responses: make(chan packet),
|
2020-08-24 21:27:21 +00:00
|
|
|
errors: make(chan error),
|
|
|
|
closed: make(chan struct{}),
|
2021-04-09 22:24:47 +00:00
|
|
|
hostToIP: map[dnsname.FQDN][]netaddr.IP{},
|
|
|
|
ipToHost: map[netaddr.IP]dnsname.FQDN{},
|
2020-08-19 19:39:25 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
r.forwarder = newForwarder(r.logf, r.responses, linkMon, linkSel)
|
2021-04-03 02:42:05 +00:00
|
|
|
return r
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 05:09:55 +00:00
|
|
|
func (r *Resolver) TestOnlySetHook(hook func(Config)) { r.saveConfigForTests = hook }
|
|
|
|
|
2021-04-01 08:33:58 +00:00
|
|
|
func (r *Resolver) SetConfig(cfg Config) error {
|
2021-04-07 05:09:55 +00:00
|
|
|
if r.saveConfigForTests != nil {
|
|
|
|
r.saveConfigForTests(cfg)
|
|
|
|
}
|
|
|
|
|
2021-04-09 22:24:47 +00:00
|
|
|
reverse := make(map[netaddr.IP]dnsname.FQDN, len(cfg.Hosts))
|
2021-04-01 08:33:58 +00:00
|
|
|
|
|
|
|
for host, ips := range cfg.Hosts {
|
|
|
|
for _, ip := range ips {
|
|
|
|
reverse[ip] = host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 03:40:37 +00:00
|
|
|
r.forwarder.setRoutes(cfg.Routes)
|
2021-04-01 08:33:58 +00:00
|
|
|
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.localDomains = cfg.LocalDomains
|
|
|
|
r.hostToIP = cfg.Hosts
|
|
|
|
r.ipToHost = reverse
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// Close shuts down the resolver and ensures poll goroutines have exited.
|
|
|
|
// The Resolver cannot be used again after Close is called.
|
|
|
|
func (r *Resolver) Close() {
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
close(r.closed)
|
2020-08-19 19:39:25 +00:00
|
|
|
|
2021-04-01 05:13:23 +00:00
|
|
|
r.forwarder.Close()
|
2021-03-12 16:39:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// EnqueueRequest places the given DNS request in the resolver's queue.
|
|
|
|
// It takes ownership of the payload and does not block.
|
|
|
|
// If the queue is full, the request will be dropped and an error will be returned.
|
2021-04-01 06:06:47 +00:00
|
|
|
func (r *Resolver) EnqueueRequest(bs []byte, from netaddr.IPPort) error {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSQueryLocal.Add(1)
|
2020-07-07 19:25:32 +00:00
|
|
|
select {
|
2020-08-19 19:39:25 +00:00
|
|
|
case <-r.closed:
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSQueryErrorClosed.Add(1)
|
2020-08-19 19:39:25 +00:00
|
|
|
return ErrClosed
|
2020-07-07 19:25:32 +00:00
|
|
|
default:
|
2021-06-23 04:53:43 +00:00
|
|
|
}
|
|
|
|
if n := atomic.AddInt32(&r.activeQueriesAtomic, 1); n > maxActiveQueries() {
|
|
|
|
atomic.AddInt32(&r.activeQueriesAtomic, -1)
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSQueryErrorQueue.Add(1)
|
2020-07-07 19:25:32 +00:00
|
|
|
return errFullQueue
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
go r.handleQuery(packet{bs, from})
|
|
|
|
return nil
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// NextResponse returns a DNS response to a previously enqueued request.
|
|
|
|
// It blocks until a response is available and gives up ownership of the response payload.
|
2021-04-01 06:06:47 +00:00
|
|
|
func (r *Resolver) NextResponse() (packet []byte, to netaddr.IPPort, err error) {
|
2020-07-07 19:25:32 +00:00
|
|
|
select {
|
2020-08-19 19:39:25 +00:00
|
|
|
case <-r.closed:
|
2021-04-01 06:06:47 +00:00
|
|
|
return nil, netaddr.IPPort{}, ErrClosed
|
2020-07-07 19:25:32 +00:00
|
|
|
case resp := <-r.responses:
|
2021-04-01 06:06:47 +00:00
|
|
|
return resp.bs, resp.addr, nil
|
2020-07-07 19:25:32 +00:00
|
|
|
case err := <-r.errors:
|
2021-04-01 06:06:47 +00:00
|
|
|
return nil, netaddr.IPPort{}, err
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 17:58:34 +00:00
|
|
|
// HandleExitNodeDNSQuery handles a DNS query that arrived from a peer
|
|
|
|
// via the peerapi's DoH server. This is only used when the local
|
|
|
|
// node is being an exit node.
|
|
|
|
func (r *Resolver) HandleExitNodeDNSQuery(ctx context.Context, q []byte, from netaddr.IPPort) (res []byte, err error) {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSQueryForPeer.Add(1)
|
2021-11-23 17:58:34 +00:00
|
|
|
ch := make(chan packet, 1)
|
|
|
|
|
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, from}, ch)
|
|
|
|
if err == errNoUpstreams {
|
|
|
|
// Handle to the system resolver.
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
// Assume for now that we don't have an upstream because
|
|
|
|
// they're using systemd-resolved and we're in Split DNS mode
|
|
|
|
// where we don't know the base config.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, from}, ch, resolverAndDelay{
|
|
|
|
name: dnstype.Resolver{
|
|
|
|
Addr: "127.0.0.1:53",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
// TODO(bradfitz): if we're on an exit node
|
|
|
|
// on, say, Windows, we need to parse the DNS
|
|
|
|
// packet in q and call OS-native APIs for
|
|
|
|
// each question. But we'll want to strip out
|
|
|
|
// questions for MagicDNS names probably, so
|
|
|
|
// they don't loop back into
|
|
|
|
// 100.100.100.100. We don't want to resolve
|
|
|
|
// MagicDNS names across Tailnets once we
|
|
|
|
// permit sharing exit nodes.
|
|
|
|
//
|
|
|
|
// For now, just return an error.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSQueryForPeerError.Add(1)
|
2021-11-23 17:58:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case p, ok := <-ch:
|
|
|
|
if ok {
|
|
|
|
return p.bs, nil
|
|
|
|
}
|
|
|
|
panic("unexpected close chan")
|
|
|
|
default:
|
|
|
|
panic("unexpected unreadable chan")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:33:58 +00:00
|
|
|
// resolveLocal returns an IP for the given domain, if domain is in
|
|
|
|
// the local hosts map and has an IP corresponding to the requested
|
|
|
|
// typ (A, AAAA, ALL).
|
|
|
|
// Returns dns.RCodeRefused to indicate that the local map is not
|
|
|
|
// authoritative for domain.
|
2021-04-09 22:24:47 +00:00
|
|
|
func (r *Resolver) resolveLocal(domain dnsname.FQDN, typ dns.Type) (netaddr.IP, dns.RCode) {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocal.Add(1)
|
2021-02-26 16:16:12 +00:00
|
|
|
// Reject .onion domains per RFC 7686.
|
2021-04-09 22:24:47 +00:00
|
|
|
if dnsname.HasSuffix(domain.WithoutTrailingDot(), ".onion") {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalErrorOnion.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeNameError
|
2021-02-26 16:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 08:33:58 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
hosts := r.hostToIP
|
|
|
|
localDomains := r.localDomains
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
addrs, found := hosts[domain]
|
2020-06-08 22:19:26 +00:00
|
|
|
if !found {
|
2021-04-01 08:33:58 +00:00
|
|
|
for _, suffix := range localDomains {
|
2021-04-09 22:24:47 +00:00
|
|
|
if suffix.Contains(domain) {
|
2021-04-01 08:33:58 +00:00
|
|
|
// We are authoritative for the queried domain.
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalErrorMissing.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeNameError
|
|
|
|
}
|
2021-01-14 22:48:00 +00:00
|
|
|
}
|
2021-04-01 08:33:58 +00:00
|
|
|
// Not authoritative, signal that forwarding is advisable.
|
|
|
|
return netaddr.IP{}, dns.RCodeRefused
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2020-08-27 04:07:15 +00:00
|
|
|
|
2020-08-27 04:40:30 +00:00
|
|
|
// Refactoring note: this must happen after we check suffixes,
|
|
|
|
// otherwise we will respond with NOTIMP to requests that should be forwarded.
|
2021-04-01 08:33:58 +00:00
|
|
|
//
|
|
|
|
// DNS semantics subtlety: when a DNS name exists, but no records
|
|
|
|
// are available for the requested record type, we must return
|
|
|
|
// RCodeSuccess with no data, not NXDOMAIN.
|
|
|
|
switch typ {
|
2021-01-08 22:14:10 +00:00
|
|
|
case dns.TypeA:
|
2021-04-01 08:33:58 +00:00
|
|
|
for _, ip := range addrs {
|
|
|
|
if ip.Is4() {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalOKA.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return ip, dns.RCodeSuccess
|
|
|
|
}
|
2020-08-27 04:40:30 +00:00
|
|
|
}
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalNoA.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeSuccess
|
2021-01-08 22:14:10 +00:00
|
|
|
case dns.TypeAAAA:
|
2021-04-01 08:33:58 +00:00
|
|
|
for _, ip := range addrs {
|
|
|
|
if ip.Is6() {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalOKAAAA.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return ip, dns.RCodeSuccess
|
|
|
|
}
|
2020-08-27 04:40:30 +00:00
|
|
|
}
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalNoAAAA.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeSuccess
|
2021-01-08 22:14:10 +00:00
|
|
|
case dns.TypeALL:
|
|
|
|
// Answer with whatever we've got.
|
|
|
|
// It could be IPv4, IPv6, or a zero addr.
|
|
|
|
// TODO: Return all available resolutions (A and AAAA, if we have them).
|
2021-04-01 08:33:58 +00:00
|
|
|
if len(addrs) == 0 {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalNoAll.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeSuccess
|
|
|
|
}
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveLocalOKAll.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return addrs[0], dns.RCodeSuccess
|
2020-12-31 22:31:33 +00:00
|
|
|
|
|
|
|
// Leave some some record types explicitly unimplemented.
|
|
|
|
// These types relate to recursive resolution or special
|
2021-04-01 08:33:58 +00:00
|
|
|
// DNS semantics and might be implemented in the future.
|
2020-12-31 22:31:33 +00:00
|
|
|
case dns.TypeNS, dns.TypeSOA, dns.TypeAXFR, dns.TypeHINFO:
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveNotImplType.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
return netaddr.IP{}, dns.RCodeNotImplemented
|
2020-12-31 22:31:33 +00:00
|
|
|
|
2021-08-24 14:36:48 +00:00
|
|
|
// For everything except for the few types above that are explicitly not implemented, return no records.
|
2020-12-31 22:31:33 +00:00
|
|
|
// This is what other DNS systems do: always return NOERROR
|
|
|
|
// without any records whenever the requested record type is unknown.
|
|
|
|
// You can try this with:
|
|
|
|
// dig -t TYPE9824 example.com
|
|
|
|
// and note that NOERROR is returned, despite that record type being made up.
|
|
|
|
default:
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSResolveNoRecordType.Add(1)
|
2021-04-01 08:33:58 +00:00
|
|
|
// The name exists, but no records exist of the requested type.
|
|
|
|
return netaddr.IP{}, dns.RCodeSuccess
|
2020-08-27 04:07:15 +00:00
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 06:35:26 +00:00
|
|
|
// resolveReverse returns the unique domain name that maps to the given address.
|
2021-09-02 00:25:02 +00:00
|
|
|
func (r *Resolver) resolveLocalReverse(name dnsname.FQDN) (dnsname.FQDN, dns.RCode) {
|
|
|
|
var ip netaddr.IP
|
|
|
|
var ok bool
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(name.WithTrailingDot(), rdnsv4Suffix):
|
|
|
|
ip, ok = rdnsNameToIPv4(name)
|
|
|
|
case strings.HasSuffix(name.WithTrailingDot(), rdnsv6Suffix):
|
|
|
|
ip, ok = rdnsNameToIPv6(name)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// This isn't a well-formed in-addr.arpa or ip6.arpa name, but
|
|
|
|
// who knows what upstreams might do, try kicking it up to
|
|
|
|
// them. We definitely won't handle it.
|
|
|
|
return "", dns.RCodeRefused
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:25:28 +00:00
|
|
|
r.mu.Lock()
|
2021-06-23 04:53:43 +00:00
|
|
|
defer r.mu.Unlock()
|
2021-09-02 00:25:02 +00:00
|
|
|
ret, ok := r.ipToHost[ip]
|
2021-06-23 04:53:43 +00:00
|
|
|
if !ok {
|
2021-09-02 00:25:02 +00:00
|
|
|
for _, suffix := range r.localDomains {
|
|
|
|
if suffix.Contains(name) {
|
|
|
|
// We are authoritative for this chunk of IP space.
|
|
|
|
return "", dns.RCodeNameError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not authoritative, signal that forwarding is advisable.
|
|
|
|
return "", dns.RCodeRefused
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
2021-09-02 00:25:02 +00:00
|
|
|
return ret, dns.RCodeSuccess
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
func (r *Resolver) handleQuery(pkt packet) {
|
|
|
|
defer atomic.AddInt32(&r.activeQueriesAtomic, -1)
|
2020-07-07 19:25:32 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
out, err := r.respond(pkt.bs)
|
|
|
|
if err == errNotOurName {
|
|
|
|
err = r.forwarder.forward(pkt)
|
|
|
|
if err == nil {
|
|
|
|
// forward will send response into r.responses, nothing to do.
|
2020-07-07 19:25:32 +00:00
|
|
|
return
|
2020-08-19 19:39:25 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
case r.errors <- err:
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
case r.responses <- packet{out, pkt.addr}:
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:19:26 +00:00
|
|
|
type response struct {
|
2020-07-07 19:25:32 +00:00
|
|
|
Header dns.Header
|
|
|
|
Question dns.Question
|
2020-08-06 18:25:28 +00:00
|
|
|
// Name is the response to a PTR query.
|
2021-04-09 22:24:47 +00:00
|
|
|
Name dnsname.FQDN
|
2020-08-27 04:40:30 +00:00
|
|
|
// IP is the response to an A, AAAA, or ALL query.
|
2020-08-06 18:25:28 +00:00
|
|
|
IP netaddr.IP
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
var dnsParserPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(dnsParser)
|
|
|
|
},
|
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
// dnsParser parses DNS queries using x/net/dns/dnsmessage.
|
|
|
|
// These structs are pooled with dnsParserPool.
|
|
|
|
type dnsParser struct {
|
|
|
|
Header dns.Header
|
|
|
|
Question dns.Question
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
parser dns.Parser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *dnsParser) response() *response {
|
|
|
|
return &response{Header: p.Header, Question: p.Question}
|
|
|
|
}
|
|
|
|
|
|
|
|
// zeroParser clears parser so it doesn't retain its most recently
|
|
|
|
// parsed DNS query's []byte while it's sitting in a sync.Pool.
|
|
|
|
// It's not useful to keep anyway: the next Start will do the same.
|
|
|
|
func (p *dnsParser) zeroParser() { p.parser = dns.Parser{} }
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
// parseQuery parses the query in given packet into p.Header and
|
|
|
|
// p.Question.
|
|
|
|
func (p *dnsParser) parseQuery(query []byte) error {
|
|
|
|
defer p.zeroParser()
|
|
|
|
var err error
|
|
|
|
p.Header, err = p.parser.Start(query)
|
2020-06-08 22:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
if p.Header.Response {
|
|
|
|
return errNotQuery
|
|
|
|
}
|
|
|
|
p.Question, err = p.parser.Question()
|
|
|
|
return err
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// marshalARecord serializes an A record into an active builder.
|
2020-06-09 17:09:43 +00:00
|
|
|
// The caller may continue using the builder following the call.
|
2020-07-07 19:25:32 +00:00
|
|
|
func marshalARecord(name dns.Name, ip netaddr.IP, builder *dns.Builder) error {
|
2020-06-08 22:19:26 +00:00
|
|
|
var answer dns.AResource
|
|
|
|
|
|
|
|
answerHeader := dns.ResourceHeader{
|
2020-07-07 19:25:32 +00:00
|
|
|
Name: name,
|
2020-06-08 22:19:26 +00:00
|
|
|
Type: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
2020-06-09 17:09:43 +00:00
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2020-07-07 19:25:32 +00:00
|
|
|
ipbytes := ip.As4()
|
|
|
|
copy(answer.A[:], ipbytes[:])
|
2020-06-08 22:19:26 +00:00
|
|
|
return builder.AResource(answerHeader, answer)
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// marshalAAAARecord serializes an AAAA record into an active builder.
|
2020-06-09 17:09:43 +00:00
|
|
|
// The caller may continue using the builder following the call.
|
2020-07-07 19:25:32 +00:00
|
|
|
func marshalAAAARecord(name dns.Name, ip netaddr.IP, builder *dns.Builder) error {
|
|
|
|
var answer dns.AAAAResource
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
answerHeader := dns.ResourceHeader{
|
|
|
|
Name: name,
|
|
|
|
Type: dns.TypeAAAA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2020-07-07 19:25:32 +00:00
|
|
|
ipbytes := ip.As16()
|
|
|
|
copy(answer.AAAA[:], ipbytes[:])
|
|
|
|
return builder.AAAAResource(answerHeader, answer)
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:25:28 +00:00
|
|
|
// marshalPTRRecord serializes a PTR record into an active builder.
|
|
|
|
// The caller may continue using the builder following the call.
|
2021-04-09 22:24:47 +00:00
|
|
|
func marshalPTRRecord(queryName dns.Name, name dnsname.FQDN, builder *dns.Builder) error {
|
2020-08-06 18:25:28 +00:00
|
|
|
var answer dns.PTRResource
|
|
|
|
var err error
|
|
|
|
|
|
|
|
answerHeader := dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}
|
2021-04-09 22:24:47 +00:00
|
|
|
answer.PTR, err = dns.NewName(name.WithTrailingDot())
|
2020-08-06 18:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return builder.PTRResource(answerHeader, answer)
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// marshalResponse serializes the DNS response into a new buffer.
|
|
|
|
func marshalResponse(resp *response) ([]byte, error) {
|
2020-06-09 17:09:43 +00:00
|
|
|
resp.Header.Response = true
|
|
|
|
resp.Header.Authoritative = true
|
|
|
|
if resp.Header.RecursionDesired {
|
|
|
|
resp.Header.RecursionAvailable = true
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
builder := dns.NewBuilder(nil, resp.Header)
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2020-12-17 06:14:36 +00:00
|
|
|
isSuccess := resp.Header.RCode == dns.RCodeSuccess
|
2020-06-09 17:09:43 +00:00
|
|
|
|
2020-12-17 06:14:36 +00:00
|
|
|
if resp.Question.Type != 0 || isSuccess {
|
|
|
|
err := builder.StartQuestions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = builder.Question(resp.Question)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
// Only successful responses contain answers.
|
2020-12-17 06:14:36 +00:00
|
|
|
if !isSuccess {
|
2020-07-07 19:25:32 +00:00
|
|
|
return builder.Finish()
|
|
|
|
}
|
|
|
|
|
2020-12-17 06:14:36 +00:00
|
|
|
err := builder.StartAnswers()
|
2020-06-08 22:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:25:28 +00:00
|
|
|
switch resp.Question.Type {
|
|
|
|
case dns.TypeA, dns.TypeAAAA, dns.TypeALL:
|
|
|
|
if resp.IP.Is4() {
|
|
|
|
err = marshalARecord(resp.Question.Name, resp.IP, &builder)
|
2020-08-27 04:40:30 +00:00
|
|
|
} else if resp.IP.Is6() {
|
2020-08-06 18:25:28 +00:00
|
|
|
err = marshalAAAARecord(resp.Question.Name, resp.IP, &builder)
|
|
|
|
}
|
|
|
|
case dns.TypePTR:
|
|
|
|
err = marshalPTRRecord(resp.Question.Name, resp.Name, &builder)
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2020-07-07 19:25:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:25:32 +00:00
|
|
|
return builder.Finish()
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:54:18 +00:00
|
|
|
const (
|
|
|
|
rdnsv4Suffix = ".in-addr.arpa."
|
|
|
|
rdnsv6Suffix = ".ip6.arpa."
|
2020-08-06 18:25:28 +00:00
|
|
|
)
|
|
|
|
|
2020-09-23 19:05:51 +00:00
|
|
|
// hasRDNSBonjourPrefix reports whether name has a Bonjour Service Prefix..
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc6763 lists
|
|
|
|
// "five special RR names" for Bonjour service discovery:
|
|
|
|
//
|
|
|
|
// b._dns-sd._udp.<domain>.
|
|
|
|
// db._dns-sd._udp.<domain>.
|
|
|
|
// r._dns-sd._udp.<domain>.
|
|
|
|
// dr._dns-sd._udp.<domain>.
|
|
|
|
// lb._dns-sd._udp.<domain>.
|
2021-04-09 22:24:47 +00:00
|
|
|
func hasRDNSBonjourPrefix(name dnsname.FQDN) bool {
|
|
|
|
s := name.WithTrailingDot()
|
2020-09-23 19:05:51 +00:00
|
|
|
dot := strings.IndexByte(s, '.')
|
|
|
|
if dot == -1 {
|
|
|
|
return false // shouldn't happen
|
|
|
|
}
|
|
|
|
switch s[:dot] {
|
|
|
|
case "b", "db", "r", "dr", "lb":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.HasPrefix(s[dot:], "._dns-sd._udp.")
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:54:18 +00:00
|
|
|
// rawNameToLower converts a raw DNS name to a string, lowercasing it.
|
|
|
|
func rawNameToLower(name []byte) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
sb.Grow(len(name))
|
|
|
|
|
|
|
|
for _, b := range name {
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
|
|
|
b = b - 'A' + 'a'
|
|
|
|
}
|
|
|
|
sb.WriteByte(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:25:28 +00:00
|
|
|
// ptrNameToIPv4 transforms a PTR name representing an IPv4 address to said address.
|
|
|
|
// Such names are IPv4 labels in reverse order followed by .in-addr.arpa.
|
|
|
|
// For example,
|
|
|
|
// 4.3.2.1.in-addr.arpa
|
|
|
|
// is transformed to
|
|
|
|
// 1.2.3.4
|
2021-04-09 22:24:47 +00:00
|
|
|
func rdnsNameToIPv4(name dnsname.FQDN) (ip netaddr.IP, ok bool) {
|
|
|
|
s := strings.TrimSuffix(name.WithTrailingDot(), rdnsv4Suffix)
|
|
|
|
ip, err := netaddr.ParseIP(s)
|
2020-08-06 18:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
if !ip.Is4() {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
b := ip.As4()
|
|
|
|
return netaddr.IPv4(b[3], b[2], b[1], b[0]), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptrNameToIPv6 transforms a PTR name representing an IPv6 address to said address.
|
|
|
|
// Such names are dot-separated nibbles in reverse order followed by .ip6.arpa.
|
|
|
|
// For example,
|
|
|
|
// b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.
|
|
|
|
// is transformed to
|
|
|
|
// 2001:db8::567:89ab
|
2021-04-09 22:24:47 +00:00
|
|
|
func rdnsNameToIPv6(name dnsname.FQDN) (ip netaddr.IP, ok bool) {
|
2020-08-06 18:25:28 +00:00
|
|
|
var b [32]byte
|
|
|
|
var ipb [16]byte
|
|
|
|
|
2021-04-09 22:24:47 +00:00
|
|
|
s := strings.TrimSuffix(name.WithTrailingDot(), rdnsv6Suffix)
|
2020-08-06 18:25:28 +00:00
|
|
|
// 32 nibbles and 31 dots between them.
|
2021-04-09 22:24:47 +00:00
|
|
|
if len(s) != 63 {
|
2020-08-06 18:25:28 +00:00
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dots and hex digits alternate.
|
|
|
|
prevDot := true
|
|
|
|
// i ranges over name backward; j ranges over b forward.
|
2021-04-09 22:24:47 +00:00
|
|
|
for i, j := len(s)-1, 0; i >= 0; i-- {
|
|
|
|
thisDot := (s[i] == '.')
|
2020-08-06 18:25:28 +00:00
|
|
|
if prevDot == thisDot {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
prevDot = thisDot
|
|
|
|
|
|
|
|
if !thisDot {
|
|
|
|
// This is safe assuming alternation.
|
|
|
|
// We do not check that non-dots are hex digits: hex.Decode below will do that.
|
2021-04-09 22:24:47 +00:00
|
|
|
b[j] = s[i]
|
2020-08-06 18:25:28 +00:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := hex.Decode(ipb[:], b[:])
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return netaddr.IPFrom16(ipb), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// respondReverse returns a DNS response to a PTR query.
|
|
|
|
// It is assumed that resp.Question is populated by respond before this is called.
|
2021-04-09 22:24:47 +00:00
|
|
|
func (r *Resolver) respondReverse(query []byte, name dnsname.FQDN, resp *response) ([]byte, error) {
|
2020-09-23 19:05:51 +00:00
|
|
|
if hasRDNSBonjourPrefix(name) {
|
|
|
|
return nil, errNotOurName
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:25:02 +00:00
|
|
|
resp.Name, resp.Header.RCode = r.resolveLocalReverse(name)
|
|
|
|
if resp.Header.RCode == dns.RCodeRefused {
|
2020-08-19 19:39:25 +00:00
|
|
|
return nil, errNotOurName
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSMagicDNSSuccessReverse.Add(1)
|
2020-08-06 18:25:28 +00:00
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:27:21 +00:00
|
|
|
// respond returns a DNS response to query if it can be resolved locally.
|
|
|
|
// Otherwise, it returns errNotOurName.
|
2020-07-07 19:25:32 +00:00
|
|
|
func (r *Resolver) respond(query []byte) ([]byte, error) {
|
2021-06-23 04:53:43 +00:00
|
|
|
parser := dnsParserPool.Get().(*dnsParser)
|
|
|
|
defer dnsParserPool.Put(parser)
|
2020-07-07 19:25:32 +00:00
|
|
|
|
|
|
|
// ParseQuery is sufficiently fast to run on every DNS packet.
|
|
|
|
// This is considerably simpler than extracting the name by hand
|
|
|
|
// to shave off microseconds in case of delegation.
|
2021-06-23 04:53:43 +00:00
|
|
|
err := parser.parseQuery(query)
|
2020-06-08 22:19:26 +00:00
|
|
|
// We will not return this error: it is the sender's fault.
|
|
|
|
if err != nil {
|
2020-12-17 06:14:36 +00:00
|
|
|
if errors.Is(err, dns.ErrSectionDone) {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSErrorParseNoQ.Add(1)
|
2020-12-17 06:14:36 +00:00
|
|
|
r.logf("parseQuery(%02x): no DNS questions", query)
|
|
|
|
} else {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSErrorParseQuery.Add(1)
|
2020-12-17 06:14:36 +00:00
|
|
|
r.logf("parseQuery(%02x): %v", query, err)
|
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
resp := parser.response()
|
2020-06-08 22:19:26 +00:00
|
|
|
resp.Header.RCode = dns.RCodeFormatError
|
2020-07-07 19:25:32 +00:00
|
|
|
return marshalResponse(resp)
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
rawName := parser.Question.Name.Data[:parser.Question.Name.Length]
|
2021-04-09 22:24:47 +00:00
|
|
|
name, err := dnsname.ToFQDN(rawNameToLower(rawName))
|
|
|
|
if err != nil {
|
2021-11-26 22:43:38 +00:00
|
|
|
metricDNSErrorNotFQDN.Add(1)
|
2021-04-09 22:24:47 +00:00
|
|
|
// DNS packet unexpectedly contains an invalid FQDN.
|
2021-06-23 04:53:43 +00:00
|
|
|
resp := parser.response()
|
2021-04-09 22:24:47 +00:00
|
|
|
resp.Header.RCode = dns.RCodeFormatError
|
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
|
2020-08-06 18:25:28 +00:00
|
|
|
// Always try to handle reverse lookups; delegate inside when not found.
|
2020-09-23 19:05:51 +00:00
|
|
|
// This way, queries for existent nodes do not leak,
|
2020-08-06 18:25:28 +00:00
|
|
|
// but we behave gracefully if non-Tailscale nodes exist in CGNATRange.
|
2021-06-23 04:53:43 +00:00
|
|
|
if parser.Question.Type == dns.TypePTR {
|
|
|
|
return r.respondReverse(query, name, parser.response())
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
ip, rcode := r.resolveLocal(name, parser.Question.Type)
|
|
|
|
if rcode == dns.RCodeRefused {
|
|
|
|
return nil, errNotOurName // sentinel error return value: it requests forwarding
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
2020-08-27 04:07:15 +00:00
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
resp := parser.response()
|
|
|
|
resp.Header.RCode = rcode
|
|
|
|
resp.IP = ip
|
2020-07-07 19:25:32 +00:00
|
|
|
return marshalResponse(resp)
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
2021-11-26 22:43:38 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
metricDNSQueryLocal = clientmetric.NewCounter("dns_query_local")
|
|
|
|
metricDNSQueryErrorClosed = clientmetric.NewCounter("dns_query_local_error_closed")
|
|
|
|
metricDNSQueryErrorQueue = clientmetric.NewCounter("dns_query_local_error_queue")
|
|
|
|
|
|
|
|
metricDNSErrorParseNoQ = clientmetric.NewCounter("dns_query_respond_error_no_question")
|
|
|
|
metricDNSErrorParseQuery = clientmetric.NewCounter("dns_query_respond_error_parse")
|
|
|
|
metricDNSErrorNotFQDN = clientmetric.NewCounter("dns_query_respond_error_not_fqdn")
|
|
|
|
|
|
|
|
metricDNSMagicDNSSuccessName = clientmetric.NewCounter("dns_query_magic_success_name")
|
|
|
|
metricDNSMagicDNSSuccessReverse = clientmetric.NewCounter("dns_query_magic_success_reverse")
|
|
|
|
|
|
|
|
metricDNSQueryForPeer = clientmetric.NewCounter("dns_query_peerapi")
|
|
|
|
metricDNSQueryForPeerError = clientmetric.NewCounter("dns_query_peerapi_error")
|
|
|
|
|
|
|
|
metricDNSFwd = clientmetric.NewCounter("dns_query_fwd")
|
|
|
|
metricDNSFwdDropBonjour = clientmetric.NewCounter("dns_query_fwd_drop_bonjour")
|
|
|
|
metricDNSFwdErrorName = clientmetric.NewCounter("dns_query_fwd_error_name")
|
|
|
|
metricDNSFwdErrorNoUpstream = clientmetric.NewCounter("dns_query_fwd_error_no_upstream")
|
|
|
|
metricDNSFwdSuccess = clientmetric.NewCounter("dns_query_fwd_success")
|
|
|
|
metricDNSFwdErrorContext = clientmetric.NewCounter("dns_query_fwd_error_context")
|
|
|
|
metricDNSFwdErrorContextGotError = clientmetric.NewCounter("dns_query_fwd_error_context_got_error")
|
|
|
|
|
|
|
|
metricDNSFwdErrorType = clientmetric.NewCounter("dns_query_fwd_error_type")
|
|
|
|
metricDNSFwdErrorParseAddr = clientmetric.NewCounter("dns_query_fwd_error_parse_addr")
|
|
|
|
|
|
|
|
metricDNSFwdUDP = clientmetric.NewCounter("dns_query_fwd_udp") // on entry
|
|
|
|
metricDNSFwdUDPWrote = clientmetric.NewCounter("dns_query_fwd_udp_wrote") // sent UDP packet
|
|
|
|
metricDNSFwdUDPErrorWrite = clientmetric.NewCounter("dns_query_fwd_udp_error_write")
|
|
|
|
metricDNSFwdUDPErrorServer = clientmetric.NewCounter("dns_query_fwd_udp_error_server")
|
|
|
|
metricDNSFwdUDPErrorTxID = clientmetric.NewCounter("dns_query_fwd_udp_error_txid")
|
|
|
|
metricDNSFwdUDPErrorRead = clientmetric.NewCounter("dns_query_fwd_udp_error_read")
|
|
|
|
metricDNSFwdUDPSuccess = clientmetric.NewCounter("dns_query_fwd_udp_success")
|
|
|
|
|
|
|
|
metricDNSFwdDoH = clientmetric.NewCounter("dns_query_fwd_doh")
|
|
|
|
metricDNSFwdDoHErrorStatus = clientmetric.NewCounter("dns_query_fwd_doh_error_status")
|
|
|
|
metricDNSFwdDoHErrorCT = clientmetric.NewCounter("dns_query_fwd_doh_error_content_type")
|
|
|
|
metricDNSFwdDoHErrorTransport = clientmetric.NewCounter("dns_query_fwd_doh_error_transport")
|
|
|
|
metricDNSFwdDoHErrorBody = clientmetric.NewCounter("dns_query_fwd_doh_error_body")
|
|
|
|
|
|
|
|
metricDNSResolveLocal = clientmetric.NewCounter("dns_resolve_local")
|
|
|
|
metricDNSResolveLocalErrorOnion = clientmetric.NewCounter("dns_resolve_local_error_onion")
|
|
|
|
metricDNSResolveLocalErrorMissing = clientmetric.NewCounter("dns_resolve_local_error_missing")
|
|
|
|
metricDNSResolveLocalErrorRefused = clientmetric.NewCounter("dns_resolve_local_error_refused")
|
|
|
|
metricDNSResolveLocalOKA = clientmetric.NewCounter("dns_resolve_local_ok_a")
|
|
|
|
metricDNSResolveLocalOKAAAA = clientmetric.NewCounter("dns_resolve_local_ok_aaaa")
|
|
|
|
metricDNSResolveLocalOKAll = clientmetric.NewCounter("dns_resolve_local_ok_all")
|
|
|
|
metricDNSResolveLocalNoA = clientmetric.NewCounter("dns_resolve_local_no_a")
|
|
|
|
metricDNSResolveLocalNoAAAA = clientmetric.NewCounter("dns_resolve_local_no_aaaa")
|
|
|
|
metricDNSResolveLocalNoAll = clientmetric.NewCounter("dns_resolve_local_no_all")
|
|
|
|
metricDNSResolveNotImplType = clientmetric.NewCounter("dns_resolve_local_not_impl_type")
|
|
|
|
metricDNSResolveNoRecordType = clientmetric.NewCounter("dns_resolve_local_no_record_type")
|
|
|
|
)
|