From 53c4492fb34f8ad49ebbe476b3a5eba50e8236da Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 29 Sep 2021 10:46:17 +0200 Subject: [PATCH] agent: netlink: Use the grpc IP family field when updating the route Not all routes have either a gateway or a destination IP. Interface routes, where the source, destination and gateway are undefined, will default to IP v4 with the current is_ipv6() check even when they are v6 routes. We use the provided gRPC Route.Family field instead. This field is built from the host netlink messages, and is a reliable way of finding out a route's IP family. Fixes: #2768 Signed-off-by: Samuel Ortiz (cherry picked from commit a44cde7e8dda2bd501ceff8226e18178366c9f5e) --- src/agent/src/netlink.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/agent/src/netlink.rs b/src/agent/src/netlink.rs index 647b281a23..1d488b3880 100644 --- a/src/agent/src/netlink.rs +++ b/src/agent/src/netlink.rs @@ -312,7 +312,6 @@ impl Handle { for route in list { let link = self.find_link(LinkFilter::Name(&route.device)).await?; - let is_v6 = is_ipv6(route.get_gateway()) || is_ipv6(route.get_dest()); const MAIN_TABLE: u8 = packet::constants::RT_TABLE_MAIN; const UNICAST: u8 = packet::constants::RTN_UNICAST; @@ -334,7 +333,7 @@ impl Handle { // `rtnetlink` offers a separate request builders for different IP versions (IP v4 and v6). // This if branch is a bit clumsy because it does almost the same. - if is_v6 { + if route.get_family() == IPFamily::v6 { let dest_addr = if !route.dest.is_empty() { Ipv6Network::from_str(&route.dest)? } else { @@ -594,10 +593,6 @@ fn format_address(data: &[u8]) -> Result { } } -fn is_ipv6(str: &str) -> bool { - Ipv6Addr::from_str(str).is_ok() -} - fn parse_mac_address(addr: &str) -> Result<[u8; 6]> { let mut split = addr.splitn(6, ':'); @@ -932,16 +927,6 @@ mod tests { assert_eq!(bytes, [0xAB, 0x0C, 0xDE, 0x12, 0x34, 0x56]); } - #[test] - fn check_ipv6() { - assert!(is_ipv6("::1")); - assert!(is_ipv6("2001:0:3238:DFE1:63::FEFB")); - - assert!(!is_ipv6("")); - assert!(!is_ipv6("127.0.0.1")); - assert!(!is_ipv6("10.10.10.10")); - } - fn clean_env_for_test_add_one_arp_neighbor(dummy_name: &str, ip: &str) { // ip link delete dummy Command::new("ip")