1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: MIT

mod config;
pub use config::*;

mod stats;
pub use stats::*;

use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{self, DefaultNla, NlaBuffer},
    parsers::{parse_string, parse_u32, parse_u64},
    traits::Parseable,
    DecodeError,
};

use crate::constants::*;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum Nla {
    Unspec(Vec<u8>),
    // FIXME: parse this nla
    Parms(Vec<u8>),
    Name(String),
    Threshold1(u32),
    Threshold2(u32),
    Threshold3(u32),
    Config(Vec<u8>),
    Stats(Vec<u8>),
    GcInterval(u64),
    Other(DefaultNla),
}

impl nla::Nla for Nla {
    #[rustfmt::skip]
    fn value_len(&self) -> usize {
        use self::Nla::*;
        match *self {
            Unspec(ref bytes) | Parms(ref bytes) | Config(ref bytes) | Stats(ref bytes)=> bytes.len(),
            // strings: +1 because we need to append a nul byte
            Name(ref s) => s.len() + 1,
            Threshold1(_) | Threshold2(_) | Threshold3(_) => 4,
            GcInterval(_) => 8,
            Other(ref attr) => attr.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        use self::Nla::*;
        match *self {
            Unspec(ref bytes) | Parms(ref bytes) | Config(ref bytes)
            | Stats(ref bytes) => buffer.copy_from_slice(bytes.as_slice()),
            Name(ref string) => {
                buffer[..string.len()].copy_from_slice(string.as_bytes());
                buffer[string.len()] = 0;
            }
            GcInterval(ref value) => NativeEndian::write_u64(buffer, *value),
            Threshold1(ref value)
            | Threshold2(ref value)
            | Threshold3(ref value) => NativeEndian::write_u32(buffer, *value),
            Other(ref attr) => attr.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        use self::Nla::*;
        match *self {
            Unspec(_) => NDTA_UNSPEC,
            Name(_) => NDTA_NAME,
            Config(_) => NDTA_CONFIG,
            Stats(_) => NDTA_STATS,
            Parms(_) => NDTA_PARMS,
            GcInterval(_) => NDTA_GC_INTERVAL,
            Threshold1(_) => NDTA_THRESH1,
            Threshold2(_) => NDTA_THRESH2,
            Threshold3(_) => NDTA_THRESH3,
            Other(ref attr) => attr.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Nla {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        use self::Nla::*;

        let payload = buf.value();
        Ok(match buf.kind() {
            NDTA_UNSPEC => Unspec(payload.to_vec()),
            NDTA_NAME => {
                Name(parse_string(payload).context("invalid NDTA_NAME value")?)
            }
            NDTA_CONFIG => Config(payload.to_vec()),
            NDTA_STATS => Stats(payload.to_vec()),
            NDTA_PARMS => Parms(payload.to_vec()),
            NDTA_GC_INTERVAL => GcInterval(
                parse_u64(payload).context("invalid NDTA_GC_INTERVAL value")?,
            ),
            NDTA_THRESH1 => Threshold1(
                parse_u32(payload).context("invalid NDTA_THRESH1 value")?,
            ),
            NDTA_THRESH2 => Threshold2(
                parse_u32(payload).context("invalid NDTA_THRESH2 value")?,
            ),
            NDTA_THRESH3 => Threshold3(
                parse_u32(payload).context("invalid NDTA_THRESH3 value")?,
            ),
            kind => Other(
                DefaultNla::parse(buf)
                    .context(format!("unknown NLA type {kind}"))?,
            ),
        })
    }
}