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.
154 lines
4.1 KiB
Go
154 lines
4.1 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
3 years ago
|
|
||
2 years ago
|
// Common code for FreeBSD and Darwin. This might also work on other
|
||
|
// BSD systems (e.g. OpenBSD) but has not been tested.
|
||
3 years ago
|
|
||
2 years ago
|
//go:build darwin || freebsd
|
||
3 years ago
|
|
||
7 months ago
|
package netmon
|
||
3 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"log"
|
||
2 years ago
|
"net/netip"
|
||
2 years ago
|
"syscall"
|
||
3 years ago
|
|
||
|
"golang.org/x/net/route"
|
||
|
"golang.org/x/sys/unix"
|
||
2 years ago
|
"tailscale.com/net/netaddr"
|
||
3 years ago
|
)
|
||
|
|
||
2 years ago
|
// ErrNoGatewayIndexFound is returned by DefaultRouteInterfaceIndex when no
|
||
|
// default route is found.
|
||
|
var ErrNoGatewayIndexFound = errors.New("no gateway index found")
|
||
|
|
||
2 years ago
|
// DefaultRouteInterfaceIndex returns the index of the network interface that
|
||
|
// owns the default route. It returns the first IPv4 or IPv6 default route it
|
||
|
// finds (it does not prefer one or the other).
|
||
3 years ago
|
func DefaultRouteInterfaceIndex() (int, error) {
|
||
|
// $ netstat -nr
|
||
|
// Routing tables
|
||
|
// Internet:
|
||
|
// Destination Gateway Flags Netif Expire
|
||
|
// default 10.0.0.1 UGSc en0 <-- want this one
|
||
|
// default 10.0.0.1 UGScI en1
|
||
|
|
||
|
// From man netstat:
|
||
|
// U RTF_UP Route usable
|
||
|
// G RTF_GATEWAY Destination requires forwarding by intermediary
|
||
|
// S RTF_STATIC Manually added
|
||
|
// c RTF_PRCLONING Protocol-specified generate new routes on use
|
||
|
// I RTF_IFSCOPE Route is associated with an interface scope
|
||
|
|
||
|
rib, err := fetchRoutingTable()
|
||
|
if err != nil {
|
||
|
return 0, fmt.Errorf("route.FetchRIB: %w", err)
|
||
|
}
|
||
2 years ago
|
msgs, err := parseRoutingTable(rib)
|
||
3 years ago
|
if err != nil {
|
||
|
return 0, fmt.Errorf("route.ParseRIB: %w", err)
|
||
|
}
|
||
|
for _, m := range msgs {
|
||
|
rm, ok := m.(*route.RouteMessage)
|
||
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
2 years ago
|
if isDefaultGateway(rm) {
|
||
2 years ago
|
if delegatedIndex, err := getDelegatedInterface(rm.Index); err == nil && delegatedIndex != 0 {
|
||
|
return delegatedIndex, nil
|
||
|
} else if err != nil {
|
||
|
log.Printf("interfaces_bsd: could not get delegated interface: %v", err)
|
||
2 years ago
|
}
|
||
2 years ago
|
return rm.Index, nil
|
||
3 years ago
|
}
|
||
|
}
|
||
2 years ago
|
return 0, ErrNoGatewayIndexFound
|
||
3 years ago
|
}
|
||
|
|
||
|
func init() {
|
||
|
likelyHomeRouterIP = likelyHomeRouterIPBSDFetchRIB
|
||
|
}
|
||
|
|
||
11 months ago
|
func likelyHomeRouterIPBSDFetchRIB() (ret, myIP netip.Addr, ok bool) {
|
||
3 years ago
|
rib, err := fetchRoutingTable()
|
||
|
if err != nil {
|
||
|
log.Printf("routerIP/FetchRIB: %v", err)
|
||
11 months ago
|
return ret, myIP, false
|
||
3 years ago
|
}
|
||
2 years ago
|
msgs, err := parseRoutingTable(rib)
|
||
3 years ago
|
if err != nil {
|
||
|
log.Printf("routerIP/ParseRIB: %v", err)
|
||
11 months ago
|
return ret, myIP, false
|
||
3 years ago
|
}
|
||
|
for _, m := range msgs {
|
||
|
rm, ok := m.(*route.RouteMessage)
|
||
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
2 years ago
|
if !isDefaultGateway(rm) {
|
||
3 years ago
|
continue
|
||
|
}
|
||
2 years ago
|
|
||
|
gw, ok := rm.Addrs[unix.RTAX_GATEWAY].(*route.Inet4Addr)
|
||
|
if !ok {
|
||
3 years ago
|
continue
|
||
|
}
|
||
11 months ago
|
// If the route entry has an interface address associated with
|
||
|
// it, then parse and return that. This is optional.
|
||
|
if len(rm.Addrs) >= unix.RTAX_IFA {
|
||
|
if addr, ok := rm.Addrs[unix.RTAX_IFA].(*route.Inet4Addr); ok {
|
||
|
myIP = netaddr.IPv4(addr.IP[0], addr.IP[1], addr.IP[2], addr.IP[3])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return netaddr.IPv4(gw.IP[0], gw.IP[1], gw.IP[2], gw.IP[3]), myIP, true
|
||
3 years ago
|
}
|
||
|
|
||
11 months ago
|
return ret, myIP, false
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
|
var v4default = [4]byte{0, 0, 0, 0}
|
||
|
var v6default = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||
|
|
||
|
func isDefaultGateway(rm *route.RouteMessage) bool {
|
||
|
if rm.Flags&unix.RTF_GATEWAY == 0 {
|
||
|
return false
|
||
|
}
|
||
|
// Defined locally because FreeBSD does not have unix.RTF_IFSCOPE.
|
||
|
const RTF_IFSCOPE = 0x1000000
|
||
|
if rm.Flags&RTF_IFSCOPE != 0 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Addrs is [RTAX_DST, RTAX_GATEWAY, RTAX_NETMASK, ...]
|
||
|
if len(rm.Addrs) <= unix.RTAX_NETMASK {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
dst := rm.Addrs[unix.RTAX_DST]
|
||
|
netmask := rm.Addrs[unix.RTAX_NETMASK]
|
||
2 years ago
|
if dst == nil || netmask == nil {
|
||
|
return false
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if dst.Family() == syscall.AF_INET && netmask.Family() == syscall.AF_INET {
|
||
|
dstAddr, dstOk := dst.(*route.Inet4Addr)
|
||
|
nmAddr, nmOk := netmask.(*route.Inet4Addr)
|
||
|
if dstOk && nmOk && dstAddr.IP == v4default && nmAddr.IP == v4default {
|
||
|
return true
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
if dst.Family() == syscall.AF_INET6 && netmask.Family() == syscall.AF_INET6 {
|
||
|
dstAddr, dstOk := dst.(*route.Inet6Addr)
|
||
|
nmAddr, nmOk := netmask.(*route.Inet6Addr)
|
||
|
if dstOk && nmOk && dstAddr.IP == v6default && nmAddr.IP == v6default {
|
||
|
return true
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
return false
|
||
|
}
|