I understand that Linux chooses the most specific route to the destination when it does routing selection. But what about a route's metric? Does it have a higher priority than route's specificity?
A reference to the details of the routing selection algorithm used by Linux would also be appreciated.
The routes metric is to set preference among routes with equal specificity. That is true of routing in general (i.e. Cisco, Windows, etc). So the model works like:
Note that there are other things that could be going on such a policy based routing that lets you do things like route based on the source IP address. But route specificity, administrative distance, and then metric are what I would consider to be the main three things.
*It is called the longest prefix match because a subnet in binary (/24 for example) looks like
11111111.11111111.11111111.00000000
. So a router can just scan the prefix for binary 1s and stop once it hits a zero, and then it has matched the prefix.Linux provides a number of tools for flexible routing selection.
Single routing table
In the simplest case, there is just one kernel routing table and no routes with the SRC attribute. This table contains a number of routes, which were placed there manually (
ip route add
), by the DHCP daemon, or by routing daemons. In this case, the kernel chooses:Note that the kernel metric (displayed by
ip route show
) is chosen by the routing daemon, and is not necessarily related to the metric of any particular routing protocol. For example, Quagga uses the same metric for all the routes it installs in the kernel, independently of the protocol's metric.Source-specific routes
Linux also supports routes with a SRC attribute which only match packets with a given source address. SRC only works for IPv6, and was buggy until very recently (3.11, if memory serves); I don't recommend using it unless you know what you are doing.
Multiple routing tables
If you need more flexibility than the above provides, you will need to play with multiple routing tables, and write rules to choose one particular routing table for each packet. A common technique is to dispatch on source-address in order to simulate source-specific routes. Another technique is to run each routing daemon in its own routing table, and simulate Cisco's "administrative distance". All of this is described in detail in Chapter 4 of the LARTC.