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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use std::net::IpAddr;
use crate::rule::{
Bitwise, Cmp, CmpOp, Expr, Meta, MetaProperty, Payload, PayloadBase, PayloadOp, Register,
Verdict,
};
/// High-level builder for rule expressions.
#[derive(Clone, Debug, Default)]
pub struct RuleBuilder {
ipv4: bool,
ipv6: bool,
ip_src_addr: Option<(IpAddr, u8)>,
ip_dst_addr: Option<(IpAddr, u8)>,
ether_saddr: Option<[u8; 6]>,
ether_daddr: Option<[u8; 6]>,
verdict: Option<Verdict>,
}
impl RuleBuilder {
pub fn new() -> Self {
Self::default()
}
/// Adds a filter matching if ethernet frame contains the given source address.
///
/// This is the equivalent of `ether saddr <addr>`.
pub fn with_ether_saddr(mut self, addr: [u8; 6]) -> Self {
self.ether_saddr = Some(addr);
self
}
/// Adds a filter matching if the ethernet frame contains the given destination address.
///
/// This is the equivalent of `ether daddr <addr>`.
pub fn with_ether_daddr(mut self, addr: [u8; 6]) -> Self {
self.ether_daddr = Some(addr);
self
}
/// Adds a filter matching if the packet contains an IPv4 packet.
pub fn with_ipv4(mut self) -> Self {
self.ipv4 = true;
self
}
/// Adds a filter matching if the packet contains an IPv6 packet.
pub fn with_ipv6(mut self) -> Self {
self.ipv6 = true;
self
}
/// Add a filter matching if the packet contains the given source address.
///
/// This is the equivalent of `ip saddr <addr>` or `ip6 saddr <addr>`.
pub fn with_ip_saddr(self, addr: IpAddr) -> Self {
match addr {
IpAddr::V4(_) => self.with_ip_saddr_prefix(addr, 32),
IpAddr::V6(_) => self.with_ip_saddr_prefix(addr, 128),
}
}
/// Adds a filter matching if the given `prefix` bits match the source address of the packet.
///
/// This is the equivalent of `ip saddr <addr>` or `ip6 saddr <addr>` where `<addr>` is in CIDR
/// notation (e.g. `127.0.0.0/8`).
///
/// # Panics
///
/// - If the given [`IpAddr`] is an IPv4 address: panics if `prefix > 32`.
/// - If the given [`IpAddr`] is an IPv6 address: panics if `prefix > 128`.
pub fn with_ip_saddr_prefix(mut self, addr: IpAddr, prefix: u8) -> Self {
self.ip_src_addr = Some((addr, prefix));
match addr {
IpAddr::V4(_) => {
debug_assert!(prefix <= 32);
self.ipv4 = true;
}
IpAddr::V6(_) => {
debug_assert!(prefix <= 128);
self.ipv6 = true;
}
}
self
}
/// Adds a filter matching if the packet contains the given destination address.
///
/// This is the equivalent of `ip daddr <addr>` or `ip6a addr <addr>`.
pub fn with_ip_daddr(self, addr: IpAddr) -> Self {
match addr {
IpAddr::V4(_) => self.with_ip_daddr_prefix(addr, 32),
IpAddr::V6(_) => self.with_ip_daddr_prefix(addr, 128),
}
}
/// Adds a filter matching if the given `prefix` bits match the destination address of the
/// packet.
///
/// This is the equivalent of `ip saddr <addr>` or `ip6 saddr <addr>` where `<addr>` is in CIDR
/// notation (e.g. `127.0.0.0/8`).
///
/// # Panics
///
/// - If the given [`IpAddr`] is an IPv4 address: panics if `prefix > 32`.
/// - If the given [`IpAddr`] is an IPv6 address: panics if `prefix > 128`.
pub fn with_ip_daddr_prefix(mut self, addr: IpAddr, prefix: u8) -> Self {
self.ip_dst_addr = Some((addr, prefix));
match addr {
IpAddr::V4(_) => {
debug_assert!(prefix <= 32);
self.ipv4 = true;
}
IpAddr::V6(_) => {
debug_assert!(prefix <= 128);
self.ipv6 = true;
}
}
self
}
/// Sets the [`Verdict`] applied to all packets matching the filters.
///
/// Note that the verdict is always applied at the very end. If you add additional
/// filters after calling `with_verdict` the verdict will be placed after those filters.
/// This is in contrast to `nftables`, which allows to place filters after a verdict.
pub fn with_verdict(mut self, verdict: Verdict) -> Self {
self.verdict = Some(verdict);
self
}
pub fn build(&self) -> Vec<Expr> {
let mut exprs = Vec::new();
if self.ether_saddr.is_some() || self.ether_daddr.is_some() {
exprs.extend([
Expr::Meta(Meta {
key: MetaProperty::IIfType,
register: Register::Reg1,
src_register: false,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: libc::ARPHRD_ETHER.to_ne_bytes().to_vec(),
}),
]);
if let Some(addr) = self.ether_saddr {
exprs.extend([
Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::LinkLayer,
offset: 6,
len: 6,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_vec(),
}),
]);
}
if let Some(addr) = self.ether_daddr {
exprs.extend([
Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::LinkLayer,
offset: 0,
len: 6,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_vec(),
}),
]);
}
}
if self.ipv4 {
exprs.extend([
Expr::Meta(Meta {
key: MetaProperty::NfProto,
register: Register::Reg1,
src_register: false,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: vec![libc::NFPROTO_IPV4 as u8],
}),
]);
}
if self.ipv6 {
exprs.extend([
Expr::Meta(Meta {
key: MetaProperty::NfProto,
register: Register::Reg1,
src_register: false,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: vec![libc::NFPROTO_IPV6 as u8],
}),
]);
}
match self.ip_src_addr {
Some((IpAddr::V4(addr), prefix)) => {
exprs.push(Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::Network,
offset: 12,
len: 4,
}));
let mut addr = addr.to_bits();
if prefix != 32 {
let mask = ((1_u32 << prefix) - 1).reverse_bits();
addr &= mask;
exprs.push(Expr::Bitwise(Bitwise {
src_register: Register::Reg1,
dst_register: Register::Reg1,
mask: mask.to_be_bytes().to_vec(),
xor: Vec::new(),
len: 4,
}));
}
exprs.extend([Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_be_bytes().to_vec(),
})]);
}
Some((IpAddr::V6(addr), prefix)) => {
exprs.push(Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::Network,
offset: 8,
len: 16,
}));
let mut addr = addr.to_bits();
if prefix != 128 {
let mask = ((1_u128 << prefix) - 1).reverse_bits();
addr &= mask;
exprs.push(Expr::Bitwise(Bitwise {
src_register: Register::Reg1,
dst_register: Register::Reg1,
mask: mask.to_be_bytes().to_vec(),
xor: Vec::new(),
len: 16,
}));
}
exprs.push(Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_be_bytes().to_vec(),
}));
}
None => {}
}
match self.ip_dst_addr {
Some((IpAddr::V4(addr), prefix)) => {
exprs.push(Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::Network,
offset: 16,
len: 4,
}));
let mut addr = addr.to_bits();
if prefix != 32 {
let mask = ((1_u32 << prefix) - 1).reverse_bits();
addr &= mask;
exprs.push(Expr::Bitwise(Bitwise {
src_register: Register::Reg1,
dst_register: Register::Reg1,
mask: mask.to_be_bytes().to_vec(),
xor: Vec::new(),
len: 4,
}));
}
exprs.extend([Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_be_bytes().to_vec(),
})]);
}
Some((IpAddr::V6(addr), prefix)) => {
exprs.push(Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::Network,
offset: 24,
len: 16,
}));
let mut addr = addr.to_bits();
if prefix != 128 {
let mask = ((1_u128 << prefix) - 1).reverse_bits();
addr &= mask;
exprs.push(Expr::Bitwise(Bitwise {
src_register: Register::Reg1,
dst_register: Register::Reg1,
mask: mask.to_be_bytes().to_vec(),
xor: Vec::new(),
len: 16,
}));
}
exprs.push(Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: addr.to_be_bytes().to_vec(),
}));
}
None => {}
}
if let Some(verdict) = self.verdict.clone() {
exprs.push(Expr::Verdict(verdict));
}
exprs
}
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr};
use crate::rule::builder::RuleBuilder;
use crate::rule::{
Bitwise, Cmp, CmpOp, Expr, Meta, MetaProperty, Payload, PayloadBase, PayloadOp, Register,
};
#[test]
fn build_ipv4_with_prefix() {
let exprs = RuleBuilder::new()
.with_ip_saddr_prefix(IpAddr::V4(Ipv4Addr::new(10, 20, 255, 40)), 19)
.build();
assert_eq!(
exprs,
[
Expr::Meta(Meta {
key: MetaProperty::NfProto,
register: Register::Reg1,
src_register: false,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: vec![2],
}),
Expr::Payload(Payload {
op: PayloadOp::Load(Register::Reg1),
base: PayloadBase::Network,
offset: 12,
len: 4,
}),
Expr::Bitwise(Bitwise {
src_register: Register::Reg1,
dst_register: Register::Reg1,
mask: vec![0b1111_1111, 0b1111_1111, 0b1110_0000, 0b0000_0000],
xor: vec![],
len: 4,
}),
Expr::Cmp(Cmp {
op: CmpOp::Equal,
register: Register::Reg1,
data: vec![10, 20, 224, 0],
}),
]
);
}
}