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" != "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
48 mwnote
"$1 has failed and is currently offline."
53 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]//g")
54 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]//g")
57 if [ "$existing_failover" == "3" ]; then
58 iptables
-D FW
${wanid}MARK
2 -t mangle
60 mwnote
"$1 has recovered and is back online!"
73 local health_fail_retries
76 health_fail_retries
=`uci -q -P /var/state get multiwan.${1}.health_fail_retries`
77 weight
=`uci -q -P /var/state get multiwan.${1}.weight`
79 failchk
=$
(query_config failchk
$1)
80 recvrychk
=$
(query_config recvrychk
$1)
81 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]//g")
83 if [ -z "$failchk" ]; then
84 wan_fail_map
="$wan_fail_map${1}[1]"
86 if [ "$health_fail_retries" == "1" ]; then
90 if [ "$failchk" != "x" ]; then
91 new_fail_count
=$
(expr $failchk + 1)
92 if [ "$new_fail_count" -lt "$health_fail_retries" ]; then
93 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]/$1\[${new_fail_count}\]/g")
98 if [ "$weight" != "disable" ]; then
114 local health_recovery_retires
117 health_recovery_retries
=`uci -q -P /var/state get multiwan.${1}.health_recovery_retries`
118 weight
=`uci -q -P /var/state get multiwan.${1}.weight`
120 failchk
=$
(query_config failchk
$1)
121 recvrychk
=$
(query_config recvrychk
$1)
122 wanid
=$
(query_config wanid
$1)
124 if [ ! -z "$failchk" -a "$failchk" != "x" ]; then
125 wan_fail_map
=$
(echo $wan_fail_map |
sed -e "s/${1}\[${failchk}\]//g")
129 if [ "$failchk" == "x" ]; then
130 if [ -z "$recvrychk" ]; then
131 wan_recovery_map
="$wan_recovery_map${1}[1]"
133 if [ "$health_recovery_retries" == "1" ]; then
137 new_recovery_count
=$
(expr $recvrychk + 1)
138 if [ "$new_recovery_count" -lt "$health_recovery_retries" ]; then
139 wan_recovery_map
=$
(echo $wan_recovery_map |
sed -e "s/${1}\[${recvrychk}\]/$1\[${new_recovery_count}\]/g")
144 if [ "$weight" != "disable" ]; then
153 if [ $wancount -lt 9 ]; 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 wan_if_map
="$wan_if_map${1}[${ifname}]"
183 wan_id_map
="$wan_id_map${1}[${wancount}]"
184 wan_gw_map
="$wan_gw_map${1}[${gateway}]"
185 wan_ip_map
="$wan_ip_map${1}[${ipaddr}]"
187 old_ipaddr
=$
(query_config ipaddr
$1)
188 old_gateway
=$
(query_config gateway
$1)
189 old_ifname
=$
(query_config ifname
$1)
190 get_wanid
=$
(query_config wanid
$1)
192 wan_if_map
=$
(echo $wan_if_map |
sed -e "s/${1}\[${old_ifname}\]/$1\[${ifname}\]/g")
193 wan_ip_map
=$
(echo $wan_ip_map |
sed -e "s/${1}\[${old_ipaddr}\]/$1\[${ipaddr}\]/g")
194 wan_gw_map
=$
(echo $wan_gw_map |
sed -e "s/${1}\[${old_gateway}\]/$1\[${gateway}\]/g")
196 if [ "$old_ifname" != "$ifname" ]; then
197 iptables
-D MultiWanPreHandler
-t mangle
-i $old_$ifname -m state
--state NEW
-j FW
${get_wanid}MARK
198 iptables
-A MultiWanPreHandler
-t mangle
-i $ifname -m state
--state NEW
-j FW
${get_wanid}MARK
199 iptables
-D MultiWanPostHandler
-t mangle
-o $old_$ifname -m mark
--mark 0x123 -j FW
${get_wanid}MARK
200 iptables
-A MultiWanPostHandler
-t mangle
-o $ifname -m mark
--mark 0x123 -j FW
${get_wanid}MARK
203 if [ "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
205 iprules_config
$get_wanid
222 if [ ! -d /tmp
/.mwan
]; then
223 mkdir
/tmp
/.mwan
> /dev
/null
2>&1
226 rm /tmp
/.mwan
/cache
> /dev
/null
2>&1
227 touch /tmp
/.mwan
/cache
229 echo "# Automatically Generated by Multi-WAN Agent Script. Do not modify or remove. #" > /tmp
/.mwan
/cache
230 echo "wan_id_map=\"$wan_id_map\"" >> /tmp
/.mwan
/cache
231 echo "wan_if_map=\"$wan_if_map\"" >> /tmp
/.mwan
/cache
232 echo "wan_ip_map=\"$wan_ip_map\"" >> /tmp
/.mwan
/cache
233 echo "wan_gw_map=\"$wan_gw_map\"" >> /tmp
/.mwan
/cache
234 echo "wan_fail_map=\"$wan_fail_map\"" >> /tmp
/.mwan
/cache
235 echo "wan_recovery_map=\"$wan_recovery_map\"" >> /tmp
/.mwan
/cache
241 update
) update_cache_data
;;
242 ifname
) echo $wan_if_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
243 ipaddr
) echo $wan_ip_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
244 gateway
) echo $wan_gw_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
245 wanid
) echo $wan_id_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
246 failchk
) echo $wan_fail_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
247 recvrychk
) echo $wan_recovery_map |
grep -o "$2\[\w*.*\]" |
awk -F "[" '{print $2}' |
awk -F "]" '{print $1}';;
248 group
) echo $wan_id_map |
grep -o "\w*\[$2\]" |
awk -F "[" '{print $1}';;
255 otherpids
=$
(ps
-a 2>&1 |
grep 'multiwan agent' |
grep -v $$ |
awk -F " " '{print $1}')
256 echo "$otherpids" |
while read execute
258 kill -9 ${execute} > /dev
/null
2>&1
269 if [ "$1" != "restart" ]; then
270 echo "## Refreshing Interfaces ##"
272 while [ $i -lt $wancount ]; do
274 group
=$
(query_config group
$i)
275 fdown
$group > /dev
/null
2>&1
276 ifup
$group > /dev
/null
2>&1 &
279 if [ ! -z "$CHKFORQOS" ]; then
280 /etc
/init.d
/qos restart
& > /dev
/null
2>&1
283 echo "## Unloaded, updating syslog and exiting. ##"
284 mwnote
"Succesfully Unloaded on $(exec date -R)."
286 rm -r /tmp
/.mwan
> /dev
/null
2>&1
290 echo "## Restarting Multi-WAN. ##"
291 mwnote
"Reinitializing Multi-WAN Configuration."
293 rm -r /tmp
/.mwan
> /dev
/null
2>&1
294 /etc
/init.d
/multiwan start
& > /dev
/null
2>&1
304 iptables
-t mangle
-F PREROUTING
305 iptables
-t mangle
-F FORWARD
306 iptables
-t mangle
-F POSTROUTING
307 iptables
-t mangle
-F OUTPUT
308 iptables
-t mangle
-F MultiWan
309 iptables
-t mangle
-X MultiWan
310 iptables
-t mangle
-F MultiWanRules
311 iptables
-t mangle
-X MultiWanRules
312 iptables
-t mangle
-F MultiWanDNS
313 iptables
-t mangle
-X MultiWanDNS
314 iptables
-t mangle
-F MultiWanPreHandler
315 iptables
-t mangle
-X MultiWanPreHandler
316 iptables
-t mangle
-F MultiWanPostHandler
317 iptables
-t mangle
-X MultiWanPostHandler
318 iptables
-t mangle
-F LoadBalancer
319 iptables
-t mangle
-X LoadBalancer
322 while [ $i -lt $wancount ]; do
324 iptables
-t mangle
-F FW
${i}MARK
328 while [ $i -lt $wancount ]; do
330 iptables
-t mangle
-X FW
${i}MARK
333 if [ ! -z "$CHKFORQOS" ]; then
335 iptables
-t mangle
-F MultiWanQoS
336 iptables
-t mangle
-X MultiWanQoS
339 while [ $i -lt $wancount ]; do
341 group
=$
(query_config group
$i)
342 iptables
-t mangle
-F MultiWanQoS_
${group}
343 iptables
-t mangle
-F MultiWanQoS_
${group}_ct
344 iptables
-t mangle
-X MultiWanQoS_
${group}
345 iptables
-t mangle
-X MultiWanQoS_
${group}_ct
355 local get_wan_iptables
356 local add_qos_iptables
362 ifname
=$
(query_config ifname
$1)
364 if [ "$ifname" == "x" ]; then
368 queue_count
=$
(tc filter list dev
$ifname |
tail -n 1 |
awk -F " " '{print $10}' |
sed "s/0x//g")
370 if [ -z "$queue_count" ]; then
374 queue_count
=`expr $queue_count + 1`
376 iptables
-t mangle
-N MultiWanQoS_
${1}
377 iptables
-t mangle
-N MultiWanQoS_
${1}_ct
379 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")
380 get_wan_iptables
=$
(iptables-save |
egrep '(-A Default )|(-A Default_ct )' |
grep -v "MultiWanQoS" |
sed -e "s/Default /MultiWanQoS_${1} /g" -e "s/Default_ct /MultiWanQoS_${1}_ct /g" -e "s/-A/iptables -t mangle -A/g")
383 while [ $i -lt $queue_count ]; do
384 echo "s/\(0x$i \|0x$i\/0xffffffff\)/0x${2}${i} /g" >> /tmp
/.mwan
/qos.
$1.sedfilter
388 add_qos_iptables
=$
(echo "$get_wan_iptables" |
sed -f /tmp
/.mwan
/qos.
$1.sedfilter
)
389 echo "$add_qos_iptables" |
while read execute
; do ${execute}; done
391 rm /tmp
/.mwan
/qos.
$1.sedfilter
393 while [ $i -lt $queue_count ]; do
394 echo "s/0x$i /0x${2}${i} fw /g" >> /tmp
/.mwan
/qos.
$1.sedfilter
398 add_qos_tc
=$
(echo "$get_wan_tc" |
sed -f /tmp
/.mwan
/qos.
$1.sedfilter
)
399 echo "$add_qos_tc" |
while read execute
; do ${execute}; done
400 rm /tmp
/.mwan
/qos.
$1.sedfilter
403 while [ $i -lt $queue_count ]; do
404 p
=`expr $i + $2 \* 10`
405 if [ $i -lt $
(expr $queue_count - 1) ]; then
406 ip rule add fwmark
0x$
(expr $p + 1) table $
(expr $2 \
* 10) prio $
(expr $p + 2)
408 iptables
-t mangle
-A MultiWanQoS
-m mark
--mark 0x
$p -j MultiWanQoS_
${1}
420 config_get src
$1 src
421 config_get dst
$1 dst
422 config_get ports
$1 ports
423 config_get proto
$1 proto
424 config_get wanrule
$1 wanrule
425 ports_first
=${ports%-*}
426 ports_last
=${ports#*-}
428 if [ -z "$wanrule" ]; then
432 if [ "$wanrule" != "balancer" ]; then
433 wanrule
=$
(query_config wanid
${wanrule})
434 wanrule
="FW${wanrule}MARK"
435 elif [ "$wanrule" == "balancer" ]; then
436 wanrule
="LoadBalancer"
438 if [ "$dst" == "all" ]; then
441 if [ "$proto" == "all" ]; then
444 if [ "$ports" == "all" ]; then
447 if [ "$ports_first" -ne "$ports_last" ]; then
448 ports
="$ports_first:$ports_last"
451 if [ "$proto" == "icmp" ]; then
454 if [ "$src" == "all" ]; then
457 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0\
458 ${proto:+-p $proto} \
461 ${ports:+--dport $ports} \
464 if [ -z "$proto" -a ! -z "$ports" ]; then
485 iptables
-F MultiWanDNS
-t mangle
487 rm /tmp
/resolv.conf.auto
488 touch /tmp
/resolv.conf.auto
490 echo "## Refreshing DNS Resolution and Tables ##"
493 while [ $i -lt $wancount ]; do
495 group
=$
(query_config group
$i)
496 gateway
=$
(query_config gateway
$group)
497 ipaddr
=$
(query_config ipaddr
$group)
498 ifname
=$
(query_config ifname
$group)
499 failchk
=$
(query_config failchk
$group)
501 dns
=`uci -q -P /var/state get multiwan.${group}.dns`
503 if [ -z "$dns" -o "$dns" == "auto" ]; then
504 dns
=`uci -q -P /var/state get network.${group}.dns`
507 dns
=$
(echo $dns |
sed -e "s/ /\n/g")
509 if [ ! -z "$dns" -a "$failchk" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" -a "$ifname" != "x" ]; then
510 echo "$dns" |
while read dns_server
512 iptables
-t mangle
-A MultiWanDNS
-d $dns_server -j FW
${i}MARK
514 compile_dns
="nameserver $dns_server"
515 echo "$compile_dns" >> /tmp
/resolv.conf.auto
520 last_resolv_update
=$
(ls -l -e /tmp
/resolv.conf.auto |
awk -F " " '{print $5, $9}')
524 echo "## IPTables Rule Initialization ##"
530 local default_route_id
533 if [ ! -z "$CHKFORQOS" ]; then
534 echo "## QoS Initialization ##"
536 /etc
/init.d
/qos restart
> /dev
/null
2>&1
538 IMQ_NFO
=`iptables -n -L PREROUTING -t mangle -v | grep IMQ | awk -F " " '{print $6,$12}'`
540 iptables
-t mangle
-F PREROUTING
541 iptables
-t mangle
-F FORWARD
542 iptables
-t mangle
-F POSTROUTING
543 iptables
-t mangle
-F OUTPUT
545 echo "$IMQ_NFO" |
while read execute
547 iptables
-t mangle
-A PREROUTING
-i $
(echo $execute |
awk -F " " '{print $1}') -j IMQ
--todev $
(echo $execute |
awk -F " " '{print $2}')
550 iptables
-t mangle
-N MultiWanQoS
553 while [ $i -lt $wancount ]; do
555 qos_init $
(query_config group
$i) $i
560 iptables
-t mangle
-N MultiWan
561 iptables
-t mangle
-N LoadBalancer
562 iptables
-t mangle
-N MultiWanRules
563 iptables
-t mangle
-N MultiWanDNS
564 iptables
-t mangle
-N MultiWanPreHandler
565 iptables
-t mangle
-N MultiWanPostHandler
566 iptables
-t mangle
-N MultiWanLoadBalancer
568 echo "## Creating FW Rules ##"
570 while [ $i -lt $wancount ]; do
572 iprule
=$
(expr $i \
* 10)
573 iptables
-t mangle
-N FW
${i}MARK
574 iptables
-t mangle
-A FW
${i}MARK
-j MARK
--set-mark 0x
${iprule}
575 iptables
-t mangle
-A FW
${i}MARK
-j CONNMARK
--save-mark
578 iptables
-t mangle
-A LoadBalancer
-j MARK
--set-mark 0x123
579 iptables
-t mangle
-A LoadBalancer
-j CONNMARK
--save-mark
581 iptables
-t mangle
-I PREROUTING
-j MultiWan
582 iptables
-t mangle
-I PREROUTING
2 -j MultiWanPreHandler
583 iptables
-t mangle
-I PREROUTING
3 -j MultiWanDNS
584 iptables
-t mangle
-I PREROUTING
4 -j MultiWanRules
585 iptables
-t mangle
-I PREROUTING
5 -j MultiWanLoadBalancer
587 iptables
-t mangle
-I FORWARD
-j MultiWan
589 iptables
-t mangle
-I OUTPUT
-j MultiWan
590 iptables
-t mangle
-I OUTPUT
2 -j MultiWanDNS
591 iptables
-t mangle
-I OUTPUT
3 -j MultiWanRules
592 iptables
-t mangle
-I OUTPUT
4 -j MultiWanLoadBalancer
593 iptables
-t mangle
-I OUTPUT
5 -j MultiWanPostHandler
596 iptables
-t mangle
-I POSTROUTING
-j MultiWan
597 iptables
-t mangle
-I POSTROUTING
2 -j MultiWanPostHandler
599 iptables
-t mangle
-A MultiWan
-j CONNMARK
--restore-mark
603 config_load
"multiwan"
604 config_foreach mwanrule mwanfw
606 if [ "$default_route" != "balancer" ]; then
607 default_route_id
=$
(query_config wanid
$default_route)
608 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0 -j FW
${default_route_id}MARK
610 iptables
-t mangle
-A MultiWanRules
-m mark
--mark 0x0 -j LoadBalancer
614 while [ $i -lt $wancount ]; do
616 group
=$
(query_config group
$i)
617 ifname
=$
(query_config ifname
$group)
618 iptables
-t mangle
-A MultiWanPreHandler
-i $ifname -m state
--state NEW
-j FW
${i}MARK
619 iptables
-t mangle
-A MultiWanPostHandler
-o $ifname -m mark
--mark 0x123 -j FW
${i}MARK
622 if [ ! -z "$CHKFORQOS" ]; then
623 iptables
-t mangle
-I PREROUTING
6 -j MultiWanQoS
624 iptables
-t mangle
-A FORWARD
-j MultiWanQoS
625 iptables
-t mangle
-A OUTPUT
-j MultiWanQoS
626 iptables
-t mangle
-A POSTROUTING
-j MultiWanQoS
630 refresh_loadbalancer
() {
637 local pre_nexthop_chk
638 local rand_probability
639 local last_probability
648 echo "## Refreshing Load Balancer ##"
650 CHKIPROUTE
=`cat /etc/iproute2/rt_tables | grep LoadBalancer`
651 if [ -z "$CHKIPROUTE" ]; then
652 echo "123 LoadBalancer" >> /etc
/iproute
2/rt_tables
654 ip rule del prio
123 > /dev
/null
2>&1
655 ip route flush table
123 > /dev
/null
2>&1
659 ip route |
grep link |
grep -Ev ^default |
while read ROUTE
661 ip route add table
$TABLE to
$ROUTE
665 iptables
-F MultiWanLoadBalancer
-t mangle
671 while [ $i -lt $wancount ]; do
673 group
=$
(query_config group
$i)
674 failchk
=$
(query_config failchk
$group)
675 gateway
=$
(query_config gateway
$group)
676 ifname
=$
(query_config ifname
$group)
677 weight
=`uci -q -P /var/state get multiwan.${group}.weight`
678 if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
679 total_weight
=$
(expr $total_weight + $weight)
691 while [ $i -lt $wancount ]; do
693 group
=$
(query_config group
$i)
694 failchk
=$
(query_config failchk
$group)
695 gateway
=$
(query_config gateway
$group)
696 ifname
=$
(query_config ifname
$group)
698 weight
=`uci -q -P /var/state get multiwan.${group}.weight`
700 if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
701 nexthop
="$nexthop nexthop via $gateway dev $ifname weight $weight"
703 n
=$
(expr $n + $last_probability)
704 last_probability
=$
(expr $x / $
(expr $total_wans - $i + 1))
705 rand_probability
=$
(expr $
(expr $weight \
* 1000) / $total_weight)
706 roundlen
=`expr length $rand_probability - 1`
707 roundme
=${rand_probability:$roundlen}
708 rand_probability
=$
(echo $rand_probability |
sed s
/$roundme//)
710 if [ $roundme -ge 5 ]; then
711 rand_probability
=$
(expr $rand_probability + 1)
714 rand_probability
=$
(expr $rand_probability + $n + $last_probability)
717 if [ $rand_probability -lt 10 ]; then
718 rand_probability
="0.0${rand_probability}"
719 elif [ $rand_probability -lt 100 ]; then
720 rand_probability
="0.${rand_probability}"
722 rand_probability
="1.0"
725 if [ -z "$CHKFORMODULE" ]; then
726 iptables
-A MultiWanLoadBalancer
-t mangle
-m mark
--mark 0x123 -m statistic
--mode random
--probability $rand_probability -j FW
${i}MARK
732 pre_nexthop_chk
=`echo $nexthop | awk -F "nexthop" '{print NF-1}'`
733 if [ "$pre_nexthop_chk" == "1" ]; then
734 ip route add default via $
(echo $nexthop |
awk -F " " '{print $3}') dev $
(echo $nexthop |
awk -F " " '{print $5}') proto static table
123
735 elif [ "$pre_nexthop_chk" -gt "1" ]; then
736 ip route add proto static table
123 default scope global
$nexthop
739 ip rule add fwmark
0x123 table
123 prio
123
751 echo "## Refreshing Routing Tables ##"
754 while [ $i -lt $wancount ]
757 group
=$
(query_config group
$i)
758 gateway
=$
(query_config gateway
$group)
759 ifname
=$
(query_config ifname
$group)
760 ipaddr
=$
(query_config ipaddr
$group)
762 iprule
=$
(expr $i \
* 10)
763 ip route flush table
$iprule > /dev
/null
2>&1
767 ip route |
grep link |
grep -Ev ^default |
while read ROUTE
769 ip route add table
$TABLE to
$ROUTE
773 if [ "$gateway" != "x" -a "$ipaddr" != "x" -a "$ifname" != "x" ]; then
774 ip route add default via
$gateway table
$iprule src
$ipaddr proto static
775 route add default gw
$gateway dev
$ifname
789 iprule
=$
(expr $1 \
* 10)
790 group
=$
(query_config group
$1)
791 gateway
=$
(query_config gateway
$group)
792 ipaddr
=$
(query_config ipaddr
$group)
794 CHKIPROUTE
=`cat /etc/iproute2/rt_tables | grep MWAN${1}`
795 if [ -z "$CHKIPROUTE" ]; then
796 echo "$iprule MWAN${1}" >> /etc
/iproute
2/rt_tables
799 ip rule del prio
$iprule > /dev
/null
2>&1
800 ip rule del prio $
(expr $iprule + 1) > /dev
/null
2>&1
802 if [ "$gateway" != "x" -a "$ipaddr" != "x" ]; then
803 ip rule add from
$ipaddr table
$iprule prio
$iprule
804 ip rule add fwmark
0x
${iprule} table
$iprule prio $
(expr $iprule + 1)
812 echo "## Flushing IP Rules & Routes ##"
814 ip rule flush
> /dev
/null
2>&1
815 ip rule add lookup main prio
32766 > /dev
/null
2>&1
816 ip rule add lookup default prio
32767 > /dev
/null
2>&1
818 ip route flush table
123 > /dev
/null
821 while [ $i -lt $wancount ]; do
823 iprule
=$
(expr $i \
* 10)
824 ip route del default
> /dev
/null
2>&1
825 ip route flush table
$iprule > /dev
/null
2>&1
828 echo "## Clearing Rules ##"
829 clear_rules
> /dev
/null
2>&1
831 rm $jobfile > /dev
/null
2>&1
837 local health_interval
840 echo "## Main Initialization ##"
842 mkdir
/tmp
/.mwan
> /dev
/null
2>&1
848 echo "## IP Rules Initialization ##"
850 while [ $i -lt $wancount ]; do
860 RP_PATH
=/proc
/sys
/net
/ipv
4/conf
861 for IFACE
in `ls $RP_PATH`; do
862 echo 0 > $RP_PATH/$IFACE/rp_filter
864 echo "## Initialization Complete, switching to background mode. ##"
865 mwnote
"Succesfully Initialized on $(exec date -R)."
868 stagger_health_monitors
() {
870 while [ $i -lt $wancount ]; do
872 group
=$
(query_config group
$i)
873 health_interval
=`uci -q -P /var/state get multiwan.${group}.health_interval`
874 if [ ! -z "$health_interval" -a "$health_interval" != "disable" -a "$health_interval" -gt 0 ]; then
875 health_monitor
$group &
881 stagger_health_monitors
&
896 local icmp_hosts_acquire
897 local default_routes_check
901 local health_interval
906 timeout
=`uci -q -P /var/state get multiwan.${1}.timeout`
907 icmp_hosts
=`uci -q -P /var/state get multiwan.${1}.icmp_hosts`
908 health_interval
=`uci -q -P /var/state get multiwan.${1}.health_interval`
909 ifname_cur
=$
(query_config ifname
$1)
910 ipaddr_cur
=$
(query_config ipaddr
$1)
911 gateway_cur
=$
(query_config gateway
$1)
915 ifname
=`uci -q -P /var/state get network.${1}.ifname`
916 ipaddr
=`uci -q -P /var/state get network.${1}.ipaddr`
917 gateway
=`uci -q -P /var/state get network.${1}.gateway`
919 if [ -z "$ifname" ]; then
923 if [ -z "$ipaddr" ]; then
927 if [ -z "$gateway" ]; then
931 if [ "$ifname_cur" != "$ifname" -o "$ipaddr_cur" != "$ipaddr" -o "$gateway_cur" != "$gateway" ]; then
932 echo $1.acquire
>> $jobfile
935 if [ "$gateway" != "x" ]; then
936 default_routes_check
=`ip route | grep -o $gateway`
937 if [ -z "$default_routes_check" ]; then
938 check_for_job
=`cat $jobfile 2>&1 | grep -o "route.refresh"`
939 if [ -z "$check_for_job" ]; then
940 echo route.refresh
>> $jobfile
946 if [ "$icmp_hosts" != "disable" -a "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
948 if [ "$icmp_hosts" == "gateway" -o -z "$icmp_hosts" ]; then
949 icmp_hosts_acquire
=$gateway
950 elif [ "$icmp_hosts" == "dns" ]; then
951 icmp_hosts_acquire
=`uci -q -P /var/state get multiwan.$1.dns`
952 if [ -z "$icmp_hosts_acquire" -o "$icmp_hosts_acquire" == "auto" ]; then
953 icmp_hosts_acquire
=`uci -q -P /var/state get network.$1.dns`
956 icmp_hosts_acquire
=$icmp_hosts
959 icmp_hosts
=$
(echo $icmp_hosts_acquire |
sed -e "s/\,/ /g" |
sed -e "s/ /\n/g")
962 echo "$icmp_hosts" |
while read icmp_test_host
964 ping -c 1 -W $timeout -I $ifname $icmp_test_host 2>&1 |
grep -o "round-trip"
968 check_test
=$
(ping_test
)
970 if [ -z "$check_test" ]; then
971 echo "$1.fail" >> $jobfile
973 echo "$1.pass" >> $jobfile
976 elif [ "$icmp_hosts" == "disable" ]; then
977 echo "$1.pass" >> $jobfile
980 sleep $health_interval
988 local current_resolv_file
996 if [ "$bg_counter" -eq 5 ]; then
998 check_iptables
=$
(iptables
-n -L MultiWan
-t mangle |
grep "references" |
awk -F "(" '{print $2}' | cut
-d " " -f 1)
1000 if [ -z "$check_iptables" -o "$check_iptables" -lt 4 ]; then
1001 mwnote
"Netfilter rules appear to of been altered."
1002 /etc
/init.d
/multiwan restart
&
1006 current_resolv_file
=$
(ls -l -e /tmp
/resolv.conf.auto |
awk -F " " '{print $5, $9}')
1008 if [ "$last_resolv_update" != "$current_resolv_file" ]; then
1016 if [ -f $jobfile ]; then
1018 mv $jobfile $jobfile.work
1026 pass
) recover_wan
$1;;
1027 acquire
) acquire_wan_data
$1 && health_monitor
$1 &;;
1028 refresh
) refresh_routes
;;
1032 queued_task
=`echo $LINE | awk -F "." '{print $1,$2}'`
1033 execute_task
$queued_task
1034 done < $jobfile.work
1039 bg_counter
=$
(expr $bg_counter + 1)
1052 while [ $i -lt $wancount ]; do
1054 group
=$
(query_config group
$i)
1055 ifname
=$
(query_config ifname
$group)
1056 ipaddr
=$
(query_config ipaddr
$group)
1057 gateway
=$
(query_config gateway
$group)
1059 if [ "$ifname" == "x" -o "$ipaddr" == "x" -o "$gateway" == "x" ]; then
1068 config_load
"multiwan"
1069 config_get default_route config default_route
1070 config_get debug config debug
1072 config_foreach acquire_wan_data interface
1076 CHKFORQOS
=`iptables -n -L Default -t mangle 2>&1 | grep "Chain Default"`
1077 CHKFORMODULE
=`iptables -m statistic 2>&1 | grep -o "File not found"`
1079 jobfile
="/tmp/.mwan/jobqueue"
1082 agent
) silencer main_init
;;
1083 restart
) silencer stop restart
;;
1084 stop
) silencer stop
;;