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
|
2020-10-15 21:12:23 +00:00
|
|
|
ula4To6Range oncePrefix
|
2021-04-16 18:47:55 +00:00
|
|
|
ulaEph6Range oncePrefix
|
2020-10-15 21:12:23 +00:00
|
|
|
)
|
2020-07-07 05:33:29 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// TailscaleServiceIP returns the listen address of services
|
2021-03-23 17:21:01 +00:00
|
|
|
// provided by Tailscale itself such as the MagicDNS proxy.
|
2020-07-31 20:27:09 +00:00
|
|
|
func TailscaleServiceIP() netaddr.IP {
|
|
|
|
serviceIP.Do(func() { mustIP(&serviceIP.v, "100.100.100.100") })
|
|
|
|
return serviceIP.v
|
|
|
|
}
|
|
|
|
|
|
|
|
var serviceIP onceIP
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return Tailscale4To6Range().IP
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
}
|
|
|
|
ret := Tailscale4To6Range().IP.As16()
|
|
|
|
v4 := ipv4.As4()
|
|
|
|
copy(ret[13:], v4[1:])
|
|
|
|
return netaddr.IPFrom16(ret)
|
2020-07-07 05:33:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 01:28:57 +00:00
|
|
|
func IsULA(ip netaddr.IP) bool {
|
|
|
|
ulaRange.Do(func() { mustPrefix(&ulaRange.v, "fc00::/7") })
|
|
|
|
return ulaRange.v.Contains(ip)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func mustIP(v *netaddr.IP, ip string) {
|
|
|
|
var err error
|
|
|
|
*v, err = netaddr.ParseIP(ip)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type onceIP struct {
|
|
|
|
sync.Once
|
|
|
|
v netaddr.IP
|
|
|
|
}
|