Skip to content
Buy me a coffee

CIDR and subnets

tag: networking

An IPv4 address is four 8-bit octets: 32 bits in all (see IP addresses). The prefix number says how many of those bits are locked, counting from the left.

In 10.34.0.0/16 the first 16 bits are fixed. The remaining 32 − 16 = 16 bits decide which addresses are in the block.

10.34.0.0/16, bit by bit

  1. Base IP: the first address in the block.
  2. Prefix: how many bits are fixed. The same thing has two common spellings:
    • slash notation: /24 (Linux ip addr, cloud consoles, IOC lists)
    • dotted mask: 255.255.255.0 (Windows ipconfig calls it “Subnet Mask”)

Two special cases:

x.x.x.x/32 one single address
0.0.0.0/0 every IPv4 address

Example: 192.134.0.0/29

  1. Free bits: 32 − 29 = 3.
  2. Addresses: 2³ = 8.
  3. Range: 192.134.0.0 – 192.134.0.7.
  4. In a normal subnet the first (.0) is the network address and the last (.7) the broadcast address, so 6 are usable by devices.

Every extra prefix bit halves the block

Range What it is
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 Private: never routed on the internet (Public and private addresses)
127.0.0.0/8 Loopback, this machine (127.0.0.1 and localhost)
169.254.0.0/16 Link-local: a Windows machine that didn’t get an address
100.64.0.0/10 Carrier-grade NAT
  • IOC lists and blocklists use CIDR: an attacker’s whole /24 instead of 256 single addresses.
  • “Is this IP in that range?” in Python:
import ipaddress
net = ipaddress.ip_network("192.134.0.0/29")
print(net.num_addresses, net[0], net[-1]) # 8 192.134.0.0 192.134.0.7
print(ipaddress.ip_address("192.134.0.5") in net) # True
How many addresses are in a /27?
  • 2^(32 − 27) = 2⁵ = 32.
Is `10.34.200.7` inside `10.34.0.0/16`?
  • Yes. Only the first two octets are fixed.
What's /20 as a dotted mask?
  • 255.255.240.0 (20 ones: 8 + 8 + 4).
What's the smallest block that holds 192.168.1.0 to 192.168.1.63?
  • 64 addresses = 2⁶, so /26.

Ports: one address, many conversations at once.

One Reverse Look

"A little learning is a dangerous thing." ~ A. Pope

One Reverse Look · Made with ❤ · Your friendly knowledge seeker