wg-quick: bring interface up while setting MTU

This avoids another ip(8) invocation for little benefit.
Confirmed to work with iproute2 and busybox.

Signed-off-by: Aaron Jones <aaronmdjones@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Aaron Jones 2018-12-16 21:04:02 +00:00 committed by Jason A. Donenfeld
parent 586b466394
commit 48a31572f1

View file

@ -112,18 +112,14 @@ del_if() {
cmd ip link delete dev "$INTERFACE" cmd ip link delete dev "$INTERFACE"
} }
up_if() {
cmd ip link set "$INTERFACE" up
}
add_addr() { add_addr() {
cmd ip address add "$1" dev "$INTERFACE" cmd ip address add "$1" dev "$INTERFACE"
} }
set_mtu() { set_mtu_up() {
local mtu=0 endpoint output local mtu=0 endpoint output
if [[ -n $MTU ]]; then if [[ -n $MTU ]]; then
cmd ip link set mtu "$MTU" dev "$INTERFACE" cmd ip link set mtu "$MTU" up dev "$INTERFACE"
return return
fi fi
while read -r _ endpoint; do while read -r _ endpoint; do
@ -136,7 +132,7 @@ set_mtu() {
[[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}" [[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
fi fi
[[ $mtu -gt 0 ]] || mtu=1500 [[ $mtu -gt 0 ]] || mtu=1500
cmd ip link set mtu $(( mtu - 80 )) dev "$INTERFACE" cmd ip link set mtu $(( mtu - 80 )) up dev "$INTERFACE"
} }
resolvconf_iface_prefix() { resolvconf_iface_prefix() {
@ -286,8 +282,7 @@ cmd_up() {
for i in "${ADDRESSES[@]}"; do for i in "${ADDRESSES[@]}"; do
add_addr "$i" add_addr "$i"
done done
set_mtu set_mtu_up
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
add_route "$i" add_route "$i"