2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-15 20:00:20 +00:00
|
|
|
|
2024-04-28 05:18:18 +01:00
|
|
|
package netmon
|
2021-03-15 20:00:20 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-07 01:11:46 +00:00
|
|
|
"io"
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2021-03-15 20:00:20 +00:00
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go4.org/mem"
|
2024-11-05 04:49:40 +00:00
|
|
|
"tailscale.com/util/lineiter"
|
2021-03-15 20:00:20 +00:00
|
|
|
"tailscale.com/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLikelyHomeRouterIPSyscallExec(t *testing.T) {
|
2023-12-19 21:27:52 +00:00
|
|
|
syscallIP, _, syscallOK := likelyHomeRouterIPBSDFetchRIB()
|
2022-10-14 16:16:55 +01:00
|
|
|
netstatIP, netstatIf, netstatOK := likelyHomeRouterIPDarwinExec()
|
|
|
|
|
2021-03-15 20:00:20 +00:00
|
|
|
if syscallOK != netstatOK || syscallIP != netstatIP {
|
|
|
|
t.Errorf("syscall() = %v, %v, netstat = %v, %v",
|
|
|
|
syscallIP, syscallOK,
|
|
|
|
netstatIP, netstatOK,
|
|
|
|
)
|
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
|
|
|
|
if !syscallOK {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
def, err := defaultRoute()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("defaultRoute() error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if def.InterfaceName != netstatIf {
|
|
|
|
t.Errorf("syscall default route interface %s differs from netstat %s", def.InterfaceName, netstatIf)
|
|
|
|
}
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-10-14 16:16:55 +01:00
|
|
|
Parse out 10.0.0.1 and en0 from:
|
2021-03-15 20:00:20 +00:00
|
|
|
|
|
|
|
$ netstat -r -n -f inet
|
|
|
|
Routing tables
|
|
|
|
|
|
|
|
Internet:
|
|
|
|
Destination Gateway Flags Netif Expire
|
|
|
|
default 10.0.0.1 UGSc en0
|
|
|
|
default link#14 UCSI utun2
|
|
|
|
10/16 link#4 UCS en0 !
|
|
|
|
10.0.0.1/32 link#4 UCS en0 !
|
|
|
|
...
|
|
|
|
*/
|
2022-10-14 16:16:55 +01:00
|
|
|
func likelyHomeRouterIPDarwinExec() (ret netip.Addr, netif string, ok bool) {
|
2021-03-15 20:00:20 +00:00
|
|
|
if version.IsMobile() {
|
|
|
|
// Don't try to do subprocesses on iOS. Ends up with log spam like:
|
|
|
|
// kernel: "Sandbox: IPNExtension(86580) deny(1) process-fork"
|
|
|
|
// This is why we have likelyHomeRouterIPDarwinSyscall.
|
2022-10-14 16:16:55 +01:00
|
|
|
return ret, "", false
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command("/usr/sbin/netstat", "-r", "-n", "-f", "inet")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cmd.Wait()
|
2024-03-07 01:11:46 +00:00
|
|
|
defer io.Copy(io.Discard, stdout) // clear the pipe to prevent hangs
|
2021-03-15 20:00:20 +00:00
|
|
|
|
|
|
|
var f []mem.RO
|
2024-11-05 04:49:40 +00:00
|
|
|
for lr := range lineiter.Reader(stdout) {
|
|
|
|
lineb, err := lr.Value()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-03-15 20:00:20 +00:00
|
|
|
line := mem.B(lineb)
|
|
|
|
if !mem.Contains(line, mem.S("default")) {
|
2024-11-05 04:49:40 +00:00
|
|
|
continue
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
|
|
|
f = mem.AppendFields(f[:0], line)
|
2022-10-14 16:16:55 +01:00
|
|
|
if len(f) < 4 || !f[0].EqualString("default") {
|
2024-11-05 04:49:40 +00:00
|
|
|
continue
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
ipm, flagsm, netifm := f[1], f[2], f[3]
|
2021-03-15 20:00:20 +00:00
|
|
|
if !mem.Contains(flagsm, mem.S("G")) {
|
2024-11-05 04:49:40 +00:00
|
|
|
continue
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
if mem.Contains(flagsm, mem.S("I")) {
|
2024-11-05 04:49:40 +00:00
|
|
|
continue
|
2022-10-14 16:16:55 +01:00
|
|
|
}
|
2022-07-26 04:55:44 +01:00
|
|
|
ip, err := netip.ParseAddr(string(mem.Append(nil, ipm)))
|
2021-07-27 03:51:18 +01:00
|
|
|
if err == nil && ip.IsPrivate() {
|
2021-03-15 20:00:20 +00:00
|
|
|
ret = ip
|
2022-10-14 16:16:55 +01:00
|
|
|
netif = netifm.StringCopy()
|
2021-03-15 20:00:20 +00:00
|
|
|
// We've found what we're looking for.
|
2024-11-05 04:49:40 +00:00
|
|
|
break
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
2024-11-05 04:49:40 +00:00
|
|
|
}
|
2022-10-14 16:16:55 +01:00
|
|
|
return ret, netif, ret.IsValid()
|
2021-03-15 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 21:23:19 +01:00
|
|
|
func TestFetchRoutingTable(t *testing.T) {
|
|
|
|
// Issue 1345: this used to be flaky on darwin.
|
2024-04-16 21:15:13 +01:00
|
|
|
for range 20 {
|
2021-04-23 21:23:19 +01:00
|
|
|
_, err := fetchRoutingTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|