use std::net::IpAddr;
use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use crate::model::host::StatusProtocol;
use crate::protocols::{ip, ndp};
pub enum ProtocolMatch {
Unhandled,
Solicited(Option<IpAddr>),
AllNodes { identifier: u16, sequence: u16 },
}
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(None))
}
fn status_protocol(&self) -> StatusProtocol {
StatusProtocol::Arp
}
}
pub struct NdpProtocol;
impl DiscoveryProtocol for NdpProtocol {
fn interpret(&self, frame: &EthernetPacket) -> anyhow::Result<ProtocolMatch> {
match ndp::advertised_target(frame) {
Some(target) => Ok(ProtocolMatch::Solicited(Some(IpAddr::V6(target)))),
None => Ok(ProtocolMatch::Unhandled),
}
}
fn status_protocol(&self) -> StatusProtocol {
StatusProtocol::Ndp
}
}
pub struct Icmpv6EchoProtocol;
impl DiscoveryProtocol for Icmpv6EchoProtocol {
fn interpret(&self, frame: &EthernetPacket) -> anyhow::Result<ProtocolMatch> {
if frame.get_ethertype() != EtherTypes::Ipv6 {
return Ok(ProtocolMatch::Unhandled);
}
let destination = ip::ipv6_destination(frame)?;
if !destination.is_unicast_link_local() {
return Ok(ProtocolMatch::Unhandled);
}
match ip::icmpv6_echo_token(frame) {
Some((identifier, sequence)) => Ok(ProtocolMatch::AllNodes {
identifier,
sequence,
}),
None => Ok(ProtocolMatch::Unhandled),
}
}
fn status_protocol(&self) -> StatusProtocol {
StatusProtocol::IcmpEcho
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocols::{arp, ethernet, ip as ip_protocol};
use pnet::datalink::MacAddr;
use pnet::packet::icmpv6::echo_reply::{Icmpv6Codes, MutableEchoReplyPacket};
use pnet::packet::icmpv6::{Icmpv6Types, MutableIcmpv6Packet};
use pnet::packet::ip::{IpNextHeaderProtocol, IpNextHeaderProtocols};
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);
const ICMPV6_ECHO_LEN: usize = 8;
fn arp_reply_frame(sender_ip: Ipv4Addr) -> Vec<u8> {
arp::create_request(&PEER_MAC, &sender_ip, Ipv4Addr::new(10, 0, 0, 1))
}
fn ipv6_frame(destination: Ipv6Addr, protocol: IpNextHeaderProtocol, body: &[u8]) -> Vec<u8> {
let source = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2);
let eth_header = ethernet::create_header(
PEER_MAC,
LOCAL_MAC,
pnet::packet::ethernet::EtherTypes::Ipv6,
);
let ip_header = ip_protocol::create_ipv6_header(
source,
destination,
body.len() as u16,
protocol,
ip_protocol::HOP_LIMIT_ON_LINK,
);
[eth_header, ip_header, body.to_vec()].concat()
}
fn echo_reply_frame_with(destination: Ipv6Addr, identifier: u16, sequence: u16) -> Vec<u8> {
let mut body = vec![0u8; ICMPV6_ECHO_LEN];
{
let mut echo = MutableEchoReplyPacket::new(&mut body).expect("echo reply buffer");
echo.set_icmpv6_type(Icmpv6Types::EchoReply);
echo.set_icmpv6_code(Icmpv6Codes::NoCode);
echo.set_identifier(identifier);
echo.set_sequence_number(sequence);
}
ipv6_frame(destination, IpNextHeaderProtocols::Icmpv6, &body)
}
fn echo_reply_frame(destination: Ipv6Addr) -> Vec<u8> {
echo_reply_frame_with(destination, 0, 0)
}
fn neighbor_solicitation_body() -> Vec<u8> {
let mut body = vec![0u8; MutableIcmpv6Packet::minimum_packet_size() + 20];
{
let mut icmp = MutableIcmpv6Packet::new(&mut body).expect("icmpv6 buffer");
icmp.set_icmpv6_type(Icmpv6Types::NeighborSolicit);
}
body
}
#[test]
fn arp_protocol_ignores_non_arp_frames() {
let frame_bytes = echo_reply_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(None)));
}
#[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 = Icmpv6EchoProtocol.interpret(&frame);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn icmpv6_protocol_ignores_traffic_not_addressed_to_a_link_local_unicast() {
let frame_bytes = echo_reply_frame(Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 1)); let frame = EthernetPacket::new(&frame_bytes).unwrap();
let result = Icmpv6EchoProtocol.interpret(&frame);
assert!(matches!(result.unwrap(), ProtocolMatch::Unhandled));
}
#[test]
fn icmpv6_protocol_claims_an_echo_reply_for_the_all_nodes_probe() {
let frame_bytes = echo_reply_frame(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
let frame = EthernetPacket::new(&frame_bytes).unwrap();
for _ in 0..2 {
let result = Icmpv6EchoProtocol.interpret(&frame).unwrap();
assert!(matches!(result, ProtocolMatch::AllNodes { .. }));
}
assert_eq!(
Icmpv6EchoProtocol.status_protocol(),
StatusProtocol::IcmpEcho
);
}
#[test]
fn icmpv6_protocol_carries_the_echoed_token_back() {
let frame_bytes =
echo_reply_frame_with(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1), 0x5ac5, 2);
let frame = EthernetPacket::new(&frame_bytes).unwrap();
let result = Icmpv6EchoProtocol.interpret(&frame).unwrap();
assert!(matches!(
result,
ProtocolMatch::AllNodes {
identifier: 0x5ac5,
sequence: 2
}
));
}
#[test]
fn icmpv6_protocol_ignores_ipv6_traffic_that_is_not_an_echo_reply() {
let link_local = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
for frame_bytes in [
ipv6_frame(link_local, IpNextHeaderProtocols::Tcp, &[0u8; 20]),
ipv6_frame(
link_local,
IpNextHeaderProtocols::Icmpv6,
&neighbor_solicitation_body(),
),
ipv6_frame(link_local, IpNextHeaderProtocols::Icmpv6, &[]),
] {
let frame = EthernetPacket::new(&frame_bytes).unwrap();
assert!(matches!(
Icmpv6EchoProtocol.interpret(&frame).unwrap(),
ProtocolMatch::Unhandled
));
}
}
}