2023-10-27 21:20:10 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// Package appc implements App Connectors.
|
|
|
|
// An AppConnector provides DNS domain oriented routing of traffic. An App
|
|
|
|
// Connector becomes a DNS server for a peer, authoritative for the set of
|
|
|
|
// configured domains. DNS resolution of the target domain triggers dynamic
|
|
|
|
// publication of routes to ensure that traffic to the domain is routed through
|
|
|
|
// the App Connector.
|
2023-10-27 21:20:10 +00:00
|
|
|
package appc
|
|
|
|
|
|
|
|
import (
|
2024-01-18 18:18:25 +00:00
|
|
|
"context"
|
2023-10-27 21:20:10 +00:00
|
|
|
"net/netip"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2023-10-31 21:59:18 +00:00
|
|
|
xmaps "golang.org/x/exp/maps"
|
2023-10-27 21:20:10 +00:00
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
|
|
|
"tailscale.com/types/logger"
|
2023-10-31 21:59:18 +00:00
|
|
|
"tailscale.com/types/views"
|
2023-11-08 18:57:16 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2024-01-18 18:18:25 +00:00
|
|
|
"tailscale.com/util/execqueue"
|
2023-10-27 21:20:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RouteAdvertiser is an interface that allows the AppConnector to advertise
|
|
|
|
// newly discovered routes that need to be served through the AppConnector.
|
|
|
|
type RouteAdvertiser interface {
|
|
|
|
// AdvertiseRoute adds a new route advertisement if the route is not already
|
|
|
|
// being advertised.
|
|
|
|
AdvertiseRoute(netip.Prefix) error
|
2024-01-17 19:35:55 +00:00
|
|
|
|
|
|
|
// UnadvertiseRoute removes a route advertisement.
|
|
|
|
UnadvertiseRoute(netip.Prefix) error
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// AppConnector is an implementation of an AppConnector that performs
|
2023-10-27 21:20:10 +00:00
|
|
|
// its function as a subsystem inside of a tailscale node. At the control plane
|
|
|
|
// side App Connector routing is configured in terms of domains rather than IP
|
|
|
|
// addresses.
|
|
|
|
// The AppConnectors responsibility inside tailscaled is to apply the routing
|
|
|
|
// and domain configuration as supplied in the map response.
|
|
|
|
// DNS requests for configured domains are observed. If the domains resolve to
|
|
|
|
// routes not yet served by the AppConnector the local node configuration is
|
|
|
|
// updated to advertise the new route.
|
2023-11-01 23:56:30 +00:00
|
|
|
type AppConnector struct {
|
2023-10-27 21:20:10 +00:00
|
|
|
logf logger.Logf
|
|
|
|
routeAdvertiser RouteAdvertiser
|
|
|
|
|
|
|
|
// mu guards the fields that follow
|
|
|
|
mu sync.Mutex
|
2024-01-17 19:35:55 +00:00
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// domains is a map of lower case domain names with no trailing dot, to a
|
|
|
|
// list of resolved IP addresses.
|
|
|
|
domains map[string][]netip.Addr
|
2023-11-08 18:57:16 +00:00
|
|
|
|
2024-01-17 19:35:55 +00:00
|
|
|
// controlRoutes is the list of routes that were last supplied by control.
|
|
|
|
controlRoutes []netip.Prefix
|
|
|
|
|
2023-11-08 18:57:16 +00:00
|
|
|
// wildcards is the list of domain strings that match subdomains.
|
|
|
|
wildcards []string
|
2024-01-18 18:18:25 +00:00
|
|
|
|
|
|
|
// queue provides ordering for update operations
|
|
|
|
queue execqueue.ExecQueue
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// NewAppConnector creates a new AppConnector.
|
|
|
|
func NewAppConnector(logf logger.Logf, routeAdvertiser RouteAdvertiser) *AppConnector {
|
|
|
|
return &AppConnector{
|
2023-10-27 21:20:10 +00:00
|
|
|
logf: logger.WithPrefix(logf, "appc: "),
|
|
|
|
routeAdvertiser: routeAdvertiser,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:18:25 +00:00
|
|
|
// UpdateDomainsAndRoutes starts an asynchronous update of the configuration
|
|
|
|
// given the new domains and routes.
|
|
|
|
func (e *AppConnector) UpdateDomainsAndRoutes(domains []string, routes []netip.Prefix) {
|
|
|
|
e.queue.Add(func() {
|
|
|
|
// Add the new routes first.
|
|
|
|
e.updateRoutes(routes)
|
|
|
|
e.updateDomains(domains)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDomains asynchronously replaces the current set of configured domains
|
|
|
|
// with the supplied set of domains. Domains must not contain a trailing dot,
|
|
|
|
// and should be lower case. If the domain contains a leading '*' label it
|
|
|
|
// matches all subdomains of a domain.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) UpdateDomains(domains []string) {
|
2024-01-18 18:18:25 +00:00
|
|
|
e.queue.Add(func() {
|
|
|
|
e.updateDomains(domains)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for the currently scheduled asynchronous configuration changes to
|
|
|
|
// complete.
|
|
|
|
func (e *AppConnector) Wait(ctx context.Context) {
|
|
|
|
e.queue.Wait(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AppConnector) updateDomains(domains []string) {
|
2023-10-27 21:20:10 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
2023-11-08 18:57:16 +00:00
|
|
|
var oldDomains map[string][]netip.Addr
|
|
|
|
oldDomains, e.domains = e.domains, make(map[string][]netip.Addr, len(domains))
|
2023-11-10 00:05:50 +00:00
|
|
|
e.wildcards = e.wildcards[:0]
|
2023-10-27 21:20:10 +00:00
|
|
|
for _, d := range domains {
|
|
|
|
d = strings.ToLower(d)
|
2023-11-08 18:57:16 +00:00
|
|
|
if len(d) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(d, "*.") {
|
|
|
|
e.wildcards = append(e.wildcards, d[2:])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.domains[d] = oldDomains[d]
|
|
|
|
delete(oldDomains, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that still-live wildcards addresses are preserved as well.
|
|
|
|
for d, addrs := range oldDomains {
|
|
|
|
for _, wc := range e.wildcards {
|
|
|
|
if dnsname.HasSuffix(d, wc) {
|
|
|
|
e.domains[d] = addrs
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
2023-11-08 18:57:16 +00:00
|
|
|
e.logf("handling domains: %v and wildcards: %v", xmaps.Keys(e.domains), e.wildcards)
|
2023-10-31 21:59:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 18:18:25 +00:00
|
|
|
// updateRoutes merges the supplied routes into the currently configured routes. The routes supplied
|
2024-01-17 19:35:55 +00:00
|
|
|
// by control for UpdateRoutes are supplemental to the routes discovered by DNS resolution, but are
|
|
|
|
// also more often whole ranges. UpdateRoutes will remove any single address routes that are now
|
|
|
|
// covered by new ranges.
|
2024-01-18 18:18:25 +00:00
|
|
|
func (e *AppConnector) updateRoutes(routes []netip.Prefix) {
|
2024-01-17 19:35:55 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
// If there was no change since the last update, no work to do.
|
|
|
|
if slices.Equal(e.controlRoutes, routes) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nextRoute:
|
|
|
|
for _, r := range routes {
|
|
|
|
if err := e.routeAdvertiser.AdvertiseRoute(r); err != nil {
|
|
|
|
e.logf("failed to advertise route: %v: %v", r, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range e.domains {
|
|
|
|
for _, a := range addr {
|
|
|
|
if r.Contains(a) {
|
|
|
|
pfx := netip.PrefixFrom(a, a.BitLen())
|
|
|
|
if err := e.routeAdvertiser.UnadvertiseRoute(pfx); err != nil {
|
|
|
|
e.logf("failed to unadvertise route: %v: %v", pfx, err)
|
|
|
|
}
|
|
|
|
continue nextRoute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.controlRoutes = routes
|
|
|
|
}
|
|
|
|
|
2023-10-31 21:59:18 +00:00
|
|
|
// Domains returns the currently configured domain list.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) Domains() views.Slice[string] {
|
2023-10-31 21:59:18 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
return views.SliceOf(xmaps.Keys(e.domains))
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 21:34:52 +00:00
|
|
|
// DomainRoutes returns a map of domains to resolved IP
|
|
|
|
// addresses.
|
|
|
|
func (e *AppConnector) DomainRoutes() map[string][]netip.Addr {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
drCopy := make(map[string][]netip.Addr)
|
|
|
|
for k, v := range e.domains {
|
2023-11-15 19:50:13 +00:00
|
|
|
drCopy[k] = append(drCopy[k], v...)
|
2023-11-07 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return drCopy
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// ObserveDNSResponse is a callback invoked by the DNS resolver when a DNS
|
|
|
|
// response is being returned over the PeerAPI. The response is parsed and
|
|
|
|
// matched against the configured domains, if matched the routeAdvertiser is
|
|
|
|
// advised to advertise the discovered route.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) ObserveDNSResponse(res []byte) {
|
2023-10-27 21:20:10 +00:00
|
|
|
var p dnsmessage.Parser
|
|
|
|
if _, err := p.Start(res); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.SkipAllQuestions(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:35:55 +00:00
|
|
|
nextAnswer:
|
2023-10-27 21:20:10 +00:00
|
|
|
for {
|
|
|
|
h, err := p.AnswerHeader()
|
|
|
|
if err == dnsmessage.ErrSectionDone {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Class != dnsmessage.ClassINET {
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if h.Type != dnsmessage.TypeA && h.Type != dnsmessage.TypeAAAA {
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := h.Name.String()
|
|
|
|
if len(domain) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-11-08 18:57:16 +00:00
|
|
|
domain = strings.TrimSuffix(domain, ".")
|
2023-10-27 21:20:10 +00:00
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
e.logf("[v2] observed DNS response for %s", domain)
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
addrs, ok := e.domains[domain]
|
2023-11-08 18:57:16 +00:00
|
|
|
// match wildcard domains
|
|
|
|
if !ok {
|
|
|
|
for _, wc := range e.wildcards {
|
|
|
|
if dnsname.HasSuffix(domain, wc) {
|
|
|
|
e.domains[domain] = nil
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
e.mu.Unlock()
|
2023-11-08 18:57:16 +00:00
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
if !ok {
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr netip.Addr
|
|
|
|
switch h.Type {
|
|
|
|
case dnsmessage.TypeA:
|
|
|
|
r, err := p.AResource()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addr = netip.AddrFrom4(r.A)
|
|
|
|
case dnsmessage.TypeAAAA:
|
|
|
|
r, err := p.AAAAResource()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addr = netip.AddrFrom16(r.AAAA)
|
|
|
|
default:
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if slices.Contains(addrs, addr) {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-17 19:35:55 +00:00
|
|
|
for _, route := range e.controlRoutes {
|
|
|
|
if route.Contains(addr) {
|
|
|
|
// record the new address associated with the domain for faster matching in subsequent
|
|
|
|
// requests and for diagnostic records.
|
|
|
|
e.mu.Lock()
|
|
|
|
e.domains[domain] = append(addrs, addr)
|
|
|
|
e.mu.Unlock()
|
|
|
|
continue nextAnswer
|
|
|
|
}
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
if err := e.routeAdvertiser.AdvertiseRoute(netip.PrefixFrom(addr, addr.BitLen())); err != nil {
|
2023-12-19 17:33:38 +00:00
|
|
|
e.logf("failed to advertise route for %s: %v: %v", domain, addr, err)
|
2023-10-27 21:20:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.logf("[v2] advertised route for %v: %v", domain, addr)
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
e.domains[domain] = append(addrs, addr)
|
|
|
|
e.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|