Skip to contentSkip to conversation

wg-quick: add possibility to force use userspace implementation

PATCH New Dmitry Selivanov 1 participant 1 message Mar 19, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wg-quick: add possibility to force use userspace implementation

This patch allows to use userspace implementation with "WG_QUICK_USERSPACE_IMPLEMENTATION" env variable even if kernel driver is installed. Quotes around program name are removed to run like this:
WG_QUICK_USERSPACE_IMPLEMENTATION="setfib 1 wireguard-go" wg-quick up wg0 on FreeBSD or with "ip netns exec" on Linux. This can move wireguard traffic to non-default routing table or namespace.

---
 src/wg-quick/freebsd.bash | 6 +++++-
 src/wg-quick/linux.bash   | 8 ++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)
src/wg-quick/freebsd.bash +5 -1
index b529ab2..663af48 100755
@@ -116,6 +116,10 @@ auto_su() {
 
 add_if() {
 	local ret rc
+	if [[ -n $WG_QUICK_USERSPACE_IMPLEMENTATION ]]; then
+		cmd $WG_QUICK_USERSPACE_IMPLEMENTATION "$INTERFACE"
+		return
+	fi
 	if ret="$(cmd ifconfig wg create name "$INTERFACE" 2>&1 >/dev/null)"; then
 		return 0
 	fi
@@ -125,7 +129,7 @@ add_if() {
 		return $rc
 	fi
 	echo "[!] Missing WireGuard kernel support ($ret). Falling back to slow userspace implementation." >&3
-	cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
+	cmd wireguard-go "$INTERFACE"
 }
 
 del_routes() {
src/wg-quick/linux.bash +6 -2
index 69e5bef..a883df9 100755
@@ -87,11 +87,15 @@ auto_su() {
 
 add_if() {
 	local ret
+	if [[ -n $WG_QUICK_USERSPACE_IMPLEMENTATION ]]; then
+		cmd $WG_QUICK_USERSPACE_IMPLEMENTATION "$INTERFACE"
+		return
+	fi
 	if ! cmd ip link add "$INTERFACE" type wireguard; then
 		ret=$?
-		[[ -e /sys/module/wireguard ]] || ! command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" >/dev/null && exit $ret
+		[[ -e /sys/module/wireguard ]] || ! command -v "wireguard-go" >/dev/null && exit $ret
 		echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation." >&2
-		cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
+		cmd wireguard-go "$INTERFACE"
 	fi
 }
 
Signature · 1 line
2.30.2