use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use crate::core::models::host::StatusProtocol;
use crate::protocols::ip;
pub enum ProtocolMatch {
Unhandled,
Solicited,
AllNodes,
}
pub trait DiscoveryProtocol: Send {
fn interpret(&self, frame: &EthernetPacket) -> anyhow::Result<ProtocolMatch>;
fn status_protocol(&self) -> StatusProtocol;
}
pub struct ArpProtocol;
impl DiscoveryProtocol for ArpProtocol {
fn interpret(&self, frame: &EthernetPacket) -> anyhow::Result<ProtocolMatch> {
if frame.get_ethertype() != EtherTypes::Arp {
return Ok(ProtocolMatch::Unhandled);
}
Ok(ProtocolMatch::Solicited)
}
fn status_protocol(&self) -> StatusProtocol {
StatusProtocol::Arp
}
}
pub struct Icmpv6Protocol;
impl DiscoveryProtocol for Icmpv6Protocol {
fn interpret(&self, frame: &EthernetPacket) -> 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);
}
Ok(ProtocolMatch::AllNodes)
}
fn status_protocol(&self) -> StatusProtocol {
StatusProtocol::Ndp
}
}
#[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 result = ArpProtocol.interpret(&frame);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn arp_protocol_claims_arp_frames_as_solicited() {
let frame_bytes = arp_reply_frame(Ipv4Addr::new(192, 168, 1, 50));
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let result = ArpProtocol.interpret(&frame).unwrap();
assert!(matches!(result, ProtocolMatch::Solicited));
}
#[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 result = Icmpv6Protocol.interpret(&frame);
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 result = Icmpv6Protocol.interpret(&frame);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn icmpv6_protocol_claims_link_local_traffic_for_the_all_nodes_probe() {
let frame_bytes = ipv6_frame(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
let frame = EthernetPacket::new(&frame_bytes).unwrap();
for _ in 0..2 {
let result = Icmpv6Protocol.interpret(&frame).unwrap();
assert!(matches!(result, ProtocolMatch::AllNodes));
}
}
}