2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-05-28 23:27:04 +01:00
|
|
|
|
|
|
|
// Package netns contains the common code for using the Go net package
|
|
|
|
// in a logical "network namespace" to avoid routing loops where
|
|
|
|
// Tailscale-created packets would otherwise loop back through
|
|
|
|
// Tailscale routes.
|
|
|
|
//
|
|
|
|
// Despite the name netns, the exact mechanism used differs by
|
|
|
|
// operating system, and perhaps even by version of the OS.
|
2020-06-01 18:50:37 +01:00
|
|
|
//
|
|
|
|
// The netns package also handles connecting via SOCKS proxies when
|
|
|
|
// configured by the environment.
|
2020-05-28 23:27:04 +01:00
|
|
|
package netns
|
|
|
|
|
2020-06-01 18:50:37 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2022-08-04 05:51:02 +01:00
|
|
|
"sync/atomic"
|
2021-07-09 12:40:41 +01:00
|
|
|
|
2021-09-28 15:55:22 +01:00
|
|
|
"tailscale.com/net/netknob"
|
2023-04-18 00:01:41 +01:00
|
|
|
"tailscale.com/net/netmon"
|
2021-11-18 20:18:02 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-06-01 18:50:37 +01:00
|
|
|
)
|
2020-05-29 01:43:15 +01:00
|
|
|
|
2022-08-04 05:51:02 +01:00
|
|
|
var disabled atomic.Bool
|
2021-09-09 23:20:08 +01:00
|
|
|
|
2021-09-11 06:24:30 +01:00
|
|
|
// SetEnabled enables or disables netns for the process.
|
|
|
|
// It defaults to being enabled.
|
|
|
|
func SetEnabled(on bool) {
|
2022-08-04 05:51:02 +01:00
|
|
|
disabled.Store(!on)
|
2021-09-09 23:20:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-25 18:17:40 +00:00
|
|
|
var bindToInterfaceByRoute atomic.Bool
|
|
|
|
|
|
|
|
// SetBindToInterfaceByRoute enables or disables whether we use the system's
|
|
|
|
// route information to bind to a particular interface. It is the same as
|
|
|
|
// setting the TS_BIND_TO_INTERFACE_BY_ROUTE.
|
|
|
|
//
|
|
|
|
// Currently, this only changes the behaviour on macOS.
|
|
|
|
func SetBindToInterfaceByRoute(v bool) {
|
|
|
|
bindToInterfaceByRoute.Store(v)
|
|
|
|
}
|
|
|
|
|
2023-02-08 18:30:06 +00:00
|
|
|
var disableBindConnToInterface atomic.Bool
|
|
|
|
|
|
|
|
// SetDisableBindConnToInterface disables the (normal) behavior of binding
|
|
|
|
// connections to the default network interface.
|
|
|
|
//
|
|
|
|
// Currently, this only has an effect on Darwin.
|
|
|
|
func SetDisableBindConnToInterface(v bool) {
|
|
|
|
disableBindConnToInterface.Store(v)
|
|
|
|
}
|
|
|
|
|
2020-05-28 23:27:04 +01:00
|
|
|
// Listener returns a new net.Listener with its Control hook func
|
|
|
|
// initialized as necessary to run in logical network namespace that
|
|
|
|
// doesn't route back into Tailscale.
|
2023-04-18 00:01:41 +01:00
|
|
|
// The netMon parameter is optional; if non-nil it's used to do faster interface lookups.
|
|
|
|
func Listener(logf logger.Logf, netMon *netmon.Monitor) *net.ListenConfig {
|
2022-08-04 05:51:02 +01:00
|
|
|
if disabled.Load() {
|
2021-09-09 23:20:08 +01:00
|
|
|
return new(net.ListenConfig)
|
|
|
|
}
|
2023-04-18 00:01:41 +01:00
|
|
|
return &net.ListenConfig{Control: control(logf, netMon)}
|
2020-05-28 23:27:04 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 18:50:37 +01:00
|
|
|
// NewDialer returns a new Dialer using a net.Dialer with its Control
|
|
|
|
// hook func initialized as necessary to run in a logical network
|
|
|
|
// namespace that doesn't route back into Tailscale. It also handles
|
|
|
|
// using a SOCKS if configured in the environment with ALL_PROXY.
|
2023-04-18 00:01:41 +01:00
|
|
|
// The netMon parameter is optional; if non-nil it's used to do faster interface lookups.
|
|
|
|
func NewDialer(logf logger.Logf, netMon *netmon.Monitor) Dialer {
|
|
|
|
return FromDialer(logf, netMon, &net.Dialer{
|
2021-09-28 15:55:22 +01:00
|
|
|
KeepAlive: netknob.PlatformTCPKeepAlive(),
|
|
|
|
})
|
2020-06-01 18:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromDialer returns sets d.Control as necessary to run in a logical
|
|
|
|
// network namespace that doesn't route back into Tailscale. It also
|
|
|
|
// handles using a SOCKS if configured in the environment with
|
|
|
|
// ALL_PROXY.
|
2023-04-18 00:01:41 +01:00
|
|
|
// The netMon parameter is optional; if non-nil it's used to do faster interface lookups.
|
|
|
|
func FromDialer(logf logger.Logf, netMon *netmon.Monitor, d *net.Dialer) Dialer {
|
2022-08-04 05:51:02 +01:00
|
|
|
if disabled.Load() {
|
2021-09-09 23:20:08 +01:00
|
|
|
return d
|
|
|
|
}
|
2023-04-18 00:01:41 +01:00
|
|
|
d.Control = control(logf, netMon)
|
2020-06-01 18:50:37 +01:00
|
|
|
if wrapDialer != nil {
|
|
|
|
return wrapDialer(d)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSOCKSDialer reports whether d is SOCKS-proxying dialer as returned by
|
|
|
|
// NewDialer or FromDialer.
|
|
|
|
func IsSOCKSDialer(d Dialer) bool {
|
|
|
|
if d == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := d.(*net.Dialer)
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapDialer, if non-nil, specifies a function to wrap a dialer in a
|
|
|
|
// SOCKS-using dialer. It's set conditionally by socks.go.
|
|
|
|
var wrapDialer func(Dialer) Dialer
|
|
|
|
|
|
|
|
// Dialer is the interface for a dialer that can dial with or without a context.
|
|
|
|
// It's the type implemented both by net.Dialer and the Go SOCKS dialer.
|
|
|
|
type Dialer interface {
|
|
|
|
Dial(network, address string) (net.Conn, error)
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
2020-05-29 00:48:08 +01:00
|
|
|
}
|
2021-07-09 12:40:41 +01:00
|
|
|
|
|
|
|
func isLocalhost(addr string) bool {
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
// error means the string didn't contain a port number, so use the string directly
|
|
|
|
host = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// localhost6 == RedHat /etc/hosts for ::1, ip6-loopback & ip6-localhost == Debian /etc/hosts for ::1
|
|
|
|
if host == "localhost" || host == "localhost6" || host == "ip6-loopback" || host == "ip6-localhost" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:55:44 +01:00
|
|
|
ip, _ := netip.ParseAddr(host)
|
2021-07-09 12:40:41 +01:00
|
|
|
return ip.IsLoopback()
|
|
|
|
}
|