Sub topic 2
Addresses, prefixes, and CIDR notation
This chapter assumes you know what an IP address and a subnet are, and that you can read binary and decimal octets. You should also be comfortable running Python and Linux commands in a terminal. Later chapters build the routing table and longest prefix match on the ideas here. A routing table is a list of prefixes, so you need to read a prefix exactly before you can read a table.
Why routers care about prefixes
A router never keeps one entry per destination host. The Internet has billions of hosts, and no forwarding table could hold them all. A router keeps entries for groups of addresses that share the same leading bits and the same way out. Each group is a prefix.
A prefix is written as an address, a slash, and a number: 10.1.0.0/16. The number is the prefix length. It says how many leading bits are fixed. The remaining bits are free to vary, and they identify hosts inside the group.
IPv4 addresses as 32-bit numbers
An IPv4 address is a 32-bit number. Dotted decimal notation writes it as four octets of 8 bits each. 10.1.2.3 is this bit string:
00001010 00000001 00000010 00000011The prefix 10.1.0.0/16 fixes the first 16 bits: 00001010 00000001. Any address that starts with those 16 bits belongs to the prefix. That covers 10.1.0.0 through 10.1.255.255.
A router decides whether an address belongs to a prefix by comparing only the first N bits. It does not compare the whole address.
Netmasks and the three special addresses
The same information can be written as a netmask: N ones followed by zeros. A /16 is 255.255.0.0. A /26 is 255.255.255.192. Routers and operating systems accept either form. CIDR (Classless Inter-Domain Routing) notation, the slash form, is the standard way to write a prefix.
For any subnet, three values are worth knowing:
- The network address has all host bits set to 0. It names the prefix itself.
- The broadcast address has all host bits set to 1. On a traditional subnet, it reaches every host on the link.
- The usable host range lies between the two.
The following table shows how size grows as the prefix gets shorter. The "Total addresses" column is 2 raised to (32 minus the prefix length).
| Prefix | Netmask | Total addresses | Typical use |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | Very large allocation |
| /16 | 255.255.0.0 | 65,536 | Large site |
| /24 | 255.255.255.0 | 256 | Typical LAN |
| /30 | 255.255.255.252 | 4 | Older point-to-point link |
| /31 | 255.255.255.254 | 2 | Point-to-point link (RFC 3021) |
| /32 | 255.255.255.255 | 1 | A single host |
A /24 has 254 usable hosts, because the network and broadcast addresses are reserved. A /31 is a special case. It has no network or broadcast address, and both addresses are usable on a link between two routers. A /32 in a routing table matches exactly one address, and routers use it for host routes.
Computing a prefix with Python
The standard library's ipaddress module does this arithmetic. An interface address such as 192.0.2.130/26 combines a host address with the length of the subnet it sits on.
import ipaddress
iface = ipaddress.ip_interface("192.0.2.130/26")
net = iface.network
print(iface.ip) # 192.0.2.130
print(net) # 192.0.2.128/26
print(net.netmask) # 255.255.255.192
print(net.network_address) # 192.0.2.128
print(net.broadcast_address) # 192.0.2.191
print(net.num_addresses) # 64A /26 leaves 6 host bits, so each subnet holds 64 addresses. The blocks in the last octet start at 0, 64, 128 and 192. The address .130 falls in the block that starts at 128.
The prefix test a router performs
The test is a bit comparison. Shift away the host bits, then check that what remains is equal. This sketch works for IPv4 only.
import ipaddress
def matches(addr, prefix):
net = ipaddress.ip_network(prefix)
shift = 32 - net.prefixlen
a = int(ipaddress.ip_address(addr))
n = int(net.network_address)
return (a >> shift) == (n >> shift)
print(matches("10.1.2.3", "10.1.0.0/16")) # True
print(matches("10.2.2.3", "10.1.0.0/16")) # False
print(matches("8.8.8.8", "0.0.0.0/0")) # TrueThe prefix 0.0.0.0/0 has length 0, so the shift is 32 and every address matches. This is the property that makes a default route work. Hardware routers do the same comparison with ternary lookup memory or tries, but the logic is the same.
From classes to CIDR
Early IPv4 divided the address space into fixed classes. Class A used a /8, class B a /16, and class C a /24. Allocations were wasteful. An organization with 300 hosts did not fit in a class C, so it received a class B with 65,536 addresses. Routing tables also grew too fast.
CIDR, introduced in the 1990s, removed the fixed boundaries. A prefix can have any length from 0 to 32. This allows two things:
- Allocations match real need, such as a
/22for roughly 1,000 hosts. - Adjacent blocks can be aggregated into one shorter prefix. Aggregation is also called route summarization.
Four adjacent /24 blocks, 10.1.0.0/24 through 10.1.3.0/24, can be announced as the single prefix 10.1.0.0/22. A router upstream then needs one entry instead of four.
The nesting shown above is what longest prefix match uses. Address 10.1.2.3 matches all four prefixes, and the longest one is the most specific. That is the next chapter's topic.
Aggregation and the host bits rule
A prefix must have all its host bits set to 0. The address 10.1.2.3/16 is not a valid prefix, because bits 17 to 32 are not zero. Linux rejects it when you add a route, and the ipaddress module rejects it by default:
import ipaddress
try:
ipaddress.ip_network("10.1.2.3/16")
except ValueError as err:
print(err) # 10.1.2.3/16 has host bits set
print(ipaddress.ip_network("10.1.2.3/16", strict=False)) # 10.1.0.0/16
blocks = [ipaddress.ip_network(f"10.1.{i}.0/24") for i in range(4)]
print(list(ipaddress.collapse_addresses(blocks))) # [IPv4Network('10.1.0.0/22')]Aggregation works only when the blocks are adjacent and the result is aligned on a boundary. 10.1.1.0/24 and 10.1.2.0/24 are neighbors, but they cannot merge into a /23. A /23 must start at an even third octet, such as 10.1.0.0/23 or 10.1.2.0/23.
IPv6 prefixes
IPv6 uses the same idea with 128-bit addresses. An address such as 2001:db8:1:2::1/64 has a 64-bit prefix, 2001:db8:1:2::/64. Conventions differ from IPv4 in three ways:
- A subnet on a LAN is almost always a
/64, because features such as stateless address autoconfiguration (SLAAC) assume 64 host bits. - There is no broadcast address. Multicast is used instead.
- Sites commonly receive a
/48or/56, which gives them many/64subnets.
Leading-bit comparison and longest prefix match work identically. Only the width changes.
Common errors
- Off-by-one on the boundary.
10.1.0.0/16ends at10.1.255.255, not10.1.255.0. The final address is the broadcast address of the subnet. - Counting hosts as 2^n. Usable hosts on a traditional subnet are 2^n minus 2, where n is the number of host bits.
- Wrong length on a host route. Use
/32for one host. A/24capturing a single machine's traffic also captures 253 other addresses. - Mixed-up masks.
255.255.255.192is/26, not/25. Count the ones: 24 from the first three octets plus 2 from192(binary11000000). - Overlapping prefixes assumed to conflict. Overlap is legal and common, such as a
/16and a/24inside it. The router uses the more specific one.
Exercise
A host has the interface address 172.16.45.200/20. What are the netmask, the network address, and the broadcast address of its subnet? How many usable hosts does the subnet have?
Show answer
A /20 leaves 12 host bits. The mask is 255.255.240.0, because the third octet has 4 mask bits set (11110000 = 240). The third-octet block size is 16, so 45 falls in the block 32 to 47. The network address is 172.16.32.0 and the broadcast address is 172.16.47.255. The subnet has 2^12 = 4,096 addresses, so 4,094 are usable.
Exercise
Can 192.168.4.0/24 and 192.168.5.0/24 be aggregated into one prefix? Can 192.168.5.0/24 and 192.168.6.0/24?
Show answer
The first pair can. Together they form 192.168.4.0/23, because 4 is even and the two blocks are adjacent. The second pair cannot. A /23 must start at an even third octet, so 192.168.5.0/23 is not a valid prefix, and the two blocks stay separate.