2020-07-07 05:33:29 +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 tsaddr handles Tailscale-specific IPs and ranges.
|
|
|
|
package tsaddr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChromeOSVMRange returns the subset of the CGNAT IPv4 range used by
|
|
|
|
// ChromeOS to interconnect the host OS to containers and VMs. We
|
|
|
|
// avoid allocating Tailscale IPs from it, to avoid conflicts.
|
|
|
|
func ChromeOSVMRange() netaddr.IPPrefix {
|
|
|
|
chromeOSRange.Do(func() { mustPrefix(&chromeOSRange.v, "100.115.92.0/23") })
|
|
|
|
return chromeOSRange.v
|
|
|
|
}
|
|
|
|
|
|
|
|
var chromeOSRange oncePrefix
|
|
|
|
|
|
|
|
// CGNATRange returns the Carrier Grade NAT address range that
|
|
|
|
// is the superset range that Tailscale assigns out of.
|
|
|
|
// See https://tailscale.com/kb/1015/100.x-addresses.
|
|
|
|
// Note that Tailscale does not assign out of the ChromeOSVMRange.
|
|
|
|
func CGNATRange() netaddr.IPPrefix {
|
|
|
|
cgnatRange.Do(func() { mustPrefix(&cgnatRange.v, "100.64.0.0/10") })
|
|
|
|
return cgnatRange.v
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:12:23 +00:00
|
|
|
var (
|
|
|
|
cgnatRange oncePrefix
|
|
|
|
ulaRange oncePrefix
|
2021-03-30 01:28:57 +00:00
|
|
|
tsUlaRange oncePrefix
|
2022-03-30 15:47:16 +00:00
|
|
|
tsViaRange oncePrefix
|
2020-10-15 21:12:23 +00:00
|
|
|
ula4To6Range oncePrefix
|
2021-04-16 18:47:55 +00:00
|
|
|
ulaEph6Range oncePrefix
|
2022-01-04 21:33:08 +00:00
|
|
|
serviceIPv6 oncePrefix
|
2020-10-15 21:12:23 +00:00
|
|
|
)
|
2020-07-07 05:33:29 +00:00
|
|
|
|
2022-01-04 21:33:08 +00:00
|
|
|
// TailscaleServiceIP returns the IPv4 listen address of services
|
2021-03-23 17:21:01 +00:00
|
|
|
// provided by Tailscale itself such as the MagicDNS proxy.
|
2022-01-04 21:33:08 +00:00
|
|
|
//
|
|
|
|
// For IPv6, use TailscaleServiceIPv6.
|
2020-07-31 20:27:09 +00:00
|
|
|
func TailscaleServiceIP() netaddr.IP {
|
2021-05-29 03:13:01 +00:00
|
|
|
return netaddr.IPv4(100, 100, 100, 100) // "100.100.100.100" for those grepping
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 21:33:08 +00:00
|
|
|
// TailscaleServiceIPv6 returns the IPv6 listen address of the services
|
|
|
|
// provided by Tailscale itself such as the MagicDNS proxy.
|
|
|
|
//
|
|
|
|
// For IPv4, use TailscaleServiceIP.
|
|
|
|
func TailscaleServiceIPv6() netaddr.IP {
|
|
|
|
serviceIPv6.Do(func() { mustPrefix(&serviceIPv6.v, "fd7a:115c:a1e0::53/128") })
|
|
|
|
return serviceIPv6.v.IP()
|
|
|
|
}
|
|
|
|
|
2020-07-07 05:33:29 +00:00
|
|
|
// IsTailscaleIP reports whether ip is an IP address in a range that
|
|
|
|
// Tailscale assigns from.
|
|
|
|
func IsTailscaleIP(ip netaddr.IP) bool {
|
2020-10-15 21:12:23 +00:00
|
|
|
if ip.Is4() {
|
|
|
|
return CGNATRange().Contains(ip) && !ChromeOSVMRange().Contains(ip)
|
|
|
|
}
|
|
|
|
return TailscaleULARange().Contains(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TailscaleULARange returns the IPv6 Unique Local Address range that
|
|
|
|
// is the superset range that Tailscale assigns out of.
|
|
|
|
func TailscaleULARange() netaddr.IPPrefix {
|
2021-03-30 01:28:57 +00:00
|
|
|
tsUlaRange.Do(func() { mustPrefix(&tsUlaRange.v, "fd7a:115c:a1e0::/48") })
|
|
|
|
return tsUlaRange.v
|
2020-10-15 21:12:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:47:16 +00:00
|
|
|
// TailscaleViaRange returns the IPv6 Unique Local Address subset range
|
|
|
|
// TailscaleULARange that's used for IPv4 tunneling via IPv6.
|
|
|
|
func TailscaleViaRange() netaddr.IPPrefix {
|
|
|
|
// Mnemonic: "b1a" sounds like "via".
|
|
|
|
tsViaRange.Do(func() { mustPrefix(&tsViaRange.v, "fd7a:115c:a1e0:b1a::/64") })
|
|
|
|
return tsViaRange.v
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:12:23 +00:00
|
|
|
// Tailscale4To6Range returns the subset of TailscaleULARange used for
|
|
|
|
// auto-translated Tailscale ipv4 addresses.
|
|
|
|
func Tailscale4To6Range() netaddr.IPPrefix {
|
|
|
|
// This IP range has no significance, beyond being a subset of
|
|
|
|
// TailscaleULARange. The bits from /48 to /104 were picked at
|
|
|
|
// random.
|
|
|
|
ula4To6Range.Do(func() { mustPrefix(&ula4To6Range.v, "fd7a:115c:a1e0:ab12:4843:cd96:6200::/104") })
|
|
|
|
return ula4To6Range.v
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:47:55 +00:00
|
|
|
// TailscaleEphemeral6Range returns the subset of TailscaleULARange
|
|
|
|
// used for ephemeral IPv6-only Tailscale nodes.
|
|
|
|
func TailscaleEphemeral6Range() netaddr.IPPrefix {
|
|
|
|
// This IP range has no significance, beyond being a subset of
|
2021-04-19 19:54:53 +00:00
|
|
|
// TailscaleULARange. The bits from /48 to /64 were picked at
|
2021-04-16 18:47:55 +00:00
|
|
|
// random, with the only criterion being to not be the conflict
|
|
|
|
// with the Tailscale4To6Range above.
|
2021-04-19 19:54:53 +00:00
|
|
|
ulaEph6Range.Do(func() { mustPrefix(&ulaEph6Range.v, "fd7a:115c:a1e0:efe3::/64") })
|
2021-04-16 18:47:55 +00:00
|
|
|
return ulaEph6Range.v
|
|
|
|
}
|
|
|
|
|
2021-03-03 03:32:04 +00:00
|
|
|
// Tailscale4To6Placeholder returns an IP address that can be used as
|
|
|
|
// a source IP when one is required, but a netmap didn't provide
|
|
|
|
// any. This address never gets allocated by the 4-to-6 algorithm in
|
|
|
|
// control.
|
|
|
|
//
|
|
|
|
// Currently used to work around a Windows limitation when programming
|
|
|
|
// IPv6 routes in corner cases.
|
|
|
|
func Tailscale4To6Placeholder() netaddr.IP {
|
2021-05-15 01:07:28 +00:00
|
|
|
return Tailscale4To6Range().IP()
|
2021-03-03 03:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:12:23 +00:00
|
|
|
// Tailscale4To6 returns a Tailscale IPv6 address that maps 1:1 to the
|
|
|
|
// given Tailscale IPv4 address. Returns a zero IP if ipv4 isn't a
|
|
|
|
// Tailscale IPv4 address.
|
|
|
|
func Tailscale4To6(ipv4 netaddr.IP) netaddr.IP {
|
|
|
|
if !ipv4.Is4() || !IsTailscaleIP(ipv4) {
|
|
|
|
return netaddr.IP{}
|
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
ret := Tailscale4To6Range().IP().As16()
|
2020-10-15 21:12:23 +00:00
|
|
|
v4 := ipv4.As4()
|
|
|
|
copy(ret[13:], v4[1:])
|
|
|
|
return netaddr.IPFrom16(ret)
|
2020-07-07 05:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustPrefix(v *netaddr.IPPrefix, prefix string) {
|
|
|
|
var err error
|
|
|
|
*v, err = netaddr.ParseIPPrefix(prefix)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type oncePrefix struct {
|
|
|
|
sync.Once
|
|
|
|
v netaddr.IPPrefix
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2021-05-05 19:58:40 +00:00
|
|
|
// NewContainsIPFunc returns a func that reports whether ip is in addrs.
|
|
|
|
//
|
|
|
|
// It's optimized for the cases of addrs being empty and addrs
|
|
|
|
// containing 1 or 2 single-IP prefixes (such as one IPv4 address and
|
|
|
|
// one IPv6 address).
|
|
|
|
//
|
|
|
|
// Otherwise the implementation is somewhat slow.
|
|
|
|
func NewContainsIPFunc(addrs []netaddr.IPPrefix) func(ip netaddr.IP) bool {
|
|
|
|
// Specialize the three common cases: no address, just IPv4
|
|
|
|
// (or just IPv6), and both IPv4 and IPv6.
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return func(netaddr.IP) bool { return false }
|
|
|
|
}
|
|
|
|
// If any addr is more than a single IP, then just do the slow
|
|
|
|
// linear thing until
|
|
|
|
// https://github.com/inetaf/netaddr/issues/139 is done.
|
|
|
|
for _, a := range addrs {
|
|
|
|
if a.IsSingleIP() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
acopy := append([]netaddr.IPPrefix(nil), addrs...)
|
|
|
|
return func(ip netaddr.IP) bool {
|
|
|
|
for _, a := range acopy {
|
|
|
|
if a.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fast paths for 1 and 2 IPs:
|
|
|
|
if len(addrs) == 1 {
|
|
|
|
a := addrs[0]
|
2021-05-15 01:07:28 +00:00
|
|
|
return func(ip netaddr.IP) bool { return ip == a.IP() }
|
2021-05-05 19:58:40 +00:00
|
|
|
}
|
|
|
|
if len(addrs) == 2 {
|
|
|
|
a, b := addrs[0], addrs[1]
|
2021-05-15 01:07:28 +00:00
|
|
|
return func(ip netaddr.IP) bool { return ip == a.IP() || ip == b.IP() }
|
2021-05-05 19:58:40 +00:00
|
|
|
}
|
|
|
|
// General case:
|
|
|
|
m := map[netaddr.IP]bool{}
|
|
|
|
for _, a := range addrs {
|
2021-05-15 01:07:28 +00:00
|
|
|
m[a.IP()] = true
|
2021-05-05 19:58:40 +00:00
|
|
|
}
|
|
|
|
return func(ip netaddr.IP) bool { return m[ip] }
|
|
|
|
}
|
2021-09-07 22:45:28 +00:00
|
|
|
|
|
|
|
// PrefixesContainsFunc reports whether f is true for any IPPrefix in
|
|
|
|
// ipp.
|
|
|
|
func PrefixesContainsFunc(ipp []netaddr.IPPrefix, f func(netaddr.IPPrefix) bool) bool {
|
|
|
|
for _, v := range ipp {
|
|
|
|
if f(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-29 02:01:50 +00:00
|
|
|
// PrefixesContainsIP reports whether any prefix in ipp contains ip.
|
|
|
|
func PrefixesContainsIP(ipp []netaddr.IPPrefix, ip netaddr.IP) bool {
|
|
|
|
for _, r := range ipp {
|
|
|
|
if r.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-07 22:45:28 +00:00
|
|
|
// IPsContainsFunc reports whether f is true for any IP in ips.
|
|
|
|
func IPsContainsFunc(ips []netaddr.IP, f func(netaddr.IP) bool) bool {
|
|
|
|
for _, v := range ips {
|
|
|
|
if f(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-10-04 14:54:16 +00:00
|
|
|
|
|
|
|
// PrefixIs4 reports whether p is an IPv4 prefix.
|
|
|
|
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() }
|
2022-03-04 16:00:53 +00:00
|
|
|
|
|
|
|
// 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 }
|
2022-03-28 17:24:11 +00:00
|
|
|
|
|
|
|
// ExitRoutes returns a slice containing AllIPv4 and AllIPv6.
|
|
|
|
func ExitRoutes() []netaddr.IPPrefix { return []netaddr.IPPrefix{allIPv4, allIPv6} }
|
2022-03-30 15:47:16 +00:00
|
|
|
|
|
|
|
// FilterPrefixes returns a new slice, not aliasing in, containing elements of
|
|
|
|
// in that match f.
|
|
|
|
func FilterPrefixesCopy(in []netaddr.IPPrefix, f func(netaddr.IPPrefix) bool) []netaddr.IPPrefix {
|
|
|
|
var out []netaddr.IPPrefix
|
|
|
|
for _, v := range in {
|
|
|
|
if f(v) {
|
|
|
|
out = append(out, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsViaPrefix reports whether p is a CIDR in the Tailscale "via" range.
|
|
|
|
// See TailscaleViaRange.
|
|
|
|
func IsViaPrefix(p netaddr.IPPrefix) bool {
|
|
|
|
return TailscaleViaRange().Contains(p.IP())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmapVia returns the IPv4 address that corresponds to the provided Tailscale
|
|
|
|
// "via" IPv4-in-IPv6 address.
|
|
|
|
//
|
|
|
|
// If ip is not a via address, it returns ip unchanged.
|
|
|
|
func UnmapVia(ip netaddr.IP) netaddr.IP {
|
|
|
|
if TailscaleViaRange().Contains(ip) {
|
|
|
|
a := ip.As16()
|
|
|
|
return netaddr.IPFrom4(*(*[4]byte)(a[12:16]))
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|