Q&A-4Explain CIDR longest-prefix matching using a broad transit route and a narrower peering route.
Answer
The 90-second answer. A router forwards on the longest prefix match: among every route whose prefix covers the destination, the one with the most bits in its mask wins. 10.40.0.0/16 via transit and 10.40.8.0/24 via peering both cover 10.40.8.25, so the /24 wins: 24 > 16. 10.40.9.25 matches only the /16, so it goes to transit. Nothing else changes that outcome: administrative distance, BGP attributes and configuration order only break ties between routes of the same prefix length.
Mechanism. A route is a prefix plus a length. A destination matches when addr & mask == prefix. A /24 mask has eight more contiguous 1-bits than a /16, so it constrains eight more bits of the address and matches a strictly smaller set: 256 addresses instead of 65,536. That is the whole reason longest-prefix match exists: it lets you advertise one aggregate and still carve out exceptions underneath it, with no ordering rule and no "most recent wins" semantics. Forwarding tables implement it as a trie walked bit by bit, or a multibit trie (commonly a 24-8-8 split for IPv4), so lookup cost is bounded by prefix length rather than table size. 0.0.0.0/0 is just the length-0 entry that matches everything and loses to every other match.
Worked trace. RIB on the source router:
| prefix | next hop | AD |
|---|---|---|
0.0.0.0/0 | transit | 20 (eBGP) |
10.40.0.0/16 | transit | 20 (eBGP) |
10.40.8.0/24 | peering | 20 (eBGP) |
10.40.8.25/32 | static | 1 |
FIB lookups:
| destination | matching prefixes | winner | egress |
|---|---|---|---|
10.40.8.25 | /0, /16, /24, /32 | /32 | static |
10.40.8.25 (host route withdrawn) | /0, /16, /24 | /24 | peering |
10.40.9.25 | /0, /16 | /16 | transit |
10.41.1.1 | /0 | /0 | transit |
Bit check on the third octet: 10.40.8.25 is 00001010.00101000.00001000.00011001; 10.40.9.25 is 00001010.00101000.00001001.00011001. They diverge at bit 24, the last bit of the third octet, so 10.40.9.25 fails the /24 match and falls back to the /16.
RIB versus FIB. The RIB holds every route from every protocol, including both the /16 and the /24: BGP does not compare them, because they are different prefixes and both are valid. The FIB holds one selected next hop per prefix. Longest prefix match is a forwarding-plane rule applied at lookup time; administrative distance (Cisco defaults: connected 0, static 1, eBGP 20, OSPF 110, iBGP 200) and then BGP attributes pick between routes for the same prefix before it reaches the FIB. A static /16 with AD 1 still loses to a learned /24 at lookup.
Failure modes. A more specific route always wins, so a leaked /24 from a peer silently overrides your /16 aggregate: the classic traffic-hijack and traffic-engineering hazard. Detect it by diffing the FIB against the intended policy, not by reading configs. Standard mitigations: reject inbound prefixes longer than /24 in the DFZ, and set RPKI maxLength on ROAs (a ROA for 10.40.0.0/16 with maxLength 24 validates 10.40.8.0/24 but not a /25). Also check the return path separately: asymmetric longest-prefix outcomes are common when only one side has the exception route.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.