blob: e884d22f6be01846e7f982aad3e6093a893e0140 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2
3. /lib/functions.sh
4. /usr/share/libubox/jshn.sh
5. /lib/functions/network.sh
6. /lib/mwan3/mwan3.sh
7
8command_help() {
9 local cmd="$1"
10 local help="$2"
11
12 echo "$(printf "%-25s%s" "${cmd}" "${help}")"
13}
14
15help()
16{
17 cat <<EOF
18Syntax: mwan3 [command]
19
20Available commands:
21EOF
22 command_help "start" "Load iptables rules, ip rules and ip routes"
23 command_help "stop" "Unload iptables rules, ip rules and ip routes"
24 command_help "restart" "Reload iptables rules, ip rules and ip routes"
25 command_help "ifup <iface>" "Load rules and routes for specific interface"
26 command_help "ifdown <iface>" "Unload rules and routes for specific interface"
27 command_help "interfaces" "Show interfaces status"
28 command_help "policies" "Show currently active policy"
29 command_help "connected" "Show directly connected networks"
30 command_help "rules" "Show active rules"
31 command_help "status" "Show all status"
32 command_help "internal <ipv4|ipv6>" "Show internal configuration <default: ipv4>"
33 command_help "use <iface> <cmd>" "Run a command bound to <iface> and avoid mwan3 rules"
34}
35
36ifdown() {
37 if [ -z "$1" ]; then
38 echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>"
39 exit 0
40 fi
41
42 if [ -n "$2" ]; then
43 echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>"
44 exit 0
45 fi
46
47 mwan3_interface_hotplug_shutdown "$1" 1
48}
49
50ifup() {
51 . /etc/init.d/mwan3
52
53 if [ -z "$1" ]; then
54 echo "Expecting interface. Usage: mwan3 ifup <interface>"
55 exit 0
56 fi
57
58 if [ -n "$2" ]; then
59 echo "Too many arguments. Usage: mwan3 ifup <interface>"
60 exit 0
61 fi
62
63 mwan3_ifup "$1" "cmd"
64}
65
66interfaces()
67{
68 config_load mwan3
69
70 echo "Interface status:"
71 config_foreach mwan3_report_iface_status interface
72 echo
73}
74
75policies()
76{
77 echo "Current ipv4 policies:"
78 mwan3_report_policies_v4
79 echo
80 [ $NO_IPV6 -ne 0 ] && return
81 echo "Current ipv6 policies:"
82 mwan3_report_policies_v6
83 echo
84}
85
86connected()
87{
88 echo "Directly connected ipv4 networks:"
89 mwan3_report_connected_v4
90 echo
91 [ $NO_IPV6 -ne 0 ] && return
92 echo "Directly connected ipv6 networks:"
93 mwan3_report_connected_v6
94 echo
95}
96
97rules()
98{
99 echo "Active ipv4 user rules:"
100 mwan3_report_rules_v4
101 echo
102 [ $NO_IPV6 -ne 0 ] && return
103 echo "Active ipv6 user rules:"
104 mwan3_report_rules_v6
105 echo
106}
107
108status()
109{
110 interfaces
111 policies
112 connected
113 rules
114}
115
116internal()
117{
118 local family="$1"
119 local dash="-------------------------------------------------"
120
121 if [ -f "/etc/openwrt_release" ]; then
122 . /etc/openwrt_release
123 fi
124
125 local ipt ip output
126
127 if [ "$family" = "ipv6" ]; then
128 ipt="$IPT6"
129 ip="$IP6"
130 else
131 ipt="$IPT4"
132 ip="$IP4"
133 fi
134
135 echo "Software-Version"
136 echo "$dash"
137
138 if [ "$DISTRIB_RELEASE" != "" ]; then
139 echo "OpenWrt - $DISTRIB_RELEASE"
140 else
141 echo "OpenWrt - unknown"
142 fi
143
144 echo ""
145 echo "Output of \"$ip a show\""
146 echo "$dash"
147 output="$($ip a show)"
148 if [ "$output" != "" ]; then
149 echo "$output"
150 else
151 echo "No data found"
152 fi
153
154 echo ""
155 echo "Output of \"$ip route show\""
156 echo "$dash"
157 output="$($ip route show)"
158 if [ "$output" != "" ]; then
159 echo "$output"
160 else
161 echo "No data found"
162 fi
163
164 echo ""
165 echo "Output of \"$ip rule show\""
166 echo "$dash"
167 output="$($ip rule show)"
168 if [ "$output" != "" ]; then
169 echo "$output"
170 else
171 echo "No data found"
172 fi
173
174 echo ""
175 echo "Output of \"$ip route list table 1-250\""
176 echo "$dash"
177 local dump=0
178 for i in $(seq 1 250); do
179 output=$($ip route list table $i 2>/dev/null)
180 if [ "$output" != "" ];then
181 dump=1
182 echo "Routing table $i:"
183 echo "$output"
184 echo ""
185 fi
186 done
187 if [ "$dump" = "0" ]; then
188 echo "No data found"
189 echo ""
190 fi
191
192 echo "Output of \"$ipt -L -v -n\""
193 echo "$dash"
194 output="$($ipt -L -v -n)"
195 if [ "$output" != "" ]; then
196 echo "$output"
197 else
198 echo "No data found"
199 fi
200}
201
202start() {
203 /etc/init.d/mwan3 enable
204 /etc/init.d/mwan3 start
205}
206
207stop() {
208 /etc/init.d/mwan3 disable
209 /etc/init.d/mwan3 stop
210}
211
212restart() {
213 /etc/init.d/mwan3 enable
214 /etc/init.d/mwan3 stop
215 /etc/init.d/mwan3 start
216}
217
218use() {
219 # Run a command with the device, src_ip and fwmark set to avoid processing by mwan3
220 # firewall rules
221
222 local interface device src_ip family
223
224 interface=$1 ; shift
225 [ -z "$*" ] && echo "no command specified for mwan3 use" && return
226 network_get_device device $interface
227 [ -z "$device" ] && echo "could not find device for $interface" && return
228
229 mwan3_get_src_ip src_ip $interface
230 [ -z "$src_ip" ] && echo "could not find src_ip for $interface" && return
231
232 config_get family $interface family
233 [ -z "$family" ] && echo "could not find family for $interface. Using ipv4." && family='ipv4'
234
235 echo "Running '$*' with DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT FAMILY=$family"
236 # if a program (not a shell builtin) is run: use "exec" for allowing direct process control
237 if [ -x "$(command -v "$1")" ]; then
238 set -- exec "$@"
239 fi
240 FAMILY=$family DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT LD_PRELOAD=/lib/mwan3/libwrap_mwan3_sockopt.so.1.0 "$@"
241}
242
243case "$1" in
244 ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart|use|internal)
245 mwan3_init
246 "$@"
247 ;;
248 *)
249 help
250 ;;
251esac
252
253exit 0