Skip to content

Commit

Permalink
illumos: restore REAL_INTERFACE, smf fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
nshalman committed Dec 28, 2022
1 parent c7c6d8e commit 9f40195
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion contrib/smf/wg-quick.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</exec_method>
<property_group name="application" type="application"/>
<property_group name="startd" type="framework">
<propval name="duration" type="astring" value="transient"/>
<propval name="duration" type="astring" value="contract"/>
<propval name="ignore_error" type="astring" value="core,signal"/>
</property_group>
<!-- /instance -->
Expand Down
66 changes: 42 additions & 24 deletions src/wg-quick/sunos.bash
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ parse_options() {
[[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
[[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
CONFIG_FILE="$(readlink -f "$CONFIG_FILE")"
((($(/usr/bin/stat --format '0%#a' "$CONFIG_FILE") & $(/usr/bin/stat --format '0%#a' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2
((($(stat -c '0%#a' "$CONFIG_FILE") & $(stat -c '0%#a' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2
INTERFACE="${BASH_REMATCH[2]}"
shopt -s nocasematch
while read -r line || [[ -n $line ]]; do
Expand Down Expand Up @@ -86,28 +86,42 @@ auto_su() {
[[ $UID == 0 ]] || die "must be run as root"
}

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")"
[[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1
diff=$(( $(stat -c %Y "/var/run/wireguard/$interface.sock" 2>/dev/null || echo 200) - $(stat -c %Y "/var/run/wireguard/$INTERFACE.name" 2>/dev/null || echo 100) ))
[[ $diff -ge 2 || $diff -le -2 ]] && return 1
REAL_INTERFACE="$interface"
echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2
return 0
}

add_if() {
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard}" $INTERFACE
# Stuff is going on behind the scenes in the forked process...
until ipadm show-if $INTERFACE 2>/dev/null | grep -q $INTERFACE; do
export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name"
mkdir -p "/var/run/wireguard/"
cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun
cmd sleep 0.1
get_real_interface
until ipadm show-if $REAL_INTERFACE 2>/dev/null | grep -q $REAL_INTERFACE; do
cmd sleep 0.1
done
}

#XXX TODO FIXME
del_routes() {
local todelete=( ) destination gateway netif
[[ -n $INTERFACE ]] || return 0
[[ -n $REAL_INTERFACE ]] || return 0
while read -r destination _ _ _ _ netif _; do
[[ $netif == "$INTERFACE" ]] && todelete+=( "$destination" )
[[ $netif == "$REAL_INTERFACE" ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet "$destination" || true
done
todelete=( )
while read -r destination gateway _ netif; do
[[ $netif == "$INTERFACE" || ( $netif == lo* && $gateway == "$INTERFACE" ) ]] && todelete+=( "$destination" )
[[ $netif == "$REAL_INTERFACE" || ( $netif == lo* && $gateway == "$REAL_INTERFACE" ) ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet6)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet6 "$destination" || true
Expand All @@ -124,10 +138,11 @@ del_routes() {
del_if() {
local addr
unset_dns
[[ -n $INTERFACE ]] && ipadm show-addr -p -o addrobj | grep $INTERFACE | while read addr; do
[[ -n $REAL_INTERFACE ]] && ipadm show-addr -p -o addrobj | grep $REAL_INTERFACE/ | while read addr; do
cmd ipadm delete-addr $addr
done
[[ -n $INTERFACE ]] && cmd rm /var/run/wireguard/$INTERFACE.sock
[[ -z $REAL_INTERFACE ]] || cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
cmd rm -f "/var/run/wireguard/$INTERFACE.name"
}

up_if() {
Expand All @@ -145,14 +160,14 @@ add_addr() {
[[ -n $FIRSTADDR4 ]] || FIRSTADDR4="${1%/*}"
fi
# cleanup just in case
ipadm delete-addr "$INTERFACE/$2" &>/dev/null || true
cmd ipadm create-addr -t -T static -a "local=$1,remote=$1" "$INTERFACE/$2"
ipadm delete-addr "$REAL_INTERFACE/$2" &>/dev/null || true
cmd ipadm create-addr -t -T static -a "local=$1,remote=$1" "$REAL_INTERFACE/$2"
}

set_mtu() {
local mtu=0 endpoint output family
if [[ -n $MTU ]]; then
cmd ifconfig "$INTERFACE" mtu "$MTU"
cmd ifconfig "$REAL_INTERFACE" mtu "$MTU"
return
fi
while read -r _ endpoint; do
Expand All @@ -161,13 +176,13 @@ set_mtu() {
[[ ${BASH_REMATCH[1]} == *:* ]] && family=inet6
output="$(route -n get "-$family" "${BASH_REMATCH[1]}" || true)"
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
done < <(wg show "$INTERFACE" endpoints)
done < <(wg show "$REAL_INTERFACE" endpoints)
if [[ $mtu -eq 0 ]]; then
read -r output < <(route -n get default || true) || true
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
fi
[[ $mtu -gt 0 ]] || mtu=1500
cmd ifconfig "$INTERFACE" mtu $(( mtu - 80 ))
cmd ifconfig "$REAL_INTERFACE" mtu $(( mtu - 80 ))
}


Expand All @@ -194,7 +209,7 @@ collect_endpoints() {
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
ENDPOINTS+=( "${BASH_REMATCH[1]}" )
done < <(wg show "$INTERFACE" endpoints)
done < <(wg show "$REAL_INTERFACE" endpoints)
}

set_endpoint_direct_route() {
Expand Down Expand Up @@ -260,7 +275,7 @@ monitor_daemon() {
# endpoints change.
while read -u 19 -r event; do
[[ $event == RTM_* ]] || continue
ifconfig "$INTERFACE" >/dev/null 2>&1 || break
ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break
[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
# TODO: set the mtu as well, but only if up
done
Expand Down Expand Up @@ -307,20 +322,20 @@ add_route() {
cmd route -q -n add -inet 128.0.0.0/1 -iface "$ifaceroute"
fi
else
[[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $INTERFACE$'\n' ]] || cmd route -q -n add "-$family" "$1" -iface "$ifaceroute"
[[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $REAL_INTERFACE$'\n' ]] || cmd route -q -n add "-$family" "$1" -iface "$ifaceroute"
fi
}

set_config() {
cmd wg setconf "$INTERFACE" <(echo "$WG_CONFIG")
cmd wg setconf "$REAL_INTERFACE" <(echo "$WG_CONFIG")
}

save_config() {
local old_umask new_config current_config address network cmd
new_config=$'[Interface]\n'
{ read -r _; while read -r _ _ network address _; do
[[ $network == *Link* ]] || new_config+="Address = $address"$'\n'
done } < <(netstat -I "$INTERFACE" -n -v)
done } < <(netstat -I "$REAL_INTERFACE" -n -v)
# TODO: actually determine current DNS for interface
for address in "${DNS[@]}"; do
new_config+="DNS = $address"$'\n'
Expand All @@ -342,7 +357,7 @@ save_config() {
done
old_umask="$(umask)"
umask 077
current_config="$(cmd wg showconf "$INTERFACE")"
current_config="$(cmd wg showconf "$REAL_INTERFACE")"
trap 'rm -f "$CONFIG_FILE.tmp"; exit' INT TERM EXIT
echo "${current_config/\[Interface\]$'\n'/$new_config}" > "$CONFIG_FILE.tmp" || die "Could not write configuration file"
sync "$CONFIG_FILE.tmp"
Expand All @@ -354,7 +369,7 @@ save_config() {
execute_hooks() {
local hook
for hook in "$@"; do
hook="${hook//%i/$INTERFACE}"
hook="${hook//%i/$REAL_INTERFACE}"
hook="${hook//%I/$INTERFACE}"
echo "[#] $hook" >&2
(eval "$hook")
Expand Down Expand Up @@ -391,18 +406,19 @@ cmd_usage() {
cmd_up() {
local i
local n=0
get_real_interface && die "\`$INTERFACE' already exists as \`$REAL_INTERFACE'"
trap 'del_if; del_routes; exit' INT TERM EXIT
execute_hooks "${PRE_UP[@]}"
add_if
for i in "${ADDRESSES[@]}"; do
add_addr "$i" "wg$n"
add_addr "$i" "$INTERFACE$n"
n=$((n + 1))
done
set_config
set_mtu
up_if
set_dns
for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$REAL_INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
add_route "$i"
done
[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
Expand All @@ -412,6 +428,7 @@ cmd_up() {
}

cmd_down() {
get_real_interface || die "\`$INTERFACE' is not a WireGuard interface"
execute_hooks "${PRE_DOWN[@]}"
[[ $SAVE_CONFIG -eq 0 ]] || save_config
del_if
Expand All @@ -420,6 +437,7 @@ cmd_down() {
}

cmd_save() {
get_real_interface || die "\`$INTERFACE' is not a WireGuard interface"
save_config
}

Expand Down

0 comments on commit 9f40195

Please sign in to comment.