netlink_packet_route/tc/filters/
matchall.rs

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

use crate::tc::{TcAction, TcError, TcHandle};
/// Matchall filter
///
/// Matches all packets and performs an action on them.
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::nla::{DefaultNla, Nla, NlaBuffer, NlasIterator};
use netlink_packet_utils::parsers::parse_u32;
use netlink_packet_utils::traits::{Emitable, Parseable};

const TCA_MATCHALL_CLASSID: u16 = 1;
const TCA_MATCHALL_ACT: u16 = 2;
const TCA_MATCHALL_FLAGS: u16 = 3;
const TCA_MATCHALL_PCNT: u16 = 4;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct TcFilterMatchAll {}
impl TcFilterMatchAll {
    pub const KIND: &'static str = "matchall";
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum TcFilterMatchAllOption {
    ClassId(TcHandle),
    Action(Vec<TcAction>),
    Pnct(Vec<u8>),
    Flags(u32),
    Other(DefaultNla),
}

impl Nla for TcFilterMatchAllOption {
    fn value_len(&self) -> usize {
        match self {
            Self::Pnct(b) => b.len(),
            Self::ClassId(_) => 4,
            Self::Flags(_) => 4,
            Self::Action(acts) => acts.as_slice().buffer_len(),
            Self::Other(attr) => attr.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Pnct(b) => buffer.copy_from_slice(b.as_slice()),
            Self::ClassId(i) => NativeEndian::write_u32(buffer, (*i).into()),
            Self::Flags(i) => NativeEndian::write_u32(buffer, *i),
            Self::Action(acts) => acts.as_slice().emit(buffer),
            Self::Other(attr) => attr.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::ClassId(_) => TCA_MATCHALL_CLASSID,
            Self::Action(_) => TCA_MATCHALL_ACT,
            Self::Pnct(_) => TCA_MATCHALL_PCNT,
            Self::Flags(_) => TCA_MATCHALL_FLAGS,
            Self::Other(attr) => attr.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for TcFilterMatchAllOption {
    type Error = TcError;
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, TcError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            TCA_MATCHALL_CLASSID => Self::ClassId(
                parse_u32(payload)
                    .map_err(|error| TcError::ParseFilterMatchallOption {
                        kind: "TCA_MATCHALL_UNSPEC",
                        error,
                    })?
                    .into(),
            ),
            TCA_MATCHALL_ACT => {
                let mut acts = vec![];
                for act in NlasIterator::new(payload) {
                    let act = act?;
                    acts.push(TcAction::parse(&act)?);
                }
                Self::Action(acts)
            }
            TCA_MATCHALL_PCNT => Self::Pnct(payload.to_vec()),
            TCA_MATCHALL_FLAGS => Self::Flags(parse_u32(payload).map_err(|error| {
                TcError::ParseFilterMatchallOption { kind: "TCA_MATCHALL_FLAGS", error }
            })?),
            kind => Self::Other(DefaultNla::parse(buf).map_err(|error| {
                TcError::UnknownFilterMatchAllOption { kind: kind.to_string(), error }
            })?),
        })
    }
}