Linux – How to add a route for a specific subnet

linuxnetworkingrouting

I have a Linux computer and an eth0 interface with 2 IPs:

ip : 10.10.1.1
mask : 255.128.0.0
gw : 0.0.0.0

ip : 10.22.32.1
mask : 255.255.255.0
gw : 10.22.32.254

I would like to add a specific gateway for network 10.22.0.0/16, so this is what I did:

route add -net 10.22.0.0/16 gw 10.22.32.254 dev eth0

This doesn't work, and I can't reach machines on 10.10.0.0 anymore. Here is the routing table:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 eth0
10.0.0.0        0.0.0.0         255.128.0.0     U     1      0        0 eth0
10.22.0.0       10.22.32.254    255.255.0.0     UG    0      0        0 eth0
10.22.32.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0

Does it fail because 10.22.0.0/16 is a subpart of 10.10.0.0/9?

Best Answer

The network layout has several issues:

  1. As Stache has pointed out the supposed router for 10.22.0.0/16 needs to have a reachable interface in the subnet "10.10.1.1/9" (see below). As it is, it can only be reached from inside the same subnet which doesn't make sense.
  2. The "10.10.1.1/9" subnet is actually the 10.0.0.0/9 subnet which contains the 10.22.0.0/16 subnet, which is probably where all the confusion comes from.
  3. The very first information you provide is that you use the 10.22.32.0/24 subnet - you'll need to sort that out as well. You can supernet and subnet the routes but you need to make sure that it fits together.

You'll need to sort out subnet addresses and masks. If you use 10.10.0.0/16 you can use 10.22.0.0/16 as a distinct subnet but you'll need a router with an interface within each subnet.