network interface changes cleanup

This commit is contained in:
Christien Rioux 2023-10-12 21:17:47 -04:00
parent 54c403ebfb
commit ebd36d82ef
8 changed files with 84 additions and 69 deletions

View File

@ -3,9 +3,9 @@ use super::*;
use libc::{ use libc::{
close, freeifaddrs, getifaddrs, if_nametoindex, ifaddrs, ioctl, pid_t, sockaddr, sockaddr_in6, close, freeifaddrs, getifaddrs, if_nametoindex, ifaddrs, ioctl, pid_t, sockaddr, sockaddr_in6,
socket, sysctl, time_t, AF_INET6, CTL_NET, IFF_BROADCAST, IFF_LOOPBACK, IFF_RUNNING, IFNAMSIZ, socket, sysctl, time_t, AF_INET6, CTL_NET, IFF_BROADCAST, IFF_LOOPBACK, IFF_POINTOPOINT,
NET_RT_FLAGS, PF_ROUTE, RTAX_DST, RTAX_GATEWAY, RTAX_MAX, RTA_DST, RTA_GATEWAY, RTF_GATEWAY, IFF_RUNNING, IFNAMSIZ, NET_RT_FLAGS, PF_ROUTE, RTAX_DST, RTAX_GATEWAY, RTAX_MAX, RTA_DST,
SOCK_DGRAM, RTA_GATEWAY, RTF_GATEWAY, SOCK_DGRAM,
}; };
use sockaddr_tools::SockAddr; use sockaddr_tools::SockAddr;
use std::ffi::CStr; use std::ffi::CStr;
@ -13,12 +13,15 @@ use std::io;
use std::os::raw::{c_int, c_uchar, c_ulong, c_ushort, c_void}; use std::os::raw::{c_int, c_uchar, c_ulong, c_ushort, c_void};
const SIOCGIFAFLAG_IN6: c_ulong = 0xC1206949; const SIOCGIFAFLAG_IN6: c_ulong = 0xC1206949;
const SIOCGIFALIFETIME_IN6: c_ulong = 0xC1206951;
const IN6_IFF_TENTATIVE: c_ushort = 0x0002; const IN6_IFF_TENTATIVE: c_ushort = 0x0002;
const IN6_IFF_DUPLICATED: c_ushort = 0x0004; const IN6_IFF_DUPLICATED: c_ushort = 0x0004;
const IN6_IFF_DETACHED: c_ushort = 0x0008; const IN6_IFF_DETACHED: c_ushort = 0x0008;
const IN6_IFF_AUTOCONF: c_ushort = 0x0040;
const IN6_IFF_TEMPORARY: c_ushort = 0x0080; const IN6_IFF_TEMPORARY: c_ushort = 0x0080;
const IN6_IFF_DEPRECATED: c_ushort = 0x0010; const IN6_IFF_DEPRECATED: c_ushort = 0x0010;
const IN6_IFF_DYNAMIC: c_ushort = 0x0100; const IN6_IFF_DYNAMIC: c_ushort = 0x0100;
const IN6_IFF_SECURED: c_ushort = 0x0400;
macro_rules! set_name { macro_rules! set_name {
($name_field:expr, $name_str:expr) => {{ ($name_field:expr, $name_str:expr) => {{
@ -198,6 +201,9 @@ impl in6_ifreq {
pub fn get_flags6(&self) -> c_ushort { pub fn get_flags6(&self) -> c_ushort {
unsafe { self.ifr_ifru.ifru_flags6 as c_ushort } unsafe { self.ifr_ifru.ifru_flags6 as c_ushort }
} }
pub fn get_ia6t_expire(&self) -> time_t {
unsafe { self.ifr_ifru.ifru_lifetime.ia6t_expire as time_t }
}
} }
pub fn do_broadcast(ifaddr: &ifaddrs) -> Option<IpAddr> { pub fn do_broadcast(ifaddr: &ifaddrs) -> Option<IpAddr> {
@ -368,37 +374,59 @@ impl PlatformSupportApple {
Ok(InterfaceFlags { Ok(InterfaceFlags {
is_loopback: (flags & IFF_LOOPBACK) != 0, is_loopback: (flags & IFF_LOOPBACK) != 0,
is_running: (flags & IFF_RUNNING) != 0, is_running: (flags & IFF_RUNNING) != 0,
is_point_to_point: (flags & IFF_POINTOPOINT) != 0,
has_default_route: self.default_route_interfaces.contains(&index), has_default_route: self.default_route_interfaces.contains(&index),
}) })
} }
fn get_address_flags(ifname: &str, addr: sockaddr_in6) -> EyreResult<AddressFlags> { fn get_address_flags(ifname: &str, addr: sockaddr_in6) -> EyreResult<AddressFlags> {
let mut req = in6_ifreq::from_name(ifname).unwrap();
req.set_addr(addr);
let sock = unsafe { socket(AF_INET6, SOCK_DGRAM, 0) }; let sock = unsafe { socket(AF_INET6, SOCK_DGRAM, 0) };
if sock < 0 { if sock < 0 {
bail!("Socket error {:?}", io::Error::last_os_error()); bail!("Socket error {:?}", io::Error::last_os_error());
} }
let mut req = in6_ifreq::from_name(ifname).unwrap();
req.set_addr(addr);
let res = unsafe { ioctl(sock, SIOCGIFAFLAG_IN6, &mut req) }; let res = unsafe { ioctl(sock, SIOCGIFAFLAG_IN6, &mut req) };
unsafe { close(sock) };
if res < 0 { if res < 0 {
unsafe { close(sock) };
bail!( bail!(
"SIOCGIFAFLAG_IN6 failed with error on device '{}': {:?}", "SIOCGIFAFLAG_IN6 failed with error on device '{}': {:?}",
ifname, ifname,
io::Error::last_os_error() io::Error::last_os_error()
); );
} }
let flags = req.get_flags6(); let flags = req.get_flags6();
let mut req = in6_ifreq::from_name(ifname).unwrap();
req.set_addr(addr);
let res = unsafe { ioctl(sock, SIOCGIFALIFETIME_IN6, &mut req) };
unsafe { close(sock) };
if res < 0 {
bail!(
"SIOCGIFALIFETIME_IN6 failed with error on device '{}': {:?}",
ifname,
io::Error::last_os_error()
);
}
let expire = req.get_ia6t_expire();
let is_auto_generated_random_address =
flags & (IN6_IFF_SECURED | IN6_IFF_AUTOCONF) == (IN6_IFF_SECURED | IN6_IFF_AUTOCONF);
let is_temporary =
(flags & IN6_IFF_TEMPORARY) != 0 || (expire != 0 && is_auto_generated_random_address);
let is_dynamic = (flags & (IN6_IFF_DYNAMIC | IN6_IFF_AUTOCONF)) != 0;
let is_preferred = (flags
& (IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED | IN6_IFF_DEPRECATED))
== 0;
Ok(AddressFlags { Ok(AddressFlags {
is_temporary: (flags & IN6_IFF_TEMPORARY) != 0, is_temporary,
is_dynamic: (flags & IN6_IFF_DYNAMIC) != 0, is_dynamic,
is_preferred: (flags is_preferred,
& (IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED | IN6_IFF_DEPRECATED))
== 0,
}) })
} }
@ -408,11 +436,6 @@ impl PlatformSupportApple {
) -> EyreResult<()> { ) -> EyreResult<()> {
self.refresh_default_route_interfaces().await?; self.refresh_default_route_interfaces().await?;
// If we have no routes, this isn't going to work
if self.default_route_interfaces.is_empty() {
bail!("no routes available for NetworkInterfaces");
}
// Ask for all the addresses we have // Ask for all the addresses we have
let ifaddrs = IfAddrs::new().wrap_err("failed to get interface addresses")?; let ifaddrs = IfAddrs::new().wrap_err("failed to get interface addresses")?;
for ifaddr in ifaddrs.iter() { for ifaddr in ifaddrs.iter() {

View File

@ -74,6 +74,7 @@ pub struct Ifv6Addr {
pub struct InterfaceFlags { pub struct InterfaceFlags {
pub is_loopback: bool, pub is_loopback: bool,
pub is_running: bool, pub is_running: bool,
pub is_point_to_point: bool,
pub has_default_route: bool, pub has_default_route: bool,
} }
@ -261,6 +262,10 @@ impl NetworkInterface {
self.flags.is_loopback self.flags.is_loopback
} }
pub fn is_point_to_point(&self) -> bool {
self.flags.is_point_to_point
}
pub fn is_running(&self) -> bool { pub fn is_running(&self) -> bool {
self.flags.is_running self.flags.is_running
} }
@ -363,9 +368,9 @@ impl NetworkInterfaces {
// See if our best addresses have changed // See if our best addresses have changed
if old_best_addresses != inner.interface_address_cache { if old_best_addresses != inner.interface_address_cache {
trace!( debug!(
"Network interface addresses changed: {:?}", "Network interface addresses changed: \nFrom: {:?}\n To: {:?}\n",
inner.interface_address_cache old_best_addresses, inner.interface_address_cache
); );
return Ok(true); return Ok(true);
} }
@ -391,7 +396,11 @@ impl NetworkInterfaces {
// Reduce interfaces to their best routable ip addresses // Reduce interfaces to their best routable ip addresses
let mut intf_addrs = Vec::new(); let mut intf_addrs = Vec::new();
for intf in inner.interfaces.values() { for intf in inner.interfaces.values() {
if !intf.is_running() || !intf.has_default_route() || intf.is_loopback() { if !intf.is_running()
|| !intf.has_default_route()
|| intf.is_loopback()
|| intf.is_point_to_point()
{
continue; continue;
} }
if let Some(pipv4) = intf.primary_ipv4() { if let Some(pipv4) = intf.primary_ipv4() {

View File

@ -4,8 +4,8 @@ use alloc::collections::btree_map::Entry;
use futures_util::stream::TryStreamExt; use futures_util::stream::TryStreamExt;
use ifstructs::ifreq; use ifstructs::ifreq;
use libc::{ use libc::{
close, if_indextoname, ioctl, socket, IFF_LOOPBACK, IFF_RUNNING, IF_NAMESIZE, SIOCGIFFLAGS, close, if_indextoname, ioctl, socket, IFF_LOOPBACK, IFF_POINTOPOINT, IFF_RUNNING, IF_NAMESIZE,
SOCK_DGRAM, SIOCGIFFLAGS, SOCK_DGRAM,
}; };
use netlink_packet_route::{ use netlink_packet_route::{
nlas::address::Nla, AddressMessage, AF_INET, AF_INET6, IFA_F_DADFAILED, IFA_F_DEPRECATED, nlas::address::Nla, AddressMessage, AF_INET, AF_INET6, IFA_F_DADFAILED, IFA_F_DEPRECATED,
@ -138,6 +138,7 @@ impl PlatformSupportNetlink {
Ok(InterfaceFlags { Ok(InterfaceFlags {
is_loopback: (flags & IFF_LOOPBACK) != 0, is_loopback: (flags & IFF_LOOPBACK) != 0,
is_running: (flags & IFF_RUNNING) != 0, is_running: (flags & IFF_RUNNING) != 0,
is_point_to_point: (flags & IFF_POINTOPOINT) != 0,
has_default_route: self.default_route_interfaces.contains(&index), has_default_route: self.default_route_interfaces.contains(&index),
}) })
} }
@ -248,11 +249,6 @@ impl PlatformSupportNetlink {
// Refresh the routes // Refresh the routes
self.refresh_default_route_interfaces().await?; self.refresh_default_route_interfaces().await?;
// If we have no routes, this isn't going to work
if self.default_route_interfaces.is_empty() {
bail!("no routes available for NetworkInterfaces");
}
// Ask for all the addresses we have // Ask for all the addresses we have
let mut names = BTreeMap::<u32, String>::new(); let mut names = BTreeMap::<u32, String>::new();
let mut addresses = self.handle.as_ref().unwrap().address().get().execute(); let mut addresses = self.handle.as_ref().unwrap().address().get().execute();

View File

@ -48,13 +48,7 @@ impl SockAddr {
((sa.sin_addr.s_addr >> 16) & 255) as u8, ((sa.sin_addr.s_addr >> 16) & 255) as u8,
((sa.sin_addr.s_addr >> 24) & 255) as u8, ((sa.sin_addr.s_addr >> 24) & 255) as u8,
))), ))),
Some(SockAddrIn::In6(sa)) => { Some(SockAddrIn::In6(sa)) => Some(IpAddr::V6(Ipv6Addr::from(sa.sin6_addr.s6_addr))),
// Ignore all fe80:: addresses as these are link locals
if sa.sin6_addr.s6_addr[0] == 0xfe && sa.sin6_addr.s6_addr[1] == 0x80 {
return None;
}
Some(IpAddr::V6(Ipv6Addr::from(sa.sin6_addr.s6_addr)))
}
None => None, None => None,
} }
} }
@ -64,10 +58,6 @@ impl SockAddr {
match self.sockaddr_in() { match self.sockaddr_in() {
Some(SockAddrIn::In(sa)) => { Some(SockAddrIn::In(sa)) => {
let s_addr = unsafe { sa.sin_addr.S_un.S_addr() }; let s_addr = unsafe { sa.sin_addr.S_un.S_addr() };
// Ignore all 169.254.x.x addresses as these are not active interfaces
if s_addr & 65535 == 0xfea9 {
return None;
}
Some(IpAddr::V4(Ipv4Addr::new( Some(IpAddr::V4(Ipv4Addr::new(
(s_addr & 255u32) as u8, (s_addr & 255u32) as u8,
((s_addr >> 8) & 255u32) as u8, ((s_addr >> 8) & 255u32) as u8,
@ -77,10 +67,6 @@ impl SockAddr {
} }
Some(SockAddrIn::In6(sa)) => { Some(SockAddrIn::In6(sa)) => {
let s6_addr = unsafe { sa.sin6_addr.u.Byte() }; let s6_addr = unsafe { sa.sin6_addr.u.Byte() };
// Ignore all fe80:: addresses as these are link locals
if s6_addr[0] == 0xfe && s6_addr[1] == 0x80 {
return None;
}
Some(IpAddr::V6(Ipv6Addr::from(*s6_addr))) Some(IpAddr::V6(Ipv6Addr::from(*s6_addr)))
} }
None => None, None => None,

View File

@ -13,7 +13,7 @@ use libc::{self, c_ulong, c_void, size_t};
use std::ffi::CStr; use std::ffi::CStr;
use std::{io, ptr}; use std::{io, ptr};
use winapi::shared::ifdef::IfOperStatusUp; use winapi::shared::ifdef::IfOperStatusUp;
use winapi::shared::ipifcons::IF_TYPE_SOFTWARE_LOOPBACK; use winapi::shared::ipifcons::{IF_TYPE_SOFTWARE_LOOPBACK, IF_TYPE_TUNNEL};
use winapi::shared::nldef::{ use winapi::shared::nldef::{
IpDadStatePreferred, IpPrefixOriginDhcp, IpSuffixOriginDhcp, IpSuffixOriginRandom, IpDadStatePreferred, IpPrefixOriginDhcp, IpSuffixOriginDhcp, IpSuffixOriginRandom,
}; };
@ -36,6 +36,7 @@ impl PlatformSupportWindows {
InterfaceFlags { InterfaceFlags {
is_loopback: intf.get_flag_loopback(), is_loopback: intf.get_flag_loopback(),
is_running: intf.get_flag_running(), is_running: intf.get_flag_running(),
is_point_to_point: intf.get_flag_point_to_point(),
has_default_route: intf.get_has_default_route(), has_default_route: intf.get_has_default_route(),
} }
} }
@ -55,13 +56,6 @@ impl PlatformSupportWindows {
&mut self, &mut self,
interfaces: &mut BTreeMap<String, NetworkInterface>, interfaces: &mut BTreeMap<String, NetworkInterface>,
) -> EyreResult<()> { ) -> EyreResult<()> {
//self.refresh_default_route_interfaces().await?;
// If we have no routes, this isn't going to work
// if self.default_route_interfaces.is_empty() {
// return Err("no routes available for NetworkInterfaces".to_owned());
// }
// Iterate all the interfaces // Iterate all the interfaces
let windows_interfaces = let windows_interfaces =
WindowsInterfaces::new().wrap_err("failed to get windows interfaces")?; WindowsInterfaces::new().wrap_err("failed to get windows interfaces")?;
@ -224,6 +218,9 @@ impl IpAdapterAddresses {
pub fn get_flag_running(&self) -> bool { pub fn get_flag_running(&self) -> bool {
unsafe { (*self.data).OperStatus == IfOperStatusUp } unsafe { (*self.data).OperStatus == IfOperStatusUp }
} }
pub fn get_flag_point_to_point(&self) -> bool {
unsafe { (*self.data).IfType == IF_TYPE_TUNNEL }
}
pub fn get_has_default_route(&self) -> bool { pub fn get_has_default_route(&self) -> bool {
unsafe { !(*self.data).FirstGatewayAddress.is_null() } unsafe { !(*self.data).FirstGatewayAddress.is_null() }
} }

View File

@ -658,12 +658,20 @@ impl DiscoveryContext {
return; return;
} }
// Did external addresses change from the last time we made dialinfo? // Did external address change from the last time we made dialinfo?
// Disregard port for this because we only need to know if the ip address has changed
// If the port has changed it will change only for this protocol and will be overwritten individually by each protocol discover()
let some_clear_network_callback = { let some_clear_network_callback = {
let inner = self.inner.lock(); let inner = self.inner.lock();
let ext_1 = inner.external_1.as_ref().unwrap().address; let ext_1 = inner.external_1.as_ref().unwrap().address.address();
let ext_2 = inner.external_2.as_ref().unwrap().address; let ext_2 = inner.external_2.as_ref().unwrap().address.address();
if (ext_1 != ext_2) || Some(ext_1) != self.unlocked_inner.existing_external_address { if (ext_1 != ext_2)
|| Some(ext_1)
!= self
.unlocked_inner
.existing_external_address
.map(|ea| ea.address())
{
// External address was not found, or has changed, go ahead and clear the network so we can do better // External address was not found, or has changed, go ahead and clear the network so we can do better
Some(self.unlocked_inner.clear_network_callback.clone()) Some(self.unlocked_inner.clear_network_callback.clone())
} else { } else {

View File

@ -375,15 +375,14 @@ impl Network {
addrs addrs
} }
// See if our interface addresses have changed, if so we need to punt the network // See if our interface addresses have changed, if so redo public dial info if necessary
// and redo all our addresses. This is overkill, but anything more accurate
// would require inspection of routing tables that we dont want to bother with
async fn check_interface_addresses(&self) -> EyreResult<bool> { async fn check_interface_addresses(&self) -> EyreResult<bool> {
if !self.unlocked_inner.interfaces.refresh().await? { if !self.unlocked_inner.interfaces.refresh().await? {
return Ok(false); return Ok(false);
} }
self.inner.lock().network_needs_restart = true; self.inner.lock().needs_public_dial_info_check = true;
Ok(true) Ok(true)
} }
@ -700,7 +699,7 @@ impl Network {
self.unlocked_inner self.unlocked_inner
.interfaces .interfaces
.with_interfaces(|interfaces| { .with_interfaces(|interfaces| {
trace!("interfaces: {:#?}", interfaces); debug!("interfaces: {:#?}", interfaces);
for intf in interfaces.values() { for intf in interfaces.values() {
// Skip networks that we should never encounter // Skip networks that we should never encounter
@ -978,9 +977,8 @@ impl Network {
_l: u64, _l: u64,
_t: u64, _t: u64,
) -> EyreResult<()> { ) -> EyreResult<()> {
if self.check_interface_addresses().await? { self.check_interface_addresses().await?;
info!("interface addresses changed, restarting network");
}
Ok(()) Ok(())
} }

View File

@ -50,13 +50,11 @@ impl Network {
let mut add = false; let mut add = false;
if let Some(edi) = existing_dial_info.get(&(pt, at)) { if let Some(edi) = existing_dial_info.get(&(pt, at)) {
if did.class < edi.class { if did.class <= edi.class {
// Better dial info class was found, clear existing dialinfo for this pt/at pair // Better or same dial info class was found, clear existing dialinfo for this pt/at pair
// Only keep one dial info per protocol/address type combination
clear = true; clear = true;
add = true; add = true;
} else if did.class == edi.class {
// Same dial info class, just add dial info
add = true;
} }
// Otherwise, don't upgrade, don't add, this is worse than what we have already // Otherwise, don't upgrade, don't add, this is worse than what we have already
} else { } else {