2021-02-05 20:44:43 +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 nmcfg converts a controlclient.NetMap into a wgcfg config.
|
|
|
|
package nmcfg
|
|
|
|
|
|
|
|
import (
|
2021-04-21 17:47:50 +00:00
|
|
|
"bytes"
|
2021-02-05 20:44:43 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/control/controlclient"
|
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2021-04-29 20:52:20 +00:00
|
|
|
"tailscale.com/types/wgkey"
|
2021-02-05 20:44:43 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func nodeDebugName(n *tailcfg.Node) string {
|
|
|
|
name := n.Name
|
|
|
|
if name == "" {
|
|
|
|
name = n.Hostinfo.Hostname
|
|
|
|
}
|
|
|
|
if i := strings.Index(name, "."); i != -1 {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
|
|
|
if name == "" && len(n.Addresses) != 0 {
|
|
|
|
return n.Addresses[0].String()
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// cidrIsSubnet reports whether cidr is a non-default-route subnet
|
|
|
|
// exported by node that is not one of its own self addresses.
|
|
|
|
func cidrIsSubnet(node *tailcfg.Node, cidr netaddr.IPPrefix) bool {
|
2021-05-15 01:07:28 +00:00
|
|
|
if cidr.Bits() == 0 {
|
2021-02-05 20:44:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !cidr.IsSingleIP() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, selfCIDR := range node.Addresses {
|
|
|
|
if cidr == selfCIDR {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WGCfg returns the NetworkMaps's Wireguard configuration.
|
2021-02-25 04:05:23 +00:00
|
|
|
func WGCfg(nm *netmap.NetworkMap, logf logger.Logf, flags netmap.WGConfigFlags, exitNode tailcfg.StableNodeID) (*wgcfg.Config, error) {
|
2021-02-05 20:44:43 +00:00
|
|
|
cfg := &wgcfg.Config{
|
|
|
|
Name: "tailscale",
|
2021-04-29 20:52:20 +00:00
|
|
|
PrivateKey: wgkey.Private(nm.PrivateKey),
|
2021-02-05 20:44:43 +00:00
|
|
|
Addresses: nm.Addresses,
|
|
|
|
Peers: make([]wgcfg.Peer, 0, len(nm.Peers)),
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:47:50 +00:00
|
|
|
// Logging buffers
|
|
|
|
skippedUnselected := new(bytes.Buffer)
|
|
|
|
skippedIPs := new(bytes.Buffer)
|
|
|
|
skippedSubnets := new(bytes.Buffer)
|
|
|
|
|
2021-02-05 20:44:43 +00:00
|
|
|
for _, peer := range nm.Peers {
|
|
|
|
if controlclient.Debug.OnlyDisco && peer.DiscoKey.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cfg.Peers = append(cfg.Peers, wgcfg.Peer{
|
2021-04-29 20:52:20 +00:00
|
|
|
PublicKey: wgkey.Key(peer.Key),
|
2021-02-05 20:44:43 +00:00
|
|
|
})
|
|
|
|
cpeer := &cfg.Peers[len(cfg.Peers)-1]
|
|
|
|
if peer.KeepAlive {
|
|
|
|
cpeer.PersistentKeepalive = 25 // seconds
|
|
|
|
}
|
|
|
|
|
2021-04-30 23:45:36 +00:00
|
|
|
cpeer.Endpoints = wgcfg.Endpoints{PublicKey: wgkey.Key(peer.Key), DiscoKey: peer.DiscoKey}
|
|
|
|
if peer.DiscoKey.IsZero() {
|
|
|
|
// Legacy connection. Add IP+port endpoints.
|
|
|
|
var ipps []netaddr.IPPort
|
|
|
|
if err := appendEndpoint(cpeer, &ipps, peer.DERP); err != nil {
|
2021-02-05 20:44:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ep := range peer.Endpoints {
|
2021-04-30 23:45:36 +00:00
|
|
|
if err := appendEndpoint(cpeer, &ipps, ep); err != nil {
|
2021-02-05 20:44:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 23:45:36 +00:00
|
|
|
cpeer.Endpoints.IPPorts = wgcfg.NewIPPortSet(ipps...)
|
2021-02-05 20:44:43 +00:00
|
|
|
}
|
2021-03-31 16:51:55 +00:00
|
|
|
didExitNodeWarn := false
|
2021-02-05 20:44:43 +00:00
|
|
|
for _, allowedIP := range peer.AllowedIPs {
|
2021-05-15 01:07:28 +00:00
|
|
|
if allowedIP.Bits() == 0 && peer.StableID != exitNode {
|
2021-03-31 16:51:55 +00:00
|
|
|
if didExitNodeWarn {
|
|
|
|
// Don't log about both the IPv4 /0 and IPv6 /0.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
didExitNodeWarn = true
|
2021-04-21 17:47:50 +00:00
|
|
|
if skippedUnselected.Len() > 0 {
|
|
|
|
skippedUnselected.WriteString(", ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(skippedUnselected, "%q (%v)", nodeDebugName(peer), peer.Key.ShortString())
|
2021-02-25 04:05:23 +00:00
|
|
|
continue
|
2021-05-15 01:07:28 +00:00
|
|
|
} else if allowedIP.IsSingleIP() && tsaddr.IsTailscaleIP(allowedIP.IP()) && (flags&netmap.AllowSingleHosts) == 0 {
|
2021-04-21 17:47:50 +00:00
|
|
|
if skippedIPs.Len() > 0 {
|
|
|
|
skippedIPs.WriteString(", ")
|
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
fmt.Fprintf(skippedIPs, "%v from %q (%v)", allowedIP.IP(), nodeDebugName(peer), peer.Key.ShortString())
|
2021-02-05 20:44:43 +00:00
|
|
|
continue
|
|
|
|
} else if cidrIsSubnet(peer, allowedIP) {
|
2021-02-05 23:44:46 +00:00
|
|
|
if (flags & netmap.AllowSubnetRoutes) == 0 {
|
2021-04-21 17:47:50 +00:00
|
|
|
if skippedSubnets.Len() > 0 {
|
|
|
|
skippedSubnets.WriteString(", ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(skippedSubnets, "%v from %q (%v)", allowedIP, nodeDebugName(peer), peer.Key.ShortString())
|
2021-02-05 20:44:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpeer.AllowedIPs = append(cpeer.AllowedIPs, allowedIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:47:50 +00:00
|
|
|
if skippedUnselected.Len() > 0 {
|
|
|
|
logf("[v1] wgcfg: skipped unselected default routes from: %s", skippedUnselected.Bytes())
|
|
|
|
}
|
|
|
|
if skippedIPs.Len() > 0 {
|
|
|
|
logf("[v1] wgcfg: skipped node IPs: %s", skippedIPs)
|
|
|
|
}
|
|
|
|
if skippedSubnets.Len() > 0 {
|
|
|
|
logf("[v1] wgcfg: did not accept subnet routes: %s", skippedSubnets)
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:44:43 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 23:45:36 +00:00
|
|
|
func appendEndpoint(peer *wgcfg.Peer, ipps *[]netaddr.IPPort, epStr string) error {
|
2021-02-05 20:44:43 +00:00
|
|
|
if epStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-30 23:45:36 +00:00
|
|
|
ipp, err := netaddr.ParseIPPort(epStr)
|
2021-02-05 20:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("malformed endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
|
|
|
|
}
|
2021-04-30 23:45:36 +00:00
|
|
|
*ipps = append(*ipps, ipp)
|
2021-02-05 20:44:43 +00:00
|
|
|
return nil
|
|
|
|
}
|