2022-03-08 20:44:46 +00:00
|
|
|
// Copyright (c) 2022 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.
|
|
|
|
|
2022-10-14 15:16:55 +00: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 08:29:34 +00:00
|
|
|
//go:build darwin || freebsd
|
2022-03-08 20:44:46 +00:00
|
|
|
|
|
|
|
package interfaces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
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 04:14:09 +00:00
|
|
|
"net/netip"
|
2022-10-14 15:16:55 +00:00
|
|
|
"syscall"
|
2022-03-08 20:44:46 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/route"
|
|
|
|
"golang.org/x/sys/unix"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2022-11-30 01:54:45 +00:00
|
|
|
"tailscale.com/syncs"
|
2022-03-08 20:44:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func defaultRoute() (d DefaultRouteDetails, err error) {
|
|
|
|
idx, err := DefaultRouteInterfaceIndex()
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
iface, err := net.InterfaceByIndex(idx)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
d.InterfaceName = iface.Name
|
|
|
|
d.InterfaceIndex = idx
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-30 01:54:45 +00:00
|
|
|
if f := defaultRouteInterfaceIndexFunc.Load(); f != nil {
|
|
|
|
if ifIndex := f(); ifIndex != 0 {
|
|
|
|
return ifIndex, nil
|
|
|
|
}
|
|
|
|
// Fallthrough if we can't use the alternate implementation.
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:44:46 +00:00
|
|
|
// $ 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 08:29:34 +00: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 15:16:55 +00:00
|
|
|
if isDefaultGateway(rm) {
|
|
|
|
return rm.Index, nil
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-14 15:16:55 +00:00
|
|
|
return 0, errors.New("no gateway index found")
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:54:45 +00:00
|
|
|
var defaultRouteInterfaceIndexFunc syncs.AtomicValue[func() int]
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
// SetDefaultRouteInterfaceIndexFunc allows an alternate implementation of
|
2022-11-30 01:54:45 +00:00
|
|
|
// DefaultRouteInterfaceIndex to be provided. If none is set, or if f() returns a 0
|
|
|
|
// (indicating an unknown interface index), then the default implementation (that parses
|
|
|
|
// the routing table) will be used.
|
|
|
|
func SetDefaultRouteInterfaceIndexFunc(f func() int) {
|
|
|
|
defaultRouteInterfaceIndexFunc.Store(f)
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:44:46 +00:00
|
|
|
func init() {
|
|
|
|
likelyHomeRouterIP = likelyHomeRouterIPBSDFetchRIB
|
|
|
|
}
|
|
|
|
|
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 04:14:09 +00:00
|
|
|
func likelyHomeRouterIPBSDFetchRIB() (ret netip.Addr, ok bool) {
|
2022-03-08 20:44:46 +00:00
|
|
|
rib, err := fetchRoutingTable()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("routerIP/FetchRIB: %v", err)
|
|
|
|
return ret, false
|
|
|
|
}
|
2022-10-14 08:29:34 +00:00
|
|
|
msgs, err := parseRoutingTable(rib)
|
2022-03-08 20:44:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("routerIP/ParseRIB: %v", err)
|
|
|
|
return ret, false
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
rm, ok := m.(*route.RouteMessage)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-14 15:16:55 +00:00
|
|
|
if !isDefaultGateway(rm) {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-14 15:16:55 +00:00
|
|
|
|
|
|
|
gw, ok := rm.Addrs[unix.RTAX_GATEWAY].(*route.Inet4Addr)
|
|
|
|
if !ok {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-14 15:16:55 +00:00
|
|
|
return netaddr.IPv4(gw.IP[0], gw.IP[1], gw.IP[2], gw.IP[3]), true
|
2022-03-08 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, false
|
|
|
|
}
|
2022-10-14 15:16:55 +00: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-25 23:23:36 +00:00
|
|
|
if dst == nil || netmask == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-10-14 15:16:55 +00:00
|
|
|
|
2022-10-25 23:23:36 +00: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 15:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 23:23:36 +00: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 15:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|