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
|
use libc::{NF_ACCEPT, NF_DROP};
use rustables_macros::nfnetlink_struct;
use crate::error::{DecodeError, QueryError};
use crate::nlmsg::{NfNetlinkAttribute, NfNetlinkDeserializable, NfNetlinkObject};
use crate::sys::{
NFTA_CHAIN_FLAGS, NFTA_CHAIN_HOOK, NFTA_CHAIN_NAME, NFTA_CHAIN_POLICY, NFTA_CHAIN_TABLE,
NFTA_CHAIN_TYPE, NFTA_CHAIN_USERDATA, NFTA_HOOK_HOOKNUM, NFTA_HOOK_PRIORITY, NFT_MSG_DELCHAIN,
NFT_MSG_NEWCHAIN,
};
use crate::{ProtocolFamily, Table};
use std::fmt::Debug;
pub type ChainPriority = i32;
/// The netfilter event hooks a chain can register for.
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[repr(i32)]
pub enum HookClass {
/// Hook into the pre-routing stage of netfilter. Corresponds to `NF_INET_PRE_ROUTING`.
PreRouting = libc::NF_INET_PRE_ROUTING,
/// Hook into the input stage of netfilter. Corresponds to `NF_INET_LOCAL_IN`.
In = libc::NF_INET_LOCAL_IN,
/// Hook into the forward stage of netfilter. Corresponds to `NF_INET_FORWARD`.
Forward = libc::NF_INET_FORWARD,
/// Hook into the output stage of netfilter. Corresponds to `NF_INET_LOCAL_OUT`.
Out = libc::NF_INET_LOCAL_OUT,
/// Hook into the post-routing stage of netfilter. Corresponds to `NF_INET_POST_ROUTING`.
PostRouting = libc::NF_INET_POST_ROUTING,
}
#[derive(Clone, PartialEq, Eq, Default, Debug)]
#[nfnetlink_struct(nested = true)]
pub struct Hook {
/// Define the action netfilter will apply to packets processed by this chain, but that did not match any rules in it.
#[field(NFTA_HOOK_HOOKNUM)]
class: u32,
#[field(NFTA_HOOK_PRIORITY)]
priority: u32,
}
impl Hook {
pub fn new(class: HookClass, priority: ChainPriority) -> Self {
Hook::default()
.with_class(class as u32)
.with_priority(priority as u32)
}
}
/// A chain policy. Decides what to do with a packet that was processed by the chain but did not
/// match any rules.
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[repr(i32)]
pub enum ChainPolicy {
/// Accept the packet.
Accept = NF_ACCEPT,
/// Drop the packet.
Drop = NF_DROP,
}
impl NfNetlinkAttribute for ChainPolicy {
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 ChainPolicy {
fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
let (v, remaining_data) = i32::deserialize(buf)?;
Ok((
match v {
NF_ACCEPT => ChainPolicy::Accept,
NF_DROP => ChainPolicy::Accept,
_ => return Err(DecodeError::UnknownChainPolicy),
},
remaining_data,
))
}
}
/// Base chain type.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ChainType {
/// Used to filter packets.
/// Supported protocols: ip, ip6, inet, arp, and bridge tables.
Filter,
/// Used to reroute packets if IP headers or packet marks are modified.
/// Supported protocols: ip, and ip6 tables.
Route,
/// Used to perform NAT.
/// Supported protocols: ip, and ip6 tables.
Nat,
}
impl ChainType {
fn as_str(&self) -> &'static str {
match *self {
ChainType::Filter => "filter",
ChainType::Route => "route",
ChainType::Nat => "nat",
}
}
}
impl NfNetlinkAttribute for ChainType {
fn get_size(&self) -> usize {
self.as_str().len()
}
unsafe fn write_payload(&self, addr: *mut u8) {
self.as_str().to_string().write_payload(addr);
}
}
impl NfNetlinkDeserializable for ChainType {
fn deserialize(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
let (s, remaining_data) = String::deserialize(buf)?;
Ok((
match s.as_str() {
"filter" => ChainType::Filter,
"route" => ChainType::Route,
"nat" => ChainType::Nat,
_ => return Err(DecodeError::UnknownChainType),
},
remaining_data,
))
}
}
/// Abstraction over an nftable chain. Chains reside inside [`Table`]s and they hold [`Rule`]s.
///
/// [`Table`]: struct.Table.html
/// [`Rule`]: struct.Rule.html
#[derive(PartialEq, Eq, Default, Debug)]
#[nfnetlink_struct(derive_deserialize = false)]
pub struct Chain {
family: ProtocolFamily,
#[field(NFTA_CHAIN_TABLE)]
table: String,
#[field(NFTA_CHAIN_NAME)]
name: String,
#[field(NFTA_CHAIN_HOOK)]
hook: Hook,
#[field(NFTA_CHAIN_POLICY)]
policy: ChainPolicy,
#[field(NFTA_CHAIN_TYPE, name_in_functions = "type")]
chain_type: ChainType,
#[field(NFTA_CHAIN_FLAGS)]
flags: u32,
#[field(NFTA_CHAIN_USERDATA)]
userdata: Vec<u8>,
}
impl Chain {
/// Creates a new chain instance inside the given [`Table`].
///
/// [`Table`]: struct.Table.html
pub fn new(table: &Table) -> Chain {
let mut chain = Chain::default();
chain.family = table.get_family();
if let Some(table_name) = table.get_name() {
chain.set_table(table_name);
}
chain
}
}
impl NfNetlinkObject for Chain {
const MSG_TYPE_ADD: u32 = NFT_MSG_NEWCHAIN;
const MSG_TYPE_DEL: u32 = NFT_MSG_DELCHAIN;
fn get_family(&self) -> ProtocolFamily {
self.family
}
fn set_family(&mut self, family: ProtocolFamily) {
self.family = family;
}
}
pub fn list_chains_for_table(table: &Table) -> Result<Vec<Chain>, QueryError> {
let mut result = Vec::new();
crate::query::list_objects_with_data(
libc::NFT_MSG_GETCHAIN as u16,
&|chain: Chain, (table, chains): &mut (&Table, &mut Vec<Chain>)| {
if chain.get_table() == table.get_name() {
chains.push(chain);
} else {
info!(
"Ignoring chain {:?} because it doesn't map the table {:?}",
chain.get_name(),
table.get_name()
);
}
Ok(())
},
None,
&mut (&table, &mut result),
)?;
Ok(result)
}
|