What is Longest prefix match?

The rule by which a router, when several routes in its routing table match a destination IP address, chooses the route with the longest (most specific) prefix. For example, a destination that matches both 10.0.0.0/8 and 10.1.0.0/16 uses the /16 route. The default route (0.0.0.0/0), used when nothing else matches, is the shortest possible route, with a length of 0.

Japanese name: ロンゲストマッチ(最長一致)

Longest prefix match appears in 2 practice questions across 2 topics, including Computer Networking Basics, Network Design and Operations.

Field: Network Design and Operations

Try it: questions about Longest prefix match

A router's routing table has four routes: 0.0.0.0/0 (next hop router A), 10.0.0.0/8 (router B), 10.1.0.0/16 (router C) and 10.1.2.0/24 (router D). To which router is a packet for 10.1.5.9 sent?

  1. ARouter B
  2. BRouter D
  3. CRouter C
Show answer and explanation

Answer: C. Router C

Of the routes that match the destination address, a router chooses the one with the longest (most specific) prefix. This is called longest prefix match. 10.1.5.9 matches 0.0.0.0/0, 10.0.0.0/8 and 10.1.0.0/16, but it is not in the 10.1.2.0/24 range (10.1.2.0 to 10.1.2.255). The longest of the matches is /16, so the packet goes to router C. 0.0.0.0/0 is the default route that matches every destination and is used only when no other route matches.

A router's routing table has four routes: 172.16.0.0/12 (next hop router B), 172.16.32.0/19 (router A), 172.16.48.0/20 (router C) and 0.0.0.0/0 (router D). To which router is a packet for 172.16.45.10 sent?

  1. ARouter A
  2. BRouter B
  3. CRouter C
Show answer and explanation

Answer: A. Router A

A router chooses, among the routes that match the destination, the one with the longest prefix (longest prefix match). The third octet, 45, is 00101101 in binary. A /19 compares the upper 3 bits of the third octet, and 45 AND 224 = 32, so the address matches 172.16.32.0/19 (172.16.32.0 to 172.16.63.255). A /20 compares the upper 4 bits, and 45 AND 240 = 32, which differs from 48, so it does not match 172.16.48.0/20 (172.16.48.0 to 172.16.63.255). The address also matches 172.16.0.0/12 and the default route, but the longest match, the /19, is chosen and the packet goes to router A.

Longest prefix match in the explanations

Questions that mention Longest prefix match (2)

  1. A router's routing table has four routes: 0.0.0.0/0 (next hop router A), 10.0.0.0/8 (router B), 10.1.0.0/16 (router C) and 10.1.2.0/24 (router D). To which router is a packet for 10.1.5.9 sent?
  2. A router's routing table has four routes: 172.16.0.0/12 (next hop router B), 172.16.32.0/19 (router A), 172.16.48.0/20 (router C) and 0.0.0.0/0 (router D). To which router is a packet for 172.16.45.10 sent?

Books on Network Design and Operations Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

Related terms

Router IP routing Packet ACL (access control list) Administrative distance BGP (Border Gateway Protocol) Broadcast domain Link aggregation OSPF (Open Shortest Path First) Path MTU discovery

Back to the glossary