aboutsummaryrefslogtreecommitdiff
path: root/src/expr/mod.rs
blob: 63385e077714241391f6093adba138bb73e494b2 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! A module with all the nftables expressions that can be added to [`Rule`]s to build up how
//! they match against packets.
//!
//! [`Rule`]: struct.Rule.html

use std::fmt::Debug;
use std::mem::transmute;

use crate::nlmsg::NfNetlinkAttribute;
use crate::nlmsg::NfNetlinkDeserializable;
use crate::parser::pad_netlink_object;
use crate::parser::pad_netlink_object_with_variable_size;
use crate::parser::write_attribute;
use crate::parser::DecodeError;
use crate::sys::{self, nlattr};
use crate::sys::{
    NFTA_DATA_VALUE, NFTA_DATA_VERDICT, NFTA_EXPR_DATA, NFTA_EXPR_NAME, NLA_TYPE_MASK,
};
use rustables_macros::nfnetlink_struct;
use thiserror::Error;

mod bitwise;
pub use self::bitwise::*;

/*
mod cmp;
pub use self::cmp::*;

mod counter;
pub use self::counter::*;

pub mod ct;
pub use self::ct::*;
*/

mod immediate;
pub use self::immediate::*;

mod log;
pub use self::log::*;
/*

mod lookup;
pub use self::lookup::*;

mod masquerade;
pub use self::masquerade::*;
*/

mod meta;
pub use self::meta::*;

/*
mod nat;
pub use self::nat::*;

mod payload;
pub use self::payload::*;
*/

mod reject;
pub use self::reject::{IcmpCode, Reject, RejectType};

mod register;
pub use self::register::Register;

mod verdict;
pub use self::verdict::*;

/*

mod wrapper;
pub use self::wrapper::ExpressionWrapper;
*/

#[derive(Debug, Error)]
pub enum ExpressionError {
    #[error("The log prefix string is more than 127 characters long")]
    /// The log prefix string is more than 127 characters long
    TooLongLogPrefix,

    #[error("The expected expression type doesn't match the name of the raw expression")]
    /// The expected expression type doesn't match the name of the raw expression.
    InvalidExpressionKind,

    #[error("Deserializing the requested type isn't implemented yet")]
    /// Deserializing the requested type isn't implemented yet.
    NotImplemented,

    #[error("The expression value cannot be deserialized to the requested type")]
    /// The expression value cannot be deserialized to the requested type.
    InvalidValue,

    #[error("A pointer was null while a non-null pointer was expected")]
    /// A pointer was null while a non-null pointer was expected.
    NullPointer,

    #[error(
        "The size of a raw value was incoherent with the expected type of the deserialized value"
    )]
    /// The size of a raw value was incoherent with the expected type of the deserialized value/
    InvalidDataSize,
}

pub trait Expression {
    fn get_name() -> &'static str;
}

#[derive(Clone, PartialEq, Eq, Default, Debug)]
#[nfnetlink_struct(nested = true, derive_decoder = false)]
pub struct RawExpression {
    #[field(NFTA_EXPR_NAME)]
    name: String,
    #[field(NFTA_EXPR_DATA)]
    data: ExpressionVariant,
}

impl RawExpression {
    pub fn new<T>(expr: T) -> Self
    where
        T: Expression,
        ExpressionVariant: From<T>,
    {
        RawExpression::default()
            .with_name(T::get_name())
            .with_data(ExpressionVariant::from(expr))
    }
}

#[macro_export]
macro_rules! create_expr_variant {
    ($enum:ident $(, [$name:ident, $type:ty])+) => {
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub enum $enum {
            $(
                $name($type),
            )+
        }

        impl $crate::nlmsg::NfNetlinkAttribute for $enum {
            fn is_nested(&self) -> bool {
                true
            }

            fn get_size(&self) -> usize {
                match self {
                    $(
                        $enum::$name(val) => val.get_size(),
                    )+
                }
            }

            unsafe fn write_payload(&self, addr: *mut u8) {
                match self {
                    $(
                        $enum::$name(val) => val.write_payload(addr),
                    )+
                }
            }
        }

        $(
            impl From<$type> for $enum {
                fn from(val: $type) -> Self {
                    $enum::$name(val)
                }
            }
        )+

        impl $crate::nlmsg::AttributeDecoder for RawExpression {
            fn decode_attribute(
                &mut self,
                attr_type: u16,
                buf: &[u8],
            ) -> Result<(), $crate::parser::DecodeError> {
                debug!("Decoding attribute {} in an expression", attr_type);
                match attr_type {
                    x if x == sys::NFTA_EXPR_NAME => {
                        debug!("Calling {}::deserialize()", std::any::type_name::<String>());
                        let (val, remaining) = String::deserialize(buf)?;
                        if remaining.len() != 0 {
                            return Err($crate::parser::DecodeError::InvalidDataSize);
                        }
                        self.name = Some(val);
                        Ok(())
                    },
                    x if x == sys::NFTA_EXPR_DATA => {
                        // we can assume we have already the name parsed, as that's how we identify the
                        // type of expression
                        let name = self.name.as_ref()
                            .ok_or($crate::parser::DecodeError::MissingExpressionName)?;
                        match name {
                            $(
                                x if x == <$type>::get_name() => {
                                    debug!("Calling {}::deserialize()", std::any::type_name::<$type>());
                                    let (res, remaining) =  <$type>::deserialize(buf)?;
                                    if remaining.len() != 0 {
                                            return Err($crate::parser::DecodeError::InvalidDataSize);
                                    }
                                    self.data = Some(ExpressionVariant::from(res));
                                    Ok(())
                                },
                            )+
                            name => {
                                info!("Unrecognized expression '{}', generating an ExpressionRaw", name);
                                self.data = Some(ExpressionVariant::ExpressionRaw(ExpressionRaw::deserialize(buf)?.0));
                                Ok(())
                            }
                        }
                    },
                    _ => Err(DecodeError::UnsupportedAttributeType(attr_type)),
                }
            }
        }
    };
}

create_expr_variant!(
    ExpressionVariant,
    [Log, Log],
    [Immediate, Immediate],
    [Bitwise, Bitwise],
    [ExpressionRaw, ExpressionRaw],
    [Meta, Meta],
    [Reject, Reject]
);

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ExpressionList {
    exprs: Vec<RawExpression>,
}

impl ExpressionList {
    pub fn builder() -> Self {
        Self { exprs: Vec::new() }
    }

    /// Useful to add raw expressions because RawExpression cannot infer alone its type
    pub fn add_raw_expression(&mut self, e: RawExpression) {
        self.exprs.push(e);
    }

    pub fn add_expression<T>(&mut self, e: T)
    where
        T: Expression,
        ExpressionVariant: From<T>,
    {
        self.exprs.push(RawExpression::new(e));
    }

    pub fn with_expression<T>(mut self, e: T) -> Self
    where
        T: Expression,
        ExpressionVariant: From<T>,
    {
        self.add_expression(e);
        self
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a ExpressionVariant> {
        self.exprs.iter().map(|e| e.get_data().unwrap())
    }
}

impl NfNetlinkAttribute for ExpressionList {
    fn is_nested(&self) -> bool {
        true
    }

    fn get_size(&self) -> usize {
        // one nlattr LIST_ELEM per object
        self.exprs.iter().fold(0, |acc, item| {
            acc + item.get_size() + pad_netlink_object::<nlattr>()
        })
    }

    unsafe fn write_payload(&self, mut addr: *mut u8) {
        for item in &self.exprs {
            write_attribute(sys::NFTA_LIST_ELEM, item, addr);
            addr = addr.offset((pad_netlink_object::<nlattr>() + item.get_size()) as isize);
        }
    }
}

impl NfNetlinkDeserializable for ExpressionList {
    fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
        let mut exprs = Vec::new();

        let mut pos = 0;
        while buf.len() - pos > pad_netlink_object::<nlattr>() {
            let nlattr = unsafe { *transmute::<*const u8, *const nlattr>(buf[pos..].as_ptr()) };
            // ignore the byteorder and nested attributes
            let nla_type = nlattr.nla_type & NLA_TYPE_MASK as u16;

            if nla_type != sys::NFTA_LIST_ELEM {
                return Err(DecodeError::UnsupportedAttributeType(nla_type));
            }

            let (expr, remaining) = RawExpression::deserialize(
                &buf[pos + pad_netlink_object::<nlattr>()..pos + nlattr.nla_len as usize],
            )?;
            if remaining.len() != 0 {
                return Err(DecodeError::InvalidDataSize);
            }
            exprs.push(expr);

            pos += pad_netlink_object_with_variable_size(nlattr.nla_len as usize);
        }

        if pos != buf.len() {
            Err(DecodeError::InvalidDataSize)
        } else {
            Ok((Self { exprs }, &[]))
        }
    }
}

impl<T> From<Vec<T>> for ExpressionList
where
    ExpressionVariant: From<T>,
    T: Expression,
{
    fn from(v: Vec<T>) -> Self {
        ExpressionList {
            exprs: v.into_iter().map(RawExpression::new).collect(),
        }
    }
}

#[derive(Clone, PartialEq, Eq, Default, Debug)]
#[nfnetlink_struct(nested = true)]
pub struct ExpressionData {
    #[field(NFTA_DATA_VALUE)]
    value: Vec<u8>,
    #[field(NFTA_DATA_VERDICT)]
    verdict: VerdictAttribute,
}

// default type for expressions that we do not handle yet
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionRaw(Vec<u8>);

impl NfNetlinkAttribute for ExpressionRaw {
    fn get_size(&self) -> usize {
        self.0.get_size()
    }

    unsafe fn write_payload(&self, addr: *mut u8) {
        self.0.write_payload(addr);
    }
}

impl NfNetlinkDeserializable for ExpressionRaw {
    fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
        Ok((ExpressionRaw(buf.to_vec()), &[]))
    }
}

// Because we loose the name of the expression when parsing, this is the only expression
// where deserializing a message and then reserializing it is invalid
impl Expression for ExpressionRaw {
    fn get_name() -> &'static str {
        "unknown_expression"
    }
}