Skip to content

Commit bf8e6dc

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 5f121d2 commit bf8e6dc

File tree

8 files changed

+632
-3
lines changed

8 files changed

+632
-3
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/network/bridge.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{
55
os::fd::BorrowedFd,
66
};
77

8-
use crate::dns::aardvark::SafeString;
98
use crate::network::core_utils::get_default_route_interface;
109
use crate::network::dhcp::{dhcp_teardown, get_dhcp_lease};
1110
use crate::network::netlink::Socket;
@@ -22,6 +21,7 @@ use crate::{
2221
},
2322
network::{constants, sysctl::disable_ipv6_autoconf, types},
2423
};
24+
use crate::{dns::aardvark::SafeString, network::netlink_netfilter::flush_udp_conntrack};
2525
use ipnet::IpNet;
2626
use log::{debug, error};
2727
use netlink_packet_route::address::{AddressAttribute, AddressScope};
@@ -542,7 +542,18 @@ impl<'a> Bridge<'a> {
542542

543543
self.info.firewall.setup_network(sn, &system_dbus)?;
544544

545+
let port_mappings = spf.port_mappings;
546+
let container_ip_v4 = spf.container_ip_v4;
547+
let container_ip_v6 = spf.container_ip_v6;
548+
545549
self.info.firewall.setup_port_forward(spf, &system_dbus)?;
550+
551+
if let Some(port_mappings) = port_mappings {
552+
// Flush stale UDP conntrack entries to prevent dropped packets.
553+
// See the function's doc comment for more details.
554+
flush_udp_conntrack(port_mappings, container_ip_v4, container_ip_v6)?;
555+
}
556+
546557
Ok(())
547558
}
548559

@@ -639,7 +650,18 @@ impl<'a> Bridge<'a> {
639650
complete_teardown,
640651
};
641652

653+
let port_mappings = tpf.config.port_mappings;
654+
let container_ip_v4 = tpf.config.container_ip_v4;
655+
let container_ip_v6 = tpf.config.container_ip_v6;
656+
642657
self.info.firewall.teardown_port_forward(tpf)?;
658+
659+
if let Some(port_mappings) = port_mappings {
660+
// Flush stale UDP conntrack entries to prevent dropped packets.
661+
// See the function's doc comment for more details.
662+
flush_udp_conntrack(port_mappings, container_ip_v4, container_ip_v6)?;
663+
}
664+
643665
Ok(())
644666
}
645667
}

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)