This document discusses how to configure a multi-homed router to connect to multiple internet service providers (ISPs) simultaneously. It describes using multiple routing tables associated with each network interface to direct traffic. Rules are used to classify traffic and mark packets so they are routed to the appropriate table. Network address translation (NAT) is configured to map internal IP addresses to external IP addresses for each ISP connection. Additional documentation and tools are provided for monitoring link status and load balancing across connections.
4. www.getclouder.com
Assumptions
• You already know how routing is done on
the Internet
• You know what are the iptables tables
• filter
• mangle
• nat
• You already know what NAT is and how it
works
• Forwarding is enabled and allowed in the
router
Chapter 1
Multihoming
5. www.getclouder.com
Usual setup
main routing table
10.0.0.0/24 dev eth2 src 10.0.0.1
77.70.33.0/24 dev eth0 src 77.70.33.12
default via 77.70.33.1 dev eth0
iptables -t nat -A POSTROUTING -j SNAT
-s 10.0.0.0/24 -o eth0 --to 77.70.33.12
Chapter 2
Setup
6. www.getclouder.com
Multiple routing tables
ISP1:
10.0.0.0/24 dev eth2 src 10.0.0.1
77.70.33.0/24 dev eth0 src 77.70.33.12
default via 77.70.33.1 dev eth0
ISP2:
10.0.0.0/24 dev eth2 src 10.0.0.1
83.12.21.0/24 dev eth1 src 83.15.21.65
default via 83.15.21.254 dev eth1
Chapter 2
Setup
9. www.getclouder.com
How to create iproute2 routing tables
Chapter 2
Setup
$ cat /etc/iproute2/rt_tables
# reserved values
255 local
254 main
253 default
10 isp1
20 isp2
0 unspec
10. www.getclouder.com
How to add routes into the new routing
tables
Chapter 2
Setup
ip r a 10.0.0.0/24 dev eth2 t isp1
ip r a 10.0.0.0/24 dev eth2 t isp2
ip r a 77.70.33.0/24 dev eth0 t isp1
ip r a 0/0 via 77.70.33.1 t isp1
ip r a 83.12.21.0/24 dev eth0 t isp2
ip r a 0/0 via 83.15.21.254 t isp2
ip r a == ip route add
t == table
11. www.getclouder.com
How to guide the traffic to enter these
tables
Chapter 2
Setup
ip ru a from 77.70.33.12 t isp1
ip ru a from 83.15.21.65 t isp2
ip ru a == ip rule add
t == table
12. www.getclouder.com
Separating the clients from the home
network
Chapter 2
Separation
• Manually
• based on source IP
• based on destination IP
• based on protocols
• Automatic
• statistic (random,nth)
• recent
• time based
• Route based
13. www.getclouder.com
Manually separating clients
Chapter 2
Separation
• based on source IP
ip rule add from 10.0.0.x table isp1
• based on destination IP
ip rule add to 192.168.0.x table isp1
• based on protocols
iptables marking per protocol
14. www.getclouder.com
CONNMARK
Chapter 2
Separation
iptables -A POSTROUTING -t mangle
-j CONNMARK --restore-mark
iptables -A POSTROUTING -t mangle
-m mark ! --mark 0 -j ACCEPT
iptables -A POSTROUTING -p tcp
--dport 21 -t mangle -j MARK --set-mark 1
iptables -A POSTROUTING -p tcp
--dport 80 -t mangle -j MARK --set-mark 2
iptables -A POSTROUTING -t mangle
-j CONNMARK --save-mark
15. Automatically separate clients
between uplinks
● Statistic
iptables -t mangle -A PREROUTING -j MARK
--set-mark 2 -o dev_ips2 -m statistic --mode
random --probability 0.2
iptables -t mangle -A PREROUTING -j MARK
--set-mark 1 -o dev_ips1 -m statistic --mode nth
--every 2 --packet 0
iptables -t mangle -A PREROUTING -j MARK
--set-mark 2 -o dev_ips2 -m statistic --mode nth
--every 2 --packet 1
17. Automatically separate clients
between uplinks
● recent
-m recent --name ballanced --rcheck
--seconds 300 -j MARK --set-mark 2
-m recent --name ballanced --set -j MARK
--set-mark 1
● time based
-m time --timestart 09:30 --timestop 17:30
--days Mon,Tue,Wed,Thu,Fri
18. Automatically separate clients
between uplinks
● route based
ip route add default scope global
nexthop via $gw_isp1 dev $dev_isp1 weight 1
nexthop via $gw_isp2 dev $dev_isp2 weight 1
19. www.getclouder.com
Source NAT - Set the outgoing IPs
iptables -t nat -A POSTROUTING
-o $dev_isp1 -j SNAT --to $ip_isp1
iptables -t nat -A POSTROUTING
-o $dev_isp2 -j SNAT --to $ip_isp2
SNAT
Chapter 3