wg-quick: add the "Table" config option

* Table=auto (default) selects the current behaviour
* Table=off disables creation of routes altogether
* All other values are passed through to "ip route add"'s table option

Signed-off-by: Luis Ressel <aranea@aixah.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Luis Ressel 2017-12-12 23:10:08 +01:00 committed by Jason A. Donenfeld
parent 89b983fa22
commit 31d8ebcd2a
2 changed files with 18 additions and 3 deletions

View File

@ -79,6 +79,11 @@ MTU \(em if not specified, the MTU is automatically determined from the endpoint
or the system default route, which is usually a sane choice. However, to manually specify or the system default route, which is usually a sane choice. However, to manually specify
an MTU to override this automatic discovery, this value may be specified explicitly. an MTU to override this automatic discovery, this value may be specified explicitly.
.IP \(bu .IP \(bu
Table \(em Controls the routing table to which routes are added. There are two
special values: `off' disables the creation of routes altogether, and `auto'
(the default) adds routes to the default table and enables special handling of
default routes.
.IP \(bu
PreUp, PostUp, PreDown, PostDown \(em script snippets which will be executed by PreUp, PostUp, PreDown, PostDown \(em script snippets which will be executed by
.BR bash (1) .BR bash (1)
before/after setting up/tearing down the interface, most commonly used before/after setting up/tearing down the interface, most commonly used

View File

@ -16,6 +16,7 @@ INTERFACE=""
ADDRESSES=( ) ADDRESSES=( )
MTU="" MTU=""
DNS=( ) DNS=( )
TABLE=""
PRE_UP=( ) PRE_UP=( )
POST_UP=( ) POST_UP=( )
PRE_DOWN=( ) PRE_DOWN=( )
@ -45,6 +46,7 @@ parse_options() {
Address) ADDRESSES+=( ${value//,/ } ); continue ;; Address) ADDRESSES+=( ${value//,/ } ); continue ;;
MTU) MTU="$value"; continue ;; MTU) MTU="$value"; continue ;;
DNS) DNS+=( ${value//,/ } ); continue ;; DNS) DNS+=( ${value//,/ } ); continue ;;
Table) TABLE="$value"; continue ;;
PreUp) PRE_UP+=( "$value" ); continue ;; PreUp) PRE_UP+=( "$value" ); continue ;;
PreDown) PRE_DOWN+=( "$value" ); continue ;; PreDown) PRE_DOWN+=( "$value" ); continue ;;
PostUp) POST_UP+=( "$value" ); continue ;; PostUp) POST_UP+=( "$value" ); continue ;;
@ -146,10 +148,14 @@ unset_dns() {
} }
add_route() { add_route() {
if [[ $1 == 0.0.0.0/0 || $1 =~ ^[0:]+/0$ ]]; then [[ $TABLE != off ]] || return 0
if [[ -n $TABLE && $TABLE != auto ]]; then
cmd ip route add "$1" dev "$INTERFACE" table "$TABLE"
elif [[ $1 == 0.0.0.0/0 || $1 =~ ^[0:]+/0$ ]]; then
add_default "$1" add_default "$1"
else else
cmd ip route add "$1" dev "$INTERFACE" [[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || cmd ip route add "$1" dev "$INTERFACE"
fi fi
} }
@ -189,6 +195,7 @@ save_config() {
[[ $address =~ ^nameserver\ ([a-zA-Z0-9_=+:%.-]+)$ ]] && new_config+="DNS = ${BASH_REMATCH[1]}"$'\n' [[ $address =~ ^nameserver\ ([a-zA-Z0-9_=+:%.-]+)$ ]] && new_config+="DNS = ${BASH_REMATCH[1]}"$'\n'
done < <(resolvconf -l "tun.$INTERFACE" 2>/dev/null) done < <(resolvconf -l "tun.$INTERFACE" 2>/dev/null)
[[ -n $MTU && $(ip link show dev "$INTERFACE") =~ mtu\ ([0-9]+) ]] && new_config+="MTU = ${BASH_REMATCH[1]}"$'\n' [[ -n $MTU && $(ip link show dev "$INTERFACE") =~ mtu\ ([0-9]+) ]] && new_config+="MTU = ${BASH_REMATCH[1]}"$'\n'
[[ -n $TABLE ]] && new_config+="Table = $TABLE"$'\n'
[[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n' [[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n'
for cmd in "${PRE_UP[@]}"; do for cmd in "${PRE_UP[@]}"; do
new_config+="PreUp = $cmd"$'\n' new_config+="PreUp = $cmd"$'\n'
@ -236,6 +243,9 @@ cmd_usage() {
IP addresses (with an optional CIDR mask) to be set for the interface. IP addresses (with an optional CIDR mask) to be set for the interface.
- DNS: an optional DNS server to use while the device is up. - DNS: an optional DNS server to use while the device is up.
- MTU: an optional MTU for the interface; if unspecified, auto-calculated. - MTU: an optional MTU for the interface; if unspecified, auto-calculated.
- Table: an optional routing table to which routes will be added; if
unspecified or \`auto', the default table is used. If \`off', no routes
are added.
- PreUp, PostUp, PreDown, PostDown: script snippets which will be executed - PreUp, PostUp, PreDown, PostDown: script snippets which will be executed
by bash(1) at the corresponding phases of the link, most commonly used by bash(1) at the corresponding phases of the link, most commonly used
to configure DNS. The string \`%i' is expanded to INTERFACE. to configure DNS. The string \`%i' is expanded to INTERFACE.
@ -260,7 +270,7 @@ cmd_up() {
up_if up_if
set_dns 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 "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
[[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || add_route "$i" add_route "$i"
done done
execute_hooks "${POST_UP[@]}" execute_hooks "${POST_UP[@]}"
trap - INT TERM EXIT trap - INT TERM EXIT