I have a PC with two internet connections. The first one is an Ethernet interface to connect to my local network and access internet via my router. The second one use a GSM modem to connect with ppp.
I would like to be able to use each connection independently when specifying the interface, e.g.:
ping -I eth0 www.google.com
ping -I ppp0 www.google.com
And to be able to use eth0 by default if no interface is selected and the connection via eth0 works but ppp0 if the connection via eth0 does not work.
I read the this article and created the following rules:
# Main table
ip route add 10.0.0.0/24 dev eth0 src 10.0.0.100
ip route add 10.64.64.64 dev ppp0 src 10.123.122.101
ip route add default via 10.0.0.1
# Specific tables
ip route add 10.0.0.0/24 dev eth0 src 10.0.0.100 table eth0
ip route add default via 10.0.0.1 table eth0
ip route add 10.64.64.64 dev ppp0 src 10.123.122.101 table ppp0
ip route add default via 10.64.64.64 table ppp0
# Rules
ip rule add from 10.0.0.100 table eth0
ip rule add from 10.123.122.101 table ppp0
At first it seems to work. However the eth0 table seems to be never used. I expected the following to happen:
ping -I eth0 www.google.ch # Use default gateway in table eth0
ping -I ppp0 www.google.ch # Use default gateway in table ppp0
ping www.google.ch # Use default gateway (main)
However if I remove the default gateway (main) the eth0 interface does not work at all. There is obviously something I don't understand. Can you explain me how to achieve what I want?
EDIT according to @derobert answer:
I tested the following configuration but still get the same error (ok for ppp0 but not working for eth0).
Routes:
# ip rule list
0: from all lookup local
1500: from 10.0.0.100 lookup eth0
1501: from 10.123.122.101 lookup ppp0
2000: from all fwmark 0x1 lookup eth0
2001: from all fwmark 0x2 lookup ppp0
32766: from all lookup main
32767: from all lookup default
# ip route list table eth0
10.0.0.0/24 dev eth0 src 10.0.0.100
default via 10.0.0.1 dev eth0
# ip route list table ppp0
10.64.64.64 dev ppp0 src 10.123.122.101
default via 10.64.64.64 dev ppp0
# ip route list table main
10.64.64.64 dev ppp0 src 10.123.122.101
192.168.1.0/24 dev eth1 src 192.168.1.1
10.0.0.0/24 dev eth0 src 10.0.0.100
Firewall:
# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
# iptables -L -t mangle
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
CONNMARK all -- anywhere anywhere CONNMARK restore
RETURN all -- anywhere anywhere mark match !0x0
MARK all -- anywhere anywhere MARK set 0x1
MARK all -- anywhere anywhere MARK set 0x2
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MARK all -- anywhere anywhere MARK set 0x1
MARK all -- anywhere anywhere MARK set 0x2
CONNMARK all -- anywhere anywhere CONNMARK save
# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 10.0.0.1 anywhere to:10.0.0.100
SNAT all -- 10.64.64.64 anywhere to:10.123.122.101
I removed all the other firewall rules to be sure they do not interfere. I get this results:
# ip route get 8.2.1.1 from 10.0.0.100
8.2.1.1 from 10.0.0.100 via 10.0.0.1 dev eth0
# ip route get 8.2.1.1 from 10.123.122.101
8.2.1.1 from 10.123.122.101 via 10.64.64.64 dev ppp0
# ping -I ppp0 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=45 time=350.108 ms
64 bytes from 8.8.8.8: seq=1 ttl=45 time=349.768 ms
64 bytes from 8.8.8.8: seq=2 ttl=45 time=329.671 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 329.671/343.182/350.108 ms
128# ping -I eth0 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 0 packets received, 100% packet loss
The rules seems to be OK but obviously there is another problem. I don't really understand the role of the SNAT here. I added the rules to reflect the configuration of the answer be they might be wrong.
ip -4 rule ls
? I don't see anypref
in your rules, want to make sure they're in the right order.ip rule list
is the following:0: from all lookup local 32764: from 10.0.0.100 lookup eth0 32765: from 10.123.122.101 lookup ppp0 32766: from all lookup main 32767: from all lookup default
ip route get 8.2.1.1 from 10.0.0.100
andip route get 8.2.1.1 from 10.123.122.101
. Also, those are RFC1918 space, not sure if you've just changed the numbers for privacy, or if this is internal traffic, or if there is NAT running on this box?