netlink_packet_route/tc/actions/
mirror.rs

1// SPDX-License-Identifier: MIT
2
3/// Mirror action
4///
5/// The mirred action allows packet mirroring (copying) or
6/// redirecting (stealing) the packet it receives. Mirroring is what
7/// is sometimes referred to as Switch Port Analyzer (SPAN) and is
8/// commonly used to analyze and/or debug flows.
9use netlink_packet_utils::{
10    DecodeError,
11    nla::{DefaultNla, Nla, NlaBuffer},
12    traits::{Emitable, Parseable},
13};
14
15use super::{TcActionGeneric, TcActionGenericBuffer};
16
17/// Traffic control action used to mirror or redirect packets.
18#[derive(Debug, PartialEq, Eq, Clone)]
19#[non_exhaustive]
20pub struct TcActionMirror {}
21impl TcActionMirror {
22    /// The `TcActionAttribute::Kind` of this action.
23    pub const KIND: &'static str = "mirred";
24}
25
26const TCA_MIRRED_TM: u16 = 1;
27const TCA_MIRRED_PARMS: u16 = 2;
28
29/// Options for the `TcActionMirror` action.
30#[derive(Debug, PartialEq, Eq, Clone)]
31#[non_exhaustive]
32pub enum TcActionMirrorOption {
33    /// TODO: document this after we make it something better than `Vec<u8>`
34    Tm(Vec<u8>),
35    /// Parameters for the mirred action.
36    Parms(TcMirror),
37    /// Other attributes unknown at the time of writing.
38    Other(DefaultNla),
39}
40
41impl Nla for TcActionMirrorOption {
42    fn value_len(&self) -> usize {
43        match self {
44            Self::Tm(bytes) => bytes.len(),
45            Self::Parms(_) => TC_MIRRED_BUF_LEN,
46            Self::Other(attr) => attr.value_len(),
47        }
48    }
49
50    fn emit_value(&self, buffer: &mut [u8]) {
51        match self {
52            Self::Tm(bytes) => buffer.copy_from_slice(bytes.as_slice()),
53            Self::Parms(p) => p.emit(buffer),
54            Self::Other(attr) => attr.emit_value(buffer),
55        }
56    }
57    fn kind(&self) -> u16 {
58        match self {
59            Self::Tm(_) => TCA_MIRRED_TM,
60            Self::Parms(_) => TCA_MIRRED_PARMS,
61            Self::Other(nla) => nla.kind(),
62        }
63    }
64}
65
66impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for TcActionMirrorOption {
67    type Error = DecodeError;
68    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
69        let payload = buf.value();
70        Ok(match buf.kind() {
71            TCA_MIRRED_TM => Self::Tm(payload.to_vec()),
72            TCA_MIRRED_PARMS => Self::Parms(TcMirror::parse(&TcMirrorBuffer::new(payload)?)?),
73            _ => Self::Other(DefaultNla::parse(buf)?),
74        })
75    }
76}
77
78const TC_MIRRED_BUF_LEN: usize = TcActionGeneric::BUF_LEN + 8;
79
80/// Parameters for the mirred action.
81#[derive(Debug, PartialEq, Eq, Clone, Default)]
82#[non_exhaustive]
83pub struct TcMirror {
84    /// Generic action parameters.
85    pub generic: TcActionGeneric,
86    /// Describes how the packet be mirrored or redirected.
87    pub eaction: TcMirrorActionType,
88    /// Interface index to mirror or redirect to.
89    pub ifindex: u32,
90}
91
92// kernel struct `tc_mirred`
93buffer!(TcMirrorBuffer(TC_MIRRED_BUF_LEN) {
94    generic: (slice, 0..20),
95    eaction: (i32, 20..24),
96    ifindex: (u32, 24..28),
97});
98
99impl Emitable for TcMirror {
100    fn buffer_len(&self) -> usize {
101        TC_MIRRED_BUF_LEN
102    }
103
104    fn emit(&self, buffer: &mut [u8]) {
105        let mut packet = TcMirrorBuffer::new_unchecked(buffer);
106        self.generic.emit(packet.generic_mut());
107        packet.set_eaction(self.eaction.into());
108        packet.set_ifindex(self.ifindex);
109    }
110}
111
112impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcMirrorBuffer<&'a T>> for TcMirror {
113    type Error = DecodeError;
114    fn parse(buf: &TcMirrorBuffer<&T>) -> Result<Self, DecodeError> {
115        Ok(Self {
116            generic: TcActionGeneric::parse(&TcActionGenericBuffer::new(buf.generic())?)?,
117            eaction: buf.eaction().into(),
118            ifindex: buf.ifindex(),
119        })
120    }
121}
122
123const TCA_EGRESS_REDIR: i32 = 1;
124const TCA_EGRESS_MIRROR: i32 = 2;
125const TCA_INGRESS_REDIR: i32 = 3;
126const TCA_INGRESS_MIRROR: i32 = 4;
127
128/// Type of mirroring or redirecting action.
129#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
130#[non_exhaustive]
131pub enum TcMirrorActionType {
132    #[default]
133    /// Redirect to the egress pipeline.
134    EgressRedir,
135    /// Mirror to the egress pipeline.
136    EgressMirror,
137    /// Redirect to the ingress pipeline.
138    IngressRedir,
139    /// Mirror to the ingress pipeline.
140    IngressMirror,
141    /// Other action type unknown at the time of writing.
142    Other(i32),
143}
144
145impl From<i32> for TcMirrorActionType {
146    fn from(d: i32) -> Self {
147        match d {
148            TCA_EGRESS_REDIR => Self::EgressRedir,
149            TCA_EGRESS_MIRROR => Self::EgressMirror,
150            TCA_INGRESS_REDIR => Self::IngressRedir,
151            TCA_INGRESS_MIRROR => Self::IngressMirror,
152            _ => Self::Other(d),
153        }
154    }
155}
156
157impl From<TcMirrorActionType> for i32 {
158    fn from(v: TcMirrorActionType) -> i32 {
159        match v {
160            TcMirrorActionType::EgressRedir => TCA_EGRESS_REDIR,
161            TcMirrorActionType::EgressMirror => TCA_EGRESS_MIRROR,
162            TcMirrorActionType::IngressRedir => TCA_INGRESS_REDIR,
163            TcMirrorActionType::IngressMirror => TCA_INGRESS_MIRROR,
164            TcMirrorActionType::Other(d) => d,
165        }
166    }
167}