2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-03-08 20:44:46 +00:00
|
|
|
|
2022-10-14 16:16:55 +01:00
|
|
|
// Common code for FreeBSD and Darwin. This might also work on other
|
|
|
|
// BSD systems (e.g. OpenBSD) but has not been tested.
|
2022-03-08 20:44:46 +00:00
|
|
|
|
2022-10-14 09:29:34 +01:00
|
|
|
//go:build darwin || freebsd
|
2022-03-08 20:44:46 +00:00
|
|
|
|
2024-04-28 05:18:18 +01:00
|
|
|
package netmon
|
2022-03-08 20:44:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
"net/netip"
|
2022-10-14 16:16:55 +01:00
|
|
|
"syscall"
|
2022-03-08 20:44:46 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/route"
|
|
|
|
"golang.org/x/sys/unix"
|
2022-07-25 04:08:42 +01:00
|
|
|
"tailscale.com/net/netaddr"
|
2022-03-08 20:44:46 +00:00
|
|
|
)
|
|
|
|
|
2023-04-18 17:44:59 +01:00
|
|
|
// ErrNoGatewayIndexFound is returned by DefaultRouteInterfaceIndex when no
|
|
|
|
// default route is found.
|
|
|
|
var ErrNoGatewayIndexFound = errors.New("no gateway index found")
|
|
|
|
|
2022-11-30 01:54:45 +00:00
|
|
|
// 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).
|
2022-03-08 20:44:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-10-14 09:29:34 +01:00
|
|
|
msgs, err := parseRoutingTable(rib)
|
2022-03-08 20:44:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("route.ParseRIB: %w", err)
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
rm, ok := m.(*route.RouteMessage)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
if isDefaultGateway(rm) {
|
2023-02-10 23:02:12 +00:00
|
|
|
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)
|
2023-02-08 18:30:06 +00:00
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
return rm.Index, nil
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-18 17:44:59 +01:00
|
|
|
return 0, ErrNoGatewayIndexFound
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
likelyHomeRouterIP = likelyHomeRouterIPBSDFetchRIB
|
|
|
|
}
|
|
|
|
|
2023-12-19 21:27:52 +00:00
|
|
|
func likelyHomeRouterIPBSDFetchRIB() (ret, myIP netip.Addr, ok bool) {
|
2022-03-08 20:44:46 +00:00
|
|
|
rib, err := fetchRoutingTable()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("routerIP/FetchRIB: %v", err)
|
2023-12-19 21:27:52 +00:00
|
|
|
return ret, myIP, false
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
2022-10-14 09:29:34 +01:00
|
|
|
msgs, err := parseRoutingTable(rib)
|
2022-03-08 20:44:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("routerIP/ParseRIB: %v", err)
|
2023-12-19 21:27:52 +00:00
|
|
|
return ret, myIP, false
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
rm, ok := m.(*route.RouteMessage)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
if !isDefaultGateway(rm) {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
|
|
|
|
gw, ok := rm.Addrs[unix.RTAX_GATEWAY].(*route.Inet4Addr)
|
|
|
|
if !ok {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-12-19 21:27:52 +00:00
|
|
|
// 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
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 21:27:52 +00:00
|
|
|
return ret, myIP, false
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
|
|
|
|
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]
|
2022-10-26 00:23:36 +01:00
|
|
|
if dst == nil || netmask == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
|
2022-10-26 00:23:36 +01:00
|
|
|
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
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 00:23:36 +01:00
|
|
|
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
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|