summaryrefslogtreecommitdiffhomepage
path: root/net/netns/netns_linux.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@tailscale.com>2020-08-10 12:40:00 -0700
committerBrad Fitzpatrick <bradfitz@tailscale.com>2020-08-10 13:01:49 -0700
commitc1024a5de2fc6695bc2acf1b3444ae6deda3fa2e (patch)
treeee151fa8d8e63c92404fc7feb0718dc85b87371d /net/netns/netns_linux.go
parentd65e2632ab1940b9bae9fa14b012cf79161b9cbc (diff)
downloadtailscale-bradfitz/linux_default_route_interface.tar.xz
tailscale-bradfitz/linux_default_route_interface.zip
net/netns, net/interfaces: move defaultRouteInterface, add Android fallbackbradfitz/linux_default_route_interface
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Diffstat (limited to 'net/netns/netns_linux.go')
-rw-r--r--net/netns/netns_linux.go49
1 files changed, 2 insertions, 47 deletions
diff --git a/net/netns/netns_linux.go b/net/netns/netns_linux.go
index 5a607066b..d24b5adfc 100644
--- a/net/netns/netns_linux.go
+++ b/net/netns/netns_linux.go
@@ -5,19 +5,15 @@
package netns
import (
- "bufio"
- "bytes"
- "errors"
"flag"
"fmt"
- "io"
"os"
"os/exec"
- "strings"
"sync"
"syscall"
"golang.org/x/sys/unix"
+ "tailscale.com/net/interfaces"
)
// tailscaleBypassMark is the mark indicating that packets originating
@@ -43,47 +39,6 @@ func ipRuleAvailable() bool {
return ipRuleOnce.v
}
-var zeroRouteBytes = []byte("00000000")
-
-// defaultRouteInterface returns the name of the network interface that owns
-// the default route, not including any tailscale interfaces. We only use
-// this in SO_BINDTODEVICE mode.
-func defaultRouteInterface() (string, error) {
- f, err := os.Open("/proc/net/route")
- if err != nil {
- return "", err
- }
- defer f.Close()
- br := bufio.NewReaderSize(f, 128)
- for {
- line, err := br.ReadSlice('\n')
- if err == io.EOF {
- break
- }
- 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
- }
- }
-
- return "", errors.New("no default routes found")
-}
-
// ignoreErrors returns true if we should ignore setsocketopt errors in
// this instance.
func ignoreErrors() bool {
@@ -133,7 +88,7 @@ func setBypassMark(fd uintptr) error {
}
func bindToDevice(fd uintptr) error {
- ifc, err := defaultRouteInterface()
+ ifc, err := interfaces.DefaultRouteInterface()
if err != nil {
// Make sure we bind to *some* interface,
// or we could get a routing loop.