Skip to content

Commit 585353a

Browse files
committed
wg-quick: cleanup openbsd support
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 9eda95d commit 585353a

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

Diff for: src/wg-quick/linux.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ add_if() {
9090
if ! cmd ip link add "$INTERFACE" type wireguard; then
9191
ret=$?
9292
[[ -e /sys/module/wireguard ]] || ! command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" >/dev/null && exit $ret
93-
echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation."
93+
echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation." >&2
9494
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
9595
fi
9696
}

Diff for: src/wg-quick/openbsd.bash

+33-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set -e -o pipefail
88
shopt -s extglob
99
export LC_ALL=C
1010

11+
exec 3>&2
1112
SELF="$(readlink -f "${BASH_SOURCE[0]}")"
1213
export PATH="${SELF%/*}:$PATH"
1314

@@ -28,7 +29,7 @@ PROGRAM="${0##*/}"
2829
ARGS=( "$@" )
2930

3031
cmd() {
31-
echo "[#] $*" >&2
32+
echo "[#] $*" >&3
3233
"$@"
3334
}
3435

@@ -91,34 +92,38 @@ get_real_interface() {
9192
wg show interfaces >/dev/null
9293
[[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1
9394
interface="$(< "/var/run/wireguard/$INTERFACE.name")"
94-
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
95-
diff=$(( $(stat -f %m "/var/run/wireguard/$interface.sock" 2>/dev/null || echo 200) - $(stat -f %m "/var/run/wireguard/$INTERFACE.name" 2>/dev/null || echo 100) ))
96-
[[ $diff -ge 2 || $diff -le -2 ]] && return 1
95+
if [[ $interface != wg* ]]; then
96+
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
97+
diff=$(( $(stat -f %m "/var/run/wireguard/$interface.sock" 2>/dev/null || echo 200) - $(stat -f %m "/var/run/wireguard/$INTERFACE.name" 2>/dev/null || echo 100) ))
98+
[[ $diff -ge 2 || $diff -le -2 ]] && return 1
99+
else
100+
[[ " $(wg show interfaces) " == *" $interface "* ]] || return 1
101+
fi
97102
REAL_INTERFACE="$interface"
98103
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
99104
return 0
100105
}
101106

102107
add_if() {
103-
local index
104-
echo "find wg" | config -e /bsd 2>/dev/null | grep "wg count 1" >/dev/null
105-
if [[ $? == 0 ]]; then
106-
REAL_INTERFACE=""
107-
index=0
108-
while [[ $REAL_INTERFACE == "" ]]; do
109-
ifconfig wg$index create
110-
if [[ $? == 0 ]]; then
111-
$REAL_INTERFACE="wg$index"
112-
fi
113-
index=$((index+1))
114-
done
115-
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
116-
else
117-
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
118-
mkdir -p "/var/run/wireguard/"
119-
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
120-
get_real_interface
121-
fi
108+
local index=0 ret
109+
while true; do
110+
if ret="$(cmd ifconfig wg$index create 2>&1)"; then
111+
mkdir -p "/var/run/wireguard/"
112+
echo wg$index > /var/run/wireguard/$INTERFACE.name
113+
get_real_interface
114+
return 0
115+
fi
116+
if [[ $ret != *"ifconfig: SIOCIFCREATE: File exists"* ]]; then
117+
echo "[!] Missing WireGuard kernel support ($ret). Falling back to slow userspace implementation." >&3
118+
break
119+
fi
120+
echo "[+] wg$index in use, trying next"
121+
((++index))
122+
done
123+
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
124+
mkdir -p "/var/run/wireguard/"
125+
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
126+
get_real_interface
122127
}
123128

124129
del_routes() {
@@ -148,7 +153,11 @@ del_routes() {
148153

149154
del_if() {
150155
unset_dns
151-
[[ -z $REAL_INTERFACE ]] || cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
156+
if [[ -n $REAL_INTERFACE && $REAL_INTERFACE != wg* ]]; then
157+
cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
158+
else
159+
cmd ifconfig $REAL_INTERFACE destroy
160+
fi
152161
cmd rm -f "/var/run/wireguard/$INTERFACE.name"
153162
}
154163

0 commit comments

Comments
 (0)