24

On my PC I have to following routing table:

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0

I don't understand how it is analyzed, I mean from top-down or bottom-up?

If it is analyzed from top-down then everything will always be sent to the router in my home even though the IP destination was 192.168.1.15; but what I knew (wrongly?) was that if a PC is inside my same local network then once I recovered the MAC destination through a broadcast message then my PC could send directly the message to the destination.

27

The routing table is used in order of most specific to least specific.

However on linux it's a bit more complicated than you might expect. Firstly there is more than one routing table, and when which routing table is used is dependent on a number of rules.

To get the full picture:

$ ip rule show
0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default

$ ip route show table local
broadcast 127.0.0.0 dev lo  proto kernel  scope link  src 127.0.0.1 
local 127.0.0.0/8 dev lo  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.1 dev lo  proto kernel  scope host  src 127.0.0.1 
broadcast 127.255.255.255 dev lo  proto kernel  scope link  src 127.0.0.1 
broadcast 192.168.0.0 dev eth0  proto kernel  scope link  src 192.168.1.27 
local 192.168.1.27 dev eth0  proto kernel  scope host  src 192.168.1.27 
broadcast 192.168.1.255 dev eth0  proto kernel  scope link  src 192.168.1.27 

$ ip route show table main
default via 192.168.1.254 dev eth0 
192.168.0.0/23 dev eth0  proto kernel  scope link  src 192.168.1.27 

$ ip route show table default

$

The local table is the special routing table containing high priority control routes for local and broadcast addresses.

The main table is the normal routing table containing all non-policy routes. This is also the table you get to see if you simply execute ip route show (or ip ro for short). I recommend not using the old route command anymore, as it only shows the main table and its output format is somewhat archaic.

The table default is empty and reserved for post-processing if previous default rules did not select the packet.

You can add your own tables and add rules to use those in specific cases. One example is if you have two internet connections, but one host or subnet must always be routed via one particular internet connection.

The Policy Routing with Linux book explains all this in exquisite detail.

1
  • is there a way to change that priority ? – Mheni May 30 '18 at 15:59
5

The routing table is usually applied in order from "most specific" to "least specific". A destination of 0.0.0.0 with a netmask of 0.0.0.0, i.e. your default route, is the least specific possible and so will always be applied last.

If you had the MAC address for another PC on your local network, but that was on a different subnet, you could add a specific host route for that PC and you would probably get communication.

8
  • s/usually/always/ . That's pretty fundamental to the way routing tables work. – Celada Mar 6 '15 at 11:12
  • 2
    @Celada I have omitted mention of metrics for "same specific" routes. And then there's policy routing. For the moment at least, I stand by usually. – roaima Mar 6 '15 at 11:27
  • @roaima Could you please a bit clarify what you mean by "same specific" routes? Does policy routing have some specificity compared with regular routing? I thought that the priority in all the routing tables is defined based on Longest Prefix Match - so the longest prefix is more specific. – NK-cell Jun 11 at 14:41
  • 1
    @NK-cell we're getting way into deep water territory here. Consider a computer with a wired connection to a local network and also wireless to that same local network. Imagine the computer has 192.168.1.45 (wired) and 192.168.1.46 (wireless). Both addresses would reference the same gateway for an example named route 192.168.0.0/16. In order to ensure that the (faster?) wired network takes precedence, the routing table has a has a lower metric for this route via 192.168.1.45 than the route via 192.168.1.46. Two of the same specific routes, one with a priority over the other. Does this help you? – roaima Jun 11 at 14:49
  • @roaima This helped, thanks! But what's the reason for having the second(slower) route? Will not traffic never go through it? I'll try to explain what's still unclear for me. Consider the following. We have two routes: 10.11.12.13 via 192.168.1.1, and 10.11.12/24 via 1.2.3.4 with the same metrics. In usual destination routing the longest prefix match will choose 192.168.1.1 because the destination is more specific. Returning to your comment "I stand by usually": Is there some unusual routing, where 1.2.3.4 can be chosen? Sorry if my question is a bit stupid. – NK-cell Jun 11 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.