From d2ed20e3886fe3fb03b5d51764cafdbeb792c48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Ciarcin=CC=81ski?= Date: Mon, 23 Dec 2024 12:31:43 +0100 Subject: [PATCH] Introduce load_wireguard_kernel_module() for FreeBSD --- Cargo.lock | 38 +++++++++++++++++++------------------- Cargo.toml | 2 +- src/bsd/ifconfig.rs | 17 ----------------- src/bsd/mod.rs | 13 +++++++++++++ src/wgapi_freebsd.rs | 1 + 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e881823..33e2868 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,9 +62,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.94" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" +checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" [[package]] name = "autocfg" @@ -151,7 +151,7 @@ dependencies = [ [[package]] name = "defguard_wireguard_rs" -version = "0.6.0" +version = "0.6.1" dependencies = [ "base64", "env_logger", @@ -166,15 +166,15 @@ dependencies = [ "nix", "serde", "serde_test", - "thiserror 2.0.6", + "thiserror 2.0.9", "x25519-dalek", ] [[package]] name = "env_filter" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" dependencies = [ "log", "regex", @@ -182,9 +182,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.11.5" +version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" dependencies = [ "anstream", "anstyle", @@ -224,9 +224,9 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "libc" -version = "0.2.168" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "log" @@ -410,9 +410,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.23" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" [[package]] name = "serde" @@ -451,9 +451,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.90" +version = "2.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035" dependencies = [ "proc-macro2", "quote", @@ -471,11 +471,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.6" +version = "2.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fec2a1820ebd077e2b90c4df007bebf344cd394098a13c563957d0afc83ea47" +checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" dependencies = [ - "thiserror-impl 2.0.6", + "thiserror-impl 2.0.9", ] [[package]] @@ -491,9 +491,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.6" +version = "2.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d65750cab40f4ff1929fb1ba509e9914eb756131cef4210da8d5d700d26f6312" +checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index a2c3724..8bbce56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/bsd/ifconfig.rs b/src/bsd/ifconfig.rs index f5fb66f..e2dcc49 100644 --- a/src/bsd/ifconfig.rs +++ b/src/bsd/ifconfig.rs @@ -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}; @@ -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(), diff --git a/src/bsd/mod.rs b/src/bsd/mod.rs index 90a7c87..4e7bb24 100644 --- a/src/bsd/mod.rs +++ b/src/bsd/mod.rs @@ -81,6 +81,8 @@ pub enum IoError { NetworkInterface, #[error("Not enough bytes to unpack")] Unpack, + #[error("Failed to load kernel module")] + KernelModule, } impl From for WireguardInterfaceError { @@ -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()?; diff --git a/src/wgapi_freebsd.rs b/src/wgapi_freebsd.rs index fde760e..9f46d9b 100644 --- a/src/wgapi_freebsd.rs +++ b/src/wgapi_freebsd.rs @@ -14,6 +14,7 @@ use crate::{ impl WireguardInterfaceApi for WGApi { /// 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);