wg-quick: cleanup openbsd support

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-05-10 00:58:31 -06:00
parent 9eda95d084
commit 10b4e7677f
2 changed files with 35 additions and 26 deletions

View file

@ -90,7 +90,7 @@ add_if() {
if ! cmd ip link add "$INTERFACE" type wireguard; then if ! cmd ip link add "$INTERFACE" type wireguard; then
ret=$? ret=$?
[[ -e /sys/module/wireguard ]] || ! command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" >/dev/null && exit $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" cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
fi fi
} }

View file

@ -8,6 +8,7 @@ set -e -o pipefail
shopt -s extglob shopt -s extglob
export LC_ALL=C export LC_ALL=C
exec 3>&2
SELF="$(readlink -f "${BASH_SOURCE[0]}")" SELF="$(readlink -f "${BASH_SOURCE[0]}")"
export PATH="${SELF%/*}:$PATH" export PATH="${SELF%/*}:$PATH"
@ -28,7 +29,7 @@ PROGRAM="${0##*/}"
ARGS=( "$@" ) ARGS=( "$@" )
cmd() { cmd() {
echo "[#] $*" >&2 echo "[#] $*" >&3
"$@" "$@"
} }
@ -91,34 +92,38 @@ get_real_interface() {
wg show interfaces >/dev/null wg show interfaces >/dev/null
[[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1 [[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1
interface="$(< "/var/run/wireguard/$INTERFACE.name")" interface="$(< "/var/run/wireguard/$INTERFACE.name")"
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1 if [[ $interface != wg* ]]; then
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) )) [[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
[[ $diff -ge 2 || $diff -le -2 ]] && 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
echo "[+] Tun interface for $INTERFACE is $interface" >&2
else
[[ " $(wg show interfaces) " == *" $interface "* ]] || return 1
fi
REAL_INTERFACE="$interface" REAL_INTERFACE="$interface"
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
return 0 return 0
} }
add_if() { add_if() {
local index local index=0 ret
echo "find wg" | config -e /bsd 2>/dev/null | grep "wg count 1" >/dev/null while true; do
if [[ $? == 0 ]]; then if ret="$(cmd ifconfig wg$index create 2>&1)"; then
REAL_INTERFACE="" mkdir -p "/var/run/wireguard/"
index=0 echo wg$index > /var/run/wireguard/$INTERFACE.name
while [[ $REAL_INTERFACE == "" ]]; do get_real_interface
ifconfig wg$index create return 0
if [[ $? == 0 ]]; then fi
$REAL_INTERFACE="wg$index" if [[ $ret != *"ifconfig: SIOCIFCREATE: File exists"* ]]; then
fi echo "[!] Missing WireGuard kernel support ($ret). Falling back to slow userspace implementation." >&3
index=$((index+1)) break
done fi
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2 echo "[+] wg$index in use, trying next"
else ((++index))
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name" done
mkdir -p "/var/run/wireguard/" export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun mkdir -p "/var/run/wireguard/"
get_real_interface cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
fi get_real_interface
} }
del_routes() { del_routes() {
@ -148,7 +153,11 @@ del_routes() {
del_if() { del_if() {
unset_dns 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" cmd rm -f "/var/run/wireguard/$INTERFACE.name"
} }