use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum NeighborState {
Failed,
Resolving,
Resolved,
}
impl NeighborState {
pub(crate) fn is_unresolved(self) -> bool {
self != Self::Resolved
}
}
pub(crate) type NeighborTable = HashMap<IpAddr, NeighborState>;
type Reader = Box<dyn Fn() -> std::io::Result<NeighborTable> + Send + Sync>;
type Router = Box<dyn Fn(IpAddr) -> std::io::Result<Option<IpAddr>> + Send + Sync>;
pub(crate) struct KernelNeighbors {
read: Reader,
snapshot: Mutex<Option<(Instant, NeighborTable)>>,
route: Router,
next_hops: Mutex<HashMap<IpAddr, Option<IpAddr>>>,
}
impl KernelNeighbors {
pub(crate) fn from_system() -> Option<Self> {
#[cfg(target_os = "linux")]
{
Some(Self::with_reader(Box::new(linux::dump)).routing(Box::new(linux::next_hop)))
}
#[cfg(not(target_os = "linux"))]
{
None
}
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn with_reader(read: Reader) -> Self {
Self {
read,
snapshot: Mutex::new(None),
route: Box::new(|_| Ok(None)),
next_hops: Mutex::new(HashMap::new()),
}
}
#[cfg(test)]
pub(crate) fn asking_on_write(
sent: std::sync::Arc<Mutex<Vec<crate::transport::probe::SentProbe>>>,
held: &[IpAddr],
live: &[IpAddr],
) -> Self {
let held = held.to_vec();
let live = live.to_vec();
Self::with_reader(Box::new(move || {
let written: Vec<IpAddr> = sent
.lock()
.expect("the sender's record")
.iter()
.map(|(_, _, destination)| *destination)
.collect();
let mut table: NeighborTable = held
.iter()
.map(|address| (*address, NeighborState::Resolved))
.collect();
for address in written {
let state = if live.contains(&address) {
NeighborState::Resolved
} else {
NeighborState::Resolving
};
table.entry(address).or_insert(state);
}
Ok(table)
}))
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn routing(mut self, route: Router) -> Self {
self.route = route;
self
}
pub(crate) fn next_hop(&self, address: IpAddr) -> Option<IpAddr> {
let mut next_hops = self.next_hops.lock().ok()?;
*next_hops
.entry(address)
.or_insert_with(|| (self.route)(address).ok().flatten())
}
pub(crate) fn state(&self, address: IpAddr, since: Instant) -> Option<NeighborState> {
let mut snapshot = self.snapshot.lock().ok()?;
let fresh = snapshot.as_ref().is_some_and(|(taken, _)| *taken >= since);
if !fresh {
let taken = Instant::now();
let table = (self.read)().ok()?;
*snapshot = Some((taken, table));
}
snapshot.as_ref()?.1.get(&address).copied()
}
}
const DEFAULT_HOLD_DOWN: Duration = Duration::from_secs(20);
pub(crate) fn hold_down() -> Duration {
static HOLD_DOWN: OnceLock<Duration> = OnceLock::new();
*HOLD_DOWN.get_or_init(|| host_down_time().unwrap_or(DEFAULT_HOLD_DOWN))
}
#[cfg(target_os = "macos")]
fn host_down_time() -> Option<Duration> {
let mut seconds: libc::c_int = 0;
let mut size = std::mem::size_of::<libc::c_int>();
let code = unsafe {
libc::sysctlbyname(
c"net.link.ether.inet.host_down_time".as_ptr(),
(&raw mut seconds).cast::<libc::c_void>(),
&mut size,
std::ptr::null_mut(),
0,
)
};
let seconds = u64::try_from(seconds).ok().filter(|_| code == 0)?;
Some(Duration::from_secs(seconds))
}
#[cfg(not(target_os = "macos"))]
fn host_down_time() -> Option<Duration> {
None
}
#[cfg(any(target_os = "linux", test))]
mod wire {
use super::{NeighborState, NeighborTable};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub(super) const RTM_NEWNEIGH: u16 = 28;
pub(super) const NLMSG_ERROR: u16 = 2;
pub(super) const NLMSG_DONE: u16 = 3;
pub(super) const NDA_DST: u16 = 1;
pub(super) const NUD_INCOMPLETE: u16 = 0x01;
pub(super) const NUD_FAILED: u16 = 0x20;
pub(super) const NUD_NONE: u16 = 0x00;
pub(super) const NLMSG_HEADER: usize = 16;
pub(super) const NDMSG: usize = 12;
pub(super) const RTM_NEWROUTE: u16 = 24;
pub(super) const RTA_DST: u16 = 1;
pub(super) const RTA_GATEWAY: u16 = 5;
pub(super) const RTN_UNICAST: u8 = 1;
pub(super) const RTMSG: usize = 12;
pub(super) fn align(len: usize) -> usize {
(len + 3) & !3
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(super) struct Batch {
pub(super) done: bool,
pub(super) error: Option<i32>,
}
pub(super) fn parse(buffer: &[u8], table: &mut NeighborTable) -> Batch {
let mut batch = Batch::default();
let mut offset = 0;
while offset + NLMSG_HEADER <= buffer.len() {
let header = &buffer[offset..];
let len = u32::from_ne_bytes(header[0..4].try_into().expect("four bytes")) as usize;
let kind = u16::from_ne_bytes(header[4..6].try_into().expect("two bytes"));
if len < NLMSG_HEADER || offset + len > buffer.len() {
break;
}
let body = &buffer[offset + NLMSG_HEADER..offset + len];
match kind {
NLMSG_DONE => {
batch.done = true;
break;
}
NLMSG_ERROR => {
let errno = body
.get(0..4)
.map(|bytes| i32::from_ne_bytes(bytes.try_into().expect("four bytes")));
if let Some(errno) = errno.filter(|errno| *errno != 0) {
batch.error = Some(errno);
batch.done = true;
break;
}
}
RTM_NEWNEIGH => {
if let Some((address, state)) = entry(body) {
let known = table.entry(address).or_insert(state);
*known = (*known).max(state);
}
}
_ => {}
}
offset += align(len);
}
batch
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Route {
Via(IpAddr),
Direct,
Elsewhere(Option<i32>),
}
pub(super) fn parse_route(buffer: &[u8]) -> Route {
let mut offset = 0;
while offset + NLMSG_HEADER <= buffer.len() {
let header = &buffer[offset..];
let len = u32::from_ne_bytes(header[0..4].try_into().expect("four bytes")) as usize;
let kind = u16::from_ne_bytes(header[4..6].try_into().expect("two bytes"));
if len < NLMSG_HEADER || offset + len > buffer.len() {
break;
}
let body = &buffer[offset + NLMSG_HEADER..offset + len];
match kind {
NLMSG_ERROR => {
let errno = body
.get(0..4)
.map(|bytes| i32::from_ne_bytes(bytes.try_into().expect("four bytes")));
if errno.is_some_and(|errno| errno != 0) {
return Route::Elsewhere(errno);
}
}
RTM_NEWROUTE => return route(body),
_ => {}
}
offset += align(len);
}
Route::Elsewhere(None)
}
fn route(body: &[u8]) -> Route {
if body.len() < RTMSG || body[7] != RTN_UNICAST {
return Route::Elsewhere(None);
}
let mut attributes = &body[RTMSG..];
while attributes.len() >= 4 {
let len = usize::from(u16::from_ne_bytes(
attributes[0..2].try_into().expect("two bytes"),
));
let kind = u16::from_ne_bytes(attributes[2..4].try_into().expect("two bytes"));
if len < 4 || len > attributes.len() {
return Route::Elsewhere(None);
}
if kind == RTA_GATEWAY {
return match address(&attributes[4..len]) {
Some(gateway) => Route::Via(gateway),
None => Route::Elsewhere(None),
};
}
attributes = attributes.get(align(len)..).unwrap_or(&[]);
}
Route::Direct
}
fn address(value: &[u8]) -> Option<IpAddr> {
match value.len() {
4 => Some(IpAddr::V4(Ipv4Addr::from(<[u8; 4]>::try_from(value).ok()?))),
16 => Some(IpAddr::V6(Ipv6Addr::from(
<[u8; 16]>::try_from(value).ok()?,
))),
_ => None,
}
}
fn entry(body: &[u8]) -> Option<(IpAddr, NeighborState)> {
if body.len() < NDMSG {
return None;
}
let state = match u16::from_ne_bytes(body[8..10].try_into().expect("two bytes")) {
NUD_NONE => return None,
NUD_INCOMPLETE => NeighborState::Resolving,
NUD_FAILED => NeighborState::Failed,
_ => NeighborState::Resolved,
};
let mut attributes = &body[NDMSG..];
while attributes.len() >= 4 {
let len = usize::from(u16::from_ne_bytes(
attributes[0..2].try_into().expect("two bytes"),
));
let kind = u16::from_ne_bytes(attributes[2..4].try_into().expect("two bytes"));
if len < 4 || len > attributes.len() {
return None;
}
if kind == NDA_DST {
return Some((address(&attributes[4..len])?, state));
}
attributes = attributes.get(align(len)..).unwrap_or(&[]);
}
None
}
}
#[cfg(any(target_os = "linux", test))]
use wire::*;
#[cfg(target_os = "linux")]
mod linux {
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::net::IpAddr;
use super::{
NDMSG, NLMSG_HEADER, NeighborTable, RTA_DST, RTMSG, Route, align, parse, parse_route,
};
const READ_BUFFER: usize = 32 * 1024;
pub(super) fn dump() -> std::io::Result<NeighborTable> {
let len = NLMSG_HEADER + NDMSG;
let mut request = vec![0u8; len];
request[0..4].copy_from_slice(&(len as u32).to_ne_bytes());
request[4..6].copy_from_slice(&libc::RTM_GETNEIGH.to_ne_bytes());
let flags = (libc::NLM_F_REQUEST | libc::NLM_F_DUMP) as u16;
request[6..8].copy_from_slice(&flags.to_ne_bytes());
request[8..12].copy_from_slice(&1u32.to_ne_bytes());
let socket = ask(&request)?;
let mut table = NeighborTable::new();
let mut buffer = vec![0u8; READ_BUFFER];
loop {
let read = unsafe {
libc::recv(
socket.as_raw_fd(),
buffer.as_mut_ptr().cast(),
buffer.len(),
0,
)
};
if read < 0 {
let error = std::io::Error::last_os_error();
if error.kind() == std::io::ErrorKind::Interrupted {
continue;
}
return Err(error);
}
if read == 0 {
return Ok(table);
}
let batch = parse(&buffer[..read as usize], &mut table);
if let Some(errno) = batch.error {
return Err(std::io::Error::from_raw_os_error(-errno));
}
if batch.done {
return Ok(table);
}
}
}
pub(super) fn next_hop(address: IpAddr) -> std::io::Result<Option<IpAddr>> {
let (family, octets) = match address {
IpAddr::V4(v4) => (libc::AF_INET, v4.octets().to_vec()),
IpAddr::V6(v6) => (libc::AF_INET6, v6.octets().to_vec()),
};
let attribute = 4 + octets.len();
let len = NLMSG_HEADER + RTMSG + align(attribute);
let mut request = vec![0u8; len];
request[0..4].copy_from_slice(&(len as u32).to_ne_bytes());
request[4..6].copy_from_slice(&libc::RTM_GETROUTE.to_ne_bytes());
request[6..8].copy_from_slice(&(libc::NLM_F_REQUEST as u16).to_ne_bytes());
request[8..12].copy_from_slice(&1u32.to_ne_bytes());
request[NLMSG_HEADER] = family as u8;
request[NLMSG_HEADER + 1] = (octets.len() * 8) as u8;
let at = NLMSG_HEADER + RTMSG;
request[at..at + 2].copy_from_slice(&(attribute as u16).to_ne_bytes());
request[at + 2..at + 4].copy_from_slice(&RTA_DST.to_ne_bytes());
request[at + 4..at + 4 + octets.len()].copy_from_slice(&octets);
let socket = ask(&request)?;
let mut buffer = vec![0u8; READ_BUFFER];
let read = loop {
let read = unsafe {
libc::recv(
socket.as_raw_fd(),
buffer.as_mut_ptr().cast(),
buffer.len(),
0,
)
};
if read >= 0 {
break read as usize;
}
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error);
}
};
Ok(match parse_route(&buffer[..read]) {
Route::Via(gateway) => Some(gateway),
Route::Direct => Some(address),
Route::Elsewhere(_) => None,
})
}
fn ask(request: &[u8]) -> std::io::Result<OwnedFd> {
let fd = unsafe {
libc::socket(
libc::AF_NETLINK,
libc::SOCK_RAW | libc::SOCK_CLOEXEC,
libc::NETLINK_ROUTE,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
let socket = unsafe { OwnedFd::from_raw_fd(fd) };
let sent = unsafe {
let mut kernel: libc::sockaddr_nl = std::mem::zeroed();
kernel.nl_family = libc::AF_NETLINK as libc::sa_family_t;
libc::sendto(
socket.as_raw_fd(),
request.as_ptr().cast(),
request.len(),
0,
(&raw const kernel).cast(),
std::mem::size_of::<libc::sockaddr_nl>() as libc::socklen_t,
)
};
if sent < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(socket)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::Ipv4Addr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
const ON_LINK: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 7));
#[cfg(target_os = "macos")]
#[test]
fn the_hold_down_is_the_kernel_s_own() {
let held = host_down_time().expect("macOS names its hold-down");
assert!(held > Duration::ZERO);
assert_eq!(hold_down(), held);
}
fn neighbour(address: IpAddr, state: u16) -> Vec<u8> {
let octets = match address {
IpAddr::V4(v4) => v4.octets().to_vec(),
IpAddr::V6(v6) => v6.octets().to_vec(),
};
let attribute_len = 4 + octets.len();
let len = NLMSG_HEADER + NDMSG + align(attribute_len);
let mut message = vec![0u8; len];
message[0..4].copy_from_slice(&(len as u32).to_ne_bytes());
message[4..6].copy_from_slice(&RTM_NEWNEIGH.to_ne_bytes());
let body = NLMSG_HEADER;
message[body + 8..body + 10].copy_from_slice(&state.to_ne_bytes());
let attribute = body + NDMSG;
message[attribute..attribute + 2].copy_from_slice(&(attribute_len as u16).to_ne_bytes());
message[attribute + 2..attribute + 4].copy_from_slice(&NDA_DST.to_ne_bytes());
message[attribute + 4..attribute + 4 + octets.len()].copy_from_slice(&octets);
message
}
fn route_reply(kind: u8, gateway: Option<IpAddr>) -> Vec<u8> {
let mut attributes = Vec::new();
if let Some(gateway) = gateway {
let octets = match gateway {
IpAddr::V4(v4) => v4.octets().to_vec(),
IpAddr::V6(v6) => v6.octets().to_vec(),
};
attributes.extend_from_slice(&8u16.to_ne_bytes());
attributes.extend_from_slice(&RTA_DST.to_ne_bytes());
attributes.extend_from_slice(&[198, 51, 100, 7]);
let len = 4 + octets.len();
attributes.extend_from_slice(&(len as u16).to_ne_bytes());
attributes.extend_from_slice(&RTA_GATEWAY.to_ne_bytes());
attributes.extend_from_slice(&octets);
attributes.resize(align(attributes.len()), 0);
}
let len = NLMSG_HEADER + RTMSG + attributes.len();
let mut message = vec![0u8; NLMSG_HEADER + RTMSG];
message[0..4].copy_from_slice(&(len as u32).to_ne_bytes());
message[4..6].copy_from_slice(&RTM_NEWROUTE.to_ne_bytes());
message[NLMSG_HEADER + 7] = kind;
message.extend(attributes);
message
}
#[test]
fn a_route_lookup_names_the_gateway_or_the_link() {
let v4: IpAddr = "192.0.2.254".parse().expect("an address");
let v6: IpAddr = "fe80::1".parse().expect("an address");
assert_eq!(
parse_route(&route_reply(RTN_UNICAST, Some(v4))),
Route::Via(v4)
);
assert_eq!(
parse_route(&route_reply(RTN_UNICAST, Some(v6))),
Route::Via(v6)
);
assert_eq!(parse_route(&route_reply(RTN_UNICAST, None)), Route::Direct);
assert_eq!(parse_route(&route_reply(2, None)), Route::Elsewhere(None));
let mut refused = vec![0u8; NLMSG_HEADER + 4];
refused[0..4].copy_from_slice(&((NLMSG_HEADER + 4) as u32).to_ne_bytes());
refused[4..6].copy_from_slice(&NLMSG_ERROR.to_ne_bytes());
refused[NLMSG_HEADER..].copy_from_slice(&(-101i32).to_ne_bytes());
assert_eq!(parse_route(&refused), Route::Elsewhere(Some(-101)));
}
#[test]
fn a_next_hop_is_asked_for_once_per_address() {
let asked = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&asked);
let gateway: IpAddr = "192.0.2.254".parse().expect("an address");
let routed: IpAddr = "198.51.100.7".parse().expect("an address");
let neighbours = KernelNeighbors::with_reader(Box::new(|| Ok(NeighborTable::new())))
.routing(Box::new(move |_| {
counted.fetch_add(1, Ordering::SeqCst);
Ok(Some(gateway))
}));
for _ in 0..3 {
assert_eq!(neighbours.next_hop(routed), Some(gateway));
}
assert_eq!(asked.load(Ordering::SeqCst), 1);
}
fn done() -> Vec<u8> {
let mut message = vec![0u8; NLMSG_HEADER + 4];
message[0..4].copy_from_slice(&((NLMSG_HEADER + 4) as u32).to_ne_bytes());
message[4..6].copy_from_slice(&NLMSG_DONE.to_ne_bytes());
message
}
#[test]
fn a_dump_reads_each_neighbours_resolution_state() {
let v6: IpAddr = "2001:db8::7".parse().expect("an address");
let resolved: IpAddr = "192.0.2.8".parse().expect("an address");
let stale: IpAddr = "192.0.2.9".parse().expect("an address");
let mut buffer = neighbour(ON_LINK, NUD_INCOMPLETE);
buffer.extend(neighbour(v6, NUD_FAILED));
buffer.extend(neighbour(resolved, 0x02)); buffer.extend(neighbour(stale, 0x04)); buffer.extend(done());
let mut table = NeighborTable::new();
let batch = parse(&buffer, &mut table);
assert!(batch.done, "the dump's end was read");
assert_eq!(table.get(&ON_LINK), Some(&NeighborState::Resolving));
assert_eq!(table.get(&v6), Some(&NeighborState::Failed));
assert_eq!(table.get(&resolved), Some(&NeighborState::Resolved));
assert_eq!(table.get(&stale), Some(&NeighborState::Resolved));
}
#[test]
fn an_address_on_two_links_reads_as_its_most_resolved_entry() {
let mut buffer = neighbour(ON_LINK, NUD_FAILED);
buffer.extend(neighbour(ON_LINK, 0x02));
buffer.extend(neighbour(ON_LINK, NUD_INCOMPLETE));
let mut table = NeighborTable::new();
parse(&buffer, &mut table);
assert_eq!(table.get(&ON_LINK), Some(&NeighborState::Resolved));
}
#[test]
fn the_walk_survives_any_buffer() {
let mut state = 0x2545_F491_4F6C_DD1Du64;
let mut next = || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
for _ in 0..20_000 {
let len = (next() % 256) as usize;
let buffer: Vec<u8> = (0..len).map(|_| (next() & 0xFF) as u8).collect();
parse(&buffer, &mut NeighborTable::new());
}
for _ in 0..20_000 {
let mut buffer = neighbour(ON_LINK, NUD_INCOMPLETE);
let at = (next() as usize) % buffer.len();
buffer[at] = (next() & 0xFF) as u8;
buffer.truncate((next() as usize) % (buffer.len() + 1));
parse(&buffer, &mut NeighborTable::new());
}
for _ in 0..20_000 {
let mut buffer = route_reply(RTN_UNICAST, Some(ON_LINK));
let at = (next() as usize) % buffer.len();
buffer[at] = (next() & 0xFF) as u8;
buffer.truncate((next() as usize) % (buffer.len() + 1));
parse_route(&buffer);
}
}
#[test]
fn a_reading_older_than_the_question_is_read_again() {
let reads = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&reads);
let neighbours = KernelNeighbors::with_reader(Box::new(move || {
counted.fetch_add(1, Ordering::SeqCst);
Ok(NeighborTable::from([(ON_LINK, NeighborState::Resolving)]))
}));
let before = Instant::now();
assert_eq!(
neighbours.state(ON_LINK, before),
Some(NeighborState::Resolving)
);
assert_eq!(
neighbours.state(ON_LINK, before),
Some(NeighborState::Resolving)
);
assert_eq!(reads.load(Ordering::SeqCst), 1, "the reading was shared");
let later = Instant::now() + Duration::from_millis(1);
std::thread::sleep(Duration::from_millis(2));
neighbours.state(ON_LINK, later);
assert_eq!(
reads.load(Ordering::SeqCst),
2,
"a later question read again"
);
}
#[cfg(target_os = "linux")]
#[test]
fn the_running_kernels_table_can_be_read() {
linux::dump().expect("rtnetlink answers a neighbour dump");
}
#[cfg(target_os = "linux")]
#[test]
fn the_running_kernels_routes_can_be_asked() {
let loopback = IpAddr::V4(Ipv4Addr::LOCALHOST);
assert_eq!(
linux::next_hop(loopback).expect("rtnetlink answers a route lookup"),
None
);
}
}