mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
net,ipn,cmd: migrate dialer hooks to ControlContext
Use ControlContext for production net.Dialer socket hooks: PeerAPI and debug peer dialing, captive-portal probes, stunstamp TCP measurements, and the test agent connection. Update the PeerDialControlFunc callback signature and Apple extension implementation together with both consumers. Preserve namespace binding, socket settings, timeout values, and existing error handling. Listener Control and RawConn.Control are separate APIs and remain unchanged. Add coverage for platform hook forwarding and errors, debug and captive portal dialing, cancellation, SO_REUSEADDR, and test-agent hook selection and keepalive configuration. Cover the Apple missing-interface error and non-Linux bypass no-op in their platform-specific tests. Updates tailscale/corp#50948 Change-Id: I4fb162666724f307f2d3d0beba6ed1b0ed15bb4f Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:
1 parent
a7befef034
commit
4ad0fa1bee
17 files changed
+345
-18
No files matched your search
@@ -207,7 +207,7 @@ func tcpDial(ctx context.Context, lport *lportForTCPConn, dst netip.AddrPort) (n
|
||||
LocalAddr: &net.TCPAddr{
|
||||
Port: int(*lport),
|
||||
},
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
ControlContext: func(_ context.Context, network, address string, c syscall.RawConn) error {
|
||||
return c.Control(func(fd uintptr) {
|
||||
// we may restart faster than TIME_WAIT can clear
|
||||
opErr = setSOReuseAddr(fd)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestTCPDialReuseAddr(t *testing.T) {
|
||||
c := testTCPDial(t)
|
||||
raw, err := c.(syscall.Conn).SyscallConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var value int
|
||||
var sockErr error
|
||||
if err := raw.Control(func(fd uintptr) {
|
||||
value, sockErr = unix.GetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sockErr != nil {
|
||||
t.Fatal(sockErr)
|
||||
}
|
||||
if value != 1 {
|
||||
t.Errorf("SO_REUSEADDR = %d; want 1", value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testTCPDial(t *testing.T) net.Conn {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { ln.Close() })
|
||||
var port lportForTCPConn // let the kernel select an available local port
|
||||
c, err := tcpDial(t.Context(), &port, netip.MustParseAddrPort(ln.Addr().String()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { c.Close() })
|
||||
return c
|
||||
}
|
||||
|
||||
func TestTCPDial(t *testing.T) {
|
||||
if c := testTCPDial(t); c.LocalAddr().(*net.TCPAddr).Port == 0 {
|
||||
t.Error("dial did not bind a local port")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPDialCanceled(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
var port lportForTCPConn
|
||||
c, err := tcpDial(ctx, &port, netip.MustParseAddrPort("127.0.0.1:1"))
|
||||
if c != nil {
|
||||
c.Close()
|
||||
}
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Errorf("error = %v; want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"syscall"
|
||||
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
"tailscale.com/net/netmon"
|
||||
)
|
||||
|
||||
// bypassControlFunc is set as net.Dialer.Control so that sockets dialed by
|
||||
// bypassControlFunc is set as net.Dialer.ControlContext so that sockets dialed by
|
||||
// TTA bypass tailscaled's policy routing. Without it, sockets opened before
|
||||
// tailscaled installs an exit-node route would have their packets rerouted
|
||||
// via the exit node when the route is later installed, breaking the
|
||||
@@ -21,7 +22,7 @@ import (
|
||||
// LAN-facing NIC) rather than relying on the bypass fwmark. The fwmark
|
||||
// approach is conditional on tailscaled having configured SO_MARK-based
|
||||
// policy routing; binding to the underlying interface is unconditional.
|
||||
func bypassControlFunc(network, address string, c syscall.RawConn) error {
|
||||
func bypassControlFunc(_ context.Context, network, address string, c syscall.RawConn) error {
|
||||
ifc, err := netmon.DefaultRouteInterface()
|
||||
if err != nil {
|
||||
return fmt.Errorf("netmon.DefaultRouteInterface: %w", err)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/net/netmon"
|
||||
)
|
||||
|
||||
func TestBypassControlError(t *testing.T) {
|
||||
if _, err := netmon.DefaultRouteInterface(); err != nil {
|
||||
t.Skipf("no default interface: %v", err)
|
||||
}
|
||||
wantErr := errors.New("raw socket control failed")
|
||||
raw := &bypassTestRawConn{err: wantErr}
|
||||
if err := bypassControlFunc(t.Context(), "tcp4", "192.0.2.1:80", raw); !errors.Is(err, wantErr) {
|
||||
t.Errorf("hook error = %v; want raw socket error", err)
|
||||
}
|
||||
if raw.calls != 1 {
|
||||
t.Errorf("RawConn.Control calls = %d; want 1", raw.calls)
|
||||
}
|
||||
}
|
||||
|
||||
type bypassTestRawConn struct {
|
||||
syscall.RawConn
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (c *bypassTestRawConn) Control(func(uintptr)) error {
|
||||
c.calls++
|
||||
return c.err
|
||||
}
|
||||
@@ -5,10 +5,13 @@
|
||||
|
||||
package main
|
||||
|
||||
import "syscall"
|
||||
import (
|
||||
"context"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// bypassControlFunc is a no-op on non-Linux platforms; SO_MARK is a Linux
|
||||
// concept and exit-node routing only matters here for Linux VMs in vmtest.
|
||||
func bypassControlFunc(network, address string, c syscall.RawConn) error {
|
||||
func bypassControlFunc(_ context.Context, network, address string, c syscall.RawConn) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !linux
|
||||
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBypassControlNoop(t *testing.T) {
|
||||
if err := bypassControlFunc(t.Context(), "tcp4", "192.0.2.1:80", nil); err != nil {
|
||||
t.Fatalf("non-Linux bypass hook: %v", err)
|
||||
}
|
||||
}
|
||||
+7
-3
@@ -483,9 +483,9 @@ func resetDialCancels() {
|
||||
}
|
||||
}
|
||||
|
||||
func connect() (net.Conn, error) {
|
||||
d := net.Dialer{
|
||||
Control: bypassControlFunc,
|
||||
func driverDialer() *net.Dialer {
|
||||
return &net.Dialer{
|
||||
ControlContext: bypassControlFunc,
|
||||
KeepAliveConfig: net.KeepAliveConfig{
|
||||
Enable: true,
|
||||
Idle: 5 * time.Second,
|
||||
@@ -493,6 +493,10 @@ func connect() (net.Conn, error) {
|
||||
Count: 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func connect() (net.Conn, error) {
|
||||
d := driverDialer()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
h := registerDialCancel(cancel)
|
||||
defer func() {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDriverDialer(t *testing.T) {
|
||||
d := driverDialer()
|
||||
if d.Control != nil || d.ControlContext == nil {
|
||||
t.Fatal("driver dialer must use ControlContext exclusively")
|
||||
}
|
||||
if reflect.ValueOf(d.ControlContext).Pointer() != reflect.ValueOf(bypassControlFunc).Pointer() {
|
||||
t.Error("driver dialer did not install the platform bypass hook")
|
||||
}
|
||||
want := net.KeepAliveConfig{Enable: true, Idle: 5 * time.Second, Interval: 5 * time.Second, Count: 3}
|
||||
if d.KeepAliveConfig != want {
|
||||
t.Errorf("keepalive = %+v; want %+v", d.KeepAliveConfig, want)
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ func (h *Handler) serveDebugDialTypes(w http.ResponseWriter, r *http.Request) {
|
||||
dialer := h.b.Dialer()
|
||||
|
||||
var peerDialer net.Dialer
|
||||
peerDialer.Control = dialer.PeerDialControlFunc()
|
||||
peerDialer.ControlContext = dialer.PeerDialControlFunc()
|
||||
|
||||
// Kick off a dial with each available dialer in parallel.
|
||||
dialers := []struct {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !ts_omit_debug
|
||||
|
||||
package localapi
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/util/httpm"
|
||||
)
|
||||
|
||||
func TestServeDebugDialTypes(t *testing.T) {
|
||||
b := newTestLocalBackend(t)
|
||||
if b.Dialer().PeerDialControlFunc() != nil {
|
||||
t.Skip("requires an initialized peer interface in an Apple extension")
|
||||
}
|
||||
ln, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
host, port, err := net.SplitHostPort(ln.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
form := url.Values{"ip": {host}, "port": {port}, "network": {"tcp4"}}
|
||||
req := httptest.NewRequest(httpm.POST, "/debug-dial-types", strings.NewReader(form.Encode()))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
w := httptest.NewRecorder()
|
||||
h := &Handler{b: b, PermitWrite: true}
|
||||
h.serveDebugDialTypes(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
||||
}
|
||||
for _, name := range []string{"SystemDial", "UserDial", "PeerDial", "BareDial"} {
|
||||
want := "[" + name + "] connected=true err=<nil>"
|
||||
if !strings.Contains(w.Body.String(), want) {
|
||||
t.Errorf("missing successful %s result in %q", name, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func (d *Detector) dialContext(ctx context.Context, network, addr string) (net.C
|
||||
|
||||
dl := &net.Dialer{
|
||||
Timeout: Timeout,
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
ControlContext: func(_ context.Context, network, address string, c syscall.RawConn) error {
|
||||
return setSocketInterfaceIndex(c, ifIndex, d.logf)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package captivedetection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDialContext(t *testing.T) {
|
||||
ln, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
d := NewDetector(t.Logf)
|
||||
c, err := d.dialContext(t.Context(), "tcp4", ln.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Close()
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
if c, err := d.dialContext(ctx, "tcp4", ln.Addr().String()); !errors.Is(err, context.Canceled) {
|
||||
if c != nil {
|
||||
c.Close()
|
||||
}
|
||||
t.Errorf("canceled dial error = %v; want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package tsdial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPeerDialControlContext(t *testing.T) {
|
||||
original := peerDialControlFunc
|
||||
t.Cleanup(func() { peerDialControlFunc = original })
|
||||
for _, enabled := range []bool{false, true} {
|
||||
name := "no_hook"
|
||||
if enabled {
|
||||
name = "platform_hook"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
d := new(Dialer)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
raw := &struct{ syscall.RawConn }{}
|
||||
wantErr := errors.New("platform control failed")
|
||||
calls := 0
|
||||
peerDialControlFunc = nil
|
||||
if enabled {
|
||||
peerDialControlFunc = func(got *Dialer) func(context.Context, string, string, syscall.RawConn) error {
|
||||
if got != d {
|
||||
t.Error("factory received the wrong dialer")
|
||||
}
|
||||
return func(gotCtx context.Context, network, address string, c syscall.RawConn) error {
|
||||
calls++
|
||||
if gotCtx != ctx || network != "tcp4" || address != "100.64.0.1:80" || c != raw {
|
||||
t.Error("platform hook arguments were not forwarded")
|
||||
}
|
||||
return wantErr
|
||||
}
|
||||
}
|
||||
}
|
||||
// The debug peer dialer uses the public factory directly.
|
||||
hook := d.PeerDialControlFunc()
|
||||
if (hook != nil) != enabled {
|
||||
t.Fatalf("factory hook present = %v; want %v", hook != nil, enabled)
|
||||
}
|
||||
if hook != nil && hook(ctx, "tcp4", "100.64.0.1:80", raw) != wantErr {
|
||||
t.Error("factory hook lost platform error")
|
||||
}
|
||||
// Normal PeerAPI dialing uses the cached dialer.
|
||||
nd := d.getPeerDialer()
|
||||
if nd.Control != nil || (nd.ControlContext != nil) != enabled {
|
||||
t.Fatal("peer dialer did not use ControlContext exclusively")
|
||||
}
|
||||
if nd.ControlContext != nil && nd.ControlContext(ctx, "tcp4", "100.64.0.1:80", raw) != wantErr {
|
||||
t.Error("peer dialer hook lost platform error")
|
||||
}
|
||||
if enabled && calls != 2 {
|
||||
t.Errorf("hook calls = %d; want 2", calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
package tsdial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"syscall"
|
||||
@@ -21,7 +22,7 @@ func init() {
|
||||
peerDialControlFunc = peerDialControlFuncNetworkExtension
|
||||
}
|
||||
|
||||
func peerDialControlFuncNetworkExtension(d *Dialer) func(network, address string, c syscall.RawConn) error {
|
||||
func peerDialControlFuncNetworkExtension(d *Dialer) func(context.Context, string, string, syscall.RawConn) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
@@ -31,7 +32,7 @@ func peerDialControlFuncNetworkExtension(d *Dialer) func(network, address string
|
||||
}
|
||||
var lc net.ListenConfig
|
||||
netns.SetListenConfigInterfaceIndex(&lc, index)
|
||||
return func(network, address string, c syscall.RawConn) error {
|
||||
return func(_ context.Context, network, address string, c syscall.RawConn) error {
|
||||
if index == -1 {
|
||||
return errors.New("failed to find TUN interface to bind to")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build ts_macext && (darwin || ios)
|
||||
|
||||
package tsdial
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPeerDialControlMissingInterface(t *testing.T) {
|
||||
hook := peerDialControlFuncNetworkExtension(new(Dialer))
|
||||
if err := hook(t.Context(), "tcp4", "100.64.0.1:80", nil); err == nil || !strings.Contains(err.Error(), "failed to find TUN interface") {
|
||||
t.Errorf("hook error = %v; want missing peer interface", err)
|
||||
}
|
||||
}
|
||||
@@ -371,17 +371,17 @@ func (d *Dialer) closeSysConn(id int) {
|
||||
|
||||
// peerDialControlFunc is non-nil on platforms that require a way to
|
||||
// bind to dial out to other peers.
|
||||
var peerDialControlFunc func(*Dialer) func(network, address string, c syscall.RawConn) error
|
||||
var peerDialControlFunc func(*Dialer) func(ctx context.Context, network, address string, c syscall.RawConn) error
|
||||
|
||||
// PeerDialControlFunc returns a function
|
||||
// that can assigned to net.Dialer.Control to set sockopts or whatnot
|
||||
// that can be assigned to net.Dialer.ControlContext to set socket options
|
||||
// to make a dial escape the current platform's network sandbox.
|
||||
//
|
||||
// On many platforms the returned func will be nil.
|
||||
//
|
||||
// Notably, this is non-nil on iOS and macOS when run as a Network or
|
||||
// System Extension (the GUI variants).
|
||||
func (d *Dialer) PeerDialControlFunc() func(network, address string, c syscall.RawConn) error {
|
||||
func (d *Dialer) PeerDialControlFunc() func(ctx context.Context, network, address string, c syscall.RawConn) error {
|
||||
if peerDialControlFunc == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -747,9 +747,9 @@ func (d *Dialer) dialPeerAPI(ctx context.Context, network, addr string) (net.Con
|
||||
func (d *Dialer) getPeerDialer() *net.Dialer {
|
||||
d.peerDialerOnce.Do(func() {
|
||||
d.peerDialer = &net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: netknob.PlatformTCPKeepAlive(),
|
||||
Control: d.PeerDialControlFunc(),
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: netknob.PlatformTCPKeepAlive(),
|
||||
ControlContext: d.PeerDialControlFunc(),
|
||||
}
|
||||
})
|
||||
return d.peerDialer
|
||||
|
||||
Reference in new issue
Block a user