Sub topic 2
Addresses, prefixes, and CIDR notation
This chapter assumes you know what an IPv4 address looks like, what a network, host, and link are, and how to run commands in a terminal. It explains how a router reads an address as a network part and a host part. Every routing decision in the later chapters depends on this split.
Why routers care about prefixes
A router does not keep a route for every host on the Internet. It keeps routes for groups of addresses. A group is written as a prefix: the leading bits that all addresses in the group share. The remaining bits identify a host inside that group.
An IPv4 address is 32 bits, usually written as four decimal bytes such as 10.1.2.3. A prefix length says how many leading bits are the network part.
CIDR notation
CIDR (Classless Inter-Domain Routing) writes a prefix as an address, a slash, and the prefix length. For example, 10.1.0.0/16 means the first 16 bits are fixed, and the last 16 bits can vary.
The old classful rules (class A, B, C) fixed the split by the first bits of the address. CIDR allows any length from /0 to /32, so allocations can match real needs and routes can be summarized.
| Prefix | Network bits | Host bits | Addresses in block |
|---|---|---|---|
| `10.0.0.0/8` | 8 | 24 | 16,777,216 |
| `10.1.0.0/16` | 16 | 16 | 65,536 |
| `10.1.2.0/24` | 24 | 8 | 256 |
| `10.1.2.4/30` | 30 | 2 | 4 |
| `0.0.0.0/0` | 0 | 32 | every address |
The block size is 2 raised to the number of host bits.
The netmask
The same split can be written as a netmask: 32 bits with ones for the network part and zeros for the host part. A /24 is 255.255.255.0. A /20 is 255.255.240.0. A router applies the mask with a bitwise AND to find the network an address belongs to.
import ipaddress
addr = ipaddress.ip_address("10.1.2.3")
net = ipaddress.ip_network("10.1.0.0/16")
print(addr in net) # True
print(net.netmask) # 255.255.0.0
print(net.num_addresses) # 65536
print(net.network_address) # 10.1.0.0
print(net.broadcast_address) # 10.1.255.255Prefixes nest
A longer prefix describes a smaller block that sits inside a shorter one. 10.1.0.0/16 lies entirely inside 10.0.0.0/8. The address 10.1.2.3 therefore belongs to both, and a routing table can hold both at once. The next chapters show how a router chooses between them.
import ipaddress
big = ipaddress.ip_network("10.0.0.0/8")
small = ipaddress.ip_network("10.1.0.0/16")
print(small.subnet_of(big)) # True
print(list(small.subnets(new_prefix=18))) # four /18 networksSummarizing routes
CIDR lets a router replace several adjacent routes with one. 10.2.0.0/24 and 10.2.1.0/24 differ only in the last network bit, so together they equal 10.2.0.0/23. This aggregation keeps routing tables small. It works only when the blocks are adjacent and aligned on the boundary of the larger block.
import ipaddress
nets = [ipaddress.ip_network("10.2.0.0/24"),
ipaddress.ip_network("10.2.1.0/24")]
print(list(ipaddress.collapse_addresses(nets))) # [IPv4Network('10.2.0.0/23')]Common errors
- Writing a host address with a prefix, such as
10.1.2.3/24. This is valid for an interface, where it means "this host, on that network". As a route, it needs the host bits set to zero, soip_networkraises an error unless you passstrict=False. - Misreading
/25or/26as byte boundaries. Only/8,/16, and/24fall on a dot. - Forgetting that the first and last addresses of an ordinary subnet are the network and broadcast addresses, and are not given to hosts.
Exercise
Which prefix contains 10.1.2.3: 10.1.2.0/25 or 10.1.2.128/25? How many addresses are in each?
Show answer
10.1.2.0/25 contains it. It covers 10.1.2.0 to 10.1.2.127, and the last byte 3 falls in that range. Each /25 has 7 host bits, so 2^7 = 128 addresses.
Exercise
Can 192.168.4.0/24 and 192.168.5.0/24 be summarized as a single prefix? What about 192.168.5.0/24 and 192.168.6.0/24?
Show answer
The first pair can: it becomes 192.168.4.0/23. The second pair cannot, because 192.168.5.0 is not aligned on a /23 boundary (5 is odd), so the two blocks do not form one /23.