Skip to content

Commit

Permalink
wg-quick: cleanup openbsd support
Browse files Browse the repository at this point in the history
Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed May 11, 2020
1 parent 9eda95d commit 585353a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/wg-quick/linux.bash
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ add_if() {
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
echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation."
echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation." >&2
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
fi
}
Expand Down
57 changes: 33 additions & 24 deletions src/wg-quick/openbsd.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set -e -o pipefail
shopt -s extglob
export LC_ALL=C

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

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

cmd() {
echo "[#] $*" >&2
echo "[#] $*" >&3
"$@"
}

Expand Down Expand Up @@ -91,34 +92,38 @@ get_real_interface() {
wg show interfaces >/dev/null
[[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1
interface="$(< "/var/run/wireguard/$INTERFACE.name")"
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
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) ))
[[ $diff -ge 2 || $diff -le -2 ]] && return 1
if [[ $interface != wg* ]]; then
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
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) ))
[[ $diff -ge 2 || $diff -le -2 ]] && return 1
else
[[ " $(wg show interfaces) " == *" $interface "* ]] || return 1
fi
REAL_INTERFACE="$interface"
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
return 0
}

add_if() {
local index
echo "find wg" | config -e /bsd 2>/dev/null | grep "wg count 1" >/dev/null
if [[ $? == 0 ]]; then
REAL_INTERFACE=""
index=0
while [[ $REAL_INTERFACE == "" ]]; do
ifconfig wg$index create
if [[ $? == 0 ]]; then
$REAL_INTERFACE="wg$index"
fi
index=$((index+1))
done
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
else
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
mkdir -p "/var/run/wireguard/"
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
get_real_interface
fi
local index=0 ret
while true; do
if ret="$(cmd ifconfig wg$index create 2>&1)"; then
mkdir -p "/var/run/wireguard/"
echo wg$index > /var/run/wireguard/$INTERFACE.name
get_real_interface
return 0
fi
if [[ $ret != *"ifconfig: SIOCIFCREATE: File exists"* ]]; then
echo "[!] Missing WireGuard kernel support ($ret). Falling back to slow userspace implementation." >&3
break
fi
echo "[+] wg$index in use, trying next"
((++index))
done
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
mkdir -p "/var/run/wireguard/"
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
get_real_interface
}

del_routes() {
Expand Down Expand Up @@ -148,7 +153,11 @@ del_routes() {

del_if() {
unset_dns
[[ -z $REAL_INTERFACE ]] || cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
if [[ -n $REAL_INTERFACE && $REAL_INTERFACE != wg* ]]; then
cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
else
cmd ifconfig $REAL_INTERFACE destroy
fi
cmd rm -f "/var/run/wireguard/$INTERFACE.name"
}

Expand Down

0 comments on commit 585353a

Please sign in to comment.