CIDR and Route Aggregation: Prefixes and Longest Match
How Classless Inter-Domain Routing replaced address classes with prefixes of any length, how hierarchy and aggregation keep routing tables small, when two routes can and can't be merged, and how routers choose among overlapping routes with longest prefix match.
kvi5hnuCode Sprout team member13 min readOn this page
By the early 1990s the Internet had three problems at once. Class B networks were nearly used up. The 32-bit IPv4 space looked too small for the long term. And the global routing table, which every core router had to hold, was growing so fast that routers would soon run out of memory and processing power.
A group in the IETF (the Internet Engineering Task Force, article 5) studied these problems from 1992. Its answer for the long term was a new protocol with bigger addresses, which became IPv6 (article 10). Its answer for the urgent problems, published in 1993, was Classless Inter-Domain Routing (CIDR). CIDR is still how every IPv4 and IPv6 route on the Internet works, and the current specification is RFC 4632 from 2006.
Prefixes instead of classes
CIDR does two things:
- It removes classes. A block of addresses no longer gets its size from its first bits. Any prefix length is allowed, so a registry can give an organization exactly the size it needs, such as a /20 (4,096 addresses), instead of choosing between a class B (65,536) and a handful of class Cs (256 each).
- It makes routes carry their prefix length. Subnet masks used to be a private matter inside an organization (article 8). With CIDR, every route announced between networks says how many bits it covers, so core routers must handle masks too.
A prefix is a fixed value for the first n bits of an address, written as the base address, a slash and n. The remaining bits can be anything. A shorter prefix covers more addresses. Here are some prefixes and the ranges they cover, computed in the lab (the last line is an IPv6 prefix, which works the same way with 128-bit addresses, as article 10 shows):
0.0.0.0/0 4,294,967,296 addresses 0.0.0.0 - 255.255.255.255
10.0.0.0/8 16,777,216 addresses 10.0.0.0 - 10.255.255.255
198.51.100.0/24 256 addresses 198.51.100.0 - 198.51.100.255
198.51.100.192/27 32 addresses 198.51.100.192 - 198.51.100.223
203.0.113.77/32 1 addresses 203.0.113.77 - 203.0.113.77
2001:db8::/32 79,228,162,514,264,337,593,543,950,336 addresses 2001:db8:: - 2001:db8:ffff:ffff:ffff:ffff:ffff:ffff
Two of those are special:
- 0.0.0.0/0 fixes no bits at all, so it matches every address. That's the default route: "if nothing more specific matches, send it here".
- A /32 (or /128 in IPv6) fixes every bit, so it matches exactly one address. It's called a host route.
The old classes are just particular prefix lengths: a class A network is a /8, a class B a /16 and a class C a /24. You'll sometimes see trailing zero bytes left off, as in 198.51.100/24, which means 198.51.100.0/24. Some tools accept that form and some don't, so write the full address.
Why hierarchy matters
Classless allocation fixed the waste of addresses, but not the size of routing tables. A router needs a routing entry for every destination it must distinguish. If addresses are handed out with no relation to where networks are, a router near the top of the network must list every network below it individually.
The fix comes from research on hierarchical routing in the late 1970s. If addresses are assigned so that everything reachable through one link shares a common prefix, a router needs just one route per link, no matter how many networks are behind it.

In the right-hand tree, adding more networks below either branch never grows the top router's table, as long as the new networks are numbered from that branch's block. The catch is that the numbering is tied to the topology: if a network moves to the other branch, it must be renumbered, or it breaks the pattern and needs its own route again.
On the Internet, the "tree" is the hierarchy of registries and providers. A regional registry gives an ISP (Internet service provider) a large block, the ISP gives each customer a piece of it, and the rest of the Internet needs only one route for the ISP's whole block. Article 13 describes that hierarchy and what happens when a customer wants to leave it.
Route aggregation
Aggregation (also called summarization) is the operation that makes hierarchy pay off: replacing several routes with one shorter prefix that covers them all. Two prefixes can be merged into one when they are the two halves of the same larger block:
- Same length.
- Numerically adjacent.
- Together they make up a block that starts on a multiple of its own size.
The lab uses Python's ipaddress.collapse_addresses(), which merges routes as far as these rules allow:
1. three /26s with a gap
routes in : 198.51.100.0/26, 198.51.100.64/26, 198.51.100.192/26
aggregated: 198.51.100.0/25, 198.51.100.192/26
2. the gap filled
routes in : 198.51.100.0/26, 198.51.100.64/26, 198.51.100.192/26, 198.51.100.128/26
aggregated: 198.51.100.0/24
3. two adjacent, aligned /24s
routes in : 10.1.2.0/24, 10.1.3.0/24
aggregated: 10.1.2.0/23
4. two adjacent /24s that are NOT aligned
routes in : 10.1.3.0/24, 10.1.4.0/24
aggregated: 10.1.3.0/24, 10.1.4.0/24
5. four /24s
routes in : 10.1.4.0/24, 10.1.5.0/24, 10.1.6.0/24, 10.1.7.0/24
aggregated: 10.1.4.0/22

Case 4 is the one people get wrong. 10.1.3.0/24 and 10.1.4.0/24 sit right next to each other, but they belong to different /23 blocks:
10.1.3.0/24 belongs to /23 10.1.2.0/23
10.1.4.0/24 belongs to /23 10.1.4.0/23
A /23 is 512 addresses, so it must start where the third byte is even. In binary the reason is easy to see. Two /24s fit in one /23 only if their first 23 bits match, which means the third bytes may differ only in their last bit. 2 (00000010) and 3 (00000011) qualify, but 3 (00000011) and 4 (00000100) already differ in the second-to-last bit. A route for "10.1.3.0/23" would actually mean 10.1.2.0/23, which covers a different pair of /24s. The only single prefix covering both 10.1.3.0/24 and 10.1.4.0/24 is 10.1.0.0/21, which would also cover six /24s that aren't yours.
How big is the table today?
Aggregation didn't stop routing tables from growing, but it slowed them down enormously. In late 2026, a full Internet routing table held about 1.09 million IPv4 prefixes and about 267,000 IPv6 prefixes, as measured by the daily routing reports of APNIC (the regional address registry for the Asia-Pacific region). About half of the IPv4 entries are more-specifics: smaller prefixes announced inside a larger one that's also announced, usually for traffic engineering (steering where traffic enters a network) or to handle multihoming, being connected to more than one provider (article 13). With those removed, the IPv4 table would aggregate to about half its size.
Longest prefix match
Aggregation and more-specifics create overlapping routes: a router may have a route for 198.51.100.0/24 and another for 198.51.100.128/25 inside it. Both match a packet to 198.51.100.150. Which one wins?
The rule, used by every IP router and host, is longest prefix match: among all routes whose prefix contains the destination, use the one with the longest prefix, the most specific. The default route (/0) matches everything, so it's used only when nothing else does.
Here's a small routing table built in the lab with nested prefixes, each pointing out a different interface so you can see which one is chosen. The interfaces are Linux dummy interfaces, fake ones that exist only for tests like this. In this listing, default means 0.0.0.0/0, and a single address with no prefix length, such as 198.51.100.200, is a /32:
default dev d0 scope link
198.51.100.0/24 dev d24 scope link
198.51.100.128/25 dev d25 scope link
198.51.100.192/26 dev d26 scope link
198.51.100.200 dev d32 scope link
And here's what ip route get says for several destinations. It asks the kernel to look up a destination exactly as it would for a real packet:
198.51.100.200 -> dev d32
198.51.100.201 -> dev d26
198.51.100.150 -> dev d25
198.51.100.77 -> dev d24
203.0.113.5 -> dev d0

- 198.51.100.200 is contained in all five routes. The /32 is the longest, so it goes out
d32. - 198.51.100.201 is in the /0, /24, /25 and /26, but not the /32. The /26 wins.
- 198.51.100.150 is in the /0, /24 and /25. The /25 wins.
- 198.51.100.77 is in the /0 and /24 only.
- 203.0.113.5 matches only the default route.
Longest prefix match is why more-specifics work. A provider can announce 198.51.100.0/24, a customer's more-specific /25 can be announced from somewhere else, and every router sends the /25's traffic to the more-specific route without any special configuration. It's also why an attacker who announces a more-specific route for someone else's addresses can attract their traffic, a problem article 13 returns to.
Routers implement this lookup in specialized data structures, such as tries (trees that branch on one address bit at a time) and TCAM (ternary content-addressable memory, a chip that compares an address with every stored prefix at once), so they can do it for every packet at hundreds of millions of lookups per second. Article 27 follows a packet through the whole forwarding process.
Common questions
What does "classless" in CIDR actually mean?
That an address's first bits no longer decide where the network part ends. Every route and every interface address carries an explicit prefix length instead, and any length from /0 to /32 (or /128 for IPv6) is allowed.
Is 198.51.100.0/24 the same as a class C network?
It's the same size and shape, 24 network bits and 8 host bits. But under CIDR any address can start a /24, including ones in the old class A or B ranges, such as 10.20.30.0/24.
Can I aggregate any two adjacent prefixes?
Only if they're the two halves of one larger, aligned block. Adjacent prefixes that straddle a boundary, such as 10.1.3.0/24 and 10.1.4.0/24, can't be combined into one prefix of the next size up.
If a more-specific route and an aggregate both match, does the router send the packet both ways?
No. It uses exactly one route, the longest match. The aggregate is used only for the addresses the more-specific route doesn't cover.
What is the default route really?
The prefix 0.0.0.0/0 (or ::/0 for IPv6). It contains every address, so it always matches, and because its length is 0, any other matching route beats it. Hosts usually have a default route pointing at their gateway, and core Internet routers in the "default-free zone" have none at all, because they carry a full table.
Why do people announce more-specific routes if they make the table bigger?
To control where traffic enters their network (traffic engineering), to be reachable through two providers (multihoming), or because their block was never part of a provider's aggregate in the first place. Each of these trades a bigger global table for flexibility at one site.
Does longest prefix match apply to hosts too, or only routers?
Both. Your laptop's routing table uses the same rule: the route for your local subnet beats the default route for local destinations, and a VPN (virtual private network) client can add more-specific routes to send some traffic through the tunnel.
Summary
- CIDR (1993, now RFC 4632) removed address classes. Every route and address carries a prefix length, and any length is allowed.
- A prefix fixes the first n bits. /0 is the default route and matches everything. /32 is a host route.
- Assigning addresses to follow the topology lets one route cover everything behind a link. On the Internet, the registry and provider hierarchy plays that role.
- Two prefixes aggregate into one only if they're the two aligned halves of a larger block. Adjacent but unaligned prefixes can't merge.
- In late 2026 the Internet routing table holds about 1.09 million IPv4 and 267,000 IPv6 prefixes. About half the IPv4 entries are more-specifics.
- Routers and hosts choose among overlapping routes by longest prefix match: the most specific matching route wins.
Practice questions
1. Can 192.0.2.0/25 and 192.0.2.128/25 be aggregated? Into what?
Answer
Yes. They're the two halves of 192.0.2.0/24, which starts on a multiple of 256, so they aggregate into 192.0.2.0/24.
2. Can 10.4.6.0/23 and 10.4.8.0/23 be aggregated into a /22?
Answer
No. A /22 is 4 /24s, so it must start where the third byte is a multiple of 4. 10.4.6.0/23 belongs to 10.4.4.0/22 and 10.4.8.0/23 belongs to 10.4.8.0/22. The smallest single prefix covering both is 10.4.0.0/20.
3. What is the smallest single prefix that covers 203.0.113.0/26, 203.0.113.64/26 and 203.0.113.128/26?
Answer
203.0.113.0/24. The first two make 203.0.113.0/25, but adding .128/26 requires the whole /24. (The /24 also covers .192/26, which isn't in the list, so announcing it would claim those addresses too.)
4. A router has routes 0.0.0.0/0 → A, 198.51.100.0/24 → B, 198.51.100.0/25 → C and 198.51.100.96/27 → D. Where does it send packets to 198.51.100.100, 198.51.100.130 and 192.0.2.1?
Answer
198.51.100.100 is in /0, /24, /25 (.0–.127) and /27 (.96–.127), so D. 198.51.100.130 is in /0 and /24 only (it's above the /25), so B. 192.0.2.1 matches only the default, so A.
5. Why does hierarchical address assignment make renumbering necessary when a network changes providers?
Answer
Because the provider's single aggregate route covers the customer only while the customer's addresses come from the provider's block. If the customer moves to another provider and keeps those addresses, the new provider must announce them as a separate more-specific route, and the old aggregate still covers them, so the global table grows and the hierarchy is broken. To keep tables small, the customer is expected to take addresses from the new provider's block, which means renumbering.
6. Why does 203.0.113.5 still reach the router in the lab, even though no route mentions 203.0.113.0/24?
Answer
Because the default route 0.0.0.0/0 contains every address. It's the longest (and only) match for 203.0.113.5, so the packet goes out d0.
Previous: Subnetting: Masks, Prefix Lengths, VLSM and Broadcast Addresses
Next: IPv6 Addresses: Notation, Prefixes, Interface Identifiers and Privacy Addresses

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.