Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 79 ยท The Network Layer: IP

CIDR, VLSM and Route Aggregation

Learn classless addressing: CIDR prefix notation, variable-length subnet masks, supernetting, route aggregation and longest-prefix match, with Python planners.

By the early 1990s the Internet faced two crises at once. The class A, B and C system wasted addresses, so the pool of IPv4 space was draining quickly. And every class C network needed its own entry in the routers' tables, so the global routing table was growing faster than hardware could keep up. The fix, introduced in 1993, was Classless Inter-Domain Routing (CIDR): drop the classes and let a network be any power-of-two block with a prefix of any length from /0 to /32.

CIDR changed how addresses are handed out, how organizations carve up their blocks, and how routers keep their tables small. In this lesson you will learn prefix notation, plan subnets of different sizes with VLSM, combine several blocks into one route (supernetting or aggregation), and see how a router chooses between overlapping routes with longest-prefix match. You will use Python's ipaddress module, which does all of this arithmetic for you after the previous lessons showed what it does underneath.

Classless addressing and prefix notation

In CIDR an address block is written as an address followed by the prefix length, such as 203.0.113.0/24 or 10.4.0.0/14. The prefix length says how many leading bits are the network portion, exactly as in subnetting, but now there is no connection to the first bits of the address. A /20 can start at any address whose host bits are all zero, and the mask can sit on any bit boundary, not only at 8, 16 and 24.

Two rules define a valid CIDR block:

  • The block holds 2^(32 - prefix) addresses.
  • Its first address must be a multiple of the block size, which means the host bits of the network address are all zero. 10.1.1.0/23 is invalid because 10.1.1.0 is not aligned to 512 addresses (the third octet must be even); the correct block is 10.1.0.0/23.

A shorter prefix means a bigger block: a /16 contains 256 different /24 blocks. An ISP receives a large block from a regional registry (say a /16) and assigns smaller pieces to customers (a /24 to a small business, a /29 to a tiny office), so one ISP stays one hierarchy level in the routing tables.

flowchart TB
    R["Registry assigns<br/>198.51.0.0/16 to ISP"]
    R --> C1["Customer 1<br/>198.51.0.0/20"]
    R --> C2["Customer 2<br/>198.51.16.0/22"]
    R --> C3["Customer 3<br/>198.51.20.0/24"]
    R --> F["Free<br/>198.51.21.0 onward"]

The ISP hands out right-sized pieces of its block. To the rest of the Internet they all appear as the single route 198.51.0.0/16.

VLSM: subnets of different sizes

The equal-size split from the last lesson fits poorly in practice: a branch office might have 120 computers, a server room 20, a management network 6 and two router links with 2 each. If every subnet were a /25 (126 hosts), most of the space would be wasted.

Variable-Length Subnet Masks (VLSM) means different subnets of the same block have different prefix lengths. The method is a simple greedy algorithm:

  1. List the requirements and add 2 to each (network and broadcast; use exactly 2 for a point-to-point link only if you want a /31).
  2. For each, find the smallest power of two that fits. That gives its prefix length.
  3. Sort the requirements from largest to smallest. Allocating the large ones first keeps every block aligned, with no gaps caused by misalignment.
  4. Allocate them one after another from the start of the parent block, each beginning at the next address that is a multiple of its own size.

For a branch needing 120, 50, 20, 6, 2 and 2 hosts out of 192.168.50.0/24:

Need Addresses needed Block Prefix
120 hosts 122 128 /25
50 hosts 52 64 /26
20 hosts 22 32 /27
6 hosts 8 8 /29
2 hosts 4 4 /30
2 hosts 4 4 /30

Together they use 128 + 64 + 32 + 8 + 4 + 4 = 240 addresses of the 256, leaving a spare block for growth. Equal /25 subnets could not even have held all six.

Python A VLSM planner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

VLSM requires a routing protocol that carries the mask with every route. All modern ones (OSPF, RIPv2, BGP) do. Very old protocols such as RIPv1 did not, which is why they could not support it.

Why fixed-length subnetting wastes addresses

With FLSM (fixed-length subnet masks) every subnet has the same size. If the biggest department needs 50 hosts, every subnet must be a /26 (62 usable hosts), including the point-to-point link between two routers that needs only 2 addresses. VLSM fixes this by giving each subnet the smallest block that fits. The classic recipe is to sort the requirements largest first, allocate each from the next free address, and use the block size (a power of two that holds the hosts plus two) for that subnet.

Benefits of VLSM are minimal address waste, better scalability and the ability to summarize routes by region. Its costs are more complex planning and documentation (a mistake makes subnets overlap), the need for a classless routing protocol (RIPv2, OSPF, EIGRP, BGP), and more work for staff who must be trained to read variable masks.

Python Wasted addresses: FLSM versus VLSM

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fixed plan needs 320 addresses and does not fit in the /24 at all, while VLSM fits everything and leaves room to grow.

Route aggregation (supernetting)

CIDR works in the other direction too. If several networks are contiguous and together form an aligned block, a router can advertise them as one route with a shorter prefix. This is route aggregation, also called supernetting or route summarization. It is what keeps the global routing table at about a million entries rather than many millions.

Take four consecutive class-C-sized networks:

Text
192.168.0.0/24    11000000.10101000.00000000.xxxxxxxx
192.168.1.0/24    11000000.10101000.00000001.xxxxxxxx
192.168.2.0/24    11000000.10101000.00000010.xxxxxxxx
192.168.3.0/24    11000000.10101000.00000011.xxxxxxxx
                  ^^^^^^^^ ^^^^^^^^ ^^^^^^------ the first 22 bits are common

The common prefix is 22 bits, so they summarize to 192.168.0.0/22. The method: write the networks in binary, count the leading bits they all share, and the aggregate is the first network with that prefix length. Aggregation only works when the set of networks exactly fills a block that is correctly aligned. 192.168.1.0/24 through 192.168.4.0/24 cannot be merged into a single route, because they straddle a boundary; you would need 192.168.1.0/24, 192.168.2.0/23 and 192.168.4.0/24, or a larger summary that also covers addresses that you do not own.

Why aggregation matters

An ISP that owns 198.51.0.0/16 announces just that one prefix to the world, even though it has hundreds of customers inside. Routers elsewhere need only one entry. Inside the ISP, more specific routes direct traffic to each customer. The catch is that customers who change ISP take their addresses with them only if the new ISP announces their prefix separately, which adds to the table. This is why provider-assigned addresses aggregate well but are painful to change, and why BGP operators fight over table size.

Python Aggregate and summarize routes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Longest-prefix match

Once routes overlap (a /16 aggregate plus a /24 inside it), a destination address can match several table entries. The rule that makes this work is longest-prefix match: among all entries whose prefix contains the destination address, the router picks the one with the longest prefix, that is, the most specific route.

Suppose a router's table is:

Prefix Next hop
0.0.0.0/0 ISP (default route)
10.0.0.0/8 Core router
10.1.0.0/16 Router B
10.1.2.0/24 Router C

The destination 10.1.2.9 matches all four entries, but /24 is the longest, so it goes to Router C. The destination 10.1.9.9 matches /0, /8 and /16, so Router B gets it. 10.200.0.1 matches /0 and /8 and goes to the core. 8.8.8.8 matches only the default route.

This is what lets an ISP announce an aggregate and still carve out exceptions, and why a hijacker announcing a more specific prefix attracts traffic away from the legitimate owner (see BGP). Real routers use special data structures, tries and hardware TCAM memory, to do this lookup in nanoseconds; the next chapter shows the forwarding process in full.

Python Longest-prefix match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

CIDR and the address registries

CIDR also shaped how addresses are allocated: the five regional registries give ISPs aligned blocks, and ISPs divide them for customers. Prefix lengths seen in the wild are typically /24 (the smallest block commonly accepted in global BGP), /22 to /16 for medium organizations, and /8 for historic holders. Home connections usually get a single public address. Because IPv4 space is exhausted, a block is now often obtained on a transfer market, and the shortage is why NAT and IPv6 matter.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is 10.1.1.0/23 not a valid CIDR network address?

    A /23 covers 512 addresses, so its network address must be a multiple of 512. The correct block containing 10.1.1.0 is 10.1.0.0/23.
  2. 2. In VLSM, why should you allocate the largest subnets first?

    Blocks must start at a multiple of their own size. Starting with the biggest means each later, smaller block lands on a valid boundary right after the previous one.
  3. 3. Which single route summarizes 172.16.8.0/24, 172.16.9.0/24, 172.16.10.0/24 and 172.16.11.0/24?

    Four consecutive /24 networks make 1,024 addresses, a /22. The third octets 8 to 11 share their first six bits (000010xx), and 8 is a multiple of 4, so the block is aligned.
  4. 4. A router has routes for 10.0.0.0/8 and 10.5.0.0/16. Where does a packet to 10.5.1.1 go?

    Longest-prefix match picks the most specific entry that contains the destination.
  5. 5. What is the main benefit of route aggregation for the Internet?

    Many contiguous networks appear as one prefix to the outside world, which keeps routing tables and update traffic small.

Summary

  • CIDR replaced classes with prefixes of any length written as address/length. A block must start at an address aligned to its size.
  • VLSM gives subnets of one block different prefix lengths, allocated largest first so everything stays aligned and little space is wasted.
  • Aggregation (supernetting) merges contiguous, aligned networks into one shorter-prefix route, keeping routing tables small. Misaligned sets cannot be merged exactly.
  • When routes overlap, routers pick the matching entry with the longest prefix. Specific routes override aggregates, and the default route 0.0.0.0/0 matches everything but loses to anything else.
  • Python's ipaddress module provides subnets, supernet, collapse_addresses and membership tests for planning.

Next, look inside the packets that carry all these addresses: the IPv4 packet and fragmentation.