2018-11-06 18:04:53 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-06-22 06:23:39 +00:00
|
|
|
|
2018-11-29 10:16:21 +00:00
|
|
|
import Foundation
|
2018-12-11 22:12:04 +00:00
|
|
|
import Network
|
|
|
|
import NetworkExtension
|
2018-07-07 20:54:44 +00:00
|
|
|
import os.log
|
2018-06-22 06:23:39 +00:00
|
|
|
|
|
|
|
class PacketTunnelProvider: NEPacketTunnelProvider {
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-22 17:29:05 +00:00
|
|
|
private var handle: Int32?
|
2018-12-11 22:12:04 +00:00
|
|
|
private var networkMonitor: NWPathMonitor?
|
2018-12-25 21:38:32 +00:00
|
|
|
private var ifname: String?
|
2018-12-21 14:56:03 +00:00
|
|
|
private var packetTunnelSettingsGenerator: PacketTunnelSettingsGenerator?
|
2018-09-26 09:22:54 +00:00
|
|
|
|
2018-12-22 04:41:54 +00:00
|
|
|
deinit {
|
|
|
|
networkMonitor?.cancel()
|
|
|
|
}
|
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
2018-12-13 20:54:53 +00:00
|
|
|
let activationAttemptId = options?["activationAttemptId"] as? String
|
2018-12-22 02:41:54 +00:00
|
|
|
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId)
|
2018-12-13 20:54:53 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
guard let tunnelProviderProtocol = protocolConfiguration as? NETunnelProviderProtocol,
|
2018-12-21 23:28:18 +00:00
|
|
|
let tunnelConfiguration = tunnelProviderProtocol.asTunnelConfiguration() else {
|
2018-12-13 20:54:53 +00:00
|
|
|
errorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
|
2018-11-08 12:20:47 +00:00
|
|
|
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()
|
2019-01-22 12:09:38 +00:00
|
|
|
#if os(macOS)
|
|
|
|
wgEnableRoaming(true)
|
|
|
|
#endif
|
2018-11-29 10:16:21 +00:00
|
|
|
|
2018-12-21 21:05:47 +00:00
|
|
|
wg_log(.info, message: "Starting tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
|
2018-12-21 10:10:04 +00:00
|
|
|
|
2018-11-08 12:20:47 +00:00
|
|
|
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
2018-12-21 13:53:16 +00:00
|
|
|
guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
|
|
|
|
errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
|
|
|
|
startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure)
|
2018-11-08 12:20:47 +00:00
|
|
|
return
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
2018-11-08 12:20:47 +00:00
|
|
|
assert(endpoints.count == resolvedEndpoints.count)
|
|
|
|
|
2018-12-21 14:56:03 +00:00
|
|
|
packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
|
2018-11-08 12:20:47 +00:00
|
|
|
|
2018-12-22 17:29:05 +00:00
|
|
|
setTunnelNetworkSettings(packetTunnelSettingsGenerator!.generateNetworkSettings()) { error in
|
2018-08-27 20:32:47 +00:00
|
|
|
if let error = error {
|
2018-12-22 01:21:07 +00:00
|
|
|
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
|
2018-12-22 02:41:54 +00:00
|
|
|
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
|
|
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
2018-08-27 20:32:47 +00:00
|
|
|
} else {
|
2018-12-22 17:29:05 +00:00
|
|
|
self.networkMonitor = NWPathMonitor()
|
2020-06-19 10:42:35 +00:00
|
|
|
self.networkMonitor!.pathUpdateHandler = { [weak self] path in
|
|
|
|
self?.pathUpdate(path: path)
|
|
|
|
}
|
2018-12-22 17:29:05 +00:00
|
|
|
self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
|
|
|
|
|
|
|
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
|
|
|
|
if fileDescriptor < 0 {
|
|
|
|
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
|
|
|
|
errorNotifier.notify(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
|
|
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
|
|
|
return
|
|
|
|
}
|
2020-06-19 10:37:34 +00:00
|
|
|
|
|
|
|
self.ifname = Self.getInterfaceName(fileDescriptor: fileDescriptor)
|
2018-12-25 21:38:32 +00:00
|
|
|
wg_log(.info, message: "Tunnel interface is \(self.ifname ?? "unknown")")
|
2020-06-19 10:37:34 +00:00
|
|
|
|
2020-02-07 11:31:42 +00:00
|
|
|
let handle = self.packetTunnelSettingsGenerator!.uapiConfiguration()
|
|
|
|
.withCString { return wgTurnOn($0, fileDescriptor) }
|
2018-12-22 17:29:05 +00:00
|
|
|
if handle < 0 {
|
|
|
|
wg_log(.error, message: "Starting tunnel failed with wgTurnOn returning \(handle)")
|
|
|
|
errorNotifier.notify(PacketTunnelProviderError.couldNotStartBackend)
|
|
|
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartBackend)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.handle = handle
|
2018-12-17 05:51:25 +00:00
|
|
|
startTunnelCompletionHandler(nil)
|
2018-08-27 20:32:47 +00:00
|
|
|
}
|
2018-07-07 20:54:44 +00:00
|
|
|
}
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
2018-12-11 22:12:04 +00:00
|
|
|
networkMonitor?.cancel()
|
|
|
|
networkMonitor = nil
|
2018-12-12 17:40:57 +00:00
|
|
|
|
2018-12-13 20:54:53 +00:00
|
|
|
ErrorNotifier.removeLastErrorFile()
|
|
|
|
|
2018-12-04 10:28:02 +00:00
|
|
|
wg_log(.info, staticMessage: "Stopping tunnel")
|
2018-12-22 17:29:05 +00:00
|
|
|
if let handle = handle {
|
2018-08-27 20:32:47 +00:00
|
|
|
wgTurnOff(handle)
|
|
|
|
}
|
2018-06-22 06:23:39 +00:00
|
|
|
completionHandler()
|
2019-02-07 14:01:37 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
// HACK: This is a filthy hack to work around Apple bug 32073323 (dup'd by us as 47526107).
|
|
|
|
// Remove it when they finally fix this upstream and the fix has been rolled out to
|
|
|
|
// sufficient quantities of users.
|
2019-10-15 14:51:50 +00:00
|
|
|
exit(0)
|
2019-02-07 14:01:37 +00:00
|
|
|
#endif
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 23:00:46 +00:00
|
|
|
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
|
|
|
guard let completionHandler = completionHandler else { return }
|
|
|
|
guard let handle = handle else {
|
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if messageData.count == 1 && messageData[0] == 0 {
|
|
|
|
guard let settings = wgGetConfig(handle) else {
|
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completionHandler(String(cString: settings).data(using: .utf8)!)
|
|
|
|
free(settings)
|
|
|
|
} else {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:37:34 +00:00
|
|
|
private class func getInterfaceName(fileDescriptor: Int32) -> String? {
|
|
|
|
var ifnameBytes = [CChar](repeating: 0, count: Int(IF_NAMESIZE))
|
|
|
|
|
|
|
|
return ifnameBytes.withUnsafeMutableBufferPointer { bufferPointer -> String? in
|
|
|
|
guard let baseAddress = bufferPointer.baseAddress else { return nil }
|
|
|
|
|
|
|
|
var ifnameSize = socklen_t(bufferPointer.count)
|
|
|
|
let result = getsockopt(
|
|
|
|
fileDescriptor,
|
|
|
|
2 /* SYSPROTO_CONTROL */,
|
|
|
|
2 /* UTUN_OPT_IFNAME */,
|
|
|
|
baseAddress, &ifnameSize
|
|
|
|
)
|
|
|
|
|
|
|
|
if result == 0 {
|
|
|
|
return String(cString: baseAddress)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 12:03:53 +00:00
|
|
|
private func configureLogger() {
|
2019-03-17 06:41:10 +00:00
|
|
|
Logger.configureGlobal(tagged: "NET", withFilePath: FileManager.logFileURL?.path)
|
2018-12-13 14:26:04 +00:00
|
|
|
wgSetLogger { level, msgC in
|
|
|
|
guard let msgC = msgC else { return }
|
2018-08-28 12:03:53 +00:00
|
|
|
let logType: OSLogType
|
|
|
|
switch level {
|
|
|
|
case 0:
|
|
|
|
logType = .debug
|
|
|
|
case 1:
|
|
|
|
logType = .info
|
|
|
|
case 2:
|
|
|
|
logType = .error
|
|
|
|
default:
|
|
|
|
logType = .default
|
|
|
|
}
|
2018-12-13 14:26:04 +00:00
|
|
|
wg_log(logType, message: String(cString: msgC))
|
2018-08-28 12:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 14:56:03 +00:00
|
|
|
private func pathUpdate(path: Network.NWPath) {
|
2019-01-22 12:09:38 +00:00
|
|
|
guard let handle = handle else { return }
|
2019-05-30 13:31:09 +00:00
|
|
|
wg_log(.debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)")
|
|
|
|
|
|
|
|
#if os(iOS)
|
2019-01-22 12:09:38 +00:00
|
|
|
if let packetTunnelSettingsGenerator = packetTunnelSettingsGenerator {
|
2020-02-07 11:31:42 +00:00
|
|
|
_ = packetTunnelSettingsGenerator.endpointUapiConfiguration()
|
|
|
|
.withCString { return wgSetConfig(handle, $0) }
|
2019-01-22 12:09:38 +00:00
|
|
|
}
|
2019-05-30 13:31:09 +00:00
|
|
|
#endif
|
2019-06-10 16:47:39 +00:00
|
|
|
wgBumpSockets(handle)
|
2018-08-28 12:04:38 +00:00
|
|
|
}
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|