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
// SPDX-License-Identifier: MIT

use super::{buffer::RuleMessageBuffer, RULE_HEADER_LEN};
use crate::{
    constants::*,
    utils::{Emitable, Parseable},
    DecodeError,
};

bitflags! {
    pub struct RuleFlags: u32 {
        const FIB_RULE_PERMANENT = FIB_RULE_PERMANENT;
        const FIB_RULE_INVERT = FIB_RULE_INVERT;
        const FIB_RULE_UNRESOLVED = FIB_RULE_UNRESOLVED;
        const FIB_RULE_IIF_DETACHED = FIB_RULE_IIF_DETACHED;
        const FIB_RULE_DEV_DETACHED = FIB_RULE_DEV_DETACHED;
        const FIB_RULE_OIF_DETACHED = FIB_RULE_OIF_DETACHED;
        const FIB_RULE_FIND_SADDR = FIB_RULE_FIND_SADDR;
    }
}

impl Default for RuleFlags {
    fn default() -> Self {
        Self::empty()
    }
}

// see https://github.com/torvalds/linux/blob/master/include/uapi/linux/fib_rules.h
// see https://github.com/torvalds/linux/blob/master/include/net/fib_rules.h
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct RuleHeader {
    /// Address family: one of the `AF_*` constants.
    pub family: u8,
    pub dst_len: u8,
    pub src_len: u8,
    pub tos: u8,
    /// RT_TABLE_*
    pub table: u8,
    /// FR_ACT_*
    pub action: u8,
    /// fib rule flags
    pub flags: u32,
}

impl Emitable for RuleHeader {
    fn buffer_len(&self) -> usize {
        RULE_HEADER_LEN
    }

    fn emit(&self, buffer: &mut [u8]) {
        let mut packet = RuleMessageBuffer::new(buffer);
        packet.set_family(self.family);
        packet.set_dst_len(self.dst_len);
        packet.set_src_len(self.src_len);
        packet.set_flags(self.flags);
        packet.set_table(self.table);
        packet.set_tos(self.tos);
        packet.set_action(self.action);
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<RuleMessageBuffer<&'a T>> for RuleHeader {
    fn parse(buf: &RuleMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
        Ok(RuleHeader {
            family: buf.family(),
            dst_len: buf.dst_len(),
            src_len: buf.src_len(),
            tos: buf.tos(),
            table: buf.table(),
            action: buf.action(),
            flags: buf.flags(),
        })
    }
}