use crate::network::oui::OuiLookup;
use crate::network::types::NdpNeighbor;
use std::net::{IpAddr, Ipv6Addr};
pub fn extract_neighbor(
icmpv6_data: &[u8],
src_ip: IpAddr,
oui_lookup: Option<&OuiLookup>,
) -> Option<NdpNeighbor> {
if icmpv6_data.len() < 8 || icmpv6_data[1] != 0 {
return None;
}
let (options_offset, wanted_option, ip) = match icmpv6_data[0] {
133 => (8, 1, src_ip),
134 => (16, 1, src_ip),
135 => (24, 1, src_ip),
136 => (24, 2, target_address(icmpv6_data)?),
137 => (40, 2, target_address(icmpv6_data)?),
_ => return None,
};
let mac = find_link_layer_option(icmpv6_data.get(options_offset..)?, wanted_option)?;
let vendor = oui_lookup.and_then(|oui| oui.lookup(&mac).map(String::from));
Some(NdpNeighbor { ip, mac, vendor })
}
fn target_address(icmpv6_data: &[u8]) -> Option<IpAddr> {
let bytes: [u8; 16] = icmpv6_data.get(8..24)?.try_into().ok()?;
Some(IpAddr::V6(Ipv6Addr::from(bytes)))
}
fn find_link_layer_option(mut options: &[u8], wanted: u8) -> Option<String> {
let mut mac = None;
while !options.is_empty() {
if options.len() < 8 {
return None;
}
let length = options[1] as usize * 8;
if length == 0 {
return None;
}
if mac.is_none() && options[0] == wanted && length == 8 {
mac = Some(crate::network::oui::format_mac(&options[2..8]));
}
options = options.get(length..)?;
}
mac
}
#[cfg(test)]
mod tests {
use super::*;
const MAC: [u8; 6] = [0x68, 0x5e, 0xdd, 0x09, 0x15, 0x5e];
fn ip(s: &str) -> IpAddr {
s.parse().unwrap()
}
fn option(kind: u8, payload: &[u8]) -> Vec<u8> {
let mut opt = vec![kind, ((payload.len() + 2).div_ceil(8)) as u8];
opt.extend_from_slice(payload);
while opt.len() % 8 != 0 {
opt.push(0);
}
opt
}
fn solicitation(target: &str) -> Vec<u8> {
let mut msg = vec![135, 0, 0, 0, 0, 0, 0, 0];
match ip(target) {
IpAddr::V6(v6) => msg.extend_from_slice(&v6.octets()),
IpAddr::V4(_) => unreachable!(),
}
msg
}
fn advertisement(target: &str) -> Vec<u8> {
let mut msg = vec![136, 0, 0, 0, 0x60, 0, 0, 0]; match ip(target) {
IpAddr::V6(v6) => msg.extend_from_slice(&v6.octets()),
IpAddr::V4(_) => unreachable!(),
}
msg
}
#[test]
fn solicitation_learns_source_from_its_option() {
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&option(1, &MAC));
let neighbor = extract_neighbor(&msg, ip("fe80::2"), None).unwrap();
assert_eq!(neighbor.ip, ip("fe80::2"));
assert_eq!(neighbor.mac, "68:5e:dd:09:15:5e");
assert_eq!(neighbor.vendor, None);
}
#[test]
fn advertisement_learns_target_address_not_source() {
let mut msg = advertisement("2001:db8::10");
msg.extend_from_slice(&option(2, &MAC));
let neighbor = extract_neighbor(&msg, ip("fe80::2"), None).unwrap();
assert_eq!(neighbor.ip, ip("2001:db8::10"));
assert_eq!(neighbor.mac, "68:5e:dd:09:15:5e");
}
#[test]
fn option_walk_skips_unrelated_options() {
let mut msg = vec![134, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
msg.extend_from_slice(&option(5, &[0, 0, 0, 0, 0x05, 0xdc]));
msg.extend_from_slice(&option(3, &[0u8; 30]));
msg.extend_from_slice(&option(1, &MAC));
let neighbor = extract_neighbor(&msg, ip("fe80::1"), None).unwrap();
assert_eq!(neighbor.ip, ip("fe80::1"));
assert_eq!(neighbor.mac, "68:5e:dd:09:15:5e");
}
#[test]
fn wrong_option_kind_is_not_learned() {
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&option(2, &MAC));
assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
}
#[test]
fn malformed_zero_length_option_aborts() {
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]); assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
}
#[test]
fn malformed_option_after_the_match_rejects_the_message() {
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&option(1, &MAC));
msg.extend_from_slice(&[5, 0, 0, 0, 0, 0, 0, 0]);
assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&option(1, &MAC));
msg.extend_from_slice(&[5, 4, 0, 0, 0, 0, 0, 0]); assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
let mut msg = solicitation("fe80::1");
msg.extend_from_slice(&option(1, &MAC));
msg.extend_from_slice(&[5, 1, 0]);
assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
}
#[test]
fn non_ndp_and_nonzero_code_are_ignored() {
assert!(extract_neighbor(&[128, 0, 0, 0, 0, 1, 0, 1], ip("fe80::2"), None).is_none());
let mut msg = solicitation("fe80::1");
msg[1] = 1;
msg.extend_from_slice(&option(1, &MAC));
assert!(extract_neighbor(&msg, ip("fe80::2"), None).is_none());
}
#[test]
fn truncated_advertisement_is_rejected() {
assert!(extract_neighbor(&[136, 0, 0, 0], ip("fe80::2"), None).is_none());
}
}