Skip to content

Commit

Permalink
Introduce load_wireguard_kernel_module() for FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
moubctez committed Dec 23, 2024
1 parent 3bdd14e commit d2ed20e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "defguard_wireguard_rs"
version = "0.6.0"
version = "0.6.1"
edition = "2021"
rust-version = "1.80"
description = "A unified multi-platform high-level API for managing WireGuard interfaces"
Expand Down
17 changes: 0 additions & 17 deletions src/bsd/ifconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::{
os::fd::AsRawFd,
};

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

Expand Down Expand Up @@ -75,21 +73,6 @@ pub struct IfReq {
impl IfReq {
#[must_use]
pub(super) fn new(if_name: &str) -> Self {
// First, try to load a kernel module for this type of network interface.
// Omit digits at the end of interface name, e.g. "wg0" -> "if_wg".
#[cfg(target_os = "freebsd")]
{
let index = if_name
.find(|c: char| c.is_ascii_digit())
.unwrap_or(if_name.len());
let mod_name = format!("if_{}", &if_name[0..index]);
unsafe {
// Ignore the return value for the time being.
// Do the cast because `c_char` differs across platforms.
kld_load(mod_name.as_ptr() as *const c_char);
}
}

Self {
ifr_name: make_ifr_name(if_name),
ifr_ifru: SockAddrIn::default(),
Expand Down
13 changes: 13 additions & 0 deletions src/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub enum IoError {
NetworkInterface,
#[error("Not enough bytes to unpack")]
Unpack,
#[error("Failed to load kernel module")]
KernelModule,
}

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

#[cfg(target_os = "freebsd")]
pub fn load_wireguard_kernel_module() -> Result<(), IoError> {
// Ignore the return value for the time being.
let retval = unsafe { libc::kld_load(c"if_wg".as_ptr()) };
if retval == 0 {
Ok(())
} else {
Err(IoError::KernelModule)
}
}

pub fn create_interface(if_name: &str) -> Result<(), IoError> {
let mut ifreq = IfReq::new(if_name);
ifreq.create()?;
Expand Down
1 change: 1 addition & 0 deletions src/wgapi_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
impl WireguardInterfaceApi for WGApi<Kernel> {
/// Creates a WireGuard network interface.
fn create_interface(&self) -> Result<(), WireguardInterfaceError> {
bsd::load_wireguard_kernel_module();
debug!("Creating interface {}", &self.ifname);
bsd::create_interface(&self.ifname)?;
debug!("Interface {} created successfully", &self.ifname);
Expand Down

0 comments on commit d2ed20e

Please sign in to comment.