Sub topic 13
Pitfalls: Range Boundaries and Overlapping Networks
What this chapter assumes
This chapter assumes you already know four things from earlier chapters:
- how to read a CIDR prefix such as
/12 - how a mask splits an address into network and host bits
- how to compute a network's first and last address
- the three RFC 1918 ranges:
10.0.0.0/8,172.16.0.0/12and192.168.0.0/16
The chapter covers the mistakes people most often make with those ranges. Some are boundary mistakes, where an address is wrongly judged to be inside or outside a range. The others are overlap mistakes, where two networks claim the same addresses.
Pitfall 1: The edges of the private ranges
Only 10.0.0.0/8 is easy to remember, because its prefix covers exactly one octet. The other two ranges are where people slip.
172.16.0.0/12 fixes 12 bits. That is the whole first octet (172) plus the top 4 bits of the second octet. Those four bits must be 0001. The second octet therefore runs from 00010000 (16) to 00011111 (31).
| Address | In RFC 1918? | Reason |
|---|---|---|
| 10.255.255.255 | Yes | Last address of 10.0.0.0/8 |
| 11.0.0.0 | No | First octet is not 10 |
| 172.15.255.255 | No | Second octet 15 is `00001111` |
| 172.16.0.0 | Yes | First address of 172.16.0.0/12 |
| 172.31.255.255 | Yes | Last address of 172.16.0.0/12 |
| 172.32.0.0 | No | Second octet 32 is `00100000` |
| 192.167.255.255 | No | Second octet is not 168 |
| 192.168.0.0 | Yes | First address of 192.168.0.0/16 |
| 192.169.0.0 | No | Second octet is not 168 |
Run the boundary cases through the standard library instead of reasoning by eye:
import ipaddress
RFC1918 = [
ipaddress.ip_network("10.0.0.0/8"),
ipaddress.ip_network("172.16.0.0/12"),
ipaddress.ip_network("192.168.0.0/16"),
]
def is_rfc1918(text):
address = ipaddress.ip_address(text)
return any(address in network for network in RFC1918)
for text in ["10.255.255.255", "11.0.0.0", "172.15.255.255",
"172.16.0.0", "172.31.255.255", "172.32.0.0",
"192.167.255.255", "192.168.0.0", "192.169.0.0"]:
print(f"{text:16} {is_rfc1918(text)}")The output matches the table: True for 10.255.255.255, 172.16.0.0, 172.31.255.255 and 192.168.0.0, and False for the rest.
String matching gives wrong answers
A frequent bug is to test addresses as text. This function looks reasonable and is wrong:
def looks_private_wrong(text):
return text.startswith(("10.", "172.", "192.168."))
print(looks_private_wrong("172.32.0.1")) # True, but 172.32.0.1 is public
print(looks_private_wrong("172.217.4.1")) # True, but this is a public addressThe check treats every 172.x.x.x address as private. Only 172.16.0.0 through 172.31.255.255 is private, which is 1 of the 256 possible second-octet blocks in 16. Firewall rules, allow-lists and log filters written with a wildcard like 172.* have the same flaw. Always compare addresses as numbers, either with a prefix test or with the ipaddress module.
Writing the wrong mask
The mask for /12 is 255.240.0.0. Writing 172.16.0.0/255.255.0.0 (a /16) covers only 172.16.0.0 to 172.16.255.255. That is one sixteenth of the real block. Hosts on 172.20.1.5 would then look like they are outside the private range.
Pitfall 2: Blocks that do not start on a boundary
A CIDR block must start at an address whose host bits are all zero. 192.168.1.128/24 is not a valid network, because a /24 must start at .0. The ipaddress module rejects it unless you ask it to be lenient:
import ipaddress
for text in ["192.168.1.0/24", "192.168.1.128/24"]:
try:
net = ipaddress.ip_network(text)
print(text, "is valid:", net)
except ValueError:
print(text, "has host bits set")
print(ipaddress.ip_network("192.168.1.128/24", strict=False)) # 192.168.1.0/24With strict=False the module silently rounds down to 192.168.1.0/24. That is convenient, but it can hide a typo. A route that you meant to be 192.168.1.128/25 would become a full /24 and cover 128 extra addresses.
Pitfall 3: Overlapping networks
Two networks overlap when they share at least one address. CIDR blocks are always either nested (one inside the other) or disjoint, and they never partly overlap. So an overlap means one block contains the other.
The usual causes are:
- Common defaults. Home routers often ship with
192.168.0.0/24or192.168.1.0/24. Many office networks use10.0.0.0/24or10.0.1.0/24. - Docker. The default bridge network is
172.17.0.0/16, which sits inside172.16.0.0/12. A corporate network that uses172.17.x.xcollides with it. - Mergers and partnerships. Two companies that each picked
10.0.0.0/8must now connect. - Cloud peering and VPNs. Virtual networks cannot be peered, or sometimes even routed, when their ranges intersect.
The laptop's routing table holds two routes for the same range. The directly connected home network is normally preferred, so traffic meant for the office server reaches the home printer instead. Nothing reports an error. The connection simply goes to the wrong machine, or times out.
Nested ranges and longest-prefix match
When one block is nested inside another, routers pick the most specific route. This rule is called longest-prefix match. Suppose an office uses 10.0.0.0/16 and a partner uses 10.0.200.0/24. Traffic for 10.0.200.7 goes to the partner. The office hosts that really live at 10.0.200.x become unreachable from anywhere that sees both routes.
Detecting overlaps in code
The overlaps method tests two networks. Checking every pair catches conflicts before you deploy a plan:
import ipaddress
from itertools import combinations
plan = [
ipaddress.ip_network("10.0.0.0/16"),
ipaddress.ip_network("10.0.128.0/17"),
ipaddress.ip_network("10.1.0.0/16"),
ipaddress.ip_network("192.168.1.0/24"),
ipaddress.ip_network("192.168.0.0/23"),
]
conflicts = [(a, b) for a, b in combinations(plan, 2) if a.overlaps(b)]
for a, b in conflicts:
print(f"{a} overlaps {b}")This prints two lines:
10.0.0.0/16 overlaps 10.0.128.0/17
192.168.1.0/24 overlaps 192.168.0.0/23The /23 is the less obvious case. Its mask 255.255.254.0 makes it span 192.168.0.0 to 192.168.1.255, so it contains the /24. Pairwise checking is fine for tens of networks. For thousands, sort by start address and compare neighbours.
Avoiding and fixing overlaps
Prevention is far cheaper than repair.
- Keep an address plan. Record every allocation in one place, and run the overlap check on each change.
- Avoid popular defaults. Choose less common blocks, for example
10.73.0.0/16instead of10.0.0.0/24or192.168.1.0/24. - Allocate with room to grow. Give each site or environment a block aligned to its prefix, such as one
/16per region. - Change Docker's range if it collides with yours. Set
default-address-poolsin the daemon configuration.
If overlap already exists, the options are all more expensive:
- Renumber one side. This is a clean fix, but it means touching every host, DNS record, firewall rule and static route.
- Translate between the networks with NAT, so each side sees the other under a different, non-overlapping range. This works but complicates logging and protocols that embed addresses in their payload.
- Segment the clash. If only a small part overlaps, move those hosts to a fresh subnet.
Checking more than RFC 1918
Some addresses are not in RFC 1918 and still must not be treated as ordinary public addresses. Loopback 127.0.0.0/8, link-local 169.254.0.0/16 and the shared space 100.64.0.0/10 each have their own rules. Overlap checks should include them. A plan that quietly reuses 100.64.0.0/10 inside a network behind a carrier-grade NAT, for instance, creates the same ambiguity described above. The final chapter returns to that range.
Exercise
A colleague writes an allow-list rule 172.0.0.0/8 to match "the private 172 range". Why is this wrong, and what rule is correct?
Show answer
A /8 covers 172.0.0.0 through 172.255.255.255, which is 16 times larger than the private block and includes many public addresses (for example 172.217.x.x). The RFC 1918 block is 172.16.0.0/12, which covers 172.16.0.0 through 172.31.255.255 and has mask 255.240.0.0.
Exercise
Your office uses 10.0.0.0/16. A partner asks you to route to their network 10.0.200.0/24 over a VPN. Do the two overlap? What happens to traffic for 10.0.200.7, and what are two ways to resolve it?
Show answer
Yes. 10.0.200.0/24 lies inside 10.0.0.0/16, so they overlap. Longest-prefix match sends traffic for 10.0.200.7 to the partner, which makes any office hosts in 10.0.200.0/24 unreachable. You can renumber one side, or use NAT to present the partner network to your side under a different, non-overlapping range.