netlink_packet_route/rtnl/neighbour/nlas/
mod.rs

1// SPDX-License-Identifier: MIT
2
3mod cache_info;
4pub use self::cache_info::*;
5
6use anyhow::Context;
7use byteorder::{ByteOrder, NativeEndian};
8
9use crate::{
10    constants::*,
11    nlas::{self, DefaultNla, NlaBuffer},
12    parsers::{parse_u16, parse_u32},
13    traits::Parseable,
14    DecodeError,
15};
16
17#[derive(Debug, PartialEq, Eq, Clone)]
18pub enum Nla {
19    Unspec(Vec<u8>),
20    Destination(Vec<u8>),
21    LinkLocalAddress(Vec<u8>),
22    CacheInfo(Vec<u8>),
23    Probes(Vec<u8>),
24    Vlan(u16),
25    Port(Vec<u8>),
26    Vni(u32),
27    IfIndex(u32),
28    Master(Vec<u8>),
29    LinkNetNsId(Vec<u8>),
30    SourceVni(u32),
31    Other(DefaultNla),
32}
33
34impl nlas::Nla for Nla {
35    #[rustfmt::skip]
36    fn value_len(&self) -> usize {
37        use self::Nla::*;
38        match *self {
39            Unspec(ref bytes)
40            | Destination(ref bytes)
41            | LinkLocalAddress(ref bytes)
42            | Probes(ref bytes)
43            | Port(ref bytes)
44            | Master(ref bytes)
45            | CacheInfo(ref bytes)
46            | LinkNetNsId(ref bytes) => bytes.len(),
47            Vlan(_) => 2,
48            Vni(_)
49            | IfIndex(_)
50            | SourceVni(_) => 4,
51            Other(ref attr) => attr.value_len(),
52        }
53    }
54
55    #[rustfmt::skip]
56    fn emit_value(&self, buffer: &mut [u8]) {
57        use self::Nla::*;
58        match *self {
59            Unspec(ref bytes)
60            | Destination(ref bytes)
61            | LinkLocalAddress(ref bytes)
62            | Probes(ref bytes)
63            | Port(ref bytes)
64            | Master(ref bytes)
65            | CacheInfo(ref bytes)
66            | LinkNetNsId(ref bytes) => buffer.copy_from_slice(bytes.as_slice()),
67            Vlan(ref value) => NativeEndian::write_u16(buffer, *value),
68            Vni(ref value)
69            | IfIndex(ref value)
70            | SourceVni(ref value) => NativeEndian::write_u32(buffer, *value),
71            Other(ref attr) => attr.emit_value(buffer),
72        }
73    }
74
75    fn kind(&self) -> u16 {
76        use self::Nla::*;
77        match *self {
78            Unspec(_) => NDA_UNSPEC,
79            Destination(_) => NDA_DST,
80            LinkLocalAddress(_) => NDA_LLADDR,
81            CacheInfo(_) => NDA_CACHEINFO,
82            Probes(_) => NDA_PROBES,
83            Vlan(_) => NDA_VLAN,
84            Port(_) => NDA_PORT,
85            Vni(_) => NDA_VNI,
86            IfIndex(_) => NDA_IFINDEX,
87            Master(_) => NDA_MASTER,
88            LinkNetNsId(_) => NDA_LINK_NETNSID,
89            SourceVni(_) => NDA_SRC_VNI,
90            Other(ref nla) => nla.kind(),
91        }
92    }
93}
94
95impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Nla {
96    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
97        use self::Nla::*;
98
99        let payload = buf.value();
100        Ok(match buf.kind() {
101            NDA_UNSPEC => Unspec(payload.to_vec()),
102            NDA_DST => Destination(payload.to_vec()),
103            NDA_LLADDR => LinkLocalAddress(payload.to_vec()),
104            NDA_CACHEINFO => CacheInfo(payload.to_vec()),
105            NDA_PROBES => Probes(payload.to_vec()),
106            NDA_VLAN => Vlan(parse_u16(payload)?),
107            NDA_PORT => Port(payload.to_vec()),
108            NDA_VNI => Vni(parse_u32(payload)?),
109            NDA_IFINDEX => IfIndex(parse_u32(payload)?),
110            NDA_MASTER => Master(payload.to_vec()),
111            NDA_LINK_NETNSID => LinkNetNsId(payload.to_vec()),
112            NDA_SRC_VNI => SourceVni(parse_u32(payload)?),
113            _ => Other(DefaultNla::parse(buf).context("invalid link NLA value (unknown type)")?),
114        })
115    }
116}