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

Last change on this file since 21415 was 21415, checked in by craigc, 6 years ago

[packages] multiwan: Tweaked probability for random packet selection

  • Property svn:executable set to *
File size: 25.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 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
566iptables -t mangle -N MultiWanLoadBalancer
567
568echo "## Creating FW Rules ##"
569i=0
570while [ $i -lt $wancount ]; do
571i=`expr $i + 1` 
572iprule=$(expr $i \* 10)
573iptables -t mangle -N FW${i}MARK
574iptables -t mangle -A FW${i}MARK -j MARK --set-mark 0x${iprule}
575iptables -t mangle -A FW${i}MARK -j CONNMARK --save-mark
576done
577
578iptables -t mangle -A LoadBalancer -j MARK --set-mark 0x123
579iptables -t mangle -A LoadBalancer -j CONNMARK --save-mark
580
581iptables -t mangle -I PREROUTING -j MultiWan
582iptables -t mangle -I PREROUTING 2 -j MultiWanPreHandler
583iptables -t mangle -I PREROUTING 3 -j MultiWanDNS
584iptables -t mangle -I PREROUTING 4 -j MultiWanRules
585iptables -t mangle -I PREROUTING 5 -j MultiWanLoadBalancer
586
587iptables -t mangle -I FORWARD -j MultiWan
588
589iptables -t mangle -I OUTPUT -j MultiWan
590iptables -t mangle -I OUTPUT 2 -j MultiWanDNS
591iptables -t mangle -I OUTPUT 3 -j MultiWanRules
592iptables -t mangle -I OUTPUT 4 -j MultiWanLoadBalancer
593iptables -t mangle -I OUTPUT 5 -j MultiWanPostHandler
594
595
596iptables -t mangle -I POSTROUTING -j MultiWan
597iptables -t mangle -I POSTROUTING 2 -j MultiWanPostHandler
598
599iptables -t mangle -A MultiWan -j CONNMARK --restore-mark
600
601refresh_dns
602
603config_load "multiwan"
604config_foreach mwanrule mwanfw
605
606if [ "$default_route" != "balancer" ]; then
607default_route_id=$(query_config wanid $default_route)
608iptables -t mangle -A MultiWanRules -m mark --mark 0x0 -j FW${default_route_id}MARK
609else
610iptables -t mangle -A MultiWanRules -m mark --mark 0x0 -j LoadBalancer
611fi
612
613i=0
614while [ $i -lt $wancount ]; do
615i=`expr $i + 1` 
616group=$(query_config group $i)
617ifname=$(query_config ifname $group)
618iptables -t mangle -A MultiWanPreHandler -i $ifname -m state --state NEW -j FW${i}MARK
619iptables -t mangle -A MultiWanPostHandler -o $ifname -m mark --mark 0x123 -j FW${i}MARK
620done
621
622if [ ! -z "$CHKFORQOS" ]; then
623iptables -t mangle -I PREROUTING 6 -j MultiWanQoS
624iptables -t mangle -A FORWARD -j MultiWanQoS
625iptables -t mangle -A OUTPUT -j MultiWanQoS
626iptables -t mangle -A POSTROUTING -j MultiWanQoS
627fi
628}
629
630refresh_loadbalancer() {
631local group
632local gateway
633local ifname
634local failchk
635local weight
636local nexthop
637local pre_nexthop_chk
638local rand_probability
639local last_probability
640local total_weight
641local i
642
643echo "## Refreshing Load Balancer ##"
644
645CHKIPROUTE=`cat /etc/iproute2/rt_tables | grep LoadBalancer`
646 if [ -z "$CHKIPROUTE" ]; then
647echo "123     LoadBalancer" >> /etc/iproute2/rt_tables
648 fi
649ip rule del prio 123 > /dev/null 2>&1
650ip route flush table 123 > /dev/null 2>&1
651
652        for TABLE in 123
653        do
654                ip route | grep link | grep -Ev ^default | while read ROUTE
655                do
656                ip route add table $TABLE to $ROUTE
657                done
658         done
659
660total_weight=0
661last_probability=0
662
663iptables -F MultiWanLoadBalancer -t mangle
664
665i=0
666while [ $i -lt $wancount ]; do
667i=`expr $i + 1` 
668group=$(query_config group $i)
669failchk=$(query_config failchk $group)
670gateway=$(query_config gateway $group)
671ifname=$(query_config ifname $group)
672weight=`uci -q -P /var/state get multiwan.${group}.weight`
673        if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
674                total_weight=$(expr $total_weight + $weight)
675        fi
676done
677
678i=0
679while [ $i -lt $wancount ]; do
680i=`expr $i + 1` 
681group=$(query_config group $i)
682failchk=$(query_config failchk $group)
683gateway=$(query_config gateway $group)
684ifname=$(query_config ifname $group)
685
686weight=`uci -q -P /var/state get multiwan.${group}.weight`
687
688if [ "$gateway" != "x" -a "$ifname" != "x" -a "$failchk" != "x" -a "$weight" != "disable" ]; then
689nexthop="$nexthop nexthop via $gateway dev $ifname weight $weight"
690rand_probability=$(expr $(expr $weight \* 100) / $total_weight + $last_probability)
691last_probability=$rand_probability
692
693        if [ $rand_probability -lt 10 ]; then
694                rand_probability="0.0${rand_probability}"
695        elif [ $rand_probability -lt 100 ]; then
696                rand_probability="0.${rand_probability}"
697        else
698                rand_probability="1.0"
699        fi
700
701        if [ -z "$CHKFORMODULE" ]; then
702                iptables -A MultiWanLoadBalancer -t mangle -m mark --mark 0x123 -m statistic --mode random --probability $rand_probability -j FW${i}MARK
703        fi
704fi
705
706done
707
708pre_nexthop_chk=`echo $nexthop | awk -F "nexthop" '{print NF-1}'`
709if [ "$pre_nexthop_chk" == "1" ]; then
710ip route add default via $(echo $nexthop | awk -F " " '{print $3}') dev $(echo $nexthop | awk -F " " '{print $5}') proto static table 123
711elif [ "$pre_nexthop_chk" -gt "1" ]; then
712ip route add proto static table 123 default scope global $nexthop
713fi
714
715ip rule add fwmark 0x123 table 123 prio 123
716ip route flush cache
717}
718
719refresh_routes() {
720local iprule
721local gateway
722local group
723local ifname
724local ipaddr
725local i
726
727echo "## Refreshing Routing Tables ##"
728
729i=0
730while [ $i -lt $wancount ] 
731do
732i=`expr $i + 1` 
733group=$(query_config group $i)
734gateway=$(query_config gateway $group)
735ifname=$(query_config ifname $group)
736ipaddr=$(query_config ipaddr $group)
737
738iprule=$(expr $i \* 10)
739ip route flush table $iprule > /dev/null 2>&1
740
741        for TABLE in $iprule
742        do
743                ip route | grep link | grep -Ev ^default | while read ROUTE
744                do
745                ip route add table $TABLE to $ROUTE
746                done
747         done
748
749if [ "$gateway" != "x" -a "$ipaddr" != "x" -a "$ifname" != "x" ]; then
750ip route add default via $gateway table $iprule src $ipaddr proto static
751route add default gw $gateway dev $ifname 
752fi
753done
754
755ip route flush cache
756}
757
758iprules_config() {
759
760local iprule
761local group
762local gateway
763local ipaddr
764
765iprule=$(expr $1 \* 10)
766group=$(query_config group $1)
767gateway=$(query_config gateway $group)
768ipaddr=$(query_config ipaddr $group)
769
770CHKIPROUTE=`cat /etc/iproute2/rt_tables | grep MWAN${1}`
771 if [ -z "$CHKIPROUTE" ]; then
772echo "$iprule      MWAN${1}" >> /etc/iproute2/rt_tables
773 fi
774
775ip rule del prio $iprule > /dev/null 2>&1
776ip rule del prio $(expr $iprule + 1) > /dev/null 2>&1
777
778if [ "$gateway" != "x" -a "$ipaddr" != "x" ]; then
779ip rule add from $ipaddr table $iprule prio $iprule
780ip rule add fwmark 0x${iprule} table $iprule prio $(expr $iprule + 1)
781fi
782}
783
784flush() {
785local iprule
786local i
787
788echo "## Flushing IP Rules & Routes ##"
789
790ip rule flush > /dev/null 2>&1
791ip rule add lookup main prio 32766 > /dev/null 2>&1
792ip rule add lookup default prio 32767 > /dev/null 2>&1
793
794ip route flush table 123 > /dev/null
795
796        i=0
797        while [ $i -lt $wancount ]; do
798                i=`expr $i + 1` 
799                iprule=$(expr $i \* 10)
800                ip route del default > /dev/null 2>&1
801                ip route flush table $iprule > /dev/null 2>&1
802        done
803
804echo "## Clearing Rules ##"
805clear_rules > /dev/null 2>&1
806
807rm $jobfile > /dev/null 2>&1
808}
809
810main_init() {
811local RP_PATH
812local group
813local health_interval
814local i
815
816echo "## Main Initialization ##"
817
818mkdir /tmp/.mwan > /dev/null 2>&1
819
820mwan_kill
821flush
822
823
824echo "## IP Rules Initialization ##"
825i=0
826while [ $i -lt $wancount ]; do
827i=`expr $i + 1` 
828iprules_config $i
829done
830
831refresh_routes
832iptables_init
833
834refresh_loadbalancer
835
836RP_PATH=/proc/sys/net/ipv4/conf
837for IFACE in `ls $RP_PATH`; do
838   echo 0 > $RP_PATH/$IFACE/rp_filter
839done
840echo "## Initialization Complete, switching to background mode. ##"
841mwnote "Succesfully Initialized on $(exec date -R)."
842fail_start_check
843
844stagger_health_monitors() {
845i=0
846while [ $i -lt $wancount ]; do
847i=`expr $i + 1`
848group=$(query_config group $i) 
849health_interval=`uci -q -P /var/state get multiwan.${group}.health_interval`
850if [ ! -z "$health_interval" -a "$health_interval" != "disable" -a "$health_interval" -gt 0 ]; then
851health_monitor $group &
852sleep 3
853fi
854done
855}
856
857stagger_health_monitors &
858bg_task &
859
860exit
861}
862
863health_monitor() {
864local ipaddr_cur
865local gateway_cur
866local ifname_cur
867local ifname
868local ipaddr
869local gateway
870local failchk
871local icmp_hosts
872local icmp_hosts_acquire
873local default_routes_check
874local icmp_test_host
875local timeout
876local check_test
877local health_interval
878local check_for_job
879
880. /tmp/.mwan/cache
881
882timeout=`uci -q -P /var/state get multiwan.${1}.timeout`
883icmp_hosts=`uci -q -P /var/state get multiwan.${1}.icmp_hosts`
884health_interval=`uci -q -P /var/state get multiwan.${1}.health_interval`
885ifname_cur=$(query_config ifname $1)
886ipaddr_cur=$(query_config ipaddr $1)
887gateway_cur=$(query_config gateway $1)
888
889while [ 1 ]; do
890
891ifname=`uci -q -P /var/state get network.${1}.ifname`
892ipaddr=`uci -q -P /var/state get network.${1}.ipaddr`
893gateway=`uci -q -P /var/state get network.${1}.gateway`
894
895if [ -z "$ifname" ]; then
896ifname="x"
897fi
898
899if [ -z "$ipaddr" ]; then
900ipaddr="x"
901fi
902
903if [ -z "$gateway" ]; then
904gateway="x"
905fi
906
907if [ "$ifname_cur" != "$ifname" -o "$ipaddr_cur" != "$ipaddr" -o "$gateway_cur" != "$gateway" ]; then
908echo $1.acquire >> $jobfile
909exit
910else
911        if [ "$gateway" != "x" ]; then
912        default_routes_check=`ip route | grep -o $gateway`
913                if [ -z "$default_routes_check" ]; then
914                        check_for_job=`cat $jobfile 2>&1 | grep -o "route.refresh"`
915                        if [ -z "$check_for_job" ]; then
916                                echo route.refresh >> $jobfile
917                        fi
918                fi
919        fi
920fi
921
922if [ "$icmp_hosts" != "disable" -a "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
923
924        if [ "$icmp_hosts" == "gateway" -o -z "$icmp_hosts" ]; then
925                icmp_hosts_acquire=$gateway
926        elif [ "$icmp_hosts" == "dns" ]; then
927                icmp_hosts_acquire=`uci -q -P /var/state get multiwan.$1.dns`
928                if [ -z "$icmp_hosts_acquire" -o "$icmp_hosts_acquire" == "auto" ]; then
929                icmp_hosts_acquire=`uci -q -P /var/state get network.$1.dns`
930                fi
931        else
932                icmp_hosts_acquire=$icmp_hosts
933        fi
934
935icmp_hosts=$(echo $icmp_hosts_acquire | sed -e "s/\,/ /g" | sed -e "s/ /\n/g")
936
937ping_test() {
938echo "$icmp_hosts" | while read icmp_test_host
939do
940ping -c 1 -W $timeout -I $ifname $icmp_test_host 2>&1 | grep -o "round-trip"
941done
942}
943
944check_test=$(ping_test)
945
946        if [ -z "$check_test" ]; then
947                echo "$1.fail" >> $jobfile
948        else
949                echo "$1.pass" >> $jobfile
950        fi                     
951
952elif [ "$icmp_hosts" == "disable" ]; then
953echo "$1.pass" >> $jobfile
954fi
955
956sleep $health_interval
957done
958}
959
960bg_task() {
961local check_iptables
962local queued_task
963local bg_counter
964local current_resolv_file
965
966bg_counter=0
967
968while [ 1 ]; do
969
970. /tmp/.mwan/cache
971
972if [ "$bg_counter" -eq 5 ]; then
973
974check_iptables=$(iptables -n -L MultiWan -t mangle | grep "references" | awk -F "(" '{print $2}' | cut -d " " -f 1)
975
976        if [ -z "$check_iptables" -o "$check_iptables" -lt 4 ]; then
977                mwnote "Netfilter rules appear to of been altered."
978                /etc/init.d/multiwan restart &
979                exit
980                fi
981
982current_resolv_file=$(ls -l -e /tmp/resolv.conf.auto | awk -F " " '{print $5, $9}')
983
984        if [ "$last_resolv_update" != "$current_resolv_file" ]; then
985                refresh_dns
986        fi
987
988bg_counter=0
989
990fi
991
992if [ -f $jobfile ]; then
993
994mv $jobfile $jobfile.work
995
996while read LINE
997do
998
999execute_task(){
1000case $2 in
1001fail) fail_wan $1;;
1002pass) recover_wan $1;;
1003acquire) acquire_wan_data $1 && health_monitor $1 &;;
1004refresh) refresh_routes;;
1005esac
1006}
1007
1008queued_task=`echo $LINE | awk -F "." '{print $1,$2}'`
1009execute_task $queued_task
1010done < $jobfile.work
1011
1012rm $jobfile.work
1013fi
1014
1015bg_counter=$(expr $bg_counter + 1)
1016
1017sleep 1
1018done
1019}
1020
1021fail_start_check(){ 
1022local ipaddr
1023local gateway
1024local ifname
1025local group
1026
1027i=0
1028while [ $i -lt $wancount ]; do
1029i=`expr $i + 1` 
1030group=$(query_config group $i)
1031ifname=$(query_config ifname $group)
1032ipaddr=$(query_config ipaddr $group)
1033gateway=$(query_config gateway $group)
1034
1035if [ "$ifname" == "x" -o "$ipaddr" == "x" -o "$gateway" == "x" ]; then
1036failover add $group
1037fi
1038done
1039}
1040
1041wancount=0
1042
1043config_clear
1044config_load "multiwan"
1045config_get default_route    config default_route
1046config_get debug            config debug
1047
1048config_foreach acquire_wan_data interface
1049
1050update_cache
1051
1052CHKFORQOS=`iptables -n -L Default -t mangle 2>&1 | grep "Chain Default"`
1053CHKFORMODULE=`iptables -m statistic 2>&1 | grep -o "File not found"`
1054
1055jobfile="/tmp/.mwan/jobqueue"
1056
1057case $1 in
1058     agent) silencer main_init;;
1059     restart) silencer stop restart;;
1060     stop) silencer stop;;
1061esac
1062
Note: See TracBrowser for help on using the repository browser.