2020-09-10 19:40:02 +01:00
|
|
|
// Copyright (c) 2020 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.
|
|
|
|
|
|
|
|
package netns
|
|
|
|
|
|
|
|
import (
|
2020-11-15 03:52:03 +00:00
|
|
|
"math/bits"
|
2020-10-13 23:22:08 +01:00
|
|
|
"strings"
|
2020-09-10 19:40:02 +01:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2020-09-26 03:11:05 +01:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2020-09-10 19:40:02 +01:00
|
|
|
"tailscale.com/net/interfaces"
|
2020-11-15 03:52:03 +00:00
|
|
|
"tailscale.com/util/endian"
|
2020-09-10 19:40:02 +01:00
|
|
|
)
|
|
|
|
|
2020-09-26 03:11:05 +01:00
|
|
|
func interfaceIndex(iface *winipcfg.IPAdapterAddresses) uint32 {
|
2020-09-22 22:20:01 +01: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 03:11:05 +01:00
|
|
|
return iface.IfIndex
|
2020-09-22 22:20:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 19:40:02 +01:00
|
|
|
// control binds c to the Windows interface that holds a default
|
|
|
|
// route, and is not the Tailscale WinTun interface.
|
|
|
|
func control(network, address string, c syscall.RawConn) error {
|
2020-10-13 23:22:08 +01:00
|
|
|
if strings.HasPrefix(address, "127.") {
|
|
|
|
// 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
|
|
|
|
}
|
2020-09-10 19:40:02 +01:00
|
|
|
canV4, canV6 := false, false
|
|
|
|
switch network {
|
|
|
|
case "tcp", "udp":
|
|
|
|
canV4, canV6 = true, true
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
canV4 = true
|
|
|
|
case "tcp6", "udp6":
|
|
|
|
canV6 = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if canV4 {
|
2020-09-26 03:11:05 +01:00
|
|
|
iface, err := interfaces.GetWindowsDefault(windows.AF_INET)
|
2020-09-10 19:40:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 22:20:01 +01:00
|
|
|
if err := bindSocket4(c, interfaceIndex(iface)); err != nil {
|
2020-09-10 19:40:02 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if canV6 {
|
2020-09-26 03:11:05 +01:00
|
|
|
iface, err := interfaces.GetWindowsDefault(windows.AF_INET6)
|
2020-09-10 19:40:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 22:20:01 +01:00
|
|
|
if err := bindSocket6(c, interfaceIndex(iface)); err != nil {
|
2020-09-10 19:40:02 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-22 20:51:43 +01: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 19:40:02 +01: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 20:51:43 +01: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 19:40:02 +01:00
|
|
|
var controlErr error
|
|
|
|
err := c.Control(func(fd uintptr) {
|
2020-09-22 20:51:43 +01:00
|
|
|
controlErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, sockoptBoundInterface, int(indexAsAddr))
|
2020-09-10 19:40:02 +01: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 {
|
2020-11-15 03:52:03 +00:00
|
|
|
if endian.Big {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return bits.ReverseBytes32(i)
|
2020-09-10 19:40:02 +01:00
|
|
|
}
|