From b493ef5b717f2e381883648dd10f3723b705e70d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 4 Mar 2022 08:00:53 -0800 Subject: [PATCH] net/tsaddr: add func ContainsExitRoutes Change-Id: I772441a406083e2fe0f9374b2b23d89aac18928f Signed-off-by: Brad Fitzpatrick --- ipn/prefs.go | 14 ++------------ net/tsaddr/tsaddr.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ipn/prefs.go b/ipn/prefs.go index 813fc4cd4..330fd073d 100644 --- a/ipn/prefs.go +++ b/ipn/prefs.go @@ -18,6 +18,7 @@ import ( "inet.af/netaddr" "tailscale.com/atomicfile" + "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/persist" "tailscale.com/types/preftype" @@ -442,18 +443,7 @@ func (p *Prefs) AdvertisesExitNode() bool { if p == nil { return false } - var v4, v6 bool - for _, r := range p.AdvertiseRoutes { - if r.Bits() != 0 { - continue - } - if r.IP().Is4() { - v4 = true - } else if r.IP().Is6() { - v6 = true - } - } - return v4 && v6 + return tsaddr.ContainsExitRoutes(p.AdvertiseRoutes) } // SetAdvertiseExitNode mutates p (if non-nil) to add or remove the two diff --git a/net/tsaddr/tsaddr.go b/net/tsaddr/tsaddr.go index b42cf586b..5d23d717b 100644 --- a/net/tsaddr/tsaddr.go +++ b/net/tsaddr/tsaddr.go @@ -213,3 +213,28 @@ func PrefixIs4(p netaddr.IPPrefix) bool { return p.IP().Is4() } // PrefixIs6 reports whether p is an IPv6 prefix. func PrefixIs6(p netaddr.IPPrefix) bool { return p.IP().Is6() } + +// ContainsExitRoutes reports whether rr contains both the IPv4 and +// IPv6 /0 route. +func ContainsExitRoutes(rr []netaddr.IPPrefix) bool { + var v4, v6 bool + for _, r := range rr { + if r == allIPv4 { + v4 = true + } else if r == allIPv6 { + v6 = true + } + } + return v4 && v6 +} + +var ( + allIPv4 = netaddr.MustParseIPPrefix("0.0.0.0/0") + allIPv6 = netaddr.MustParseIPPrefix("::/0") +) + +// AllIPv4 returns 0.0.0.0/0. +func AllIPv4() netaddr.IPPrefix { return allIPv4 } + +// AllIPv6 returns ::/0. +func AllIPv6() netaddr.IPPrefix { return allIPv6 }