2020-02-21 16:13:21 +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 interfaces contains helpers for looking up system network interfaces.
|
|
|
|
package interfaces
|
|
|
|
|
|
|
|
import (
|
2020-03-27 20:26:35 +00:00
|
|
|
"fmt"
|
2020-02-21 16:13:21 +00:00
|
|
|
"net"
|
2020-08-13 22:25:54 +00:00
|
|
|
"net/http"
|
2020-03-10 19:25:42 +00:00
|
|
|
"reflect"
|
2021-01-26 23:14:59 +00:00
|
|
|
"runtime"
|
2020-10-02 19:07:00 +00:00
|
|
|
"sort"
|
2020-02-21 16:13:21 +00:00
|
|
|
"strings"
|
2020-05-28 16:58:52 +00:00
|
|
|
|
|
|
|
"inet.af/netaddr"
|
2020-07-07 05:33:29 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2020-08-13 22:25:54 +00:00
|
|
|
"tailscale.com/net/tshttpproxy"
|
2020-02-21 16:13:21 +00:00
|
|
|
)
|
|
|
|
|
2020-08-13 22:25:54 +00:00
|
|
|
// LoginEndpointForProxyDetermination is the URL used for testing
|
|
|
|
// which HTTP proxy the system should use.
|
|
|
|
var LoginEndpointForProxyDetermination = "https://login.tailscale.com/"
|
|
|
|
|
2020-02-21 16:13:21 +00:00
|
|
|
// Tailscale returns the current machine's Tailscale interface, if any.
|
|
|
|
// If none is found, all zero values are returned.
|
|
|
|
// A non-nil error is only returned on a problem listing the system interfaces.
|
|
|
|
func Tailscale() (net.IP, *net.Interface, error) {
|
|
|
|
ifs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for _, iface := range ifs {
|
|
|
|
if !maybeTailscaleInterfaceName(iface.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
2021-01-26 23:00:05 +00:00
|
|
|
if ipnet, ok := a.(*net.IPNet); ok {
|
|
|
|
nip, ok := netaddr.FromStdIP(ipnet.IP)
|
|
|
|
if ok && tsaddr.IsTailscaleIP(nip) {
|
|
|
|
return ipnet.IP, &iface, nil
|
|
|
|
}
|
2020-02-21 16:13:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybeTailscaleInterfaceName reports whether s is an interface
|
|
|
|
// name that might be used by Tailscale.
|
|
|
|
func maybeTailscaleInterfaceName(s string) bool {
|
2020-08-12 21:03:34 +00:00
|
|
|
return s == "Tailscale" ||
|
|
|
|
strings.HasPrefix(s, "wg") ||
|
2020-02-21 16:13:21 +00:00
|
|
|
strings.HasPrefix(s, "ts") ||
|
2020-02-27 22:06:50 +00:00
|
|
|
strings.HasPrefix(s, "tailscale") ||
|
|
|
|
strings.HasPrefix(s, "utun")
|
2020-02-21 16:13:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 18:38:44 +00:00
|
|
|
func isUp(nif *net.Interface) bool { return nif.Flags&net.FlagUp != 0 }
|
|
|
|
func isLoopback(nif *net.Interface) bool { return nif.Flags&net.FlagLoopback != 0 }
|
|
|
|
|
2021-01-26 23:14:59 +00:00
|
|
|
func isProblematicInterface(nif *net.Interface) bool {
|
|
|
|
name := nif.Name
|
|
|
|
// Don't try to send disco/etc packets over zerotier; they effectively
|
|
|
|
// DoS each other by doing traffic amplification, both of them
|
|
|
|
// preferring/trying to use each other for transport. See:
|
|
|
|
// https://github.com/tailscale/tailscale/issues/1208
|
|
|
|
if strings.HasPrefix(name, "zt") || (runtime.GOOS == "windows" && strings.Contains(name, "ZeroTier")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:38:44 +00:00
|
|
|
// LocalAddresses returns the machine's IP addresses, separated by
|
|
|
|
// whether they're loopback addresses.
|
2021-03-04 06:02:45 +00:00
|
|
|
func LocalAddresses() (regular, loopback []netaddr.IP, err error) {
|
2020-03-02 18:38:44 +00:00
|
|
|
// TODO(crawshaw): don't serve interface addresses that we are routing
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for i := range ifaces {
|
|
|
|
iface := &ifaces[i]
|
2021-01-26 23:14:59 +00:00
|
|
|
if !isUp(iface) || isProblematicInterface(iface) {
|
|
|
|
// Skip down interfaces and ones that are
|
|
|
|
// problematic that we don't want to try to
|
|
|
|
// send Tailscale traffic over.
|
2020-03-02 18:38:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ifcIsLoopback := isLoopback(iface)
|
|
|
|
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *net.IPNet:
|
2020-05-28 16:58:52 +00:00
|
|
|
ip, ok := netaddr.FromStdIP(v.IP)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-02 18:38:44 +00:00
|
|
|
// TODO(apenwarr): don't special case cgNAT.
|
|
|
|
// In the general wireguard case, it might
|
|
|
|
// very well be something we can route to
|
|
|
|
// directly, because both nodes are
|
|
|
|
// behind the same CGNAT router.
|
2020-07-07 05:33:29 +00:00
|
|
|
if tsaddr.IsTailscaleIP(ip) {
|
2020-03-02 18:38:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-16 19:20:44 +00:00
|
|
|
if ip.IsLinkLocalUnicast() {
|
2020-03-02 18:38:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ip.IsLoopback() || ifcIsLoopback {
|
2021-03-04 06:02:45 +00:00
|
|
|
loopback = append(loopback, ip)
|
2020-03-02 18:38:44 +00:00
|
|
|
} else {
|
2021-03-04 06:02:45 +00:00
|
|
|
regular = append(regular, ip)
|
2020-03-02 18:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 06:02:45 +00:00
|
|
|
sortIPs(regular)
|
|
|
|
sortIPs(loopback)
|
2020-03-02 18:38:44 +00:00
|
|
|
return regular, loopback, nil
|
|
|
|
}
|
|
|
|
|
2021-03-04 06:02:45 +00:00
|
|
|
func sortIPs(s []netaddr.IP) {
|
|
|
|
sort.Slice(s, func(i, j int) bool { return s[i].Less(s[j]) })
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:02:30 +00:00
|
|
|
// Interface is a wrapper around Go's net.Interface with some extra methods.
|
|
|
|
type Interface struct {
|
|
|
|
*net.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Interface) IsLoopback() bool { return isLoopback(i.Interface) }
|
|
|
|
func (i Interface) IsUp() bool { return isUp(i.Interface) }
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
// ForeachInterfaceAddress calls fn for each interface's address on
|
|
|
|
// the machine. The IPPrefix's IP is the IP address assigned to the
|
|
|
|
// interface, and Bits are the subnet mask.
|
|
|
|
func ForeachInterfaceAddress(fn func(Interface, netaddr.IPPrefix)) error {
|
2020-03-10 18:02:30 +00:00
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range ifaces {
|
|
|
|
iface := &ifaces[i]
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *net.IPNet:
|
2021-02-23 04:43:35 +00:00
|
|
|
if pfx, ok := netaddr.FromStdIPNet(v); ok {
|
|
|
|
fn(Interface{iface}, pfx)
|
2020-05-28 16:58:52 +00:00
|
|
|
}
|
2020-03-10 18:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
// ForeachInterface calls fn for each interface on the machine, with
|
|
|
|
// all its addresses. The IPPrefix's IP is the IP address assigned to
|
|
|
|
// the interface, and Bits are the subnet mask.
|
|
|
|
func ForeachInterface(fn func(Interface, []netaddr.IPPrefix)) error {
|
2021-02-25 23:47:29 +00:00
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range ifaces {
|
|
|
|
iface := &ifaces[i]
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
var pfxs []netaddr.IPPrefix
|
2021-02-25 23:47:29 +00:00
|
|
|
for _, a := range addrs {
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *net.IPNet:
|
2021-02-23 04:43:35 +00:00
|
|
|
if pfx, ok := netaddr.FromStdIPNet(v); ok {
|
|
|
|
pfxs = append(pfxs, pfx)
|
2021-02-25 23:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
fn(Interface{iface}, pfxs)
|
2021-02-25 23:47:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:25:42 +00:00
|
|
|
// State is intended to store the state of the machine's network interfaces,
|
|
|
|
// routing table, and other network configuration.
|
|
|
|
// For now it's pretty basic.
|
|
|
|
type State struct {
|
2021-02-23 04:43:35 +00:00
|
|
|
// InterfaceIPs maps from an interface name to the IP addresses
|
|
|
|
// configured on that interface. Each address is represented as an
|
|
|
|
// IPPrefix, where the IP is the interface IP address and Bits is
|
|
|
|
// the subnet mask.
|
|
|
|
InterfaceIPs map[string][]netaddr.IPPrefix
|
2021-03-26 04:26:01 +00:00
|
|
|
Interface map[string]Interface
|
2020-04-10 02:10:55 +00:00
|
|
|
|
2020-05-28 16:58:52 +00:00
|
|
|
// HaveV6Global is whether this machine has an IPv6 global address
|
2020-10-06 22:22:46 +00:00
|
|
|
// on some non-Tailscale interface that's up.
|
2020-05-28 16:58:52 +00:00
|
|
|
HaveV6Global bool
|
|
|
|
|
2020-10-06 22:22:46 +00:00
|
|
|
// HaveV4 is whether the machine has some non-localhost,
|
|
|
|
// non-link-local IPv4 address on a non-Tailscale interface that's up.
|
2020-05-28 16:58:52 +00:00
|
|
|
HaveV4 bool
|
|
|
|
|
2020-04-10 02:10:55 +00:00
|
|
|
// IsExpensive is whether the current network interface is
|
|
|
|
// considered "expensive", which currently means LTE/etc
|
|
|
|
// instead of Wifi. This field is not populated by GetState.
|
|
|
|
IsExpensive bool
|
2020-08-12 19:48:34 +00:00
|
|
|
|
|
|
|
// DefaultRouteInterface is the interface name for the machine's default route.
|
|
|
|
// It is not yet populated on all OSes.
|
2020-10-06 22:22:46 +00:00
|
|
|
// Its exact value should not be assumed to be a map key for
|
|
|
|
// the Interface maps above; it's only used for debugging.
|
2020-08-12 19:48:34 +00:00
|
|
|
DefaultRouteInterface string
|
2020-08-13 22:25:54 +00:00
|
|
|
|
|
|
|
// HTTPProxy is the HTTP proxy to use.
|
|
|
|
HTTPProxy string
|
2020-10-01 22:33:37 +00:00
|
|
|
|
|
|
|
// PAC is the URL to the Proxy Autoconfig URL, if applicable.
|
|
|
|
PAC string
|
2020-03-10 19:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:07:00 +00:00
|
|
|
func (s *State) String() string {
|
|
|
|
var sb strings.Builder
|
|
|
|
fmt.Fprintf(&sb, "interfaces.State{defaultRoute=%v ifs={", s.DefaultRouteInterface)
|
2021-03-26 04:26:01 +00:00
|
|
|
ifs := make([]string, 0, len(s.Interface))
|
|
|
|
for k := range s.Interface {
|
2021-02-13 02:38:16 +00:00
|
|
|
if anyInterestingIP(s.InterfaceIPs[k]) {
|
|
|
|
ifs = append(ifs, k)
|
2020-10-02 19:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(ifs, func(i, j int) bool {
|
2021-03-26 04:26:01 +00:00
|
|
|
upi, upj := s.Interface[ifs[i]].IsUp(), s.Interface[ifs[j]].IsUp()
|
2020-10-02 19:07:00 +00:00
|
|
|
if upi != upj {
|
|
|
|
// Up sorts before down.
|
|
|
|
return upi
|
|
|
|
}
|
|
|
|
return ifs[i] < ifs[j]
|
|
|
|
})
|
|
|
|
for i, ifName := range ifs {
|
|
|
|
if i > 0 {
|
|
|
|
sb.WriteString(" ")
|
|
|
|
}
|
2021-03-26 04:26:01 +00:00
|
|
|
if s.Interface[ifName].IsUp() {
|
2020-10-02 19:07:00 +00:00
|
|
|
fmt.Fprintf(&sb, "%s:[", ifName)
|
|
|
|
needSpace := false
|
2021-02-23 04:43:35 +00:00
|
|
|
for _, pfx := range s.InterfaceIPs[ifName] {
|
|
|
|
if !isInterestingIP(pfx.IP) {
|
2020-10-02 19:07:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if needSpace {
|
|
|
|
sb.WriteString(" ")
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
fmt.Fprintf(&sb, "%s", pfx)
|
2020-10-02 19:07:00 +00:00
|
|
|
needSpace = true
|
|
|
|
}
|
|
|
|
sb.WriteString("]")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(&sb, "%s:down", ifName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.WriteString("}")
|
|
|
|
|
|
|
|
if s.IsExpensive {
|
|
|
|
sb.WriteString(" expensive")
|
|
|
|
}
|
|
|
|
if s.HTTPProxy != "" {
|
|
|
|
fmt.Fprintf(&sb, " httpproxy=%s", s.HTTPProxy)
|
|
|
|
}
|
|
|
|
if s.PAC != "" {
|
|
|
|
fmt.Fprintf(&sb, " pac=%s", s.PAC)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&sb, " v4=%v v6global=%v}", s.HaveV4, s.HaveV6Global)
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:25:42 +00:00
|
|
|
func (s *State) Equal(s2 *State) bool {
|
|
|
|
return reflect.DeepEqual(s, s2)
|
|
|
|
}
|
|
|
|
|
2020-10-05 22:12:35 +00:00
|
|
|
func (s *State) HasPAC() bool { return s != nil && s.PAC != "" }
|
|
|
|
|
2020-10-06 22:22:46 +00:00
|
|
|
// AnyInterfaceUp reports whether any interface seems like it has Internet access.
|
|
|
|
func (s *State) AnyInterfaceUp() bool {
|
|
|
|
return s != nil && (s.HaveV4 || s.HaveV6Global)
|
|
|
|
}
|
|
|
|
|
net/interfaces, wgengine/monitor: fix false positives link changes
interfaces.State.String tries to print a concise summary of the
network state, removing any interfaces that don't have any or any
interesting IP addresses. On macOS and iOS, for instance, there are a
ton of misc things.
But the link monitor based its are-there-changes decision on
interfaces.State.Equal, which just used reflect.DeepEqual, including
comparing all the boring interfaces. On macOS, when turning wifi on or off, there
are a ton of misc boring interface changes, resulting in hitting an earlier
check I'd added on suspicion this was happening:
[unexpected] network state changed, but stringification didn't
This fixes that by instead adding a new
interfaces.State.RemoveUninterestingInterfacesAndAddresses method that
does, uh, that. Then use that in the monitor. So then when Equal is
used later, it's DeepEqualing the already-cleaned version with only
interesting interfaces.
This makes cmd/tailscaled debug --monitor much less noisy.
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-03-09 04:41:23 +00:00
|
|
|
// RemoveUninterestingInterfacesAndAddresses removes uninteresting IPs
|
|
|
|
// from InterfaceIPs, also removing from both the InterfaceIPs and
|
|
|
|
// InterfaceUp map any interfaces that don't have any interesting IPs.
|
|
|
|
func (s *State) RemoveUninterestingInterfacesAndAddresses() {
|
2021-03-26 04:26:01 +00:00
|
|
|
for ifName := range s.Interface {
|
net/interfaces, wgengine/monitor: fix false positives link changes
interfaces.State.String tries to print a concise summary of the
network state, removing any interfaces that don't have any or any
interesting IP addresses. On macOS and iOS, for instance, there are a
ton of misc things.
But the link monitor based its are-there-changes decision on
interfaces.State.Equal, which just used reflect.DeepEqual, including
comparing all the boring interfaces. On macOS, when turning wifi on or off, there
are a ton of misc boring interface changes, resulting in hitting an earlier
check I'd added on suspicion this was happening:
[unexpected] network state changed, but stringification didn't
This fixes that by instead adding a new
interfaces.State.RemoveUninterestingInterfacesAndAddresses method that
does, uh, that. Then use that in the monitor. So then when Equal is
used later, it's DeepEqualing the already-cleaned version with only
interesting interfaces.
This makes cmd/tailscaled debug --monitor much less noisy.
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-03-09 04:41:23 +00:00
|
|
|
ips := s.InterfaceIPs[ifName]
|
|
|
|
keep := ips[:0]
|
|
|
|
for _, pfx := range ips {
|
|
|
|
if isInterestingIP(pfx.IP) {
|
|
|
|
keep = append(keep, pfx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(keep) == 0 {
|
2021-03-26 04:26:01 +00:00
|
|
|
delete(s.Interface, ifName)
|
net/interfaces, wgengine/monitor: fix false positives link changes
interfaces.State.String tries to print a concise summary of the
network state, removing any interfaces that don't have any or any
interesting IP addresses. On macOS and iOS, for instance, there are a
ton of misc things.
But the link monitor based its are-there-changes decision on
interfaces.State.Equal, which just used reflect.DeepEqual, including
comparing all the boring interfaces. On macOS, when turning wifi on or off, there
are a ton of misc boring interface changes, resulting in hitting an earlier
check I'd added on suspicion this was happening:
[unexpected] network state changed, but stringification didn't
This fixes that by instead adding a new
interfaces.State.RemoveUninterestingInterfacesAndAddresses method that
does, uh, that. Then use that in the monitor. So then when Equal is
used later, it's DeepEqualing the already-cleaned version with only
interesting interfaces.
This makes cmd/tailscaled debug --monitor much less noisy.
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-03-09 04:41:23 +00:00
|
|
|
delete(s.InterfaceIPs, ifName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(keep) < len(ips) {
|
|
|
|
s.InterfaceIPs[ifName] = keep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:25:42 +00:00
|
|
|
// RemoveTailscaleInterfaces modifes s to remove any interfaces that
|
|
|
|
// are owned by this process. (TODO: make this true; currently it
|
2021-02-25 23:47:29 +00:00
|
|
|
// uses some heuristics)
|
2020-03-10 19:25:42 +00:00
|
|
|
func (s *State) RemoveTailscaleInterfaces() {
|
2021-02-23 04:43:35 +00:00
|
|
|
for name, pfxs := range s.InterfaceIPs {
|
|
|
|
if isTailscaleInterface(name, pfxs) {
|
2020-03-10 19:25:42 +00:00
|
|
|
delete(s.InterfaceIPs, name)
|
2021-03-26 04:26:01 +00:00
|
|
|
delete(s.Interface, name)
|
2020-03-10 19:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
func hasTailscaleIP(pfxs []netaddr.IPPrefix) bool {
|
|
|
|
for _, pfx := range pfxs {
|
|
|
|
if tsaddr.IsTailscaleIP(pfx.IP) {
|
2021-02-25 23:47:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
func isTailscaleInterface(name string, ips []netaddr.IPPrefix) bool {
|
2021-02-25 23:47:29 +00:00
|
|
|
if runtime.GOOS == "darwin" && strings.HasPrefix(name, "utun") && hasTailscaleIP(ips) {
|
|
|
|
// On macOS in the sandboxed app (at least as of
|
|
|
|
// 2021-02-25), we often see two utun devices
|
|
|
|
// (e.g. utun4 and utun7) with the same IPv4 and IPv6
|
|
|
|
// addresses. Just remove all utun devices with
|
|
|
|
// Tailscale IPs until we know what's happening with
|
|
|
|
// macOS NetworkExtensions and utun devices.
|
|
|
|
return true
|
|
|
|
}
|
2020-10-06 22:22:46 +00:00
|
|
|
return name == "Tailscale" || // as it is on Windows
|
|
|
|
strings.HasPrefix(name, "tailscale") // TODO: use --tun flag value, etc; see TODO in method doc
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:33:37 +00:00
|
|
|
// getPAC, if non-nil, returns the current PAC file URL.
|
|
|
|
var getPAC func() string
|
|
|
|
|
2020-04-10 02:10:55 +00:00
|
|
|
// GetState returns the state of all the current machine's network interfaces.
|
|
|
|
//
|
|
|
|
// It does not set the returned State.IsExpensive. The caller can populate that.
|
2020-03-10 19:25:42 +00:00
|
|
|
func GetState() (*State, error) {
|
2020-04-10 02:10:55 +00:00
|
|
|
s := &State{
|
2021-02-23 04:43:35 +00:00
|
|
|
InterfaceIPs: make(map[string][]netaddr.IPPrefix),
|
2021-03-26 04:26:01 +00:00
|
|
|
Interface: make(map[string]Interface),
|
2020-04-10 02:10:55 +00:00
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
if err := ForeachInterface(func(ni Interface, pfxs []netaddr.IPPrefix) {
|
2020-10-06 22:22:46 +00:00
|
|
|
ifUp := ni.IsUp()
|
2021-03-26 04:26:01 +00:00
|
|
|
s.Interface[ni.Name] = ni
|
2021-02-23 04:43:35 +00:00
|
|
|
s.InterfaceIPs[ni.Name] = append(s.InterfaceIPs[ni.Name], pfxs...)
|
|
|
|
if !ifUp || isTailscaleInterface(ni.Name, pfxs) {
|
2021-02-25 23:47:29 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
for _, pfx := range pfxs {
|
|
|
|
if pfx.IP.IsLoopback() || pfx.IP.IsLinkLocalUnicast() {
|
2021-02-25 23:47:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
s.HaveV6Global = s.HaveV6Global || isGlobalV6(pfx.IP)
|
|
|
|
s.HaveV4 = s.HaveV4 || pfx.IP.Is4()
|
2020-10-06 22:22:46 +00:00
|
|
|
}
|
2020-03-10 19:25:42 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
net/interfaces, wgengine/monitor: fix false positives link changes
interfaces.State.String tries to print a concise summary of the
network state, removing any interfaces that don't have any or any
interesting IP addresses. On macOS and iOS, for instance, there are a
ton of misc things.
But the link monitor based its are-there-changes decision on
interfaces.State.Equal, which just used reflect.DeepEqual, including
comparing all the boring interfaces. On macOS, when turning wifi on or off, there
are a ton of misc boring interface changes, resulting in hitting an earlier
check I'd added on suspicion this was happening:
[unexpected] network state changed, but stringification didn't
This fixes that by instead adding a new
interfaces.State.RemoveUninterestingInterfacesAndAddresses method that
does, uh, that. Then use that in the monitor. So then when Equal is
used later, it's DeepEqualing the already-cleaned version with only
interesting interfaces.
This makes cmd/tailscaled debug --monitor much less noisy.
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-03-09 04:41:23 +00:00
|
|
|
|
2020-08-12 19:48:34 +00:00
|
|
|
s.DefaultRouteInterface, _ = DefaultRouteInterface()
|
2020-08-13 22:25:54 +00:00
|
|
|
|
2020-10-06 22:22:46 +00:00
|
|
|
if s.AnyInterfaceUp() {
|
|
|
|
req, err := http.NewRequest("GET", LoginEndpointForProxyDetermination, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if u, err := tshttpproxy.ProxyFromEnvironment(req); err == nil && u != nil {
|
|
|
|
s.HTTPProxy = u.String()
|
|
|
|
}
|
|
|
|
if getPAC != nil {
|
|
|
|
s.PAC = getPAC()
|
|
|
|
}
|
2020-10-01 22:33:37 +00:00
|
|
|
}
|
2020-08-13 22:25:54 +00:00
|
|
|
|
2020-03-10 19:25:42 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
2020-03-27 20:26:35 +00:00
|
|
|
|
|
|
|
// HTTPOfListener returns the HTTP address to ln.
|
|
|
|
// If the listener is listening on the unspecified address, it
|
|
|
|
// it tries to find a reasonable interface address on the machine to use.
|
|
|
|
func HTTPOfListener(ln net.Listener) string {
|
|
|
|
ta, ok := ln.Addr().(*net.TCPAddr)
|
|
|
|
if !ok || !ta.IP.IsUnspecified() {
|
|
|
|
return fmt.Sprintf("http://%v/", ln.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
var goodIP string
|
|
|
|
var privateIP string
|
2021-02-23 04:43:35 +00:00
|
|
|
ForeachInterfaceAddress(func(i Interface, pfx netaddr.IPPrefix) {
|
|
|
|
ip := pfx.IP
|
2020-03-27 20:26:35 +00:00
|
|
|
if isPrivateIP(ip) {
|
|
|
|
if privateIP == "" {
|
|
|
|
privateIP = ip.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
goodIP = ip.String()
|
|
|
|
})
|
|
|
|
if privateIP != "" {
|
|
|
|
goodIP = privateIP
|
|
|
|
}
|
|
|
|
if goodIP != "" {
|
|
|
|
return fmt.Sprintf("http://%v/", net.JoinHostPort(goodIP, fmt.Sprint(ta.Port)))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("http://localhost:%v/", fmt.Sprint(ta.Port))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:34:52 +00:00
|
|
|
var likelyHomeRouterIP func() (netaddr.IP, bool)
|
|
|
|
|
|
|
|
// LikelyHomeRouterIP returns the likely IP of the residential router,
|
|
|
|
// which will always be an IPv4 private address, if found.
|
2020-07-06 20:51:17 +00:00
|
|
|
// In addition, it returns the IP address of the current machine on
|
|
|
|
// the LAN using that gateway.
|
2020-07-06 17:34:52 +00:00
|
|
|
// This is used as the destination for UPnP, NAT-PMP, PCP, etc queries.
|
2020-07-06 20:51:17 +00:00
|
|
|
func LikelyHomeRouterIP() (gateway, myIP netaddr.IP, ok bool) {
|
2020-07-06 17:34:52 +00:00
|
|
|
if likelyHomeRouterIP != nil {
|
2020-07-06 20:51:17 +00:00
|
|
|
gateway, ok = likelyHomeRouterIP()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return
|
2020-07-06 17:34:52 +00:00
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
ForeachInterfaceAddress(func(i Interface, pfx netaddr.IPPrefix) {
|
|
|
|
ip := pfx.IP
|
2020-07-06 20:51:17 +00:00
|
|
|
if !i.IsUp() || ip.IsZero() || !myIP.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, prefix := range privatev4s {
|
|
|
|
if prefix.Contains(gateway) && prefix.Contains(ip) {
|
|
|
|
myIP = ip
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return gateway, myIP, !myIP.IsZero()
|
2020-07-06 17:34:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:58:52 +00:00
|
|
|
func isPrivateIP(ip netaddr.IP) bool {
|
2020-03-27 20:26:35 +00:00
|
|
|
return private1.Contains(ip) || private2.Contains(ip) || private3.Contains(ip)
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:58:52 +00:00
|
|
|
func isGlobalV6(ip netaddr.IP) bool {
|
|
|
|
return v6Global1.Contains(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustCIDR(s string) netaddr.IPPrefix {
|
|
|
|
prefix, err := netaddr.ParseIPPrefix(s)
|
2020-03-27 20:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-28 16:58:52 +00:00
|
|
|
return prefix
|
2020-03-27 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-03-16 19:20:44 +00:00
|
|
|
private1 = mustCIDR("10.0.0.0/8")
|
|
|
|
private2 = mustCIDR("172.16.0.0/12")
|
|
|
|
private3 = mustCIDR("192.168.0.0/16")
|
|
|
|
privatev4s = []netaddr.IPPrefix{private1, private2, private3}
|
|
|
|
v6Global1 = mustCIDR("2000::/3")
|
2020-03-27 20:26:35 +00:00
|
|
|
)
|
2020-10-02 19:07:00 +00:00
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
// anyInterestingIP reports whether pfxs contains any IP that matches
|
2021-02-13 02:38:16 +00:00
|
|
|
// isInterestingIP.
|
2021-02-23 04:43:35 +00:00
|
|
|
func anyInterestingIP(pfxs []netaddr.IPPrefix) bool {
|
|
|
|
for _, pfx := range pfxs {
|
|
|
|
if isInterestingIP(pfx.IP) {
|
2021-02-13 02:38:16 +00:00
|
|
|
return true
|
2020-10-02 19:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-13 02:38:16 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isInterestingIP reports whether ip is an interesting IP that we
|
|
|
|
// should log in interfaces.State logging. We don't need to show
|
|
|
|
// localhost or link-local addresses.
|
|
|
|
func isInterestingIP(ip netaddr.IP) bool {
|
|
|
|
if ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-02 19:07:00 +00:00
|
|
|
return true
|
|
|
|
}
|