2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-08 22:19:26 +00:00
|
|
|
|
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-11-29 22:18:09 +00:00
|
|
|
"net"
|
2022-07-25 03:08:42 +00:00
|
|
|
"net/netip"
|
2021-11-29 22:18:09 +00:00
|
|
|
"os"
|
2021-06-23 04:53:43 +00:00
|
|
|
"runtime"
|
2021-04-01 08:33:58 +00:00
|
|
|
"sort"
|
2022-04-21 21:13:13 +00:00
|
|
|
"strconv"
|
2020-08-20 22:54:18 +00:00
|
|
|
"strings"
|
2020-06-08 22:19:26 +00:00
|
|
|
"sync"
|
2020-06-09 17:09:43 +00:00
|
|
|
"time"
|
2020-06-08 22:19:26 +00:00
|
|
|
|
|
|
|
dns "golang.org/x/net/dns/dnsmessage"
|
2023-09-07 20:27:50 +00:00
|
|
|
"tailscale.com/control/controlknobs"
|
2023-02-03 16:25:31 +00:00
|
|
|
"tailscale.com/envknob"
|
2024-07-29 17:48:46 +00:00
|
|
|
"tailscale.com/health"
|
2022-02-15 04:17:51 +00:00
|
|
|
"tailscale.com/net/dns/resolvconffile"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2023-04-18 21:26:58 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2021-11-29 22:18:09 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2021-12-01 04:39:12 +00:00
|
|
|
"tailscale.com/net/tsdial"
|
2022-08-04 17:43:49 +00:00
|
|
|
"tailscale.com/syncs"
|
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"
|
2022-06-30 02:32:41 +00:00
|
|
|
"tailscale.com/util/cloudenv"
|
2021-01-10 20:03:01 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-06-08 22:19:26 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 22:56:21 +00:00
|
|
|
const dnsSymbolicFQDN = "magicdns.localhost-tailscale-daemon."
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
var (
|
2021-04-01 08:33:58 +00:00
|
|
|
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 {
|
2023-09-07 20:27:50 +00:00
|
|
|
bs []byte
|
|
|
|
family string // either "tcp" or "udp"
|
|
|
|
addr netip.AddrPort // 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 ".".
|
2022-05-03 21:41:58 +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.
|
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
|
|
|
Hosts map[dnsname.FQDN][]netip.Addr
|
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)
|
|
|
|
}
|
2022-06-30 02:32:41 +00:00
|
|
|
if c := cloudenv.Get(); c != "" {
|
|
|
|
fmt.Fprintf(w, ", cloud=%q", string(c))
|
|
|
|
}
|
2021-07-20 05:24:43 +00:00
|
|
|
w.WriteString("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteIPPorts writes vv to w.
|
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 WriteIPPorts(w *bufio.Writer, vv []netip.AddrPort) {
|
2021-07-20 05:24:43 +00:00
|
|
|
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.
|
2022-05-03 21:41:58 +00:00
|
|
|
func WriteDNSResolver(w *bufio.Writer, r *dnstype.Resolver) {
|
2021-08-03 13:56:31 +00:00
|
|
|
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.
|
2022-05-03 21:41:58 +00:00
|
|
|
func WriteDNSResolvers(w *bufio.Writer, resolvers []*dnstype.Resolver) {
|
2021-08-03 13:56:31 +00:00
|
|
|
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.
|
2022-05-03 21:41:58 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
xcode/iOS: set MatchDomains when no route requires a custom DNS resolver (#10576)
Updates https://github.com/tailscale/corp/issues/15802.
On iOS exclusively, this PR adds logic to use a split DNS configuration in more cases, with the goal of improving battery life. Acting as the global DNS resolver on iOS should be avoided, as it leads to frequent wakes of IPNExtension.
We try to determine if we can have Tailscale only handle DNS queries for resources inside the tailnet, that is, all routes in the DNS configuration do not require a custom resolver (this is the case for app connectors, for instance).
If so, we set all Routes as MatchDomains. This enables a split DNS configuration which will help preserve battery life. Effectively, for the average Tailscale user who only relies on MagicDNS to resolve *.ts.net domains, this means that Tailscale DNS will only be used for those domains.
This PR doesn't affect users with Override Local DNS enabled. For these users, there should be no difference and Tailscale will continue acting as a global DNS resolver.
Signed-off-by: Andrea Gottardo <andrea@tailscale.com>
2024-05-29 19:11:02 +00:00
|
|
|
// RoutesRequireNoCustomResolvers returns true if this resolver.Config only contains routes
|
|
|
|
// that do not specify a set of custom resolver(s), i.e. they can be resolved by the local
|
|
|
|
// upstream DNS resolver.
|
|
|
|
func (c *Config) RoutesRequireNoCustomResolvers() bool {
|
|
|
|
for route, resolvers := range c.Routes {
|
|
|
|
if route.WithoutTrailingDot() == "ts.net" {
|
|
|
|
// Ignore the "ts.net" route here. It always specifies the corp resolvers but
|
|
|
|
// its presence is not an issue, as ts.net will be a search domain.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(resolvers) != 0 {
|
|
|
|
// Found a route with custom resolvers.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No routes other than ts.net have specified one or more resolvers.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
2024-04-27 05:06:20 +00:00
|
|
|
netMon *netmon.Monitor // non-nil
|
2021-12-01 04:39:12 +00:00
|
|
|
dialer *tsdial.Dialer // non-nil
|
2024-07-29 17:48:46 +00:00
|
|
|
health *health.Tracker // non-nil
|
2021-04-07 05:09:55 +00:00
|
|
|
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
|
|
|
|
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-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
|
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
|
|
|
hostToIP map[dnsname.FQDN][]netip.Addr
|
|
|
|
ipToHost map[netip.Addr]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.
|
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
|
|
|
PickLink(netip.Addr) (linkName string)
|
2021-06-23 04:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 05:13:23 +00:00
|
|
|
// New returns a new resolver.
|
2024-07-29 17:48:46 +00:00
|
|
|
// dialer and health must be non-nil.
|
|
|
|
func New(logf logger.Logf, linkSel ForwardLinkSelector, dialer *tsdial.Dialer, health *health.Tracker, knobs *controlknobs.Knobs) *Resolver {
|
2021-12-01 04:39:12 +00:00
|
|
|
if dialer == nil {
|
|
|
|
panic("nil Dialer")
|
|
|
|
}
|
2024-07-29 17:48:46 +00:00
|
|
|
if health == nil {
|
|
|
|
panic("nil health")
|
|
|
|
}
|
2024-04-27 05:06:20 +00:00
|
|
|
netMon := dialer.NetMon()
|
|
|
|
if netMon == nil {
|
|
|
|
logf("nil netMon")
|
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
r := &Resolver{
|
2022-04-14 20:27:59 +00:00
|
|
|
logf: logger.WithPrefix(logf, "resolver: "),
|
2023-04-18 21:26:58 +00:00
|
|
|
netMon: netMon,
|
2022-04-14 20:27:59 +00:00
|
|
|
closed: make(chan struct{}),
|
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
|
|
|
hostToIP: map[dnsname.FQDN][]netip.Addr{},
|
|
|
|
ipToHost: map[netip.Addr]dnsname.FQDN{},
|
2022-04-14 20:27:59 +00:00
|
|
|
dialer: dialer,
|
2024-07-29 17:48:46 +00:00
|
|
|
health: health,
|
2022-04-14 20:27:59 +00:00
|
|
|
}
|
2024-07-29 17:48:46 +00:00
|
|
|
r.forwarder = newForwarder(r.logf, netMon, linkSel, dialer, health, knobs)
|
2021-04-03 02:42:05 +00:00
|
|
|
return r
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 19:21:32 +00:00
|
|
|
// SetMissingUpstreamRecovery sets a callback to be called upon encountering
|
|
|
|
// a SERVFAIL due to missing upstream resolvers.
|
|
|
|
//
|
|
|
|
// This call should only happen before the resolver is used. It is not safe
|
|
|
|
// for concurrent use.
|
2024-06-25 18:56:13 +00:00
|
|
|
func (r *Resolver) SetMissingUpstreamRecovery(f func()) {
|
|
|
|
r.forwarder.missingUpstreamRecovery = f
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
reverse := make(map[netip.Addr]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
|
|
|
}
|
|
|
|
|
2022-05-18 17:40:04 +00:00
|
|
|
// dnsQueryTimeout is not intended to be user-visible (the users
|
|
|
|
// DNS resolver will retry well before that), just put an upper
|
|
|
|
// bound on per-query resource usage.
|
|
|
|
const dnsQueryTimeout = 10 * time.Second
|
|
|
|
|
2023-09-07 20:27:50 +00:00
|
|
|
func (r *Resolver) Query(ctx context.Context, bs []byte, family string, from netip.AddrPort) ([]byte, 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)
|
2022-05-02 16:35:32 +00:00
|
|
|
return nil, net.ErrClosed
|
2020-07-07 19:25:32 +00:00
|
|
|
default:
|
2021-06-23 04:53:43 +00:00
|
|
|
}
|
2022-04-08 19:17:31 +00:00
|
|
|
|
2022-04-14 20:27:59 +00:00
|
|
|
out, err := r.respond(bs)
|
|
|
|
if err == errNotOurName {
|
|
|
|
responses := make(chan packet, 1)
|
2022-05-18 17:40:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, dnsQueryTimeout)
|
2022-04-14 20:27:59 +00:00
|
|
|
defer close(responses)
|
|
|
|
defer cancel()
|
2023-09-07 20:27:50 +00:00
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{bs, family, from}, responses)
|
2022-04-14 20:27:59 +00:00
|
|
|
if err != nil {
|
2022-06-15 23:19:05 +00:00
|
|
|
select {
|
|
|
|
// Best effort: use any error response sent by forwardWithDestChan.
|
|
|
|
// This is present in some errors paths, such as when all upstream
|
|
|
|
// DNS servers replied with an error.
|
|
|
|
case resp := <-responses:
|
|
|
|
return resp.bs, err
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-08 19:17:31 +00:00
|
|
|
}
|
2022-04-14 20:27:59 +00:00
|
|
|
return (<-responses).bs, nil
|
2022-04-08 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:27:59 +00:00
|
|
|
return out, err
|
2020-07-07 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 22:18:09 +00:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// HandlePeerDNSQuery handles a DNS query that arrived from a peer
|
|
|
|
// via the peerapi's DoH server. This is used when the local
|
|
|
|
// node is being an exit node or an app connector.
|
2021-11-29 22:18:09 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2023-10-27 21:20:10 +00:00
|
|
|
func (r *Resolver) HandlePeerDNSQuery(ctx context.Context, q []byte, from netip.AddrPort, allowName func(name string) bool) (res []byte, err error) {
|
2021-11-29 22:18:09 +00:00
|
|
|
metricDNSExitProxyQuery.Add(1)
|
2021-11-23 17:58:34 +00:00
|
|
|
ch := make(chan packet, 1)
|
|
|
|
|
2021-11-29 22:18:09 +00:00
|
|
|
resp := parseExitNodeQuery(q)
|
|
|
|
if resp == nil {
|
|
|
|
return nil, errors.New("bad query")
|
|
|
|
}
|
|
|
|
name := resp.Question.Name.String()
|
|
|
|
if !allowName(name) {
|
|
|
|
metricDNSExitProxyErrorName.Add(1)
|
|
|
|
resp.Header.RCode = dns.RCodeRefused
|
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupported exit node OS")
|
2022-01-06 23:01:50 +00:00
|
|
|
case "windows", "android":
|
2023-02-03 16:25:31 +00:00
|
|
|
return handleExitNodeDNSQueryWithNetPkg(ctx, r.logf, nil, resp)
|
2021-11-29 22:18:09 +00:00
|
|
|
case "darwin":
|
|
|
|
// /etc/resolv.conf is a lie and only says one upstream DNS
|
|
|
|
// but for now that's probably good enough. Later we'll
|
|
|
|
// want to blend in everything from scutil --dns.
|
|
|
|
fallthrough
|
2023-10-05 19:18:21 +00:00
|
|
|
case "linux", "freebsd", "openbsd", "illumos", "ios":
|
2021-11-29 22:18:09 +00:00
|
|
|
nameserver, err := stubResolverForOS()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("stubResolverForOS: %v", err)
|
|
|
|
metricDNSExitProxyErrorResolvConf.Add(1)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: more than 1 resolver from /etc/resolv.conf?
|
|
|
|
|
|
|
|
var resolvers []resolverAndDelay
|
2023-10-05 19:18:21 +00:00
|
|
|
switch nameserver {
|
|
|
|
case tsaddr.TailscaleServiceIP(), tsaddr.TailscaleServiceIPv6():
|
2021-11-29 22:18:09 +00:00
|
|
|
// 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.
|
2023-10-05 19:18:21 +00:00
|
|
|
case netip.Addr{}:
|
|
|
|
// Likewise, if the platform has no resolv.conf, just use our defaults.
|
|
|
|
default:
|
2021-11-29 22:18:09 +00:00
|
|
|
resolvers = []resolverAndDelay{{
|
2022-05-03 21:41:58 +00:00
|
|
|
name: &dnstype.Resolver{Addr: net.JoinHostPort(nameserver.String(), "53")},
|
2021-11-29 22:18:09 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-09-07 20:27:50 +00:00
|
|
|
err = r.forwarder.forwardWithDestChan(ctx, packet{q, "tcp", from}, ch, resolvers...)
|
2021-11-29 22:18:09 +00:00
|
|
|
if err != nil {
|
|
|
|
metricDNSExitProxyErrorForward.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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:25:31 +00:00
|
|
|
var debugExitNodeDNSNetPkg = envknob.RegisterBool("TS_DEBUG_EXIT_NODE_DNS_NET_PKG")
|
|
|
|
|
2021-12-09 20:01:19 +00:00
|
|
|
// handleExitNodeDNSQueryWithNetPkg takes a DNS query message in q and
|
|
|
|
// return a reply (for the ExitDNS DoH service) using the net package's
|
2023-10-05 19:18:21 +00:00
|
|
|
// native APIs.
|
2021-12-09 20:01:19 +00:00
|
|
|
//
|
|
|
|
// If resolver is nil, the net.Resolver zero value is used.
|
|
|
|
//
|
|
|
|
// response contains the pre-serialized response, which notably
|
|
|
|
// includes the original question and its header.
|
2023-02-03 16:25:31 +00:00
|
|
|
func handleExitNodeDNSQueryWithNetPkg(ctx context.Context, logf logger.Logf, resolver *net.Resolver, resp *response) (res []byte, err error) {
|
|
|
|
logf = logger.WithPrefix(logf, "exitNodeDNSQueryWithNetPkg: ")
|
2021-12-09 20:01:19 +00:00
|
|
|
if resp.Question.Class != dns.ClassINET {
|
|
|
|
return nil, errors.New("unsupported class")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := resolver
|
|
|
|
if r == nil {
|
|
|
|
r = new(net.Resolver)
|
|
|
|
}
|
|
|
|
name := resp.Question.Name.String()
|
|
|
|
|
|
|
|
handleError := func(err error) (res []byte, _ error) {
|
|
|
|
if isGoNoSuchHostError(err) {
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf(`converting Go "no such host" error to a NXDOMAIN: %v`, err)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
resp.Header.RCode = dns.RCodeNameError
|
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
2023-02-03 16:25:31 +00:00
|
|
|
|
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("returning error: %v", err)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
// TODO: map other errors to RCodeServerFailure?
|
|
|
|
// Or I guess our caller should do that?
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Header.RCode = dns.RCodeSuccess // unless changed below
|
|
|
|
|
|
|
|
switch resp.Question.Type {
|
|
|
|
case dns.TypeA, dns.TypeAAAA:
|
|
|
|
network := "ip4"
|
|
|
|
if resp.Question.Type == dns.TypeAAAA {
|
|
|
|
network = "ip6"
|
|
|
|
}
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving %s %q", network, name)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
ips, err := r.LookupIP(ctx, network, name)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
for _, stdIP := range ips {
|
2022-08-02 20:38:11 +00:00
|
|
|
if ip, ok := netip.AddrFromSlice(stdIP); ok {
|
|
|
|
resp.IPs = append(resp.IPs, ip.Unmap())
|
2021-12-09 20:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case dns.TypeTXT:
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving TXT %q", name)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
strs, err := r.LookupTXT(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
resp.TXT = strs
|
|
|
|
case dns.TypePTR:
|
|
|
|
ipStr, ok := unARPA(name)
|
|
|
|
if !ok {
|
|
|
|
// TODO: is this RCodeFormatError?
|
|
|
|
return nil, errors.New("bogus PTR name")
|
|
|
|
}
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving PTR %q", ipStr)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
addrs, err := r.LookupAddr(ctx, ipStr)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
if len(addrs) > 0 {
|
|
|
|
resp.Name, _ = dnsname.ToFQDN(addrs[0])
|
|
|
|
}
|
|
|
|
case dns.TypeCNAME:
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving CNAME %q", name)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
cname, err := r.LookupCNAME(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
resp.CNAME = cname
|
|
|
|
case dns.TypeSRV:
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving SRV %q", name)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
// Thanks, Go: "To accommodate services publishing SRV
|
|
|
|
// records under non-standard names, if both service
|
|
|
|
// and proto are empty strings, LookupSRV looks up
|
|
|
|
// name directly."
|
|
|
|
_, srvs, err := r.LookupSRV(ctx, "", "", name)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
resp.SRVs = srvs
|
|
|
|
case dns.TypeNS:
|
2023-02-03 16:25:31 +00:00
|
|
|
if debugExitNodeDNSNetPkg() {
|
|
|
|
logf("resolving NS %q", name)
|
|
|
|
}
|
2021-12-09 20:01:19 +00:00
|
|
|
nss, err := r.LookupNS(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
|
|
|
resp.NSs = nss
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported record type %v", resp.Question.Type)
|
|
|
|
}
|
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGoNoSuchHostError(err error) bool {
|
|
|
|
if de, ok := err.(*net.DNSError); ok {
|
|
|
|
return de.IsNotFound
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-29 22:18:09 +00:00
|
|
|
type resolvConfCache struct {
|
|
|
|
mod time.Time
|
|
|
|
size int64
|
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
|
|
|
ip netip.Addr
|
2021-11-29 22:18:09 +00:00
|
|
|
// TODO: inode/dev?
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolvConfCacheValue contains the most recent stat metadata and parsed
|
|
|
|
// version of /etc/resolv.conf.
|
2022-08-04 17:43:49 +00:00
|
|
|
var resolvConfCacheValue syncs.AtomicValue[resolvConfCache]
|
2021-11-29 22:18:09 +00:00
|
|
|
|
|
|
|
var errEmptyResolvConf = errors.New("resolv.conf has no nameservers")
|
|
|
|
|
|
|
|
// stubResolverForOS returns the IP address of the first nameserver in
|
|
|
|
// /etc/resolv.conf.
|
2023-10-05 19:18:21 +00:00
|
|
|
//
|
|
|
|
// It may also return the netip.Addr zero value and a nil error to mean
|
|
|
|
// that the platform has no resolv.conf.
|
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 stubResolverForOS() (ip netip.Addr, err error) {
|
2023-10-05 19:18:21 +00:00
|
|
|
if runtime.GOOS == "ios" {
|
|
|
|
return netip.Addr{}, nil // no resolv.conf on iOS
|
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
fi, err := os.Stat(resolvconffile.Path)
|
2021-11-29 22:18:09 +00:00
|
|
|
if err != 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
|
|
|
return netip.Addr{}, err
|
2021-11-29 22:18:09 +00:00
|
|
|
}
|
|
|
|
cur := resolvConfCache{
|
|
|
|
mod: fi.ModTime(),
|
|
|
|
size: fi.Size(),
|
|
|
|
}
|
2022-08-04 17:43:49 +00:00
|
|
|
if c, ok := resolvConfCacheValue.LoadOk(); ok && c.mod == cur.mod && c.size == cur.size {
|
2021-11-29 22:18:09 +00:00
|
|
|
return c.ip, nil
|
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
conf, err := resolvconffile.ParseFile(resolvconffile.Path)
|
2021-11-29 22:18:09 +00:00
|
|
|
if err != 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
|
|
|
return netip.Addr{}, err
|
2021-11-29 22:18:09 +00:00
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
if len(conf.Nameservers) == 0 {
|
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
|
|
|
return netip.Addr{}, errEmptyResolvConf
|
2021-11-29 22:18:09 +00:00
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
ip = conf.Nameservers[0]
|
2021-11-29 22:18:09 +00:00
|
|
|
cur.ip = ip
|
|
|
|
resolvConfCacheValue.Store(cur)
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
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.
|
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 (r *Resolver) resolveLocal(domain dnsname.FQDN, typ dns.Type) (netip.Addr, 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)
|
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
|
|
|
return netip.Addr{}, dns.RCodeNameError
|
2021-02-26 16:16:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 22:56:21 +00:00
|
|
|
// We return a symbolic domain if someone does a reverse lookup on the
|
|
|
|
// DNS endpoint. To round out this special case, we also do the inverse
|
|
|
|
// (returning the endpoint IP if someone looks up the symbolic domain).
|
|
|
|
if domain == dnsSymbolicFQDN {
|
|
|
|
switch typ {
|
|
|
|
case dns.TypeA:
|
|
|
|
return tsaddr.TailscaleServiceIP(), dns.RCodeSuccess
|
|
|
|
case dns.TypeAAAA:
|
|
|
|
return tsaddr.TailscaleServiceIPv6(), dns.RCodeSuccess
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 16:57:36 +00:00
|
|
|
// Special-case: 4via6 DNS names.
|
2024-06-10 23:34:20 +00:00
|
|
|
if ip, ok := r.resolveViaDomain(domain, typ); ok {
|
2022-04-21 21:13:13 +00:00
|
|
|
return ip, dns.RCodeSuccess
|
|
|
|
}
|
2022-04-06 22:56:21 +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)
|
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
|
|
|
return netip.Addr{}, dns.RCodeNameError
|
2021-04-01 08:33:58 +00:00
|
|
|
}
|
2021-01-14 22:48:00 +00:00
|
|
|
}
|
2021-04-01 08:33:58 +00:00
|
|
|
// Not authoritative, signal that forwarding is advisable.
|
2023-12-22 01:40:03 +00:00
|
|
|
metricDNSResolveLocalErrorRefused.Add(1)
|
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
|
|
|
return netip.Addr{}, 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)
|
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
|
|
|
return netip.Addr{}, 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)
|
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
|
|
|
return netip.Addr{}, 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)
|
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
|
|
|
return netip.Addr{}, dns.RCodeSuccess
|
2021-04-01 08:33:58 +00:00
|
|
|
}
|
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
|
|
|
|
2022-09-25 18:29:55 +00:00
|
|
|
// Leave some record types explicitly unimplemented.
|
2020-12-31 22:31:33 +00:00
|
|
|
// 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)
|
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
|
|
|
return netip.Addr{}, 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.
|
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
|
|
|
return netip.Addr{}, dns.RCodeSuccess
|
2020-08-27 04:07:15 +00:00
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 23:34:20 +00:00
|
|
|
// resolveViaDomain synthesizes an IP address for quad-A DNS requests of the form
|
2022-11-11 16:57:36 +00:00
|
|
|
// `<IPv4-address-with-hypens-instead-of-dots>-via-<siteid>[.*]`. Two prior formats that
|
|
|
|
// didn't pan out (due to a Chrome issue and DNS search ndots issues) were
|
|
|
|
// `<IPv4-address>.via-<X>` and the older `via-<X>.<IPv4-address>`,
|
2022-06-02 17:58:46 +00:00
|
|
|
// where X is a decimal, or hex-encoded number with a '0x' prefix.
|
2022-04-21 21:13:13 +00:00
|
|
|
//
|
|
|
|
// This exists as a convenient mapping into Tailscales 'Via Range'.
|
2022-06-02 17:58:46 +00:00
|
|
|
//
|
2024-06-10 23:34:20 +00:00
|
|
|
// It returns a zero netip.Addr and true to indicate a successful response with
|
|
|
|
// an empty answers section if the specified domain is a valid Tailscale 4via6
|
|
|
|
// domain, but the request type is neither quad-A nor ALL.
|
|
|
|
//
|
2022-06-02 17:58:46 +00:00
|
|
|
// TODO(maisem/bradfitz/tom): `<IPv4-address>.via-<X>` was introduced
|
|
|
|
// (2022-06-02) to work around an issue in Chrome where it would treat
|
|
|
|
// "http://via-1.1.2.3.4" as a search string instead of a URL. We should rip out
|
|
|
|
// the old format in early 2023.
|
2024-06-10 23:34:20 +00:00
|
|
|
func (r *Resolver) resolveViaDomain(domain dnsname.FQDN, typ dns.Type) (netip.Addr, bool) {
|
2022-04-21 21:13:13 +00:00
|
|
|
fqdn := string(domain.WithoutTrailingDot())
|
2024-06-10 23:34:20 +00:00
|
|
|
switch typ {
|
|
|
|
case dns.TypeA, dns.TypeAAAA, dns.TypeALL:
|
|
|
|
// For Type A requests, we should return a successful response
|
|
|
|
// with an empty answer section rather than an NXDomain
|
|
|
|
// if the specified domain is a valid Tailscale 4via6 domain.
|
|
|
|
//
|
|
|
|
// Therefore, we should continue and parse the domain name first
|
|
|
|
// before deciding whether to return an IPv6 address,
|
|
|
|
// a zero (invalid) netip.Addr and true to indicate a successful empty response,
|
|
|
|
// or a zero netip.Addr and false to indicate that it is not a Tailscale 4via6 domain.
|
|
|
|
default:
|
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
|
|
|
return netip.Addr{}, false
|
2022-04-21 21:13:13 +00:00
|
|
|
}
|
|
|
|
if len(fqdn) < len("via-X.0.0.0.0") {
|
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
|
|
|
return netip.Addr{}, false // too short to be valid
|
2022-04-21 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 17:58:46 +00:00
|
|
|
var siteID string
|
|
|
|
var ip4Str string
|
2022-11-11 16:57:36 +00:00
|
|
|
switch {
|
|
|
|
case strings.Contains(fqdn, "-via-"):
|
|
|
|
// Format number 3: "192-168-1-2-via-7" or "192-168-1-2-via-7.foo.ts.net."
|
|
|
|
// Third time's a charm. The earlier two formats follow after this block.
|
|
|
|
firstLabel, domain, _ := strings.Cut(fqdn, ".") // "192-168-1-2-via-7"
|
|
|
|
if !(domain == "" || dnsname.HasSuffix(domain, "ts.net") || dnsname.HasSuffix(domain, "tailscale.net")) {
|
|
|
|
return netip.Addr{}, false
|
|
|
|
}
|
|
|
|
v4hyphens, suffix, ok := strings.Cut(firstLabel, "-via-")
|
|
|
|
if !ok {
|
|
|
|
return netip.Addr{}, false
|
|
|
|
}
|
|
|
|
siteID = suffix
|
|
|
|
ip4Str = strings.ReplaceAll(v4hyphens, "-", ".")
|
|
|
|
case strings.HasPrefix(fqdn, "via-"):
|
2022-06-02 17:58:46 +00:00
|
|
|
firstDot := strings.Index(fqdn, ".")
|
|
|
|
if firstDot < 0 {
|
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
|
|
|
return netip.Addr{}, false // missing dot delimiters
|
2022-06-02 17:58:46 +00:00
|
|
|
}
|
|
|
|
siteID = fqdn[len("via-"):firstDot]
|
|
|
|
ip4Str = fqdn[firstDot+1:]
|
2022-11-11 16:57:36 +00:00
|
|
|
default:
|
2022-06-02 17:58:46 +00:00
|
|
|
lastDot := strings.LastIndex(fqdn, ".")
|
|
|
|
if lastDot < 0 {
|
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
|
|
|
return netip.Addr{}, false // missing dot delimiters
|
2022-06-02 17:58:46 +00:00
|
|
|
}
|
|
|
|
suffix := fqdn[lastDot+1:]
|
|
|
|
if !strings.HasPrefix(suffix, "via-") {
|
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
|
|
|
return netip.Addr{}, false
|
2022-06-02 17:58:46 +00:00
|
|
|
}
|
|
|
|
siteID = suffix[len("via-"):]
|
|
|
|
ip4Str = fqdn[:lastDot]
|
2022-04-21 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 03:55:44 +00:00
|
|
|
ip4, err := netip.ParseAddr(ip4Str)
|
2022-04-21 21:13:13 +00:00
|
|
|
if err != nil {
|
2022-11-11 16:57:36 +00:00
|
|
|
return netip.Addr{}, false // badly formed, don't respond
|
2022-04-21 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prefix, err := strconv.ParseUint(siteID, 0, 32)
|
|
|
|
if err != nil {
|
2022-11-11 16:57:36 +00:00
|
|
|
return netip.Addr{}, false // badly formed, don't respond
|
2022-04-21 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 23:34:20 +00:00
|
|
|
if typ == dns.TypeA {
|
|
|
|
return netip.Addr{}, true // the name exists, but cannot be resolved to an IPv4 address
|
|
|
|
}
|
|
|
|
|
2023-06-27 16:00:34 +00:00
|
|
|
// MapVia will never error when given an IPv4 netip.Prefix.
|
2022-07-25 03:08:42 +00:00
|
|
|
out, _ := tsaddr.MapVia(uint32(prefix), netip.PrefixFrom(ip4, ip4.BitLen()))
|
|
|
|
return out.Addr(), true
|
2022-04-21 21:13:13 +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) {
|
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 ip netip.Addr
|
2021-09-02 00:25:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:35:20 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
// If the requested IP is part of the IPv6 4-to-6 range, it might
|
|
|
|
// correspond to an IPv4 address (assuming IPv4 is enabled).
|
|
|
|
if ip4, ok := tsaddr.Tailscale6to4(ip); ok {
|
|
|
|
fqdn, code := r.fqdnForIPLocked(ip4, name)
|
|
|
|
if code == dns.RCodeSuccess {
|
|
|
|
return fqdn, code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.fqdnForIPLocked(ip, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// r.mu must be held.
|
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 (r *Resolver) fqdnForIPLocked(ip netip.Addr, name dnsname.FQDN) (dnsname.FQDN, dns.RCode) {
|
2022-04-06 22:56:21 +00:00
|
|
|
// If someone curiously does a reverse lookup on the DNS IP, we
|
|
|
|
// return a domain that helps indicate that Tailscale is using
|
|
|
|
// this IP for a special purpose and it is not a node on their
|
|
|
|
// tailnet.
|
|
|
|
if ip == tsaddr.TailscaleServiceIP() || ip == tsaddr.TailscaleServiceIPv6() {
|
|
|
|
return dnsSymbolicFQDN, dns.RCodeSuccess
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2021-12-09 20:01:19 +00:00
|
|
|
|
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
|
2021-12-09 20:01:19 +00:00
|
|
|
|
|
|
|
// IP and IPs are the responses to an A, AAAA, or ALL query.
|
|
|
|
// Either/both/neither can be populated.
|
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
|
|
|
IP netip.Addr
|
|
|
|
IPs []netip.Addr
|
2021-12-09 20:01:19 +00:00
|
|
|
|
|
|
|
// TXT is the response to a TXT query.
|
|
|
|
// Each one is its own RR with one string.
|
|
|
|
TXT []string
|
|
|
|
|
|
|
|
// CNAME is the response to a CNAME query.
|
|
|
|
CNAME string
|
|
|
|
|
|
|
|
// SRVs are the responses to a SRV query.
|
|
|
|
SRVs []*net.SRV
|
|
|
|
|
|
|
|
// NSs are the responses to an NS query.
|
|
|
|
NSs []*net.NS
|
2020-06-08 22:19:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:53:43 +00:00
|
|
|
var dnsParserPool = &sync.Pool{
|
2022-03-16 23:27:57 +00:00
|
|
|
New: func() any {
|
2021-06-23 04:53:43 +00:00
|
|
|
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()
|
2021-11-29 22:18:09 +00:00
|
|
|
p.zeroParser()
|
2021-06-23 04:53:43 +00:00
|
|
|
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.
|
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 marshalARecord(name dns.Name, ip netip.Addr, 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.
|
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 marshalAAAARecord(name dns.Name, ip netip.Addr, builder *dns.Builder) error {
|
2020-07-07 19:25:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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 marshalIP(name dns.Name, ip netip.Addr, builder *dns.Builder) error {
|
2021-12-09 20:01:19 +00:00
|
|
|
if ip.Is4() {
|
|
|
|
return marshalARecord(name, ip, builder)
|
|
|
|
}
|
|
|
|
if ip.Is6() {
|
|
|
|
return marshalAAAARecord(name, ip, builder)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:01:19 +00:00
|
|
|
func marshalTXT(queryName dns.Name, txts []string, builder *dns.Builder) error {
|
|
|
|
for _, txt := range txts {
|
|
|
|
if err := builder.TXTResource(dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypeTXT,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}, dns.TXTResource{
|
|
|
|
TXT: []string{txt},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalCNAME(queryName dns.Name, cname string, builder *dns.Builder) error {
|
|
|
|
if cname == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name, err := dns.NewName(cname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return builder.CNAMEResource(dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}, dns.CNAMEResource{
|
|
|
|
CNAME: name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalNS(queryName dns.Name, nss []*net.NS, builder *dns.Builder) error {
|
|
|
|
for _, ns := range nss {
|
|
|
|
name, err := dns.NewName(ns.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = builder.NSResource(dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypeNS,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}, dns.NSResource{NS: name})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalSRV(queryName dns.Name, srvs []*net.SRV, builder *dns.Builder) error {
|
|
|
|
for _, s := range srvs {
|
|
|
|
srvName, err := dns.NewName(s.Target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = builder.SRVResource(dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypeSRV,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}, dns.SRVResource{
|
|
|
|
Target: srvName,
|
|
|
|
Priority: s.Priority,
|
|
|
|
Port: s.Port,
|
|
|
|
Weight: s.Weight,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-12-09 20:01:19 +00:00
|
|
|
// TODO(bradfitz): I'm not sure why this wasn't enabled
|
|
|
|
// before, but for now (2021-12-09) enable it at least when
|
|
|
|
// there's more than 1 record (which was never the case
|
|
|
|
// before), where it really helps.
|
|
|
|
if len(resp.IPs) > 1 {
|
|
|
|
builder.EnableCompression()
|
|
|
|
}
|
|
|
|
|
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:
|
2021-12-09 20:01:19 +00:00
|
|
|
if err := marshalIP(resp.Question.Name, resp.IP, &builder); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ip := range resp.IPs {
|
|
|
|
if err := marshalIP(resp.Question.Name, ip, &builder); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
case dns.TypePTR:
|
|
|
|
err = marshalPTRRecord(resp.Question.Name, resp.Name, &builder)
|
2021-12-09 20:01:19 +00:00
|
|
|
case dns.TypeTXT:
|
|
|
|
err = marshalTXT(resp.Question.Name, resp.TXT, &builder)
|
|
|
|
case dns.TypeCNAME:
|
|
|
|
err = marshalCNAME(resp.Question.Name, resp.CNAME, &builder)
|
|
|
|
case dns.TypeSRV:
|
|
|
|
err = marshalSRV(resp.Question.Name, resp.SRVs, &builder)
|
|
|
|
case dns.TypeNS:
|
|
|
|
err = marshalNS(resp.Question.Name, resp.NSs, &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:
|
|
|
|
//
|
2022-08-02 16:33:46 +00:00
|
|
|
// 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()
|
2022-03-16 21:25:31 +00:00
|
|
|
base, rest, ok := strings.Cut(s, ".")
|
|
|
|
if !ok {
|
2020-09-23 19:05:51 +00:00
|
|
|
return false // shouldn't happen
|
|
|
|
}
|
2022-03-16 21:25:31 +00:00
|
|
|
switch base {
|
2020-09-23 19:05:51 +00:00
|
|
|
case "b", "db", "r", "dr", "lb":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-16 21:25:31 +00:00
|
|
|
return strings.HasPrefix(rest, "_dns-sd._udp.")
|
2020-09-23 19:05:51 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// 4.3.2.1.in-addr.arpa
|
|
|
|
//
|
2020-08-06 18:25:28 +00:00
|
|
|
// is transformed to
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// 1.2.3.4
|
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 rdnsNameToIPv4(name dnsname.FQDN) (ip netip.Addr, ok bool) {
|
2021-04-09 22:24:47 +00:00
|
|
|
s := strings.TrimSuffix(name.WithTrailingDot(), rdnsv4Suffix)
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(s)
|
2020-08-06 18:25:28 +00:00
|
|
|
if err != 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
|
|
|
return netip.Addr{}, false
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
if !ip.Is4() {
|
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
|
|
|
return netip.Addr{}, false
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
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,
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2020-08-06 18:25:28 +00:00
|
|
|
// is transformed to
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// 2001:db8::567:89ab
|
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 rdnsNameToIPv6(name dnsname.FQDN) (ip netip.Addr, 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 {
|
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
|
|
|
return netip.Addr{}, false
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
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
|
|
|
return netip.Addr{}, false
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
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 {
|
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
|
|
|
return netip.Addr{}, false
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:38:11 +00:00
|
|
|
return netip.AddrFrom16(ipb), true
|
2020-08-06 18:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2021-12-19 17:48:30 +00:00
|
|
|
metricDNSReverseMissBonjour.Add(1)
|
2020-09-23 19:05:51 +00:00
|
|
|
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 {
|
2021-12-19 17:48:30 +00:00
|
|
|
metricDNSReverseMissOther.Add(1)
|
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
|
2023-12-22 01:40:03 +00:00
|
|
|
metricDNSMagicDNSSuccessName.Add(1)
|
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
|
|
|
|
2021-12-09 20:01:19 +00:00
|
|
|
// unARPA maps from "4.4.8.8.in-addr.arpa." to "8.8.4.4", etc.
|
|
|
|
func unARPA(a string) (ipStr string, ok bool) {
|
|
|
|
const suf4 = ".in-addr.arpa."
|
2023-02-01 21:43:06 +00:00
|
|
|
if s, ok := strings.CutSuffix(a, suf4); ok {
|
2021-12-09 20:01:19 +00:00
|
|
|
// Parse and reverse octets.
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(s)
|
2021-12-09 20:01:19 +00:00
|
|
|
if err != nil || !ip.Is4() {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
a4 := ip.As4()
|
|
|
|
return netaddr.IPv4(a4[3], a4[2], a4[1], a4[0]).String(), true
|
|
|
|
}
|
|
|
|
const suf6 = ".ip6.arpa."
|
|
|
|
if len(a) == len("e.0.0.2.0.0.0.0.0.0.0.0.0.0.0.0.b.0.8.0.a.0.0.4.0.b.8.f.7.0.6.2.ip6.arpa.") &&
|
|
|
|
strings.HasSuffix(a, suf6) {
|
|
|
|
var hx [32]byte
|
|
|
|
var a16 [16]byte
|
|
|
|
for i := range hx {
|
|
|
|
hx[31-i] = a[i*2]
|
|
|
|
if a[i*2+1] != '.' {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hex.Decode(a16[:], hx[:])
|
2022-08-02 20:38:11 +00:00
|
|
|
return netip.AddrFrom16(a16).Unmap().String(), true
|
2021-12-09 20:01:19 +00:00
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:43:38 +00:00
|
|
|
var (
|
|
|
|
metricDNSQueryLocal = clientmetric.NewCounter("dns_query_local")
|
|
|
|
metricDNSQueryErrorClosed = clientmetric.NewCounter("dns_query_local_error_closed")
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-11-29 22:18:09 +00:00
|
|
|
metricDNSExitProxyQuery = clientmetric.NewCounter("dns_exit_node_query")
|
|
|
|
metricDNSExitProxyErrorName = clientmetric.NewCounter("dns_exit_node_error_name")
|
|
|
|
metricDNSExitProxyErrorForward = clientmetric.NewCounter("dns_exit_node_error_forward")
|
|
|
|
metricDNSExitProxyErrorResolvConf = clientmetric.NewCounter("dns_exit_node_error_resolvconf")
|
2021-11-26 22:43:38 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-12-22 01:40:03 +00:00
|
|
|
metricDNSFwdErrorType = clientmetric.NewCounter("dns_query_fwd_error_type")
|
|
|
|
metricDNSFwdTruncated = clientmetric.NewCounter("dns_query_fwd_truncated")
|
2021-11-26 22:43:38 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-09-07 20:27:50 +00:00
|
|
|
metricDNSFwdTCP = clientmetric.NewCounter("dns_query_fwd_tcp") // on entry
|
|
|
|
metricDNSFwdTCPWrote = clientmetric.NewCounter("dns_query_fwd_tcp_wrote") // sent TCP packet
|
|
|
|
metricDNSFwdTCPErrorWrite = clientmetric.NewCounter("dns_query_fwd_tcp_error_write")
|
|
|
|
metricDNSFwdTCPErrorServer = clientmetric.NewCounter("dns_query_fwd_tcp_error_server")
|
|
|
|
metricDNSFwdTCPErrorTxID = clientmetric.NewCounter("dns_query_fwd_tcp_error_txid")
|
|
|
|
metricDNSFwdTCPErrorRead = clientmetric.NewCounter("dns_query_fwd_tcp_error_read")
|
|
|
|
metricDNSFwdTCPSuccess = clientmetric.NewCounter("dns_query_fwd_tcp_success")
|
|
|
|
|
2021-11-26 22:43:38 +00:00
|
|
|
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")
|
2021-12-19 17:48:30 +00:00
|
|
|
|
|
|
|
metricDNSReverseMissBonjour = clientmetric.NewCounter("dns_reverse_miss_bonjour")
|
|
|
|
metricDNSReverseMissOther = clientmetric.NewCounter("dns_reverse_miss_other")
|
2021-11-26 22:43:38 +00:00
|
|
|
)
|