6 if [ -z "$debug" -o "$debug" == "0" ]; then
14 logger
"[Multi-WAN Notice]: $1"
19 local failover_to_wanid
23 local existing_failover
25 failchk
=$
(query_config failchk
$2)
26 recvrychk
=$
(query_config recvrychk
$2)
28 wanid
=$
(query_config wanid
$2)
29 failover_to
=`uci -q -P /var/state get multiwan.${2}.failover_to`
30 failover_to_wanid
=$
(query_config wanid
$failover_to)
32 existing_failover
=$
(iptables
-n -L FW
${wanid}MARK
-t mangle |
echo $
(expr $
(wc -l) - 2))
36 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]//g")
37 wan_fail_map
=$
(echo $wan_fail_map${1}[x
])
38 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]//g")
41 if [ "$existing_failover" == "2" ]; then
42 if [ "$failover_to" != "balancer" -a "$failover_to" != "fastbalancer" -a "$failover_to" != "disable" -a "$failover_to_wanid" != "$wanid" ]; then
43 iptables
-I FW
${wanid}MARK
2 -t mangle
-j FW
${failover_to_wanid}MARK
44 elif [ "$failover_to" == "balancer" ]; then
45 iptables
-I FW
${wanid}MARK
2 -t mangle
-j LoadBalancer
46 elif [ "$failover_to" == "fastbalancer" ]; then
47 iptables
-I FW
${wanid}MARK
2 -t mangle
-j FastBalancer
50 mwnote
"$1 has failed and is currently offline."
55 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]//g")
56 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]//g")
59 if [ "$existing_failover" == "3" ]; then
60 iptables
-D FW
${wanid}MARK
2 -t mangle
62 mwnote
"$1 has recovered and is back online!"
75 local health_fail_retries
78 health_fail_retries
=`uci -q -P /var/state get multiwan.${1}.health_fail_retries`
79 weight
=`uci -q -P /var/state get multiwan.${1}.weight`
81 failchk
=$
(query_config failchk
$1)
82 recvrychk
=$
(query_config recvrychk
$1)
83 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]//g")
85 if [ -z "$failchk" ]; then
86 wan_fail_map
="$wan_fail_map${1}[1]"
88 if [ "$health_fail_retries" == "1" ]; then
92 if [ "$failchk" != "x" ]; then
93 new_fail_count
=$
(expr $failchk + 1)
94 if [ "$new_fail_count" -lt "$health_fail_retries" ]; then
95 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]/$1\[${new_fail_count}\]/g")
100 if [ "$weight" != "disable" ]; then
116 local health_recovery_retires
119 health_recovery_retries
=`uci -q -P /var/state get multiwan.${1}.health_recovery_retries`
120 weight
=`uci -q -P /var/state get multiwan.${1}.weight`
122 failchk
=$
(query_config failchk
$1)
123 recvrychk
=$
(query_config recvrychk
$1)
124 wanid
=$
(query_config wanid
$1)
126 if [ ! -z "$failchk" -a "$failchk" != "x" ]; then
127 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]//g")
131 if [ "$failchk" == "x" ]; then
132 if [ -z "$recvrychk" ]; then
133 wan_recovery_map
="$wan_recovery_map${1}[1]"
135 if [ "$health_recovery_retries" == "1" ]; then
139 new_recovery_count
=$
(expr $recvrychk + 1)
140 if [ "$new_recovery_count" -lt "$health_recovery_retries" ]; then
141 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]/$1\[${new_recovery_count}\]/g")
146 if [ "$weight" != "disable" ]; then
164 ifname
=`uci -q -P /var/state get network.${1}.ifname`
165 ipaddr
=`uci -q -P /var/state get network.${1}.ipaddr`
166 gateway
=`uci -q -P /var/state get network.${1}.gateway`
168 if [ -z "$ifname" ]; then
171 if [ -z "$ipaddr" ]; then
174 if [ -z "$gateway" ]; then
178 check_old_map
=`echo $wan_id_map 2>&1 | grep -o "$1\["`
180 if [ -z $check_old_map ]; then
181 wancount
=`expr $wancount + 1`
182 if [ $wancount -gt 20 ]; then
186 wan_if_map
="$wan_if_map${1}[${ifname}]"
187 wan_id_map
="$wan_id_map${1}[${wancount}]"
188 wan_gw_map
="$wan_gw_map${1}[${gateway}]"
189 wan_ip_map
="$wan_ip_map${1}[${ipaddr}]"
191 old_ipaddr
=$
(query_config ipaddr
$1)
192 old_gateway
=$
(query_config gateway
$1)
193 old_ifname
=$
(query_config ifname
$1)
194 get_wanid
=$
(query_config wanid
$1)
196 wan_if_map
=$
(echo $wan_if_map |
sed -e "s/${1}\[${old_ifname}\]/$1\[${ifname}\]/g")
197 wan_ip_map
=$
(echo $wan_ip_map |
sed -e "s/${1}\[${old_ipaddr}\]/$1\[${ipaddr}\]/g")
198 wan_gw_map
=$
(echo $wan_gw_map |
sed -e "s/${1}\[${old_gateway}\]/$1\[${gateway}\]/g")
200 if [ "$old_ifname" != "$ifname" ]; then
201 iptables
-D MultiWanPreHandler
-t mangle
-i $old_$ifname -m state
--state NEW
-j FW
${get_wanid}MARK
202 iptables
-A MultiWanPreHandler
-t mangle
-i $ifname -m state
--state NEW
-j FW
${get_wanid}MARK
203 iptables
-D MultiWanPostHandler
-t mangle
-o $old_$ifname -m mark
--mark 0x1 -j FW
${get_wanid}MARK
204 iptables
-A MultiWanPostHandler
-t mangle
-o $ifname -m mark
--mark 0x1 -j FW
${get_wanid}MARK
207 if [ "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
209 iprules_config
$get_wanid
223 if [ ! -d /tmp
/.mwan
]; then
224 mkdir
/tmp
/.mwan
> /dev
/null
2>&1
227 rm /tmp
/.mwan
/cache
> /dev
/null
2>&1
228 touch /tmp
/.mwan
/cache
230 echo "# Automatically Generated by Multi-WAN Agent Script. Do not modify or remove. #" > /tmp
/.mwan
/cache
231 echo "wan_id_map=\"$wan_id_map\"" >> /tmp
/.mwan
/cache
232 echo "wan_if_map=\"$wan_if_map\"" >> /tmp
/.mwan
/cache
233 echo "wan_ip_map=\"$wan_ip_map\"" >> /tmp
/.mwan
/cache
234 echo "wan_gw_map=\"$wan_gw_map\"" >> /tmp
/.mwan
/cache
235 echo "wan_fail_map=\"$wan_fail_map\"" >> /tmp
/.mwan
/cache
236 echo "wan_recovery_map=\"$wan_recovery_map\"" >> /tmp
/.mwan
/cache
242 update
) update_cache_data
;;
243 ifname
) echo $wan_if_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
244 ipaddr
) echo $wan_ip_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
245 gateway
) echo $wan_gw_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
246 wanid
) echo $wan_id_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
247 failchk
) echo $wan_fail_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
248 recvrychk
) echo $wan_recovery_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
249 group
) echo $wan_id_map |
grep -o "\w*\[$2\]" |
awk -F "[" '{print $1}';;
256 otherpids
=$
(ps
-a 2>&1 |
grep 'multiwan agent' |
grep -v $$ |
awk -F " " '{print $1}')
257 echo "$otherpids" |
while read execute
259 kill -9 ${execute} > /dev
/null
2>&1
270 if [ "$1" != "restart" ]; then
271 echo "## Refreshing Interfaces ##"
273 while [ $i -lt $wancount ]; do
275 group
=$
(query_config group
$i)
276 fdown
$group > /dev
/null
2>&1
277 ifup
$group > /dev
/null
2>&1 &
280 echo "## Unloaded, updating syslog and exiting. ##"
281 mwnote
"Succesfully Unloaded on $(exec date -R)."
283 rm -r /tmp
/.mwan
> /dev
/null
2>&1
287 echo "## Restarting Multi-WAN. ##"
288 mwnote
"Reinitializing Multi-WAN Configuration."
290 rm -r /tmp
/.mwan
> /dev
/null
2>&1
291 /etc
/init.d
/multiwan start
& > /dev
/null
2>&1
301 iptables
-t mangle
-F PREROUTING
302 iptables
-t mangle
-F FORWARD
303 iptables
-t mangle
-F POSTROUTING
304 iptables
-t mangle
-F OUTPUT
305 iptables
-t mangle
-F MultiWan
306 iptables
-t mangle
-X MultiWan
307 iptables
-t mangle
-F MultiWanRules
308 iptables
-t mangle
-X MultiWanRules
309 iptables
-t mangle
-F MultiWanDNS
310 iptables
-t mangle
-X MultiWanDNS
311 iptables
-t mangle
-F MultiWanPreHandler
312 iptables
-t mangle
-X MultiWanPreHandler
313 iptables
-t mangle
-F MultiWanPostHandler
314 iptables
-t mangle
-X MultiWanPostHandler
315 iptables
-t mangle
-F LoadBalancer
316 iptables
-t mangle
-X LoadBalancer
317 iptables
-t mangle
-F FastBalancer
318 iptables
-t mangle
-X FastBalancer
321 while [ $i -lt $wancount ]; do
323 iptables
-t mangle
-F FW
${i}MARK
327 while [ $i -lt $wancount ]; do
329 iptables
-t mangle
-X FW
${i}MARK
332 if [ ! -z "$CHKFORQOS" ]; then
334 iptables
-t mangle
-F MultiWanQoS
335 iptables
-t mangle
-X MultiWanQoS
338 while [ $i -lt $wancount ]; do
340 group
=$
(query_config group
$i)
341 iptables
-t mangle
-F qos_
${group}
342 iptables
-t mangle
-F qos_
${group}_ct
343 iptables
-t mangle
-X qos_
${group}
344 iptables
-t mangle
-X qos_
${group}_ct
354 local get_wan_iptables
355 local add_qos_iptables
363 ifname
=$
(query_config ifname
$1)
365 if [ "$ifname" == "x" ]; then
369 qos_if_test
=$
(echo $qos_if_done |
grep $ifname.
)
371 if [ ! -z "$qos_if_test" ]; then
375 qos_if_done
=$
(echo ${qos_if_done}.
${ifname})
377 queue_count
=$
(tc filter list dev
$ifname |
tail -n 1 |
awk -F " " '{print $10}' |
sed "s/0x//g")
379 if [ -z "$queue_count" ]; then
383 queue_count
=`expr $queue_count + 1`
385 iptables
-t mangle
-N qos_
${1}
386 iptables
-t mangle
-N qos_
${1}_ct
388 get_wan_tc
=$
(tc filter list dev
$ifname |
grep "0x" |
sed -e "s/filter /tc filter add dev $ifname /g" -e "s/pref/prio/g" -e "s/fw//g")
389 get_wan_iptables
=$
(iptables-save |
egrep '(-A Default )|(-A Default_ct )' |
grep -v "MultiWanQoS" |
sed -e "s/Default /qos_${1} /g" -e "s/Default_ct /qos_${1}_ct /g" -e "s/-A/iptables -t mangle -A/g")
393 while [ $i -lt $queue_count ]; do
394 echo "s/\(0x$i \|0x$i\/0xffffffff\)/0x$(expr $2 \* 10 + $i) /g" >> /tmp
/.mwan
/qos.
$1.sedfilter
398 add_qos_iptables
=$
(echo "$get_wan_iptables" |
sed -f /tmp
/.mwan
/qos.
$1.sedfilter
)
399 echo "$add_qos_iptables" |
while read execute
; do ${execute}; done
401 rm /tmp
/.mwan
/qos.
$1.sedfilter
403 while [ $i -lt $queue_count ]; do
404 echo "s/0x$i /0x${2}${i} fw /g" >> /tmp
/.mwan
/qos.
$1.sedfilter
408 add_qos_tc
=$
(echo "$get_wan_tc" |
sed -f /tmp
/.mwan
/qos.
$1.sedfilter
)
409 echo "$add_qos_tc" |
while read execute
; do ${execute}; done
410 rm /tmp
/.mwan
/qos.
$1.sedfilter
413 while [ $i -lt $queue_count ]; do
414 if [ $i -lt $
(expr $queue_count - 1) ]; then
415 ip rule add fwmark
0x$
(expr $2 \
* 10 + $i + 1) table $
(expr $2 + 170) prio $
(expr $2 \
* 10 + $i + 2)
417 iptables
-t mangle
-A MultiWanQoS
-m mark
--mark 0x$
(expr $2 \
* 10 + $i) -j qos_
${1}
429 config_get src
$1 src
430 config_get dst
$1 dst
431 config_get ports
$1 ports
432 config_get proto
$1 proto
433 config_get wanrule
$1 wanrule
434 ports_first
=${ports%-*}
435 ports_last
=${ports#*-}
437 if [ -z "$wanrule" ]; then
441 if [ "$wanrule" != "balancer" -a "$wanrule" != "fastbalancer" ]; then
442 wanrule
=$
(query_config wanid
${wanrule})
443 wanrule
="FW${wanrule}MARK"
444 elif [ "$wanrule" == "balancer" ]; then
445 wanrule
="LoadBalancer"
446 elif [ "$wanrule" == "fastbalancer" ]; then
447 wanrule
="FastBalancer"
449 if [ "$dst" == "all" ]; then
452 if [ "$proto" == "all" ]; then
455 if [ "$ports" == "all" ]; then
458 if [ "$ports_first" -ne "$ports_last" ]; then
459 ports
="$ports_first:$ports_last"
462 if [ "$proto" == "icmp" ]; then
465 if [ "$src" == "all" ]; then
468 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0\
469 ${proto:+-p $proto} \
472 ${ports:+--dport $ports} \
475 if [ -z "$proto" -a ! -z "$ports" ]; then
496 iptables
-F MultiWanDNS
-t mangle
498 rm /tmp
/resolv.conf.auto
499 touch /tmp
/resolv.conf.auto
501 echo "## Refreshing DNS Resolution and Tables ##"
504 while [ $i -lt $wancount ]; do
506 group
=$
(query_config group
$i)
507 gateway
=$
(query_config gateway
$group)
508 ipaddr
=$
(query_config ipaddr
$group)
509 ifname
=$
(query_config ifname
$group)
510 failchk
=$
(query_config failchk
$group)
512 dns
=`uci -q -P /var/state get multiwan.${group}.dns`
514 if [ -z "$dns" -o "$dns" == "auto" ]; then
515 dns
=`uci -q -P /var/state get network.${group}.dns`
518 dns
=$
(echo $dns |
sed -e "s/ /\n/g")
520 if [ ! -z "$dns" -a "$failchk" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" -a "$ifname" != "x" ]; then
521 echo "$dns" |
while read dns_server
523 iptables
-t mangle
-A MultiWanDNS
-d $dns_server -p tcp
--dport 53 -j FW
${i}MARK
524 iptables
-t mangle
-A MultiWanDNS
-d $dns_server -p udp
--dport 53 -j FW
${i}MARK
526 compile_dns
="nameserver $dns_server"
527 echo "$compile_dns" >> /tmp
/resolv.conf.auto
532 last_resolv_update
=$
(ls -l -e /tmp
/resolv.conf.auto |
awk -F " " '{print $5, $9}')
536 echo "## IPTables Rule Initialization ##"
542 local default_route_id
545 if [ ! -z "$CHKFORQOS" ]; then
546 echo "## QoS Initialization ##"
548 /etc
/init.d
/qos restart
> /dev
/null
2>&1
550 IMQ_NFO
=`iptables -n -L PREROUTING -t mangle -v | grep IMQ | awk -F " " '{print $6,$12}'`
552 iptables
-t mangle
-F PREROUTING
553 iptables
-t mangle
-F FORWARD
554 iptables
-t mangle
-F POSTROUTING
555 iptables
-t mangle
-F OUTPUT
557 echo "$IMQ_NFO" |
while read execute
559 iptables
-t mangle
-A PREROUTING
-i $
(echo $execute |
awk -F " " '{print $1}') -j IMQ
--todev $
(echo $execute |
awk -F " " '{print $2}')
562 iptables
-t mangle
-N MultiWanQoS
565 while [ $i -lt $wancount ]; do
567 qos_init $
(query_config group
$i) $i
572 iptables
-t mangle
-N MultiWan
573 iptables
-t mangle
-N LoadBalancer
574 iptables
-t mangle
-N FastBalancer
575 iptables
-t mangle
-N MultiWanRules
576 iptables
-t mangle
-N MultiWanDNS
577 iptables
-t mangle
-N MultiWanPreHandler
578 iptables
-t mangle
-N MultiWanPostHandler
579 iptables
-t mangle
-N MultiWanLoadBalancer
581 echo "## Creating FW Rules ##"
583 while [ $i -lt $wancount ]; do
585 iprule
=$
(expr $i \
* 10)
586 iptables
-t mangle
-N FW
${i}MARK
587 iptables
-t mangle
-A FW
${i}MARK
-j MARK
--set-mark 0x
${iprule}
588 iptables
-t mangle
-A FW
${i}MARK
-j CONNMARK
--save-mark
591 iptables
-t mangle
-A LoadBalancer
-j MARK
--set-mark 0x1
592 iptables
-t mangle
-A LoadBalancer
-j CONNMARK
--save-mark
594 if [ -z "$CHKFORMODULE" ]; then
595 iptables
-t mangle
-A FastBalancer
-j MARK
--set-mark 0x2
596 iptables
-t mangle
-A FastBalancer
-j CONNMARK
--save-mark
598 mwnote
"Performance load balancer(fastbalanacer) is unavailable due to current kernel limitations.."
599 iptables
-t mangle
-A FastBalancer
-j MARK
--set-mark 0x1
600 iptables
-t mangle
-A FastBalancer
-j CONNMARK
--save-mark
603 iptables
-t mangle
-A MultiWan
-j CONNMARK
--restore-mark
604 iptables
-t mangle
-A MultiWan
-j MultiWanPreHandler
605 iptables
-t mangle
-A MultiWan
-j MultiWanRules
606 iptables
-t mangle
-A MultiWan
-j MultiWanLoadBalancer
607 iptables
-t mangle
-A MultiWan
-j MultiWanDNS
608 iptables
-t mangle
-A MultiWan
-j MultiWanPostHandler
610 iptables
-t mangle
-I PREROUTING
-j MultiWan
611 iptables
-t mangle
-I FORWARD
-j MultiWan
612 iptables
-t mangle
-I OUTPUT
-j MultiWan
613 iptables
-t mangle
-I POSTROUTING
-j MultiWan
618 config_load
"multiwan"
619 config_foreach mwanrule mwanfw
621 if [ "$default_route" != "balancer" -a "$default_route" != "fastbalancer" ]; then
622 default_route_id
=$
(query_config wanid
$default_route)
623 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0 -j FW
${default_route_id}MARK
624 elif [ "$default_route" == "fastbalancer" ]; then
625 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0 -j FastBalancer
627 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0 -j LoadBalancer
631 while [ $i -lt $wancount ]; do
633 group
=$
(query_config group
$i)
634 ifname
=$
(query_config ifname
$group)
635 iptables
-t mangle
-A MultiWanPreHandler
-i $ifname -m state
--state NEW
-j FW
${i}MARK
636 iptables
-t mangle
-A MultiWanPostHandler
-o $ifname -m mark
--mark 0x1 -j FW
${i}MARK
639 if [ ! -z "$CHKFORQOS" ]; then
640 iptables
-t mangle
-A MultiWan
-j MultiWanQoS
645 refresh_loadbalancer
() {
652 local pre_nexthop_chk
653 local rand_probability
657 echo "## Refreshing Load Balancer ##"
659 ip rule del prio
9 > /dev
/null
2>&1
660 ip route flush table
170 > /dev
/null
2>&1
664 ip route |
grep -Ev ^default |
while read ROUTE
666 ip route add table
$TABLE to
$ROUTE
670 iptables
-F MultiWanLoadBalancer
-t mangle
675 while [ $i -lt $wancount ]; do
677 group
=$
(query_config group
$i)
678 failchk
=$
(query_config failchk
$group)
679 gateway
=$
(query_config gateway
$group)
680 ifname
=$
(query_config ifname
$group)
681 weight
=`uci -q -P /var/state get multiwan.${group}.weight`
682 if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
683 total_weight
=$
(expr $total_weight + $weight)
688 while [ $i -lt $wancount ]; do
690 group
=$
(query_config group
$i)
691 failchk
=$
(query_config failchk
$group)
692 gateway
=$
(query_config gateway
$group)
693 ifname
=$
(query_config ifname
$group)
695 weight
=`uci -q -P /var/state get multiwan.${group}.weight`
697 if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
698 nexthop
="$nexthop nexthop via $gateway dev $ifname weight $weight"
700 rand_probability
=$
(expr $
(expr $weight \
* 100) / $total_weight)
701 total_weight
=$
(expr $total_weight - $weight)
703 if [ $rand_probability -lt 10 ]; then
704 rand_probability
="0.0${rand_probability}"
705 elif [ $rand_probability -lt 100 ]; then
706 rand_probability
="0.${rand_probability}"
708 rand_probability
="1.0"
711 if [ -z "$CHKFORMODULE" ]; then
712 iptables
-A MultiWanLoadBalancer
-t mangle
-m mark
--mark 0x2 -m statistic
--mode random
--probability $rand_probability -j FW
${i}MARK
718 pre_nexthop_chk
=`echo $nexthop | awk -F "nexthop" '{print NF-1}'`
719 if [ "$pre_nexthop_chk" == "1" ]; then
720 ip route add default via $
(echo $nexthop |
awk -F " " '{print $3}') dev $
(echo $nexthop |
awk -F " " '{print $5}') proto static table
170
721 elif [ "$pre_nexthop_chk" -gt "1" ]; then
722 ip route add proto static table
170 default scope global
$nexthop
725 ip rule add fwmark
0x1 table
170 prio
9
737 echo "## Refreshing Routing Tables ##"
740 while [ $i -lt $wancount ]
743 group
=$
(query_config group
$i)
744 gateway
=$
(query_config gateway
$group)
745 ifname
=$
(query_config ifname
$group)
746 ipaddr
=$
(query_config ipaddr
$group)
747 ip route flush table $
(expr $i + 170) > /dev
/null
2>&1
749 for TABLE
in $
(expr $i + 170)
751 ip route |
grep -Ev ^default |
while read ROUTE
753 ip route add table
$TABLE to
$ROUTE
757 if [ "$gateway" != "x" -a "$ipaddr" != "x" -a "$ifname" != "x" ]; then
758 ip route add default via
$gateway table $
(expr $i + 170) src
$ipaddr proto static
759 route add default gw
$gateway > /dev
/null
2>&1
773 group
=$
(query_config group
$1)
774 gateway
=$
(query_config gateway
$group)
775 ipaddr
=$
(query_config ipaddr
$group)
777 CHKIPROUTE
=`cat /etc/iproute2/rt_tables | grep MWAN${1}`
778 if [ -z "$CHKIPROUTE" ]; then
779 echo "$(expr $1 + 170) MWAN${1}" >> /etc
/iproute
2/rt_tables
782 ip rule del prio $
(expr $1 \
* 10) > /dev
/null
2>&1
783 ip rule del prio $
(expr $1 \
* 10 + 1) > /dev
/null
2>&1
785 if [ "$gateway" != "x" -a "$ipaddr" != "x" ]; then
786 ip rule add from
$ipaddr table $
(expr $1 + 170) prio $
(expr $1 \
* 10)
787 ip rule add fwmark
0x$
(expr $1 \
* 10) table $
(expr $1 + 170) prio $
(expr $
(expr $1 \
* 10) + 1)
794 echo "## Flushing IP Rules & Routes ##"
796 ip rule flush
> /dev
/null
2>&1
797 ip rule add lookup main prio
32766 > /dev
/null
2>&1
798 ip rule add lookup default prio
32767 > /dev
/null
2>&1
800 ip route flush table
170 > /dev
/null
803 while [ $i -lt $wancount ]; do
805 ip route del default
> /dev
/null
2>&1
806 ip route flush table $
(expr $i + 170) > /dev
/null
2>&1
809 echo "## Clearing Rules ##"
810 clear_rules
> /dev
/null
2>&1
812 rm $jobfile > /dev
/null
2>&1
818 local health_interval
821 echo "## Main Initialization ##"
823 mkdir
/tmp
/.mwan
> /dev
/null
2>&1
829 echo "## IP Rules Initialization ##"
831 CHKIPROUTE
=`cat /etc/iproute2/rt_tables | grep LoadBalancer`
832 if [ -z "$CHKIPROUTE" ]; then
833 echo "#" >> /etc
/iproute
2/rt_tables
834 echo "170 LoadBalancer" >> /etc
/iproute
2/rt_tables
838 while [ $i -lt $wancount ]; do
848 RP_PATH
=/proc
/sys
/net
/ipv
4/conf
849 for IFACE
in `ls $RP_PATH`; do
850 echo 0 > $RP_PATH/$IFACE/rp_filter
852 echo "## Initialization Complete, switching to background mode. ##"
853 mwnote
"Succesfully Initialized on $(exec date -R)."
856 stagger_health_monitors
() {
858 while [ $i -lt $wancount ]; do
860 group
=$
(query_config group
$i)
861 health_interval
=`uci -q -P /var/state get multiwan.${group}.health_interval`
862 if [ ! -z "$health_interval" -a "$health_interval" != "disable" -a "$health_interval" -gt 0 ]; then
863 health_monitor
$group &
869 stagger_health_monitors
&
884 local icmp_hosts_acquire
885 local default_routes_check
889 local health_interval
894 timeout
=`uci -q -P /var/state get multiwan.${1}.timeout`
895 icmp_hosts
=`uci -q -P /var/state get multiwan.${1}.icmp_hosts`
896 health_interval
=`uci -q -P /var/state get multiwan.${1}.health_interval`
897 ifname_cur
=$
(query_config ifname
$1)
898 ipaddr_cur
=$
(query_config ipaddr
$1)
899 gateway_cur
=$
(query_config gateway
$1)
903 ifname
=`uci -q -P /var/state get network.${1}.ifname`
904 ipaddr
=`uci -q -P /var/state get network.${1}.ipaddr`
905 gateway
=`uci -q -P /var/state get network.${1}.gateway`
907 if [ -z "$ifname" ]; then
911 if [ -z "$ipaddr" ]; then
915 if [ -z "$gateway" ]; then
919 if [ "$ifname_cur" != "$ifname" -o "$ipaddr_cur" != "$ipaddr" -o "$gateway_cur" != "$gateway" ]; then
920 echo $1.acquire
>> $jobfile
923 if [ "$gateway" != "x" ]; then
924 default_routes_check
=`ip route | grep -o $gateway`
925 if [ -z "$default_routes_check" ]; then
926 check_for_job
=`cat $jobfile 2>&1 | grep -o "route.refresh"`
927 if [ -z "$check_for_job" ]; then
928 echo route.refresh
>> $jobfile
934 if [ "$icmp_hosts" != "disable" -a "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
936 if [ "$icmp_hosts" == "gateway" -o -z "$icmp_hosts" ]; then
937 icmp_hosts_acquire
=$gateway
938 elif [ "$icmp_hosts" == "dns" ]; then
939 icmp_hosts_acquire
=`uci -q -P /var/state get multiwan.$1.dns`
940 if [ -z "$icmp_hosts_acquire" -o "$icmp_hosts_acquire" == "auto" ]; then
941 icmp_hosts_acquire
=`uci -q -P /var/state get network.$1.dns`
944 icmp_hosts_acquire
=$icmp_hosts
947 icmp_hosts
=$
(echo $icmp_hosts_acquire |
sed -e "s/\,/ /g" |
sed -e "s/ /\n/g")
950 echo "$icmp_hosts" |
while read icmp_test_host
952 ping -c 1 -W $timeout -I $ifname $icmp_test_host 2>&1 |
grep -o "round-trip"
956 check_test
=$
(ping_test
)
958 if [ -z "$check_test" ]; then
959 echo "$1.fail" >> $jobfile
961 echo "$1.pass" >> $jobfile
964 elif [ "$icmp_hosts" == "disable" ]; then
965 echo "$1.pass" >> $jobfile
968 sleep $health_interval
976 local current_resolv_file
984 if [ "$bg_counter" -eq 5 ]; then
986 check_iptables
=$
(iptables
-n -L MultiWan
-t mangle |
grep "references" |
awk -F "(" '{print $2}' | cut
-d " " -f 1)
988 if [ -z "$check_iptables" -o "$check_iptables" -lt 4 ]; then
989 mwnote
"Netfilter rules appear to of been altered."
990 /etc
/init.d
/multiwan restart
&
994 current_resolv_file
=$
(ls -l -e /tmp
/resolv.conf.auto |
awk -F " " '{print $5, $9}')
996 if [ "$last_resolv_update" != "$current_resolv_file" ]; then
1004 if [ -f $jobfile ]; then
1006 mv $jobfile $jobfile.work
1014 pass
) recover_wan
$1;;
1015 acquire
) acquire_wan_data
$1 && health_monitor
$1 &;;
1016 refresh
) refresh_routes
;;
1020 queued_task
=`echo $LINE | awk -F "." '{print $1,$2}'`
1021 execute_task
$queued_task
1022 done < $jobfile.work
1027 bg_counter
=$
(expr $bg_counter + 1)
1040 while [ $i -lt $wancount ]; do
1042 group
=$
(query_config group
$i)
1043 ifname
=$
(query_config ifname
$group)
1044 ipaddr
=$
(query_config ipaddr
$group)
1045 gateway
=$
(query_config gateway
$group)
1047 if [ "$ifname" == "x" -o "$ipaddr" == "x" -o "$gateway" == "x" ]; then
1056 config_load
"multiwan"
1057 config_get default_route config default_route
1058 config_get debug config debug
1060 config_foreach acquire_wan_data interface
1064 CHKFORQOS
=`iptables -n -L Default -t mangle 2>&1 | grep "Chain Default"`
1065 CHKFORMODULE
=`iptables -m statistic 2>&1 | grep -o "File not found"`
1067 jobfile
="/tmp/.mwan/jobqueue"
1070 agent
) silencer main_init
;;
1071 restart
) silencer stop restart
;;
1072 stop
) silencer stop
;;