Sub topic 11
The three RFC 1918 private ranges
This chapter assumes you can read dotted-decimal addresses, know that a prefix length such as /12 counts the network bits, and can work out the first and last address of a subnet. It covers the three blocks reserved for private use, how to test an address against them, and the errors people commonly make.
What RFC 1918 reserves
RFC 1918 sets aside three blocks of IPv4 space for private networks. Routers on the public internet do not route these addresses. Any organization can use them inside its own network without asking a registry, and many organizations reuse the same blocks at the same time.
| Block | Range | Prefix | Addresses | Classful equivalent |
|---|---|---|---|---|
| 10.0.0.0/8 | 10.0.0.0 to 10.255.255.255 | /8 | 16,777,216 | One class A network |
| 172.16.0.0/12 | 172.16.0.0 to 172.31.255.255 | /12 | 1,048,576 | 16 class B networks |
| 192.168.0.0/16 | 192.168.0.0 to 192.168.255.255 | /16 | 65,536 | 256 class C networks |
The classful column explains the odd shape of the middle block. The old class B space is 128.0.0.0 to 191.255.255.255, and RFC 1918 took 16 consecutive class B networks from it, 172.16 through 172.31. Sixteen blocks need 4 bits of the second octet, so the prefix is /12 and not /16.
Why 172.16.0.0/12 ends at 172.31
A /12 prefix fixes the first 12 bits. The first octet (172) uses 8 bits. The remaining 4 bits come from the second octet, whose binary form starts with 0001. The last 4 bits of that octet are free to vary:
00010000is 1600011111is 31
The second octet therefore runs from 16 to 31. The value 32 is 00100000, which differs in the fixed bits, so 172.32.0.1 is a public address.
Testing an address in code
The check is a membership test against the three networks. The ipaddress module does the bit comparison for you.
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)
print(is_rfc1918("10.20.30.40")) # True
print(is_rfc1918("172.31.255.254")) # True
print(is_rfc1918("172.32.0.1")) # False
print(is_rfc1918("8.8.8.8")) # FalseBe careful with the built-in is_private property. It is broader than RFC 1918, because it also returns true for loopback, link-local, and other special-purpose ranges.
import ipaddress
print(ipaddress.ip_address("192.168.1.10").is_private) # True
print(ipaddress.ip_address("127.0.0.1").is_private) # True
print(ipaddress.ip_address("8.8.8.8").is_private) # FalseHere 127.0.0.1 counts as private even though RFC 1918 does not list it. Use an explicit list such as the one above when you need exactly the three RFC 1918 blocks.
Common errors
- Wrong boundary on 172.x. Treating all of 172.0.0.0/8 as private blocks legitimate public hosts or lets in traffic you meant to filter.
- Confusing 192.168/16 with 192.168/24. Home routers usually hand out one /24 from it, but the whole /16 is reserved.
- Overlapping networks. Two sites that both use 10.0.0.0/8 cannot be joined by a VPN or peering link without renumbering or translation, because each side believes the other's addresses are local. Plan address allocation across sites before you build.
- Assuming private means secure. A private address only means the address is not routed on the public internet. It is not a firewall.
Exercise
Is 172.20.14.9 an RFC 1918 address, and which block does it belong to? What about 172.40.1.1?
Show answer
172.20.14.9 is private. Its second octet, 20, is between 16 and 31, so it falls in 172.16.0.0/12. 172.40.1.1 is public, because 40 is outside the 16 to 31 range.
Exercise
Two offices both use 192.168.1.0/24 and you want to connect them with a VPN. What goes wrong, and what is the fix?
Show answer
Both sides treat 192.168.1.0/24 as their local network, so a host cannot tell whether a destination is local or across the VPN, and traffic is not routed correctly. The fix is to renumber one office to a non-overlapping range, or to translate addresses at the VPN boundary.