Skip to content

Commit ee95425

Browse files
authored
Use new wireguard-rs API with BoringTun (#220)
1 parent fcfcc7a commit ee95425

File tree

16 files changed

+853
-95
lines changed

16 files changed

+853
-95
lines changed

Cargo.lock

Lines changed: 773 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
22
name = "defguard-gateway"
33
version = "1.6.0"
4-
edition = "2021"
4+
edition = "2024"
55

66
[dependencies]
77
defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "8649a9ba225d7bd2066a09c9e1347705c34bd158" }
88
axum = "0.8"
99
base64 = "0.22"
1010
clap = { version = "4.5", features = ["derive", "env"] }
11-
defguard_wireguard_rs = "0.7.7"
11+
defguard_wireguard_rs = { git = "https://github.com/DefGuard/wireguard-rs", rev = "0db4ea7bf4a6bd21c449f9ab8fa6676aebf4698f" }
1212
env_logger = "0.11"
1313
gethostname = "1.0"
1414
ipnetwork = "0.21"

deny.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ allow = [
8888
"Apache-2.0",
8989
"Apache-2.0 WITH LLVM-exception",
9090
"MPL-2.0",
91+
"BSD-2-Clause",
9192
"BSD-3-Clause",
9293
"Unicode-3.0",
9394
"Unicode-DFS-2016", # unicode-ident

examples/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
collections::HashMap,
3-
io::{stdout, Write},
3+
io::{Write, stdout},
44
net::{IpAddr, Ipv4Addr, SocketAddr},
55
sync::{Arc, Mutex},
66
};
@@ -19,7 +19,7 @@ use tokio::{
1919
},
2020
};
2121
use tokio_stream::wrappers::UnboundedReceiverStream;
22-
use tonic::{transport::Server, Request, Response, Status, Streaming};
22+
use tonic::{Request, Response, Status, Streaming, transport::Server};
2323

2424
pub struct HostConfig {
2525
name: String,

src/enterprise/firewall/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl FirewallApi {
3737
pub(crate) trait FirewallManagementApi {
3838
/// Set up the firewall with `default_policy`, `priority`, and cleans up any existing rules.
3939
fn setup(&mut self, default_policy: Policy, priority: Option<i32>)
40-
-> Result<(), FirewallError>;
40+
-> Result<(), FirewallError>;
4141

4242
/// Clean up the firewall rules.
4343
fn cleanup(&mut self) -> Result<(), FirewallError>;

src/enterprise/firewall/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ pub(crate) enum Policy {
189189

190190
impl From<bool> for Policy {
191191
fn from(allow: bool) -> Self {
192-
if allow {
193-
Self::Allow
194-
} else {
195-
Self::Deny
196-
}
192+
if allow { Self::Allow } else { Self::Deny }
197193
}
198194
}
199195

src/enterprise/firewall/nftables/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use netfilter::{
1212
use nftnl::Batch;
1313

1414
use super::{
15+
Address, FirewallError, FirewallRule, Policy, Port, Protocol, SnatBinding,
1516
api::{FirewallApi, FirewallManagementApi},
1617
iprange::IpAddrRangeError,
17-
Address, FirewallError, FirewallRule, Policy, Port, Protocol, SnatBinding,
1818
};
1919
use crate::enterprise::firewall::iprange::IpAddrRange;
2020

@@ -273,7 +273,9 @@ impl FirewallManagementApi for FirewallApi {
273273
masquerade_enabled: bool,
274274
snat_bindings: &[SnatBinding],
275275
) -> Result<(), FirewallError> {
276-
debug!("Setting up POSTROUTING chain rules with masquerade status: {masquerade_enabled} and SNAT bindings: {snat_bindings:?}");
276+
debug!(
277+
"Setting up POSTROUTING chain rules with masquerade status: {masquerade_enabled} and SNAT bindings: {snat_bindings:?}"
278+
);
277279

278280
if let Some(batch) = &mut self.batch {
279281
set_nat_rules(batch, &self.ifname, masquerade_enabled, snat_bindings)?;

src/enterprise/firewall/nftables/netfilter.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::{
44
};
55

66
use nftnl::{
7+
Batch, Chain, FinalizedBatch, ProtoFamily, Rule, Table,
78
expr::{Expression, Immediate, InterfaceName, Nat, NatType, Register},
89
nft_expr, nftnl_sys,
910
set::{Set, SetKey},
10-
Batch, Chain, FinalizedBatch, ProtoFamily, Rule, Table,
1111
};
1212

13-
use super::{get_set_id, Address, FilterRule, Policy, Port, Protocol, State};
14-
use crate::enterprise::firewall::{iprange::IpAddrRange, max_address, FirewallError, SnatBinding};
13+
use super::{Address, FilterRule, Policy, Port, Protocol, State, get_set_id};
14+
use crate::enterprise::firewall::{FirewallError, SnatBinding, iprange::IpAddrRange, max_address};
1515

1616
const FILTER_TABLE: &str = "filter";
1717
const NAT_TABLE: &str = "nat";
@@ -98,7 +98,7 @@ fn add_address_to_set(set: *mut nftnl_sys::nftnl_set, ip: &Address) -> Result<()
9898
return Err(FirewallError::InvalidConfiguration(format!(
9999
"Expected both addresses to be of the same type, got {net:?} and \
100100
{upper_bound:?}",
101-
)))
101+
)));
102102
}
103103
}
104104
}
@@ -308,29 +308,26 @@ impl FirewallRule for FilterRule<'_> {
308308
// 1 Protocol
309309
// > 0 Ports
310310
else if !self.dest_ports.is_empty() {
311-
if let Some(protocol) = self.protocols.first() {
312-
if protocol.supports_ports() {
313-
let set = new_anon_set::<InetService>(
314-
chain.get_table(),
315-
ProtoFamily::Inet,
316-
true,
317-
)?;
318-
batch.add(&set, nftnl::MsgType::Add);
319-
320-
for port in self.dest_ports {
321-
add_port_to_set(set.as_ptr(), port)?;
322-
}
323-
324-
// <protocol> dport {x, x-x}
325-
set.elems_iter().for_each(|elem| {
326-
batch.add(&elem, nftnl::MsgType::Add);
327-
});
328-
329-
rule.add_expr(&nft_expr!(meta l4proto));
330-
rule.add_expr(&nft_expr!(cmp == *protocol as u8));
331-
rule.add_expr(protocol.as_port_payload_expr()?);
332-
rule.add_expr(&nft_expr!(lookup & set));
311+
if let Some(protocol) = self.protocols.first()
312+
&& protocol.supports_ports()
313+
{
314+
let set =
315+
new_anon_set::<InetService>(chain.get_table(), ProtoFamily::Inet, true)?;
316+
batch.add(&set, nftnl::MsgType::Add);
317+
318+
for port in self.dest_ports {
319+
add_port_to_set(set.as_ptr(), port)?;
333320
}
321+
322+
// <protocol> dport {x, x-x}
323+
set.elems_iter().for_each(|elem| {
324+
batch.add(&elem, nftnl::MsgType::Add);
325+
});
326+
327+
rule.add_expr(&nft_expr!(meta l4proto));
328+
rule.add_expr(&nft_expr!(cmp == *protocol as u8));
329+
rule.add_expr(protocol.as_port_payload_expr()?);
330+
rule.add_expr(&nft_expr!(lookup & set));
334331
}
335332

336333
debug!(
@@ -876,7 +873,7 @@ pub(crate) fn send_batch(batch: &FinalizedBatch) -> Result<(), FirewallError> {
876873
Err(err) => {
877874
return Err(FirewallError::NetlinkError(format!(
878875
"There was an error while sending netlink messages: {err:?}"
879-
)))
876+
)));
880877
}
881878
};
882879
}

src/enterprise/firewall/packetfilter/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::os::fd::AsRawFd;
22

33
use super::{
4-
calls::{pf_begin, pf_commit, pf_rollback, IocTrans, IocTransElement},
5-
rule::RuleSet,
64
FirewallRule,
5+
calls::{IocTrans, IocTransElement, pf_begin, pf_commit, pf_rollback},
6+
rule::RuleSet,
77
};
88
use crate::enterprise::firewall::{
9-
api::{FirewallApi, FirewallManagementApi},
109
FirewallError, Policy, SnatBinding,
10+
api::{FirewallApi, FirewallManagementApi},
1111
};
1212

1313
impl FirewallManagementApi for FirewallApi {

src/enterprise/firewall/packetfilter/calls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use std::{
44
ffi::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void},
55
fmt,
6-
mem::{size_of, zeroed, MaybeUninit},
6+
mem::{MaybeUninit, size_of, zeroed},
77
ptr,
88
};
99

1010
use ipnetwork::IpNetwork;
11-
use libc::{pid_t, uid_t, IFNAMSIZ};
11+
use libc::{IFNAMSIZ, pid_t, uid_t};
1212
use nix::{ioctl_none, ioctl_readwrite};
1313

1414
use super::rule::{Action, AddressFamily, Direction, PacketFilterRule, RuleSet, State};

0 commit comments

Comments
 (0)