Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Containerization/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public protocol Interface: Sendable {
/// The IP address for the default route, or nil for no default route.
var ipv4Gateway: IPv4Address? { get }

/// The interface IPv6 address and subnet prefix length, as a CIDR address.
/// Example: `fdef:4eb5:9ccb:341e:fc98:82ff:fe82:4f28/64`
var ipv6Address: CIDRv6? { get }

/// The IPv6 address for the default route, or nil for no default route.
var ipv6Gateway: IPv6Address? { get }

/// The interface MAC address, or nil to auto-configure the address.
var macAddress: MACAddress? { get }

Expand All @@ -34,4 +41,6 @@ public protocol Interface: Sendable {

extension Interface {
public var mtu: UInt32 { 1500 }
public var ipv6Address: CIDRv6? { nil }
public var ipv6Gateway: IPv6Address? { nil }
}
15 changes: 13 additions & 2 deletions Sources/Containerization/LinuxContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,11 @@ extension LinuxContainer {
}

// For every interface asked for:
// 1. Add the address requested
// 1. Add the IPv4 address requested
// 2. Online the adapter
// 3. If a gateway IP address is present, add the default route.
// 3. If an IPv4 gateway IP address is present, add the default route.
// 4. Add the IPv6 address if present
// 5. If an IPv6 gateway IP address is present, add the IPv6 default route.
for (index, i) in self.interfaces.enumerated() {
let name = "eth\(index)"
self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)")
Expand All @@ -489,6 +491,15 @@ extension LinuxContainer {
}
try await agent.routeAddDefault(name: name, ipv4Gateway: ipv4Gateway)
}

// Configure IPv6 if address is present
if let ipv6Address = i.ipv6Address {
self.logger?.debug("setting up interface \(name) with IPv6 address \(ipv6Address)")
try await agent.addressAdd(name: name, ipv6Address: ipv6Address)
if let ipv6Gateway = i.ipv6Gateway {
try await agent.routeAddDefault(name: name, ipv6Gateway: ipv6Gateway)
}
}
}

// Setup /etc/resolv.conf and /etc/hosts if asked for.
Expand Down
15 changes: 13 additions & 2 deletions Sources/Containerization/LinuxPod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,11 @@ extension LinuxPod {
}

// For every interface asked for:
// 1. Add the address requested
// 1. Add the IPv4 address requested
// 2. Online the adapter
// 3. If a gateway IP address is present, add the default route.
// 3. If an IPv4 gateway IP address is present, add the default route.
// 4. Add the IPv6 address if present
// 5. If an IPv6 gateway IP address is present, add the IPv6 default route.
for (index, i) in self.interfaces.enumerated() {
let name = "eth\(index)"
self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)")
Expand All @@ -439,6 +441,15 @@ extension LinuxPod {
}
try await agent.routeAddDefault(name: name, ipv4Gateway: ipv4Gateway)
}

// Configure IPv6 if address is present
if let ipv6Address = i.ipv6Address {
self.logger?.debug("setting up interface \(name) with IPv6 address \(ipv6Address)")
try await agent.addressAdd(name: name, ipv6Address: ipv6Address)
if let ipv6Gateway = i.ipv6Gateway {
try await agent.routeAddDefault(name: name, ipv6Gateway: ipv6Gateway)
}
}
}

// Setup /etc/resolv.conf and /etc/hosts for each container.
Expand Down
13 changes: 12 additions & 1 deletion Sources/Containerization/NATInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ import ContainerizationExtras
public struct NATInterface: Interface {
public var ipv4Address: CIDRv4
public var ipv4Gateway: IPv4Address?
public var ipv6Address: CIDRv6?
public var ipv6Gateway: IPv6Address?
public var macAddress: MACAddress?
public var mtu: UInt32

public init(ipv4Address: CIDRv4, ipv4Gateway: IPv4Address?, macAddress: MACAddress? = nil, mtu: UInt32 = 1500) {
public init(
ipv4Address: CIDRv4,
ipv4Gateway: IPv4Address?,
ipv6Address: CIDRv6? = nil,
ipv6Gateway: IPv6Address? = nil,
macAddress: MACAddress? = nil,
mtu: UInt32 = 1500
) {
self.ipv4Address = ipv4Address
self.ipv4Gateway = ipv4Gateway
self.ipv6Address = ipv6Address
self.ipv6Gateway = ipv6Gateway
self.macAddress = macAddress
self.mtu = mtu
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/Containerization/NATNetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import Synchronization
public final class NATNetworkInterface: Interface, Sendable {
public let ipv4Address: CIDRv4
public let ipv4Gateway: IPv4Address?
public let ipv6Address: CIDRv6?
public let ipv6Gateway: IPv6Address?
public let macAddress: MACAddress?
public let mtu: UInt32

Expand All @@ -40,12 +42,16 @@ public final class NATNetworkInterface: Interface, Sendable {
public init(
ipv4Address: CIDRv4,
ipv4Gateway: IPv4Address?,
ipv6Address: CIDRv6? = nil,
ipv6Gateway: IPv6Address? = nil,
reference: sending vmnet_network_ref,
macAddress: MACAddress? = nil,
mtu: UInt32 = 1500
) {
self.ipv4Address = ipv4Address
self.ipv4Gateway = ipv4Gateway
self.ipv6Address = ipv6Address
self.ipv6Gateway = ipv6Gateway
self.macAddress = macAddress
self.mtu = mtu
self.reference = reference
Expand All @@ -55,11 +61,15 @@ public final class NATNetworkInterface: Interface, Sendable {
public init(
ipv4Address: CIDRv4,
ipv4Gateway: IPv4Address?,
ipv6Address: CIDRv6? = nil,
ipv6Gateway: IPv6Address? = nil,
macAddress: MACAddress? = nil,
mtu: UInt32 = 1500
) {
self.ipv4Address = ipv4Address
self.ipv4Gateway = ipv4Gateway
self.ipv6Address = ipv6Address
self.ipv6Gateway = ipv6Gateway
self.macAddress = macAddress
self.mtu = mtu
self.reference = nil
Expand Down
Loading