]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
wg-quick: add the "Table" config option
authorLuis Ressel <aranea@aixah.de>
Tue, 12 Dec 2017 22:10:08 +0000 (23:10 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 13 Dec 2017 15:28:39 +0000 (16:28 +0100)
* 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>
src/wg-quick.8
src/wg-quick.bash

index fd1d23f0334c7fceced858320fbd4f7e775cbb91..2039094600b7c2d8b07ad59d01f21a64dbc41773 100644 (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
 an MTU to override this automatic discovery, this value may be specified explicitly.
 .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
 .BR bash (1)
 before/after setting up/tearing down the interface, most commonly used
index b2acbff041cd9d95bd415514eeee7b9555dfae84..c2d43fc19c8f173d1c64815ca0ea001d9bbb2f75 100755 (executable)
@@ -16,6 +16,7 @@ INTERFACE=""
 ADDRESSES=( )
 MTU=""
 DNS=( )
+TABLE=""
 PRE_UP=( )
 POST_UP=( )
 PRE_DOWN=( )
@@ -45,6 +46,7 @@ parse_options() {
                        Address) ADDRESSES+=( ${value//,/ } ); continue ;;
                        MTU) MTU="$value"; continue ;;
                        DNS) DNS+=( ${value//,/ } ); continue ;;
+                       Table) TABLE="$value"; continue ;;
                        PreUp) PRE_UP+=( "$value" ); continue ;;
                        PreDown) PRE_DOWN+=( "$value" ); continue ;;
                        PostUp) POST_UP+=( "$value" ); continue ;;
@@ -146,10 +148,14 @@ unset_dns() {
 }
 
 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"
        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
 }
 
@@ -189,6 +195,7 @@ save_config() {
                [[ $address =~ ^nameserver\ ([a-zA-Z0-9_=+:%.-]+)$ ]] && new_config+="DNS = ${BASH_REMATCH[1]}"$'\n'
        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 $TABLE ]] && new_config+="Table = $TABLE"$'\n'
        [[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n'
        for cmd in "${PRE_UP[@]}"; do
                new_config+="PreUp = $cmd"$'\n'
@@ -236,6 +243,9 @@ cmd_usage() {
            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.
          - 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
            by bash(1) at the corresponding phases of the link, most commonly used
            to configure DNS. The string \`%i' is expanded to INTERFACE.
@@ -260,7 +270,7 @@ cmd_up() {
        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
-               [[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || add_route "$i"
+               add_route "$i"
        done
        execute_hooks "${POST_UP[@]}"
        trap - INT TERM EXIT