wg-quick: freebsd: add kernel support

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-03-10 21:17:59 -07:00
parent 396b85280a
commit 576e40056d
1 changed files with 17 additions and 3 deletions

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
"$@" "$@"
} }
@ -114,6 +115,16 @@ auto_su() {
} }
add_if() { add_if() {
local ret rc
if ret="$(cmd ifconfig wg create name "$INTERFACE" 2>&1 >/dev/null)"; then
return 0
fi
rc=$?
if [[ $ret == *"ifconfig: ioctl SIOCSIFNAME (set name): File exists"* ]]; then
echo "$ret" >&3
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 "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
} }
@ -157,7 +168,11 @@ if_exists() {
del_if() { del_if() {
[[ $HAVE_SET_DNS -eq 0 ]] || unset_dns [[ $HAVE_SET_DNS -eq 0 ]] || unset_dns
if [[ -f /var/run/wireguard/$INTERFACE.sock ]]; then
cmd rm -f "/var/run/wireguard/$INTERFACE.sock" cmd rm -f "/var/run/wireguard/$INTERFACE.sock"
else
cmd ifconfig "$INTERFACE" destroy
fi
while if_exists; do while if_exists; do
# HACK: it would be nice to `route monitor` here and wait for RTM_IFANNOUNCE # HACK: it would be nice to `route monitor` here and wait for RTM_IFANNOUNCE
# but it turns out that the announcement is made before the interface # but it turns out that the announcement is made before the interface
@ -290,7 +305,6 @@ monitor_daemon() {
# endpoints change. # endpoints change.
while read -r event; do while read -r event; do
[[ $event == RTM_* ]] || continue [[ $event == RTM_* ]] || continue
[[ -e /var/run/wireguard/$INTERFACE.sock ]] || break
if_exists || break if_exists || break
[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route [[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
# TODO: set the mtu as well, but only if up # TODO: set the mtu as well, but only if up