use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Mutex;
use std::time::{Duration, Instant};
use pnet_base::MacAddr;
use pnet_packet::Packet;
use pnet_packet::arp::{ArpOperations, ArpPacket};
use pnet_packet::ethernet::{EtherTypes, EthernetPacket};
use crate::protocols::arp;
use crate::transport::capture::{self, FrameSink};
use crate::transport::frame;
use crate::transport::neighbor::{self, LinkRoute, NeighborResolver};
use crate::transport::probe::{Emission, IpProtocols, ProbeSender, SendError};
const ARP_TIMEOUT: Duration = Duration::from_millis(500);
const ARP_REQUESTS: u32 = 3;
const NEIGHBOR_UNREACHABLE_TTL: Duration = Duration::from_secs(30);
const CHANNEL_READ_TIMEOUT: Duration = Duration::from_millis(50);
struct InterfaceChannel {
channel: capture::FrameChannel,
}
#[derive(Default)]
struct UnansweredNeighbors {
seen: HashMap<(String, IpAddr), Instant>,
}
impl UnansweredNeighbors {
fn note(&mut self, interface: &str, next_hop: IpAddr, at: Instant) {
self.seen.insert((interface.to_string(), next_hop), at);
}
fn clear(&mut self, interface: &str, next_hop: IpAddr) {
self.seen.remove(&(interface.to_string(), next_hop));
}
fn is_fresh(&self, interface: &str, next_hop: IpAddr, now: Instant, ttl: Duration) -> bool {
self.seen
.get(&(interface.to_string(), next_hop))
.is_some_and(|at| now.saturating_duration_since(*at) < ttl)
}
}
pub struct EthernetSender {
resolver: Mutex<NeighborResolver>,
channels: Mutex<HashMap<String, InterfaceChannel>>,
unanswered: Mutex<UnansweredNeighbors>,
protocols: IpProtocols,
}
impl EthernetSender {
pub fn from_system(protocols: IpProtocols) -> Option<Self> {
let resolver = NeighborResolver::from_system();
if !resolver.has_ethernet() {
return None;
}
Some(Self {
resolver: Mutex::new(resolver),
channels: Mutex::new(HashMap::new()),
unanswered: Mutex::new(UnansweredNeighbors::default()),
protocols,
})
}
fn next_hop_mac(&self, route: &LinkRoute) -> Result<MacAddr, SendError> {
if let Some(mac) = route.next_hop_mac {
return Ok(mac);
}
let target_v4 = match route.next_hop {
IpAddr::V4(v4) => v4,
IpAddr::V6(_) => {
return Err(SendError::Unsupported(
"IPv6 next-hop resolution (NDP) is not yet implemented",
));
}
};
let src_v4 = match route.src_ip {
IpAddr::V4(v4) => v4,
IpAddr::V6(_) => {
return Err(SendError::Unsupported(
"an IPv4 target cannot be reached from an IPv6 source",
));
}
};
let mac = recall_or_ask(&self.unanswered, &route.interface, route.next_hop, || {
self.arp_resolve(&route.interface, route.src_mac, src_v4, target_v4)
})?;
self.resolver
.lock()
.map_err(|_| poisoned("route resolver"))?
.remember(&route.interface, route.next_hop, mac);
Ok(mac)
}
fn arp_resolve(
&self,
interface: &str,
src_mac: MacAddr,
src_ip: Ipv4Addr,
target: Ipv4Addr,
) -> Result<MacAddr, SendError> {
let mut channels = self
.channels
.lock()
.map_err(|_| poisoned("datalink channel"))?;
let channel = self.channel_for(&mut channels, interface)?;
let request = arp::build_request(src_mac, src_ip, target);
resolve_over(&mut channel.channel, &request, target)
.map_err(|reason| SendError::Refused(format!("sending an ARP request: {reason}")))?
.ok_or_else(|| unanswered_neighbor(IpAddr::V4(target), interface))
}
fn channel_for<'a>(
&self,
channels: &'a mut HashMap<String, InterfaceChannel>,
interface: &str,
) -> Result<&'a mut InterfaceChannel, SendError> {
if !channels.contains_key(interface) {
let channel = capture::FrameChannel::open(interface, "arp", CHANNEL_READ_TIMEOUT)
.map_err(|error| {
SendError::Refused(format!("no datalink channel on {interface}: {error}"))
})?;
channels.insert(interface.to_string(), InterfaceChannel { channel });
}
Ok(channels.get_mut(interface).unwrap())
}
}
impl ProbeSender for EthernetSender {
fn send(
&self,
segment: &[u8],
src: IpAddr,
dst: IpAddr,
_zone: Option<u32>,
emission: Emission,
) -> Result<(), SendError> {
(|| -> Result<(), SendError> {
let route = self
.resolver
.lock()
.map_err(|_| poisoned("route resolver"))?
.resolve_from(src, dst)
.ok_or_else(|| {
SendError::Unroutable(format!("no Ethernet route from {src} to {dst}"))
})?;
let dst_mac = self.next_hop_mac(&route)?;
let src_mac = spoofed_source_mac(route.src_mac, emission.source_mac);
let protocol = self.protocols.for_destination(dst);
let spec = frame::FrameSpec {
src_mac,
dst_mac,
src,
dst,
protocol,
hop_limit: emission.hop_limit,
};
let frames = match emission.fragment {
Some(mtu) => frame::build_fragmented_ethernet_frames(&spec, segment, mtu),
None => frame::build_ethernet_frame(&spec, segment).map(|frame| vec![frame]),
}
.map_err(|error| {
SendError::Refused(format!("the frame could not be built: {error}"))
})?;
let mut channels = self
.channels
.lock()
.map_err(|_| poisoned("datalink channel"))?;
let channel = self.channel_for(&mut channels, &route.interface)?;
for frame in &frames {
channel.channel.send_frame(frame).map_err(|reason| {
SendError::Refused(format!("the frame could not be sent: {reason}"))
})?;
}
Ok(())
})()
}
}
trait ResolutionLink: FrameSink {
fn next_frame(&mut self) -> Option<&[u8]>;
fn now(&self) -> Instant {
Instant::now()
}
}
impl ResolutionLink for capture::FrameChannel {
fn next_frame(&mut self) -> Option<&[u8]> {
capture::FrameChannel::next_frame(self)
}
}
fn resolve_over(
link: &mut impl ResolutionLink,
request: &[u8],
target: Ipv4Addr,
) -> Result<Option<MacAddr>, String> {
let started = link.now();
let spacing = ARP_TIMEOUT / ARP_REQUESTS;
let mut sent = 0;
loop {
let elapsed = link.now().duration_since(started);
if elapsed >= ARP_TIMEOUT {
return Ok(None);
}
if sent < ARP_REQUESTS && elapsed >= spacing * sent {
link.send_frame(request)?;
sent += 1;
}
let Some(frame) = link.next_frame() else {
continue; };
if let Some(mac) = parse_arp_reply(frame, target) {
return Ok(Some(mac));
}
}
}
fn poisoned(what: &str) -> SendError {
SendError::Refused(format!(
"the {what} lock was poisoned by another thread's panic"
))
}
fn recall_or_ask(
unanswered: &Mutex<UnansweredNeighbors>,
interface: &str,
next_hop: IpAddr,
exchange: impl FnOnce() -> Result<MacAddr, SendError>,
) -> Result<MacAddr, SendError> {
if let Some(mac) = neighbor::learned_neighbor(interface, next_hop) {
return Ok(mac);
}
let mac = ask_unless_unanswered(unanswered, interface, next_hop, exchange)?;
neighbor::learn_neighbor(interface, next_hop, mac);
Ok(mac)
}
fn ask_unless_unanswered(
unanswered: &Mutex<UnansweredNeighbors>,
interface: &str,
next_hop: IpAddr,
exchange: impl FnOnce() -> Result<MacAddr, SendError>,
) -> Result<MacAddr, SendError> {
let record = || {
unanswered
.lock()
.map_err(|_| poisoned("unanswered neighbours"))
};
if record()?.is_fresh(
interface,
next_hop,
Instant::now(),
NEIGHBOR_UNREACHABLE_TTL,
) {
return Err(unanswered_neighbor(next_hop, interface));
}
let outcome = exchange();
match &outcome {
Ok(_) => record()?.clear(interface, next_hop),
Err(SendError::Unresolved(_)) => record()?.note(interface, next_hop, Instant::now()),
Err(_) => {}
}
outcome
}
fn unanswered_neighbor(next_hop: IpAddr, interface: &str) -> SendError {
SendError::Unresolved(format!(
"{next_hop} did not answer address resolution on {interface}"
))
}
fn spoofed_source_mac(interface: MacAddr, spoofed: Option<crate::model::mac::MacAddr>) -> MacAddr {
match spoofed {
Some(mac) => {
let octets: [u8; 6] = mac.into();
MacAddr::new(
octets[0], octets[1], octets[2], octets[3], octets[4], octets[5],
)
}
None => interface,
}
}
fn parse_arp_reply(frame: &[u8], target: Ipv4Addr) -> Option<MacAddr> {
let eth = EthernetPacket::new(frame)?;
if eth.get_ethertype() != EtherTypes::Arp {
return None;
}
let arp = ArpPacket::new(eth.payload())?;
if arp.get_operation() == ArpOperations::Reply && arp.get_sender_proto_addr() == target {
Some(arp.get_sender_hw_addr())
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use pnet_packet::arp::{ArpHardwareTypes, ArpOperations, MutableArpPacket};
use pnet_packet::ethernet::MutableEthernetPacket;
fn arp_reply(sender_ip: Ipv4Addr, sender_mac: MacAddr) -> Vec<u8> {
let mut buf = vec![0u8; 42];
{
let mut eth = MutableEthernetPacket::new(&mut buf[..14]).unwrap();
eth.set_ethertype(EtherTypes::Arp);
eth.set_source(sender_mac);
eth.set_destination(MacAddr::broadcast());
}
{
let mut a = MutableArpPacket::new(&mut buf[14..]).unwrap();
a.set_hardware_type(ArpHardwareTypes::Ethernet);
a.set_protocol_type(EtherTypes::Ipv4);
a.set_hw_addr_len(6);
a.set_proto_addr_len(4);
a.set_operation(ArpOperations::Reply);
a.set_sender_hw_addr(sender_mac);
a.set_sender_proto_addr(sender_ip);
a.set_target_proto_addr(Ipv4Addr::new(192, 0, 2, 50));
}
buf
}
const QUIET_READ: Duration = Duration::from_millis(5);
struct LossyNeighbour {
reply: Vec<u8>,
answers: u32,
sent_at: Vec<Duration>,
replied: bool,
epoch: Instant,
clock: Duration,
}
impl LossyNeighbour {
fn answering(answers: u32, ip: Ipv4Addr, mac: MacAddr) -> Self {
Self {
reply: arp_reply(ip, mac),
answers,
sent_at: Vec::new(),
replied: false,
epoch: Instant::now(),
clock: Duration::ZERO,
}
}
fn requests(&self) -> u32 {
self.sent_at.len() as u32
}
}
impl FrameSink for LossyNeighbour {
fn send_frame(&mut self, _frame: &[u8]) -> Result<(), String> {
self.sent_at.push(self.clock);
Ok(())
}
}
impl ResolutionLink for LossyNeighbour {
fn next_frame(&mut self) -> Option<&[u8]> {
if self.requests() >= self.answers && !self.replied {
self.replied = true;
return Some(&self.reply);
}
self.clock += QUIET_READ;
None
}
fn now(&self) -> Instant {
self.epoch + self.clock
}
}
#[test]
fn a_neighbour_answering_only_a_later_request_is_resolved() {
let ip = Ipv4Addr::new(192, 0, 2, 200);
let mac = MacAddr::new(0x02, 0, 0, 0, 0, 0xc8);
for answers in 2..=ARP_REQUESTS.max(2) {
let mut neighbour = LossyNeighbour::answering(answers, ip, mac);
let resolved = resolve_over(&mut neighbour, b"request", ip);
assert_eq!(resolved, Ok(Some(mac)), "answering request {answers}");
assert_eq!(neighbour.requests(), answers, "and asked no further");
}
}
#[test]
fn a_neighbour_that_never_answers_costs_one_timeout_across_every_request() {
let ip = Ipv4Addr::new(192, 0, 2, 200);
let mut neighbour =
LossyNeighbour::answering(u32::MAX, ip, MacAddr::new(0x02, 0, 0, 0, 0, 1));
assert_eq!(resolve_over(&mut neighbour, b"request", ip), Ok(None));
let spacing = ARP_TIMEOUT / ARP_REQUESTS;
let scheduled: Vec<Duration> = (0..ARP_REQUESTS).map(|n| spacing * n).collect();
assert_eq!(
neighbour.sent_at.len(),
scheduled.len(),
"every request sent"
);
for (sent, due) in neighbour.sent_at.iter().zip(&scheduled) {
assert!(
*sent >= *due && *sent < *due + QUIET_READ,
"a request due at {due:?} went out at {sent:?}"
);
}
assert!(
neighbour.clock >= ARP_TIMEOUT && neighbour.clock < ARP_TIMEOUT + QUIET_READ,
"a dead neighbour cost {:?}",
neighbour.clock
);
}
#[test]
fn parses_matching_arp_reply() {
let ip = Ipv4Addr::new(192, 0, 2, 200);
let mac = MacAddr::new(0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01);
assert_eq!(parse_arp_reply(&arp_reply(ip, mac), ip), Some(mac));
}
#[test]
fn a_spoofed_source_mac_replaces_the_interface_and_converts_faithfully() {
let interface = MacAddr::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);
assert_eq!(spoofed_source_mac(interface, None), interface);
let spoofed = crate::model::mac::MacAddr::new(0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01);
assert_eq!(
spoofed_source_mac(interface, Some(spoofed)),
MacAddr::new(0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01)
);
}
#[test]
fn ignores_arp_reply_from_other_ip() {
let mac = MacAddr::new(0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01);
let reply = arp_reply(Ipv4Addr::new(192, 0, 2, 201), mac);
assert_eq!(parse_arp_reply(&reply, Ipv4Addr::new(192, 0, 2, 200)), None);
}
#[test]
fn ignores_non_arp_frame() {
let mut buf = vec![0u8; 42];
let mut eth = MutableEthernetPacket::new(&mut buf).unwrap();
eth.set_ethertype(EtherTypes::Ipv4);
assert_eq!(parse_arp_reply(&buf, Ipv4Addr::new(192, 0, 2, 200)), None);
}
const DEAD: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 200));
#[test]
fn a_neighbour_that_never_answers_is_asked_once() {
let mut unanswered = UnansweredNeighbors::default();
let t0 = Instant::now();
let ttl = NEIGHBOR_UNREACHABLE_TTL;
assert!(!unanswered.is_fresh("en0", DEAD, t0, ttl));
unanswered.note("en0", DEAD, t0);
assert!(unanswered.is_fresh("en0", DEAD, t0, ttl));
assert!(unanswered.is_fresh("en0", DEAD, t0 + Duration::from_secs(1), ttl));
}
#[test]
fn a_failure_is_remembered_per_next_hop_and_interface() {
let mut unanswered = UnansweredNeighbors::default();
let t0 = Instant::now();
let ttl = NEIGHBOR_UNREACHABLE_TTL;
let other: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 201));
unanswered.note("en0", DEAD, t0);
assert!(
!unanswered.is_fresh("en0", other, t0, ttl),
"a different next hop"
);
assert!(
!unanswered.is_fresh("en1", DEAD, t0, ttl),
"the same next hop, another link"
);
}
#[test]
fn an_aged_out_failure_is_asked_again() {
let mut unanswered = UnansweredNeighbors::default();
let t0 = Instant::now();
let ttl = NEIGHBOR_UNREACHABLE_TTL;
unanswered.note("en0", DEAD, t0);
assert!(
!unanswered.is_fresh("en0", DEAD, t0 + ttl, ttl),
"at the horizon"
);
assert!(
!unanswered.is_fresh("en0", DEAD, t0 + ttl + Duration::from_secs(1), ttl),
"past it"
);
}
#[test]
fn a_dead_neighbour_is_resolved_once_and_refused_after() {
let unanswered = Mutex::new(UnansweredNeighbors::default());
let exchanges = std::cell::Cell::new(0);
let timing_out = || {
exchanges.set(exchanges.get() + 1);
Err(unanswered_neighbor(DEAD, "en0"))
};
for _ in 0..5 {
assert!(matches!(
ask_unless_unanswered(&unanswered, "en0", DEAD, timing_out),
Err(SendError::Unresolved(_))
));
}
assert_eq!(exchanges.get(), 1, "one exchange for five probes");
}
#[test]
fn only_an_unanswered_exchange_is_remembered() {
let unanswered = Mutex::new(UnansweredNeighbors::default());
let mac = MacAddr::new(0x02, 0, 0, 0, 0, 0x20);
let exchanges = std::cell::Cell::new(0);
let count = |result: Result<MacAddr, SendError>| {
exchanges.set(exchanges.get() + 1);
result
};
let refused = || count(Err(SendError::Refused("no channel".into())));
assert!(ask_unless_unanswered(&unanswered, "en0", DEAD, refused).is_err());
assert!(ask_unless_unanswered(&unanswered, "en0", DEAD, refused).is_err());
assert_eq!(exchanges.get(), 2, "a refusal is asked again");
let expired = Instant::now()
.checked_sub(NEIGHBOR_UNREACHABLE_TTL + Duration::from_secs(1))
.expect("a monotonic clock that has run past the window");
unanswered.lock().unwrap().note("en0", DEAD, expired);
let answering = || count(Ok(mac));
assert_eq!(
ask_unless_unanswered(&unanswered, "en0", DEAD, answering).ok(),
Some(mac)
);
assert!(
unanswered.lock().unwrap().seen.is_empty(),
"an answer leaves no record behind"
);
}
#[test]
fn a_neighbour_a_sweep_heard_is_not_asked_again() {
let interface = "test-heard0";
let mac = MacAddr::new(0x02, 0, 0, 0, 0, 0x31);
let unanswered = Mutex::new(UnansweredNeighbors::default());
unanswered
.lock()
.unwrap()
.note(interface, DEAD, Instant::now());
neighbor::learn_neighbor(interface, DEAD, mac);
let exchanges = std::cell::Cell::new(0);
let resolved = recall_or_ask(&unanswered, interface, DEAD, || {
exchanges.set(exchanges.get() + 1);
Err(unanswered_neighbor(DEAD, interface))
});
assert_eq!(resolved.ok(), Some(mac));
assert_eq!(exchanges.get(), 0, "the heard address was asked for again");
}
#[test]
fn a_resolution_one_sender_ran_serves_the_next() {
let interface = "test-heard1";
let mac = MacAddr::new(0x02, 0, 0, 0, 0, 0x32);
let exchanges = std::cell::Cell::new(0);
let answering = || {
exchanges.set(exchanges.get() + 1);
Ok(mac)
};
let first = Mutex::new(UnansweredNeighbors::default());
let second = Mutex::new(UnansweredNeighbors::default());
assert_eq!(
recall_or_ask(&first, interface, DEAD, answering).ok(),
Some(mac)
);
assert_eq!(
recall_or_ask(&second, interface, DEAD, answering).ok(),
Some(mac)
);
assert_eq!(exchanges.get(), 1, "one exchange for two senders");
}
#[test]
fn a_neighbour_that_answers_is_no_longer_skipped() {
let mut unanswered = UnansweredNeighbors::default();
let t0 = Instant::now();
let ttl = NEIGHBOR_UNREACHABLE_TTL;
unanswered.note("en0", DEAD, t0);
assert!(unanswered.is_fresh("en0", DEAD, t0, ttl));
unanswered.clear("en0", DEAD);
assert!(!unanswered.is_fresh("en0", DEAD, t0, ttl));
}
}