netlink_packet_route/neighbour/
header.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_utils::nla::{NlaBuffer, NlaError, NlasIterator};
4use netlink_packet_utils::traits::{Emitable, Parseable};
5use netlink_packet_utils::DecodeError;
6
7use super::flags::NeighbourFlags;
8use super::NeighbourState;
9use crate::route::RouteType;
10use crate::AddressFamily;
11
12const NEIGHBOUR_HEADER_LEN: usize = 12;
13
14buffer!(NeighbourMessageBuffer(NEIGHBOUR_HEADER_LEN) {
15    family: (u8, 0),
16    ifindex: (u32, 4..8),
17    state: (u16, 8..10),
18    flags: (u8, 10),
19    kind: (u8, 11),
20    payload:(slice, NEIGHBOUR_HEADER_LEN..),
21});
22
23impl<'a, T: AsRef<[u8]> + ?Sized> NeighbourMessageBuffer<&'a T> {
24    pub fn attributes(&self) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, NlaError>> {
25        NlasIterator::new(self.payload())
26    }
27}
28
29/// Neighbour headers have the following structure:
30///
31/// ```no_rust
32/// 0                8                16              24               32
33/// +----------------+----------------+----------------+----------------+
34/// |     family     |                     padding                      |
35/// +----------------+----------------+----------------+----------------+
36/// |                             link index                            |
37/// +----------------+----------------+----------------+----------------+
38/// |              state              |     flags      |     ntype      |
39/// +----------------+----------------+----------------+----------------+
40/// ```
41///
42/// `NeighbourHeader` exposes all these fields.
43// Linux kernel struct `struct ndmsg`
44#[derive(Debug, PartialEq, Eq, Clone, Default)]
45pub struct NeighbourHeader {
46    pub family: AddressFamily,
47    pub ifindex: u32,
48    /// Neighbour cache entry state.
49    pub state: NeighbourState,
50    /// Neighbour cache entry flags. It should be set to a combination
51    /// of the `NTF_*` constants
52    pub flags: NeighbourFlags,
53    /// Neighbour cache entry type. It should be set to one of the
54    /// `NDA_*` constants.
55    pub kind: RouteType,
56}
57
58impl<T: AsRef<[u8]>> Parseable<NeighbourMessageBuffer<T>> for NeighbourHeader {
59    type Error = ();
60    fn parse(buf: &NeighbourMessageBuffer<T>) -> Result<Self, ()> {
61        Ok(Self {
62            family: buf.family().into(),
63            ifindex: buf.ifindex(),
64            state: buf.state().into(),
65            flags: NeighbourFlags::from_bits_retain(buf.flags()),
66            kind: buf.kind().into(),
67        })
68    }
69}
70
71impl Emitable for NeighbourHeader {
72    fn buffer_len(&self) -> usize {
73        NEIGHBOUR_HEADER_LEN
74    }
75
76    fn emit(&self, buffer: &mut [u8]) {
77        let mut packet = NeighbourMessageBuffer::new(buffer);
78        packet.set_family(self.family.into());
79        packet.set_ifindex(self.ifindex);
80        packet.set_state(self.state.into());
81        packet.set_flags(self.flags.bits());
82        packet.set_kind(self.kind.into());
83    }
84}