Route Preference

Suppose a router receives a packet destined for the IP address The router has in its routing table the following three routes:

Protocol AD Metric Prefix Next Hop
OSPF 110 240
EIGRP 90 33789
RIP 120 6

To which next hop address will the packet be routed?

If you picked, you're correct. Why? A router evaluates routes in the following order.

  1. Prefix Length - The longest-matching route is preferred first. Prefix length trumps all other route attributes.
  2. Administrative Distance - In the event there are multiple routes to a destination with the same prefix length, the route learned by the protocol with the lowest administrative distance is preferred.
  3. Metric - In the event there are multiple routes learned by the same protocol with same prefix length, the route with the lowest metric is preferred. (If two or more of these routes have equal metrics, load balancing across them may occur.)

Following these rules, we can see that our RIP-learned route is preferred because it is the most specific route: it has the longest matching prefix (26 bits in length compared to 25 and 24). These rules hold true even when evaluating directly connected routes, which have an AD of zero:

Router# show ip route
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area 
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route

Gateway of last resort is not set is subnetted, 3 subnets
C is directly connected, Loopback1
C is directly connected, Loopback2
C is directly connected, Loopback3 is variably subnetted, 3 subnets, 3 masks
S [120/0] via
C is directly connected, Loopback99
Router# show ip route
Routing entry for
  Known via "static", distance 120, metric 0
  Routing Descriptor Blocks:
      Route metric is 0, traffic share count is 1

I'm afraid that this core concept is too often glossed over in CCNA and other introductory networking classes. Remember: it doesn't matter if we have a less-specific route with a direct 100 Gbps connection to the destination, and a more-specific route which takes 15 hops over 56 Kbps serial links through a bad neighborhood. The most-specific route will always be preferred.