2018-10-27 09:32:32 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-10-30 02:57:35 +00:00
|
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
2018-10-27 09:32:32 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Network
|
2018-11-08 10:14:13 +00:00
|
|
|
import NetworkExtension
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-11-08 10:14:13 +00:00
|
|
|
class PacketTunnelSettingsGenerator {
|
|
|
|
let tunnelConfiguration: TunnelConfiguration
|
|
|
|
let resolvedEndpoints: [Endpoint?]
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-11-08 10:14:13 +00:00
|
|
|
init(tunnelConfiguration: TunnelConfiguration, resolvedEndpoints: [Endpoint?]) {
|
|
|
|
self.tunnelConfiguration = tunnelConfiguration
|
|
|
|
self.resolvedEndpoints = resolvedEndpoints
|
|
|
|
}
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-12-21 14:56:03 +00:00
|
|
|
func endpointUapiConfiguration(currentListenPort: UInt16?) -> String {
|
2018-12-21 12:02:44 +00:00
|
|
|
var wgSettings = ""
|
|
|
|
|
2018-12-21 14:56:03 +00:00
|
|
|
if let currentListenPort = currentListenPort {
|
|
|
|
wgSettings.append("listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n")
|
2018-12-21 12:02:44 +00:00
|
|
|
}
|
2018-12-11 22:12:04 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
2018-12-11 22:12:04 +00:00
|
|
|
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
2018-12-12 17:40:57 +00:00
|
|
|
if let endpoint = resolvedEndpoints[index] {
|
2018-12-11 22:12:04 +00:00
|
|
|
if case .name(_, _) = endpoint.host { assert(false, "Endpoint is not resolved") }
|
2018-12-21 04:52:45 +00:00
|
|
|
wgSettings.append("endpoint=\(endpoint.stringRepresentation)\n")
|
2018-12-11 22:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
|
2018-12-11 22:12:04 +00:00
|
|
|
return wgSettings
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
|
2018-12-11 22:59:15 +00:00
|
|
|
func uapiConfiguration() -> String {
|
2018-10-27 09:32:32 +00:00
|
|
|
var wgSettings = ""
|
2018-11-08 10:14:13 +00:00
|
|
|
let privateKey = tunnelConfiguration.interface.privateKey.hexEncodedString()
|
2018-10-27 09:32:32 +00:00
|
|
|
wgSettings.append("private_key=\(privateKey)\n")
|
2018-11-08 10:14:13 +00:00
|
|
|
if let listenPort = tunnelConfiguration.interface.listenPort {
|
2018-10-27 09:32:32 +00:00
|
|
|
wgSettings.append("listen_port=\(listenPort)\n")
|
|
|
|
}
|
2018-12-20 17:22:37 +00:00
|
|
|
if !tunnelConfiguration.peers.isEmpty {
|
2018-10-27 09:32:32 +00:00
|
|
|
wgSettings.append("replace_peers=true\n")
|
|
|
|
}
|
2018-11-08 10:14:13 +00:00
|
|
|
assert(tunnelConfiguration.peers.count == resolvedEndpoints.count)
|
2018-12-12 17:40:57 +00:00
|
|
|
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
2018-10-27 09:32:32 +00:00
|
|
|
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
|
|
|
if let preSharedKey = peer.preSharedKey {
|
|
|
|
wgSettings.append("preshared_key=\(preSharedKey.hexEncodedString())\n")
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
if let endpoint = resolvedEndpoints[index] {
|
2018-10-27 09:32:32 +00:00
|
|
|
if case .name(_, _) = endpoint.host { assert(false, "Endpoint is not resolved") }
|
2018-12-21 04:52:45 +00:00
|
|
|
wgSettings.append("endpoint=\(endpoint.stringRepresentation)\n")
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
let persistentKeepAlive = peer.persistentKeepAlive ?? 0
|
|
|
|
wgSettings.append("persistent_keepalive_interval=\(persistentKeepAlive)\n")
|
2018-12-12 18:28:27 +00:00
|
|
|
if !peer.allowedIPs.isEmpty {
|
2018-10-27 09:32:32 +00:00
|
|
|
wgSettings.append("replace_allowed_ips=true\n")
|
2018-12-21 04:52:45 +00:00
|
|
|
peer.allowedIPs.forEach { wgSettings.append("allowed_ip=\($0.stringRepresentation)\n") }
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 10:14:13 +00:00
|
|
|
return wgSettings
|
|
|
|
}
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-11-08 10:14:13 +00:00
|
|
|
func generateNetworkSettings() -> NEPacketTunnelNetworkSettings {
|
2018-11-05 05:23:26 +00:00
|
|
|
/* iOS requires a tunnel endpoint, whereas in WireGuard it's valid for
|
|
|
|
* a tunnel to have no endpoint, or for there to be many endpoints, in
|
|
|
|
* which case, displaying a single one in settings doesn't really
|
|
|
|
* make sense. So, we fill it in with this placeholder, which is not
|
|
|
|
* a valid IP address that will actually route over the Internet.
|
|
|
|
*/
|
2018-12-13 03:09:52 +00:00
|
|
|
var remoteAddress = "0.0.0.0"
|
2018-12-12 21:33:14 +00:00
|
|
|
let endpointsCompact = resolvedEndpoints.compactMap { $0 }
|
2018-11-05 05:23:26 +00:00
|
|
|
if endpointsCompact.count == 1 {
|
2018-12-12 18:28:27 +00:00
|
|
|
switch endpointsCompact.first!.host {
|
2018-10-27 09:32:32 +00:00
|
|
|
case .ipv4(let address):
|
|
|
|
remoteAddress = "\(address)"
|
|
|
|
case .ipv6(let address):
|
|
|
|
remoteAddress = "\(address)"
|
|
|
|
default:
|
2018-11-05 05:23:26 +00:00
|
|
|
break
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:14:13 +00:00
|
|
|
let networkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: remoteAddress)
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
let dnsServerStrings = tunnelConfiguration.interface.dns.map { $0.stringRepresentation }
|
2018-11-23 07:34:55 +00:00
|
|
|
let dnsSettings = NEDNSSettings(servers: dnsServerStrings)
|
2018-12-13 06:50:10 +00:00
|
|
|
dnsSettings.matchDomains = [""] // All DNS queries must first go through the tunnel's DNS
|
2018-11-23 07:34:55 +00:00
|
|
|
networkSettings.dnsSettings = dnsSettings
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-11-08 10:14:13 +00:00
|
|
|
let mtu = tunnelConfiguration.interface.mtu ?? 0
|
2018-12-12 18:28:27 +00:00
|
|
|
if mtu == 0 {
|
2018-11-08 10:14:13 +00:00
|
|
|
// 0 imples automatic MTU, where we set overhead as 80 bytes, which is the worst case for WireGuard
|
|
|
|
networkSettings.tunnelOverheadBytes = 80
|
|
|
|
} else {
|
|
|
|
networkSettings.mtu = NSNumber(value: mtu)
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
let (ipv4Routes, ipv6Routes) = routes()
|
|
|
|
let (ipv4IncludedRoutes, ipv6IncludedRoutes) = includedRoutes()
|
|
|
|
let (ipv4ExcludedRoutes, ipv6ExcludedRoutes) = excludedRoutes()
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
let ipv4Settings = NEIPv4Settings(addresses: ipv4Routes.map { $0.destinationAddress }, subnetMasks: ipv4Routes.map { $0.destinationSubnetMask })
|
|
|
|
ipv4Settings.includedRoutes = ipv4IncludedRoutes
|
|
|
|
ipv4Settings.excludedRoutes = ipv4ExcludedRoutes
|
|
|
|
networkSettings.ipv4Settings = ipv4Settings
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
let ipv6Settings = NEIPv6Settings(addresses: ipv6Routes.map { $0.destinationAddress }, networkPrefixLengths: ipv6Routes.map { $0.destinationNetworkPrefixLength })
|
|
|
|
ipv6Settings.includedRoutes = ipv6IncludedRoutes
|
|
|
|
ipv6Settings.excludedRoutes = ipv6ExcludedRoutes
|
|
|
|
networkSettings.ipv6Settings = ipv6Settings
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
return networkSettings
|
|
|
|
}
|
2018-10-27 09:32:32 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
private func ipv4SubnetMaskString(of addressRange: IPAddressRange) -> String {
|
|
|
|
let length: UInt8 = addressRange.networkPrefixLength
|
|
|
|
assert(length <= 32)
|
|
|
|
var octets: [UInt8] = [0, 0, 0, 0]
|
|
|
|
let subnetMask: UInt32 = length > 0 ? ~UInt32(0) << (32 - length) : UInt32(0)
|
|
|
|
octets[0] = UInt8(truncatingIfNeeded: subnetMask >> 24)
|
|
|
|
octets[1] = UInt8(truncatingIfNeeded: subnetMask >> 16)
|
|
|
|
octets[2] = UInt8(truncatingIfNeeded: subnetMask >> 8)
|
|
|
|
octets[3] = UInt8(truncatingIfNeeded: subnetMask)
|
|
|
|
return octets.map { String($0) }.joined(separator: ".")
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
private func routes() -> ([NEIPv4Route], [NEIPv6Route]) {
|
|
|
|
var ipv4Routes = [NEIPv4Route]()
|
|
|
|
var ipv6Routes = [NEIPv6Route]()
|
2018-11-08 10:14:13 +00:00
|
|
|
for addressRange in tunnelConfiguration.interface.addresses {
|
2018-12-12 18:28:27 +00:00
|
|
|
if addressRange.address is IPv4Address {
|
2018-12-13 03:09:52 +00:00
|
|
|
ipv4Routes.append(NEIPv4Route(destinationAddress: "\(addressRange.address)", subnetMask: ipv4SubnetMaskString(of: addressRange)))
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if addressRange.address is IPv6Address {
|
2018-12-13 03:09:52 +00:00
|
|
|
/* Big fat ugly hack for broken iOS networking stack: the smallest prefix that will have
|
|
|
|
* any effect on iOS is a /120, so we clamp everything above to /120. This is potentially
|
|
|
|
* very bad, if various network parameters were actually relying on that subnet being
|
|
|
|
* intentionally small. TODO: talk about this with upstream iOS devs.
|
|
|
|
*/
|
|
|
|
ipv6Routes.append(NEIPv6Route(destinationAddress: "\(addressRange.address)", networkPrefixLength: NSNumber(value: min(120, addressRange.networkPrefixLength))))
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 03:09:52 +00:00
|
|
|
return (ipv4Routes, ipv6Routes)
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
private func includedRoutes() -> ([NEIPv4Route], [NEIPv6Route]) {
|
|
|
|
var ipv4IncludedRoutes = [NEIPv4Route]()
|
|
|
|
var ipv6IncludedRoutes = [NEIPv6Route]()
|
2018-11-08 10:14:13 +00:00
|
|
|
for peer in tunnelConfiguration.peers {
|
2018-10-27 09:32:32 +00:00
|
|
|
for addressRange in peer.allowedIPs {
|
2018-12-12 18:28:27 +00:00
|
|
|
if addressRange.address is IPv4Address {
|
2018-12-13 03:09:52 +00:00
|
|
|
ipv4IncludedRoutes.append(NEIPv4Route(destinationAddress: "\(addressRange.address)", subnetMask: ipv4SubnetMaskString(of: addressRange)))
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if addressRange.address is IPv6Address {
|
2018-12-13 03:09:52 +00:00
|
|
|
ipv6IncludedRoutes.append(NEIPv6Route(destinationAddress: "\(addressRange.address)", networkPrefixLength: NSNumber(value: addressRange.networkPrefixLength)))
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 03:09:52 +00:00
|
|
|
return (ipv4IncludedRoutes, ipv6IncludedRoutes)
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
private func excludedRoutes() -> ([NEIPv4Route], [NEIPv6Route]) {
|
|
|
|
var ipv4ExcludedRoutes = [NEIPv4Route]()
|
|
|
|
var ipv6ExcludedRoutes = [NEIPv6Route]()
|
2018-10-27 09:32:32 +00:00
|
|
|
for endpoint in resolvedEndpoints {
|
|
|
|
guard let endpoint = endpoint else { continue }
|
2018-12-12 18:28:27 +00:00
|
|
|
switch endpoint.host {
|
2018-10-27 09:32:32 +00:00
|
|
|
case .ipv4(let address):
|
2018-12-13 03:09:52 +00:00
|
|
|
ipv4ExcludedRoutes.append(NEIPv4Route(destinationAddress: "\(address)", subnetMask: "255.255.255.255"))
|
2018-10-27 09:32:32 +00:00
|
|
|
case .ipv6(let address):
|
2018-12-13 03:09:52 +00:00
|
|
|
ipv6ExcludedRoutes.append(NEIPv6Route(destinationAddress: "\(address)", networkPrefixLength: NSNumber(value: UInt8(128))))
|
2018-10-27 09:32:32 +00:00
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 03:09:52 +00:00
|
|
|
return (ipv4ExcludedRoutes, ipv6ExcludedRoutes)
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-10-27 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension Data {
|
|
|
|
func hexEncodedString() -> String {
|
|
|
|
return self.map { String(format: "%02x", $0) }.joined()
|
|
|
|
}
|
|
|
|
}
|