The Longest Match Rule

One of the the concepts that comes up occasionally is that of precedence. For example, one might consider the following routing table entries.

ip route 0.0.0.0 0.0.0.0 1.1.1.1              //default route
ip route 192.168.0.0 255.255.0.0 1.1.1.2      //supernet/cidr route
ip route 192.168.1.0 255.255.255.0 1.1.1.3    //network route
ip route 192.168.1.0 255.255.255.128 1.1.1.4  //subnet route
ip route 192.168.1.20 255.255.255.255 1.1.1.5 //host route

Questions often arise around which path a packet would take when it matches more than one entry. For example, a packet may have a destination address of 192.168.1.20. In this case it matches every single route entry.

The logic is actually simple, even straightforward. A packet will follow the most specific route entry that it matches. So a packet destined to 192.168.1.20 would be routed to a router at 1.1.1.5. If the destination happened to be 192.168.1.21, it would be routed over to 1.1.1.4.

As a personal challenge, think about where packets with the following destinations might be routed:

  1. 192.168.1.1
  2. 192.168.1.145
  3. 192.168.0.1
  4. 8.8.8.8

As with everything, there can be some exceptions to the rule. I think one could contrive interesting examples with recursive routes. Another exception is when no ip classless has been configured on the router.

In that case, the rule only has a minor change. When using no ip classless, a packet will only use the default route when there are no subnets of its classful network. That process is describe in detail in Classful Routing With-no ip classless.

Disclaimer: This article includes the independent thoughts, opinions, commentary or technical detail of Paul Stewart. This may or may does not reflect the position of past, present or future employers.

About Paul Stewart, CCIE 26009 (Security)

Paul is a Network and Security Engineer, Trainer and Blogger who enjoys understanding how things really work. With over 15 years of experience in the technology industry, Paul has helped many organizations build, maintain and secure their networks and systems.
This entry was posted in CCNA, Certification, General, Network, Technology and tagged . Bookmark the permalink.

3 Responses to The Longest Match Rule

  1. Tommy says:

    Can you explain this one line more…..The rest I understand, but this threw me….”If the destination happened to be 192.168.1.20, it would be routed over to 1.1.1.4″

  2. Pingback: Discard Routing for RFC1918 Addresses - PacketU

Comments are closed.