In a Linux system, which acts as a gateway on my lan, I tried to route traffic using iproute2.
Moreover, before routing it is necessary to perform the NAT, since
the linux machine is connected to a device which allow internet connection and it has 2 IP addresses 172.16.61.1 and 172.16.62.100
I have 2 network cards with the following configuration :
DEVICE=eth3
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
NETMASK=255.255.255.0
IPADDR=192.168.1.150
USERCTL=no
IPV6INIT=no
PEERDNS=yes
DEVICE=eth4
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
NETMASK=255.255.255.0
IPADDR=172.16.61.2
USERCTL=no
IPV6INIT=no
PEERDNS=yes
GATEWAY=172.16.61.1
and I use the following instruction for ip translation via iptables
/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.0/255.255.255.0 -j SNAT --to-source 172.16.61.2
Using the aforementioned configuration, all computers belonging to the 192.168.1.0/24 network which have 192.168.1.150 as gateway are able to connect to the internet.
If I try to use iproute2 to configure the gateway, then I remove the default gateway from eth4 which assumes the following configuration :
DEVICE=eth4
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
NETMASK=255.255.255.0
IPADDR=172.16.61.2
USERCTL=no
IPV6INIT=no
PEERDNS=yes
AND I performed the following steps:
1. In /etc/iproute2/rt_tables I have added the line 1 route61
2. /sbin/ip route add 172.16.61.0/24 via 172.16.61.1 table route61 proto static
3. /sbin/ip route add default via 172.16.61.1 table route61 proto static
4. /sbin/ip rule add from 172.16.61.0/24 pref 15000 table route61
The output of /sbin/ip route show table route61 is
172.16.61.0/24 via 172.16.61.1 dev eth4 proto static
default via 172.16.61.1 dev eth4 proto static
The output of /sbin/ip rule show is
0: from all lookup local
15000: from 172.16.61.0/24 lookup route61
32766: from all lookup main
32767: from all lookup default
but in this case it doesn't work, what am I wrong?
I also tried to use /etc/sysconfig/network-scripts/ifup-routes eth4 with no success
What I need is to understand why I can't get "table based routing" to work as I will have to use it in a context with multiple gateways
Update
To try what is suggested by dirkt, I removed the default gateway
route del -net 0.0.0.0 gw 172.16.61.1 netmask 0.0.0.0 dev eth4
and I used:
ip rule add from 192.168.1.0/24 pref 15000 table route61
what happens is
- the pc that uses 192.168.1.150 as GW, that is the pc object of this post, is able to connect to the internet
- I can't connect to GW anymore unless I remove the instruction
ip rule add from 192.168.1.0/24 pref 15000 table route61
With postrouting the packets coming from 192.168.1.0/24 shouldn't assume 172.16.61.2 as source address ?
If i use or remove default GW using route add -net 0.0.0.0 gw 172.16.61.1 netmask 0.0.0.0 dev eth4 OR route del -net 0.0.0.0 gw 172.16.61.1 netmask 0.0.0.0 dev eth4 and I do :
ip route get 216.58.205.78 from 192.168.1.5
I have RTNETLINK answers: Invalid argument
if I use ip route get 216.58.205.78 from 172.16.61.2 I have 216.58.205.78 from 172.16.61.2 via 172.16.61.1 dev eth4
After route add -net 0.0.0.0 gw 172.16.61.1 netmask 0.0.0.0 dev eth4 if I do :
nc -v 216.58.205.78 443
I have
Connection to 216.58.205.78 443 port [tcp/https] succeeded!
If I remove default GW route del -net 0.0.0.0 gw 172.16.61.1 netmask 0.0.0.0 dev eth4 nc connects only if I specify source ip:
nc -v 216.58.205.78 443 -s 172.16.61.2
