My goal is configure a container behave as a router which load balances over a number of VPN connections.
To do this I'm probabilistically marking initiating packets with:
iptables -I PREROUTING -t mangle -j CONNMARK --restore-mark
iptables -A PREROUTING -t mangle -m statistic --mode random --probability .50 -j MARK --set-mark 200 -m mark --mark 0
iptables -A PREROUTING -t mangle -j MARK --set-mark 201 -m mark --mark 0
iptables -A POSTROUTING -t mangle -j CONNMARK --save-mark
Which selects one of two routing tables:
echo "200 tun0" >> /etc/iproute2/rt_tables
echo "201 tun1" >> /etc/iproute2/rt_tables
ip rule add fwmark 200 table tun0
ip rule add fwmark 201 table tun1
I believe the routing table is being selected correctly, beacuase when I configure either of the tables tun0/1 to use the VPN gateway traffic seems to get to not get returned. A tcpdump
shows traffic exiting but any command fails.
ip route add default 10.7.7.1 dev tun0 table tun0
ip route add default 10.7.7.1 dev tun1 table tun1
If tables tun0/1 use the non-VPN gateway 10.10.10.1
traffic behaves as expected. I can also select between VPN gateways by setting the default route on the main table:
ip route add default 10.7.7.1 dev tun0/1
So the problem appears to be when the VPN gateway is selected via one of the custom tables rather than the main table. Any clues/diagnostics/advice welcomed!
NB I've configured the requisite options :
echo 0 > /proc/sys/net/ipv4/conf/**/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
sysctl -w net.ipv4.fwmark_reflect=1
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o tun1 -j MASQUERADE
ANSWER:
@A.B 's answer provides the solution. I need to add a route for traffic returning to the local network in the tun0/1 tables:
ip r a 10.10.10.0/24 via 10.10.10.1 table tun0
ip r a 10.10.10.0/24 via 10.10.10.1 table tun1
As @A.B said, without these marked packets are sent back out the tun on which they were recieved.