mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
10 months ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
|
|
||
6 months ago
|
//go:build darwin || ios
|
||
10 months ago
|
|
||
7 months ago
|
package netmon
|
||
10 months ago
|
|
||
|
import (
|
||
|
"log"
|
||
6 months ago
|
"net"
|
||
10 months ago
|
|
||
|
"tailscale.com/syncs"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
lastKnownDefaultRouteIfName syncs.AtomicValue[string]
|
||
|
)
|
||
|
|
||
|
// UpdateLastKnownDefaultRouteInterface is called by ipn-go-bridge in the iOS app when
|
||
|
// our NWPathMonitor instance detects a network path transition.
|
||
|
func UpdateLastKnownDefaultRouteInterface(ifName string) {
|
||
|
if ifName == "" {
|
||
|
return
|
||
|
}
|
||
9 months ago
|
if old := lastKnownDefaultRouteIfName.Swap(ifName); old != ifName {
|
||
6 months ago
|
log.Printf("defaultroute_darwin: update from Swift, ifName = %s (was %s)", ifName, old)
|
||
9 months ago
|
}
|
||
10 months ago
|
}
|
||
|
|
||
|
func defaultRoute() (d DefaultRouteDetails, err error) {
|
||
6 months ago
|
// We cannot rely on the delegated interface data on darwin. The NetworkExtension framework
|
||
10 months ago
|
// seems to set the delegate interface only once, upon the *creation* of the VPN tunnel.
|
||
|
// If a network transition (e.g. from Wi-Fi to Cellular) happens while the tunnel is
|
||
|
// connected, it will be ignored and we will still try to set Wi-Fi as the default route
|
||
|
// because the delegated interface is not updated by the NetworkExtension framework.
|
||
|
//
|
||
|
// We work around this on the Swift side with a NWPathMonitor instance that observes
|
||
|
// the interface name of the first currently satisfied network path. Our Swift code will
|
||
|
// call into `UpdateLastKnownDefaultRouteInterface`, so we can rely on that when it is set.
|
||
|
//
|
||
6 months ago
|
// If for any reason the Swift machinery didn't work and we don't get any updates, we will
|
||
|
// fallback to the BSD logic.
|
||
10 months ago
|
|
||
|
// Start by getting all available interfaces.
|
||
|
interfaces, err := netInterfaces()
|
||
|
if err != nil {
|
||
6 months ago
|
log.Printf("defaultroute_darwin: could not get interfaces: %v", err)
|
||
10 months ago
|
return d, ErrNoGatewayIndexFound
|
||
|
}
|
||
|
|
||
|
getInterfaceByName := func(name string) *Interface {
|
||
|
for _, ifc := range interfaces {
|
||
|
if ifc.Name != name {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if !ifc.IsUp() {
|
||
6 months ago
|
log.Printf("defaultroute_darwin: %s is down", name)
|
||
10 months ago
|
return nil
|
||
|
}
|
||
|
|
||
|
addrs, _ := ifc.Addrs()
|
||
|
if len(addrs) == 0 {
|
||
6 months ago
|
log.Printf("defaultroute_darwin: %s has no addresses", name)
|
||
10 months ago
|
return nil
|
||
|
}
|
||
|
return &ifc
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Did Swift set lastKnownDefaultRouteInterface? If so, we should use it and don't bother
|
||
|
// with anything else. However, for sanity, do check whether Swift gave us with an interface
|
||
|
// that exists, is up, and has an address.
|
||
|
if swiftIfName := lastKnownDefaultRouteIfName.Load(); swiftIfName != "" {
|
||
|
ifc := getInterfaceByName(swiftIfName)
|
||
|
if ifc != nil {
|
||
|
d.InterfaceName = ifc.Name
|
||
|
d.InterfaceIndex = ifc.Index
|
||
|
return d, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
6 months ago
|
// Fallback to the BSD logic
|
||
|
idx, err := DefaultRouteInterfaceIndex()
|
||
|
if err != nil {
|
||
|
return d, err
|
||
10 months ago
|
}
|
||
6 months ago
|
iface, err := net.InterfaceByIndex(idx)
|
||
|
if err != nil {
|
||
|
return d, err
|
||
10 months ago
|
}
|
||
6 months ago
|
d.InterfaceName = iface.Name
|
||
|
d.InterfaceIndex = idx
|
||
|
return d, nil
|
||
10 months ago
|
}
|