-
Notifications
You must be signed in to change notification settings - Fork 328
/
linux.rs
643 lines (562 loc) · 23.7 KB
/
linux.rs
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use super::{FirewallArguments, FirewallPolicy, FirewallT};
use crate::tunnel;
use ipnetwork::IpNetwork;
use lazy_static::lazy_static;
use libc;
use nftnl::{
self,
expr::{self, Payload, Verdict},
nft_expr, table, Batch, Chain, FinalizedBatch, ProtoFamily, Rule, Table,
};
use std::{
env,
ffi::{CStr, CString},
io,
net::{IpAddr, Ipv4Addr},
};
use talpid_types::net::{Endpoint, TransportProtocol};
pub type Result<T> = std::result::Result<T, Error>;
/// Errors that can happen when interacting with Linux netfilter.
#[derive(err_derive::Error, Debug)]
#[error(no_from)]
pub enum Error {
/// Unable to open netlink socket to netfilter.
#[error(display = "Unable to open netlink socket to netfilter")]
NetlinkOpenError(#[error(source)] io::Error),
/// Unable to send netlink command to netfilter.
#[error(display = "Unable to send netlink command to netfilter")]
NetlinkSendError(#[error(source)] io::Error),
/// Error while reading from netlink socket.
#[error(display = "Error while reading from netlink socket")]
NetlinkRecvError(#[error(source)] io::Error),
/// Error while processing an incoming netlink message.
#[error(display = "Error while processing an incoming netlink message")]
ProcessNetlinkError(#[error(source)] io::Error),
/// Failed to verify that our tables are set. Probably means that
/// it's the host that does not support nftables properly.
#[error(display = "Failed to set firewall rules")]
NetfilterTableNotSetError,
/// Unable to translate network interface name into index.
#[error(
display = "Unable to translate network interface name \"{}\" into index",
_0
)]
LookupIfaceIndexError(String, #[error(source)] crate::linux::IfaceIndexLookupError),
}
lazy_static! {
/// TODO(linus): This crate is not supposed to be Mullvad-aware. So at some point this should be
/// replaced by allowing the table name to be configured from the public API of this crate.
static ref TABLE_NAME: CString = CString::new("mullvad").unwrap();
static ref IN_CHAIN_NAME: CString = CString::new("in").unwrap();
static ref OUT_CHAIN_NAME: CString = CString::new("out").unwrap();
/// Allows controlling whether firewall rules should have packet counters or not from an env
/// variable. Useful for debugging the rules.
static ref ADD_COUNTERS: bool = env::var("TALPID_FIREWALL_DEBUG")
.map(|v| v == "1")
.unwrap_or(false);
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
enum Direction {
In,
Out,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
enum End {
Src,
Dst,
}
/// The Linux implementation for the firewall and DNS.
pub struct Firewall {
table_name: CString,
}
impl FirewallT for Firewall {
type Error = Error;
fn new(_args: FirewallArguments) -> Result<Self> {
Ok(Firewall {
table_name: TABLE_NAME.clone(),
})
}
fn apply_policy(&mut self, policy: FirewallPolicy) -> Result<()> {
let table = Table::new(&self.table_name, ProtoFamily::Inet);
let batch = PolicyBatch::new(&table).finalize(&policy)?;
self.send_and_process(&batch)?;
self.verify_tables(&[&TABLE_NAME])
}
fn reset_policy(&mut self) -> Result<()> {
let table = Table::new(&self.table_name, ProtoFamily::Inet);
let batch = {
let mut batch = Batch::new();
// Our batch will add and remove the table even though the goal is just to remove it.
// This because only removing it throws a strange error if the table does not exist.
batch.add(&table, nftnl::MsgType::Add);
batch.add(&table, nftnl::MsgType::Del);
batch.finalize()
};
log::debug!("Removing table and chain from netfilter");
self.send_and_process(&batch)
}
}
impl Firewall {
fn send_and_process(&self, batch: &FinalizedBatch) -> Result<()> {
let socket = mnl::Socket::new(mnl::Bus::Netfilter).map_err(Error::NetlinkOpenError)?;
socket.send_all(batch).map_err(Error::NetlinkSendError)?;
let portid = socket.portid();
let mut buffer = vec![0; nftnl::nft_nlmsg_maxsize() as usize];
let seq = 0;
while let Some(message) = Self::socket_recv(&socket, &mut buffer[..])? {
match mnl::cb_run(message, seq, portid).map_err(Error::ProcessNetlinkError)? {
mnl::CbResult::Stop => {
log::trace!("cb_run STOP");
break;
}
mnl::CbResult::Ok => log::trace!("cb_run OK"),
};
}
Ok(())
}
fn verify_tables(&self, expected_tables: &[&CStr]) -> Result<()> {
let socket = mnl::Socket::new(mnl::Bus::Netfilter).map_err(Error::NetlinkOpenError)?;
let portid = socket.portid();
let seq = 0;
let get_tables_msg = table::get_tables_nlmsg(seq);
socket
.send(&get_tables_msg)
.map_err(Error::NetlinkSendError)?;
let mut table_set = std::collections::HashSet::new();
let mut msg_buffer = vec![0; nftnl::nft_nlmsg_maxsize() as usize];
while let Some(message) = Self::socket_recv(&socket, &mut msg_buffer)? {
match mnl::cb_run2(message, seq, portid, table::get_tables_cb, &mut table_set)
.map_err(Error::ProcessNetlinkError)?
{
mnl::CbResult::Stop => {
log::trace!("cb_run STOP");
break;
}
mnl::CbResult::Ok => log::trace!("cb_run OK"),
}
}
for expected_table in expected_tables {
if !table_set.contains(*expected_table) {
log::error!(
"Expected '{}' netfilter table to be set, but it is not",
expected_table.to_string_lossy()
);
return Err(Error::NetfilterTableNotSetError);
}
}
Ok(())
}
fn socket_recv<'a>(socket: &mnl::Socket, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>> {
let ret = socket.recv(buf).map_err(Error::NetlinkRecvError)?;
log::trace!("Read {} bytes from netlink", ret);
if ret > 0 {
Ok(Some(&buf[..ret]))
} else {
Ok(None)
}
}
}
struct PolicyBatch<'a> {
batch: Batch,
in_chain: Chain<'a>,
out_chain: Chain<'a>,
}
impl<'a> PolicyBatch<'a> {
/// Bootstrap a new nftnl message batch object and add the initial messages creating the
/// table and chains.
pub fn new(table: &'a Table) -> Self {
let mut batch = Batch::new();
let mut out_chain = Chain::new(&*OUT_CHAIN_NAME, table);
let mut in_chain = Chain::new(&*IN_CHAIN_NAME, table);
out_chain.set_hook(nftnl::Hook::Out, 0);
in_chain.set_hook(nftnl::Hook::In, 0);
out_chain.set_policy(nftnl::Policy::Drop);
in_chain.set_policy(nftnl::Policy::Drop);
// A little dance that will make sure the table exists, but is cleared.
batch.add(table, nftnl::MsgType::Add);
batch.add(table, nftnl::MsgType::Del);
batch.add(table, nftnl::MsgType::Add);
batch.add(&out_chain, nftnl::MsgType::Add);
batch.add(&in_chain, nftnl::MsgType::Add);
PolicyBatch {
batch,
in_chain,
out_chain,
}
}
/// Finalize the nftnl message batch by adding every firewall rule needed to satisfy the given
/// policy.
pub fn finalize(mut self, policy: &FirewallPolicy) -> Result<FinalizedBatch> {
self.add_loopback_rules()?;
self.add_dhcp_client_rules();
self.add_policy_specific_rules(policy)?;
Ok(self.batch.finalize())
}
fn add_loopback_rules(&mut self) -> Result<()> {
const LOOPBACK_IFACE_NAME: &str = "lo";
self.batch.add(
&allow_interface_rule(&self.out_chain, Direction::Out, LOOPBACK_IFACE_NAME)?,
nftnl::MsgType::Add,
);
self.batch.add(
&allow_interface_rule(&self.in_chain, Direction::In, LOOPBACK_IFACE_NAME)?,
nftnl::MsgType::Add,
);
Ok(())
}
fn add_dhcp_client_rules(&mut self) {
use self::TransportProtocol::Udp;
// Outgoing DHCPv4 request
{
let mut out_v4 = Rule::new(&self.out_chain);
check_port(&mut out_v4, Udp, End::Src, super::DHCPV4_CLIENT_PORT);
check_ip(&mut out_v4, End::Dst, IpAddr::V4(Ipv4Addr::BROADCAST));
check_port(&mut out_v4, Udp, End::Dst, super::DHCPV4_SERVER_PORT);
add_verdict(&mut out_v4, &Verdict::Accept);
self.batch.add(&out_v4, nftnl::MsgType::Add);
}
// Incoming DHCPv4 response
{
let mut in_v4 = Rule::new(&self.in_chain);
check_port(&mut in_v4, Udp, End::Src, super::DHCPV4_SERVER_PORT);
check_port(&mut in_v4, Udp, End::Dst, super::DHCPV4_CLIENT_PORT);
add_verdict(&mut in_v4, &Verdict::Accept);
self.batch.add(&in_v4, nftnl::MsgType::Add);
}
for dhcpv6_server in &*super::DHCPV6_SERVER_ADDRS {
let mut out_v6 = Rule::new(&self.out_chain);
check_net(&mut out_v6, End::Src, *super::IPV6_LINK_LOCAL);
check_port(&mut out_v6, Udp, End::Src, super::DHCPV6_CLIENT_PORT);
check_ip(&mut out_v6, End::Dst, *dhcpv6_server);
check_port(&mut out_v6, Udp, End::Dst, super::DHCPV6_SERVER_PORT);
add_verdict(&mut out_v6, &Verdict::Accept);
self.batch.add(&out_v6, nftnl::MsgType::Add);
}
{
let mut in_v6 = Rule::new(&self.in_chain);
check_net(&mut in_v6, End::Src, *super::IPV6_LINK_LOCAL);
check_port(&mut in_v6, Udp, End::Src, super::DHCPV6_SERVER_PORT);
check_net(&mut in_v6, End::Dst, *super::IPV6_LINK_LOCAL);
check_port(&mut in_v6, Udp, End::Dst, super::DHCPV6_CLIENT_PORT);
add_verdict(&mut in_v6, &Verdict::Accept);
self.batch.add(&in_v6, nftnl::MsgType::Add);
}
// Outgoing Router solicitation (part of NDP)
{
let mut rule = Rule::new(&self.out_chain);
check_ip(
&mut rule,
End::Dst,
*super::ROUTER_SOLICITATION_OUT_DST_ADDR,
);
rule.add_expr(&nft_expr!(meta l4proto));
rule.add_expr(&nft_expr!(cmp == libc::IPPROTO_ICMPV6 as u8));
rule.add_expr(&Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Type),
));
rule.add_expr(&nft_expr!(cmp == 133u8));
rule.add_expr(&nftnl::expr::Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Code),
));
rule.add_expr(&nft_expr!(cmp == 0u8));
add_verdict(&mut rule, &Verdict::Accept);
self.batch.add(&rule, nftnl::MsgType::Add);
}
// Incoming Router advertisement (part of NDP)
{
let mut rule = Rule::new(&self.in_chain);
check_net(&mut rule, End::Src, *super::IPV6_LINK_LOCAL);
rule.add_expr(&nft_expr!(meta l4proto));
rule.add_expr(&nft_expr!(cmp == libc::IPPROTO_ICMPV6 as u8));
rule.add_expr(&Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Type),
));
rule.add_expr(&nft_expr!(cmp == 134u8));
rule.add_expr(&nftnl::expr::Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Code),
));
rule.add_expr(&nft_expr!(cmp == 0u8));
add_verdict(&mut rule, &Verdict::Accept);
self.batch.add(&rule, nftnl::MsgType::Add);
}
// Incoming Redirect (part of NDP)
{
let mut rule = Rule::new(&self.in_chain);
check_net(&mut rule, End::Src, *super::IPV6_LINK_LOCAL);
rule.add_expr(&nft_expr!(meta l4proto));
rule.add_expr(&nft_expr!(cmp == libc::IPPROTO_ICMPV6 as u8));
rule.add_expr(&Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Type),
));
rule.add_expr(&nft_expr!(cmp == 137u8));
rule.add_expr(&nftnl::expr::Payload::Transport(
nftnl::expr::TransportHeaderField::Icmpv6(nftnl::expr::Icmpv6HeaderField::Code),
));
rule.add_expr(&nft_expr!(cmp == 0u8));
add_verdict(&mut rule, &Verdict::Accept);
self.batch.add(&rule, nftnl::MsgType::Add);
}
}
fn add_policy_specific_rules(&mut self, policy: &FirewallPolicy) -> Result<()> {
let allow_lan = match policy {
FirewallPolicy::Connecting {
peer_endpoint,
pingable_hosts,
allow_lan,
} => {
self.add_allow_icmp_pingable_hosts(&pingable_hosts);
self.add_allow_endpoint_rules(peer_endpoint);
*allow_lan
}
FirewallPolicy::Connected {
peer_endpoint,
tunnel,
allow_lan,
} => {
self.add_allow_endpoint_rules(peer_endpoint);
self.add_dns_rule(tunnel, TransportProtocol::Udp)?;
self.add_dns_rule(tunnel, TransportProtocol::Tcp)?;
self.add_allow_tunnel_rules(tunnel)?;
if *allow_lan {
self.add_block_cve_2019_14899(tunnel);
}
*allow_lan
}
FirewallPolicy::Blocked { allow_lan } => *allow_lan,
};
if allow_lan {
self.add_allow_lan_rules();
}
Ok(())
}
fn add_allow_endpoint_rules(&mut self, endpoint: &Endpoint) {
let mut in_rule = Rule::new(&self.in_chain);
check_endpoint(&mut in_rule, End::Src, endpoint);
in_rule.add_expr(&nft_expr!(ct state));
let allowed_states = nftnl::expr::ct::States::ESTABLISHED.bits();
in_rule.add_expr(&nft_expr!(bitwise mask allowed_states, xor 0u32));
in_rule.add_expr(&nft_expr!(cmp != 0u32));
add_verdict(&mut in_rule, &Verdict::Accept);
self.batch.add(&in_rule, nftnl::MsgType::Add);
let mut out_rule = Rule::new(&self.out_chain);
check_endpoint(&mut out_rule, End::Dst, endpoint);
add_verdict(&mut out_rule, &Verdict::Accept);
self.batch.add(&out_rule, nftnl::MsgType::Add);
}
fn add_allow_icmp_pingable_hosts(&mut self, pingable_hosts: &[IpAddr]) {
for host in pingable_hosts {
let icmp_proto = match &host {
IpAddr::V4(_) => libc::IPPROTO_ICMP as u8,
IpAddr::V6(_) => libc::IPPROTO_ICMPV6 as u8,
};
let mut out_rule = Rule::new(&self.out_chain);
check_ip(&mut out_rule, End::Dst, *host);
out_rule.add_expr(&nft_expr!(meta l4proto));
out_rule.add_expr(&nft_expr!(cmp == icmp_proto));
add_verdict(&mut out_rule, &Verdict::Accept);
self.batch.add(&out_rule, nftnl::MsgType::Add);
let mut in_rule = Rule::new(&self.in_chain);
check_ip(&mut in_rule, End::Src, *host);
in_rule.add_expr(&nft_expr!(meta l4proto));
in_rule.add_expr(&nft_expr!(cmp == icmp_proto));
add_verdict(&mut in_rule, &Verdict::Accept);
self.batch.add(&in_rule, nftnl::MsgType::Add);
}
}
fn add_dns_rule(
&mut self,
tunnel: &tunnel::TunnelMetadata,
protocol: TransportProtocol,
) -> Result<()> {
// allow DNS traffic to the tunnel gateway
self.add_allow_dns_rule(&tunnel.interface, protocol, tunnel.ipv4_gateway.into())?;
if let Some(ipv6_gateway) = tunnel.ipv6_gateway {
self.add_allow_dns_rule(&tunnel.interface, protocol, ipv6_gateway.into())?;
};
let mut block_rule = Rule::new(&self.out_chain);
check_port(&mut block_rule, protocol, End::Dst, 53);
add_verdict(&mut block_rule, &Verdict::Drop);
self.batch.add(&block_rule, nftnl::MsgType::Add);
Ok(())
}
fn add_allow_dns_rule(
&mut self,
interface: &str,
protocol: TransportProtocol,
host: IpAddr,
) -> Result<()> {
let mut allow_rule = Rule::new(&self.out_chain);
let daddr = match host {
IpAddr::V4(_) => nft_expr!(payload ipv4 daddr),
IpAddr::V6(_) => nft_expr!(payload ipv6 daddr),
};
check_iface(&mut allow_rule, Direction::Out, interface)?;
check_port(&mut allow_rule, protocol, End::Dst, 53);
check_l3proto(&mut allow_rule, host);
allow_rule.add_expr(&daddr);
allow_rule.add_expr(&nft_expr!(cmp == host));
add_verdict(&mut allow_rule, &Verdict::Accept);
self.batch.add(&allow_rule, nftnl::MsgType::Add);
Ok(())
}
fn add_allow_tunnel_rules(&mut self, tunnel: &tunnel::TunnelMetadata) -> Result<()> {
self.batch.add(
&allow_interface_rule(&self.out_chain, Direction::Out, &tunnel.interface[..])?,
nftnl::MsgType::Add,
);
self.batch.add(
&allow_interface_rule(&self.in_chain, Direction::In, &tunnel.interface[..])?,
nftnl::MsgType::Add,
);
Ok(())
}
/// Adds rules for stopping [CVE-2019-14899](https://seclists.org/oss-sec/2019/q4/122).
/// An attacker on the same local network as the VPN connected device could figure out
/// the tunnel IP the device used if the device was set to not filter reverse path (rp_filter.)
/// These rules stops all packets coming in to the tunnel IP. As such, these rules must come
/// after the rule allowing the tunnel, otherwise even the tunnel can't talk to that IP.
fn add_block_cve_2019_14899(&mut self, tunnel: &tunnel::TunnelMetadata) {
for tunnel_ip in &tunnel.ips {
let mut rule = Rule::new(&self.in_chain);
check_ip(&mut rule, End::Dst, *tunnel_ip);
add_verdict(&mut rule, &Verdict::Drop);
self.batch.add(&rule, nftnl::MsgType::Add);
}
}
fn add_allow_lan_rules(&mut self) {
// LAN -> LAN
for net in &*super::ALLOWED_LAN_NETS {
let mut out_rule = Rule::new(&self.out_chain);
check_net(&mut out_rule, End::Dst, *net);
add_verdict(&mut out_rule, &Verdict::Accept);
self.batch.add(&out_rule, nftnl::MsgType::Add);
let mut in_rule = Rule::new(&self.in_chain);
check_net(&mut in_rule, End::Src, *net);
add_verdict(&mut in_rule, &Verdict::Accept);
self.batch.add(&in_rule, nftnl::MsgType::Add);
}
// LAN -> Multicast
for net in &*super::ALLOWED_LAN_MULTICAST_NETS {
let mut rule = Rule::new(&self.out_chain);
check_net(&mut rule, End::Dst, *net);
add_verdict(&mut rule, &Verdict::Accept);
self.batch.add(&rule, nftnl::MsgType::Add);
}
self.add_dhcp_server_rules();
}
fn add_dhcp_server_rules(&mut self) {
use TransportProtocol::Udp;
// Outgoing DHCPv4 response
{
let mut out_v4 = Rule::new(&self.out_chain);
check_port(&mut out_v4, Udp, End::Src, super::DHCPV4_SERVER_PORT);
check_port(&mut out_v4, Udp, End::Dst, super::DHCPV4_CLIENT_PORT);
add_verdict(&mut out_v4, &Verdict::Accept);
self.batch.add(&out_v4, nftnl::MsgType::Add);
}
// Incoming DHCPv4 request
{
let mut in_v4 = Rule::new(&self.in_chain);
check_port(&mut in_v4, Udp, End::Src, super::DHCPV4_CLIENT_PORT);
check_endpoint(
&mut in_v4,
End::Dst,
&Endpoint::new(Ipv4Addr::BROADCAST, super::DHCPV4_SERVER_PORT, Udp),
);
add_verdict(&mut in_v4, &Verdict::Accept);
self.batch.add(&in_v4, nftnl::MsgType::Add);
}
}
}
fn allow_interface_rule<'a>(
chain: &'a Chain<'_>,
direction: Direction,
iface: &str,
) -> Result<Rule<'a>> {
let mut rule = Rule::new(&chain);
check_iface(&mut rule, direction, iface)?;
add_verdict(&mut rule, &Verdict::Accept);
Ok(rule)
}
fn check_iface(rule: &mut Rule<'_>, direction: Direction, iface: &str) -> Result<()> {
let iface_index = crate::linux::iface_index(iface)
.map_err(|e| Error::LookupIfaceIndexError(iface.to_owned(), e))?;
rule.add_expr(&match direction {
Direction::In => nft_expr!(meta iif),
Direction::Out => nft_expr!(meta oif),
});
rule.add_expr(&nft_expr!(cmp == iface_index));
Ok(())
}
fn check_net(rule: &mut Rule<'_>, end: End, net: impl Into<IpNetwork>) {
let net = net.into();
// Must check network layer protocol before loading network layer payload
check_l3proto(rule, net.ip());
rule.add_expr(&match (net, end) {
(IpNetwork::V4(_), End::Src) => nft_expr!(payload ipv4 saddr),
(IpNetwork::V4(_), End::Dst) => nft_expr!(payload ipv4 daddr),
(IpNetwork::V6(_), End::Src) => nft_expr!(payload ipv6 saddr),
(IpNetwork::V6(_), End::Dst) => nft_expr!(payload ipv6 daddr),
});
match net {
IpNetwork::V4(_) => rule.add_expr(&nft_expr!(bitwise mask net.mask(), xor 0u32)),
IpNetwork::V6(_) => rule.add_expr(&nft_expr!(bitwise mask net.mask(), xor &[0u16; 8][..])),
};
rule.add_expr(&nft_expr!(cmp == net.ip()));
}
fn check_endpoint(rule: &mut Rule<'_>, end: End, endpoint: &Endpoint) {
check_ip(rule, end, endpoint.address.ip());
check_port(rule, endpoint.protocol, end, endpoint.address.port());
}
fn check_ip(rule: &mut Rule<'_>, end: End, ip: impl Into<IpAddr>) {
let ip = ip.into();
// Must check network layer protocol before loading network layer payload
check_l3proto(rule, ip);
rule.add_expr(&match (ip, end) {
(IpAddr::V4(..), End::Src) => nft_expr!(payload ipv4 saddr),
(IpAddr::V4(..), End::Dst) => nft_expr!(payload ipv4 daddr),
(IpAddr::V6(..), End::Src) => nft_expr!(payload ipv6 saddr),
(IpAddr::V6(..), End::Dst) => nft_expr!(payload ipv6 daddr),
});
match ip {
IpAddr::V4(addr) => rule.add_expr(&nft_expr!(cmp == addr)),
IpAddr::V6(addr) => rule.add_expr(&nft_expr!(cmp == addr)),
}
}
fn check_port(rule: &mut Rule<'_>, protocol: TransportProtocol, end: End, port: u16) {
// Must check transport layer protocol before loading transport layer payload
check_l4proto(rule, protocol);
rule.add_expr(&match (protocol, end) {
(TransportProtocol::Udp, End::Src) => nft_expr!(payload udp sport),
(TransportProtocol::Udp, End::Dst) => nft_expr!(payload udp dport),
(TransportProtocol::Tcp, End::Src) => nft_expr!(payload tcp sport),
(TransportProtocol::Tcp, End::Dst) => nft_expr!(payload tcp dport),
});
rule.add_expr(&nft_expr!(cmp == port.to_be()));
}
fn check_l3proto(rule: &mut Rule<'_>, ip: IpAddr) {
rule.add_expr(&nft_expr!(meta nfproto));
rule.add_expr(&nft_expr!(cmp == l3proto(ip)));
}
fn l3proto(addr: IpAddr) -> u8 {
match addr {
IpAddr::V4(_) => libc::NFPROTO_IPV4 as u8,
IpAddr::V6(_) => libc::NFPROTO_IPV6 as u8,
}
}
fn check_l4proto(rule: &mut Rule<'_>, protocol: TransportProtocol) {
rule.add_expr(&nft_expr!(meta l4proto));
rule.add_expr(&nft_expr!(cmp == l4proto(protocol)));
}
fn l4proto(protocol: TransportProtocol) -> u8 {
match protocol {
TransportProtocol::Udp => libc::IPPROTO_UDP as u8,
TransportProtocol::Tcp => libc::IPPROTO_TCP as u8,
}
}
fn add_verdict(rule: &mut Rule<'_>, verdict: &expr::Verdict) {
if *ADD_COUNTERS {
rule.add_expr(&nft_expr!(counter));
}
rule.add_expr(verdict);
}