From 41ffd07f3eb4a357405dd69c97f85e3db24a6fc2 Mon Sep 17 00:00:00 2001 From: Dmitry Kovba Date: Thu, 23 Apr 2026 08:05:25 -0700 Subject: [PATCH] Add a default route to the first interface only (#697) Adds a default route to the first interface only. --- Sources/Containerization/LinuxContainer.swift | 7 ++++++- Sources/Containerization/LinuxPod.swift | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Sources/Containerization/LinuxContainer.swift b/Sources/Containerization/LinuxContainer.swift index ab8c0fb..24924cb 100644 --- a/Sources/Containerization/LinuxContainer.swift +++ b/Sources/Containerization/LinuxContainer.swift @@ -590,12 +590,16 @@ extension LinuxContainer { // For every interface asked for: // 1. Add the address requested // 2. Online the adapter - // 3. If a gateway IP address is present, add the default route. + // 3. For the first interface, add the default route + var defaultRouteSet = false for (index, i) in self.interfaces.enumerated() { let name = "eth\(index)" self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)") try await agent.addressAdd(name: name, ipv4Address: i.ipv4Address) try await agent.up(name: name, mtu: i.mtu) + if defaultRouteSet { + continue + } if let ipv4Gateway = i.ipv4Gateway { if !i.ipv4Address.contains(ipv4Gateway) { self.logger?.debug("gateway \(ipv4Gateway) is outside subnet \(i.ipv4Address), adding a route first") @@ -606,6 +610,7 @@ extension LinuxContainer { self.logger?.debug("no gateway for \(name)") try await agent.routeAddDefault(name: name, ipv4Gateway: nil) } + defaultRouteSet = true } // Setup /etc/resolv.conf and /etc/hosts if asked for. diff --git a/Sources/Containerization/LinuxPod.swift b/Sources/Containerization/LinuxPod.swift index a37ce06..035e1a8 100644 --- a/Sources/Containerization/LinuxPod.swift +++ b/Sources/Containerization/LinuxPod.swift @@ -444,12 +444,16 @@ extension LinuxPod { // For every interface asked for: // 1. Add the address requested // 2. Online the adapter - // 3. If a gateway IP address is present, add the default route. + // 3. For the first interface, add the default route + var defaultRouteSet = false for (index, i) in self.interfaces.enumerated() { let name = "eth\(index)" self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)") try await agent.addressAdd(name: name, ipv4Address: i.ipv4Address) try await agent.up(name: name, mtu: i.mtu) + if defaultRouteSet { + continue + } if let ipv4Gateway = i.ipv4Gateway { if !i.ipv4Address.contains(ipv4Gateway) { self.logger?.debug("gateway \(ipv4Gateway) is outside subnet \(i.ipv4Address), adding a route first") @@ -460,6 +464,7 @@ extension LinuxPod { self.logger?.debug("no gateway for \(name)") try await agent.routeAddDefault(name: name, ipv4Gateway: nil) } + defaultRouteSet = true } // Setup /etc/resolv.conf and /etc/hosts for each container.