Skip to content

Commit d2ed20e

Browse files
committed
Introduce load_wireguard_kernel_module() for FreeBSD
1 parent 3bdd14e commit d2ed20e

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

Cargo.lock

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "defguard_wireguard_rs"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
edition = "2021"
55
rust-version = "1.80"
66
description = "A unified multi-platform high-level API for managing WireGuard interfaces"

src/bsd/ifconfig.rs

-17
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use std::{
33
os::fd::AsRawFd,
44
};
55

6-
#[cfg(target_os = "freebsd")]
7-
use libc::{c_char, kld_load};
86
use libc::{IFF_UP, IF_NAMESIZE};
97
use nix::{ioctl_readwrite, ioctl_write_ptr, sys::socket::AddressFamily};
108

@@ -75,21 +73,6 @@ pub struct IfReq {
7573
impl IfReq {
7674
#[must_use]
7775
pub(super) fn new(if_name: &str) -> Self {
78-
// First, try to load a kernel module for this type of network interface.
79-
// Omit digits at the end of interface name, e.g. "wg0" -> "if_wg".
80-
#[cfg(target_os = "freebsd")]
81-
{
82-
let index = if_name
83-
.find(|c: char| c.is_ascii_digit())
84-
.unwrap_or(if_name.len());
85-
let mod_name = format!("if_{}", &if_name[0..index]);
86-
unsafe {
87-
// Ignore the return value for the time being.
88-
// Do the cast because `c_char` differs across platforms.
89-
kld_load(mod_name.as_ptr() as *const c_char);
90-
}
91-
}
92-
9376
Self {
9477
ifr_name: make_ifr_name(if_name),
9578
ifr_ifru: SockAddrIn::default(),

src/bsd/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub enum IoError {
8181
NetworkInterface,
8282
#[error("Not enough bytes to unpack")]
8383
Unpack,
84+
#[error("Failed to load kernel module")]
85+
KernelModule,
8486
}
8587

8688
impl From<IoError> for WireguardInterfaceError {
@@ -301,6 +303,17 @@ pub fn delete_peer(if_name: &str, public_key: &Key) -> Result<(), IoError> {
301303
wg_data.write_data()
302304
}
303305

306+
#[cfg(target_os = "freebsd")]
307+
pub fn load_wireguard_kernel_module() -> Result<(), IoError> {
308+
// Ignore the return value for the time being.
309+
let retval = unsafe { libc::kld_load(c"if_wg".as_ptr()) };
310+
if retval == 0 {
311+
Ok(())
312+
} else {
313+
Err(IoError::KernelModule)
314+
}
315+
}
316+
304317
pub fn create_interface(if_name: &str) -> Result<(), IoError> {
305318
let mut ifreq = IfReq::new(if_name);
306319
ifreq.create()?;

src/wgapi_freebsd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
impl WireguardInterfaceApi for WGApi<Kernel> {
1515
/// Creates a WireGuard network interface.
1616
fn create_interface(&self) -> Result<(), WireguardInterfaceError> {
17+
bsd::load_wireguard_kernel_module();
1718
debug!("Creating interface {}", &self.ifname);
1819
bsd::create_interface(&self.ifname)?;
1920
debug!("Interface {} created successfully", &self.ifname);

0 commit comments

Comments
 (0)