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:
		
							parent
							
								
									89b983fa22
								
							
						
					
					
						commit
						31d8ebcd2a
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user