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 08:29:34 +00:00
|
|
|
// Common code for FreeBSD and Darwin.
|
2022-03-08 20:44:46 +00:00
|
|
|
|
2022-10-14 08:29:34 +00:00
|
|
|
//go:build darwin || freebsd
|
|
|
|
// +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-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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
}
|
|
|
|
indexSeen := map[int]int{} // index => count
|
|
|
|
for _, m := range msgs {
|
|
|
|
rm, ok := m.(*route.RouteMessage)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-14 08:29:34 +00:00
|
|
|
if rm.Flags&unix.RTF_GATEWAY == 0 {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-14 08:29:34 +00:00
|
|
|
if rm.Flags&unix.RTF_IFSCOPE != 0 {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
indexSeen[rm.Index]++
|
|
|
|
}
|
|
|
|
if len(indexSeen) == 0 {
|
|
|
|
return 0, errors.New("no gateway index found")
|
|
|
|
}
|
|
|
|
if len(indexSeen) == 1 {
|
|
|
|
for idx := range indexSeen {
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("ambiguous gateway interfaces found: %v", indexSeen)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if rm.Flags&unix.RTF_GATEWAY == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-14 08:29:34 +00:00
|
|
|
if rm.Flags&unix.RTF_IFSCOPE != 0 {
|
2022-03-08 20:44:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(rm.Addrs) > unix.RTAX_GATEWAY {
|
|
|
|
dst4, ok := rm.Addrs[unix.RTAX_DST].(*route.Inet4Addr)
|
|
|
|
if !ok || dst4.IP != ([4]byte{0, 0, 0, 0}) {
|
|
|
|
// Expect 0.0.0.0 as DST field.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gw, ok := rm.Addrs[unix.RTAX_GATEWAY].(*route.Inet4Addr)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return netaddr.IPv4(gw.IP[0], gw.IP[1], gw.IP[2], gw.IP[3]), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, false
|
|
|
|
}
|