fix(tun): restrict Linux route removal to the main table (#2869)

This commit is contained in:
fanyang
2026-11-26 23:16:02 +08:00
committed by GitHub
parent 7dac1b9d35
commit c3ee1440db
2 changed files with 52 additions and 2 deletions
+41 -2
View File
@@ -441,11 +441,11 @@ impl IfConfiguerTrait for NetlinkIfConfiger {
.destination()
.copied()
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
// Only routes we added (RTPROT_STATIC), not the kernel's connected route.
if destination == IpAddr::V4(address)
&& msg.dst_len() == cidr_prefix
&& msg.oif() == Some(ifidx)
&& msg.protocol() == libc::RTPROT_STATIC
&& msg.table_id()? == u32::from(libc::RT_TABLE_MAIN)
{
let request = message_request(RTM_DELROUTE, NLM_F_ACK | NLM_F_REQUEST, &msg)?;
send_netlink_req_and_wait_ack(request)?;
@@ -593,11 +593,11 @@ impl IfConfiguerTrait for NetlinkIfConfiger {
.destination()
.copied()
.unwrap_or(IpAddr::V6(Ipv6Addr::UNSPECIFIED));
// Only routes we added (RTPROT_STATIC), not the kernel's connected route.
if destination == IpAddr::V6(address)
&& msg.dst_len() == cidr_prefix
&& msg.oif() == Some(ifidx)
&& msg.protocol() == libc::RTPROT_STATIC
&& msg.table_id()? == u32::from(libc::RT_TABLE_MAIN)
{
let request = message_request(RTM_DELROUTE, NLM_F_ACK | NLM_F_REQUEST, &msg)?;
send_netlink_req_and_wait_ack(request)?;
@@ -810,6 +810,45 @@ mod tests {
.unwrap();
}
#[serial_test::serial]
#[tokio::test]
async fn remove_route_keeps_policy_table_routes() {
let iface = test_iface_name("pt");
let _link = ScopedDummyLink::new(&iface);
let ifcfg = NetlinkIfConfiger {};
let v4 = "192.0.2.241".parse().unwrap();
let v6 = "2001:db8:2416::1".parse().unwrap();
let prefixes = [("-4", "192.0.2.241/32"), ("-6", "2001:db8:2416::1/128")];
let tables = [("100", "static"), ("101", "boot"), ("1000", "static")];
ifcfg.add_ipv4_route(&iface, v4, 32, None).await.unwrap();
ifcfg.add_ipv6_route(&iface, v6, 128, None).await.unwrap();
for (family, prefix) in prefixes {
for (table, proto) in tables {
run_cmd(&format!(
"ip {family} route add {prefix} dev {iface} metric 65535 proto {proto} table {table}"
));
}
}
// Retry after removal to cover either kernel dump order.
for _ in 0..2 {
ifcfg.remove_ipv4_route(&iface, v4, 32).await.unwrap();
ifcfg.remove_ipv6_route(&iface, v6, 128).await.unwrap();
for (family, prefix) in prefixes {
let show = |table| {
run_cmd(&format!(
"ip {family} route show table {table} dev {iface} exact {prefix}"
))
};
assert!(show("main").is_empty(), "{prefix} left in main");
for (table, _) in tables {
assert!(!show(table).is_empty(), "{prefix} missing from {table}");
}
}
}
}
#[serial_test::serial]
#[tokio::test]
async fn remove_route_keeps_kernel_connected_route() {
+11
View File
@@ -362,6 +362,17 @@ impl RouteMessage {
self.protocol
}
pub(crate) fn table_id(&self) -> io::Result<u32> {
match self
.attributes
.iter()
.find(|attr| attr.kind & NLA_TYPE_MASK == RTA_TABLE)
{
Some(attr) => read_u32(&attr.value),
None => Ok(self.table.into()),
}
}
pub(crate) fn route_type(&self) -> RouteType {
RouteType::from_raw(self.route_type)
}