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

Last change on this file since 21343 was 21343, checked in by craigc, 7 years ago

[packages] multiwan: updated revision, updated cache loading on
health_monitor

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