2020-06-08 23:19:26 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-06-09 18:09:43 +01:00
|
|
|
// Package tsdns provides a Resolver capable of resolving
|
2020-06-08 23:19:26 +01:00
|
|
|
// domains on a Tailscale network.
|
|
|
|
package tsdns
|
|
|
|
|
|
|
|
import (
|
2020-08-06 19:25:28 +01:00
|
|
|
"encoding/hex"
|
2020-06-08 23:19:26 +01:00
|
|
|
"errors"
|
2020-08-19 20:39:25 +01:00
|
|
|
"net"
|
2020-08-20 23:54:18 +01:00
|
|
|
"strings"
|
2020-06-08 23:19:26 +01:00
|
|
|
"sync"
|
2020-06-09 18:09:43 +01:00
|
|
|
"time"
|
2020-06-08 23:19:26 +01:00
|
|
|
|
|
|
|
dns "golang.org/x/net/dns/dnsmessage"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2020-08-19 20:39:25 +01:00
|
|
|
// maxResponseBytes is the maximum size of a response from a Resolver.
|
|
|
|
const maxResponseBytes = 512
|
2020-07-07 20:25:32 +01:00
|
|
|
|
2020-08-27 00:17:20 +01:00
|
|
|
// queueSize is the maximal number of DNS requests that can await polling.
|
2020-07-07 20:25:32 +01:00
|
|
|
// If EnqueueRequest is called when this many requests are already pending,
|
|
|
|
// the request will be dropped to avoid blocking the caller.
|
2020-08-27 00:17:20 +01:00
|
|
|
const queueSize = 64
|
2020-07-07 20:25:32 +01:00
|
|
|
|
2020-06-09 18:09:43 +01:00
|
|
|
// defaultTTL is the TTL of all responses from Resolver.
|
|
|
|
const defaultTTL = 600 * time.Second
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// ErrClosed indicates that the resolver has been closed and readers should exit.
|
|
|
|
var ErrClosed = errors.New("closed")
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
var (
|
2020-07-07 20:25:32 +01:00
|
|
|
errFullQueue = errors.New("request queue full")
|
2020-06-08 23:19:26 +01:00
|
|
|
errMapNotSet = errors.New("domain map not set")
|
2020-08-19 20:39:25 +01:00
|
|
|
errNotForwarding = errors.New("forwarding disabled")
|
2020-06-08 23:19:26 +01:00
|
|
|
errNotImplemented = errors.New("query type not implemented")
|
|
|
|
errNotQuery = errors.New("not a DNS query")
|
2020-08-19 20:39:25 +01:00
|
|
|
errNotOurName = errors.New("not a Tailscale DNS name")
|
2020-06-08 23:19:26 +01:00
|
|
|
)
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// Packet represents a DNS payload together with the address of its origin.
|
|
|
|
type Packet struct {
|
|
|
|
// Payload is the application layer DNS payload.
|
|
|
|
// Resolver assumes ownership of the request payload when it is enqueued
|
|
|
|
// and cedes ownership of the response payload when it is returned from NextResponse.
|
|
|
|
Payload []byte
|
|
|
|
// Addr is the source address for a request and the destination address for a response.
|
|
|
|
Addr netaddr.IPPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 23:19:26 +01:00
|
|
|
type Resolver struct {
|
|
|
|
logf logger.Logf
|
2020-08-24 22:27:21 +01:00
|
|
|
// forwarder forwards requests to upstream nameservers.
|
2020-08-19 20:39:25 +01:00
|
|
|
forwarder *forwarder
|
2020-07-07 20:25:32 +01:00
|
|
|
|
|
|
|
// queue is a buffered channel holding DNS requests queued for resolution.
|
|
|
|
queue chan Packet
|
2020-08-19 20:39:25 +01:00
|
|
|
// responses is an unbuffered channel to which responses are returned.
|
2020-07-07 20:25:32 +01:00
|
|
|
responses chan Packet
|
2020-08-19 20:39:25 +01:00
|
|
|
// errors is an unbuffered channel to which errors are returned.
|
2020-07-07 20:25:32 +01:00
|
|
|
errors chan error
|
2020-08-19 20:39:25 +01:00
|
|
|
// closed signals all goroutines to stop.
|
2020-07-07 20:25:32 +01:00
|
|
|
closed chan struct{}
|
2020-08-19 20:39:25 +01:00
|
|
|
// wg signals when all goroutines have stopped.
|
|
|
|
wg sync.WaitGroup
|
2020-06-08 23:19:26 +01:00
|
|
|
|
|
|
|
// mu guards the following fields from being updated while used.
|
2020-08-06 19:25:28 +01:00
|
|
|
mu sync.Mutex
|
2020-06-08 23:19:26 +01:00
|
|
|
// dnsMap is the map most recently received from the control server.
|
|
|
|
dnsMap *Map
|
2020-08-19 20:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolverConfig is the set of configuration options for a Resolver.
|
|
|
|
type ResolverConfig struct {
|
|
|
|
// Logf is the logger to use throughout the Resolver.
|
|
|
|
Logf logger.Logf
|
|
|
|
// Forward determines whether the resolver will forward packets to
|
|
|
|
// nameservers set with SetUpstreams if the domain name is not of a Tailscale node.
|
|
|
|
Forward bool
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// NewResolver constructs a resolver associated with the given root domain.
|
2020-08-06 19:25:28 +01:00
|
|
|
// The root domain must be in canonical form (with a trailing period).
|
2020-08-19 20:39:25 +01:00
|
|
|
func NewResolver(config ResolverConfig) *Resolver {
|
2020-06-08 23:19:26 +01:00
|
|
|
r := &Resolver{
|
2020-08-24 22:27:21 +01:00
|
|
|
logf: logger.WithPrefix(config.Logf, "tsdns: "),
|
2020-08-27 00:17:20 +01:00
|
|
|
queue: make(chan Packet, queueSize),
|
2020-08-24 22:27:21 +01:00
|
|
|
responses: make(chan Packet),
|
|
|
|
errors: make(chan error),
|
|
|
|
closed: make(chan struct{}),
|
2020-08-19 20:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Forward {
|
|
|
|
r.forwarder = newForwarder(r.logf, r.responses)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:39:25 +01:00
|
|
|
func (r *Resolver) Start() error {
|
|
|
|
if r.forwarder != nil {
|
|
|
|
if err := r.forwarder.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
2020-07-07 20:25:32 +01:00
|
|
|
go r.poll()
|
2020-08-19 20:39:25 +01:00
|
|
|
|
|
|
|
return nil
|
2020-07-07 20:25:32 +01: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 20:39:25 +01:00
|
|
|
|
|
|
|
if r.forwarder != nil {
|
|
|
|
r.forwarder.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Wait()
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// SetMap sets the resolver's DNS map, taking ownership of it.
|
2020-06-08 23:19:26 +01:00
|
|
|
func (r *Resolver) SetMap(m *Map) {
|
|
|
|
r.mu.Lock()
|
2020-07-29 02:47:23 +01:00
|
|
|
oldMap := r.dnsMap
|
2020-06-08 23:19:26 +01:00
|
|
|
r.dnsMap = m
|
|
|
|
r.mu.Unlock()
|
2020-07-29 02:47:23 +01:00
|
|
|
r.logf("map diff:\n%s", m.PrettyDiffFrom(oldMap))
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 20:39:25 +01:00
|
|
|
// SetUpstreams sets the addresses of the resolver's
|
2020-07-07 20:25:32 +01:00
|
|
|
// upstream nameservers, taking ownership of the argument.
|
2020-08-19 20:39:25 +01:00
|
|
|
func (r *Resolver) SetUpstreams(upstreams []net.Addr) {
|
|
|
|
if r.forwarder != nil {
|
|
|
|
r.forwarder.setUpstreams(upstreams)
|
|
|
|
}
|
2020-08-27 21:59:58 +01:00
|
|
|
r.logf("set upstreams: %v", upstreams)
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnqueueRequest places the given DNS request in the resolver's queue.
|
|
|
|
// It takes ownership of the payload and does not block.
|
|
|
|
// If the queue is full, the request will be dropped and an error will be returned.
|
|
|
|
func (r *Resolver) EnqueueRequest(request Packet) error {
|
|
|
|
select {
|
2020-08-19 20:39:25 +01:00
|
|
|
case <-r.closed:
|
|
|
|
return ErrClosed
|
2020-07-07 20:25:32 +01:00
|
|
|
case r.queue <- request:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errFullQueue
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// NextResponse returns a DNS response to a previously enqueued request.
|
|
|
|
// It blocks until a response is available and gives up ownership of the response payload.
|
|
|
|
func (r *Resolver) NextResponse() (Packet, error) {
|
|
|
|
select {
|
2020-08-19 20:39:25 +01:00
|
|
|
case <-r.closed:
|
|
|
|
return Packet{}, ErrClosed
|
2020-07-07 20:25:32 +01:00
|
|
|
case resp := <-r.responses:
|
|
|
|
return resp, nil
|
|
|
|
case err := <-r.errors:
|
|
|
|
return Packet{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 05:40:30 +01:00
|
|
|
// Resolve maps a given domain name to the IP address of the host that owns it,
|
|
|
|
// if the IP address conforms to the DNS resource type given by tp (one of A, AAAA, ALL).
|
2020-08-06 19:25:28 +01:00
|
|
|
// The domain name must be in canonical form (with a trailing period).
|
2020-08-27 05:07:15 +01:00
|
|
|
func (r *Resolver) Resolve(domain string, tp dns.Type) (netaddr.IP, dns.RCode, error) {
|
2020-08-06 19:25:28 +01:00
|
|
|
r.mu.Lock()
|
|
|
|
dnsMap := r.dnsMap
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
if dnsMap == nil {
|
2020-06-08 23:19:26 +01:00
|
|
|
return netaddr.IP{}, dns.RCodeServerFailure, errMapNotSet
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:27:21 +01:00
|
|
|
anyHasSuffix := false
|
|
|
|
for _, rootDomain := range dnsMap.rootDomains {
|
|
|
|
if strings.HasSuffix(domain, rootDomain) {
|
|
|
|
anyHasSuffix = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !anyHasSuffix {
|
|
|
|
return netaddr.IP{}, dns.RCodeRefused, nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:25:28 +01:00
|
|
|
addr, found := dnsMap.nameToIP[domain]
|
2020-06-08 23:19:26 +01:00
|
|
|
if !found {
|
2020-07-07 20:25:32 +01:00
|
|
|
return netaddr.IP{}, dns.RCodeNameError, nil
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-08-27 05:07:15 +01:00
|
|
|
|
2020-08-27 05:40:30 +01:00
|
|
|
// Refactoring note: this must happen after we check suffixes,
|
|
|
|
// otherwise we will respond with NOTIMP to requests that should be forwarded.
|
|
|
|
switch {
|
|
|
|
case tp == dns.TypeA || tp == dns.TypeALL:
|
|
|
|
if !addr.Is4() {
|
|
|
|
return netaddr.IP{}, dns.RCodeSuccess, nil
|
|
|
|
}
|
|
|
|
return addr, dns.RCodeSuccess, nil
|
|
|
|
case tp == dns.TypeAAAA || tp == dns.TypeALL:
|
|
|
|
if !addr.Is6() {
|
|
|
|
return netaddr.IP{}, dns.RCodeSuccess, nil
|
|
|
|
}
|
2020-08-27 05:07:15 +01:00
|
|
|
return addr, dns.RCodeSuccess, nil
|
|
|
|
default:
|
|
|
|
return netaddr.IP{}, dns.RCodeNotImplemented, errNotImplemented
|
|
|
|
}
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:25:28 +01:00
|
|
|
// ResolveReverse returns the unique domain name that maps to the given address.
|
|
|
|
// The returned domain name is in canonical form (with a trailing period).
|
|
|
|
func (r *Resolver) ResolveReverse(ip netaddr.IP) (string, dns.RCode, error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
dnsMap := r.dnsMap
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
if dnsMap == nil {
|
|
|
|
return "", dns.RCodeServerFailure, errMapNotSet
|
|
|
|
}
|
|
|
|
name, found := dnsMap.ipToName[ip]
|
|
|
|
if !found {
|
|
|
|
return "", dns.RCodeNameError, nil
|
|
|
|
}
|
|
|
|
return name, dns.RCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
func (r *Resolver) poll() {
|
2020-08-19 20:39:25 +01:00
|
|
|
defer r.wg.Done()
|
2020-07-07 20:25:32 +01:00
|
|
|
|
2020-08-19 20:39:25 +01:00
|
|
|
var packet Packet
|
2020-07-07 20:25:32 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
return
|
2020-08-19 20:39:25 +01:00
|
|
|
case packet = <-r.queue:
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := r.respond(packet.Payload)
|
|
|
|
|
|
|
|
if err == errNotOurName {
|
|
|
|
if r.forwarder != nil {
|
|
|
|
err = r.forwarder.forward(packet)
|
|
|
|
if err == nil {
|
|
|
|
// forward will send response into r.responses, nothing to do.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errNotForwarding
|
|
|
|
}
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
return
|
2020-08-19 20:39:25 +01:00
|
|
|
case r.errors <- err:
|
|
|
|
// continue
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-19 20:39:25 +01:00
|
|
|
packet.Payload = out
|
2020-07-07 20:25:32 +01:00
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
return
|
2020-08-19 20:39:25 +01:00
|
|
|
case r.responses <- packet:
|
|
|
|
// continue
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
type response struct {
|
2020-07-07 20:25:32 +01:00
|
|
|
Header dns.Header
|
|
|
|
Question dns.Question
|
2020-08-06 19:25:28 +01:00
|
|
|
// Name is the response to a PTR query.
|
|
|
|
Name string
|
2020-08-27 05:40:30 +01:00
|
|
|
// IP is the response to an A, AAAA, or ALL query.
|
2020-08-06 19:25:28 +01:00
|
|
|
IP netaddr.IP
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseQuery parses the query in given packet into a response struct.
|
2020-07-07 20:25:32 +01:00
|
|
|
func (r *Resolver) parseQuery(query []byte, resp *response) error {
|
2020-06-08 23:19:26 +01:00
|
|
|
var parser dns.Parser
|
|
|
|
var err error
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
resp.Header, err = parser.Start(query)
|
2020-06-08 23:19:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Header.Response {
|
|
|
|
return errNotQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Question, err = parser.Question()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// marshalARecord serializes an A record into an active builder.
|
2020-06-09 18:09:43 +01:00
|
|
|
// The caller may continue using the builder following the call.
|
2020-07-07 20:25:32 +01:00
|
|
|
func marshalARecord(name dns.Name, ip netaddr.IP, builder *dns.Builder) error {
|
2020-06-08 23:19:26 +01:00
|
|
|
var answer dns.AResource
|
|
|
|
|
|
|
|
answerHeader := dns.ResourceHeader{
|
2020-07-07 20:25:32 +01:00
|
|
|
Name: name,
|
2020-06-08 23:19:26 +01:00
|
|
|
Type: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
2020-06-09 18:09:43 +01:00
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-07-07 20:25:32 +01:00
|
|
|
ipbytes := ip.As4()
|
|
|
|
copy(answer.A[:], ipbytes[:])
|
2020-06-08 23:19:26 +01:00
|
|
|
return builder.AResource(answerHeader, answer)
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// marshalAAAARecord serializes an AAAA record into an active builder.
|
2020-06-09 18:09:43 +01:00
|
|
|
// The caller may continue using the builder following the call.
|
2020-07-07 20:25:32 +01:00
|
|
|
func marshalAAAARecord(name dns.Name, ip netaddr.IP, builder *dns.Builder) error {
|
|
|
|
var answer dns.AAAAResource
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
answerHeader := dns.ResourceHeader{
|
|
|
|
Name: name,
|
|
|
|
Type: dns.TypeAAAA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-07-07 20:25:32 +01:00
|
|
|
ipbytes := ip.As16()
|
|
|
|
copy(answer.AAAA[:], ipbytes[:])
|
|
|
|
return builder.AAAAResource(answerHeader, answer)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:25:28 +01:00
|
|
|
// marshalPTRRecord serializes a PTR record into an active builder.
|
|
|
|
// The caller may continue using the builder following the call.
|
|
|
|
func marshalPTRRecord(queryName dns.Name, name string, builder *dns.Builder) error {
|
|
|
|
var answer dns.PTRResource
|
|
|
|
var err error
|
|
|
|
|
|
|
|
answerHeader := dns.ResourceHeader{
|
|
|
|
Name: queryName,
|
|
|
|
Type: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
TTL: uint32(defaultTTL / time.Second),
|
|
|
|
}
|
|
|
|
answer.PTR, err = dns.NewName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return builder.PTRResource(answerHeader, answer)
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// marshalResponse serializes the DNS response into a new buffer.
|
|
|
|
func marshalResponse(resp *response) ([]byte, error) {
|
2020-06-09 18:09:43 +01:00
|
|
|
resp.Header.Response = true
|
|
|
|
resp.Header.Authoritative = true
|
|
|
|
if resp.Header.RecursionDesired {
|
|
|
|
resp.Header.RecursionAvailable = true
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
builder := dns.NewBuilder(nil, resp.Header)
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
err := builder.StartQuestions()
|
2020-06-09 18:09:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
err = builder.Question(resp.Question)
|
2020-06-08 23:19:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
// Only successful responses contain answers.
|
|
|
|
if resp.Header.RCode != dns.RCodeSuccess {
|
|
|
|
return builder.Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = builder.StartAnswers()
|
2020-06-08 23:19:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:25:28 +01:00
|
|
|
switch resp.Question.Type {
|
|
|
|
case dns.TypeA, dns.TypeAAAA, dns.TypeALL:
|
|
|
|
if resp.IP.Is4() {
|
|
|
|
err = marshalARecord(resp.Question.Name, resp.IP, &builder)
|
2020-08-27 05:40:30 +01:00
|
|
|
} else if resp.IP.Is6() {
|
2020-08-06 19:25:28 +01:00
|
|
|
err = marshalAAAARecord(resp.Question.Name, resp.IP, &builder)
|
|
|
|
}
|
|
|
|
case dns.TypePTR:
|
|
|
|
err = marshalPTRRecord(resp.Question.Name, resp.Name, &builder)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-07-07 20:25:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
return builder.Finish()
|
|
|
|
}
|
|
|
|
|
2020-08-20 23:54:18 +01:00
|
|
|
const (
|
|
|
|
rdnsv4Suffix = ".in-addr.arpa."
|
|
|
|
rdnsv6Suffix = ".ip6.arpa."
|
2020-08-06 19:25:28 +01:00
|
|
|
)
|
|
|
|
|
2020-08-20 23:54:18 +01: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 19:25:28 +01:00
|
|
|
// ptrNameToIPv4 transforms a PTR name representing an IPv4 address to said address.
|
|
|
|
// Such names are IPv4 labels in reverse order followed by .in-addr.arpa.
|
|
|
|
// For example,
|
|
|
|
// 4.3.2.1.in-addr.arpa
|
|
|
|
// is transformed to
|
|
|
|
// 1.2.3.4
|
2020-08-20 23:54:18 +01:00
|
|
|
func rdnsNameToIPv4(name string) (ip netaddr.IP, ok bool) {
|
|
|
|
name = strings.TrimSuffix(name, rdnsv4Suffix)
|
2020-08-06 19:25:28 +01:00
|
|
|
ip, err := netaddr.ParseIP(string(name))
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
if !ip.Is4() {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
b := ip.As4()
|
|
|
|
return netaddr.IPv4(b[3], b[2], b[1], b[0]), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptrNameToIPv6 transforms a PTR name representing an IPv6 address to said address.
|
|
|
|
// Such names are dot-separated nibbles in reverse order followed by .ip6.arpa.
|
|
|
|
// For example,
|
|
|
|
// b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.
|
|
|
|
// is transformed to
|
|
|
|
// 2001:db8::567:89ab
|
2020-08-20 23:54:18 +01:00
|
|
|
func rdnsNameToIPv6(name string) (ip netaddr.IP, ok bool) {
|
2020-08-06 19:25:28 +01:00
|
|
|
var b [32]byte
|
|
|
|
var ipb [16]byte
|
|
|
|
|
2020-08-20 23:54:18 +01:00
|
|
|
name = strings.TrimSuffix(name, rdnsv6Suffix)
|
2020-08-06 19:25:28 +01:00
|
|
|
// 32 nibbles and 31 dots between them.
|
|
|
|
if len(name) != 63 {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dots and hex digits alternate.
|
|
|
|
prevDot := true
|
|
|
|
// i ranges over name backward; j ranges over b forward.
|
|
|
|
for i, j := len(name)-1, 0; i >= 0; i-- {
|
|
|
|
thisDot := (name[i] == '.')
|
|
|
|
if prevDot == thisDot {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
prevDot = thisDot
|
|
|
|
|
|
|
|
if !thisDot {
|
|
|
|
// This is safe assuming alternation.
|
|
|
|
// We do not check that non-dots are hex digits: hex.Decode below will do that.
|
|
|
|
b[j] = name[i]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := hex.Decode(ipb[:], b[:])
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IP{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return netaddr.IPFrom16(ipb), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// respondReverse returns a DNS response to a PTR query.
|
|
|
|
// It is assumed that resp.Question is populated by respond before this is called.
|
2020-08-20 23:54:18 +01:00
|
|
|
func (r *Resolver) respondReverse(query []byte, name string, resp *response) ([]byte, error) {
|
2020-08-06 19:25:28 +01:00
|
|
|
var ip netaddr.IP
|
|
|
|
var ok bool
|
|
|
|
var err error
|
|
|
|
switch {
|
2020-08-20 23:54:18 +01:00
|
|
|
case strings.HasSuffix(name, rdnsv4Suffix):
|
2020-08-06 19:25:28 +01:00
|
|
|
ip, ok = rdnsNameToIPv4(name)
|
2020-08-20 23:54:18 +01:00
|
|
|
case strings.HasSuffix(name, rdnsv6Suffix):
|
2020-08-06 19:25:28 +01:00
|
|
|
ip, ok = rdnsNameToIPv6(name)
|
|
|
|
default:
|
2020-08-19 20:39:25 +01:00
|
|
|
return nil, errNotOurName
|
2020-08-06 19:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is more likely that we failed in parsing the name than that it is actually malformed.
|
|
|
|
// To avoid frustrating users, just log and delegate.
|
|
|
|
if !ok {
|
2020-08-20 23:54:18 +01:00
|
|
|
r.logf("parsing rdns: malformed name: %s", name)
|
2020-08-19 20:39:25 +01:00
|
|
|
return nil, errNotOurName
|
2020-08-06 19:25:28 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 20:39:25 +01:00
|
|
|
resp.Name, resp.Header.RCode, err = r.ResolveReverse(ip)
|
|
|
|
if err != nil {
|
|
|
|
r.logf("resolving rdns: %v", ip, err)
|
2020-08-06 19:25:28 +01:00
|
|
|
}
|
2020-08-19 20:39:25 +01:00
|
|
|
if resp.Header.RCode == dns.RCodeNameError {
|
|
|
|
return nil, errNotOurName
|
2020-08-06 19:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return marshalResponse(resp)
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:27:21 +01:00
|
|
|
// respond returns a DNS response to query if it can be resolved locally.
|
|
|
|
// Otherwise, it returns errNotOurName.
|
2020-07-07 20:25:32 +01:00
|
|
|
func (r *Resolver) respond(query []byte) ([]byte, error) {
|
|
|
|
resp := new(response)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
err := r.parseQuery(query, resp)
|
2020-06-08 23:19:26 +01:00
|
|
|
// We will not return this error: it is the sender's fault.
|
|
|
|
if err != nil {
|
2020-07-07 20:25:32 +01:00
|
|
|
r.logf("parsing query: %v", err)
|
2020-06-08 23:19:26 +01:00
|
|
|
resp.Header.RCode = dns.RCodeFormatError
|
2020-07-07 20:25:32 +01:00
|
|
|
return marshalResponse(resp)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
2020-08-20 23:54:18 +01:00
|
|
|
rawName := resp.Question.Name.Data[:resp.Question.Name.Length]
|
|
|
|
name := rawNameToLower(rawName)
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-08-06 19:25:28 +01:00
|
|
|
// Always try to handle reverse lookups; delegate inside when not found.
|
|
|
|
// This way, queries for exitent nodes do not leak,
|
|
|
|
// but we behave gracefully if non-Tailscale nodes exist in CGNATRange.
|
|
|
|
if resp.Question.Type == dns.TypePTR {
|
2020-08-20 23:54:18 +01:00
|
|
|
return r.respondReverse(query, name, resp)
|
2020-08-06 19:25:28 +01:00
|
|
|
}
|
|
|
|
|
2020-08-27 05:07:15 +01:00
|
|
|
resp.IP, resp.Header.RCode, err = r.Resolve(name, resp.Question.Type)
|
|
|
|
// This return code is special: it requests forwarding.
|
|
|
|
if resp.Header.RCode == dns.RCodeRefused {
|
|
|
|
return nil, errNotOurName
|
2020-07-07 20:25:32 +01:00
|
|
|
}
|
2020-08-27 05:07:15 +01:00
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// We will not return this error: it is the sender's fault.
|
|
|
|
if err != nil {
|
2020-07-07 20:25:32 +01:00
|
|
|
r.logf("resolving: %v", err)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:25:32 +01:00
|
|
|
return marshalResponse(resp)
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|