DNSVerdict

CIDR and subnetting explained, IPv4 and IPv6

Published · Updated

CIDR notation writes a network as an address, a slash and the number of leading bits that are fixed: 192.0.2.0/24 fixes 24 of 32 bits, leaving 8 bits and so 256 addresses. Subtract the prefix from 32 (IPv4) or 128 (IPv6) and raise 2 to that power to get the size. Check the arithmetic with the subnet calculator.

DNSVerdict’s Subnet Calculator takes an IPv4 or IPv6 prefix in your browser and shows the network address, range, mask and address count, with no network request.

Reading a prefix

RFC 4632 §3.1 defines the notation: an address, then ”/”, then a decimal number between 0 and 32 that gives the count of significant bits, the network part. The rest are host bits. A longer prefix means a smaller network.

PrefixMaskAddressesUsable hosts (traditional)
/24255.255.255.0256254
/26255.255.255.1926462
/28255.255.255.2401614
/30255.255.255.25242
/31255.255.255.25422 (point-to-point, RFC 3021)
/32255.255.255.25511 (a single host)

Computing network, broadcast and hosts

Take 192.0.2.100/26. The prefix leaves 32 - 26 = 6 host bits, so the block holds 2^6 = 64 addresses and starts on a multiple of 64 in the last octet.

Address      192.0.2.100
Prefix       /26  (mask 255.255.255.192)
Block size   64
Network      192.0.2.64     (lowest address of the block)
Broadcast    192.0.2.127    (highest address of the block)
Hosts        192.0.2.65 - 192.0.2.126   (62 addresses)

The method: find the block size (256 minus the last non-255 mask octet, here 256 - 192 = 64), round the address down to a multiple of it for the network address, and add the size minus one for the last address. In the traditional model the first address names the network and the last is the broadcast, so usable hosts are 2^n - 2. A route or firewall rule for 192.0.2.64/26 still covers all 64 addresses.

/31 and /32

A /31 has one host bit, so the “network” and “broadcast” addresses of the traditional model would use up both addresses. RFC 3021 §2.1 says that on a point-to-point link with a 31-bit mask the two addresses “MUST be interpreted as host addresses”, and §2.2 adds that the limited broadcast address is then used for broadcast traffic. It halves the address space spent on router-to-router links. Whether a device supports /31 depends on the vendor, so check the documentation before you configure one.

A /32 is exactly one address. You see it in routing tables for a loopback or a service address, and as the default meaning of an SPF ip4: mechanism without a prefix (see below).

Private ranges and CGNAT

RFC 1918 §3 reserves three blocks for private networks:

BlockRangeAddresses
10.0.0.0/810.0.0.0 - 10.255.255.25516,777,216
172.16.0.0/12172.16.0.0 - 172.31.255.2551,048,576
192.168.0.0/16192.168.0.0 - 192.168.255.25565,536

RFC 6598 adds a fourth block that is not RFC 1918 space: the Shared Address Space 100.64.0.0/10 (§7), reserved for carrier-grade NAT (CGN) deployment. Per RFC 6598 it is not globally routable, and it is for use between a provider’s CGN device and customer equipment. If the address your router reports as its WAN IP is in 100.64.0.0/10, your provider is doing CGNAT, and the address that websites see is a different, shared one. Compare both with What is my IP. The practical consequence is that inbound connections, port forwarding and hosting a mail server from that connection usually will not work.

IPv6 prefix sizes

IPv6 uses the same notation with 128 bits. The conventions are:

A /64 has 2^64 addresses, so you do not count hosts; you plan subnets. The IPv6 tools expand, compress and inspect IPv6 addresses.

Why SPF uses CIDR

SPF lists the addresses allowed to send mail for a domain, and a sender rarely has one address. RFC 7208 §5.6 defines ip4:<network>[/<prefix>] and ip6:<network>[/<prefix>]. If the prefix is omitted, it is “taken to be /32” for ip4 and “/128” for ip6. The mechanism matches when the high-order prefix bits of the sending IP equal those of the network. The RFC does not allow shortened addresses: ip4:192.0.2 is invalid and must be written ip4:192.0.2.0/24.

v=spf1 ip4:192.0.2.64/26 ip4:203.0.113.5 ip6:2001:db8:1::/48 -all

This record authorises 64 addresses, one single address and an IPv6 /48. Two points follow from the RFC:

  1. Wider is not safer. Everyone in the prefix can send as you. Use the narrowest prefix that covers your servers. A /24 you do not control entirely lets other tenants of that range pass SPF.
  2. ip4 and ip6 cost no DNS lookups. RFC 7208 §4.6.4 says they cause no DNS queries during evaluation, so “their use is not subject to” the limit of 10. When a record is near the limit, replacing an include: with the provider’s published ranges can help, at the price of maintaining them yourself; too many lookups covers the trade-off.

Check a record with the SPF check, and see setting up SPF for the whole record. If a mail server must use a specific IP, find the outbound address first, for instance with What is my IP from a browser on that network, or from the Received headers of a message it sent.

FAQ

How many usable hosts are in a /24? A /24 has 8 host bits, so 2^8 = 256 addresses. In the traditional model the first is the network address and the last the broadcast, which leaves 254 usable hosts. Routing and firewall rules still match all 256 addresses, so a rule for 192.0.2.0/24 covers the network and broadcast addresses too.

Can I use a /31 for a link between two routers? Often yes. RFC 3021 §2.1 allows two-address /31 subnets on point-to-point links, with both addresses treated as hosts. It saves half the addresses compared with a /30. Not every device or operating system supports it, so check the vendor’s documentation, and use a /30 where support is missing.

Why is the IPv6 subnet always a /64? RFC 7421 documents the 64-bit boundary between the subnet prefix and the interface identifier. The IETF consensus is that keeping it fixed outweighs the arguments for change. The RFC analyses what would break if the boundary became variable, so a LAN prefix other than /64 is a departure from the norm that you have to justify.

What prefix should I put in SPF for my mail server? Use the narrowest prefix that covers the servers that really send: a single address as ip4:203.0.113.5 (an omitted prefix means /32, RFC 7208 §5.6), or a range you own entirely. A wide prefix lets every host in it pass SPF. The ip4 and ip6 mechanisms do not count toward the 10-lookup limit.

Sources

Related: SPF too many lookups, reverse DNS for mail servers, IP blacklist removal.

Tools used

Subnet & CIDR Calculator (IPv4 and IPv6)IPv6 Address Expand, Compress and Reverse Name ToolSPF Record CheckerWhat Is My IP Address?