use std::collections::HashMap;
use std::net::IpAddr;
use std::time::{Duration, Instant};
use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use crate::protocols::ip;
use super::LocalScannerError;
pub type PendingProbes = HashMap<IpAddr, Instant>;
pub enum ProtocolMatch {
Unhandled,
Handled { rtt: Option<Duration> },
}
pub trait DiscoveryProtocol: Send {
fn interpret(
&self,
frame: &EthernetPacket,
source: IpAddr,
pending: &mut PendingProbes,
) -> anyhow::Result<ProtocolMatch>;
}
pub struct ArpProtocol;
impl DiscoveryProtocol for ArpProtocol {
fn interpret(
&self,
frame: &EthernetPacket,
source: IpAddr,
pending: &mut PendingProbes,
) -> anyhow::Result<ProtocolMatch> {
if frame.get_ethertype() != EtherTypes::Arp {
return Ok(ProtocolMatch::Unhandled);
}
let rtt = pending.remove(&source).map(|sent_at| sent_at.elapsed());
Ok(ProtocolMatch::Handled { rtt })
}
}
pub struct Icmpv6Protocol;
impl DiscoveryProtocol for Icmpv6Protocol {
fn interpret(
&self,
frame: &EthernetPacket,
_source: IpAddr,
pending: &mut PendingProbes,
) -> anyhow::Result<ProtocolMatch> {
if frame.get_ethertype() != EtherTypes::Ipv6 {
return Ok(ProtocolMatch::Unhandled);
}
let destination = ip::get_ipv6_dst_addr_from_eth(frame)?;
if !destination.is_unicast_link_local() {
return Ok(ProtocolMatch::Unhandled);
}
let destination = IpAddr::V6(destination);
let sent_at = pending
.get(&destination)
.ok_or(LocalScannerError::UnmappedRttSource(destination))?;
Ok(ProtocolMatch::Handled {
rtt: Some(sent_at.elapsed()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocols::{arp, ethernet, ip as ip_protocol};
use pnet::datalink::MacAddr;
use pnet::packet::ip::IpNextHeaderProtocol;
use std::net::{Ipv4Addr, Ipv6Addr};
const LOCAL_MAC: MacAddr = MacAddr(0x02, 0x00, 0x00, 0x00, 0x00, 0x01);
const PEER_MAC: MacAddr = MacAddr(0x02, 0x00, 0x00, 0x00, 0x00, 0x02);
fn arp_reply_frame(sender_ip: Ipv4Addr) -> Vec<u8> {
arp::create_packet(&PEER_MAC, LOCAL_MAC, &sender_ip, Ipv4Addr::new(10, 0, 0, 1))
.expect("failed to build ARP test frame")
}
fn ipv6_frame(destination: Ipv6Addr) -> Vec<u8> {
let source = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2);
let eth_header = ethernet::make_header(
PEER_MAC,
LOCAL_MAC,
pnet::packet::ethernet::EtherTypes::Ipv6,
)
.expect("failed to build Ethernet header");
let ip_header = ip_protocol::create_ipv6_header(
source,
destination,
0,
IpNextHeaderProtocol::new(58), )
.expect("failed to build IPv6 header");
[eth_header, ip_header].concat()
}
#[test]
fn arp_protocol_ignores_non_arp_frames() {
let frame_bytes = ipv6_frame(Ipv6Addr::LOCALHOST);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
let result = ArpProtocol.interpret(&frame, Ipv6Addr::LOCALHOST.into(), &mut pending);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn arp_protocol_reports_rtt_for_a_pending_probe() {
let sender = Ipv4Addr::new(192, 168, 1, 50);
let frame_bytes = arp_reply_frame(sender);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
pending.insert(sender.into(), Instant::now());
let result = ArpProtocol
.interpret(&frame, sender.into(), &mut pending)
.unwrap();
match result {
ProtocolMatch::Handled { rtt } => assert!(rtt.is_some()),
ProtocolMatch::Unhandled => panic!("expected a handled ARP reply"),
}
assert!(
!pending.contains_key(&sender.into()),
"a consumed probe should be removed"
);
}
#[test]
fn arp_protocol_reports_no_rtt_without_a_pending_probe() {
let sender = Ipv4Addr::new(192, 168, 1, 50);
let frame_bytes = arp_reply_frame(sender);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
let result = ArpProtocol
.interpret(&frame, sender.into(), &mut pending)
.unwrap();
match result {
ProtocolMatch::Handled { rtt } => assert!(rtt.is_none()),
ProtocolMatch::Unhandled => panic!("unsolicited ARP traffic should still be handled"),
}
}
#[test]
fn icmpv6_protocol_ignores_non_ipv6_frames() {
let frame_bytes = arp_reply_frame(Ipv4Addr::new(10, 0, 0, 2));
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
let result =
Icmpv6Protocol.interpret(&frame, Ipv4Addr::new(10, 0, 0, 2).into(), &mut pending);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn icmpv6_protocol_ignores_traffic_not_addressed_to_a_link_local_unicast() {
let frame_bytes = ipv6_frame(Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 1)); let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
let result = Icmpv6Protocol.interpret(&frame, Ipv6Addr::LOCALHOST.into(), &mut pending);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn icmpv6_protocol_reports_rtt_without_consuming_the_probe() {
let own_link_local = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
let frame_bytes = ipv6_frame(own_link_local);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
pending.insert(own_link_local.into(), Instant::now());
for _ in 0..2 {
let result = Icmpv6Protocol
.interpret(&frame, own_link_local.into(), &mut pending)
.unwrap();
match result {
ProtocolMatch::Handled { rtt } => assert!(rtt.is_some()),
ProtocolMatch::Unhandled => panic!("expected a handled reply"),
}
}
assert!(
pending.contains_key(&own_link_local.into()),
"a multicast probe's timestamp isn't consumed by a single reply"
);
}
#[test]
fn icmpv6_protocol_errors_when_no_probe_was_ever_sent() {
let own_link_local = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
let frame_bytes = ipv6_frame(own_link_local);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let mut pending = PendingProbes::new();
let result = Icmpv6Protocol.interpret(&frame, own_link_local.into(), &mut pending);
assert!(result.is_err());
}
}