Files
tailscale/cmd/tta/bypass_linux.go
T
James Tucker 4ad0fa1bee 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>
2026-11-10 16:47:42 -08:00

41 lines
1.3 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"context"
"fmt"
"syscall"
"golang.org/x/sys/unix"
"tailscale.com/net/netmon"
)
// 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
// existing connection.
//
// We bind the socket to the default route's interface (typically the VM's
// 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(_ context.Context, network, address string, c syscall.RawConn) error {
ifc, err := netmon.DefaultRouteInterface()
if err != nil {
return fmt.Errorf("netmon.DefaultRouteInterface: %w", err)
}
var sockErr error
if err := c.Control(func(fd uintptr) {
sockErr = unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifc)
}); err != nil {
return err
}
if sockErr != nil {
return fmt.Errorf("setting SO_BINDTODEVICE on %q: %w", ifc, sockErr)
}
return nil
}