source: packages/net/multiwan/files/usr/bin/multiwan @ 20925

Last change on this file since 20925 was 20925, checked in by jow, 7 years ago

[packages] add multiwan, a simple to use multi wan configuration agent

  • Property svn:executable set to *
File size: 22.9 KB
Line 
1#!/bin/sh
2
3. /etc/functions.sh
4
5silencer() {
6if [ -z "$debug" -o "$debug" == "0" ]; then
7$* > /dev/null 2>&1
8else
9$*
10fi
11}
12
13mwnote() {
14logger "[Multi-WAN Notice]: $1"
15}
16
17failover() {
18local failover_to
19local failover_to_wanid
20local failchk
21local wanid
22local existing_failover
23
24wanid=$(query_config wanid $2)
25failover_to=`uci -q -P /var/state get multiwan.${2}.failover_to`
26failover_to_wanid=$(query_config wanid $failover_to)
27
28existing_failover=$(iptables -n -L FW${wanid}MARK -t mangle | echo $(expr $(wc -l) - 2))
29
30add() {
31if [ "$existing_failover" == "2" ]; then
32        if [ "$failover_to" != "balancer" -a "$failover_to" != "disable" -a "$failover_to_wanid" != "$wanid" ]; then
33                iptables -I FW${wanid}MARK 2 -t mangle -j FW${failover_to_wanid}MARK
34        elif [ "$failover_to" == "balancer" ]; then
35                iptables -I FW${wanid}MARK 2 -t mangle -j LoadBalancer
36        fi
37fi
38        mwnote "$1 has failed and is currently offline."
39}
40
41del() {
42if [ "$existing_failover" == "3" ]; then
43        if [ "$failover_to" != "balancer" -a "failover_to" != "disable" -a "$failover_to_wanid" != "$wanid" ]; then
44                iptables -D FW${wanid}MARK -t mangle -j FW${failover_to_wanid}MARK
45        elif [ "$failover_to" == "balancer" ]; then
46                iptables -D FW${wanid}MARK -t mangle -j LoadBalancer
47        fi
48fi
49        mwnote "$1 has recovered and is back online!"
50}
51
52case $1 in
53add) add $2;;
54del) del $2;;
55esac
56}
57
58fail_wan() {
59local failchk
60local recvrychk
61local new_fail_count
62local health_fail_retries
63local weight
64
65health_fail_retries=`uci -q -P /var/state get multiwan.${1}.health_fail_retries`
66weight=`uci -q -P /var/state get multiwan.${1}.weight`
67
68failchk=$(query_config failchk $1)
69recvrychk=$(query_config recvrychk $1)
70wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]//g")
71
72if [ -z "$failchk" ]; then
73wan_fail_map="$wan_fail_map $1[1]"
74        if [ "$health_fail_retries" == "1" ]; then
75                fail_wan $1
76        fi
77else
78        if [ "$failchk" != "x" ]; then
79                new_fail_count=$(expr $failchk + 1)
80                if [ "$new_fail_count" -lt "$health_fail_retries" ]; then
81                        wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]/$1\[${new_fail_count}\]/g")
82                else
83                        wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]/$1\[x\]/g")
84                        if [ "$weight" != "disable" ]; then
85                        refresh_loadbalancer
86                        fi
87                        refresh_dns
88                        failover add $1
89                fi
90        fi
91fi
92}
93
94recover_wan() {
95local failchk
96local recvrychk
97local new_fail_count
98local wanid
99local health_recovery_retires
100local weight
101
102health_recovery_retries=`uci -q -P /var/state get multiwan.${1}.health_recovery_retries`
103weight=`uci -q -P /var/state get multiwan.${1}.weight`
104
105failchk=$(query_config failchk $1)
106recvrychk=$(query_config recvrychk $1)
107wanid=$(query_config wanid $1)
108
109if [ "$failchk" == "x" ]; then
110        if [ -z "$recvrychk" ]; then
111                wan_recovery_map="$wan_recovery_map $1[1]"
112                if [ "$health_recovery_retries" == "1" ]; then
113                        recover_wan $1
114                fi
115        else
116                new_recovery_count=$(expr $recvrychk + 1)
117                        if [ "$new_recovery_count" -lt "$health_recovery_retries" ]; then
118                                wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]/$1\[${new_recovery_count}\]/g")
119                        else
120                                wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]//g")
121                                wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]//g")
122                                if [ "$weight" != "disable" ]; then
123                                refresh_loadbalancer
124                                fi
125                                refresh_dns
126                                failover del $1
127                        fi
128        fi
129fi
130}
131
132acquire_wan_data() {
133if [ $wancount -lt 9 ]; then
134
135local ipaddr
136local gateway
137local ifname
138local check_old_map
139local get_wanid
140local old_ifname
141local old_ipaddr
142local old_gateway
143
144ifname=`uci -q -P /var/state get network.${1}.ifname`
145ipaddr=`uci -q -P /var/state get network.${1}.ipaddr`
146gateway=`uci -q -P /var/state get network.${1}.gateway`
147
148if [ -z "$ifname" ]; then
149ifname="x"
150fi
151if [ -z "$ipaddr" ]; then
152ipaddr="x"
153fi
154if [ -z "$gateway" ]; then
155gateway="x"
156fi
157
158check_old_map=`echo $wan_id_map 2>&1 | grep -o "$1\["`
159
160        if [ -z $check_old_map ]; then
161                wancount=`expr $wancount + 1`
162                wan_if_map="$wan_if_map $1[${ifname}] "
163                wan_id_map="$wan_id_map $1[${wancount}] "
164                wan_gw_map="$wan_gw_map $1[${gateway}] "
165                wan_ip_map="$wan_ip_map $1[${ipaddr}] "
166        else
167                old_ipaddr=$(query_config ipaddr $1)
168                old_gateway=$(query_config gateway $1)
169                old_ifname=$(query_config ifname $1)
170                get_wanid=$(query_config wanid $1)
171
172                wan_if_map=$(echo $wan_if_map | sed -e "s/${1}\[${old_ifname}\]/$1\[${ifname}\]/g")
173                wan_ip_map=$(echo $wan_ip_map | sed -e "s/${1}\[${old_ipaddr}\]/$1\[${ipaddr}\]/g")
174                wan_gw_map=$(echo $wan_gw_map | sed -e "s/${1}\[${old_gateway}\]/$1\[${gateway}\]/g")
175
176                if [ "$old_ifname" != "$ifname" ]; then
177                iptables -D MultiWanPreHandler -t mangle -i $old_$ifname -m state --state NEW -j FW${get_wanid}MARK
178                iptables -A MultiWanPreHandler -t mangle -i $ifname -m state --state NEW -j FW${get_wanid}MARK
179                iptables -D MultiWanPostHandler -t mangle -o $old_$ifname -m mark --mark 0x123 -j FW${get_wanid}MARK
180                iptables -A MultiWanPostHandler -t mangle -o $ifname -m mark --mark 0x123 -j FW${get_wanid}MARK
181                fi
182
183                refresh_routes
184                refresh_loadbalancer
185                refresh_dns
186
187                if [ "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
188                iprules_config $get_wanid
189                qos_init $1 $get_wanid
190                failover del $1
191                else
192                failover add $1
193                fi
194        fi
195else
196wancount=9
197fi
198}
199
200query_config() {
201case $1 in
202     ifname) echo $wan_if_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
203     ipaddr) echo $wan_ip_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
204     gateway) echo $wan_gw_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
205     wanid) echo $wan_id_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
206     failchk) echo $wan_fail_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
207     recvrychk) echo $wan_recovery_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
208     group) echo $wan_id_map | grep -o "\w*\[$2\]" | awk -F "[" '{print $1}';;
209esac
210}
211
212mwan_kill() {
213local otherpids
214local execute
215otherpids=$(ps -a 2>&1 | grep 'multiwan agent' | grep -v $$ | awk -F " " '{print $1}')
216echo "$otherpids" | while read execute
217do
218kill -9 ${execute} > /dev/null 2>&1
219done
220}
221
222stop() {
223local group
224local i
225
226mwan_kill
227flush
228
229if [ "$1" != "restart" ]; then
230echo "## Refreshing Interfaces ##"
231        i=0
232        while [ $i -lt $wancount ]; do
233                i=`expr $i + 1` 
234                group=$(query_config group $i)
235                fdown $group > /dev/null 2>&1
236                ifup $group > /dev/null 2>&1 &
237        done
238
239if [ ! -z "$CHKFORQOS" ]; then
240/etc/init.d/qos restart & > /dev/null 2>&1
241fi
242
243echo "## Unloaded, updating syslog and exiting. ##"
244mwnote "Succesfully Unloaded on $(exec date -R)."
245
246else
247
248echo "## Restarting Multi-WAN. ##"
249mwnote "Reinitializing Multi-WAN Configuration."
250/etc/init.d/multiwan start & > /dev/null 2>&1
251
252fi
253
254ip route flush cache
255
256exit
257}
258
259clear_rules() {
260local group
261local i
262
263iptables -t mangle -F PREROUTING
264iptables -t mangle -F FORWARD
265iptables -t mangle -F POSTROUTING
266iptables -t mangle -F OUTPUT
267iptables -t mangle -F MultiWan
268iptables -t mangle -X MultiWan
269iptables -t mangle -F MultiWanRules
270iptables -t mangle -X MultiWanRules
271iptables -t mangle -F MultiWanDNS
272iptables -t mangle -X MultiWanDNS
273iptables -t mangle -F MultiWanPreHandler
274iptables -t mangle -X MultiWanPreHandler
275iptables -t mangle -F MultiWanPostHandler
276iptables -t mangle -X MultiWanPostHandler
277iptables -t mangle -F LoadBalancer
278iptables -t mangle -X LoadBalancer
279
280i=0
281while [ $i -lt $wancount ]; do
282i=`expr $i + 1`
283iptables -t mangle -F FW${i}MARK
284done
285
286i=0
287while [ $i -lt $wancount ]; do
288i=`expr $i + 1`
289iptables -t mangle -X FW${i}MARK
290done
291
292if [ ! -z "$CHKFORQOS" ]; then
293
294iptables -t mangle -F MultiWanQoS
295iptables -t mangle -X MultiWanQoS
296
297i=0
298while [ $i -lt $wancount ]; do
299i=`expr $i + 1` 
300group=$(query_config group $i)
301iptables -t mangle -F MultiWanQoS_${group}
302iptables -t mangle -F MultiWanQoS_${group}_ct
303iptables -t mangle -X MultiWanQoS_${group}
304iptables -t mangle -X MultiWanQoS_${group}_ct
305done
306
307fi
308}
309
310qos_init() {
311local ifname
312local queue_count
313local get_wan_tc
314local get_wan_iptables
315local qos_done_chk
316local add_qos_iptables
317local add_qos_tc
318local execute
319local i
320local p
321
322qos_done_chk=`echo $qos_done | grep -o "$1\."`
323
324if [ ! -z "$qos_done_chk" ]; then
325return
326fi
327
328ifname=$(query_config ifname $1)
329
330if [ "$ifname" == "x" ]; then
331return
332fi
333
334queue_count=$(tc filter list dev $ifname | tail -n 1 | awk -F " " '{print $10}' | sed "s/0x//g")
335
336if [ -z "$queue_count" ]; then
337return
338fi
339
340queue_count=`expr $queue_count + 1`
341
342iptables -t mangle -N MultiWanQoS_${1}
343iptables -t mangle -N MultiWanQoS_${1}_ct
344
345get_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") 
346get_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")
347
348rm /tmp/.mwan.$1.sedfilter > /dev/null 2>&1
349i=0
350while [ $i -lt $queue_count ]; do
351echo "s/\(0x$i \|0x$i\/0xffffffff\)/0x${2}${i} /g" >> /tmp/.mwan.$1.sedfilter
352i=`expr $i + 1` 
353done
354
355add_qos_iptables=$(echo "$get_wan_iptables" | sed -f /tmp/.mwan.$1.sedfilter)
356echo "$add_qos_iptables" | while read execute; do ${execute}; done
357
358rm /tmp/.mwan.$1.sedfilter
359i=1
360while [ $i -lt $queue_count ]; do
361echo "s/0x$i /0x${2}${i} fw /g" >> /tmp/.mwan.$1.sedfilter
362i=`expr $i + 1` 
363done
364
365add_qos_tc=$(echo "$get_wan_tc" | sed -f /tmp/.mwan.$1.sedfilter)
366echo "$add_qos_tc" | while read execute; do ${execute}; done
367rm /tmp/.mwan.$1.sedfilter
368
369i=0
370while [ $i -lt $queue_count ]; do
371  p=`expr $i + $2 \* 10`
372if [ $i -lt $(expr $queue_count - 1) ]; then
373  ip rule add fwmark 0x$(expr $p + 1) table $(expr $2 \* 10) prio $(expr $p + 2)
374fi
375  iptables -t mangle -A MultiWanQoS -m mark --mark 0x$p -j MultiWanQoS_${1}
376  i=`expr $i + 1`
377done
378
379qos_done="$qos_done $1."
380
381}
382
383mwanrule() {
384        local src
385        local dst
386        local ports
387        local proto
388        local wanrule
389
390        config_get src $1 src
391        config_get dst $1 dst
392        config_get ports $1 ports
393        config_get proto $1 proto
394        config_get wanrule $1 wanrule
395        ports_first=${ports%-*}
396        ports_last=${ports#*-}
397
398       if [ -z "$wanrule" ]; then
399          return
400       fi
401
402    if [ "$wanrule" != "balancer" ]; then
403       wanrule=$(query_config wanid ${wanrule})
404       wanrule="FW${wanrule}MARK"
405    elif [ "$wanrule" == "balancer" ]; then
406       wanrule="LoadBalancer"
407    fi
408    if [ "$dst" == "all" ]; then
409        dst=$NULL
410    fi
411    if [ "$proto" == "all" ]; then
412        proto=$NULL
413    fi
414    if [ "$ports" == "all" ]; then
415        ports=$NULL
416    fi
417    if [ "$ports_first" -ne "$ports_last" ]; then
418        ports="$ports_first:$ports_last"
419    fi
420       add_rule() {
421            if [ "$proto" == "icmp" ]; then
422               ports=$NULL
423            fi
424                if [ "$src" == "all" ]; then
425          src=$NULL
426    fi
427               iptables -t mangle -A MultiWanRules -m mark --mark 0x0\
428                        ${proto:+-p $proto} \
429                        ${src:+-s $src} \
430                        ${dst:+-d $dst} \
431                        ${ports:+--dport $ports} \
432                        -j $wanrule
433        }
434     if  [ -z "$proto" -a ! -z "$ports" ]; then
435                proto=tcp
436                add_rule
437                proto=udp
438                add_rule
439                return
440       fi
441        add_rule
442}
443
444refresh_dns() {
445local dns
446local group
447local ipaddr
448local gateway
449local ifname
450local failchk
451local compile_dns
452local resolv_conf
453local dns_server
454local i
455
456iptables -F MultiWanDNS -t mangle
457
458rm $resolv_conf
459touch $resolv_conf
460
461echo "## Refreshing DNS Resolution and Tables ##"
462
463i=0
464while [ $i -lt $wancount ]; do
465i=`expr $i + 1`
466group=$(query_config group $i)
467gateway=$(query_config gateway $group)
468ipaddr=$(query_config ipaddr $group)
469ifname=$(query_config ifname $group)
470failchk=$(query_config failchk $group)
471
472dns=`uci -q -P /var/state get network.${group}.dns`
473dns=$(echo $dns | sed -e "s/ /\n/g")
474
475if [ ! -z "$dns" -a "$failchk" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" -a "$ifname" != "x" ]; then
476echo "$dns" | while read dns_server
477do
478        iptables -t mangle -A MultiWanDNS -d $dns_server -p UDP --dport 53 -j FW${i}MARK
479
480                compile_dns="nameserver $dns_server"
481                echo "$compile_dns" >> $resolv_conf
482done
483fi
484done
485}
486
487iptables_init() {
488echo "## IPTables Rule Initialization ##"
489local iprule
490local group
491local ifname
492local execute
493local IMQ_NFO
494local default_route_id
495local i
496
497if [ ! -z "$CHKFORQOS" ]; then
498echo "## QoS Initialization ##"
499
500/etc/init.d/qos restart > /dev/null 2>&1
501
502IMQ_NFO=`iptables -n -L PREROUTING -t mangle -v | grep IMQ |  awk -F " " '{print $6,$12}'`
503
504iptables -t mangle -F PREROUTING
505iptables -t mangle -F FORWARD
506iptables -t mangle -F POSTROUTING
507iptables -t mangle -F OUTPUT
508
509echo "$IMQ_NFO" | while read execute
510do
511iptables -t mangle -A PREROUTING -i $(echo $execute | awk -F " " '{print $1}') -j IMQ --todev $(echo $execute | awk -F " " '{print $2}')
512done
513
514iptables -t mangle -N MultiWanQoS
515
516i=0
517while [ $i -lt $wancount ]; do
518i=`expr $i + 1` 
519qos_init $(query_config group $i) $i
520done
521
522fi
523
524iptables -t mangle -N MultiWan
525iptables -t mangle -N LoadBalancer
526iptables -t mangle -N MultiWanRules
527iptables -t mangle -N MultiWanDNS
528iptables -t mangle -N MultiWanPreHandler
529iptables -t mangle -N MultiWanPostHandler
530
531echo "## Creating FW Rules ##"
532i=0
533while [ $i -lt $wancount ]; do
534i=`expr $i + 1` 
535iprule=$(expr $i \* 10)
536iptables -t mangle -N FW${i}MARK
537iptables -t mangle -A FW${i}MARK -j MARK --set-mark 0x${iprule}
538iptables -t mangle -A FW${i}MARK -j CONNMARK --save-mark
539done
540
541iptables -t mangle -A LoadBalancer -j MARK --set-mark 0x123
542iptables -t mangle -A LoadBalancer -j CONNMARK --save-mark
543
544iptables -t mangle -I PREROUTING -j MultiWan
545iptables -t mangle -I FORWARD -j MultiWan
546iptables -t mangle -I OUTPUT -j MultiWan
547iptables -t mangle -I POSTROUTING -j MultiWan
548
549iptables -t mangle -A MultiWan -j CONNMARK --restore-mark
550iptables -t mangle -A MultiWan -j MultiWanPreHandler
551
552refresh_dns
553
554config_load "multiwan"
555config_foreach mwanrule mwanfw
556
557if [ "$default_route" != "balancer" ]; then
558default_route_id=$(query_config wanid $default_route)
559iptables -t mangle -A MultiWanRules -m mark --mark 0x0 -j FW${default_route_id}MARK
560else
561iptables -t mangle -A MultiWanRules -m mark --mark 0x0 -j LoadBalancer
562fi
563
564iptables -t mangle -A MultiWan -j MultiWanRules
565iptables -t mangle -A MultiWan -j MultiWanDNS
566iptables -t mangle -A MultiWan -j MultiWanPostHandler
567
568i=0
569while [ $i -lt $wancount ]; do
570i=`expr $i + 1` 
571group=$(query_config group $i)
572ifname=$(query_config ifname $group)
573iptables -t mangle -A MultiWanPreHandler -i $ifname -m state --state NEW -j FW${i}MARK
574iptables -t mangle -A MultiWanPostHandler -o $ifname -m mark --mark 0x123 -j FW${i}MARK
575done
576
577if [ ! -z "$CHKFORQOS" ]; then
578iptables -t mangle -A MultiWan -j MultiWanQoS
579fi
580}
581
582refresh_loadbalancer() {
583local group
584local gateway
585local ifname
586local failchk
587local weight
588local nexthop
589local pre_nexthop_chk
590local i
591
592
593echo "## Refreshing Load Balancer ##"
594
595CHKIPROUTE=`cat /etc/iproute2/rt_tables | grep LoadBalancer`
596 if [ -z "$CHKIPROUTE" ]; then
597echo "123     LoadBalancer" >> /etc/iproute2/rt_tables
598 fi
599ip rule del prio 123 > /dev/null 2>&1
600ip route flush table 123 > /dev/null 2>&1
601
602        for TABLE in 123
603        do
604                ip route | grep link | while read ROUTE
605                do
606                ip route add table $TABLE to $ROUTE
607                done
608         done
609
610i=0
611while [ $i -lt $wancount ]; do
612i=`expr $i + 1` 
613group=$(query_config group $i)
614failchk=$(query_config failchk $group)
615gateway=$(query_config gateway $group)
616ifname=$(query_config ifname $group)
617
618weight=`uci -q -P /var/state get multiwan.${group}.weight`
619
620if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
621nexthop="$nexthop nexthop via $gateway dev $ifname weight $weight"
622fi
623done
624
625pre_nexthop_chk=`echo $nexthop | awk -F "nexthop" '{print NF-1}'`
626if [ "$pre_nexthop_chk" == "1" ]; then
627ip route add default via $(echo $nexthop | awk -F " " '{print $3}') dev $(echo $nexthop | awk -F " " '{print $5}') proto static table 123
628elif [ "$pre_nexthop_chk" -gt "1" ]; then
629ip route add proto static table 123 default scope global $nexthop
630fi
631
632ip rule add fwmark 0x123 table 123 prio 123
633ip route flush cache
634}
635
636refresh_routes() {
637local iprule
638local gateway
639local group
640local ifname
641local ipaddr
642local i
643
644echo "## Refreshing Routing Tables ##"
645
646i=0
647while [ $i -lt $wancount ] 
648do
649i=`expr $i + 1` 
650group=$(query_config group $i)
651gateway=$(query_config gateway $group)
652ifname=$(query_config ifname $group)
653ipaddr=$(query_config ipaddr $group)
654
655iprule=$(expr $i \* 10)
656ip route flush table $iprule > /dev/null 2>&1
657
658        for TABLE in $iprule
659        do
660                ip route | grep link | while read ROUTE
661                do
662                ip route add table $TABLE to $ROUTE
663                done
664         done
665
666if [ "$gateway" != "x" -a "$ipaddr" != "x" -a "$ifname" != "x" ]; then
667ip route add default via $gateway table $iprule src $ipaddr proto static
668route add default gw $gateway dev $ifname 
669fi
670done
671
672ip route flush cache
673}
674
675iprules_config() {
676
677local iprule
678local group
679local gateway
680local ipaddr
681
682iprule=$(expr $1 \* 10)
683group=$(query_config group $1)
684gateway=$(query_config gateway $group)
685ipaddr=$(query_config ipaddr $group)
686
687CHKIPROUTE=`cat /etc/iproute2/rt_tables | grep MWAN${1}`
688 if [ -z "$CHKIPROUTE" ]; then
689echo "$iprule      MWAN${1}" >> /etc/iproute2/rt_tables
690 fi
691
692ip rule del prio $iprule > /dev/null 2>&1
693ip rule del prio $(expr $iprule + 1) > /dev/null 2>&1
694
695if [ "$gateway" != "x" -a "$ipaddr" != "x" ]; then
696ip rule add from $ipaddr table $iprule prio $iprule
697ip rule add fwmark 0x${iprule} table $iprule prio $(expr $iprule + 1)
698fi
699}
700
701flush() {
702local iprule
703local i
704
705echo "## Flushing IP Rules & Routes ##"
706
707ip rule flush > /dev/null 2>&1
708ip rule add lookup main prio 32766 > /dev/null 2>&1
709ip rule add lookup default prio 32767 > /dev/null 2>&1
710
711ip route flush table 123 > /dev/null
712
713        i=0
714        while [ $i -lt $wancount ]; do
715                i=`expr $i + 1` 
716                iprule=$(expr $i \* 10)
717                ip route del default > /dev/null 2>&1
718                ip route flush table $iprule > /dev/null 2>&1
719        done
720
721echo "## Clearing Rules ##"
722clear_rules > /dev/null 2>&1
723
724rm $jobfile > /dev/null 2>&1
725}
726
727main_init() {
728local RP_PATH
729local group
730local health_interval
731local i
732
733echo "## Main Initialization ##"
734
735mwan_kill
736flush
737
738refresh_loadbalancer
739
740echo "## IP Rules Initialization ##"
741i=0
742while [ $i -lt $wancount ]; do
743i=`expr $i + 1` 
744iprules_config $i
745done
746
747refresh_routes
748iptables_init
749
750RP_PATH=/proc/sys/net/ipv4/conf
751for IFACE in `ls $RP_PATH`; do
752   echo 0 > $RP_PATH/$IFACE/rp_filter
753done
754echo "## Initialization Complete, switching to background mode. ##"
755mwnote "Succesfully Initialized on $(exec date -R)."
756fail_start_check
757
758stagger_health_monitors() {
759i=0
760while [ $i -lt $wancount ]; do
761i=`expr $i + 1`
762group=$(query_config group $i) 
763health_interval=`uci -q -P /var/state get multiwan.${group}.health_interval`
764if [ ! -z "$health_interval" -a "$health_interval" != "disable" -a "$health_interval" -gt 0 ]; then
765health_monitor $group &
766sleep 3
767fi
768done
769}
770
771stagger_health_monitors &
772bg_task &
773
774exit
775}
776
777health_monitor() {
778local ipaddr_cur
779local gateway_cur
780local ifname_cur
781local ifname
782local ipaddr
783local gateway
784local failchk
785local icmp_hosts
786local icmp_hosts_acquire
787local default_routes_check
788local icmp_test_host
789local timeout
790local check_test
791local health_interval
792local check_for_job
793
794timeout=`uci -q -P /var/state get multiwan.${1}.timeout`
795icmp_hosts=`uci -q -P /var/state get multiwan.${1}.icmp_hosts`
796health_interval=`uci -q -P /var/state get multiwan.${1}.health_interval`
797ifname_cur=$(query_config ifname $1)
798ipaddr_cur=$(query_config ipaddr $1)
799gateway_cur=$(query_config gateway $1)
800
801while [ 1 ]; do
802
803ifname=`uci -q -P /var/state get network.${1}.ifname`
804ipaddr=`uci -q -P /var/state get network.${1}.ipaddr`
805gateway=`uci -q -P /var/state get network.${1}.gateway`
806
807if [ -z "$ifname" ]; then
808ifname="x"
809fi
810
811if [ -z "$ipaddr" ]; then
812ipaddr="x"
813fi
814
815if [ -z "$gateway" ]; then
816gateway="x"
817fi
818
819if [ "$ifname_cur" != "$ifname" -o "$ipaddr_cur" != "$ipaddr" -o "$gateway_cur" != "$gateway" ]; then
820echo $1.acquire >> $jobfile
821exit
822else
823        if [ "$gateway" != "x" ]; then
824        default_routes_check=`ip route | grep -o $gateway`
825                if [ -z "$default_routes_check" ]; then
826                        check_for_job=`cat $jobfile 2>&1 | grep -o "route.refresh"`
827                        if [ -z "$check_for_job" ]; then
828                                echo route.refresh >> $jobfile
829                        fi
830                fi
831        fi
832fi
833
834if [ "$icmp_hosts" != "disable" -a "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
835
836        if [ "$icmp_hosts" == "gateway" -o -z "$icmp_hosts" ]; then
837                icmp_hosts_acquire=$gateway
838        elif [ "$icmp_hosts" == "dns" ]; then
839                icmp_hosts_acquire=`uci -q -P /var/state get network.$1.dns`
840        else
841                icmp_hosts_acquire=$icmp_hosts
842        fi
843
844icmp_hosts=$(echo $icmp_hosts_acquire | sed -e "s/\,/ /g" | sed -e "s/ /\n/g")
845
846ping_test() {
847echo "$icmp_hosts" | while read icmp_test_host
848do
849ping -c 1 -W $timeout -I $ifname $icmp_test_host 2>&1 | grep -o "round-trip"
850done
851}
852
853check_test=$(ping_test)
854
855        if [ -z "$check_test" ]; then
856                echo "$1.fail" >> $jobfile
857        else
858                echo "$1.pass" >> $jobfile
859        fi                     
860
861elif [ "$icmp_hosts" == "disable" ]; then
862echo "$1.pass" >> $jobfile
863fi
864
865sleep $health_interval
866done
867}
868
869bg_task() {
870local check_iptables
871local queued_task
872local bg_counter
873
874bg_counter=0
875
876while [ 1 ]; do
877
878if [ "$bg_counter" -eq 5 ]; then
879
880check_iptables=$(iptables -n -L MultiWan -t mangle | grep "references" | awk -F "(" '{print $2}' | cut -d " " -f 1)
881
882        if [ -z "$check_iptables" -o "$check_iptables" -lt 4 ]; then
883                mwnote "Netfilter rules appear to of been altered."
884                /etc/init.d/multiwan restart &
885                exit
886                fi
887
888bg_counter=0
889
890fi
891
892if [ -f $jobfile ]; then
893
894mv $jobfile $jobfile.work
895
896while read LINE
897do
898
899execute_task(){
900case $2 in
901fail) fail_wan $1;;
902pass) recover_wan $1;;
903acquire) acquire_wan_data $1 && health_monitor $1 &;;
904refresh) refresh_routes;;
905esac
906}
907
908queued_task=`echo $LINE | awk -F "." '{print $1,$2}'`
909execute_task $queued_task
910done < $jobfile.work
911
912rm $jobfile.work
913fi
914
915bg_counter=$(expr $bg_counter + 1)
916
917sleep 1
918done
919}
920
921fail_start_check(){ 
922local ipaddr
923local gateway
924local ifname
925local group
926
927i=0
928while [ $i -lt $wancount ]; do
929i=`expr $i + 1` 
930group=$(query_config group $i)
931ifname=$(query_config ifname $group)
932ipaddr=$(query_config ipaddr $group)
933gateway=$(query_config gateway $group)
934
935if [ "$ifname" == "x" -o "$ipaddr" == "x" -o "$gateway" == "x" ]; then
936failover add $group
937wan_fail_map="echo $wan_fail_map $group[x]"
938fi
939done
940}
941
942wancount=0
943
944config_clear
945config_load "multiwan"
946config_get default_route    config default_route
947config_get resolv_conf      config resolv_conf
948config_get debug            config debug
949
950config_foreach acquire_wan_data interface
951
952CHKFORQOS=`iptables -n -L Default -t mangle 2>&1 | grep "Chain Default"`
953
954jobfile="/tmp/.mwan.jobqueue"
955
956case $1 in
957     agent) silencer main_init;;
958     restart) silencer stop restart;;
959     stop) silencer stop;;
960esac
961
Note: See TracBrowser for help on using the repository browser.