aboutsummaryrefslogtreecommitdiff
path: root/net/mwan3/files/etc/hotplug.d/iface/13-mwan3
blob: c21e1db9807163aa0bfe3b3edfcd8f135d020168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh

LOG="logger -t mwan3[$$] -p"

[ "$ACTION" = "connected" -o "$ACTION" = "disconnected" ] || exit 1
[ -n "$INTERFACE" ] || exit 2

if [ "$ACTION" = "connected" ]; then
	[ -n "$DEVICE" ] || exit 3
fi

config_load mwan3
config_get_bool enabled globals 'enabled' '0'
config_get local_source globals 'local_source' 'none'
[ ${enabled} = "1" ] || exit 0
[ ${local_source} = "none" ] || exit 0

config_get enabled $INTERFACE enabled 0
config_get online_metric $INTERFACE online_metric 0
[ "$enabled" == "1" ] || exit 0

if [ "$online_metric" = 0 ]; then
	$LOG notice "No online metric for interface "$INTERFACE" found"
	exit 0
fi

mwan3_add_failover_metric() {
	local iface="$1"
	local device="$2"
	local metric="$3"

	local route_args

	config_get family $iface family ipv4

	if [ "$family" == "ipv4" ]; then
		if ubus call network.interface.${iface}_4 status 1>/dev/null 2>&1; then
			network_get_gateway route_args ${iface}_4
		else
			network_get_gateway route_args $iface
		fi

		if [ -n "$route_args" -a "$route_args" != "0.0.0.0" ]; then
			route_args="via $route_args"
		else
			route_args=""
		fi

		$IP4 route add default $route_args dev $device proto static metric $metric 1>/dev/null 2>&1
	fi

	if [ "$family" == "ipv6" ]; then
		if ubus call network.interface.${iface}_6 status 1>/dev/null 2>&1; then
			network_get_gateway6 route_args ${iface}_6
		else
			network_get_gateway6 route_args $iface
		fi

		if [ -n "$route_args" -a "$route_args" != "::" ]; then
			route_args="via $route_args"
		else
			route_args=""
		fi

		$IP6 route add default $route_args dev $device proto static metric $metric 1>/dev/null 2>&1
	fi
}

mwan3_del_failover_metric() {
	local iface="$1"
	local device="$2"
	local metric="$3"

	config_get family $iface family ipv4

	if [ "$family" == "ipv4" ]; then
		$IP4 route del default dev $device proto static metric $metric 1>/dev/null 2>&1
	fi

	if [ "$family" == "ipv6" ]; then
		$IP6 route del default dev $device proto static metric $metric 1>/dev/null 2>&1
	fi
}

case "$ACTION" in
	connected)
		mwan3_add_failover_metric "$INTERFACE" "$DEVICE" "$online_metric"
		;;
	disconnected)
		mwan3_del_failover_metric "$INTERFACE" "$DEVICE" "$online_metric"
		;;
esac

exit 0