From 8a55d463c810e6d39b1179d09d1347e52c9167fc Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 2 Mar 2021 21:13:29 -0800 Subject: [PATCH] net/interfaces: merge darwin files for DefaultRouteInterface in sandbox DefaultRouteInterface was previously guarded by build tags such that it was only accessible to tailscaled-on-macos, but there was no reason for that. It runs fine in the sandbox and gives better default info, so merge its file into interfaces_darwin.go. Signed-off-by: Brad Fitzpatrick --- net/interfaces/interfaces_darwin.go | 66 +++++++++++++++ .../interfaces_darwin_tailscaled.go | 81 ------------------- .../interfaces_defaultrouteif_todo.go | 2 +- 3 files changed, 67 insertions(+), 82 deletions(-) delete mode 100644 net/interfaces/interfaces_darwin_tailscaled.go diff --git a/net/interfaces/interfaces_darwin.go b/net/interfaces/interfaces_darwin.go index c502255a9..9d0f0526f 100644 --- a/net/interfaces/interfaces_darwin.go +++ b/net/interfaces/interfaces_darwin.go @@ -6,9 +6,13 @@ package interfaces import ( "errors" + "fmt" + "net" "os/exec" + "syscall" "go4.org/mem" + "golang.org/x/net/route" "inet.af/netaddr" "tailscale.com/util/lineread" "tailscale.com/version" @@ -72,3 +76,65 @@ func likelyHomeRouterIPDarwinExec() (ret netaddr.IP, ok bool) { } var errStopReadingNetstatTable = errors.New("found private gateway") + +func DefaultRouteInterface() (string, error) { + idx, err := DefaultRouteInterfaceIndex() + if err != nil { + return "", err + } + iface, err := net.InterfaceByIndex(idx) + if err != nil { + return "", err + } + return iface.Name, 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 := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_DUMP2, 0) + if err != nil { + return 0, fmt.Errorf("route.FetchRIB: %w", err) + } + msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib) + 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 + } + const RTF_GATEWAY = 0x2 + const RTF_IFSCOPE = 0x1000000 + if rm.Flags&RTF_GATEWAY == 0 { + continue + } + if rm.Flags&RTF_IFSCOPE != 0 { + 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) +} diff --git a/net/interfaces/interfaces_darwin_tailscaled.go b/net/interfaces/interfaces_darwin_tailscaled.go deleted file mode 100644 index 1dd598619..000000000 --- a/net/interfaces/interfaces_darwin_tailscaled.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (c) 2020 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. - -// +build darwin,!redo,!ios -// (Exclude redo, because we don't want this code in the App Store -// version's sandbox, where it won't work, and also don't want it on -// iOS. This is just for utun-using non-sandboxed cmd/tailscaled on macOS. - -package interfaces - -import ( - "errors" - "fmt" - "net" - "syscall" - - "golang.org/x/net/route" -) - -func DefaultRouteInterface() (string, error) { - idx, err := DefaultRouteInterfaceIndex() - if err != nil { - return "", err - } - iface, err := net.InterfaceByIndex(idx) - if err != nil { - return "", err - } - return iface.Name, 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 := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_DUMP2, 0) - if err != nil { - return 0, fmt.Errorf("route.FetchRIB: %w", err) - } - msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib) - 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 - } - const RTF_GATEWAY = 0x2 - const RTF_IFSCOPE = 0x1000000 - if rm.Flags&RTF_GATEWAY == 0 { - continue - } - if rm.Flags&RTF_IFSCOPE != 0 { - 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) -} diff --git a/net/interfaces/interfaces_defaultrouteif_todo.go b/net/interfaces/interfaces_defaultrouteif_todo.go index 255543336..7e1c33af4 100644 --- a/net/interfaces/interfaces_defaultrouteif_todo.go +++ b/net/interfaces/interfaces_defaultrouteif_todo.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !linux,!windows,!darwin darwin,redo +// +build !linux,!windows,!darwin package interfaces