diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-10-18 22:27:22 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-10-19 13:29:28 +0200 |
commit | c3f26340e63b9f815a752af0a8bbc757b7f0cac1 (patch) | |
tree | 2f55d4ff2891c868aec56c8df21f0388d11b353b /src | |
parent | wg-quick: android: do not free iterated pointer (diff) | |
download | wireguard-tools-c3f26340e63b9f815a752af0a8bbc757b7f0cac1.tar.xz wireguard-tools-c3f26340e63b9f815a752af0a8bbc757b7f0cac1.zip |
wg-quick: openbsd: no use for userspace support
With alignment between the kernel and userspace, along with userspace
packages, we can now rely on the kernel in the future always having
wg(4).
This also simplifies the interface selection logic, and stores the
wg-quick interface name as the description.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src')
-rwxr-xr-x | src/wg-quick/openbsd.bash | 64 |
1 files changed, 23 insertions, 41 deletions
diff --git a/src/wg-quick/openbsd.bash b/src/wg-quick/openbsd.bash index ffd1834..15550c8 100755 --- a/src/wg-quick/openbsd.bash +++ b/src/wg-quick/openbsd.bash @@ -88,42 +88,33 @@ auto_su() { get_real_interface() { - local interface diff - wg show interfaces >/dev/null - [[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1 - interface="$(< "/var/run/wireguard/$INTERFACE.name")" - 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 - echo "[+] Tun interface for $INTERFACE is $interface" >&2 - else - [[ " $(wg show interfaces) " == *" $interface "* ]] || return 1 - fi - REAL_INTERFACE="$interface" - return 0 + local interface line + while IFS= read -r line; do + if [[ $line =~ ^([a-z]+[0-9]+):\ .+ ]]; then + interface="${BASH_REMATCH[1]}" + continue + fi + if [[ $interface == wg* && $line =~ ^\ description:\ wg-quick:\ (.+) && ${BASH_REMATCH[1]} == "$INTERFACE" ]]; then + REAL_INTERFACE="$interface" + return 0 + fi + done < <(ifconfig) + return 1 } add_if() { - 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 + local -A existing_ifs="( $(wg show interfaces | sed 's/\([^ ]*\)/[\1]=1/g') )" + local index ret + for ((index=0; index <= 2147483647; ++index)); do [[ -v existing_ifs[wg$index] ]] || break; done + if ret="$(cmd ifconfig wg$index create description "wg-quick: $INTERFACE" 2>&1)"; then + REAL_INTERFACE="wg$index" 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)) + [[ $ret == *"ifconfig: SIOCIFCREATE: File exists"* ]] && continue + echo "$ret" >&3 + return 1 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() { @@ -153,12 +144,7 @@ del_routes() { del_if() { unset_dns - 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" + [[ -n $REAL_INTERFACE ]] && cmd ifconfig $REAL_INTERFACE destroy } up_if() { @@ -438,9 +424,7 @@ cmd_up() { } cmd_down() { - if ! get_real_interface || [[ " $(wg show interfaces) " != *" $REAL_INTERFACE "* ]]; then - die "\`$INTERFACE' is not a WireGuard interface" - fi + get_real_interface || die "\`$INTERFACE' is not a WireGuard interface" execute_hooks "${PRE_DOWN[@]}" [[ $SAVE_CONFIG -eq 0 ]] || save_config del_if @@ -449,9 +433,7 @@ cmd_down() { } cmd_save() { - if ! get_real_interface || [[ " $(wg show interfaces) " != *" $REAL_INTERFACE "* ]]; then - die "\`$INTERFACE' is not a WireGuard interface" - fi + get_real_interface || die "\`$INTERFACE' is not a WireGuard interface" save_config } |