2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-09-10 18:40:02 +00:00
|
|
|
|
|
|
|
package netns
|
|
|
|
|
|
|
|
import (
|
2024-05-21 20:38:53 +00:00
|
|
|
"fmt"
|
2020-11-15 03:52:03 +00:00
|
|
|
"math/bits"
|
2024-05-21 20:38:53 +00:00
|
|
|
"net/netip"
|
2020-10-13 22:22:08 +00:00
|
|
|
"strings"
|
2020-09-10 18:40:02 +00:00
|
|
|
"syscall"
|
|
|
|
|
2023-02-02 15:23:13 +00:00
|
|
|
"golang.org/x/sys/cpu"
|
2020-09-10 18:40:02 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2020-09-26 02:11:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2024-05-21 20:38:53 +00:00
|
|
|
"tailscale.com/envknob"
|
2023-04-17 23:01:41 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2024-05-21 20:38:53 +00:00
|
|
|
"tailscale.com/tsconst"
|
2021-11-18 20:18:02 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-09-10 18:40:02 +00:00
|
|
|
)
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
func interfaceIndex(iface *winipcfg.IPAdapterAddresses) uint32 {
|
2020-09-22 21:20:01 +00:00
|
|
|
if iface == nil {
|
|
|
|
// The zero ifidx means "unspecified". If we end up passing zero
|
|
|
|
// to bindSocket*(), it unsets the binding and lets the socket
|
|
|
|
// behave as normal again, which is what we want if there's no
|
|
|
|
// default route we can use.
|
|
|
|
return 0
|
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
return iface.IfIndex
|
2020-09-22 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:38:53 +00:00
|
|
|
func defaultInterfaceIndex(family winipcfg.AddressFamily) (uint32, error) {
|
|
|
|
iface, err := netmon.GetWindowsDefault(family)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaceIndex(iface), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func control(logf logger.Logf, _ *netmon.Monitor) func(network, address string, c syscall.RawConn) error {
|
|
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
|
|
return controlC(logf, network, address, c)
|
|
|
|
}
|
2021-11-18 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:38:53 +00:00
|
|
|
var bindToInterfaceByRouteEnv = envknob.RegisterBool("TS_BIND_TO_INTERFACE_BY_ROUTE")
|
|
|
|
|
2021-11-18 20:18:02 +00:00
|
|
|
// controlC binds c to the Windows interface that holds a default
|
2020-09-10 18:40:02 +00:00
|
|
|
// route, and is not the Tailscale WinTun interface.
|
2024-05-21 20:38:53 +00:00
|
|
|
func controlC(logf logger.Logf, network, address string, c syscall.RawConn) (err error) {
|
|
|
|
if isLocalhost(address) {
|
2020-10-13 22:22:08 +00:00
|
|
|
// Don't bind to an interface for localhost connections,
|
|
|
|
// otherwise we get:
|
|
|
|
// connectex: The requested address is not valid in its context
|
|
|
|
// (The derphttp tests were failing)
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-21 20:38:53 +00:00
|
|
|
|
2020-09-10 18:40:02 +00:00
|
|
|
canV4, canV6 := false, false
|
|
|
|
switch network {
|
|
|
|
case "tcp", "udp":
|
|
|
|
canV4, canV6 = true, true
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
canV4 = true
|
|
|
|
case "tcp6", "udp6":
|
|
|
|
canV6 = true
|
|
|
|
}
|
|
|
|
|
2024-05-21 20:38:53 +00:00
|
|
|
var defIfaceIdxV4, defIfaceIdxV6 uint32
|
2020-09-10 18:40:02 +00:00
|
|
|
if canV4 {
|
2024-05-21 20:38:53 +00:00
|
|
|
defIfaceIdxV4, err = defaultInterfaceIndex(windows.AF_INET)
|
2020-09-10 18:40:02 +00:00
|
|
|
if err != nil {
|
2024-05-21 20:38:53 +00:00
|
|
|
return fmt.Errorf("defaultInterfaceIndex(AF_INET): %w", err)
|
2020-09-10 18:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if canV6 {
|
2024-05-21 20:38:53 +00:00
|
|
|
defIfaceIdxV6, err = defaultInterfaceIndex(windows.AF_INET6)
|
2020-09-10 18:40:02 +00:00
|
|
|
if err != nil {
|
2024-05-21 20:38:53 +00:00
|
|
|
return fmt.Errorf("defaultInterfaceIndex(AF_INET6): %w", err)
|
2020-09-10 18:40:02 +00:00
|
|
|
}
|
2024-05-21 20:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ifaceIdxV4, ifaceIdxV6 uint32
|
|
|
|
if useRoute := bindToInterfaceByRoute.Load() || bindToInterfaceByRouteEnv(); useRoute {
|
|
|
|
addr, err := parseAddress(address)
|
2024-07-31 16:37:08 +00:00
|
|
|
if err == nil {
|
|
|
|
if canV4 && (addr.Is4() || addr.Is4In6()) {
|
|
|
|
addrV4 := addr.Unmap()
|
|
|
|
ifaceIdxV4, err = getInterfaceIndex(logf, addrV4, defIfaceIdxV4)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getInterfaceIndex(%v): %w", addrV4, err)
|
|
|
|
}
|
2024-05-21 20:38:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 16:37:08 +00:00
|
|
|
if canV6 && addr.Is6() {
|
|
|
|
ifaceIdxV6, err = getInterfaceIndex(logf, addr, defIfaceIdxV6)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getInterfaceIndex(%v): %w", addr, err)
|
|
|
|
}
|
2024-05-21 20:38:53 +00:00
|
|
|
}
|
2024-07-31 16:37:08 +00:00
|
|
|
} else {
|
2024-07-31 17:45:14 +00:00
|
|
|
if err != errUnspecifiedHost {
|
|
|
|
logf("[unexpected] netns: error parsing address %q: %v", address, err)
|
|
|
|
}
|
2024-07-31 16:37:08 +00:00
|
|
|
ifaceIdxV4, ifaceIdxV6 = defIfaceIdxV4, defIfaceIdxV6
|
2024-05-21 20:38:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ifaceIdxV4, ifaceIdxV6 = defIfaceIdxV4, defIfaceIdxV6
|
|
|
|
}
|
|
|
|
|
|
|
|
if canV4 {
|
|
|
|
if err := bindSocket4(c, ifaceIdxV4); err != nil {
|
|
|
|
return fmt.Errorf("bindSocket4(%d): %w", ifaceIdxV4, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if canV6 {
|
|
|
|
if err := bindSocket6(c, ifaceIdxV6); err != nil {
|
|
|
|
return fmt.Errorf("bindSocket6(%d): %w", ifaceIdxV6, err)
|
2020-09-10 18:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-21 20:38:53 +00:00
|
|
|
func getInterfaceIndex(logf logger.Logf, addr netip.Addr, defaultIdx uint32) (idx uint32, err error) {
|
|
|
|
idx, err = interfaceIndexFor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return defaultIdx, fmt.Errorf("interfaceIndexFor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isTS, err := isTailscaleInterface(idx)
|
|
|
|
if err != nil {
|
|
|
|
return defaultIdx, fmt.Errorf("isTailscaleInterface: %w", err)
|
|
|
|
}
|
|
|
|
if isTS {
|
|
|
|
return defaultIdx, nil
|
|
|
|
}
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTailscaleInterface(ifaceIdx uint32) (bool, error) {
|
|
|
|
ifaceLUID, err := winipcfg.LUIDFromIndex(ifaceIdx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := ifaceLUID.Interface()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := iface.Type == winipcfg.IfTypePropVirtual &&
|
|
|
|
strings.Contains(iface.Description(), tsconst.WintunInterfaceDesc)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func interfaceIndexFor(addr netip.Addr) (uint32, error) {
|
|
|
|
var sockaddr winipcfg.RawSockaddrInet
|
|
|
|
if err := sockaddr.SetAddr(addr); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var idx uint32
|
|
|
|
if err := getBestInterfaceEx(&sockaddr, &idx); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:51:43 +00:00
|
|
|
// sockoptBoundInterface is the value of IP_UNICAST_IF and IPV6_UNICAST_IF.
|
|
|
|
//
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
|
|
|
|
// and https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options
|
2020-09-10 18:40:02 +00:00
|
|
|
const sockoptBoundInterface = 31
|
|
|
|
|
|
|
|
// bindSocket4 binds the given RawConn to the network interface with
|
|
|
|
// index ifidx, for IPv4 traffic only.
|
|
|
|
func bindSocket4(c syscall.RawConn, ifidx uint32) error {
|
2020-09-22 19:51:43 +00:00
|
|
|
// For IPv4 (but NOT IPv6) the interface index must be passed
|
|
|
|
// as a big-endian integer (regardless of platform endianness)
|
|
|
|
// because the underlying sockopt takes either an IPv4 address
|
|
|
|
// or an index shoved into IPv4 address representation (an IP
|
|
|
|
// in 0.0.0.0/8 means it's actually an index).
|
|
|
|
//
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
|
|
|
|
// and IP_UNICAST_IF.
|
|
|
|
indexAsAddr := nativeToBigEndian(ifidx)
|
2020-09-10 18:40:02 +00:00
|
|
|
var controlErr error
|
|
|
|
err := c.Control(func(fd uintptr) {
|
2020-09-22 19:51:43 +00:00
|
|
|
controlErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, sockoptBoundInterface, int(indexAsAddr))
|
2020-09-10 18:40:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return controlErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// bindSocket6 binds the given RawConn to the network interface with
|
|
|
|
// index ifidx, for IPv6 traffic only.
|
|
|
|
func bindSocket6(c syscall.RawConn, ifidx uint32) error {
|
|
|
|
var controlErr error
|
|
|
|
err := c.Control(func(fd uintptr) {
|
|
|
|
controlErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, sockoptBoundInterface, int(ifidx))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return controlErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// nativeToBigEndian returns i converted into big-endian
|
|
|
|
// representation, suitable for passing to Windows APIs that require a
|
|
|
|
// mangled uint32.
|
|
|
|
func nativeToBigEndian(i uint32) uint32 {
|
2023-02-02 15:23:13 +00:00
|
|
|
if cpu.IsBigEndian {
|
2020-11-15 03:52:03 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
return bits.ReverseBytes32(i)
|
2020-09-10 18:40:02 +00:00
|
|
|
}
|