Longest Match, Administrative Distance and Metric: How a Cisco Router Really Picks a Route
Three Rules, Two Different Moments
Ask a group of engineers how a router chooses a route and most will answer "lowest administrative distance, then lowest metric". That answer is incomplete, and the missing part causes some of the most confusing BGP problems in real networks. A Cisco router uses three rules, and they act at two different moments. Administrative distance and metric act when routes are installed in the routing table. Longest prefix match acts later, every time a packet is forwarded.
The metric is the first filter, and it works inside a single protocol. OSPF compares costs, EIGRP compares composite metrics, and BGP runs its whole best path algorithm. Each protocol then offers only its winner for each prefix to the RIB. Metrics are never compared across protocols, because an OSPF cost and an EIGRP metric are measured in different units.
Administrative distance is the second filter, and it works between protocols for exactly the same prefix and mask. If OSPF and EIGRP both offer 172.16.0.0/16, the lower AD wins: EIGRP internal at 90 beats OSPF at 110, whatever their metrics are. A prefix with a different mask is a different entry, so it never competes on AD.
Longest prefix match is the last rule, and it is the one that actually moves packets. When a packet arrives, the router looks for every installed entry that contains the destination and uses the most specific one. AD has no say at this point. A route with a worse AD but a longer mask wins every time.
R1#show ip route 10.0.5.1
Routing entry for 10.0.5.0/24
Known via "bgp 65001", distance 200, metric 0
Tag 65100, type internal
Last update from 3.3.3.3 00:02:40 ago
Routing Descriptor Blocks:
* 3.3.3.3, from 3.3.3.3, 00:02:40 ago
Route metric is 0, traffic share count is 1
AS Hops 1
Route tag 65100
MPLS label: none
In this lab, OSPF owns 10.0.0.0/16 with AD 110, and iBGP owns 10.0.5.0/24 with AD 200. Both are installed, because they are different prefixes. A packet to 10.0.5.1 follows the iBGP /24, even though iBGP has the worst default AD of all dynamic protocols. Engineers who expect "OSPF wins because 110 is lower than 200" will look in the wrong place for hours.
Administrative Distance and BGP
With that order clear, the BGP distances start to make sense, because BGP is the only protocol with three of them. Cisco's defaults are 20 for routes learned from eBGP peers, 200 for routes learned from iBGP peers, and 200 for routes originated locally. You can see and change all three with "#distance bgp", whose default is "#distance bgp 20 200 200".
The values are deliberate. An eBGP route comes from another AS, and there is usually no internal route to the same destination, so it should win easily. An iBGP route is a route to somewhere outside the AS, carried across your network. If the IGP also knows that prefix, the IGP is the better source for internal reachability, so iBGP sits above OSPF, IS-IS and EIGRP.
The low eBGP distance has a known side effect. Imagine two sites of the same company connected by an internal OSPF backdoor link, while both sites also receive each other's prefixes from the ISP by eBGP. The eBGP copy has AD 20, the OSPF copy has AD 110, so traffic between the sites goes out to the ISP instead of over the private link. Cisco's fix is "#network backdoor", which gives that eBGP prefix the local BGP distance of 200 instead of 20, so the OSPF route wins.
R1(config)#router bgp 65001
R1(config-router)#address-family ipv4 unicast
R1(config-router-af)#network 172.20.0.0 mask 255.255.0.0 backdoor
R1#show ip route 172.20.0.0
Routing entry for 172.20.0.0/16
Known via "ospf 1", distance 110, metric 3, type intra area
Last update from 10.1.12.2 on GigabitEthernet0/0, 00:00:12 ago
Routing Descriptor Blocks:
* 10.1.12.2, from 2.2.2.2, 00:00:12 ago, via GigabitEthernet0/0
Route metric is 3, traffic share count is 1
R1#show ip bgp rib-failure
Network Next Hop RIB-failure RIB-NH Matches
172.20.0.0/16 203.0.113.1 Higher admin distance n/a
The OSPF route now owns the prefix, and BGP reports its own path as a RIB failure caused by a higher distance. That is the expected and healthy result here. The backdoor keyword does not advertise the prefix. It only changes the local distance.
AD can also be lowered or raised on purpose for a static route. A static route with a distance above the dynamic protocol's distance stays out of the table while the dynamic route exists and appears only when it disappears. This is the classic backup route for an eBGP uplink.
R1(config)#ip route 0.0.0.0 0.0.0.0 192.0.2.254 250
R1#show ip route 0.0.0.0
Routing entry for 0.0.0.0/0, supernet
Known via "bgp 65001", distance 20, metric 0, candidate default path
Tag 64500, type external
Last update from 203.0.113.1 01:12:40 ago
Routing Descriptor Blocks:
* 203.0.113.1, from 203.0.113.1, 01:12:40 ago
Route metric is 0, traffic share count is 1
AS Hops 1
Route tag 64500
MPLS label: none
While the eBGP default at distance 20 exists, the static at 250 stays hidden. When the eBGP session fails, the static route is installed in its place.
Equal Paths and Practical Traps
The last piece of the selection puzzle is what happens when one protocol has several equal paths for the same prefix. The RIB can hold more than one, and CEF then shares traffic between them, per destination by default. Most IPv4 routing protocols install up to 4 equal paths by default. BGP installs exactly 1, whatever the topology looks like, until you configure "#maximum-paths".
R1(config)#router bgp 65001
R1(config-router)#address-family ipv4 unicast
R1(config-router-af)#maximum-paths 2
R1#show ip route 198.51.100.0
Routing entry for 198.51.100.0/24
Known via "bgp 65001", distance 20, metric 0
Tag 65100, type external
Last update from 10.1.13.3 00:00:21 ago
Routing Descriptor Blocks:
10.1.13.3, from 10.1.13.3, 00:00:21 ago
Route metric is 0, traffic share count is 1
AS Hops 2
Route tag 65100
MPLS label: none
* 10.1.12.2, from 10.1.12.2, 00:00:21 ago
Route metric is 0, traffic share count is 1
AS Hops 2
Route tag 65100
MPLS label: none
Two descriptor blocks now appear, each with a traffic share count of 1. BGP still has only one best path in its own table. Multipath adds the other equal paths to the RIB without changing what BGP advertises to its peers. BGP multipath also has its own strict rules for which paths count as "equal", and those come later in the course.
A few traps tie the three rules together, and each of them appears in real troubleshooting. A more specific prefix from any source steals traffic from a broader one, so a leaked /24 from BGP can quietly redirect part of a range that the IGP owns as a /16. A route with AD 255 is never installed, so setting "#distance 255" for a source is a way to keep it out of the RIB. When two protocols offer the same prefix with the same configured AD, Cisco documents no tie-break rule, so never design around that case. And when a BGP route is best in "#show ip bgp" but missing from "#show ip route", look for another source with a lower AD for the same prefix before you look anywhere else.
Remember the sequence and most routing puzzles become simple. Metric picks a winner inside each protocol. Administrative distance picks the owner of each exact prefix. Longest prefix match picks the entry each packet uses. BGP is subject to all three, and its unusual distances of 20 and 200 are the reason it so often wins or loses in ways that surprise people.