ipn/ipnlocal, net/netns: be consistent about peerapi listener bind

This pulls a bind consistency fix out of http://go/ossns/43.

The rest of the testing, including a natlab vmtest, will come later
once dependent pieces are all merged.

Updates tailscale/corp#48248

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ic05f884277d9f5b0375568bcbca553b0774550b5
This commit is contained in:
Brad Fitzpatrick committed 2026-09-22 16:31:20 +00:00
1 parent df0bd83055
commit 5cf4c53c4e
6 files changed
+153 -9

No files matched your search

+13
View File
@@ -6469,6 +6469,19 @@ func (b *LocalBackend) initPeerAPIListenerLocked() {
// We don't care about the error here. Not all platforms set this.
// If ps.listen needs it, it will check for zero values and error out.
tsIfIndex, _ := netmon.TailscaleInterfaceIndex()
if tsIfIndex == 0 {
// On Linux nothing calls netmon.SetTailscaleInterfaceProps,
// so the tun device is the source of truth for the
// interface name. peerapi uses the index to bind its
// listener to the tunnel interface there.
if tun, ok := b.sys.Tun.GetOK(); ok {
if name, err := tun.Name(); err == nil {
if iface, err := net.InterfaceByName(name); err == nil {
tsIfIndex = iface.Index
}
}
}
}
ln, err = ps.listen(a.Addr(), tsIfIndex)
if err != nil {
+9 -9
View File
@@ -44,8 +44,12 @@ import (
"tailscale.com/wgengine/filter"
)
// initListenConfig, if non-nil, is called during peerAPIListener setup. It is used only
// on iOS and macOS to set socket options to bind the listener to the Tailscale interface.
// initListenConfig, if non-nil, is called during peerAPIListener setup.
// On iOS and macOS, it sets socket options to bind the listener to the
// Tailscale interface. On Linux, it binds the listener to the tunnel
// interface, since peerapi traffic from other nodes is served by
// netstack in userspace and the kernel listener only needs to serve
// the local host.
var initListenConfig func(config *net.ListenConfig, addr netip.Addr, tunIfIndex int) error
// peerDNSQueryHandler is implemented by tsdns.Resolver.
@@ -73,13 +77,9 @@ func (s *peerAPIServer) listen(ip netip.Addr, tunIfIndex int) (ln net.Listener,
if initListenConfig != nil {
// On iOS/macOS, this sets the lc.Control hook to
// setsockopt the interface index to bind to, to get
// out of the network sandbox.
// A zero tunIfIndex is invalid for peerapi. A zero value will not get us
// out of the network sandbox. Caller should log and retry.
if tunIfIndex == 0 {
return nil, fmt.Errorf("peerapi: cannot listen on %s with tunIfIndex 0", ipStr)
}
// out of the network sandbox. On Linux, it binds the
// listener to the tunnel interface; see
// initListenConfigTun.
if err := initListenConfig(&lc, ip, tunIfIndex); err != nil {
return nil, err
+45
View File
@@ -0,0 +1,45 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux && !android
package ipnlocal
import (
"net"
"net/netip"
"tailscale.com/net/netns"
)
func init() {
initListenConfig = initListenConfigTun
}
// initListenConfigTun binds the peerapi listener to the tunnel interface
// with SO_BINDTODEVICE, for consistency with the other platforms: macOS
// and iOS bind the same listener to the tunnel interface with
// IP_BOUND_IF, and Windows is protected by its strong host model.
// Unlike those, Linux accepts a packet addressed to the node's own
// Tailscale IP from any interface it arrives on, so the socket option
// is what makes the bind consistent here.
//
// No legitimate traffic is lost. Connections to the node's Tailscale
// IP from the local host are delivered via the tunnel interface, peer
// connections arrive that way in builds without userspace netstack,
// and with netstack compiled in (the usual case) peer connections are
// terminated in userspace and never reach the kernel listener at all.
// In netstack mode there is no tunnel interface and the listener binds
// all addresses; the only connections that reach it are loopback dials
// from netstack and from the local host, so loopback is the right
// device there.
func initListenConfigTun(config *net.ListenConfig, addr netip.Addr, tunIfIndex int) error {
device := "lo"
if tunIfIndex != 0 {
iface, err := net.InterfaceByIndex(tunIfIndex)
if err == nil {
device = iface.Name
}
}
return netns.SetListenConfigInterfaceName(config, device)
}
+6
View File
@@ -6,6 +6,7 @@
package ipnlocal
import (
"fmt"
"net"
"net/netip"
@@ -20,5 +21,10 @@ func init() {
// through the iOS/macOS Network/System Extension (Packet Tunnel
// Provider) sandbox.
func initListenConfigNetworkExtension(nc *net.ListenConfig, ip netip.Addr, ifaceIndex int) error {
// A zero ifaceIndex is invalid for peerapi. A zero value will not get us
// out of the network sandbox. Caller should log and retry.
if ifaceIndex == 0 {
return fmt.Errorf("peerapi: cannot listen on %s with ifaceIndex 0", ip)
}
return netns.SetListenConfigInterfaceIndex(nc, ifaceIndex)
}
+34
View File
@@ -6,7 +6,9 @@
package netns
import (
"errors"
"fmt"
"log"
"net"
"os"
"sync"
@@ -131,3 +133,35 @@ func bindToDevice(fd uintptr) error {
}
return nil
}
// SetListenConfigInterfaceName sets lc.Control so that sockets created
// with it are bound to the named interface using SO_BINDTODEVICE, and
// thus only receive packets that arrived on that interface. It is the
// Linux counterpart of darwin's SetListenConfigInterfaceIndex.
//
// Failures to set the socket option are logged and otherwise ignored:
// the socket then simply listens as it would have without this call.
// Setting SO_BINDTODEVICE requires CAP_NET_RAW, and hardening must not
// take out the caller's listener on systems where that is unavailable.
func SetListenConfigInterfaceName(lc *net.ListenConfig, ifName string) error {
if lc == nil {
return errors.New("nil ListenConfig")
}
if lc.Control != nil {
return errors.New("ListenConfig.Control already set")
}
lc.Control = func(network, address string, c syscall.RawConn) error {
var sockErr error
err := c.Control(func(fd uintptr) {
sockErr = unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifName)
})
if sockErr != nil {
log.Printf("[unexpected] netns: SO_BINDTODEVICE(%q) on %v %v: %v", ifName, network, address, sockErr)
}
if err != nil {
return fmt.Errorf("RawConn.Control on %T: %w", c, err)
}
return nil
}
return nil
}
+46
View File
@@ -4,7 +4,12 @@
package netns
import (
"context"
"net"
"os"
"testing"
"golang.org/x/sys/unix"
)
func TestSocketMarkWorks(t *testing.T) {
@@ -12,3 +17,44 @@ func TestSocketMarkWorks(t *testing.T) {
// we cannot actually assert whether the test runner has SO_MARK available
// or not, as we don't know. We're just checking that it doesn't panic.
}
func TestSetListenConfigInterfaceName(t *testing.T) {
// Setting SO_BINDTODEVICE requires CAP_NET_RAW, which the test
// runner has when running as root and may or may not have otherwise.
if os.Geteuid() != 0 {
t.Skip("skipping; setting SO_BINDTODEVICE requires CAP_NET_RAW")
}
var lc net.ListenConfig
if err := SetListenConfigInterfaceName(&lc, "lo"); err != nil {
t.Fatal(err)
}
ln, err := lc.Listen(context.Background(), "tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
tcpLn, ok := ln.(*net.TCPListener)
if !ok {
t.Fatalf("got listener of type %T, want *net.TCPListener", ln)
}
rc, err := tcpLn.SyscallConn()
if err != nil {
t.Fatal(err)
}
var got string
var sockErr error
err = rc.Control(func(fd uintptr) {
got, sockErr = unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE)
})
if err != nil {
t.Fatalf("RawConn.Control: %v", err)
}
if sockErr != nil {
t.Fatalf("getsockopt SO_BINDTODEVICE: %v", sockErr)
}
if got != "lo" {
t.Errorf("SO_BINDTODEVICE = %q, want %q", got, "lo")
}
}