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