2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-07-23 15:30:09 +00:00
|
|
|
"net"
|
2020-06-24 22:00:02 +00:00
|
|
|
"reflect"
|
2020-07-23 15:30:09 +00:00
|
|
|
"strconv"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-06-18 23:24:44 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NetworkMap struct {
|
|
|
|
// Core networking
|
|
|
|
|
|
|
|
NodeKey tailcfg.NodeKey
|
|
|
|
PrivateKey wgcfg.PrivateKey
|
|
|
|
Expiry time.Time
|
|
|
|
Addresses []wgcfg.CIDR
|
|
|
|
LocalPort uint16 // used for debugging
|
|
|
|
MachineStatus tailcfg.MachineStatus
|
2020-03-19 20:19:02 +00:00
|
|
|
Peers []*tailcfg.Node
|
2020-02-05 22:16:58 +00:00
|
|
|
DNS []wgcfg.IP
|
|
|
|
DNSDomains []string
|
|
|
|
Hostinfo tailcfg.Hostinfo
|
|
|
|
PacketFilter filter.Matches
|
|
|
|
|
2020-05-17 16:51:38 +00:00
|
|
|
// DERPMap is the last DERP server map received. It's reused
|
|
|
|
// between updates and should not be modified.
|
|
|
|
DERPMap *tailcfg.DERPMap
|
|
|
|
|
2020-06-25 17:47:33 +00:00
|
|
|
// Debug knobs from control server for debug or feature gating.
|
|
|
|
Debug *tailcfg.Debug
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// ACLs
|
|
|
|
|
|
|
|
User tailcfg.UserID
|
|
|
|
Domain string
|
|
|
|
// TODO(crawshaw): reduce UserProfiles to []tailcfg.UserProfile?
|
|
|
|
// There are lots of ways to slice this data, leave it up to users.
|
|
|
|
UserProfiles map[tailcfg.UserID]tailcfg.UserProfile
|
|
|
|
Roles []tailcfg.Role
|
|
|
|
// TODO(crawshaw): Groups []tailcfg.Group
|
|
|
|
// TODO(crawshaw): Capabilities []tailcfg.Capability
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkMap) Equal(n2 *NetworkMap) bool {
|
|
|
|
// TODO(crawshaw): this is crude, but is an easy way to avoid bugs.
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b2, err := json.Marshal(n2)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bytes.Equal(b, b2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nm NetworkMap) String() string {
|
|
|
|
return nm.Concise()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nm *NetworkMap) Concise() string {
|
|
|
|
buf := new(strings.Builder)
|
2020-06-25 17:47:33 +00:00
|
|
|
fmt.Fprintf(buf, "netmap: self: %v auth=%v",
|
|
|
|
nm.NodeKey.ShortString(), nm.MachineStatus)
|
|
|
|
if nm.LocalPort != 0 {
|
|
|
|
fmt.Fprintf(buf, " port=%v", nm.LocalPort)
|
|
|
|
}
|
|
|
|
if nm.Debug != nil {
|
|
|
|
j, _ := json.Marshal(nm.Debug)
|
|
|
|
fmt.Fprintf(buf, " debug=%s", j)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(buf, " %v", nm.Addresses)
|
|
|
|
buf.WriteByte('\n')
|
2020-02-05 22:16:58 +00:00
|
|
|
for _, p := range nm.Peers {
|
|
|
|
aip := make([]string, len(p.AllowedIPs))
|
|
|
|
for i, a := range p.AllowedIPs {
|
2020-06-24 21:24:32 +00:00
|
|
|
s := strings.TrimSuffix(fmt.Sprint(a), "/32")
|
2020-03-13 02:29:24 +00:00
|
|
|
aip[i] = s
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-13 02:29:24 +00:00
|
|
|
|
|
|
|
ep := make([]string, len(p.Endpoints))
|
|
|
|
for i, e := range p.Endpoints {
|
|
|
|
// Align vertically on the ':' between IP and port
|
|
|
|
colon := strings.IndexByte(e, ':')
|
2020-06-24 22:00:02 +00:00
|
|
|
spaces := 0
|
|
|
|
for colon > 0 && len(e)+spaces-colon < 6 {
|
|
|
|
spaces++
|
2020-03-13 02:29:24 +00:00
|
|
|
colon--
|
|
|
|
}
|
2020-06-24 22:00:02 +00:00
|
|
|
ep[i] = fmt.Sprintf("%21v", e+strings.Repeat(" ", spaces))
|
2020-03-13 02:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
derp := p.DERP
|
2020-03-13 16:48:12 +00:00
|
|
|
const derpPrefix = "127.3.3.40:"
|
|
|
|
if strings.HasPrefix(derp, derpPrefix) {
|
|
|
|
derp = "D" + derp[len(derpPrefix):]
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-13 02:29:24 +00:00
|
|
|
|
|
|
|
// Most of the time, aip is just one element, so format the
|
|
|
|
// table to look good in that case. This will also make multi-
|
|
|
|
// subnet nodes stand out visually.
|
|
|
|
fmt.Fprintf(buf, " %v %-2v %-15v : %v\n",
|
2020-03-18 22:10:46 +00:00
|
|
|
p.Key.ShortString(), derp,
|
2020-03-13 02:29:24 +00:00
|
|
|
strings.Join(aip, " "),
|
|
|
|
strings.Join(ep, " "))
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2020-03-13 03:01:08 +00:00
|
|
|
func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
|
2020-06-24 22:00:02 +00:00
|
|
|
if reflect.DeepEqual(a, b) {
|
|
|
|
// Fast path that only does one allocation.
|
|
|
|
return ""
|
|
|
|
}
|
2020-03-13 03:01:08 +00:00
|
|
|
out := []string{}
|
|
|
|
ra := strings.Split(a.Concise(), "\n")
|
|
|
|
rb := strings.Split(b.Concise(), "\n")
|
|
|
|
|
|
|
|
ma := map[string]struct{}{}
|
|
|
|
for _, s := range ra {
|
|
|
|
ma[s] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
mb := map[string]struct{}{}
|
|
|
|
for _, s := range rb {
|
|
|
|
mb[s] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range ra {
|
|
|
|
if _, ok := mb[s]; !ok {
|
|
|
|
out = append(out, "-"+s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, s := range rb {
|
|
|
|
if _, ok := ma[s]; !ok {
|
|
|
|
out = append(out, "+"+s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(out, "\n")
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func (nm *NetworkMap) JSON() string {
|
|
|
|
b, err := json.MarshalIndent(*nm, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf("[json error: %v]", err)
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
UAllowSingleHosts = 1 << iota
|
|
|
|
UAllowSubnetRoutes
|
|
|
|
UAllowDefaultRoute
|
|
|
|
UHackDefaultRoute
|
|
|
|
|
|
|
|
UDefault = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
// Several programs need to parse these arguments into uflags, so let's
|
|
|
|
// centralize it here.
|
|
|
|
func UFlagsHelper(uroutes, rroutes, droutes bool) int {
|
|
|
|
uflags := 0
|
|
|
|
if uroutes {
|
|
|
|
uflags |= UAllowSingleHosts
|
|
|
|
}
|
|
|
|
if rroutes {
|
|
|
|
uflags |= UAllowSubnetRoutes
|
|
|
|
}
|
|
|
|
if droutes {
|
|
|
|
uflags |= UAllowDefaultRoute
|
|
|
|
}
|
|
|
|
return uflags
|
|
|
|
}
|
|
|
|
|
2020-03-24 05:24:25 +00:00
|
|
|
// TODO(bradfitz): UAPI seems to only be used by the old confnode and
|
|
|
|
// pingnode; delete this when those are deleted/rewritten?
|
2020-02-05 22:16:58 +00:00
|
|
|
func (nm *NetworkMap) UAPI(uflags int, dnsOverride []wgcfg.IP) string {
|
2020-06-18 23:24:44 +00:00
|
|
|
wgcfg, err := nm.WGCfg(log.Printf, uflags, dnsOverride)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-07-23 15:30:09 +00:00
|
|
|
log.Fatalf("WGCfg() failed unexpectedly: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
s, err := wgcfg.ToUAPI()
|
|
|
|
if err != nil {
|
2020-07-23 15:30:09 +00:00
|
|
|
log.Fatalf("ToUAPI() failed unexpectedly: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-06-28 18:53:37 +00:00
|
|
|
// EndpointDiscoSuffix is appended to the hex representation of a peer's discovery key
|
|
|
|
// and is then the sole wireguard endpoint for peers with a non-zero discovery key.
|
|
|
|
// This form is then recognize by magicsock's CreateEndpoint.
|
|
|
|
const EndpointDiscoSuffix = ".disco.tailscale:12345"
|
|
|
|
|
2020-07-23 15:30:09 +00:00
|
|
|
// WGCfg returns the NetworkMaps's Wireguard configuration.
|
|
|
|
func (nm *NetworkMap) WGCfg(logf logger.Logf, uflags int, dnsOverride []wgcfg.IP) (*wgcfg.Config, error) {
|
|
|
|
cfg := &wgcfg.Config{
|
|
|
|
Name: "tailscale",
|
|
|
|
PrivateKey: nm.PrivateKey,
|
|
|
|
Addresses: nm.Addresses,
|
|
|
|
ListenPort: nm.LocalPort,
|
|
|
|
DNS: append([]wgcfg.IP(nil), dnsOverride...),
|
|
|
|
Peers: make([]wgcfg.Peer, 0, len(nm.Peers)),
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:30:09 +00:00
|
|
|
for _, peer := range nm.Peers {
|
2020-06-28 18:53:37 +00:00
|
|
|
if Debug.OnlyDisco && peer.DiscoKey.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if (uflags&UAllowSingleHosts) == 0 && len(peer.AllowedIPs) < 2 {
|
2020-07-23 15:30:09 +00:00
|
|
|
logf("wgcfg: %v skipping a single-host peer.", peer.Key.ShortString())
|
2020-02-05 22:16:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-07-23 15:30:09 +00:00
|
|
|
cfg.Peers = append(cfg.Peers, wgcfg.Peer{
|
|
|
|
PublicKey: wgcfg.Key(peer.Key),
|
|
|
|
})
|
|
|
|
cpeer := &cfg.Peers[len(cfg.Peers)-1]
|
|
|
|
if peer.KeepAlive {
|
|
|
|
cpeer.PersistentKeepalive = 25 // seconds
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-07-23 15:30:09 +00:00
|
|
|
|
2020-06-28 18:53:37 +00:00
|
|
|
if !peer.DiscoKey.IsZero() {
|
2020-07-23 15:30:09 +00:00
|
|
|
if err := appendEndpoint(cpeer, fmt.Sprintf("%x%s", peer.DiscoKey[:], EndpointDiscoSuffix)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cpeer.Endpoints = []wgcfg.Endpoint{{Host: fmt.Sprintf("%x.disco.tailscale", peer.DiscoKey[:]), Port: 12345}}
|
2020-06-28 18:53:37 +00:00
|
|
|
} else {
|
2020-07-23 15:30:09 +00:00
|
|
|
if err := appendEndpoint(cpeer, peer.DERP); err != nil {
|
|
|
|
return nil, err
|
2020-06-28 18:53:37 +00:00
|
|
|
}
|
2020-07-23 15:30:09 +00:00
|
|
|
for _, ep := range peer.Endpoints {
|
|
|
|
if err := appendEndpoint(cpeer, ep); err != nil {
|
|
|
|
return nil, err
|
2020-06-28 18:53:37 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, allowedIP := range peer.AllowedIPs {
|
|
|
|
if allowedIP.Mask == 0 {
|
|
|
|
if (uflags & UAllowDefaultRoute) == 0 {
|
2020-07-23 15:30:09 +00:00
|
|
|
logf("wgcfg: %v skipping default route", peer.Key.ShortString())
|
2020-02-05 22:16:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (uflags & UHackDefaultRoute) != 0 {
|
2020-07-23 15:30:09 +00:00
|
|
|
allowedIP = wgcfg.CIDR{IP: wgcfg.IPv4(10, 0, 0, 0), Mask: 8}
|
|
|
|
logf("wgcfg: %v converting default route => %v", peer.Key.ShortString(), allowedIP.String())
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
} else if allowedIP.Mask < 32 {
|
|
|
|
if (uflags & UAllowSubnetRoutes) == 0 {
|
2020-07-23 15:30:09 +00:00
|
|
|
logf("wgcfg: %v skipping subnet route", peer.Key.ShortString())
|
2020-02-05 22:16:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 15:30:09 +00:00
|
|
|
cpeer.AllowedIPs = append(cpeer.AllowedIPs, allowedIP)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:30:09 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEndpoint(peer *wgcfg.Peer, epStr string) error {
|
|
|
|
if epStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(epStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("malformed endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
|
|
|
|
}
|
|
|
|
port16, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid port in endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
|
|
|
|
}
|
|
|
|
peer.Endpoints = append(peer.Endpoints, wgcfg.Endpoint{Host: host, Port: uint16(port16)})
|
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|