I know it is possible to give multiple IP addresses to the same interface, say eth0. It is also possible to build several virtual interfaces on the same physical NIC, say eth0:1, eth0:2,....
Suppose now they all belong to the same subnet. Can I do policy routing with them? For instance, having several routers in the same subnet, can I assign different gateways to
- different virtual interfaces?
- different IP addresses on the same NIC?
I have tried and failed, so far. I am wondering now whether this is a problem in my configurations, or a problem in line of principle, i.e., it just cannot be done.
EDIT:
In the end, I managed to get it working, and I would like to share this howto for the casual peruser in need. I am using a Debian-family distribution. When I find the time, I will post the equivalent for systemd distros, Fedora, Arch Linux,...
I do not wish to use IP aliasing because it is an obsolete technology which is kept only for backward compatibility, see this git.kernel.org page. So I let eth0 be routed through 192.168.73.129, but then I wish to create a new virtual interface to be routed through 192.168.73.1. I do it like this: I do not change /etc/network/interfaces, where nothing is configured by me. I have added the line
200 lab
to /etc/iproute2/rt_tables. Then I issue the commands:
ip link add link eth0 mac0 address 56:61:4f:7c:77:db type macvlan
this creates the virtual interface mac0 with MAC address 56:61:4f:7c:77:db which I use for address reservation in the router;
ip link set mac0 up
this brings it up
dhclient mac0
and this gives it an IP address (always the same, thanks to address reservation);
IP=$(ifconfig | grep -A 1 mac0 | grep inet | awk '{print $2}' | awk -F ":" '{print $2}')
this stores the IP number of the virtual interface mac0 in the shell variable IP;
ip route del default via 192.168.73.1 dev eth0
ip route add default via 192.168.73.129 dev eth0
ip route add 192.168.73.0/24 dev eth0
ip route add default via 192.168.73.1 dev mac0 table lab
ip route add 192.168.73.0/24 dev mac0 table lab
ip route del 192.168.73.0/24 dev mac0 table main
This configures the routing table, to use 192.168.73.129 as a default gateway for eth0, and 192.168.73.1 as gateway for mac0 in the routing table lab;
ip rule add from $IP table lab
this rule specifies under which conditions to use the routing table lab. all of the above commands are inserted into an executable shell script, and the command to run it is placed in /etc/rc.local, so that the system comes up at boot already correctly configured.
Last things to do, some port forwarding, and address reservation. Done.
Thanks for your help.
P.S: in case anyone's wondering .... I need this because I have two routers at home, 192.168.73.1 and 192.168.73.129; the first one is a normal router, the other one is a DD-WRT router acting as an OpenVPN client to my lab's OpenVPN server. All traffic to 192.168.73.129 gets redirected to my lab, for obvious work-related reasons, while the rest of the family uses 192.168.73.1, which performs DHCP service. My work PC uses 192.168.73.129 as a gateway. When I am on the road, I sometimes need to access work-related business on my pc, which does have a SSH server running, but it passes through 192.168.73.1. So, if I try to ssh into my work pc from away from home, requests come through 192.168.73.1, but replies go through 192.168.73.129, and the ssh session is never established. The contraption above solves this conundrum.