private var handle: Int32?
private var networkMonitor: NWPathMonitor?
- private var lastFirstInterface: NWInterface?
+ private var ifname: String?
private var packetTunnelSettingsGenerator: PacketTunnelSettingsGenerator?
deinit {
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
} else {
self.networkMonitor = NWPathMonitor()
- self.lastFirstInterface = self.networkMonitor!.currentPath.availableInterfaces.first
self.networkMonitor!.pathUpdateHandler = self.pathUpdate
self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
return
}
+ var ifnameSize = socklen_t(IFNAMSIZ)
+ let ifnamePtr = UnsafeMutablePointer<CChar>.allocate(capacity: Int(ifnameSize))
+ ifnamePtr.initialize(repeating: 0, count: Int(ifnameSize))
+ if getsockopt(fileDescriptor, 2 /* SYSPROTO_CONTROL */, 2 /* UTUN_OPT_IFNAME */, ifnamePtr, &ifnameSize) == 0 {
+ self.ifname = String(cString: ifnamePtr)
+ }
+ wg_log(.info, message: "Tunnel interface is \(self.ifname ?? "unknown")")
let handle = self.packetTunnelSettingsGenerator!.uapiConfiguration().withGoString { return wgTurnOn($0, fileDescriptor) }
if handle < 0 {
wg_log(.error, message: "Starting tunnel failed with wgTurnOn returning \(handle)")
private func pathUpdate(path: Network.NWPath) {
guard let handle = handle, let packetTunnelSettingsGenerator = packetTunnelSettingsGenerator else { return }
- var listenPort: UInt16?
- //TODO(zx2c4): Remove the `true` here after extensive testing with network/cell simulations.
- if true || path.availableInterfaces.isEmpty || lastFirstInterface != path.availableInterfaces.first {
- listenPort = wgGetListenPort(handle)
- lastFirstInterface = path.availableInterfaces.first
+ wg_log(.debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)")
+ _ = packetTunnelSettingsGenerator.endpointUapiConfiguration().withGoString { return wgSetConfig(handle, $0) }
+ var interfaces = path.availableInterfaces
+ if let ifname = ifname {
+ interfaces = interfaces.filter { $0.name != ifname }
}
- guard path.status == .satisfied else { return }
- wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
- let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: listenPort)
- let err = endpointString.withGoString { return wgSetConfig(handle, $0) }
- if err == -EADDRINUSE && listenPort != nil {
- let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
- _ = endpointString.withGoString { return wgSetConfig(handle, $0) }
+ if let ifscope = interfaces.first?.index {
+ wgBindInterfaceScope(handle, Int32(ifscope))
}
}
}
return 0
}
-//export wgGetListenPort
-func wgGetListenPort(tunnelHandle int32) uint16 {
+//export wgBindInterfaceScope
+func wgBindInterfaceScope(tunnelHandle int32, ifscope int32) {
+ var operr error
device, ok := tunnelHandles[tunnelHandle]
if !ok {
- return 0
+ return
+ }
+ device.log.Info.Printf("Binding sockets to interface %d\n", ifscope)
+ bind := device.net.bind.(*NativeBind)
+ for bind.ipv4 != nil {
+ fd, err := bind.ipv4.SyscallConn()
+ if err != nil {
+ device.log.Error.Printf("Unable to bind v4 socket to interface:", err)
+ break
+ }
+ err = fd.Control(func(fd uintptr) {
+ operr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, int(ifscope))
+ })
+ if err == nil {
+ err = operr
+ }
+ if err != nil {
+ device.log.Error.Printf("Unable to bind v4 socket to interface:", err)
+ }
+ break
+ }
+ for bind.ipv6 != nil {
+ fd, err := bind.ipv6.SyscallConn()
+ if err != nil {
+ device.log.Error.Printf("Unable to bind v6 socket to interface:", err)
+ break
+ }
+ err = fd.Control(func(fd uintptr) {
+ operr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, int(ifscope))
+ })
+ if err == nil {
+ err = operr
+ }
+ if err != nil {
+ device.log.Error.Printf("Unable to bind v6 socket to interface:", err)
+ }
+ break
}
- return device.net.port
}
//export wgVersion