2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-07-06 18:34:52 +01:00
|
|
|
|
|
|
|
package interfaces
|
|
|
|
|
|
|
|
import (
|
2020-08-10 20:40:00 +01:00
|
|
|
"bufio"
|
2020-07-28 17:12:04 +01:00
|
|
|
"bytes"
|
2020-08-10 20:40:00 +01:00
|
|
|
"errors"
|
2021-03-31 00:39:20 +01:00
|
|
|
"fmt"
|
2020-08-10 20:40:00 +01:00
|
|
|
"io"
|
2020-07-28 17:12:04 +01:00
|
|
|
"log"
|
2022-02-28 22:49:37 +00:00
|
|
|
"net"
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2020-08-10 20:40:00 +01:00
|
|
|
"os"
|
2020-07-28 17:12:04 +01:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2020-08-10 20:40:00 +01:00
|
|
|
"strings"
|
2022-08-04 05:51:02 +01:00
|
|
|
"sync/atomic"
|
2020-07-28 17:12:04 +01:00
|
|
|
|
2022-02-28 22:49:37 +00:00
|
|
|
"github.com/jsimonetti/rtnetlink"
|
|
|
|
"github.com/mdlayher/netlink"
|
2020-07-06 18:34:52 +01:00
|
|
|
"go4.org/mem"
|
2022-02-28 22:49:37 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2022-07-25 04:08:42 +01:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-07-06 18:34:52 +01:00
|
|
|
"tailscale.com/util/lineread"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
likelyHomeRouterIP = likelyHomeRouterIPLinux
|
|
|
|
}
|
|
|
|
|
2022-08-04 05:51:02 +01:00
|
|
|
var procNetRouteErr atomic.Bool
|
2020-07-28 17:12:04 +01:00
|
|
|
|
2022-02-03 17:07:40 +00:00
|
|
|
// errStopReading is a sentinel error value used internally by
|
|
|
|
// lineread.File callers to stop reading. It doesn't escape to
|
|
|
|
// callers/users.
|
|
|
|
var errStopReading = errors.New("stop reading")
|
|
|
|
|
2020-07-06 18:34:52 +01:00
|
|
|
/*
|
|
|
|
Parse 10.0.0.1 out of:
|
|
|
|
|
|
|
|
$ cat /proc/net/route
|
|
|
|
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
|
|
|
ens18 00000000 0100000A 0003 0 0 0 00000000 0 0 0
|
|
|
|
ens18 0000000A 00000000 0001 0 0 0 0000FFFF 0 0 0
|
|
|
|
*/
|
2023-12-19 21:27:52 +00:00
|
|
|
func likelyHomeRouterIPLinux() (ret netip.Addr, myIP netip.Addr, ok bool) {
|
2022-08-04 05:51:02 +01:00
|
|
|
if procNetRouteErr.Load() {
|
2020-07-28 17:12:04 +01:00
|
|
|
// If we failed to read /proc/net/route previously, don't keep trying.
|
|
|
|
// But if we're on Android, go into the Android path.
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
return likelyHomeRouterIPAndroid()
|
|
|
|
}
|
2023-12-19 21:27:52 +00:00
|
|
|
return ret, myIP, false
|
2020-07-28 17:12:04 +01:00
|
|
|
}
|
2020-07-06 18:34:52 +01:00
|
|
|
lineNum := 0
|
|
|
|
var f []mem.RO
|
2022-02-03 17:07:40 +00:00
|
|
|
err := lineread.File(procNetRoutePath, func(line []byte) error {
|
2020-07-06 18:34:52 +01:00
|
|
|
lineNum++
|
|
|
|
if lineNum == 1 {
|
|
|
|
// Skip header line.
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-03 17:07:40 +00:00
|
|
|
if lineNum > maxProcNetRouteRead {
|
|
|
|
return errStopReading
|
|
|
|
}
|
2020-07-06 18:34:52 +01:00
|
|
|
f = mem.AppendFields(f[:0], mem.B(line))
|
|
|
|
if len(f) < 4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gwHex, flagsHex := f[2], f[3]
|
|
|
|
flags, err := mem.ParseUint(flagsHex, 16, 16)
|
|
|
|
if err != nil {
|
|
|
|
return nil // ignore error, skip line and keep going
|
|
|
|
}
|
2022-02-28 22:49:37 +00:00
|
|
|
if flags&(unix.RTF_UP|unix.RTF_GATEWAY) != unix.RTF_UP|unix.RTF_GATEWAY {
|
2020-07-06 18:34:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ipu32, err := mem.ParseUint(gwHex, 16, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil // ignore error, skip line and keep going
|
|
|
|
}
|
|
|
|
ip := netaddr.IPv4(byte(ipu32), byte(ipu32>>8), byte(ipu32>>16), byte(ipu32>>24))
|
2021-07-27 03:51:18 +01:00
|
|
|
if ip.IsPrivate() {
|
2020-07-06 18:34:52 +01:00
|
|
|
ret = ip
|
2022-02-03 17:07:40 +00:00
|
|
|
return errStopReading
|
2020-07-06 18:34:52 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-03 17:07:40 +00:00
|
|
|
if errors.Is(err, errStopReading) {
|
|
|
|
err = nil
|
|
|
|
}
|
2020-07-28 17:12:04 +01:00
|
|
|
if err != nil {
|
2022-08-04 05:51:02 +01:00
|
|
|
procNetRouteErr.Store(true)
|
2020-07-28 17:12:04 +01:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
return likelyHomeRouterIPAndroid()
|
|
|
|
}
|
|
|
|
log.Printf("interfaces: failed to read /proc/net/route: %v", err)
|
|
|
|
}
|
2023-04-17 17:40:37 +01:00
|
|
|
if ret.IsValid() {
|
2023-12-19 21:27:52 +00:00
|
|
|
// Try to get the local IP of the interface associated with
|
|
|
|
// this route to short-circuit finding the IP associated with
|
|
|
|
// this gateway. This isn't fatal if it fails.
|
|
|
|
if len(f) > 0 && !disableLikelyHomeRouterIPSelf() {
|
|
|
|
ForeachInterface(func(ni Interface, pfxs []netip.Prefix) {
|
|
|
|
// Ensure this is the same interface
|
|
|
|
if !f[0].EqualString(ni.Name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first IPv4 address and use it.
|
|
|
|
for _, pfx := range pfxs {
|
|
|
|
if addr := pfx.Addr(); addr.Is4() {
|
|
|
|
myIP = addr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, myIP, true
|
2023-04-17 17:40:37 +01:00
|
|
|
}
|
|
|
|
if lineNum >= maxProcNetRouteRead {
|
|
|
|
// If we went over our line limit without finding an answer, assume
|
|
|
|
// we're a big fancy Linux router (or at least not a home system)
|
|
|
|
// and set the error bit so we stop trying this in the future (and wasting CPU).
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/7621.
|
|
|
|
//
|
|
|
|
// Remember that "likelyHomeRouterIP" exists purely to find the port
|
|
|
|
// mapping service (UPnP, PMP, PCP) often present on a home router. If we hit
|
|
|
|
// the route (line) limit without finding an answer, we're unlikely to ever
|
|
|
|
// find one in the future.
|
|
|
|
procNetRouteErr.Store(true)
|
|
|
|
}
|
2023-12-19 21:27:52 +00:00
|
|
|
return netip.Addr{}, netip.Addr{}, false
|
2020-07-28 17:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Android apps don't have permission to read /proc/net/route, at
|
|
|
|
// least on Google devices and the Android emulator.
|
2023-12-19 21:27:52 +00:00
|
|
|
func likelyHomeRouterIPAndroid() (ret netip.Addr, _ netip.Addr, ok bool) {
|
2020-07-28 17:12:04 +01:00
|
|
|
cmd := exec.Command("/system/bin/ip", "route", "show", "table", "0")
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Printf("interfaces: running /system/bin/ip: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Search for line like "default via 10.0.2.2 dev radio0 table 1016 proto static mtu 1500 "
|
|
|
|
lineread.Reader(out, func(line []byte) error {
|
|
|
|
const pfx = "default via "
|
|
|
|
if !mem.HasPrefix(mem.B(line), mem.S(pfx)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
line = line[len(pfx):]
|
|
|
|
sp := bytes.IndexByte(line, ' ')
|
|
|
|
if sp == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ipb := line[:sp]
|
2022-07-26 04:55:44 +01:00
|
|
|
if ip, err := netip.ParseAddr(string(ipb)); err == nil && ip.Is4() {
|
2020-07-28 17:12:04 +01:00
|
|
|
ret = ip
|
|
|
|
log.Printf("interfaces: found Android default route %v", ip)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
2023-12-19 21:27:52 +00:00
|
|
|
return ret, netip.Addr{}, ret.IsValid()
|
2020-07-06 18:34:52 +01:00
|
|
|
}
|
2020-08-10 20:40:00 +01:00
|
|
|
|
2021-12-29 18:37:33 +00:00
|
|
|
func defaultRoute() (d DefaultRouteDetails, err error) {
|
2020-08-10 20:40:00 +01:00
|
|
|
v, err := defaultRouteInterfaceProcNet()
|
|
|
|
if err == nil {
|
2021-12-29 18:37:33 +00:00
|
|
|
d.InterfaceName = v
|
|
|
|
return d, nil
|
2020-08-10 20:40:00 +01:00
|
|
|
}
|
|
|
|
if runtime.GOOS == "android" {
|
2021-12-29 18:37:33 +00:00
|
|
|
v, err = defaultRouteInterfaceAndroidIPRoute()
|
|
|
|
d.InterfaceName = v
|
|
|
|
return d, err
|
2020-08-10 20:40:00 +01:00
|
|
|
}
|
2022-02-28 22:49:37 +00:00
|
|
|
// Issue 4038: the default route (such as on Unifi UDM Pro)
|
|
|
|
// might be in a non-default table, so it won't show up in
|
|
|
|
// /proc/net/route. Use netlink to find the default route.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): this allocates a fair bit. We should track
|
2023-04-18 22:26:58 +01:00
|
|
|
// this in net/interfaces/monitor instead and have
|
|
|
|
// interfaces.GetState take a netmon.Monitor or similar so the
|
2022-02-28 22:49:37 +00:00
|
|
|
// routing table can be cached and the monitor's existing
|
|
|
|
// subscription to route changes can update the cached state,
|
|
|
|
// rather than querying the whole thing every time like
|
|
|
|
// defaultRouteFromNetlink does.
|
|
|
|
//
|
|
|
|
// Then we should just always try to use the cached route
|
|
|
|
// table from netlink every time, and only use /proc/net/route
|
|
|
|
// as a fallback for weird environments where netlink might be
|
|
|
|
// banned but /proc/net/route is emulated (e.g. stuff like
|
|
|
|
// Cloud Run?).
|
|
|
|
return defaultRouteFromNetlink()
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultRouteFromNetlink() (d DefaultRouteDetails, err error) {
|
|
|
|
c, err := rtnetlink.Dial(&netlink.Config{Strict: true})
|
|
|
|
if err != nil {
|
|
|
|
return d, fmt.Errorf("defaultRouteFromNetlink: Dial: %w", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
rms, err := c.Route.List()
|
|
|
|
if err != nil {
|
|
|
|
return d, fmt.Errorf("defaultRouteFromNetlink: List: %w", err)
|
|
|
|
}
|
|
|
|
for _, rm := range rms {
|
|
|
|
if rm.Attributes.Gateway == nil {
|
|
|
|
// A default route has a gateway. If it doesn't, skip it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rm.Attributes.Dst != nil {
|
|
|
|
// A default route has a nil destination to mean anything
|
|
|
|
// so ignore any route for a specific destination.
|
|
|
|
// TODO(bradfitz): better heuristic?
|
|
|
|
// empirically this seems like enough.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO(bradfitz): care about address family, if
|
|
|
|
// callers ever start caring about v4-vs-v6 default
|
|
|
|
// route differences.
|
|
|
|
idx := int(rm.Attributes.OutIface)
|
|
|
|
if idx == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if iface, err := net.InterfaceByIndex(idx); err == nil {
|
|
|
|
d.InterfaceName = iface.Name
|
|
|
|
d.InterfaceIndex = idx
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d, errNoDefaultRoute
|
2020-08-10 20:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var zeroRouteBytes = []byte("00000000")
|
2021-03-25 19:24:01 +00:00
|
|
|
var procNetRoutePath = "/proc/net/route"
|
2020-08-10 20:40:00 +01:00
|
|
|
|
2022-02-03 17:07:40 +00:00
|
|
|
// maxProcNetRouteRead is the max number of lines to read from
|
|
|
|
// /proc/net/route looking for a default route.
|
|
|
|
const maxProcNetRouteRead = 1000
|
|
|
|
|
2022-02-28 22:49:37 +00:00
|
|
|
var errNoDefaultRoute = errors.New("no default route found")
|
|
|
|
|
2021-03-25 19:24:01 +00:00
|
|
|
func defaultRouteInterfaceProcNetInternal(bufsize int) (string, error) {
|
|
|
|
f, err := os.Open(procNetRoutePath)
|
2020-08-10 20:40:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2021-03-25 19:24:01 +00:00
|
|
|
|
|
|
|
br := bufio.NewReaderSize(f, bufsize)
|
2022-02-03 17:07:40 +00:00
|
|
|
lineNum := 0
|
2020-08-10 20:40:00 +01:00
|
|
|
for {
|
2022-02-03 17:07:40 +00:00
|
|
|
lineNum++
|
2020-08-10 20:40:00 +01:00
|
|
|
line, err := br.ReadSlice('\n')
|
2022-02-03 17:07:40 +00:00
|
|
|
if err == io.EOF || lineNum > maxProcNetRouteRead {
|
2022-02-28 22:49:37 +00:00
|
|
|
return "", errNoDefaultRoute
|
2020-08-10 20:40:00 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !bytes.Contains(line, zeroRouteBytes) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(string(line))
|
|
|
|
ifc := fields[0]
|
|
|
|
ip := fields[1]
|
|
|
|
netmask := fields[7]
|
|
|
|
|
|
|
|
if strings.HasPrefix(ifc, "tailscale") ||
|
|
|
|
strings.HasPrefix(ifc, "wg") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ip == "00000000" && netmask == "00000000" {
|
|
|
|
// default route
|
|
|
|
return ifc, nil // interface name
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 19:24:01 +00:00
|
|
|
}
|
2020-08-10 20:40:00 +01:00
|
|
|
|
2021-03-31 00:39:20 +01:00
|
|
|
// returns string interface name and an error.
|
|
|
|
// io.EOF: full route table processed, no default route found.
|
|
|
|
// other io error: something went wrong reading the route file.
|
2021-03-25 19:24:01 +00:00
|
|
|
func defaultRouteInterfaceProcNet() (string, error) {
|
|
|
|
rc, err := defaultRouteInterfaceProcNetInternal(128)
|
2021-03-31 00:39:20 +01:00
|
|
|
if rc == "" && (errors.Is(err, io.EOF) || err == nil) {
|
2021-03-25 19:24:01 +00:00
|
|
|
// https://github.com/google/gvisor/issues/5732
|
|
|
|
// On a regular Linux kernel you can read the first 128 bytes of /proc/net/route,
|
|
|
|
// then come back later to read the next 128 bytes and so on.
|
|
|
|
//
|
|
|
|
// In Google Cloud Run, where /proc/net/route comes from gVisor, you have to
|
|
|
|
// read it all at once. If you read only the first few bytes then the second
|
|
|
|
// read returns 0 bytes no matter how much originally appeared to be in the file.
|
|
|
|
//
|
|
|
|
// At the time of this writing (Mar 2021) Google Cloud Run has eth0 and eth1
|
|
|
|
// with a 384 byte /proc/net/route. We allocate a large buffer to ensure we'll
|
|
|
|
// read it all in one call.
|
|
|
|
return defaultRouteInterfaceProcNetInternal(4096)
|
|
|
|
}
|
|
|
|
return rc, err
|
2020-08-10 20:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultRouteInterfaceAndroidIPRoute tries to find the machine's default route interface name
|
|
|
|
// by parsing the "ip route" command output. We use this on Android where /proc/net/route
|
|
|
|
// can be missing entries or have locked-down permissions.
|
|
|
|
// See also comments in https://github.com/tailscale/tailscale/pull/666.
|
|
|
|
func defaultRouteInterfaceAndroidIPRoute() (ifname string, err error) {
|
|
|
|
cmd := exec.Command("/system/bin/ip", "route", "show", "table", "0")
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Printf("interfaces: running /system/bin/ip: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Search for line like "default via 10.0.2.2 dev radio0 table 1016 proto static mtu 1500 "
|
|
|
|
lineread.Reader(out, func(line []byte) error {
|
|
|
|
const pfx = "default via "
|
|
|
|
if !mem.HasPrefix(mem.B(line), mem.S(pfx)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ff := strings.Fields(string(line))
|
|
|
|
for i, v := range ff {
|
|
|
|
if i > 0 && ff[i-1] == "dev" && ifname == "" {
|
|
|
|
ifname = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
if ifname == "" {
|
|
|
|
return "", errors.New("no default routes found")
|
|
|
|
}
|
|
|
|
return ifname, nil
|
|
|
|
}
|