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 18:47:50 +01:00
|
|
|
"bytes"
|
2021-02-05 20:44:43 +00:00
|
|
|
"fmt"
|
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
|
|
|
"net/netip"
|
2021-02-05 20:44:43 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-10-07 00:19:38 +01:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
"tailscale.com/logtail"
|
2021-02-05 20:44:43 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2021-02-05 20:44:43 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func nodeDebugName(n *tailcfg.Node) string {
|
|
|
|
name := n.Name
|
|
|
|
if name == "" {
|
2022-02-15 16:19:44 +00:00
|
|
|
name = n.Hostinfo.Hostname()
|
2021-02-05 20:44:43 +00:00
|
|
|
}
|
|
|
|
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.
|
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 cidrIsSubnet(node *tailcfg.Node, cidr netip.Prefix) bool {
|
2021-05-15 02:07:28 +01: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
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:10:17 +01:00
|
|
|
// 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-10-28 18:44:34 +01:00
|
|
|
PrivateKey: nm.PrivateKey,
|
2021-02-05 20:44:43 +00:00
|
|
|
Addresses: nm.Addresses,
|
|
|
|
Peers: make([]wgcfg.Peer, 0, len(nm.Peers)),
|
|
|
|
}
|
|
|
|
|
2022-10-07 00:19:38 +01:00
|
|
|
// Setup log IDs for data plane audit logging.
|
|
|
|
if nm.SelfNode != nil {
|
2022-10-28 18:09:30 +01:00
|
|
|
cfg.NodeID = nm.SelfNode.StableID
|
2022-10-07 00:19:38 +01:00
|
|
|
canNetworkLog := slices.Contains(nm.SelfNode.Capabilities, tailcfg.CapabilityDataPlaneAuditLogs)
|
|
|
|
if canNetworkLog && nm.SelfNode.DataPlaneAuditLogID != "" && nm.DomainAuditLogID != "" {
|
|
|
|
nodeID, errNode := logtail.ParsePrivateID(nm.SelfNode.DataPlaneAuditLogID)
|
|
|
|
if errNode != nil {
|
|
|
|
logf("[v1] wgcfg: unable to parse node audit log ID: %v", errNode)
|
|
|
|
}
|
|
|
|
domainID, errDomain := logtail.ParsePrivateID(nm.DomainAuditLogID)
|
|
|
|
if errDomain != nil {
|
|
|
|
logf("[v1] wgcfg: unable to parse domain audit log ID: %v", errDomain)
|
|
|
|
}
|
|
|
|
if errNode == nil && errDomain == nil {
|
|
|
|
cfg.NetworkLogging.NodeID = nodeID
|
|
|
|
cfg.NetworkLogging.DomainID = domainID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 18:47:50 +01: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 {
|
2021-09-01 23:29:06 +01:00
|
|
|
if peer.DiscoKey.IsZero() && peer.DERP == "" {
|
|
|
|
// Peer predates both DERP and active discovery, we cannot
|
|
|
|
// communicate with it.
|
|
|
|
logf("[v1] wgcfg: skipped peer %s, doesn't offer DERP or disco", peer.Key.ShortString())
|
|
|
|
continue
|
|
|
|
}
|
2021-02-05 20:44:43 +00:00
|
|
|
cfg.Peers = append(cfg.Peers, wgcfg.Peer{
|
2021-11-02 03:55:52 +00:00
|
|
|
PublicKey: peer.Key,
|
2021-11-02 21:41:56 +00:00
|
|
|
DiscoKey: peer.DiscoKey,
|
2021-02-05 20:44:43 +00:00
|
|
|
})
|
|
|
|
cpeer := &cfg.Peers[len(cfg.Peers)-1]
|
|
|
|
if peer.KeepAlive {
|
|
|
|
cpeer.PersistentKeepalive = 25 // seconds
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:51:55 +01:00
|
|
|
didExitNodeWarn := false
|
2021-02-05 20:44:43 +00:00
|
|
|
for _, allowedIP := range peer.AllowedIPs {
|
2021-05-15 02:07:28 +01:00
|
|
|
if allowedIP.Bits() == 0 && peer.StableID != exitNode {
|
2021-03-31 17:51:55 +01:00
|
|
|
if didExitNodeWarn {
|
|
|
|
// Don't log about both the IPv4 /0 and IPv6 /0.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
didExitNodeWarn = true
|
2021-04-21 18:47:50 +01: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
|
2022-07-25 04:08:42 +01:00
|
|
|
} else if allowedIP.IsSingleIP() && tsaddr.IsTailscaleIP(allowedIP.Addr()) && (flags&netmap.AllowSingleHosts) == 0 {
|
2021-04-21 18:47:50 +01:00
|
|
|
if skippedIPs.Len() > 0 {
|
|
|
|
skippedIPs.WriteString(", ")
|
|
|
|
}
|
2022-07-25 04:08:42 +01:00
|
|
|
fmt.Fprintf(skippedIPs, "%v from %q (%v)", allowedIP.Addr(), 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 18:47:50 +01: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 18:47:50 +01: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
|
|
|
|
}
|