summaryrefslogtreecommitdiffstats
path: root/net/mwan3/files/usr/sbin/mwan3
blob: 9916657928dfe352664d5efc1301713c307276b1 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh

command_help() {
	local cmd="$1"
	local help="$2"

	echo "$(printf "%-25s%s" "${cmd}" "${help}")"
}

help()
{
	cat <<EOF
Syntax: mwan3 [command]

Available commands:
EOF
	command_help "start" "Load iptables rules, ip rules and ip routes"
	command_help "stop" "Unload iptables rules, ip rules and ip routes"
	command_help "restart" "Reload iptables rules, ip rules and ip routes"
	command_help "ifup <iface>" "Load rules and routes for specific interface"
	command_help "ifdown <iface>" "Unload rules and routes for specific interface"
	command_help "interfaces" "Show interfaces status"
	command_help "policies" "Show currently active policy"
	command_help "connected" "Show directly connected networks"
	command_help "rules" "Show active rules"
	command_help "status" "Show all status"
	command_help "internal <ipv4|ipv6>" "Show internal configuration <default: ipv4>"
	command_help "use <iface> <cmd>" "Run a command bound to <iface> and avoid mwan3 rules"
}

ifdown() {
	if [ -z "$1" ]; then
		echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>"
		exit 0
	fi

	if [ -n "$2" ]; then
		echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>"
		exit 0
	fi

	mwan3_interface_hotplug_shutdown "$1" 1
}

ifup() {
	. /etc/init.d/mwan3

	if [ -z "$1" ]; then
		echo "Expecting interface. Usage: mwan3 ifup <interface>"
		exit 0
	fi

	if [ -n "$2" ]; then
		echo "Too many arguments. Usage: mwan3 ifup <interface>"
		exit 0
	fi

	mwan3_ifup "$1" "cmd"
}

interfaces()
{
	config_load mwan3

	echo "Interface status:"
	config_foreach mwan3_report_iface_status interface
	echo
}

policies()
{
	echo "Current ipv4 policies:"
	mwan3_report_policies_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Current ipv6 policies:"
	mwan3_report_policies_v6
	echo
}

connected()
{
	echo "Directly connected ipv4 networks:"
	mwan3_report_connected_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Directly connected ipv6 networks:"
	mwan3_report_connected_v6
	echo
}

rules()
{
	echo "Active ipv4 user rules:"
	mwan3_report_rules_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Active ipv6 user rules:"
	mwan3_report_rules_v6
	echo
}

status()
{
	interfaces
	policies
	connected
	rules
}

internal()
{
	local family="$1"
	local dash="-------------------------------------------------"

	if [ -f "/etc/openwrt_release" ]; then
		. /etc/openwrt_release
	fi

	local ipt ip output

	if [ "$family" = "ipv6" ]; then
		ipt="$IPT6"
		ip="$IP6"
	else
		ipt="$IPT4"
		ip="$IP4"
	fi

	echo "Software-Version"
	echo "$dash"

	if [ "$DISTRIB_RELEASE" != "" ]; then
		echo "OpenWrt - $DISTRIB_RELEASE"
	else
		echo "OpenWrt - unknown"
	fi

	echo ""
	echo "Output of \"$ip a show\""
	echo "$dash"
	output="$($ip a show)"
	if [ "$output" != "" ]; then
		echo "$output"
	else
		echo "No data found"
	fi

	echo ""
	echo "Output of \"$ip route show\""
	echo "$dash"
	output="$($ip route show)"
	if [ "$output" != "" ]; then
		echo "$output"
	else
		echo "No data found"
	fi

	echo ""
	echo "Output of \"$ip rule show\""
	echo "$dash"
	output="$($ip rule show)"
	if [ "$output" != "" ]; then
		echo "$output"
	else
		echo "No data found"
	fi

	echo ""
	echo "Output of \"$ip route list table 1-250\""
	echo "$dash"
	local dump=0
	for i in $(seq 1 250); do
		output=$($ip route list table $i 2>/dev/null)
		if [ "$output" != "" ];then
			dump=1
			echo "Routing table $i:"
			echo "$output"
			echo ""
		fi
	done
	if [ "$dump" = "0" ]; then
		echo "No data found"
		echo ""
	fi

	echo "Output of \"$ipt -L -v -n\""
	echo "$dash"
	output="$($ipt -L -v -n)"
	if [ "$output" != "" ]; then
		echo "$output"
	else
		echo "No data found"
	fi
}

start() {
	/etc/init.d/mwan3 enable
	/etc/init.d/mwan3 start
}

stop() {
	/etc/init.d/mwan3 disable
	/etc/init.d/mwan3 stop
}

restart() {
	/etc/init.d/mwan3 enable
	/etc/init.d/mwan3 stop
	/etc/init.d/mwan3 start
}

use() {
	# Run a command with the device, src_ip and fwmark set to avoid processing by mwan3
	# firewall rules

	local interface device src_ip family

	interface=$1 ; shift
	[ -z "$*" ] && echo "no command specified for mwan3 use" >&2 && return
	network_get_device device $interface
	[ -z "$device" ] && echo "could not find device for $interface" >&2 && return

	mwan3_get_src_ip src_ip $interface
	[ -z "$src_ip" ] && echo "could not find src_ip for $interface" >&2 && return

	config_get family $interface family
	[ -z "$family" ] && echo "could not find family for $interface. Using ipv4." >&2 && family='ipv4'

	echo "Running '$*' with DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT FAMILY=$family" >&2
	# if a program (not a shell builtin) is run: use "exec" for allowing direct process control
	if [ -x "$(command -v "$1")" ]; then
		set -- exec "$@"
	fi
	FAMILY=$family DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT LD_PRELOAD=/lib/mwan3/libwrap_mwan3_sockopt.so.1.0 "$@"
}

case "$1" in
	ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart|internal)
		mwan3_init
		"$@"
	;;
	use)
		mwan3_init
		"$@"
		# Propagate mwan3 use command exit code
		exit "$?"
	;;
	*)
		help
	;;
esac

exit 0