diff --git a/net/captivedetection/rawconn.go b/net/captivedetection/rawconn.go index 3c6f65f84..497bc1623 100644 --- a/net/captivedetection/rawconn.go +++ b/net/captivedetection/rawconn.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & contributors // SPDX-License-Identifier: BSD-3-Clause -//go:build !(ios || darwin) +//go:build !(ios || darwin || windows) package captivedetection @@ -13,7 +13,7 @@ import ( // setSocketInterfaceIndex sets the IP_BOUND_IF socket option on the given RawConn. // This forces the socket to use the given interface. -func setSocketInterfaceIndex(c syscall.RawConn, ifIndex int, logf logger.Logf) error { - // No-op on non-Darwin platforms. +func setSocketInterfaceIndex(syscall.RawConn, int, logger.Logf) error { + // No-op on all platforms except Darwin and Windows. return nil } diff --git a/net/captivedetection/rawconn_windows.go b/net/captivedetection/rawconn_windows.go new file mode 100644 index 000000000..37b22a7f6 --- /dev/null +++ b/net/captivedetection/rawconn_windows.go @@ -0,0 +1,17 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build windows + +package captivedetection + +import ( + "syscall" + + "tailscale.com/net/netns" + "tailscale.com/types/logger" +) + +func setSocketInterfaceIndex(c syscall.RawConn, ifIndex int, _ logger.Logf) error { + return netns.BindSocket4(c, uint32(ifIndex)) +} diff --git a/net/netns/netns_windows.go b/net/netns/netns_windows.go index f13449bd7..c2a5afd66 100644 --- a/net/netns/netns_windows.go +++ b/net/netns/netns_windows.go @@ -97,13 +97,13 @@ func controlC(logf logger.Logf, netMon *netmon.Monitor, network, address string, } if canV4 { - if err := bindSocket4(c, ifaceIdxV4); err != nil { + if err := BindSocket4(c, ifaceIdxV4); err != nil { return fmt.Errorf("bindSocket4(%d): %w", ifaceIdxV4, err) } } if canV6 { - if err := bindSocket6(c, ifaceIdxV6); err != nil { + if err := BindSocket6(c, ifaceIdxV6); err != nil { return fmt.Errorf("bindSocket6(%d): %w", ifaceIdxV6, err) } } @@ -163,9 +163,9 @@ func interfaceIndexFor(addr netip.Addr) (uint32, error) { // and https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options const sockoptBoundInterface = 31 -// bindSocket4 binds the given RawConn to the network interface with +// BindSocket4 binds the given RawConn to the network interface with // index ifidx, for IPv4 traffic only. -func bindSocket4(c syscall.RawConn, ifidx uint32) error { +func BindSocket4(c syscall.RawConn, ifidx uint32) error { // 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 @@ -185,9 +185,9 @@ func bindSocket4(c syscall.RawConn, ifidx uint32) error { return controlErr } -// bindSocket6 binds the given RawConn to the network interface with +// BindSocket6 binds the given RawConn to the network interface with // index ifidx, for IPv6 traffic only. -func bindSocket6(c syscall.RawConn, ifidx uint32) error { +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))