mwan3: fixup some extra spaces and shellcheck warnings

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
This commit is contained in:
Aaron Goodman 2020-08-31 19:15:09 -04:00
parent 386f18a7e5
commit b7e26dd431
6 changed files with 92 additions and 92 deletions
net/mwan3/files
etc/hotplug.d/iface
lib/mwan3
usr/sbin

View file

@ -9,7 +9,7 @@
SCRIPTNAME="mwan3-hotplug" SCRIPTNAME="mwan3-hotplug"
[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1 [ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1
[ -n "$INTERFACE" ] || exit 2 [ -n "$INTERFACE" ] || exit 2
if ( [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ) && [ -z "$DEVICE" ]; then if { [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ; } && [ -z "$DEVICE" ]; then
LOG notice "$ACTION called on $INTERFACE with no device set" LOG notice "$ACTION called on $INTERFACE with no device set"
exit 3 exit 3
fi fi
@ -62,7 +62,7 @@ fi
binary_status=$status binary_status=$status
[ "$binary_status" = "online" ] || binary_status=offline [ "$binary_status" = "online" ] || binary_status=offline
LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})" LOG notice "Execute $ACTION event on interface $INTERFACE (${DEVICE:-unknown})"
case "$ACTION" in case "$ACTION" in
ifup|connected) ifup|connected)

View file

@ -13,5 +13,5 @@ LOG()
# when this release is out of beta, the comment in the line below # when this release is out of beta, the comment in the line below
# should be removed # should be removed
[ "$facility" = "debug" ] && return [ "$facility" = "debug" ] && return
logger -t "$SCRIPTNAME[$$]" -p $facility "$*" logger -t "${SCRIPTNAME}[$$]" -p $facility "$*"
} }

View file

@ -50,7 +50,9 @@ $*";
mwan3_update_dev_to_table() mwan3_update_dev_to_table()
{ {
local _tid local _tid
# shellcheck disable=SC2034
mwan3_dev_tbl_ipv4=" " mwan3_dev_tbl_ipv4=" "
# shellcheck disable=SC2034
mwan3_dev_tbl_ipv6=" " mwan3_dev_tbl_ipv6=" "
update_table() update_table()
@ -170,7 +172,7 @@ mwan3_init()
bitcnt=$(mwan3_count_one_bits MMX_MASK) bitcnt=$(mwan3_count_one_bits MMX_MASK)
mmdefault=$(((1<<bitcnt)-1)) mmdefault=$(((1<<bitcnt)-1))
MWAN3_INTERFACE_MAX=$(($mmdefault-3)) MWAN3_INTERFACE_MAX=$((mmdefault-3))
uci_toggle_state mwan3 globals iface_max "$MWAN3_INTERFACE_MAX" uci_toggle_state mwan3 globals iface_max "$MWAN3_INTERFACE_MAX"
LOG debug "Max interface count is ${MWAN3_INTERFACE_MAX}" LOG debug "Max interface count is ${MWAN3_INTERFACE_MAX}"
fi fi
@ -178,8 +180,8 @@ mwan3_init()
# mark mask constants # mark mask constants
bitcnt=$(mwan3_count_one_bits MMX_MASK) bitcnt=$(mwan3_count_one_bits MMX_MASK)
mmdefault=$(((1<<bitcnt)-1)) mmdefault=$(((1<<bitcnt)-1))
MM_BLACKHOLE=$(($mmdefault-2)) MM_BLACKHOLE=$((mmdefault-2))
MM_UNREACHABLE=$(($mmdefault-1)) MM_UNREACHABLE=$((mmdefault-1))
# MMX_DEFAULT should equal MMX_MASK # MMX_DEFAULT should equal MMX_MASK
MMX_DEFAULT=$(mwan3_id2mask mmdefault MMX_MASK) MMX_DEFAULT=$(mwan3_id2mask mmdefault MMX_MASK)
@ -220,14 +222,13 @@ mwan3_get_iface_id()
_tmp="${mwan3_iface_tbl##* ${2}=}" _tmp="${mwan3_iface_tbl##* ${2}=}"
_tmp=${_tmp%% *} _tmp=${_tmp%% *}
export "$1=$_tmp" export "$1=$_tmp"
new_val=$_tmp
} }
mwan3_set_custom_ipset_v4() mwan3_set_custom_ipset_v4()
{ {
local custom_network_v4 local custom_network_v4
for custom_network_v4 in $($IP4 route list table "$1" | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do for custom_network_v4 in $($IP4 route list table "$1" | awk '{print $1}' | grep -E '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
LOG notice "Adding network $custom_network_v4 from table $1 to mwan3_custom_v4 ipset" LOG notice "Adding network $custom_network_v4 from table $1 to mwan3_custom_v4 ipset"
mwan3_push_update -! add mwan3_custom_v4 "$custom_network_v4" mwan3_push_update -! add mwan3_custom_v4 "$custom_network_v4"
done done
@ -237,7 +238,7 @@ mwan3_set_custom_ipset_v6()
{ {
local custom_network_v6 local custom_network_v6
for custom_network_v6 in $($IP6 route list table "$1" | awk '{print $1}' | egrep "$IPv6_REGEX"); do for custom_network_v6 in $($IP6 route list table "$1" | awk '{print $1}' | grep -E "$IPv6_REGEX"); do
LOG notice "Adding network $custom_network_v6 from table $1 to mwan3_custom_v6 ipset" LOG notice "Adding network $custom_network_v6 from table $1 to mwan3_custom_v6 ipset"
mwan3_push_update -! add mwan3_custom_v6 "$custom_network_v6" mwan3_push_update -! add mwan3_custom_v6 "$custom_network_v6"
done done
@ -274,7 +275,7 @@ mwan3_set_connected_ipv4()
$IP4 route | awk '{print $1}' $IP4 route | awk '{print $1}'
$IP4 route list table 0 | awk '{print $2}' $IP4 route list table 0 | awk '{print $2}'
} }
for connected_network_v4 in $(route_lists | egrep "$ipv4regex"); do for connected_network_v4 in $(route_lists | grep -E "$ipv4regex"); do
if [ -z "${connected_network_v4##*/*}" ]; then if [ -z "${connected_network_v4##*/*}" ]; then
cidr_list="$cidr_list $connected_network_v4" cidr_list="$cidr_list $connected_network_v4"
else else
@ -307,7 +308,7 @@ mwan3_set_connected_iptables()
mwan3_push_update -! create mwan3_connected_v6 hash:net family inet6 mwan3_push_update -! create mwan3_connected_v6 hash:net family inet6
mwan3_push_update flush mwan3_connected_v6 mwan3_push_update flush mwan3_connected_v6
for connected_network_v6 in $($IP6 route | awk '{print $1}' | egrep "$IPv6_REGEX"); do for connected_network_v6 in $($IP6 route | awk '{print $1}' | grep -E "$IPv6_REGEX"); do
mwan3_push_update -! add mwan3_connected_v6 "$connected_network_v6" mwan3_push_update -! add mwan3_connected_v6 "$connected_network_v6"
done done
@ -336,12 +337,12 @@ mwan3_set_general_rules()
for IP in "$IP4" "$IP6"; do for IP in "$IP4" "$IP6"; do
[ "$IP" = "$IP6" ] && [ $NO_IPV6 -ne 0 ] && continue [ "$IP" = "$IP6" ] && [ $NO_IPV6 -ne 0 ] && continue
RULE_NO=$(($MM_BLACKHOLE+2000)) RULE_NO=$((MM_BLACKHOLE+2000))
if [ -z "$($IP rule list | awk -v var="$RULE_NO:" '$1 == var')" ]; then if [ -z "$($IP rule list | awk -v var="$RULE_NO:" '$1 == var')" ]; then
$IP rule add pref $RULE_NO fwmark $MMX_BLACKHOLE/$MMX_MASK blackhole $IP rule add pref $RULE_NO fwmark $MMX_BLACKHOLE/$MMX_MASK blackhole
fi fi
RULE_NO=$(($MM_UNREACHABLE+2000)) RULE_NO=$((MM_UNREACHABLE+2000))
if [ -z "$($IP rule list | awk -v var="$RULE_NO:" '$1 == var')" ]; then if [ -z "$($IP rule list | awk -v var="$RULE_NO:" '$1 == var')" ]; then
$IP rule add pref $RULE_NO fwmark $MMX_UNREACHABLE/$MMX_MASK unreachable $IP rule add pref $RULE_NO fwmark $MMX_UNREACHABLE/$MMX_MASK unreachable
fi fi
@ -475,14 +476,14 @@ mwan3_create_iface_iptables()
mwan3_push_update -A "mwan3_iface_in_$1" \ mwan3_push_update -A "mwan3_iface_in_$1" \
-i "$2" \ -i "$2" \
-m set --match-set $connected_name src \ -m set --match-set $connected_name src \
-m mark --mark 0x0/$MMX_MASK \ -m mark --mark "0x0/$MMX_MASK" \
-m comment --comment "default" \ -m comment --comment "default" \
-j MARK --set-xmark $MMX_DEFAULT/$MMX_MASK -j MARK --set-xmark "$MMX_DEFAULT/$MMX_MASK"
mwan3_push_update -A "mwan3_iface_in_$1" \ mwan3_push_update -A "mwan3_iface_in_$1" \
-i "$2" \ -i "$2" \
-m mark --mark 0x0/$MMX_MASK \ -m mark --mark "0x0/$MMX_MASK" \
-m comment --comment "$1" \ -m comment --comment "$1" \
-j MARK --set-xmark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK -j MARK --set-xmark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK"
if [ -n "${current##*-A mwan3_ifaces_in -m mark --mark 0x0/$MMX_MASK -j mwan3_iface_in_${1}*}" ]; then if [ -n "${current##*-A mwan3_ifaces_in -m mark --mark 0x0/$MMX_MASK -j mwan3_iface_in_${1}*}" ]; then
mwan3_push_update -A mwan3_ifaces_in \ mwan3_push_update -A mwan3_ifaces_in \
@ -572,7 +573,7 @@ mwan3_add_non_default_iface_route()
fi fi
mwan3_update_dev_to_table mwan3_update_dev_to_table
$IP route list table main | grep -v "^default\|linkdown\|^::/0\|^fe80::/64\|^unreachable" | while read route_line; do $IP route list table main | grep -v "^default\|linkdown\|^::/0\|^fe80::/64\|^unreachable" | while read -r route_line; do
mwan3_route_line_dev "tid" "$route_line" "$family" mwan3_route_line_dev "tid" "$route_line" "$family"
if [ -z "$tid" ] || [ "$tid" = "$id" ]; then if [ -z "$tid" ] || [ "$tid" = "$id" ]; then
$IP route add table $id $route_line || $IP route add table $id $route_line ||
@ -615,7 +616,7 @@ mwan3_add_all_nondefault_routes()
tid=0 tid=0
active_tbls=" " active_tbls=" "
config_foreach add_active_tbls interface config_foreach add_active_tbls interface
$IP route list table main | grep -v "^default\|linkdown\|^::/0\|^fe80::/64\|^unreachable" | while read route_line; do $IP route list table main | grep -v "^default\|linkdown\|^::/0\|^fe80::/64\|^unreachable" | while read -r route_line; do
mwan3_route_line_dev "tid" "$route_line" "$ipv" mwan3_route_line_dev "tid" "$route_line" "$ipv"
if [ -n "$tid" ]; then if [ -n "$tid" ]; then
$IP route add table $tid $route_line $IP route add table $tid $route_line
@ -660,21 +661,21 @@ mwan3_create_iface_rules()
return return
fi fi
while [ -n "$($IP rule list | awk '$1 == "'$(($id+1000)):'"')" ]; do while [ -n "$($IP rule list | awk '$1 == "'$((id+1000)):'"')" ]; do
$IP rule del pref $(($id+1000)) $IP rule del pref $((id+1000))
done done
while [ -n "$($IP rule list | awk '$1 == "'$(($id+2000)):'"')" ]; do while [ -n "$($IP rule list | awk '$1 == "'$((id+2000)):'"')" ]; do
$IP rule del pref $(($id+2000)) $IP rule del pref $((id+2000))
done done
$IP rule add pref $(($id+1000)) iif "$2" lookup "$id" $IP rule add pref $((id+1000)) iif "$2" lookup "$id"
$IP rule add pref $(($id+2000)) fwmark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK lookup "$id" $IP rule add pref $((id+2000)) fwmark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK" lookup "$id"
} }
mwan3_delete_iface_rules() mwan3_delete_iface_rules()
{ {
local id family local id family IP
config_get family "$1" family ipv4 config_get family "$1" family ipv4
mwan3_get_iface_id id "$1" mwan3_get_iface_id id "$1"
@ -689,12 +690,12 @@ mwan3_delete_iface_rules()
return return
fi fi
while [ -n "$($IP rule list | awk '$1 == "'$(($id+1000)):'"')" ]; do while [ -n "$($IP rule list | awk '$1 == "'$((id+1000)):'"')" ]; do
$IP rule del pref $(($id+1000)) $IP rule del pref $((id+1000))
done done
while [ -n "$($IP rule list | awk '$1 == "'$(($id+2000)):'"')" ]; do while [ -n "$($IP rule list | awk '$1 == "'$((id+2000)):'"')" ]; do
$IP rule del pref $(($id+2000)) $IP rule del pref $((id+2000))
done done
} }
@ -785,7 +786,7 @@ mwan3_set_policy()
total_weight_v4=$weight total_weight_v4=$weight
lowest_metric_v4=$metric lowest_metric_v4=$metric
elif [ "$metric" -eq "$lowest_metric_v4" ]; then elif [ "$metric" -eq "$lowest_metric_v4" ]; then
total_weight_v4=$(($total_weight_v4+$weight)) total_weight_v4=$((total_weight_v4+weight))
total_weight=$total_weight_v4 total_weight=$total_weight_v4
else else
return return
@ -796,7 +797,7 @@ mwan3_set_policy()
total_weight_v6=$weight total_weight_v6=$weight
lowest_metric_v6=$metric lowest_metric_v6=$metric
elif [ "$metric" -eq "$lowest_metric_v6" ]; then elif [ "$metric" -eq "$lowest_metric_v6" ]; then
total_weight_v6=$(($total_weight_v6+$weight)) total_weight_v6=$((total_weight_v6+weight))
total_weight=$total_weight_v6 total_weight=$total_weight_v6
else else
return return
@ -807,9 +808,9 @@ mwan3_set_policy()
mwan3_push_update -A "mwan3_policy_$policy" \ mwan3_push_update -A "mwan3_policy_$policy" \
-m mark --mark 0x0/$MMX_MASK \ -m mark --mark 0x0/$MMX_MASK \
-m comment --comment \"$iface $weight $weight\" \ -m comment --comment \"$iface $weight $weight\" \
-j MARK --set-xmark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK -j MARK --set-xmark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK"
elif [ $is_offline -eq 0 ]; then elif [ $is_offline -eq 0 ]; then
probability=$(($weight*1000/$total_weight)) probability=$((weight*1000/total_weight))
if [ "$probability" -lt 10 ]; then if [ "$probability" -lt 10 ]; then
probability="0.00$probability" probability="0.00$probability"
elif [ $probability -lt 100 ]; then elif [ $probability -lt 100 ]; then
@ -826,7 +827,7 @@ mwan3_set_policy()
--mode random \ --mode random \
--probability "$probability" \ --probability "$probability" \
-m comment --comment \"$iface $weight $total_weight\" \ -m comment --comment \"$iface $weight $total_weight\" \
-j MARK --set-xmark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK -j MARK --set-xmark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK"
elif [ -n "$device" ]; then elif [ -n "$device" ]; then
echo "$current" | grep -q "^-A mwan3_policy_$policy.*--comment .* [0-9]* [0-9]*" || echo "$current" | grep -q "^-A mwan3_policy_$policy.*--comment .* [0-9]* [0-9]*" ||
mwan3_push_update -I "mwan3_policy_$policy" \ mwan3_push_update -I "mwan3_policy_$policy" \
@ -917,12 +918,12 @@ mwan3_set_sticky_iptables()
[ -n "$id" ] || return 0 [ -n "$id" ] || return 0
if [ -z "${current##*-N mwan3_iface_in_$1*}" ]; then if [ -z "${current##*-N mwan3_iface_in_$1*}" ]; then
mwan3_push_update -I "mwan3_rule_$rule" \ mwan3_push_update -I "mwan3_rule_$rule" \
-m mark --mark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK \ -m mark --mark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK" \
-m set ! --match-set "mwan3_sticky_$rule" src,src \ -m set ! --match-set "mwan3_sticky_$rule" src,src \
-j MARK --set-xmark 0x0/$MMX_MASK -j MARK --set-xmark "0x0/$MMX_MASK"
mwan3_push_update -I "mwan3_rule_$rule" \ mwan3_push_update -I "mwan3_rule_$rule" \
-m mark --mark 0/$MMX_MASK \ -m mark --mark "0/$MMX_MASK" \
-j MARK --set-xmark $(mwan3_id2mask id MMX_MASK)/$MMX_MASK -j MARK --set-xmark "$(mwan3_id2mask id MMX_MASK)/$MMX_MASK"
fi fi
fi fi
done done
@ -1170,8 +1171,8 @@ mwan3_report_iface_status()
if [ -z "$id" ] || [ -z "$device" ]; then if [ -z "$id" ] || [ -z "$device" ]; then
result="offline" result="offline"
elif [ -n "$($IP rule | awk '$1 == "'$(($id+1000)):'"')" ] && \ elif [ -n "$($IP rule | awk '$1 == "'$((id+1000)):'"')" ] && \
[ -n "$($IP rule | awk '$1 == "'$(($id+2000)):'"')" ] && \ [ -n "$($IP rule | awk '$1 == "'$((id+2000)):'"')" ] && \
[ -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" ] && \ [ -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" ] && \
[ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then [ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then
json_init json_init
@ -1183,11 +1184,12 @@ mwan3_report_iface_status()
json_get_vars online uptime json_get_vars online uptime
json_select .. json_select ..
json_select .. json_select ..
online="$(printf '%02dh:%02dm:%02ds\n' $(($online/3600)) $(($online%3600/60)) $(($online%60)))" online="$(printf '%02dh:%02dm:%02ds\n' $((online/3600)) $((online%3600/60)) $((online%60)))"
uptime="$(printf '%02dh:%02dm:%02ds\n' $(($uptime/3600)) $(($uptime%3600/60)) $(($uptime%60)))" uptime="$(printf '%02dh:%02dm:%02ds\n' $((uptime/3600)) $((uptime%3600/60)) $((uptime%60)))"
result="$(mwan3_get_iface_hotplug_state $1) $online, uptime $uptime" result="$(mwan3_get_iface_hotplug_state $1) $online, uptime $uptime"
elif [ -n "$($IP rule | awk '$1 == "'$(($id+1000)):'"')" ] || \ elif [ -n "$($IP rule | awk '$1 == "'$((id+1000)):'"')" ] || \
[ -n "$($IP rule | awk '$1 == "'$(($id+2000)):'"')" ] || \ [ -n "$($IP rule | awk '$1 == "'$((id+2000)):'"')" ] || \
[ -n "$($IP rule | awk '$1 == "'$((id+3000)):'"')" ] || \
[ -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" ] || \ [ -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" ] || \
[ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then [ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then
result="error" result="error"
@ -1225,10 +1227,10 @@ mwan3_report_policies()
total_weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}') total_weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
if [ ! -z "${total_weight##*[!0-9]*}" ]; then if [ -n "${total_weight##*[!0-9]*}" ]; then
for iface in $($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do for iface in $($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}') weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
percent=$(($weight*100/$total_weight)) percent=$((weight*100/total_weight))
echo " $iface ($percent%)" echo " $iface ($percent%)"
done done
else else
@ -1306,6 +1308,6 @@ mwan3_flush_conntrack()
mwan3_track_clean() mwan3_track_clean()
{ {
rm -rf "$MWAN3_STATUS_DIR/${1}" &> /dev/null rm -rf "${MWAN3_STATUS_DIR:?}/${1}" &> /dev/null
rmdir --ignore-fail-on-non-empty "$MWAN3_STATUS_DIR" rmdir --ignore-fail-on-non-empty "$MWAN3_STATUS_DIR"
} }

View file

@ -44,7 +44,7 @@ ifdown()
ifup() ifup()
{ {
local device enabled up l3_device status interface true_iface local enabled up l3_device status interface true_iface
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Expecting interface. Usage: mwan3 ifup <interface>" echo "Expecting interface. Usage: mwan3 ifup <interface>"
@ -104,40 +104,40 @@ interfaces()
echo "Interface status:" echo "Interface status:"
config_foreach mwan3_report_iface_status interface config_foreach mwan3_report_iface_status interface
echo -e echo
} }
policies() policies()
{ {
echo "Current ipv4 policies:" echo "Current ipv4 policies:"
mwan3_report_policies_v4 mwan3_report_policies_v4
echo -e echo
[ $NO_IPV6 -ne 0 ] && return [ $NO_IPV6 -ne 0 ] && return
echo "Current ipv6 policies:" echo "Current ipv6 policies:"
mwan3_report_policies_v6 mwan3_report_policies_v6
echo -e echo
} }
connected() connected()
{ {
echo "Directly connected ipv4 networks:" echo "Directly connected ipv4 networks:"
mwan3_report_connected_v4 mwan3_report_connected_v4
echo -e echo
[ $NO_IPV6 -ne 0 ] && return [ $NO_IPV6 -ne 0 ] && return
echo "Directly connected ipv6 networks:" echo "Directly connected ipv6 networks:"
mwan3_report_connected_v6 mwan3_report_connected_v6
echo -e echo
} }
rules() rules()
{ {
echo "Active ipv4 user rules:" echo "Active ipv4 user rules:"
mwan3_report_rules_v4 mwan3_report_rules_v4
echo -e echo
[ $NO_IPV6 -ne 0 ] && return [ $NO_IPV6 -ne 0 ] && return
echo "Active ipv6 user rules:" echo "Active ipv6 user rules:"
mwan3_report_rules_v6 mwan3_report_rules_v6
echo -e echo
} }
status() status()
@ -210,7 +210,7 @@ stop()
$IP route flush table $tid &> /dev/null $IP route flush table $tid &> /dev/null
done done
for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do for rule in $($IP rule list | grep -E '^[1-3][0-9]{3}\:' | cut -d ':' -f 1); do
$IP rule del pref $rule &> /dev/null $IP rule del pref $rule &> /dev/null
done done
table="$($IPT -S)" table="$($IPT -S)"
@ -255,6 +255,7 @@ restart() {
case "$1" in case "$1" in
ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart) ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
mwan3_init mwan3_init
# shellcheck disable=SC2048
$* $*
;; ;;
*) *)

View file

@ -8,8 +8,7 @@
mwan3_rtmon_route_handle() mwan3_rtmon_route_handle()
{ {
config_load mwan3 config_load mwan3
local section action route_line family tbl device metric tos dst line local section action route_line family tbl device metric tos dst line tid
local route_device tid
route_line=${1##"Deleted "} route_line=${1##"Deleted "}
route_family=$2 route_family=$2
@ -48,7 +47,7 @@ mwan3_rtmon_route_handle()
[ $action = "add" ] && [ -z "${tbl##*$route_line*}" ] && return [ $action = "add" ] && [ -z "${tbl##*$route_line*}" ] && return
[ $action = "del" ] && [ -n "${tbl##*$route_line*}" ] && return [ $action = "del" ] && [ -n "${tbl##*$route_line*}" ] && return
network_get_device device "$section" network_get_device device "$section"
LOG debug "adjusting route $device: $IP route "$action" table $tid $route_line" LOG debug "adjusting route $device: $IP route \"$action\" table $tid $route_line"
$IP route "$action" table $tid $route_line || $IP route "$action" table $tid $route_line ||
LOG warn "failed: $IP route $action table $tid $route_line" LOG warn "failed: $IP route $action table $tid $route_line"
} }
@ -69,7 +68,7 @@ mwan3_rtmon_route_handle()
dst=${route_line%% *} dst=${route_line%% *}
grep_line="$dst ${tos:+tos $tos}.*table [0-9].*${metric:+metric $metric}" grep_line="$dst ${tos:+tos $tos}.*table [0-9].*${metric:+metric $metric}"
$IP route list table all | grep "$grep_line" | while read line; do $IP route list table all | grep "$grep_line" | while read -r line; do
tbl=${line##*table } tbl=${line##*table }
tbl=${tbl%% *} tbl=${tbl%% *}
[ $tbl -gt $MWAN3_INTERFACE_MAX ] && continue [ $tbl -gt $MWAN3_INTERFACE_MAX ] && continue
@ -99,7 +98,7 @@ main()
fi fi
mwan3_init mwan3_init
$IP monitor route | while read line; do $IP monitor route | while read -r line; do
[ -z "${line##*table*}" ] && continue [ -z "${line##*table*}" ] && continue
LOG debug "handling route update $family $line" LOG debug "handling route update $family $line"
mwan3_lock "service" "mwan3rtmon" mwan3_lock "service" "mwan3rtmon"

View file

@ -44,7 +44,7 @@ validate_track_method() {
LOG warn "Missing httping. Please install httping package." LOG warn "Missing httping. Please install httping package."
return 1 return 1
} }
[ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || { [ -n "$2" ] && { [ "$2" = "0.0.0.0" ] || [ "$2" = "::" ]; } && {
LOG warn "Cannot determine source IP for the interface which is required by httping." LOG warn "Cannot determine source IP for the interface which is required by httping."
return 1 return 1
} }
@ -64,10 +64,10 @@ validate_track_method() {
disconnected() { disconnected() {
echo "offline" > /var/run/mwan3track/$INTERFACE/STATUS echo "offline" > /var/run/mwan3track/$INTERFACE/STATUS
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/OFFLINE get_uptime > /var/run/mwan3track/$INTERFACE/OFFLINE
echo "0" > /var/run/mwan3track/$INTERFACE/ONLINE echo "0" > /var/run/mwan3track/$INTERFACE/ONLINE
score=0 score=0
[ "$1" == 1 ] && return [ "$1" = 1 ] && return
LOG notice "Interface $INTERFACE ($DEVICE) is offline" LOG notice "Interface $INTERFACE ($DEVICE) is offline"
env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
} }
@ -75,12 +75,12 @@ disconnected() {
connected() { connected() {
echo "online" > /var/run/mwan3track/$INTERFACE/STATUS echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
echo "0" > /var/run/mwan3track/$INTERFACE/OFFLINE echo "0" > /var/run/mwan3track/$INTERFACE/OFFLINE
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/ONLINE get_uptime > /var/run/mwan3track/$INTERFACE/ONLINE
host_up_count=0 host_up_count=0
lost=0 lost=0
turn=0 turn=0
loss=0 loss=0
[ "$1" == 1 ] && return [ "$1" = 1 ] && return
LOG notice "Interface $INTERFACE ($DEVICE) is online" LOG notice "Interface $INTERFACE ($DEVICE) is online"
env -i ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface env -i ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
} }
@ -94,11 +94,9 @@ firstconnect() {
} }
update_status() { update_status() {
local status track_ip local track_ip=$1
track_ip=$1
status=$2
echo "$1" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip} echo "$2" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
[ -z "$3" ] && return [ -z "$3" ] && return
echo "$3" > /var/run/mwan3track/$INTERFACE/LATENCY_${track_ip} echo "$3" > /var/run/mwan3track/$INTERFACE/LATENCY_${track_ip}
echo "$4" > /var/run/mwan3track/$INTERFACE/LOSS_${track_ip} echo "$4" > /var/run/mwan3track/$INTERFACE/LOSS_${track_ip}
@ -151,7 +149,7 @@ main() {
config_get failure_loss $INTERFACE failure_loss 40 config_get failure_loss $INTERFACE failure_loss 40
config_get recovery_loss $INTERFACE recovery_loss 10 config_get recovery_loss $INTERFACE recovery_loss 10
local score=$(($down+$up)) local score=$((down+up))
local track_ips=$(echo $* | cut -d ' ' -f 5-99) local track_ips=$(echo $* | cut -d ' ' -f 5-99)
local host_up_count=0 local host_up_count=0
local lost=0 local lost=0
@ -234,14 +232,14 @@ main() {
fi fi
fi fi
else else
if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then if [ "$loss" -ge "$failure_loss" ] || [ "$latency" -ge "$failure_latency" ]; then
let lost++ let lost++
update_status "$track_ip" "down" $latency $loss update_status "$track_ip" "down" $latency $loss
if [ $score -gt $up ]; then if [ $score -gt $up ]; then
LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score" LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
fi fi
elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then elif [ "$loss" -le "$recovery_loss" ] && [ "$latency" -le "$recovery_latency" ]; then
let host_up_count++ let host_up_count++
update_status "$track_ip" "up" $latency $loss update_status "$track_ip" "up" $latency $loss
@ -274,8 +272,8 @@ main() {
score=0 score=0
fi fi
else else
if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then if [ $score -lt $((down+up)) ] && [ $lost -gt 0 ]; then
LOG info "Lost $(($lost*$count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score" LOG info "Lost $((lost*count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score"
fi fi
let score++ let score++
@ -283,7 +281,7 @@ main() {
if [ $score -gt $up ]; then if [ $score -gt $up ]; then
echo "online" > /var/run/mwan3track/$INTERFACE/STATUS echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
score=$(($down+$up)) score=$((down+up))
elif [ $score -le $up ]; then elif [ $score -le $up ]; then
sleep_time=$recovery_interval sleep_time=$recovery_interval
fi fi
@ -298,7 +296,7 @@ main() {
echo "${lost}" > /var/run/mwan3track/$INTERFACE/LOST echo "${lost}" > /var/run/mwan3track/$INTERFACE/LOST
echo "${score}" > /var/run/mwan3track/$INTERFACE/SCORE echo "${score}" > /var/run/mwan3track/$INTERFACE/SCORE
echo "${turn}" > /var/run/mwan3track/$INTERFACE/TURN echo "${turn}" > /var/run/mwan3track/$INTERFACE/TURN
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/TIME get_uptime > /var/run/mwan3track/$INTERFACE/TIME
host_up_count=0 host_up_count=0
sleep "${sleep_time}" & sleep "${sleep_time}" &