Ver código fonte

mwan3: Update to version 1.5-1

Add iptables -w option, which increases stability, requires iptables v1.4.20.
Code cleanup

Signed-off-by: Jeroen Louwes <jeroen.louwes@gmail.com>
Adze1502 10 anos atrás
pai
commit
869e4539f6

+ 5
- 3
net/mwan3/Makefile Ver arquivo

@@ -8,8 +8,8 @@
8 8
 include $(TOPDIR)/rules.mk
9 9
 
10 10
 PKG_NAME:=mwan3
11
-PKG_VERSION:=1.4
12
-PKG_RELEASE:=22
11
+PKG_VERSION:=1.5
12
+PKG_RELEASE:=1
13 13
 PKG_MAINTAINER:=Jeroen Louwes <jeroen.louwes@gmail.com>
14 14
 PKG_LICENSE:=GPLv2
15 15
 
@@ -26,7 +26,9 @@ define Package/mwan3
26 26
 endef
27 27
 
28 28
 define Package/mwan3/description
29
-   Hotplug script which makes configuration of multiple WAN interfaces simple and manageable. With loadbalancing/failover support for up to 250 wan interfaces, connection tracking and an easy to manage traffic ruleset.
29
+	Hotplug script which makes configuration of multiple WAN interfaces simple
30
+	and manageable. With loadbalancing/failover support for up to 250 wan
31
+	interfaces, connection tracking and an easy to manage traffic ruleset.
30 32
 endef
31 33
 
32 34
 define Package/mwan3/conffiles

+ 93
- 93
net/mwan3/files/etc/hotplug.d/iface/15-mwan3 Ver arquivo

@@ -6,68 +6,61 @@ mwan3_get_iface_id()
6 6
 	[ "$1" == "$INTERFACE" ] && iface_id=$iface_count
7 7
 }
8 8
 
9
-mwan3_get_route_args()
10
-{
11
-	route_args=$(ip -4 route list dev $DEVICE default | head -1 | sed '/.*via \([^ ]*\) .*$/!d;s//\1/;q' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
12
-	[ -n "$route_args" ] && route_args="via $route_args"
13
-	route_args="nexthop $route_args dev $DEVICE"
14
-}
15
-
16 9
 mwan3_set_general_iptables()
17 10
 {
18
-	if ! iptables -S mwan3_ifaces -t mangle &> /dev/null; then
19
-		iptables -N mwan3_ifaces -t mangle
11
+	if ! $IPT -S mwan3_ifaces &> /dev/null; then
12
+		$IPT -N mwan3_ifaces
20 13
 	fi
21 14
 
22
-	if ! iptables -S mwan3_rules -t mangle &> /dev/null; then
23
-		iptables -N mwan3_rules -t mangle
15
+	if ! $IPT -S mwan3_rules &> /dev/null; then
16
+		$IPT -N mwan3_rules
24 17
 	fi
25 18
 
26
-	if ! iptables -S mwan3_connected -t mangle &> /dev/null; then
27
-		iptables -N mwan3_connected -t mangle
19
+	if ! $IPT -S mwan3_connected &> /dev/null; then
20
+		$IPT -N mwan3_connected
28 21
 	fi
29 22
 
30
-	if ! iptables -S mwan3_hook -t mangle &> /dev/null; then
31
-		iptables -N mwan3_hook -t mangle
32
-		iptables -A mwan3_hook -t mangle -j CONNMARK --restore-mark --nfmask 0xff00 --ctmask 0xff00
33
-		iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_ifaces
34
-		iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_connected
35
-		iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_rules
36
-		iptables -A mwan3_hook -t mangle -j CONNMARK --save-mark --nfmask 0xff00 --ctmask 0xff00
23
+	if ! $IPT -S mwan3_hook &> /dev/null; then
24
+		$IPT -N mwan3_hook
25
+		$IPT -A mwan3_hook -j CONNMARK --restore-mark --nfmask 0xff00 --ctmask 0xff00
26
+		$IPT -A mwan3_hook -m mark --mark 0x0/0xff00 -j mwan3_ifaces
27
+		$IPT -A mwan3_hook -m mark --mark 0x0/0xff00 -j mwan3_connected
28
+		$IPT -A mwan3_hook -m mark --mark 0x0/0xff00 -j mwan3_rules
29
+		$IPT -A mwan3_hook -j CONNMARK --save-mark --nfmask 0xff00 --ctmask 0xff00
37 30
 	fi
38 31
 
39
-	if ! iptables -S mwan3_track_hook -t mangle &> /dev/null; then
40
-		iptables -N mwan3_track_hook -t mangle
32
+	if ! $IPT -S mwan3_track_hook &> /dev/null; then
33
+		$IPT -N mwan3_track_hook
41 34
 	fi
42 35
 
43
-	if ! iptables -S PREROUTING -t mangle | grep mwan3_hook &> /dev/null; then
44
-		iptables -A PREROUTING -t mangle -j mwan3_hook
36
+	if ! $IPT -S PREROUTING | grep mwan3_hook &> /dev/null; then
37
+		$IPT -A PREROUTING -j mwan3_hook
45 38
 	fi
46 39
 
47
-	if ! iptables -S OUTPUT -t mangle | grep mwan3_hook &> /dev/null; then
48
-		iptables -A OUTPUT -t mangle -j mwan3_hook
40
+	if ! $IPT -S OUTPUT | grep mwan3_hook &> /dev/null; then
41
+		$IPT -A OUTPUT -j mwan3_hook
49 42
 	fi
50 43
 
51
-	if ! iptables -S OUTPUT -t mangle | grep mwan3_track_hook &> /dev/null; then
52
-		iptables -A OUTPUT -t mangle -j mwan3_track_hook
44
+	if ! $IPT -S OUTPUT | grep mwan3_track_hook &> /dev/null; then
45
+		$IPT -A OUTPUT -j mwan3_track_hook
53 46
 	fi
54 47
 
55
-	iptables -F mwan3_rules -t mangle
48
+	$IPT -F mwan3_rules
56 49
 }
57 50
 
58 51
 mwan3_set_connected_iptables()
59 52
 {
60 53
 	local connected_networks
61 54
 
62
-	if iptables -S mwan3_connected -t mangle &> /dev/null; then
63
-		iptables -F mwan3_connected -t mangle
55
+	if $IPT -S mwan3_connected &> /dev/null; then
56
+		$IPT -F mwan3_connected
64 57
 
65
-		for connected_networks in $(ip -4 route | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
66
-			iptables -A mwan3_connected -t mangle -d $connected_networks -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
58
+		for connected_networks in $($IP route | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
59
+			$IPT -A mwan3_connected -d $connected_networks -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
67 60
 		done
68 61
 
69
-		iptables -I mwan3_connected -t mangle -d 224.0.0.0/3 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
70
-		iptables -I mwan3_connected -t mangle -d 127.0.0.0/8 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
62
+		$IPT -I mwan3_connected -d 224.0.0.0/3 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
63
+		$IPT -I mwan3_connected -d 127.0.0.0/8 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
71 64
 	fi
72 65
 }
73 66
 
@@ -75,56 +68,56 @@ mwan3_set_iface_iptables()
75 68
 {
76 69
 	local local_net local_nets
77 70
 
78
-	local_net=$(ip -4 route list dev $DEVICE scope link | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
71
+	local_net=$($IP route list dev $DEVICE scope link | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
79 72
 
80
-	if ! iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
81
-		iptables -N mwan3_iface_$INTERFACE -t mangle
73
+	if ! $IPT -S mwan3_iface_$INTERFACE &> /dev/null; then
74
+		$IPT -N mwan3_iface_$INTERFACE
82 75
 	fi
83 76
 
84
-	iptables -F mwan3_iface_$INTERFACE -t mangle
85
-	iptables -D mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE &> /dev/null
77
+	$IPT -F mwan3_iface_$INTERFACE
78
+	$IPT -D mwan3_ifaces -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE &> /dev/null
86 79
 
87 80
 	if [ $ACTION == "ifup" ]; then
88 81
 		if [ -n "$local_net" ]; then
89 82
 			for local_nets in $local_net ; do
90 83
 				if [ $ACTION == "ifup" ]; then
91
-					iptables -I mwan3_iface_$INTERFACE -t mangle -s $local_net -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark 0xff00/0xff00
84
+					$IPT -I mwan3_iface_$INTERFACE -s $local_net -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark 0xff00/0xff00
92 85
 				fi
93 86
 			done
94 87
 		fi
95 88
 
96
-		iptables -A mwan3_iface_$INTERFACE -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark $(($iface_id*256))/0xff00
97
-		iptables -A mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE
89
+		$IPT -A mwan3_iface_$INTERFACE -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark $(($iface_id*256))/0xff00
90
+		$IPT -A mwan3_ifaces -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE
98 91
 	fi
99 92
 
100 93
 	if [ $ACTION == "ifdown" ]; then
101
-		iptables -X mwan3_iface_$INTERFACE -t mangle
94
+		$IPT -X mwan3_iface_$INTERFACE
102 95
 	fi
103 96
 }
104 97
 
105 98
 mwan3_set_iface_route()
106 99
 {
107
-	ip -4 route flush table $iface_id
108
-	[ $ACTION == "ifup" ] && ip -4 route add table $iface_id default $route_args
100
+	$IP route flush table $iface_id
101
+	[ $ACTION == "ifup" ] && $IP route add table $iface_id default $route_args
109 102
 }
110 103
 
111 104
 mwan3_set_iface_rules()
112 105
 {
113
-	while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+1000)):'"')" ]; do
114
-		ip -4 rule del pref $(($iface_id+1000))
106
+	while [ -n "$($IP rule list | awk '$1 == "'$(($iface_id+1000)):'"')" ]; do
107
+		$IP rule del pref $(($iface_id+1000))
115 108
 	done
116 109
 
117
-	while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+2000)):'"')" ]; do
118
-		ip -4 rule del pref $(($iface_id+2000))
110
+	while [ -n "$($IP rule list | awk '$1 == "'$(($iface_id+2000)):'"')" ]; do
111
+		$IP rule del pref $(($iface_id+2000))
119 112
 	done
120 113
 
121
-	while [ -n "$(ip -4 rule list | awk '$1 == "2254:"')" ]; do
122
-		ip -4 rule del pref 2254
114
+	while [ -n "$($IP rule list | awk '$1 == "2254:"')" ]; do
115
+		$IP rule del pref 2254
123 116
 	done
124 117
 
125
-	[ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+1000)) iif $DEVICE lookup main
126
-	[ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+2000)) fwmark $(($iface_id*256))/0xff00 lookup $iface_id
127
-        ip rule add pref 2254 fwmark 0xfe00/0xff00 unreachable                                   
118
+	[ $ACTION == "ifup" ] && $IP rule add pref $(($iface_id+1000)) iif $DEVICE lookup main
119
+	[ $ACTION == "ifup" ] && $IP rule add pref $(($iface_id+2000)) fwmark $(($iface_id*256))/0xff00 lookup $iface_id
120
+        $IP rule add pref 2254 fwmark 0xfe00/0xff00 unreachable                                   
128 121
 }
129 122
 
130 123
 mwan3_track()
@@ -145,28 +138,28 @@ mwan3_track()
145 138
 		config_get down $INTERFACE down 5
146 139
 		config_get up $INTERFACE up 5
147 140
 
148
-		if ! iptables -S mwan3_track_$INTERFACE -t mangle &> /dev/null; then
149
-			iptables -N mwan3_track_$INTERFACE -t mangle
150
-			iptables -A mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE
141
+		if ! $IPT -S mwan3_track_$INTERFACE &> /dev/null; then
142
+			$IPT -N mwan3_track_$INTERFACE
143
+			$IPT -A mwan3_track_hook -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE
151 144
 		fi
152 145
 
153
-		iptables -F mwan3_track_$INTERFACE -t mangle
146
+		$IPT -F mwan3_track_$INTERFACE
154 147
 
155 148
 		for track_ip in $track_ips; do
156
-			iptables -A mwan3_track_$INTERFACE -t mangle -d $track_ip -j MARK --set-xmark 0xff00/0xff00
149
+			$IPT -A mwan3_track_$INTERFACE -d $track_ip -j MARK --set-xmark 0xff00/0xff00
157 150
 		done
158 151
 
159 152
 		[ -x /usr/sbin/mwan3track ] && /usr/sbin/mwan3track $INTERFACE $DEVICE $reliability $count $timeout $interval $down $up $track_ips &
160 153
 	else
161
-		iptables -D mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE &> /dev/null
162
-		iptables -F mwan3_track_$INTERFACE -t mangle &> /dev/null
163
-		iptables -X mwan3_track_$INTERFACE -t mangle &> /dev/null
154
+		$IPT -D mwan3_track_hook -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE &> /dev/null
155
+		$IPT -F mwan3_track_$INTERFACE &> /dev/null
156
+		$IPT -X mwan3_track_$INTERFACE &> /dev/null
164 157
 	fi
165 158
 }
166 159
 
167 160
 mwan3_set_policy()
168 161
 {
169
-	local iface_count iface_id metric probability weight
162
+	local iface_count iface_id INTERFACE metric probability weight
170 163
 
171 164
 	config_get INTERFACE $1 interface
172 165
 	config_get metric $1 metric 1
@@ -178,12 +171,12 @@ mwan3_set_policy()
178 171
 
179 172
 	[ -n "$iface_id" ] || return 0
180 173
 
181
-	if iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
174
+	if $IPT -S mwan3_iface_$INTERFACE &> /dev/null; then
182 175
 		if [ "$metric" -lt "$lowest_metric" ]; then
183 176
 
184 177
 			total_weight=$weight
185
-			iptables -F mwan3_policy_$policy -t mangle
186
-			iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE $weight $weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
178
+			$IPT -F mwan3_policy_$policy
179
+			$IPT -A mwan3_policy_$policy -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE $weight $weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
187 180
 
188 181
 			lowest_metric=$metric
189 182
 
@@ -204,7 +197,7 @@ mwan3_set_policy()
204 197
 
205 198
 			probability="-m statistic --mode random --probability $probability"
206 199
 			
207
-			iptables -I mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 $probability -m comment --comment "$INTERFACE $weight $total_weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
200
+			$IPT -I mwan3_policy_$policy -m mark --mark 0x0/0xff00 $probability -m comment --comment "$INTERFACE $weight $total_weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
208 201
 		fi
209 202
 	fi
210 203
 }
@@ -216,22 +209,22 @@ mwan3_set_policies_iptables()
216 209
 	policy=$1
217 210
 
218 211
 	if [ "$policy" != $(echo "$policy" | cut -c1-15) ]; then
219
-		logger -t mwan3 -p warn "Policy $policy exceeds max of 15 chars. Not setting policy" && return 0
212
+		$LOG warn "Policy $policy exceeds max of 15 chars. Not setting policy" && return 0
220 213
 	fi
221 214
 
222
-	if ! iptables -S mwan3_policy_$policy -t mangle &> /dev/null; then
223
-		iptables -N mwan3_policy_$policy -t mangle
215
+	if ! $IPT -S mwan3_policy_$policy &> /dev/null; then
216
+		$IPT -N mwan3_policy_$policy
224 217
 	fi
225 218
 
226
-	iptables -F mwan3_policy_$policy -t mangle
227
-	iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "unreachable" -j MARK --set-xmark 0xfe00/0xff00
219
+	$IPT -F mwan3_policy_$policy
220
+	$IPT -A mwan3_policy_$policy -m mark --mark 0x0/0xff00 -m comment --comment "unreachable" -j MARK --set-xmark 0xfe00/0xff00
228 221
 
229 222
 	lowest_metric=256
230 223
 	total_weight=0
231 224
 
232 225
 	config_list_foreach $policy use_member mwan3_set_policy
233 226
 
234
-	iptables -X $policy -t mangle &> /dev/null
227
+	$IPT -X $policy &> /dev/null
235 228
 }
236 229
 
237 230
 mwan3_set_user_rules_iptables()
@@ -256,10 +249,10 @@ mwan3_set_user_rules_iptables()
256 249
 
257 250
 		case $proto in
258 251
 			tcp|udp)
259
-			iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m multiport --sports $src_port -m multiport --dports $dest_port -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
252
+			$IPT -A mwan3_rules -p $proto -s $src_ip -d $dest_ip -m multiport --sports $src_port -m multiport --dports $dest_port -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
260 253
 			;;
261 254
 			*)
262
-			iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
255
+			$IPT -A mwan3_rules -p $proto -s $src_ip -d $dest_ip -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
263 256
 			;;
264 257
 		esac
265 258
 	fi
@@ -269,45 +262,41 @@ mwan3_ifupdown()
269 262
 {
270 263
 	local counter enabled iface_count iface_id route_args wan_metric
271 264
 
272
-	[ -n "$DEVICE" ] || exit 0
273
-	[ -n "$INTERFACE" ] || exit 0
274
-	[ "$(uci get -P /var/state mwan3.$INTERFACE 2> /dev/null)" == "interface" ] || return 0
275
-
276 265
 	config_load mwan3
266
+	config_foreach mwan3_get_iface_id interface
267
+
268
+	[ -n "$iface_id" ] || return 0
269
+	[ "$iface_count" -le 250 ] || return 0
270
+	unset iface_count
271
+
277 272
 	config_get enabled $INTERFACE enabled 0
278 273
 
279 274
 	counter=0
280 275
 
281 276
 	if [ $ACTION == "ifup" ]; then
282
-		[ "$enabled" -eq 1 ] || exit 0
277
+		[ "$enabled" -eq 1 ] || return 0
283 278
 
284
-		while [ -z "$(ip -4 route list dev $DEVICE default | head -1)" -a "$counter" -lt 10 ]; do
279
+		while [ -z "$($IP route list dev $DEVICE default | head -1)" -a "$counter" -lt 10 ]; do
285 280
 			sleep 1
286 281
 			let counter++
287 282
 			if [ "$counter" -ge 10 ]; then
288
-				logger -t mwan3 -p warn "Could not find gateway for interface $INTERFACE ($DEVICE)" && exit 0
283
+				$LOG warn "Could not find gateway for interface $INTERFACE ($DEVICE)" && return 0
289 284
 			fi
290 285
 		done
291 286
 
292
-		mwan3_get_route_args
287
+		route_args=$($IP route list dev $DEVICE default | head -1 | sed '/.*via \([^ ]*\) .*$/!d;s//via \1/;q' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
288
+		route_args="nexthop $route_args dev $DEVICE"
293 289
 	fi
294 290
 
295 291
 	while [ "$(pgrep -f -o hotplug-call)" -ne $$ -a "$counter" -lt 60 ]; do
296 292
 		sleep 1
297 293
 		let counter++
298 294
 		if [ "$counter" -ge 60 ]; then
299
-			logger -t mwan3 -p warn "Timeout waiting for older hotplug processes to finish. $ACTION interface $INTERFACE ($DEVICE) aborted" && exit 0
295
+			$LOG warn "Timeout waiting for older hotplug processes to finish. $ACTION interface $INTERFACE ($DEVICE) aborted" && return 0
300 296
 		fi
301 297
 	done
302 298
 
303
-	config_foreach mwan3_get_iface_id interface
304
-
305
-	[ -n "$iface_id" ] || exit 0
306
-	[ "$iface_count" -le 250 ] || exit 0
307
-	unset iface_count
308
-	unset counter
309
-
310
-	logger -t mwan3 -p notice "$ACTION interface $INTERFACE ($DEVICE)"
299
+	$LOG notice "$ACTION interface $INTERFACE ($DEVICE)"
311 300
 
312 301
 	mwan3_set_general_iptables
313 302
 	mwan3_set_iface_iptables
@@ -320,9 +309,20 @@ mwan3_ifupdown()
320 309
 	config_foreach mwan3_set_user_rules_iptables rule
321 310
 }
322 311
 
312
+[ -n "$DEVICE" ] || exit 0
313
+[ -n "$INTERFACE" ] || exit 0
314
+
315
+local IP IPT LOG
316
+
317
+IP="/usr/sbin/ip -4"
318
+IPT="/usr/sbin/iptables -t mangle -w"
319
+LOG="/usr/bin/logger -t mwan3 -p"
320
+
323 321
 case "$ACTION" in
324 322
 	ifup|ifdown)
325 323
 		mwan3_ifupdown
326 324
 		mwan3_set_connected_iptables
327 325
 	;;
328 326
 esac
327
+
328
+exit 0

+ 0
- 1
net/mwan3/files/etc/init.d/mwan3 Ver arquivo

@@ -15,6 +15,5 @@ restart() {
15 15
 }
16 16
 
17 17
 boot() {
18
-	# Don't start on boot, mwan3 is started by hotplug event.
19 18
 	return 0
20 19
 }

+ 33
- 34
net/mwan3/files/usr/sbin/mwan3 Ver arquivo

@@ -16,10 +16,13 @@ EOF
16 16
 
17 17
 EXTRA_COMMANDS="ifdown ifup interfaces policies rules status"
18 18
 EXTRA_HELP="$(extra_help)"
19
-
19
+IP="/usr/sbin/ip -4"
20
+IPT="/usr/sbin/iptables -t mangle -w"
20 21
 
21 22
 ifdown()
22 23
 {
24
+	local device
25
+
23 26
 	if [ -z "$1" ]; then
24 27
 		echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>" && exit 0
25 28
 	fi
@@ -28,8 +31,6 @@ ifdown()
28 31
 		echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>" && exit 0
29 32
 	fi
30 33
 
31
-	local device
32
-	
33 34
 	device=$(uci get -p /var/state network.$1.ifname) &> /dev/null
34 35
 	
35 36
 	if [ -e /var/run/mwan3track-$1.pid ] ; then
@@ -44,6 +45,8 @@ ifdown()
44 45
 
45 46
 ifup()
46 47
 {
48
+	local device enabled
49
+
47 50
 	config_load mwan3
48 51
 
49 52
 	if [ -z "$1" ]; then
@@ -53,8 +56,6 @@ ifup()
53 56
 	if [ -n "$2" ]; then
54 57
 		echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
55 58
 	fi
56
-
57
-	local device enabled
58 59
 	
59 60
 	config_get enabled "$1" enabled 0
60 61
 
@@ -67,10 +68,10 @@ ifup()
67 68
 
68 69
 interfaces()
69 70
 {
70
-	config_load mwan3
71
-
72 71
 	local device enabled iface_id tracking
73 72
 
73
+	config_load mwan3
74
+
74 75
 	echo "Interface status:"
75 76
 	
76 77
 	check_iface_status()
@@ -91,13 +92,13 @@ interfaces()
91 92
 			tracking="down"
92 93
 		fi
93 94
 
94
-		if [ -n "$(ip rule | awk '$5 == ("'$device'")')" -a -n "$(iptables -S mwan3_iface_$1 -t mangle 2> /dev/null)" -a -n "$(ip -4 route list table $iface_id default dev $device 2> /dev/null)" ]; then
95
+		if [ -n "$($IP rule | awk '$5 == ("'$device'")')" -a -n "$($IPT -S mwan3_iface_$1 2> /dev/null)" -a -n "$($IP route list table $iface_id default dev $device 2> /dev/null)" ]; then
95 96
 			if [ -n "$(uci get -p /var/state mwan3.$1.track_ip 2> /dev/null)" ]; then
96 97
 				echo "Interface $1 is online (tracking $tracking)"
97 98
 			else
98 99
 				echo "Interface $1 is online"
99 100
 			fi
100
-		elif [ -n "$(ip rule | awk '$5 == ("'$device'")')" -o -n "$(iptables -S mwan3_iface_$1 -t mangle 2> /dev/null)" -o -n "$(ip -4 route list table $iface_id default dev $device 2> /dev/null)" ]; then
101
+		elif [ -n "$($IP rule | awk '$5 == ("'$device'")')" -o -n "$($IPT -S mwan3_iface_$1 2> /dev/null)" -o -n "$($IP route list table $iface_id default dev $device 2> /dev/null)" ]; then
101 102
 			echo "Interface $1 error"
102 103
 		else
103 104
 			if [ "$enabled" -eq 1 ]; then
@@ -119,21 +120,21 @@ policies()
119 120
 {
120 121
 	local percent policy share total_weight weight iface
121 122
 
122
-	for policy in $(iptables -S -t mangle | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
123
+	for policy in $($IPT -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
123 124
 		echo "Policy $policy:" | sed 's/mwan3_policy_//g'
124 125
 
125
-		for iface in $(iptables -S $policy -t mangle | cut -s -d'"' -f2 | awk '{print $1}'); do
126
-			[ -n "$total_weight" ] || total_weight=$(iptables -S $policy -t mangle | grep "$iface " | cut -s -d'"' -f2 | awk '{print $3}')
126
+		for iface in $($IPT -S $policy | cut -s -d'"' -f2 | awk '{print $1}'); do
127
+			[ -n "$total_weight" ] || total_weight=$($IPT -S $policy | grep "$iface " | cut -s -d'"' -f2 | awk '{print $3}')
127 128
 		done
128 129
 
129 130
 		if [ ! -z "${total_weight##*[!0-9]*}" ]; then
130
-			for iface in $(iptables -S $policy -t mangle | cut -s -d'"' -f2 | awk '{print $1}'); do
131
-				weight=$(iptables -S $policy -t mangle | grep "$iface " | cut -s -d'"' -f2 | awk '{print $2}')
131
+			for iface in $($IPT -S $policy | cut -s -d'"' -f2 | awk '{print $1}'); do
132
+				weight=$($IPT -S $policy | grep "$iface " | cut -s -d'"' -f2 | awk '{print $2}')
132 133
 				percent=$(($weight*100/$total_weight))
133 134
 				echo " $iface ($percent%)"
134 135
 			done
135 136
 		else
136
-			echo " $(iptables -S $policy -t mangle | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
137
+			echo " $($IPT -S $policy | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
137 138
 		fi
138 139
 
139 140
 		echo -e
@@ -144,19 +145,17 @@ policies()
144 145
 }
145 146
 rules()
146 147
 {
147
-	if [ -n "$(iptables -S mwan3_connected -t mangle 2> /dev/null)" ]; then
148
+	if [ -n "$($IPT -S mwan3_connected 2> /dev/null)" ]; then
148 149
 		echo "Known networks:"
149
-		echo "destination policy hits" | awk '{ printf "%-19s%-19s%-9s%s\n",$1,$2,$3}'
150
-		echo "------------------------------------------------"
151
-		iptables -L mwan3_connected -t mangle -n -v 2> /dev/null | tail -n+3 | sed 's/mark.*//' | sed 's/mwan3_policy_//g' | awk '{printf "%-19s%-19s%-9s%s\n",$9,"default",$1}'
150
+		echo "destination policy hits" | awk '{ printf "%-19s%-19s%-9s%s\n",$1,$2,$3}' | awk '1; {gsub(".","-")}1'
151
+		$IPT -L mwan3_connected -n -v 2> /dev/null | tail -n+3 | sed 's/mark.*//' | sed 's/mwan3_policy_//g' | awk '{printf "%-19s%-19s%-9s%s\n",$9,"default",$1}'
152 152
 		echo -e
153 153
 	fi
154 154
 
155
-	if [ -n "$(iptables -S mwan3_rules -t mangle 2> /dev/null)" ]; then
155
+	if [ -n "$($IPT -S mwan3_rules 2> /dev/null)" ]; then
156 156
 		echo "Active rules:"
157
-		echo "source destination proto src-port dest-port policy hits" | awk '{ printf "%-19s%-19s%-7s%-14s%-14s%-16s%-9s%s\n",$1,$2,$3,$4,$5,$6,$7}'
158
-		echo "---------------------------------------------------------------------------------------------------"
159
-		iptables -L mwan3_rules -t mangle -n -v 2> /dev/null | tail -n+3 | sed 's/mark.*//' | sed 's/mwan3_policy_//g' | awk '{ printf "%-19s%-19s%-7s%-14s%-14s%-16s%-9s%s\n",$8,$9,$4,$12,$15,$3,$1}'
157
+		echo "source destination proto src-port dest-port policy hits" | awk '{ printf "%-19s%-19s%-7s%-14s%-14s%-16s%-9s%s\n",$1,$2,$3,$4,$5,$6,$7}' | awk '1; {gsub(".","-")}1'
158
+		$IPT -L mwan3_rules -n -v 2> /dev/null | tail -n+3 | sed 's/mark.*//' | sed 's/mwan3_policy_//g' | awk '{ printf "%-19s%-19s%-7s%-14s%-14s%-16s%-9s%s\n",$8,$9,$4,$12,$15,$3,$1}'
160 159
 		echo -e
161 160
 	fi
162 161
 }
@@ -181,24 +180,24 @@ stop()
181 180
 	killall mwan3track &> /dev/null
182 181
 	rm /var/run/mwan3track-* &> /dev/null
183 182
 
184
-	for route in $(ip route list table all | sed 's/.*table \([^ ]*\) .*/\1/' |  awk '{print $1}' | awk '{for(i=1;i<=NF;i++) if($i+0>0) if($i+0<255) {print;break}}'); do
185
-		ip -4 route flush table $route &> /dev/null
183
+	for route in $($IP route list table all | sed 's/.*table \([^ ]*\) .*/\1/' |  awk '{print $1}' | awk '{for(i=1;i<=NF;i++) if($i+0>0) if($i+0<255) {print;break}}'); do
184
+		$IP route flush table $route &> /dev/null
186 185
 	done
187 186
 
188
-	for rule in $(ip -4 rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
189
-		ip -4 rule del pref $rule &> /dev/null
187
+	for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
188
+		$IP rule del pref $rule &> /dev/null
190 189
 	done
191 190
 
192
-	iptables -D PREROUTING -t mangle -j mwan3_hook &> /dev/null
193
-	iptables -D OUTPUT -t mangle -j mwan3_hook &> /dev/null
194
-	iptables -D OUTPUT -t mangle -j mwan3_track_hook &> /dev/null
191
+	$IPT -D PREROUTING -j mwan3_hook &> /dev/null
192
+	$IPT -D OUTPUT -j mwan3_hook &> /dev/null
193
+	$IPT -D OUTPUT -j mwan3_track_hook &> /dev/null
195 194
 
196
-	for table in $(iptables -S -t mangle | awk '{print $2}' | grep mwan3 | sort -u); do
197
-		iptables -F $table -t mangle &> /dev/null
195
+	for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
196
+		$IPT -F $table &> /dev/null
198 197
 	done
199 198
 
200
-	for table in $(iptables -S -t mangle | awk '{print $2}' | grep mwan3 | sort -u); do
201
-		iptables -X $table -t mangle &> /dev/null
199
+	for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
200
+		$IPT -X $table &> /dev/null
202 201
 	done
203 202
 }
204 203