Network, broadcast and usable hosts

Where the minus two in every formula comes from, and the two cases where it is wrong.

Every block reserves its first and last address

Take any subnet. The lowest address in it is the network address, which names the block itself rather than any machine in it. The highest is the broadcast address, which reaches every host in the block at once.

Neither can be assigned to a device, which is the entire origin of the minus two that follows every host calculation around.

192.168.1.0/24

192.168.1.0     network address, names the block
192.168.1.1     first usable, conventionally the router
    ...         254 usable addresses
192.168.1.254   last usable
192.168.1.255   broadcast, reaches everyone

Why they exist

The network address

In binary it is the block with every host bit set to zero. That makes it the natural name for the block, and it is what appears in routing tables: a router does not hold an entry for each host, it holds one for 192.168.1.0/24 and forwards everything in that range the same way. It is a label, not a destination.

The broadcast address

Every host bit set to one. A packet sent there is delivered to every host on the segment, which is how a device that has no address yet can ask for one. A DHCP request has to be a broadcast, because the device cannot address a server it has not met.

IPv6 removed broadcast entirely and uses multicast instead, which delivers to a subscribed group rather than to everyone. It is the same capability with less noise, and it is why IPv6 subnets do not lose an address to it.

Finding them for any block

Three steps, and it works for every prefix.

  1. Block size = 2(32 - prefix), or 256 minus the interesting octet of the mask.
  2. Network address = the largest multiple of the block size at or below your address.
  3. Broadcast = the network address plus the block size, minus one.

Worked, for 172.16.20.100/27:

prefix /27      host bits 5      block size 32

boundaries in the last octet:  0, 32, 64, 96, 128, 160, 192, 224
100 falls between 96 and 127

network      172.16.20.96
first usable 172.16.20.97
last usable  172.16.20.126
broadcast    172.16.20.127
usable count 30

The two cases where minus two is wrong

/31, where both addresses are usable

A /31 holds two addresses, so the usual rule would leave zero usable and make the prefix pointless. RFC 3021 defines an exception: on a point-to-point link there are exactly two possible destinations, so no broadcast is needed and no network address is needed to disambiguate. Both addresses are usable.

This is standard on modern router-to-router links, and it halves the waste of the older /30. It is also the case most subnet calculators get wrong, so it is worth being able to spot when a tool tells you a /31 has no usable hosts.

/32, a single host

One address and no block around it. There is nothing to name and nobody to broadcast to. You see these on loopback interfaces, as single-destination routes, and throughout firewall rules where 203.0.113.42/32 means exactly that one address.

And a historical one

Very old equipment sometimes refused to use the all-zeros and all-ones subnets within a classful network, which cost you two whole subnets rather than two addresses. That restriction has been gone from mainstream equipment for decades, but it survives in old study material and occasionally confuses people comparing answers.

Sizing a subnet for a real requirement

The direction people actually need is backwards from the formula: given a number of machines, which prefix?

  1. Take the host count you need.
  2. Add two, for the network and broadcast addresses.
  3. Round up to the next power of two.
  4. That is the block size, and the prefix is 32 minus its exponent.
You needPlus twoRound up toPrefixActually usable
10 hosts1216/2814
25 hosts2732/2730
30 hosts3232/2730
31 hosts3364/2662
100 hosts102128/25126

The 30 and 31 rows are the trap. Thirty hosts fits a /27 exactly, and thirty-one does not, so it costs you a block twice the size. Growth is worth a thought at that boundary, because the next step up is never small.

Where to swim next