-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPBRD_1
664 lines (566 loc) · 25.6 KB
/
PPBRD_1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/bin/bash
# Just a copy for reference
# For README, HowTo, ChangeLog, Contributors and License go to the end of this script
VERSION="1.6"
VERSION_DATE="20171011"
config() {
GATEWAY[1]="dummy1:10.0.1.1:wan1"
GATEWAY[2]="dummy2:10.0.2.1:wan2"
GATEWAY[3]="dummy3:10.0.3.1:wan3"
GATEWAY[4]="dummy4:10.0.4.1:wan4"
VERIFY[1]="8.8.8.8"
VERIFY[2]="8.8.4.4"
FAILOVER[1]="208.67.222.222"
FAILOVER[2]="208.67.220.220"
BALANCED="wan1 wan2"
LAN="eth0"
}
# main custom rules
custom_rules() {
## EXAMPLE RULES
##>> mac address based balancing
#iptables -t mangle -A PREROUTING -m mac --mac-source 00:23:eb:c4:24:02 -j wan1 # packets coming from main router
##>> ip and protocol based rules
#iptables -t mangle -A PREROUTING -p ALL -i $LAN -s 172.16.1.81 -j BALANCE # balance the traffic for this host
#iptables -t mangle -A PREROUTING -p ALL -i $LAN -s 192.168.102.0/24 -j wan2 # force that network to wan2
#iptables -t mangle -A PREROUTING -p UDP -i $LAN -s 172.16.1.1 -m multiport --dports 53,123 -j wan1 # dns and ntp
#iptables -t mangle -A PREROUTING -p TCP -i $LAN -s 172.16.1.1 -m multiport --dports 110,143,993,995,25,465 -j wan1 # mail protocols
#iptables -t mangle -A PREROUTING -p UDP -i $LAN -d voip.internetcalls.com --dport 5060 -j wan2 # voip
#iptables -t mangle -A PREROUTING -p UDP -i $LAN -d voip.internetcalls.com --sport 10000:20000 -j wan2 # voip
#iptables -t mangle -A PREROUTING -p UDP -i $LAN -d voip.internetcalls.com --dport 10000:20000 -j wan2 # voip
# ===> force all lan connection to specified gateway
#iptables -t mangle -A PREROUTING -p ALL -i $LAN -j BALANCE # balance all traffic coming from lan interface
#iptables -t mangle -A OUTPUT -p TCP --dport 80 -j BALANCE # balance traffic coming from firewall it self
#iptables -t mangle -A PREROUTING -p ALL -i $LAN -j wan2 # force all traffic coming from lan interface
#iptables -t mangle -A OUTPUT -p TCP --dport 80 -j wan2 # force traffic coming from firewall it self
# ===> force all lan connection to specified gateway
dummy=""
}
# pre custom rules
pre_onetime_custom_rules() {
## if we need, reset the dafault gateway here
#ip route replace default via $GWIP1 # flynet2mbit
#ip route replace default via $GWIP2 # telecom
# rate calculation based balancing
# iptables -t mangle -N BALANCE_RATE
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 flynet2mbit --rateest-bps1 2000kbit --rateest-gt --rateest2 eutelia --rateest-bps2 8000kbit -j flynet2mbit
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 flynet2mbit --rateest-bps1 2000kbit --rateest-gt --rateest2 alice20mbit --rateest-bps2 12000kbit -j flynet2mbit
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 eutelia --rateest-bps1 8000kbit --rateest-gt --rateest2 flynet2mbit --rateest-bps2 2000kbit -j eutelia
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 eutelia --rateest-bps1 8000kbit --rateest-gt --rateest2 alice20mbit --rateest-bps2 12000kbit -j eutelia
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 alice20mbit --rateest-bps1 12000kbit --rateest-gt --rateest2 flynet2mbit --rateest-bps2 2000kbit -j alice20mbit
# iptables -t mangle -A BALANCE_RATE -m state --state NEW -m rateest --rateest-delta --rateest1 alice20mbit --rateest-bps1 12000kbit --rateest-gt --rateest2 eutelia --rateest-bps2 8000kbit -j alice20mbit
# example of custom balancing rule based on marked gateway 2, 3 and 5
#iptables -t mangle -N BALANCE_CUSTOM1
#iptables -t mangle -A BALANCE_CUSTOM1 -m mark --mark 0 -m state --state NEW -m statistic --mode nth --every 3 --packet 0 -j wan2
#iptables -t mangle -A BALANCE_CUSTOM1 -m mark --mark 0 -m state --state NEW -m statistic --mode nth --every 3 --packet 1 -j wan3
#iptables -t mangle -A BALANCE_CUSTOM1 -m mark --mark 0 -m state --state NEW -m statistic --mode nth --every 3 --packet 2 -j internet5
dummy=""
}
pre_custom_rules() {
dummy=""
}
# post custom rules
post_onetime_custom_rules() {
dummy=""
}
post_custom_rules() {
#iptables -t mangle -A POSTROUTING -o ${GWIF[$ID]} -j RATEEST --rateest-name ${GWNM[$ID]} --rateest-interval 250ms --rateest-ewma 0.5s
# firewall packets itself workaround (non elegante, trovare metodo alternativo)
#iptables -t nat -A POSTROUTING ! -s $(find_primary_ip ${GWIF[$ID]}) -o ${GWIF[$ID]} -m mark --mark {GWID[$ID]} -j SNAT --to-source $(find_primary_ip ${GWIF[$ID]})
dummy=""
}
#######################################################
#######################################################
## CONFIGURATION IS OVER, DON'T EDIT ANYTHING BELLOW ##
#######################################################
#######################################################
#######################################################
# autodetect ip number of specified network interface
find_primary_ip() {
if [ -n $1 ] ; then
echo $(ip -f inet addr show $1 | grep -Po 'inet \K[\d.]+' | head -n1)
fi
}
find_broadcast() {
if [ -n $1 ]; then
echo -n "$(ip addr show $1 | grep -w 'inet' | grep -v ":" | awk '{print $4}' | head -n1)"
fi
}
find_network() {
if [ -n $1 ]; then
#echo "$(find_primary_ip $1)/$(find_netmask $1)"
echo -n "$(ipcalc --silent --network $(ip addr show $1 | grep 'inet' | grep -v ":" | awk '{print $2}') | awk -F = '{print $2}' | head -n1)/"
echo -n "$(ipcalc --silent --prefix $(ip addr show $1 | grep 'inet' | grep -v ":" | awk '{print $2}') | awk -F = '{print $2}' | head -n1)"
fi
}
find_netmask() {
if [ -n $1 ]; then
#echo "$(ip addr show $1 | grep 'inet' | grep -v ":" | awk '{print $2}' | sed -e 's/.*\///')"
echo -n "$(ipcalc --silent --netmask $(ip addr show $1 | grep 'inet' | grep -v ":" | awk '{print $2}')| awk -F = '{print $2}' | head -n1)"
fi
}
find_all_ip() {
if [ -n $1 ]; then
echo $(ip -f inet addr show $1 | grep -Po 'inet \K[\d.]+'| tr '\n' ' ')
fi
}
find_macaddress() {
if [ -n $1 ]; then
ping -W 3 -c 2 $1 >/dev/null 2>&1
arp -n | grep -w $1 | grep -v "incomplete" | awk '{print $3}'
fi
}
find_default_gateway() {
if [ -n $1 ]; then
echo $(ip route show default | grep ^"default" | awk '{print $3}')
fi
}
# calc total args numbers
count_args() {
args=0
for arg in $@
do
let args=$args+1
done
echo $args
}
global_init() {
for ID in ${!GATEWAY[@]}; do
local GW="${GATEWAY[$ID]}"
# put default variables into an array
GWID[$ID]="$ID" # gateway table id
GWIF[$ID]="$(echo $GW | awk -F":" '{print $1}')" # ethernet interface
if ! ip a show ${GWIF[$ID]} >/dev/null 2>&1 ; then echo "ERROR: the interface ${GWIF[${ID}]} of GATEWAY[$ID]=\"$GW\" doesn't exist. exiting"; exit 1; fi
GWIP[$ID]="$(echo $GW | awk -F":" '{print $2}')" # default gateway ip
GWNM[$ID]="$(echo $GW | awk -F":" '{print $3}')" # gateway interface description
GWNW[$ID]="$(find_network ${GWIF[$ID]})" # interface network
IFIP[$ID]="$(find_primary_ip ${GWIF[$ID]})" # interface ip
[ -z "${IFIP[$ID]}" ] && echo "ERROR: the interface ${GWIF[${ID}]} of GATEWAY[$ID]=\"$GW\" has no IP address associated. exiting" && exit 1
[ -z "${GWIP[$ID]}" ] && GWIP[$ID]="$(find_default_gateway ${GWIF[$ID]})" # obtain default gateway ip if not specified
# create useful variables on demand
eval GWID$ID=${GWID[$ID]}
eval GWIF$ID=${GWIF[$ID]}
eval GWIP$ID=${GWIP[$ID]}
eval GWNM$ID=${GWNM[$ID]}
[ -z "${GWNM[$ID]}" ] && ${GWNM[$ID]}=GWIP$ID
unset GW
done
# reset unused variables
unset ID
# print gateways array index id
#echo GWTOT = ${#GWID[@]}
# show debugs messages if required
[ "$DEBUG" -ne 0 ] && for ID in ${!GATEWAY[@]}; do show_debug; done
}
# desc: izbalancing main script handler
# args: $1 $2 $3 $4
# args desc: 'action' 'table id' 'interface name' 'default gateway ip'
# return: none
izbalancing() {
case $1 in
init)
# enable fast failover of broken gateways links (this isn't a real failover)
echo "10" > /proc/sys/net/ipv4/route/gc_timeout
# make this machine a Routing Machine(tm) :-)
echo "1" > /proc/sys/net/ipv4/ip_forward
# disable antispoof filters
#echo "0" > /proc/sys/net/ipv4/conf/${GWIF[$ID]}/rp_filter
for f in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 0 > $f; done
;;
start)
echo -e "=== starting balancing rules: GWID${GWID[$ID]}=${GWID[$ID]} GWIF${GWID[$ID]}=${GWIF[$ID]} GWIP${GWID[$ID]}=${GWIP[$ID]} GWNM${GWID[$ID]}=${GWNM[$ID]}"
izbalancing start_routes
izbalancing start_rules
;;
stop)
echo -e "=== stopping balancing rules: GWID${GWID[$ID]}=${GWID[$ID]} GWIF${GWID[$ID]}=${GWIF[$ID]} GWIP${GWID[$ID]}=${GWIP[$ID]} GWNM${GWID[$ID]}=${GWNM[$ID]}"
izbalancing stop_route
izbalancing stop_rule
;;
start_routes)
# add the default interface gateway to the current table
ip route add default via ${GWIP[$ID]} table ${GWID[$ID]}
# import from table 'main' to 'current table' the whole routing settings
#ip route show scope link proto kernel | while read ROUTE; do ip route add table ${GWID[$ID]} $ROUTE; done # attenzione questo non funziona con le tun
ip route show table main | grep -Ev '(^default|ppp)' | grep -Ev nexthop | grep -Ev ^169.254.0.0/16 | while read ROUTE; do ip route add table ${GWID[$ID]} $ROUTE; done
;;
start_rules)
PRIO="${GWID[$ID]}00"
ip rule add fwmark ${GWID[$ID]} lookup ${GWID[$ID]} prio $PRIO
;;
start_rules_post)
PRIO="1${GWID[$ID]}00"
for IP in $(find_all_ip ${GWIF[$ID]}) ; do
ip rule add from $IP table ${GWID[$ID]} prio $PRIO
let PRIO+=1
done
;;
stop_route)
# reset routing tables
# flush whole routing table of current table
if [ ! -z "$(ip route show table ${GWID[$ID]})" ]; then
ip route flush table ${GWID[$ID]}
fi
# reset to default single gateway
if [ -n "$DEFAULTGW" ] ; then
ip route del default
ip route add default via $DEFAULTGW
fi
# if you don't want to reset the routing cache every time you run this script, comment the following line
ip route flush cache
;;
stop_rule)
# flush whole rule table of current table (except for fwmark rules)
ip rule list | grep -w "lookup ${GWID[$ID]}" | awk '{$1="";print}' | while read RULE; do
ip rule del $RULE
done
;;
*)
echo "$0: wrong method called - abort"
exit 1
;;
esac
}
# desc: izbalancing rules (iptables, routing, etc...)
# args: $1 $2 $3 $4
# args desc: 'action' 'table id' 'interface name' 'default gateway ip'
# return: none
izbalancing_rules() {
case $1 in
pre_onetime)
# create the default balancing chains
iptables -t mangle -N BALANCE
# use the correct gateway when forwarding from LAN
iptables -t mangle -A PREROUTING -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark
# use the correct gateway for reply packets from local connections
iptables -t mangle -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark
;;
pre_onetime_custom)
pre_onetime_custom_rules
;;
pre)
# create the gateway ip chain for for connections to specified gateway
iptables -t mangle -N ${GWNM[$ID]}
# failover host management
if [ ! -z ${FAILOVER[$ID]} ]; then
iptables -t mangle -A OUTPUT -d ${FAILOVER[$ID]} -j ${GWNM[$ID]}
iptables -t mangle -A ${GWNM[$ID]} -d ${FAILOVER[$ID]} -j MARK --set-mark ${GWID[$ID]}
elif [ ! -z ${FAILOVER[1]} ]; then
echo "WARNING: the interface ${GWIF[$ID]} has not failover host associated, using the first in the list: ${FAILOVER[1]}"
iptables -t mangle -A OUTPUT -d ${FAILOVER[1]} -j ${GWNM[1]}
iptables -t mangle -A ${GWNM[$ID]} -d ${FAILOVER[1]} -j MARK --set-mark ${GWID[$ID]}
else
echo "WARNING: the interface ${GWIF[$ID]} has not failover host associated, izping will not able to recovery the connetion after a failure"
fi
# verify host management
if [ ! -z ${VERIFY[$ID]} ]; then
iptables -t mangle -A OUTPUT -d ${VERIFY[$ID]} -j ${GWNM[$ID]}
elif [ ! -z ${VERIFY[1]} ]; then
echo "WARNING: the interface ${GWIF[$ID]} has not verify host associated, using the first in the list: ${VERIFY[1]}"
iptables -t mangle -A OUTPUT -d ${VERIFY[1]} -j ${GWNM[$ID]}
else
echo "WARNING: the interface ${GWIF[$ID]} has not verify host associated, izping will not able to recovery the connetion after a failure"
fi
iptables -t mangle -A ${GWNM[$ID]} -m mark --mark 0 -j MARK --set-mark ${GWID[$ID]}
iptables -t mangle -A ${GWNM[$ID]} -j CONNMARK --save-mark
#iptables -t mangle -A ${GWTNM} -j LOG --log-prefix "GWIP${GWID[$ID]}: " --log-level info
MAC_ADDRESS="$(find_macaddress ${GWIP[$ID]})"
if [ -n "$MAC_ADDRESS" ]
then
# save the gateway in the connection mark for new incoming connections (mac address based rules)
iptables -t mangle -A PREROUTING -i ${GWIF[$ID]} -m mac --mac-source $MAC_ADDRESS -j ${GWNM[$ID]}
iptables -t mangle -A PREROUTING -i ${GWIF[$ID]} -m conntrack --ctstate NEW -j ${GWNM[$ID]}
else
# save the gateway in the connection mark for new incoming connections
iptables -t mangle -A PREROUTING -i ${GWIF[$ID]} -m conntrack --ctstate NEW -j ${GWNM[$ID]}
fi
;;
main_onetime_custom)
custom_rules
;;
post)
# save the gateway in the connection mark for new outgoing connections
post_custom_rules
iptables -t mangle -A POSTROUTING -o ${GWIF[$ID]} -m conntrack --ctstate NEW -j ${GWNM[$ID]}
;;
post_onetime_custom)
post_onetime_custom_rules
;;
post_onetime)
# round robin based balancing
BALANCE_CNT=0
for GW in $BALANCED
do
iptables -t mangle -A BALANCE -m state --state NEW -m statistic --mode nth --every $BALANCE_TOT --packet $BALANCE_CNT -j $GW
let BALANCE_CNT+=1
done
#iptables -t mangle -A POSTROUTING -j CONNMARK --save-mark
#iptables -t mangle -A OUTPUT -j CONNMARK --save-mark
;;
*)
echo "$0: wrong method called - abort"
exit 1
;;
esac
}
stop_iptables() {
iptables -t mangle -F
iptables -t mangle -X
}
stop_all() {
global_init
stop_iptables
for ID in ${!GATEWAY[@]}; do
izbalancing stop
done
}
start_all() {
global_init
izbalancing_rules pre_onetime
for ID in ${!GATEWAY[@]}; do
# show debugs messages if required
[ "$DEBUG" -ne 0 ] && show_debug
izbalancing init
izbalancing start
izbalancing_rules pre
izbalancing_rules post
done
# now add the ip rules src based after fwmark
for ID in ${!GATEWAY[@]}; do
izbalancing start_rules_post
done
izbalancing_rules pre_onetime_custom
izbalancing_rules main_onetime_custom
izbalancing_rules post_onetime_custom
izbalancing_rules post_onetime
}
env() {
# default commands path and variables
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:/root/bin
BALANCE_TOT="$(count_args $BALANCED)"
GATEWAYS_TOT="$(count_args $GATEWAYS)"
DEBUG="0"
}
show_debug() {
echo "=================== DEBUG ====================="
echo " GWID[$ID]=${GWID[$ID]} # Table ID"
echo " GWIF[$ID]=${GWIF[$ID]} # Interface"
echo " IFIP[$ID]=${IFIP[$ID]} # Interface IP"
echo " GWIP[$ID]=${GWIP[$ID]} # Gateway IP"
echo " GWNM[$ID]=${GWNM[$ID]} # Interface Name"
echo "-----------------------------------------------"
}
###############################################################################################
## valid script input from command line
# command prompt menu
usage(){
local progname=$0
echo "izBalancing :: Linux firewall/router bash script featuring in/out multihomed loadbalanced internet gateway traffic with failover facility
written by Ugo Viti <[email protected]>
version: $VERSION released: $VERSION_DATE
usage: $progname [options]
Option: Description:
-----------------------------------------------
start Start the izbalancing subsystem
stop Stop the izbalancing subsystem
restart Restart the izbalancing subsystem
config-routes Restart routes config
status <line> Show line status
-h Display this help menu
-V Display software version
usage: $0 [OPTIONS]"
}
###################################################################
#################### default program menu and arguments validations
NO_ARGS=0
_OPTERROR=65
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args?
then
usage
exit $E_OPTERROR # Exit and explain usage, if no argument(s) given.
fi
# Usage: scriptname -options
# Note: dash (-) necessary
izping_start() {
if [ -x /etc/init.d/izping ]; then
echo "=== Start iZPing Daemon"
/etc/init.d/izping -d -g "${GATEWAY[*]}" -v "${VERIFY[*]}" -f "${FAILOVER[*]}" -l "/var/log/izping.log"
echo "====== iZPing Daemon Started"
fi
}
izping_stop() {
if [ -x /etc/init.d/izping ]; then
echo "=== Stop iZPing Daemon"
/etc/init.d/izping -k -g "${GATEWAY[*]}" -v "${VERIFY[*]}" -f "${FAILOVER[*]}" -l "/var/log/izping.log"
echo "====== iZPing Daemon Stopped"
fi
}
case $1 in
stop)
config
env
stop_all
izping_stop
;;
start)
config
env
stop_all
start_all
izping_start
;;
restart)
config
env
stop_all
start_all
izping_stop
izping_start
;;
config-routes)
config
env
global_init
for ID in ${!GATEWAY[@]}; do
# show debugs messages if required
[ "$DEBUG" -ne 0 ] && show_debug
izbalancing stop_route
izbalancing start_routes
done
;;
status)
config
env
global_init
for ID in ${!GATEWAY[@]}; do
# show debugs messages if required
[ "$DEBUG" -ne 0 ] && show_debug
bstatus="$(iptables -L ${GWNM[$ID]} -v -n -t mangle --line-numbers | grep ^1 | awk '{ print $NF }' | sed 's/0x//')"
[ $bstatus = ${GWID[$ID]} ] && lstatus="UP" || lstatus="DOWN"
if [ -z $2 ] ; then
echo "Line: ${GWNM[$ID]} Status: $lstatus"
unset bstatus lstatus
else
[ $2 = ${GWNM[$ID]} ] && echo $lstatus
fi
done
;;
config)
config
;;
-V)
echo $VERSION
;;
*)
usage
exit 1
esac
## The End
###############################################################################################
# License:
# ----------------------------------------------------------------------------
# Written by: Ugo Viti <[email protected]>
# visit http://www.initzero.it for commercial support
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
# What is this?
# =============
# This bash script allow you to easly and fastly configure a complex Load Balancing Multi Homed Internet Gateway
# for inbound and outbound traffic
# Key Features:
# =============
# - Multiple balanced default gateway configuration
# - Load Balanced outgoing connections from LAN to INTERNET connections
# - Management of multiple incoming connection from many INTERNET ISP lines to DMZ/LAN Servers
# - SystemV compliant script... you can run easly at boot up (like Red Hat, Fedora, SuSE, Mandrake, etc...)
# - Automatically discover your local IP addresses... you can change your IP without reconfigure this script, just restart
# - Start and Stop cleanly your multihomed configuration with simple command (izbalancing start|stop|restart)
# - Adding new internet connections is very fast and easy
# - You must provide the interface name, router ip address and a descriptive name only
# - If the izping daemon is installed into /etc/rc.d/init.d/ it will be used for automatic failover and recovery of internet lines
# Requirements:
# =============
# - GNU/Linux Firewall running Kernel >=2.6.10 (with iptables module CONNMARK available)
# - Bash Shell >= 2.0
# - Standard GNU/Linux coreutils utilities (cat, echo, grep, if, etc...)
# - GNU Version of awk and sed utilities
# - GNU/Linux Netfilter user space utilities (iptables >= 1.2.11)
# - iproute2 utilities
# - Two or more Internet connections (also from different ISPs and IP classes)
# - An ethernet card for each ISP Router
# Tested On:
# ==========
# - GNU/Linux CentOS 6 with 3 internet connections
# Network Topology Example:
# =========================
#
# +----------+ +-------------------+ 192.168.254.254+----------+ +-------+
# | Server x +<--+ | 192.168.254.1:eth1+<--------------->+ Router 1 +<----------->+ ISP 1 +<--+
# +----------+ | | | +----------+ ADSL line 1 +-------+ |
# | | GNU/Linux | |
# +----------+ | 192.168.1.1:eth0| | 192.168.253.254+----------+ +-------+ |
# | Server y +<--+---+LAN+---------->+ 192.168.253.1:eth2+<--------------->+ Router 2 +<----------->+ ISP 2 +<--+---->INTERNET
# +----------+ | | | +----------+ HDSL line 2 +-------+ |
# | | Firewall | |
# +----------+ | | | 192.168.nnn.nnn+----------+ +-------+ |
# | Server z +<--+ | 192.168.nnn.n:ethn+<--------------->+ Router n +<----------->+ ISP n +<--+
# +----------+ +-------------------+ +----------+ T3 line n +-------+
# Installation:
# =============
# 1) If using a Red Hat Linux based distribution, just copy the izbalancing script into /etc/rc./init.d/ and run:
# chmod 755 /etc/rc./init.d/izbalancing
# chkconfig izbalancing on
# 2) Configure or add the following variables into this script:
# GATEWAYS="interface1:gatewayip1:name1 interface2:gatewayip2:name2 interface3:gatewayip3:name3"
# BALANCED="name1 name2"
# VERIFY_HOSTS="ip1 ip2 ip3"
# FAILOVER_HOSTS="ip1 ip2 ip3"
# 3) Comment out the iptables rules as you need
# 4) usable special variables created on demand using the order specified in the GATEWAYS variable:
# $GWIFx = interface name (ex. GWIF1=eth1, GWIF2=eth2 so on...)
# $GWIPx = gateway ip address (ex. GWIP1=10.1.1.1, GWIP2=10.1.2.1 so on...)
# 5) If the izping daemon script is installed into /etc/rc./init.d/, izbalancing will use it to make
# automatic failover and recovery of lines, checking the ip addresses specified into VERIFY_HOSTS and
# FAILOVER_HOSTS variables
# Testing without real interfaces:
# ip li add dummy1 type dummy ; ip a add 10.0.1.254/24 dev dummy1 ; ip link set dummy1 up
# ip li add dummy2 type dummy ; ip a add 10.0.2.254/24 dev dummy2 ; ip link set dummy2 up
# ip li add dummy3 type dummy ; ip a add 10.0.3.254/24 dev dummy3 ; ip link set dummy3 up
# ip li add dummy4 type dummy ; ip a add 10.0.4.254/24 dev dummy4 ; ip link set dummy4 up
# TODO:
# =====
# - Suggestions are welcome :-)
#
# Contributors:
# =============
# - If you want help me enhancing this script, please send patches to my mail address.
#
# Thanks:
# =======
# - Linux Advanced Routing & Traffic Control <http://lartc.org>
# - Nguyễn Đình Nam <[email protected]> of <http://routeskeeper.sourceforge.net/Routeskeeper> for your fantastic tips!
# ChangeLog:
# ==========
# 20171011 - 1.6 - fix routing when exist vpn tun connections
# 20170808 - 1.5 - fix for OUTPUT mangle rules
# 20170329 - 1.4 - updates for kernel >=3.6, major code cleanup and restructuration
# 20161220 - 1.3 - added new command line option: 'status' to display wan lines status
# 20131011 - 1.2 - added new command line option: 'config-routes' to reconfig only routing system without restarting all balancing rules
# 20130603 - 1.1 - misc bugfixes (internal release)
# 20130102 - 1.0 - first public release
# 20121023 - implemented auto failover functions via izping daemon
# 20101125 - major code clean up and restructuration
# 20050712 - some cleanups
# 20050708 - first version