Skip to content

Commit

Permalink
Reset expiry of entries in the neighbor cache on packet reception
Browse files Browse the repository at this point in the history
  • Loading branch information
korken89 committed Aug 2, 2024
1 parent 53caf70 commit 5aee679
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/iface/interface/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl InterfaceInner {
EthernetProtocol::Ipv4 => {
let ipv4_packet = check!(Ipv4Packet::new_checked(eth_frame.payload()));

self.process_ipv4(sockets, meta, &ipv4_packet, fragments)
self.process_ipv4(sockets, self.now, meta, &ipv4_packet, fragments)
.map(EthernetPacket::Ip)
}
#[cfg(feature = "proto-ipv6")]
EthernetProtocol::Ipv6 => {
let ipv6_packet = check!(Ipv6Packet::new_checked(eth_frame.payload()));
self.process_ipv6(sockets, meta, &ipv6_packet)
self.process_ipv6(sockets, self.now, meta, &ipv6_packet)
.map(EthernetPacket::Ip)
}
// Drop all other traffic.
Expand Down
5 changes: 5 additions & 0 deletions src/iface/interface/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl InterfaceInner {
pub(super) fn process_ipv4<'a>(
&mut self,
sockets: &mut SocketSet,
timestamp: Instant,
meta: PacketMeta,
ipv4_packet: &Ipv4Packet<&'a [u8]>,
frag: &'a mut FragmentsBuffer,
Expand Down Expand Up @@ -196,6 +197,10 @@ impl InterfaceInner {
}
}

#[cfg(feature = "medium-ethernet")]
self.neighbor_cache
.reset_expiry_if_existing(IpAddress::Ipv4(ipv4_repr.src_addr), timestamp);

match ipv4_repr.next_header {
IpProtocol::Icmp => self.process_icmpv4(sockets, ipv4_repr, ip_payload),

Expand Down
5 changes: 5 additions & 0 deletions src/iface/interface/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl InterfaceInner {
pub(super) fn process_ipv6<'frame>(
&mut self,
sockets: &mut SocketSet,
timestamp: Instant,
meta: PacketMeta,
ipv6_packet: &Ipv6Packet<&'frame [u8]>,
) -> Option<Packet<'frame>> {
Expand Down Expand Up @@ -228,6 +229,10 @@ impl InterfaceInner {
#[cfg(not(feature = "socket-raw"))]
let handled_by_raw_socket = false;

#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
self.neighbor_cache
.reset_expiry_if_existing(IpAddress::Ipv6(ipv6_repr.src_addr), timestamp);

self.process_nxt_hdr(
sockets,
meta,
Expand Down
4 changes: 2 additions & 2 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,12 @@ impl InterfaceInner {
Ok(IpVersion::Ipv4) => {
let ipv4_packet = check!(Ipv4Packet::new_checked(ip_payload));

self.process_ipv4(sockets, meta, &ipv4_packet, frag)
self.process_ipv4(sockets, self.now, meta, &ipv4_packet, frag)
}
#[cfg(feature = "proto-ipv6")]
Ok(IpVersion::Ipv6) => {
let ipv6_packet = check!(Ipv6Packet::new_checked(ip_payload));
self.process_ipv6(sockets, meta, &ipv6_packet)
self.process_ipv6(sockets, self.now, meta, &ipv6_packet)
}
// Drop all other traffic.
_ => None,
Expand Down
7 changes: 6 additions & 1 deletion src/iface/interface/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ impl InterfaceInner {
}
};

self.process_ipv6(sockets, meta, &check!(Ipv6Packet::new_checked(payload)))
self.process_ipv6(
sockets,
self.now,
meta,
&check!(Ipv6Packet::new_checked(payload)),
)
}

#[cfg(feature = "proto-sixlowpan-fragmentation")]
Expand Down
10 changes: 10 additions & 0 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ fn test_no_icmp_no_unicast(#[case] medium: Medium) {
// ICMP error response when the destination address is a
// broadcast address

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv4(
&mut sockets,
timestamp,
PacketMeta::default(),
&frame,
&mut iface.fragments
Expand Down Expand Up @@ -148,9 +150,11 @@ fn test_icmp_error_no_payload(#[case] medium: Medium) {
// Ensure that the unknown protocol triggers an error response.
// And we correctly handle no payload.

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv4(
&mut sockets,
timestamp,
PacketMeta::default(),
&frame,
&mut iface.fragments
Expand Down Expand Up @@ -393,9 +397,11 @@ fn test_handle_ipv4_broadcast(#[case] medium: Medium) {
let expected_packet =
Packet::new_ipv4(expected_ipv4_repr, IpPayload::Icmpv4(expected_icmpv4_repr));

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv4(
&mut sockets,
timestamp,
PacketMeta::default(),
&frame,
&mut iface.fragments
Expand Down Expand Up @@ -824,9 +830,11 @@ fn test_raw_socket_no_reply(#[case] medium: Medium) {
Ipv4Packet::new_unchecked(&bytes[..])
};

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv4(
&mut sockets,
timestamp,
PacketMeta::default(),
&frame,
&mut iface.fragments
Expand Down Expand Up @@ -921,9 +929,11 @@ fn test_raw_socket_with_udp_socket(#[case] medium: Medium) {
Ipv4Packet::new_unchecked(&bytes[..])
};

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv4(
&mut sockets,
timestamp,
PacketMeta::default(),
&frame,
&mut iface.fragments
Expand Down
29 changes: 29 additions & 0 deletions src/iface/interface/tests/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ fn any_ip(#[case] medium: Medium) {
.unwrap();
});

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand All @@ -97,6 +99,7 @@ fn any_ip(#[case] medium: Medium) {
.inner
.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
)
Expand All @@ -121,9 +124,11 @@ fn multicast_source_address(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -169,9 +174,11 @@ fn hop_by_hop_skip_with_icmp(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -204,9 +211,11 @@ fn hop_by_hop_discard_with_icmp(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -258,9 +267,11 @@ fn hop_by_hop_discard_param_problem(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -315,9 +326,11 @@ fn hop_by_hop_discard_with_multicast(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -374,9 +387,11 @@ fn imcp_empty_echo_request(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -434,9 +449,11 @@ fn icmp_echo_request(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -481,9 +498,11 @@ fn icmp_echo_reply_as_input(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -529,9 +548,11 @@ fn unknown_proto_with_multicast_dst_address(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -578,9 +599,11 @@ fn unknown_proto(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -622,9 +645,11 @@ fn ndsic_neighbor_advertisement_ethernet(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -678,9 +703,11 @@ fn ndsic_neighbor_advertisement_ethernet_multicast_addr(#[case] medium: Medium)

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down Expand Up @@ -730,9 +757,11 @@ fn ndsic_neighbor_advertisement_ieee802154(#[case] medium: Medium) {

let (mut iface, mut sockets, _device) = setup(medium);

let timestamp = Instant::ZERO;
assert_eq!(
iface.inner.process_ipv6(
&mut sockets,
timestamp,
PacketMeta::default(),
&Ipv6Packet::new_checked(&data[..]).unwrap()
),
Expand Down
11 changes: 11 additions & 0 deletions src/iface/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ impl Cache {
}
}

pub fn reset_expiry_if_existing(&mut self, protocol_addr: IpAddress, timestamp: Instant) {
if let Some(Neighbor {
expires_at,
hardware_addr: _,
}) = self.storage.get_mut(&protocol_addr)
{
net_trace!("reseting expiry for {}", protocol_addr);
*expires_at = timestamp + Self::ENTRY_LIFETIME;
}
}

pub fn fill(
&mut self,
protocol_addr: IpAddress,
Expand Down

0 comments on commit 5aee679

Please sign in to comment.