2021-02-14 15:48:38 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-11-30 01:54:45 +00:00
|
|
|
//go:build darwin
|
2021-02-14 15:48:38 +00:00
|
|
|
|
|
|
|
package netns
|
|
|
|
|
|
|
|
import (
|
2022-11-30 01:54:45 +00:00
|
|
|
"errors"
|
2021-02-14 15:48:38 +00:00
|
|
|
"fmt"
|
2022-11-30 01:54:45 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-02-14 15:48:38 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"tailscale.com/net/interfaces"
|
2021-11-18 20:18:02 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-14 15:48:38 +00:00
|
|
|
)
|
|
|
|
|
2021-11-18 20:18:02 +00:00
|
|
|
func control(logf logger.Logf) func(network, address string, c syscall.RawConn) error {
|
|
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
|
|
return controlLogf(logf, network, address, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// controlLogf marks c as necessary to dial in a separate network namespace.
|
2021-02-14 15:48:38 +00:00
|
|
|
//
|
|
|
|
// It's intentionally the same signature as net.Dialer.Control
|
|
|
|
// and net.ListenConfig.Control.
|
2021-11-18 20:18:02 +00:00
|
|
|
func controlLogf(logf logger.Logf, network, address string, c syscall.RawConn) error {
|
2022-11-30 01:54:45 +00:00
|
|
|
if isLocalhost(address) {
|
2021-02-14 15:48:38 +00:00
|
|
|
// Don't bind to an interface for localhost connections.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
idx, err := interfaces.DefaultRouteInterfaceIndex()
|
|
|
|
if err != nil {
|
2021-11-18 20:18:02 +00:00
|
|
|
logf("[unexpected] netns: DefaultRouteInterfaceIndex: %v", err)
|
2021-02-14 15:48:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-30 01:54:45 +00:00
|
|
|
|
|
|
|
return bindConnToInterface(c, network, address, idx, logf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetListenConfigInterfaceIndex sets lc.Control such that sockets are bound
|
|
|
|
// to the provided interface index.
|
|
|
|
func SetListenConfigInterfaceIndex(lc *net.ListenConfig, ifIndex int) 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 {
|
|
|
|
return bindConnToInterface(c, network, address, ifIndex, log.Printf)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindConnToInterface(c syscall.RawConn, network, address string, ifIndex int, logf logger.Logf) error {
|
2021-02-14 15:48:38 +00:00
|
|
|
v6 := strings.Contains(address, "]:") || strings.HasSuffix(network, "6") // hacky test for v6
|
|
|
|
proto := unix.IPPROTO_IP
|
|
|
|
opt := unix.IP_BOUND_IF
|
|
|
|
if v6 {
|
|
|
|
proto = unix.IPPROTO_IPV6
|
|
|
|
opt = unix.IPV6_BOUND_IF
|
|
|
|
}
|
|
|
|
|
|
|
|
var sockErr error
|
2022-11-30 01:54:45 +00:00
|
|
|
err := c.Control(func(fd uintptr) {
|
|
|
|
sockErr = unix.SetsockoptInt(int(fd), proto, opt, ifIndex)
|
2021-02-14 15:48:38 +00:00
|
|
|
})
|
2022-11-30 01:54:45 +00:00
|
|
|
if sockErr != nil {
|
|
|
|
logf("[unexpected] netns: bindConnToInterface(%q, %q), v6=%v, index=%v: %v", network, address, v6, ifIndex, sockErr)
|
|
|
|
}
|
2021-02-14 15:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RawConn.Control on %T: %w", c, err)
|
|
|
|
}
|
|
|
|
return sockErr
|
|
|
|
}
|