2020-05-29 00:43:15 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-08-05 22:42:39 +00:00
|
|
|
//go:build linux && !android
|
2021-06-24 19:50:47 +00:00
|
|
|
// +build linux,!android
|
|
|
|
|
2020-05-29 00:43:15 +00:00
|
|
|
package netns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-30 04:13:00 +00:00
|
|
|
"net"
|
2020-05-30 04:58:31 +00:00
|
|
|
"os"
|
2020-05-31 08:31:01 +00:00
|
|
|
"sync"
|
2020-05-29 00:43:15 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2022-09-11 00:46:09 +00:00
|
|
|
"tailscale.com/envknob"
|
2020-08-10 19:40:00 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2021-11-18 20:18:02 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-05-29 00:43:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// tailscaleBypassMark is the mark indicating that packets originating
|
|
|
|
// from a socket should bypass Tailscale-managed routes during routing
|
|
|
|
// table lookups.
|
|
|
|
//
|
|
|
|
// Keep this in sync with tailscaleBypassMark in
|
|
|
|
// wgengine/router/router_linux.go.
|
2020-07-22 01:24:28 +00:00
|
|
|
const tailscaleBypassMark = 0x80000
|
2020-05-29 00:43:15 +00:00
|
|
|
|
2021-06-30 04:13:00 +00:00
|
|
|
// socketMarkWorksOnce is the sync.Once & cached value for useSocketMark.
|
|
|
|
var socketMarkWorksOnce struct {
|
2020-05-31 21:01:20 +00:00
|
|
|
sync.Once
|
|
|
|
v bool
|
|
|
|
}
|
2020-05-31 08:31:01 +00:00
|
|
|
|
2021-06-30 04:13:00 +00:00
|
|
|
// socketMarkWorks returns whether SO_MARK works.
|
|
|
|
func socketMarkWorks() bool {
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:1")
|
|
|
|
if err != nil {
|
|
|
|
return true // unsure, returning true does the least harm.
|
|
|
|
}
|
|
|
|
|
|
|
|
sConn, err := net.DialUDP("udp", nil, addr)
|
|
|
|
if err != nil {
|
|
|
|
return true // unsure, return true
|
|
|
|
}
|
|
|
|
defer sConn.Close()
|
|
|
|
|
|
|
|
rConn, err := sConn.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return true // unsure, return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var sockErr error
|
|
|
|
err = rConn.Control(func(fd uintptr) {
|
|
|
|
sockErr = setBypassMark(fd)
|
|
|
|
})
|
|
|
|
if err != nil || sockErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:49:39 +00:00
|
|
|
var forceBindToDevice = envknob.RegisterBool("TS_FORCE_LINUX_BIND_TO_DEVICE")
|
2022-09-11 00:46:09 +00:00
|
|
|
|
2022-09-12 21:31:20 +00:00
|
|
|
// UseSocketMark reports whether SO_MARK is in use.
|
2020-05-31 08:31:01 +00:00
|
|
|
// If it doesn't, we have to use SO_BINDTODEVICE on our sockets instead.
|
2022-09-12 21:31:20 +00:00
|
|
|
func UseSocketMark() bool {
|
2022-09-14 19:49:39 +00:00
|
|
|
if forceBindToDevice() {
|
2022-09-11 00:46:09 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-06-30 04:13:00 +00:00
|
|
|
socketMarkWorksOnce.Do(func() {
|
2022-01-11 23:26:58 +00:00
|
|
|
socketMarkWorksOnce.v = socketMarkWorks()
|
2020-05-31 21:01:20 +00:00
|
|
|
})
|
2021-06-30 04:13:00 +00:00
|
|
|
return socketMarkWorksOnce.v
|
2020-05-31 21:01:20 +00:00
|
|
|
}
|
2020-05-31 08:31:01 +00:00
|
|
|
|
|
|
|
// ignoreErrors returns true if we should ignore setsocketopt errors in
|
|
|
|
// this instance.
|
|
|
|
func ignoreErrors() bool {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
// only root can manipulate these socket flags
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-18 20:18:02 +00:00
|
|
|
func control(logger.Logf) func(network, address string, c syscall.RawConn) error {
|
|
|
|
return controlC
|
|
|
|
}
|
|
|
|
|
|
|
|
// controlC marks c as necessary to dial in a separate network namespace.
|
2020-05-29 00:43:15 +00:00
|
|
|
//
|
|
|
|
// It's intentionally the same signature as net.Dialer.Control
|
|
|
|
// and net.ListenConfig.Control.
|
2021-11-18 20:18:02 +00:00
|
|
|
func controlC(network, address string, c syscall.RawConn) error {
|
2021-06-30 04:13:00 +00:00
|
|
|
if isLocalhost(address) {
|
2021-07-09 11:40:41 +00:00
|
|
|
// Don't bind to an interface for localhost connections.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:29:54 +00:00
|
|
|
var sockErr error
|
|
|
|
err := c.Control(func(fd uintptr) {
|
2022-09-12 21:31:20 +00:00
|
|
|
if UseSocketMark() {
|
2020-05-31 21:29:54 +00:00
|
|
|
sockErr = setBypassMark(fd)
|
|
|
|
} else {
|
|
|
|
sockErr = bindToDevice(fd)
|
2020-05-30 04:58:31 +00:00
|
|
|
}
|
2020-05-31 21:29:54 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RawConn.Control on %T: %w", c, err)
|
|
|
|
}
|
|
|
|
if sockErr != nil && ignoreErrors() {
|
|
|
|
// TODO(bradfitz): maybe log once? probably too spammy for e.g. CLI tools like tailscale netcheck.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return sockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBypassMark(fd uintptr) error {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_MARK, tailscaleBypassMark); err != nil {
|
|
|
|
return fmt.Errorf("setting SO_MARK bypass: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindToDevice(fd uintptr) error {
|
2020-08-10 19:40:00 +00:00
|
|
|
ifc, err := interfaces.DefaultRouteInterface()
|
2020-05-31 21:29:54 +00:00
|
|
|
if err != nil {
|
|
|
|
// Make sure we bind to *some* interface,
|
|
|
|
// or we could get a routing loop.
|
|
|
|
// "lo" is always wrong, but if we don't have
|
|
|
|
// a default route anyway, it doesn't matter.
|
|
|
|
ifc = "lo"
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifc); err != nil {
|
|
|
|
return fmt.Errorf("setting SO_BINDTODEVICE: %w", err)
|
2020-05-29 00:43:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|