2021-02-26 20:49:54 +00:00
|
|
|
// Copyright (c) 2021 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.
|
|
|
|
|
2022-04-13 21:49:18 +01:00
|
|
|
//go:generate go run update-dns-fallbacks.go
|
|
|
|
|
2021-02-26 20:49:54 +00:00
|
|
|
// Package dnsfallback contains a DNS fallback mechanism
|
|
|
|
// for starting up Tailscale when the system DNS is broken or otherwise unavailable.
|
|
|
|
package dnsfallback
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-28 04:40:24 +01:00
|
|
|
_ "embed"
|
2021-02-26 20:49:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2021-02-26 20:49:54 +00:00
|
|
|
"net/url"
|
2022-09-05 19:36:30 +01:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sync/atomic"
|
2021-02-26 20:49:54 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-05 19:36:30 +01:00
|
|
|
"tailscale.com/atomicfile"
|
2021-02-26 20:49:54 +00:00
|
|
|
"tailscale.com/net/netns"
|
2021-10-01 05:13:38 +01:00
|
|
|
"tailscale.com/net/tlsdial"
|
2021-02-26 20:49:54 +00:00
|
|
|
"tailscale.com/net/tshttpproxy"
|
2022-09-06 16:19:50 +01:00
|
|
|
"tailscale.com/syncs"
|
2021-06-28 04:40:24 +01:00
|
|
|
"tailscale.com/tailcfg"
|
2022-09-06 16:19:50 +01:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-26 20:49:54 +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 05:14:09 +01:00
|
|
|
func Lookup(ctx context.Context, host string) ([]netip.Addr, error) {
|
2022-07-26 04:55:44 +01:00
|
|
|
if ip, err := netip.ParseAddr(host); err == nil && ip.IsValid() {
|
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 05:14:09 +01:00
|
|
|
return []netip.Addr{ip}, nil
|
2022-03-25 05:21:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:49:54 +00:00
|
|
|
type nameIP struct {
|
|
|
|
dnsName string
|
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 05:14:09 +01:00
|
|
|
ip netip.Addr
|
2021-02-26 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 04:40:24 +01:00
|
|
|
dm := getDERPMap()
|
2022-09-05 19:36:30 +01:00
|
|
|
|
2021-03-05 03:11:36 +00:00
|
|
|
var cands4, cands6 []nameIP
|
2021-02-26 20:49:54 +00:00
|
|
|
for _, dr := range dm.Regions {
|
|
|
|
for _, n := range dr.Nodes {
|
2022-07-26 04:55:44 +01:00
|
|
|
if ip, err := netip.ParseAddr(n.IPv4); err == nil {
|
2021-03-05 03:11:36 +00:00
|
|
|
cands4 = append(cands4, nameIP{n.HostName, ip})
|
2021-02-26 20:49:54 +00:00
|
|
|
}
|
2022-07-26 04:55:44 +01:00
|
|
|
if ip, err := netip.ParseAddr(n.IPv6); err == nil {
|
2021-03-05 03:11:36 +00:00
|
|
|
cands6 = append(cands6, nameIP{n.HostName, ip})
|
2021-02-26 20:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 03:11:36 +00:00
|
|
|
rand.Shuffle(len(cands4), func(i, j int) { cands4[i], cands4[j] = cands4[j], cands4[i] })
|
|
|
|
rand.Shuffle(len(cands6), func(i, j int) { cands6[i], cands6[j] = cands6[j], cands6[i] })
|
|
|
|
|
|
|
|
const maxCands = 6
|
|
|
|
var cands []nameIP // up to maxCands alternating v4/v6 as long as we have both
|
|
|
|
for (len(cands4) > 0 || len(cands6) > 0) && len(cands) < maxCands {
|
|
|
|
if len(cands4) > 0 {
|
|
|
|
cands = append(cands, cands4[0])
|
|
|
|
cands4 = cands4[1:]
|
|
|
|
}
|
|
|
|
if len(cands6) > 0 {
|
|
|
|
cands = append(cands, cands6[0])
|
|
|
|
cands6 = cands6[1:]
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 20:49:54 +00:00
|
|
|
if len(cands) == 0 {
|
|
|
|
return nil, fmt.Errorf("no DNS fallback options for %q", host)
|
|
|
|
}
|
2021-03-05 03:11:36 +00:00
|
|
|
for _, cand := range cands {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("trying bootstrapDNS(%q, %q) for %q ...", cand.dnsName, cand.ip, host)
|
2021-02-26 20:49:54 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
dm, err := bootstrapDNSMap(ctx, cand.dnsName, cand.ip, host)
|
|
|
|
if err != nil {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("bootstrapDNS(%q, %q) for %q error: %v", cand.dnsName, cand.ip, host, err)
|
2021-02-26 20:49:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ips := dm[host]; len(ips) > 0 {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("bootstrapDNS(%q, %q) for %q = %v", cand.dnsName, cand.ip, host, ips)
|
2021-02-26 20:49:54 +00:00
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no DNS fallback candidates remain for %q", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// serverName and serverIP of are, say, "derpN.tailscale.com".
|
2021-06-25 15:05:46 +01:00
|
|
|
// queryName is the name being sought (e.g. "controlplane.tailscale.com"), passed as hint.
|
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 05:14:09 +01:00
|
|
|
func bootstrapDNSMap(ctx context.Context, serverName string, serverIP netip.Addr, queryName string) (dnsMap, error) {
|
2022-09-06 16:19:50 +01:00
|
|
|
dialer := netns.NewDialer(logf)
|
2021-02-26 20:49:54 +00:00
|
|
|
tr := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
tr.Proxy = tshttpproxy.ProxyFromEnvironment
|
|
|
|
tr.DialContext = func(ctx context.Context, netw, addr string) (net.Conn, error) {
|
|
|
|
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(serverIP.String(), "443"))
|
|
|
|
}
|
2021-10-01 05:13:38 +01:00
|
|
|
tr.TLSClientConfig = tlsdial.Config(serverName, tr.TLSClientConfig)
|
2021-02-26 20:49:54 +00:00
|
|
|
c := &http.Client{Transport: tr}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", "https://"+serverName+"/bootstrap-dns?q="+url.QueryEscape(queryName), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dm := make(dnsMap)
|
|
|
|
res, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, errors.New(res.Status)
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&dm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsMap is the JSON type returned by the DERP /bootstrap-dns handler:
|
|
|
|
// https://derp10.tailscale.com/bootstrap-dns
|
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 05:14:09 +01:00
|
|
|
type dnsMap map[string][]netip.Addr
|
2021-06-28 04:40:24 +01:00
|
|
|
|
|
|
|
// getDERPMap returns some DERP map. The DERP servers also run a fallback
|
|
|
|
// DNS server.
|
|
|
|
func getDERPMap() *tailcfg.DERPMap {
|
2022-09-05 19:36:30 +01:00
|
|
|
dm := getStaticDERPMap()
|
|
|
|
|
|
|
|
// Merge in any DERP servers from the cached map that aren't in the
|
|
|
|
// static map; this ensures that we're getting new region(s) while not
|
|
|
|
// overriding the built-in fallbacks if things go horribly wrong and we
|
|
|
|
// get a bad DERP map.
|
|
|
|
//
|
|
|
|
// TODO(andrew): should we expect OmitDefaultRegions here? We're not
|
|
|
|
// forwarding traffic, just resolving DNS, so maybe we can ignore that
|
|
|
|
// value anyway?
|
|
|
|
cached := cachedDERPMap.Load()
|
|
|
|
if cached == nil {
|
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, region := range cached.Regions {
|
|
|
|
dr, ok := dm.Regions[id]
|
|
|
|
if !ok {
|
|
|
|
dm.Regions[id] = region
|
|
|
|
continue
|
|
|
|
}
|
2021-06-28 04:40:24 +01:00
|
|
|
|
2022-09-05 19:36:30 +01:00
|
|
|
// Add any nodes that we don't already have.
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
for _, n := range dr.Nodes {
|
|
|
|
seen[n.HostName] = true
|
|
|
|
}
|
|
|
|
for _, n := range region.Nodes {
|
|
|
|
if !seen[n.HostName] {
|
|
|
|
dr.Nodes = append(dr.Nodes, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStaticDERPMap returns the DERP map that was compiled into this binary.
|
|
|
|
func getStaticDERPMap() *tailcfg.DERPMap {
|
2021-06-28 04:40:24 +01:00
|
|
|
dm := new(tailcfg.DERPMap)
|
|
|
|
if err := json.Unmarshal(staticDERPMapJSON, dm); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed dns-fallback-servers.json
|
|
|
|
var staticDERPMapJSON []byte
|
2022-09-05 19:36:30 +01:00
|
|
|
|
|
|
|
// cachedDERPMap is the path to a cached DERP map that we loaded from our on-disk cache.
|
|
|
|
var cachedDERPMap atomic.Pointer[tailcfg.DERPMap]
|
|
|
|
|
|
|
|
// cachePath is the path to the DERP map cache file, set by SetCachePath via
|
|
|
|
// ipnserver.New() if we have a state directory.
|
|
|
|
var cachePath string
|
|
|
|
|
|
|
|
// UpdateCache stores the DERP map cache back to disk.
|
|
|
|
//
|
|
|
|
// The caller must not mutate 'c' after calling this function.
|
|
|
|
func UpdateCache(c *tailcfg.DERPMap) {
|
|
|
|
// Don't do anything if nothing changed.
|
|
|
|
curr := cachedDERPMap.Load()
|
|
|
|
if reflect.DeepEqual(curr, c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("[v1] dnsfallback: UpdateCache error marshaling: %v", err)
|
2022-09-05 19:36:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only store after we're confident this is at least valid JSON
|
|
|
|
cachedDERPMap.Store(c)
|
|
|
|
|
|
|
|
// Don't try writing if we don't have a cache path set; this can happen
|
|
|
|
// when we don't have a state path (e.g. /var/lib/tailscale) configured.
|
|
|
|
if cachePath != "" {
|
|
|
|
err = atomicfile.WriteFile(cachePath, d, 0600)
|
|
|
|
if err != nil {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("[v1] dnsfallback: UpdateCache error writing: %v", err)
|
2022-09-05 19:36:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCachePath sets the path to the on-disk DERP map cache that we store and
|
|
|
|
// update. Additionally, if a file at this path exists, we load it and merge it
|
|
|
|
// with the DERP map baked into the binary.
|
|
|
|
//
|
|
|
|
// This function should be called before any calls to UpdateCache, as it is not
|
|
|
|
// concurrency-safe.
|
|
|
|
func SetCachePath(path string) {
|
|
|
|
cachePath = path
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("[v1] dnsfallback: SetCachePath error reading %q: %v", path, err)
|
2022-09-05 19:36:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
dm := new(tailcfg.DERPMap)
|
|
|
|
if err := json.NewDecoder(f).Decode(dm); err != nil {
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("[v1] dnsfallback: SetCachePath error decoding %q: %v", path, err)
|
2022-09-05 19:36:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedDERPMap.Store(dm)
|
2022-09-06 16:19:50 +01:00
|
|
|
logf("[v2] dnsfallback: SetCachePath loaded cached DERP map")
|
|
|
|
}
|
|
|
|
|
|
|
|
// logfunc stores the logging function to use for this package.
|
|
|
|
var logfunc syncs.AtomicValue[logger.Logf]
|
|
|
|
|
|
|
|
// SetLogger sets the logging function that this package will use. The default
|
|
|
|
// logger if this function is not called is 'log.Printf'.
|
|
|
|
func SetLogger(log logger.Logf) {
|
|
|
|
logfunc.Store(log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logf(format string, args ...any) {
|
|
|
|
if lf := logfunc.Load(); lf != nil {
|
|
|
|
lf(format, args...)
|
|
|
|
} else {
|
|
|
|
log.Printf(format, args...)
|
|
|
|
}
|
2022-09-05 19:36:30 +01:00
|
|
|
}
|