wireguard-apple/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift

194 lines
8.6 KiB
Swift
Raw Normal View History

2018-11-06 18:04:53 +00:00
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
2018-11-29 10:16:21 +00:00
import Foundation
import Network
import NetworkExtension
import os.log
2018-08-03 20:24:41 +00:00
enum PacketTunnelProviderError: Error {
case savedProtocolConfigurationIsInvalid
case dnsResolutionFailure(tunnelName: String, isActivateOnDemandEnabled: Bool)
case couldNotStartWireGuard
case coultNotSetNetworkSettings
2018-08-03 20:24:41 +00:00
}
class PacketTunnelProvider: NEPacketTunnelProvider {
2018-09-26 09:22:54 +00:00
private var wgHandle: Int32?
private var networkMonitor: NWPathMonitor?
2018-09-26 09:22:54 +00:00
deinit {
networkMonitor?.cancel()
}
//swiftlint:disable:next function_body_length
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
let activationAttemptId = options?["activationAttemptId"] as? String
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId, tunnelProvider: self)
guard let tunnelProviderProtocol = protocolConfiguration as? NETunnelProviderProtocol,
let tunnelConfiguration = tunnelProviderProtocol.tunnelConfiguration() else {
errorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
startTunnelCompletionHandler(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
return
2018-08-27 20:32:47 +00:00
}
2018-08-16 19:26:24 +00:00
2018-11-29 10:16:21 +00:00
configureLogger()
let tunnelName = tunnelConfiguration.interface.name
wg_log(.info, message: "Starting tunnel '\(tunnelName)'")
if activationAttemptId != nil {
wg_log(.info, staticMessage: "Tunnel activated from the app")
} else {
wg_log(.info, staticMessage: "Tunnel not activated from the app")
}
let isActivateOnDemandEnabled = tunnelProviderProtocol.isActivateOnDemandEnabled
if isActivateOnDemandEnabled {
wg_log(.info, staticMessage: "Tunnel has Activate On Demand enabled")
} else {
wg_log(.info, staticMessage: "Tunnel has Activate On Demand disabled")
}
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
guard let resolvedEndpoints = resolveDomainNames(endpoints: endpoints, isActivateOnDemandEnabled: isActivateOnDemandEnabled) else {
let dnsError = PacketTunnelProviderError.dnsResolutionFailure(tunnelName: tunnelName, isActivateOnDemandEnabled: isActivateOnDemandEnabled)
errorNotifier.notify(dnsError)
startTunnelCompletionHandler(dnsError)
return
}
assert(endpoints.count == resolvedEndpoints.count)
let packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
let fileDescriptor = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32 //swiftlint:disable:this force_cast
if fileDescriptor < 0 {
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
errorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard)
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartWireGuard)
return
}
let wireguardSettings = packetTunnelSettingsGenerator.uapiConfiguration()
var handle: Int32 = -1
networkMonitor = NWPathMonitor()
networkMonitor?.pathUpdateHandler = { path in
guard handle >= 0 else { return }
if path.status == .satisfied {
2018-12-11 23:45:50 +00:00
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
2018-12-13 18:36:43 +00:00
let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
2018-12-11 23:45:50 +00:00
if err == -EADDRINUSE {
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
2018-12-13 18:36:43 +00:00
_ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
}
}
}
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
handle = connect(interfaceName: tunnelConfiguration.interface.name, settings: wireguardSettings, fileDescriptor: fileDescriptor)
2018-08-27 20:32:47 +00:00
if handle < 0 {
wg_log(.error, staticMessage: "Starting tunnel failed: Could not start WireGuard")
errorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard)
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartWireGuard)
2018-08-27 20:32:47 +00:00
return
}
2018-08-15 20:57:40 +00:00
2018-08-27 20:32:47 +00:00
wgHandle = handle
2018-08-15 20:57:40 +00:00
let networkSettings: NEPacketTunnelNetworkSettings = packetTunnelSettingsGenerator.generateNetworkSettings()
setTunnelNetworkSettings(networkSettings) { error in
2018-08-27 20:32:47 +00:00
if let error = error {
wg_log(.error, staticMessage: "Starting tunnel failed: Error setting network settings.")
wg_log(.error, message: "Error from setTunnelNetworkSettings: \(error.localizedDescription)")
errorNotifier.notify(PacketTunnelProviderError.coultNotSetNetworkSettings)
startTunnelCompletionHandler(PacketTunnelProviderError.coultNotSetNetworkSettings)
2018-08-27 20:32:47 +00:00
} else {
startTunnelCompletionHandler(nil)
2018-08-27 20:32:47 +00:00
}
}
}
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
networkMonitor?.cancel()
networkMonitor = nil
ErrorNotifier.removeLastErrorFile()
wg_log(.info, staticMessage: "Stopping tunnel")
2018-08-27 20:32:47 +00:00
if let handle = wgHandle {
wgTurnOff(handle)
}
completionHandler()
}
private func configureLogger() {
Logger.configureGlobal(withFilePath: FileManager.networkExtensionLogFileURL?.path)
wgSetLogger { level, msgC in
guard let msgC = msgC else { return }
let logType: OSLogType
switch level {
case 0:
logType = .debug
case 1:
logType = .info
case 2:
logType = .error
default:
logType = .default
}
wg_log(logType, message: String(cString: msgC))
}
}
private func resolveDomainNames(endpoints: [Endpoint?], isActivateOnDemandEnabled: Bool) -> [Endpoint?]? {
var resolvedEndpoints = [Endpoint?]()
let dnsResolutionAttemptsCount = isActivateOnDemandEnabled ? 10 : 1
var isDNSResolved = false
for attemptIndex in 0 ..< dnsResolutionAttemptsCount {
do {
resolvedEndpoints = try DNSResolver.resolveSync(endpoints: endpoints)
isDNSResolved = true
} catch DNSResolverError.dnsResolutionFailed(let hostnames) {
wg_log(.error, staticMessage: "Starting tunnel failed: DNS resolution failure")
wg_log(.error, message: "Hostnames for which DNS resolution failed: \(hostnames.joined(separator: ", "))")
} catch {
// There can be no other errors from DNSResolver.resolveSync()
fatalError()
}
if isDNSResolved {
break
} else {
let isLastAttempt = attemptIndex == dnsResolutionAttemptsCount - 1
if !isLastAttempt {
Thread.sleep(forTimeInterval: 4 /* seconds */)
wg_log(.error, message: "Retrying DNS resolution (Attempt \(attemptIndex + 2))")
}
}
}
return isDNSResolved ? resolvedEndpoints : nil
}
private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 {
2018-12-13 18:36:43 +00:00
return withStringsAsGoStrings(interfaceName, settings) { return wgTurnOn($0.0, $0.1, fileDescriptor) }
}
}
2018-08-27 20:32:47 +00:00
2018-12-13 19:06:37 +00:00
// swiftlint:disable:next large_tuple identifier_name
2018-12-13 18:43:12 +00:00
func withStringsAsGoStrings<R>(_ s1: String, _ s2: String? = nil, _ s3: String? = nil, _ s4: String? = nil, call: ((gostring_t, gostring_t, gostring_t, gostring_t)) -> R) -> R {
2018-12-13 19:06:37 +00:00
// swiftlint:disable:next large_tuple identifier_name
2018-12-13 18:43:12 +00:00
func helper(_ p1: UnsafePointer<Int8>?, _ p2: UnsafePointer<Int8>?, _ p3: UnsafePointer<Int8>?, _ p4: UnsafePointer<Int8>?, _ call: ((gostring_t, gostring_t, gostring_t, gostring_t)) -> R) -> R {
2018-12-13 18:36:43 +00:00
return call((gostring_t(p: p1, n: s1.utf8.count), gostring_t(p: p2, n: s2?.utf8.count ?? 0), gostring_t(p: p3, n: s3?.utf8.count ?? 0), gostring_t(p: p4, n: s4?.utf8.count ?? 0)))
2018-08-27 20:32:47 +00:00
}
2018-12-13 18:43:12 +00:00
return helper(s1, s2, s3, s4, call)
2018-08-27 20:32:47 +00:00
}