Routing table investigation: longer-prefixes keyword
January 16, 2017
When router makes decision which route use to forward a packet then the route with the longest match is used. “Longest match” means the route with the longest mask.
Let’s take an example of routing table where I defined many static routes which starts with “192”.
R3#show ip route (...) 192.0.0.0/24 is variably subnetted, 2 subnets, 2 masks S 192.0.0.0/25 [1/0] via 172.16.9.99 S 192.0.0.0/24 [1/0] via 172.16.8.88 S 192.0.0.0/23 [1/0] via 172.16.7.77 S 192.0.0.0/17 [1/0] via 172.16.6.66 S 192.0.0.0/16 [1/0] via 172.16.5.55 S 192.0.0.0/15 [1/0] via 172.16.4.44 S 192.0.0.0/9 [1/0] via 172.16.3.33 S 192.0.0.0/8 [1/0] via 172.16.2.22 S 192.0.0.0/7 [1/0] via 172.16.1.11
If you want to verify if the routing table contains route to the specific subnet then use the following command.
R3#show ip route 192.0.0.0 255.255.254.0 Routing entry for 192.0.0.0/23, supernet Known via "static", distance 1, metric 0 Routing Descriptor Blocks: * 172.16.7.77 Route metric is 0, traffic share count is 1 R3#show ip route 192.0.0.0 255.255.253.0 % Network not in table R3#
If you want to verify which route will be used to forward packet to the specific host then use the following command.
R3#show ip route 192.0.0.1 Routing entry for 192.0.0.0/25 Known via "static", distance 1, metric 0 Routing Descriptor Blocks: * 172.16.9.99 Route metric is 0, traffic share count is 1 R3#
What if this route (192.0.0.0/25) is removed from the routing table? How to find out which route would be used instead? – What is the second longest match? To answer on this question you would have to issue the following commands until you find the first entry in the routing table.
R3#show ip route 192.0.0.0 255.255.255.0 R3#show ip route 192.0.0.0 255.255.254.0 R3#show ip route 192.0.0.0 255.255.252.0 R3#show ip route 192.0.0.0 255.255.248.0 (...) R3#show ip route 192.0.0.0 192.0.0.0 R3#show ip route 184.108.40.206 220.127.116.11 R3#show ip route 0.0.0.0 0.0.0.0
As you can see it can be a time-consuming process. You can make it a little bit faster using longer-prefix keyword.
R3# show ip route 192.0.0.0 255.255.0.0 longer-prefixes
How does it work? Basically this command means “find all routes in the routing table which starts with 192.0.0.0/16”. For example the routes to the following subnets would be found (if they would exist in routing table):
192.0.0.0/8 (and it matches host ip 192.0.0.1)
18.104.22.168/24 (but it does not match host ip 192.0.0.1)
22.214.171.124/26 (and it matches host ip 192.0.0.1)
However the route to subnet 126.96.36.199/1 is not found (but it matches host ip 192.0.0.1).
Summary – in this particular example I wanted to find subnets which match host ip 192.0.0.1. longer-prefixes keyword is useful to find out routes to these specific subnets however the returned result:
- Contains all subnets which you are looking for (ex. 188.8.131.52/26)
- Contains some subnets which you are not looking for (ex. 184.108.40.206/24)
- Contains only some supernets which you are looking for – it returns 192.0.0.0/8 but not 220.127.116.11/1