This commit is contained in:
Adrian Dewhurst committed 2026-11-24 16:28:19 -05:00
1 parent 4c0cb65d84
commit 72aa239b02
4 files changed
+130 -41

No files matched your search

+11 -5
View File
@@ -4926,6 +4926,7 @@ func (b *LocalBackend) setAtomicValuesFromPrefsLocked(p ipn.PrefsView) {
if !p.Valid() {
b.containsViaIPFuncAtomic.Store(ipset.FalseContainsIPFunc())
b.allowViaLocalIPFuncAtomic.Store(ipset.FalseContainsIPFunc())
b.setTCPPortsIntercepted(nil)
if f, ok := hookServeClearVIPServicesTCPPortsInterceptedLocked.GetOk(); ok {
f(b)
@@ -4933,10 +4934,9 @@ func (b *LocalBackend) setAtomicValuesFromPrefsLocked(p ipn.PrefsView) {
b.lastServeConfJSON = mem.B(nil)
b.serveConfig = ipn.ServeConfigView{}
} else {
filtered := tsaddr.FilterPrefixesCopy(p.AdvertiseRoutes(), tsaddr.IsViaPrefix)
b.containsViaIPFuncAtomic.Store(ipset.NewContainsIPFunc(views.SliceOf(filtered)))
viaLocalPrefixes :=
b.containsViaIPFuncAtomic.Store(ipset.NewContainsIPFunc(views.SliceOf(filtered)))
filtered := views.SliceOf(tsaddr.FilterPrefixesCopy(p.AdvertiseRoutes(), tsaddr.IsViaPrefix))
b.containsViaIPFuncAtomic.Store(ipset.NewContainsIPFunc(filtered))
b.allowViaLocalIPFuncAtomic.Store(generateViaTargetAllowed(filtered))
b.setTCPPortsInterceptedFromNetmapAndPrefsLocked(p)
}
}
@@ -7486,7 +7486,13 @@ func (b *LocalBackend) ShouldHandleViaIP(ip netip.Addr) bool {
// The packet filter only sees the outer via address of a 4via6 flow, so this
// is the sole policy check on the embedded target.
func (b *LocalBackend) ShouldForwardToVia(via netip.Addr) bool {
return viaTargetAllowed(tsaddr.UnmapVia(via))
// Configured exceptions take place before unmapping
if f, ok := b.allowViaLocalIPFuncAtomic.LoadOk(); ok {
if f(via) {
return true
}
}
return
}
// Logout logs out the current profile, if any, and waits for the logout to
+90 -27
View File
@@ -13,10 +13,12 @@ import (
"go4.org/netipx"
"tailscale.com/envknob"
"tailscale.com/net/ipset"
"tailscale.com/net/routelimits"
"tailscale.com/net/tsaddr"
"tailscale.com/types/views"
"tailscale.com/util/clientmetric"
"tailscale.com/util/set"
)
// viaAdditionsEnv is the name of an environment variable that can be set to
@@ -29,18 +31,20 @@ import (
// notation), or IPv4 addresses. Spaces are permitted around the commas.
const viaAdditionsEnv = "TS_4VIA6_ALLOW_LOCAL"
var envViaTargetAdditions = sync.OnceValue(func() *netipx.IPSet {
var envViaTargetAdditions = sync.OnceValue(func() func(netip.Addr) bool {
v, err := generateEnvViaTargetAdditions(envknob.String(viaAdditionsEnv))
if err != nil {
log.Print(err)
log.Printf("building envViaTargetAdditions: %v", err)
return ipset.FalseContainsIPFunc()
}
if v != nil {
has4via6AdditionsMetric := clientmetric.NewGauge("4via6_allow_local")
has4via6AdditionsMetric.Set(1)
if v == nil {
return ipset.FalseContainsIPFunc()
}
return v
has4via6AdditionsMetric := clientmetric.NewGauge("4via6_allow_local")
has4via6AdditionsMetric.Set(1)
return ipset.NewContainsIPFunc(views.SliceOf(v.Prefixes()))
})
func generateEnvViaTargetAdditions(rawAdditions string) (*netipx.IPSet, error) {
@@ -103,49 +107,108 @@ func generateEnvViaTargetAdditions(rawAdditions string) (*netipx.IPSet, error) {
return ipSet, errors.Join(errs...)
}
// viaTargetDefaultAllowed reports whether ip may be forwarded to after
// viaUnmappedTargetAllowed reports whether ip may be forwarded to after
// unmapping a 4via6 destination. The packet filter only sees the outer via
// address, so this is a substitute for the normal destination IP filter in
// [tailscale.com/wgengine/filter] when via addresses are in use. Exceptions to
// the restrictions are handled before this func is called.
func viaTargetDefaultAllowed(ip netip.Addr) bool {
// address, so this is the sole check on the embedded IPv4 target.
func viaUnmappedTargetAllowed(ip netip.Addr) bool {
if !ip.Is4() {
return false // UnmapVia only returns IPv4
}
if additions := viaTargetAdditions(); additions != nil {
if envViaTargetAdditions()(ip) {
// Permit unusual configurations to use host-scoped IP addresses as
// 4via6 destinations when provided in an envknob.
if additions.Contains(ip) {
return true
}
return true
}
return !routelimits.IsDisallowed(ip)
}
var (
v4Loopback = netip.PrefixFrom(netip.AddrFrom4([4]byte{127, 0, 0, 0}), 8)
v4LinkLocal = netip.PrefixFrom(netip.AddrFrom4([4]byte{169, 254, 0, 1}), 16)
)
// viaPerSiteIDPrefixLength is the IPv6 prefix length for the entire IPv4
// address space within each site ID. It's 128 (v6 address length) - 32 (v4
// address length) = 96
const viaPerSiteIDPrefixLength = 96
// minViaExceptionPrefixLength is the shortest IPv6 prefix length for a 4via6
// route in viaHostLocalAdvertisablePrefixes.
const minViaExceptionPrefixLength = viaPerSiteIDPrefixLength + 8
// viaHostLocalAdvertisablePrefixes is the list of host-scoped prefixes that can
// be advertised via a subnet router but only when the advertisement is specific
// enough to ensure that advertising within host-scoped address space is
// intentional.
var viaHostLocalAdvertisablePrefixes = []netip.Prefix{
netip.PrefixFrom(netip.AddrFrom4([4]byte{127, 0, 0, 0}), 8), // loopback
netip.PrefixFrom(netip.AddrFrom4([4]byte{169, 254, 0, 0}), 16), // link-local
}
// generateViaAdvertisedAllowlist creates a func that returns true when a
// destination IP is a 4via6 address (still in 4via6 form) that should be
// permitted because the administrator has advertised a route indicating
// a deliberate choice to share potentially-sensitive destinations. The mapped
// address is used for this check so that only the site ID of the matching
// advertisement is permitted, rather than any site ID.
func generateViaAdvertisedAllowlist(advertisedRoutes views.Slice[netip.Prefix]) func(netip.Addr) bool {
var restrictedBuilder netipx.IPSetBuilder
for _, prefix := range viaHostLocalAdvertisablePrefixes {
restrictedBuilder.AddPrefix(prefix)
}
for _, prefix := range routelimits.CloudMetadataPrefixes().All() {
restrictedBuilder.AddPrefix(prefix)
}
func generateViaTargetAllowed(advertisedViaRoutes views.Slice[netip.Prefix]) func(netip.Addr) bool {
var sb netipx.IPSetBuilder
sb.AddSet(envViaTargetAdditions())
// Possible optimization: intersect with advertisedRoutes if that makes
// envAdditions shorter.
// Loopback and link-local addresses that are normally restricted by
// [routelimits.IsDisallowed] are permitted if there is a specific enough
// route. Specific enough means that the prefix is fully contained within
// the restricted range.
for _, pfx := range advertisedViaRoutes.All() {
if !tsaddr.IsViaPrefix(pfx) {
for _, pfx := range advertisedRoutes.All() {
if pfx.Bits() < minViaExceptionPrefixLength {
// The prefix is definitely too broad to allow: each 4via6 ID is a
// /96 covering the entire IPv4 address space. Exceptions to the
// restrictions requires at least /104 for loopback (127.0.0.0/8) or
// /112 for link-local (169.254.0.0/16).
// This also implicitly excludes all IPv4 routes.
continue
}
if pfx.Bits() < 96 {
// The prefix is definitely too broad to allow
mappedPrefix := pfx.Masked()
if !tsaddr.IsViaPrefix(mappedPrefix) {
continue
}
unmapped := netip.PrefixFrom(tsaddr.UnmapVia(pfx.Addr()), pfx.Bits()-96)
siteID, unmappedAddr := tsaddr.UnmapViaWithSiteID(mappedPrefix.Addr())
if mappedPrefix.IsSingleIP() && routelimits.IsCloudMetadata()(unmappedAddr) {
sb.AddPrefix(mappedPrefix)
continue
}
for _, advertisable := range viaHostLocalAdvertisablePrefixes {
if mappedPrefix.Bits() < viaPerSiteIDPrefixLength+advertisable.Bits() {
continue
}
if !advertisable.Contains(unmappedAddr) {
continue
}
// As an additional safety measure for addresses in
// routelimits.isCloudMetadata, they require an exact match to be allowed.
sb.AddPrefix(mappedPrefix)
break
}
for _, addr := range viaHostLocalAdvertisableAddrs {
}
}
for _, pfx := range advertisedRoutes.All() {
}
s, err := sb.IPSet()
if err != nil {
log.Printf("[unexpected] building viaTargetAllowed: %v", err)
return ipset.FalseContainsIPFunc()
}
return ipset.NewContainsIPFunc(views.SliceOf(s.Prefixes()))
}
+17 -9
View File
@@ -20,12 +20,12 @@ import (
// v4BroadcastAddr is 255.255.255.255, the limited broadcast address.
var v4BroadcastAddr = netip.AddrFrom4([4]byte{255, 255, 255, 255})
// isCloudMetadata returns true for IP addresses (v4 or v6) that are known to
// potentially contain cloud metadata. Most cloud providers document single
// addresses (/128) but sometimes they have multiple addresses, so all ULA IPv6
// addresses are recorded as their entire ULA prefix (/48).
var isCloudMetadata = sync.OnceValue(func() func(netip.Addr) bool {
prefixes := []netip.Prefix{
// CloudMetadataPrefixes is a list of prefixes that are known to potentially
// contain cloud metadata. Most cloud providers document single addresses
// (IPv4 /32 or IPv6 /128) but sometimes they have multiple IPv6 addresses, so
// all ULA IPv6 addresses are recorded as their entire ULA prefix (/48).
var CloudMetadataPrefixes = sync.OnceValue(func() views.Slice[netip.Prefix] {
return views.SliceOf([]netip.Prefix{
netip.MustParsePrefix("169.254.169.254/32"), // standard IPv4 address
netip.MustParsePrefix("fe80::a9fe:a9fe/128"), // commonly used link local IPv6 address
@@ -69,9 +69,17 @@ var isCloudMetadata = sync.OnceValue(func() func(netip.Addr) bool {
// Alibaba
netip.MustParsePrefix("100.100.100.200/32"),
}
})
})
return ipset.NewContainsIPFunc(views.SliceOf(prefixes))
// IsCloudMetadata returns true for IP addresses (v4 or v6) that are known to
// potentially contain cloud metadata. Most cloud providers document single
// addresses (/128) but sometimes they have multiple addresses, so all ULA IPv6
// addresses are recorded as their entire ULA prefix (/48).
// When adding new IPv4 addresses here, also consider adding them to
// ipnlocal.viaHostLocalAdvertisableAddrs.
var IsCloudMetadata = sync.OnceValue(func() func(netip.Addr) bool {
return ipset.NewContainsIPFunc(CloudMetadataPrefixes())
})
// IsDisallowed reports whether addr is an IP address in an IP range
@@ -88,7 +96,7 @@ func IsDisallowed(addr netip.Addr) bool {
// it's hard to detect when forwarding would be OK, so deny always
return true
}
if isCloudMetadata()(addr) {
if IsCloudMetadata()(addr) {
return true
}
return false
+12
View File
@@ -307,6 +307,18 @@ func UnmapVia(ip netip.Addr) netip.Addr {
return ip
}
// UnmapViaWithSiteID returns the IPv4 address and site ID that corresponds to
// the provided Tailscale "via" IPv4-in-IPv6 address.
//
// If ip is not a via address, it returns netip.IPV4Unspecified() with 0 siteID.
func UnmapViaWithSiteID(ip netip.Addr) (siteID uint32, v4 netip.Addr) {
if TailscaleViaRange().Contains(ip) {
a := ip.As16()
return binary.BigEndian.Uint32(a[8:]), netip.AddrFrom4(*(*[4]byte)(a[12:16]))
}
return 0, netip.IPv4Unspecified()
}
// MapVia returns an IPv6 "via" route for an IPv4 CIDR in a given siteID.
func MapVia(siteID uint32, v4 netip.Prefix) (via netip.Prefix, err error) {
if !v4.Addr().Is4() {