diff options
author | Simon THOBY <git@nightmared.fr> | 2022-12-03 22:53:23 +0100 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2022-12-05 22:40:01 +0100 |
commit | edb440a952320ea4f021c1d7063ff6d5f2f13818 (patch) | |
tree | 5c18e7f1fabdcef8e140920ea75bfd0d0b400bd0 /src/expr | |
parent | 4b60b3cd41f5198c47a260ce69abf4c15b60ca92 (diff) |
Macros: introduce a macro to simplify enums
Diffstat (limited to 'src/expr')
-rw-r--r-- | src/expr/counter.rs | 43 | ||||
-rw-r--r-- | src/expr/log.rs | 2 | ||||
-rw-r--r-- | src/expr/meta.rs | 46 | ||||
-rw-r--r-- | src/expr/mod.rs | 5 | ||||
-rw-r--r-- | src/expr/register.rs | 33 | ||||
-rw-r--r-- | src/expr/reject.rs | 71 | ||||
-rw-r--r-- | src/expr/verdict.rs | 44 |
7 files changed, 34 insertions, 210 deletions
diff --git a/src/expr/counter.rs b/src/expr/counter.rs index 4732e85..d22fb8a 100644 --- a/src/expr/counter.rs +++ b/src/expr/counter.rs @@ -1,46 +1,21 @@ -use super::{DeserializationError, Expression, Rule}; +use rustables_macros::nfnetlink_struct; + +use super::Expression; use crate::sys; -use std::os::raw::c_char; /// A counter expression adds a counter to the rule that is incremented to count number of packets /// and number of bytes for all packets that have matched the rule. -#[derive(Debug, PartialEq)] +#[derive(Default, Clone, Debug, PartialEq, Eq)] +#[nfnetlink_struct] pub struct Counter { + #[field(sys::NFTA_COUNTER_BYTES)] pub nb_bytes: u64, + #[field(sys::NFTA_COUNTER_PACKETS)] pub nb_packets: u64, } -impl Counter { - pub fn new() -> Self { - Self { - nb_bytes: 0, - nb_packets: 0, - } - } -} - impl Expression for Counter { - fn get_raw_name() -> *const c_char { - b"counter\0" as *const _ as *const c_char - } - - fn from_expr(expr: *const sys::nftnl_expr) -> Result<Self, DeserializationError> { - unsafe { - let nb_bytes = sys::nftnl_expr_get_u64(expr, sys::NFTNL_EXPR_CTR_BYTES as u16); - let nb_packets = sys::nftnl_expr_get_u64(expr, sys::NFTNL_EXPR_CTR_PACKETS as u16); - Ok(Counter { - nb_bytes, - nb_packets, - }) - } - } - - fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { - unsafe { - let expr = try_alloc!(sys::nftnl_expr_alloc(Self::get_raw_name())); - sys::nftnl_expr_set_u64(expr, sys::NFTNL_EXPR_CTR_BYTES as u16, self.nb_bytes); - sys::nftnl_expr_set_u64(expr, sys::NFTNL_EXPR_CTR_PACKETS as u16, self.nb_packets); - expr - } + fn get_name() -> &'static str { + "counter" } } diff --git a/src/expr/log.rs b/src/expr/log.rs index 3c72257..80bb7a9 100644 --- a/src/expr/log.rs +++ b/src/expr/log.rs @@ -8,7 +8,7 @@ use crate::sys::{NFTA_LOG_GROUP, NFTA_LOG_PREFIX}; /// A Log expression will log all packets that match the rule. pub struct Log { #[field(NFTA_LOG_GROUP)] - group: u32, + group: u16, #[field(NFTA_LOG_PREFIX)] prefix: String, } diff --git a/src/expr/meta.rs b/src/expr/meta.rs index c4c1adb..79016bd 100644 --- a/src/expr/meta.rs +++ b/src/expr/meta.rs @@ -1,15 +1,11 @@ -use rustables_macros::nfnetlink_struct; +use rustables_macros::{nfnetlink_enum, nfnetlink_struct}; use super::{Expression, Register}; -use crate::{ - nlmsg::{NfNetlinkAttribute, NfNetlinkDeserializable}, - parser::DecodeError, - sys, -}; +use crate::sys; /// A meta expression refers to meta data associated with a packet. #[derive(Debug, Copy, Clone, Eq, PartialEq)] -#[repr(u32)] +#[nfnetlink_enum(u32)] #[non_exhaustive] pub enum MetaType { /// Packet ethertype protocol (skb->protocol), invalid in OUTPUT. @@ -42,42 +38,6 @@ pub enum MetaType { PRandom = sys::NFT_META_PRANDOM, } -impl NfNetlinkAttribute for MetaType { - fn get_size(&self) -> usize { - (*self as u32).get_size() - } - - unsafe fn write_payload(&self, addr: *mut u8) { - (*self as u32).write_payload(addr); - } -} - -impl NfNetlinkDeserializable for MetaType { - fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> { - let (v, remaining_data) = u32::deserialize(buf)?; - Ok(( - match v { - sys::NFT_META_PROTOCOL => Self::Protocol, - sys::NFT_META_MARK => Self::Mark, - sys::NFT_META_IIF => Self::Iif, - sys::NFT_META_OIF => Self::Oif, - sys::NFT_META_IIFNAME => Self::IifName, - sys::NFT_META_OIFNAME => Self::OifName, - sys::NFT_META_IFTYPE => Self::IifType, - sys::NFT_META_OIFTYPE => Self::OifType, - sys::NFT_META_SKUID => Self::SkUid, - sys::NFT_META_SKGID => Self::SkGid, - sys::NFT_META_NFPROTO => Self::NfProto, - sys::NFT_META_L4PROTO => Self::L4Proto, - sys::NFT_META_CGROUP => Self::Cgroup, - sys::NFT_META_PRANDOM => Self::PRandom, - value => return Err(DecodeError::UnknownMetaType(value)), - }, - remaining_data, - )) - } -} - #[derive(Clone, PartialEq, Eq, Default, Debug)] #[nfnetlink_struct] pub struct Meta { diff --git a/src/expr/mod.rs b/src/expr/mod.rs index 63385e0..d2cd917 100644 --- a/src/expr/mod.rs +++ b/src/expr/mod.rs @@ -25,9 +25,11 @@ pub use self::bitwise::*; /* mod cmp; pub use self::cmp::*; +*/ mod counter; pub use self::counter::*; +/* pub mod ct; pub use self::ct::*; @@ -222,7 +224,8 @@ create_expr_variant!( [Bitwise, Bitwise], [ExpressionRaw, ExpressionRaw], [Meta, Meta], - [Reject, Reject] + [Reject, Reject], + [Counter, Counter] ); #[derive(Debug, Clone, PartialEq, Eq, Default)] diff --git a/src/expr/register.rs b/src/expr/register.rs index def58a5..9cc1bee 100644 --- a/src/expr/register.rs +++ b/src/expr/register.rs @@ -1,15 +1,13 @@ use std::fmt::Debug; -use crate::{ - nlmsg::{NfNetlinkAttribute, NfNetlinkDeserializable}, - parser::DecodeError, - sys::{NFT_REG_1, NFT_REG_2, NFT_REG_3, NFT_REG_4, NFT_REG_VERDICT}, -}; +use rustables_macros::nfnetlink_enum; + +use crate::sys::{NFT_REG_1, NFT_REG_2, NFT_REG_3, NFT_REG_4, NFT_REG_VERDICT}; /// A netfilter data register. The expressions store and read data to and from these when /// evaluating rule statements. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -#[repr(u32)] +#[nfnetlink_enum(u32)] pub enum Register { Verdict = NFT_REG_VERDICT, Reg1 = NFT_REG_1, @@ -17,26 +15,3 @@ pub enum Register { Reg3 = NFT_REG_3, Reg4 = NFT_REG_4, } - -impl NfNetlinkAttribute for Register { - unsafe fn write_payload(&self, addr: *mut u8) { - (*self as u32).write_payload(addr); - } -} - -impl NfNetlinkDeserializable for Register { - fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), crate::parser::DecodeError> { - let (val, remaining) = u32::deserialize(buf)?; - Ok(( - match val { - NFT_REG_VERDICT => Self::Verdict, - NFT_REG_1 => Self::Reg1, - NFT_REG_2 => Self::Reg2, - NFT_REG_3 => Self::Reg3, - NFT_REG_4 => Self::Reg4, - _ => return Err(DecodeError::UnknownRegisterValue), - }, - remaining, - )) - } -} diff --git a/src/expr/reject.rs b/src/expr/reject.rs index 10b95ea..83fd843 100644 --- a/src/expr/reject.rs +++ b/src/expr/reject.rs @@ -1,10 +1,6 @@ -use rustables_macros::nfnetlink_struct; +use rustables_macros::{nfnetlink_enum, nfnetlink_struct}; -use crate::{ - nlmsg::{NfNetlinkAttribute, NfNetlinkDeserializable}, - parser::DecodeError, - sys, -}; +use crate::sys; use super::Expression; @@ -26,70 +22,19 @@ pub struct Reject { /// An ICMP reject code. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(u32)] +#[nfnetlink_enum(u32)] pub enum RejectType { IcmpUnreach = sys::NFT_REJECT_ICMP_UNREACH, TcpRst = sys::NFT_REJECT_TCP_RST, IcmpxUnreach = sys::NFT_REJECT_ICMPX_UNREACH, } -impl NfNetlinkDeserializable for RejectType { - fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> { - let (v, remaining_code) = u32::deserialize(buf)?; - Ok(( - match v { - sys::NFT_REJECT_ICMP_UNREACH => Self::IcmpUnreach, - sys::NFT_REJECT_TCP_RST => Self::TcpRst, - sys::NFT_REJECT_ICMPX_UNREACH => Self::IcmpxUnreach, - _ => return Err(DecodeError::UnknownRejectType(v)), - }, - remaining_code, - )) - } -} - -impl NfNetlinkAttribute for RejectType { - fn get_size(&self) -> usize { - (*self as u32).get_size() - } - - unsafe fn write_payload(&self, addr: *mut u8) { - (*self as u32).write_payload(addr); - } -} - /// An ICMP reject code. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(u8)] +#[nfnetlink_enum(u8)] pub enum IcmpCode { - NoRoute = sys::NFT_REJECT_ICMPX_NO_ROUTE as u8, - PortUnreach = sys::NFT_REJECT_ICMPX_PORT_UNREACH as u8, - HostUnreach = sys::NFT_REJECT_ICMPX_HOST_UNREACH as u8, - AdminProhibited = sys::NFT_REJECT_ICMPX_ADMIN_PROHIBITED as u8, -} - -impl NfNetlinkDeserializable for IcmpCode { - fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> { - let (value, remaining_code) = u8::deserialize(buf)?; - Ok(( - match value as u32 { - sys::NFT_REJECT_ICMPX_NO_ROUTE => Self::NoRoute, - sys::NFT_REJECT_ICMPX_PORT_UNREACH => Self::PortUnreach, - sys::NFT_REJECT_ICMPX_HOST_UNREACH => Self::HostUnreach, - sys::NFT_REJECT_ICMPX_ADMIN_PROHIBITED => Self::AdminProhibited, - _ => return Err(DecodeError::UnknownIcmpCode(value)), - }, - remaining_code, - )) - } -} - -impl NfNetlinkAttribute for IcmpCode { - fn get_size(&self) -> usize { - (*self as u8).get_size() - } - - unsafe fn write_payload(&self, addr: *mut u8) { - (*self as u8).write_payload(addr); - } + NoRoute = sys::NFT_REJECT_ICMPX_NO_ROUTE, + PortUnreach = sys::NFT_REJECT_ICMPX_PORT_UNREACH, + HostUnreach = sys::NFT_REJECT_ICMPX_HOST_UNREACH, + AdminProhibited = sys::NFT_REJECT_ICMPX_ADMIN_PROHIBITED, } diff --git a/src/expr/verdict.rs b/src/expr/verdict.rs index fc13f8a..c4facfb 100644 --- a/src/expr/verdict.rs +++ b/src/expr/verdict.rs @@ -1,20 +1,16 @@ use std::fmt::Debug; use libc::{NF_ACCEPT, NF_DROP, NF_QUEUE}; -use rustables_macros::nfnetlink_struct; +use rustables_macros::{nfnetlink_enum, nfnetlink_struct}; use super::{ExpressionData, Immediate, Register}; -use crate::{ - nlmsg::{NfNetlinkAttribute, NfNetlinkDeserializable}, - parser::DecodeError, - sys::{ - NFTA_VERDICT_CHAIN, NFTA_VERDICT_CHAIN_ID, NFTA_VERDICT_CODE, NFT_BREAK, NFT_CONTINUE, - NFT_GOTO, NFT_JUMP, NFT_RETURN, - }, +use crate::sys::{ + NFTA_VERDICT_CHAIN, NFTA_VERDICT_CHAIN_ID, NFTA_VERDICT_CODE, NFT_BREAK, NFT_CONTINUE, + NFT_GOTO, NFT_JUMP, NFT_RETURN, }; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -#[repr(i32)] +#[nfnetlink_enum(i32)] pub enum VerdictType { Drop = NF_DROP, Accept = NF_ACCEPT, @@ -26,36 +22,6 @@ pub enum VerdictType { Return = NFT_RETURN, } -impl NfNetlinkAttribute for VerdictType { - fn get_size(&self) -> usize { - (*self as i32).get_size() - } - - unsafe fn write_payload(&self, addr: *mut u8) { - (*self as i32).write_payload(addr); - } -} - -impl NfNetlinkDeserializable for VerdictType { - fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> { - let (v, remaining_data) = i32::deserialize(buf)?; - Ok(( - match v { - NF_DROP => VerdictType::Drop, - NF_ACCEPT => VerdictType::Accept, - NF_QUEUE => VerdictType::Queue, - NFT_CONTINUE => VerdictType::Continue, - NFT_BREAK => VerdictType::Break, - NFT_JUMP => VerdictType::Jump, - NFT_GOTO => VerdictType::Goto, - NFT_RETURN => VerdictType::Goto, - _ => return Err(DecodeError::UnknownExpressionVerdictType), - }, - remaining_data, - )) - } -} - #[derive(Clone, PartialEq, Eq, Default, Debug)] #[nfnetlink_struct(nested = true)] pub struct VerdictAttribute { |