From 4ad0fa1bee574d004af26fb60f272b12b2cb2c12 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Tue, 10 Nov 2026 23:38:58 +0000 Subject: [PATCH] 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 --- cmd/stunstamp/stunstamp.go | 2 +- cmd/stunstamp/stunstamp_linux_test.go | 32 ++++++++++++ cmd/stunstamp/stunstamp_test.go | 47 +++++++++++++++++ cmd/tta/bypass_linux.go | 5 +- cmd/tta/bypass_linux_test.go | 37 ++++++++++++++ cmd/tta/bypass_other.go | 7 ++- cmd/tta/bypass_other_test.go | 14 ++++++ cmd/tta/tta.go | 10 ++-- cmd/tta/tta_test.go | 25 +++++++++ ipn/localapi/debug.go | 2 +- ipn/localapi/debug_dial_test.go | 48 ++++++++++++++++++ net/captivedetection/captivedetection.go | 2 +- net/captivedetection/dial_test.go | 33 ++++++++++++ net/tsdial/peer_control_test.go | 64 ++++++++++++++++++++++++ net/tsdial/peerapi_macios_ext.go | 5 +- net/tsdial/peerapi_macios_ext_test.go | 18 +++++++ net/tsdial/tsdial.go | 12 ++--- 17 files changed, 345 insertions(+), 18 deletions(-) create mode 100644 cmd/stunstamp/stunstamp_linux_test.go create mode 100644 cmd/stunstamp/stunstamp_test.go create mode 100644 cmd/tta/bypass_linux_test.go create mode 100644 cmd/tta/bypass_other_test.go create mode 100644 cmd/tta/tta_test.go create mode 100644 ipn/localapi/debug_dial_test.go create mode 100644 net/captivedetection/dial_test.go create mode 100644 net/tsdial/peer_control_test.go create mode 100644 net/tsdial/peerapi_macios_ext_test.go diff --git a/cmd/stunstamp/stunstamp.go b/cmd/stunstamp/stunstamp.go index 42690cfa7..32ec9e20d 100644 --- a/cmd/stunstamp/stunstamp.go +++ b/cmd/stunstamp/stunstamp.go @@ -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) diff --git a/cmd/stunstamp/stunstamp_linux_test.go b/cmd/stunstamp/stunstamp_linux_test.go new file mode 100644 index 000000000..cfd35a4fd --- /dev/null +++ b/cmd/stunstamp/stunstamp_linux_test.go @@ -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) + } +} diff --git a/cmd/stunstamp/stunstamp_test.go b/cmd/stunstamp/stunstamp_test.go new file mode 100644 index 000000000..944b715f6 --- /dev/null +++ b/cmd/stunstamp/stunstamp_test.go @@ -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) + } +} diff --git a/cmd/tta/bypass_linux.go b/cmd/tta/bypass_linux.go index 868cd716f..5375c3cab 100644 --- a/cmd/tta/bypass_linux.go +++ b/cmd/tta/bypass_linux.go @@ -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) diff --git a/cmd/tta/bypass_linux_test.go b/cmd/tta/bypass_linux_test.go new file mode 100644 index 000000000..4ff65fa14 --- /dev/null +++ b/cmd/tta/bypass_linux_test.go @@ -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 +} diff --git a/cmd/tta/bypass_other.go b/cmd/tta/bypass_other.go index e6b453f49..ce77cbe81 100644 --- a/cmd/tta/bypass_other.go +++ b/cmd/tta/bypass_other.go @@ -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 } diff --git a/cmd/tta/bypass_other_test.go b/cmd/tta/bypass_other_test.go new file mode 100644 index 000000000..8c5e8b205 --- /dev/null +++ b/cmd/tta/bypass_other_test.go @@ -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) + } +} diff --git a/cmd/tta/tta.go b/cmd/tta/tta.go index b665cb82a..86a7f5bbd 100644 --- a/cmd/tta/tta.go +++ b/cmd/tta/tta.go @@ -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() { diff --git a/cmd/tta/tta_test.go b/cmd/tta/tta_test.go new file mode 100644 index 000000000..a0d3a3e89 --- /dev/null +++ b/cmd/tta/tta_test.go @@ -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) + } +} diff --git a/ipn/localapi/debug.go b/ipn/localapi/debug.go index b2aa87fff..a6e8f81d0 100644 --- a/ipn/localapi/debug.go +++ b/ipn/localapi/debug.go @@ -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 { diff --git a/ipn/localapi/debug_dial_test.go b/ipn/localapi/debug_dial_test.go new file mode 100644 index 000000000..7fd5a00c3 --- /dev/null +++ b/ipn/localapi/debug_dial_test.go @@ -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=" + if !strings.Contains(w.Body.String(), want) { + t.Errorf("missing successful %s result in %q", name, w.Body.String()) + } + } +} diff --git a/net/captivedetection/captivedetection.go b/net/captivedetection/captivedetection.go index 024d13639..dd0950c29 100644 --- a/net/captivedetection/captivedetection.go +++ b/net/captivedetection/captivedetection.go @@ -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) }, } diff --git a/net/captivedetection/dial_test.go b/net/captivedetection/dial_test.go new file mode 100644 index 000000000..5e912ab71 --- /dev/null +++ b/net/captivedetection/dial_test.go @@ -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) + } +} diff --git a/net/tsdial/peer_control_test.go b/net/tsdial/peer_control_test.go new file mode 100644 index 000000000..36eb731c1 --- /dev/null +++ b/net/tsdial/peer_control_test.go @@ -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) + } + }) + } +} diff --git a/net/tsdial/peerapi_macios_ext.go b/net/tsdial/peerapi_macios_ext.go index fa40feef0..6c5174e45 100644 --- a/net/tsdial/peerapi_macios_ext.go +++ b/net/tsdial/peerapi_macios_ext.go @@ -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") } diff --git a/net/tsdial/peerapi_macios_ext_test.go b/net/tsdial/peerapi_macios_ext_test.go new file mode 100644 index 000000000..208d3ad85 --- /dev/null +++ b/net/tsdial/peerapi_macios_ext_test.go @@ -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) + } +} diff --git a/net/tsdial/tsdial.go b/net/tsdial/tsdial.go index f1bf0a67b..6ff82350d 100644 --- a/net/tsdial/tsdial.go +++ b/net/tsdial/tsdial.go @@ -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