Forwarding Tables: Longest-Prefix Matching and Route Aggregation
Destination-Based Forwarding
When a router receives a datagram, it uses the destination address to look up the output link interface in its forwarding table. There are IPv4 addresses, so a forwarding table cannot keep a separate entry for every address. Because addresses in the same subnet share a network prefix, the forwarding table stores address prefixes instead: a destination matches an entry when its leading bits equal that prefix.
Suppose a router has four link interfaces and this forwarding table (* marks bits that are not compared):
| Prefix (binary) | CIDR notation | Link interface |
|---|---|---|
| 11001000 00010111 00010* ****** | 200.23.16.0/21 | 0 |
| 11001000 00010111 00011000 ******** | 200.23.24.0/24 | 1 |
| 11001000 00010111 00011* ****** | 200.23.24.0/21 | 2 |
| Otherwise | 0.0.0.0/0 | 3 |
The destination 200.23.22.161 (11001000 00010111 00010110 10100001) agrees with the first entry in its first 21 bits, so it is forwarded on interface 0. The last row has prefix length 0 and matches any address. It is the default route, used when nothing else matches.
Longest-Prefix Matching
One destination may match several entries. For example, 200.23.24.170 (11001000 00010111 00011000 10101010) agrees with the second entry in its first 24 bits and with the third entry in its first 21 bits. The router then applies the longest-prefix matching rule: among all matching entries, it chooses the one with the longest prefix. The datagram is therefore forwarded on interface 1.
A longer prefix describes a smaller, more specific address range, so longest-prefix matching means "use the most specific route." High-speed routers must finish each lookup in nanoseconds. Instead of scanning the table entry by entry, they use specialized data structures and hardware such as tries or ternary content-addressable memory (TCAM).
Longest-prefix match, bit by bit
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Route Aggregation
Hierarchical address allocation keeps forwarding tables small. Suppose an ISP obtains the block 200.23.16.0/20 and assigns eight /23 blocks to eight organizations:
| Organization | Block | Third byte (binary) |
|---|---|---|
| Organization 0 | 200.23.16.0/23 | 0001000* |
| Organization 1 | 200.23.18.0/23 | 0001001* |
| Organization 2 | 200.23.20.0/23 | 0001010* |
| … | … | … |
| Organization 7 | 200.23.30.0/23 | 0001111* |
The eight blocks share their first 20 bits, so the ISP advertises a single route to the rest of the Internet: "send me any datagram whose first 20 address bits match 200.23.16.0/20." Using one prefix to represent several networks is called route aggregation, or route summarization.
Several blocks can be aggregated into one prefix only if three conditions hold: the addresses are contiguous, the number of blocks is a power of two, and the first block starts on a boundary aligned to the aggregated size. Aggregating blocks of prefix length gives prefix length .
What if organization 1 switches to another ISP? The new ISP can advertise the more specific route 200.23.18.0/23 while the original ISP keeps advertising 200.23.16.0/20. A datagram for organization 1 matches both routes; by longest-prefix matching, the /23 wins and the datagram goes to the new ISP. Datagrams for the other organizations match only the /20 and still go to the original ISP.
Aggregate routes, then move a customer
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Apply longest-prefix matching to four destinations
A router has this forwarding table: 200.23.16.0/21 → 0, 200.23.24.0/24 → 1, 200.23.24.0/21 → 2, default 0.0.0.0/0 → 3. For each destination below, list every matching entry and the chosen interface.
200.23.22.161200.23.24.170200.23.25.7200.23.40.1
Implement longest-prefix matching in Python
Represent the forwarding table 200.23.16.0/21 → 0, 200.23.24.0/24 → 1, 200.23.24.0/21 → 2, 0.0.0.0/0 → 3 as a Python list. Write lookup(destination) that returns the length and interface of the longest matching prefix, and print the result for 200.23.22.161, 200.23.24.170, 200.23.25.7, and 200.23.40.1.