Skip to content

Commit 425517c

Browse files
committed
firewall: flush stale UDP conntrack entries on port_forward setup/teardown
Add a new netlink_netfilter module to interact with the kernel's conntrack table using the netlink_packet_netfilter crate. This module allows dumping and deleting conntrack entries. All firewall drivers now call the new flush_udp_conntrack() function during port forwarding setup and teardown. When a container with a UDP port mapping is started, stale conntrack entries can prevent traffic from reaching the new container instance. This change proactively deletes these stale entries for the mapped UDP ports, ensuring that new connections are not dropped by the kernel. Added an integration test for the same and unit tests for dump_conntrack and del_conntrack. Fixes: #1045 Signed-off-by: Shivang K Raghuvanshi <[email protected]>
1 parent c5bd6a1 commit 425517c

File tree

10 files changed

+709
-2
lines changed

10 files changed

+709
-2
lines changed

Cargo.lock

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ nix = { version = "0.30.1", features = ["net", "sched", "signal", "socket", "use
4747
rand = "0.9.2"
4848
sha2 = "0.10.9"
4949
netlink-packet-route = "0.25.1"
50+
netlink-packet-netfilter = { git = "https://github.com/shivkr6/netlink-packet-netfilter.git", branch = "conntrack-new" }
5051
netlink-packet-core = "0.8.1"
5152
netlink-sys = "0.8.7"
5253
nftables = "0.6.3"

src/firewall/firewalld.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::error::{NetavarkError, NetavarkResult};
22
use crate::network::internal_types;
33
use crate::network::internal_types::{PortForwardConfig, TearDownNetwork, TeardownPortForward};
4+
use crate::network::netlink_netfilter::flush_udp_conntrack;
45
use crate::network::types::PortMapping;
56
use crate::{firewall, wrap};
67
use core::convert::TryFrom;
@@ -373,6 +374,16 @@ impl firewall::FirewallDriver for FirewallD {
373374
setup_portfw.container_id
374375
);
375376

377+
if let Some(port_mappings) = setup_portfw.port_mappings {
378+
// Flush stale UDP conntrack entries to prevent dropped packets.
379+
// See the function's doc comment for more details.
380+
flush_udp_conntrack(
381+
port_mappings,
382+
setup_portfw.container_ip_v4,
383+
setup_portfw.container_ip_v6,
384+
)?;
385+
}
386+
376387
Ok(())
377388
}
378389

@@ -617,6 +628,16 @@ impl firewall::FirewallDriver for FirewallD {
617628
}
618629
update_policy_config(&self.conn, HOSTFWDPOLICYNAME, new_localhost_policy_config)?;
619630

631+
if let Some(port_mappings) = teardown_pf.config.port_mappings {
632+
// Flush stale UDP conntrack entries to prevent dropped packets.
633+
// See the function's doc comment for more details.
634+
flush_udp_conntrack(
635+
port_mappings,
636+
teardown_pf.config.container_ip_v4,
637+
teardown_pf.config.container_ip_v6,
638+
)?;
639+
}
640+
620641
Ok(())
621642
}
622643
}

src/firewall/iptables.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::firewall::varktables::types::{
99
use crate::network::internal_types::{
1010
PortForwardConfig, SetupNetwork, TearDownNetwork, TeardownPortForward,
1111
};
12+
use crate::network::netlink_netfilter::flush_udp_conntrack;
1213
use iptables;
1314
use iptables::IPTables;
1415

@@ -151,6 +152,17 @@ impl firewall::FirewallDriver for IptablesDriver {
151152
get_port_forwarding_chains(&self.conn6, &setup_portfw, &v6, &subnet_v6, true)?;
152153
create_network_chains(chains)?;
153154
};
155+
156+
if let Some(port_mappings) = setup_portfw.port_mappings {
157+
// Flush stale UDP conntrack entries to prevent dropped packets.
158+
// See the function's doc comment for more details.
159+
flush_udp_conntrack(
160+
port_mappings,
161+
setup_portfw.container_ip_v4,
162+
setup_portfw.container_ip_v6,
163+
)?;
164+
}
165+
154166
Result::Ok(())
155167
}
156168

@@ -216,6 +228,16 @@ impl firewall::FirewallDriver for IptablesDriver {
216228
}
217229
}
218230
}
231+
if let Some(port_mappings) = tear.config.port_mappings {
232+
// Flush stale UDP conntrack entries to prevent dropped packets.
233+
// See the function's doc comment for more details.
234+
flush_udp_conntrack(
235+
port_mappings,
236+
tear.config.container_ip_v4,
237+
tear.config.container_ip_v6,
238+
)?;
239+
}
240+
219241
Result::Ok(())
220242
}
221243
}

src/firewall/nft.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::firewall;
33
use crate::firewall::firewalld;
44
use crate::network::internal_types;
55
use crate::network::internal_types::IsolateOption;
6+
use crate::network::netlink_netfilter::flush_udp_conntrack;
67
use crate::network::types::PortMapping;
78
use ipnet::IpNet;
89
use nftables::batch::Batch;
@@ -744,6 +745,16 @@ impl firewall::FirewallDriver for Nftables {
744745

745746
helper::apply_ruleset(&rules)?;
746747

748+
if let Some(port_mappings) = setup_portfw.port_mappings {
749+
// Flush stale UDP conntrack entries to prevent dropped packets.
750+
// See the function's doc comment for more details.
751+
flush_udp_conntrack(
752+
port_mappings,
753+
setup_portfw.container_ip_v4,
754+
setup_portfw.container_ip_v6,
755+
)?;
756+
}
757+
747758
Ok(())
748759
}
749760

@@ -815,6 +826,16 @@ impl firewall::FirewallDriver for Nftables {
815826

816827
helper::apply_ruleset(&rules)?;
817828

829+
if let Some(port_mappings) = teardown_pf.config.port_mappings {
830+
// Flush stale UDP conntrack entries to prevent dropped packets.
831+
// See the function's doc comment for more details.
832+
flush_udp_conntrack(
833+
port_mappings,
834+
teardown_pf.config.container_ip_v4,
835+
teardown_pf.config.container_ip_v6,
836+
)?;
837+
}
838+
818839
Ok(())
819840
}
820841
}

src/network/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod driver;
1818
pub mod internal_types;
1919

2020
pub mod netlink;
21+
pub mod netlink_netfilter;
2122
pub mod netlink_route;
2223

2324
pub mod plugin;

0 commit comments

Comments
 (0)