4. IPv4 Addressing & Subnets

Thirty-two bits, one line drawn through them, and everything that follows from where you draw it

Think about a phone number like +46 8 555 0123. Nobody in the phone network reads it all at once. An exchange abroad looks at +46 and thinks "Sweden, send it that way". An exchange in Sweden looks at the 8 and thinks "Stockholm". Only the exchange in Stockholm cares about 555 0123, the actual phone. Each part of the number is for a different audience, and the first part says nothing about which phone you mean, only where to send the call next.

There's one catch: where does the area code end? In a phone system, the digits themselves tell you, because the numbering plan is designed so no area code is the start of another. IPv4 addresses work almost exactly like phone numbers, but they take a different approach to that catch. Every address travels with a second number, the mask, whose only job is to say where the "area code" ends. Get that line in the right place and everything works. Move it by a single bit and, as we'll see, devices on the same desk stop being able to reach each other directly.

In the ARP article, our laptop decided whether to ARP for a destination directly or for the router by ANDing addresses with 255.255.255.0, and we promised to come back to it. This article takes that mask apart bit by bit. We'll use the same home network as before:

Device IP address
Router (default gateway) 192.168.1.1
Laptop 192.168.1.10
Printer 192.168.1.20
NAS 192.168.1.30
Web server, out on the Internet 203.0.113.80

An Address Is Just a 32-bit Number

An IPv4 address is a single unsigned 32-bit integer. That's all. Nothing in the packet says "192" or "168": the IPv4 header in the Layers article carried the laptop's address as four bytes, c0 a8 01 0a. The familiar 192.168.1.10 is dotted-quad notation, a human convenience: write each of the four bytes in decimal, with dots between them.

Three ways to write the laptop's address

Dotted quad   192      .168      .1        .10
Hex           c0        a8        01        0a      = 0xc0a8010a
Binary        11000000  10101000  00000001  00001010
As one number 3,232,235,786

Each byte (networking people say octet) runs from 0 to 255, so a dotted quad like 192.168.1.300 is simply not an address. If converting between bases is rusty, the Binary Integers and Number Bases articles cover it from scratch; here we only need to turn a byte into eight bits and back.

Because it's one number, the address space is finite and easy to count:

232=4,294,967,2962^{32} = 4{,}294{,}967{,}296

About 4.3 billion addresses, from 0.0.0.0 to 255.255.255.255. That sounded inexhaustible in 1981, when RFC 791 defined IPv4. It wasn't, and we'll get to how it ran out.

A fun parsing trap

The classic C function inet_aton, still used by tools like ping on many systems, accepts more than dotted quads. A leading zero means octal and 0x means hex, so ping 010.0.0.1 may ping 8.0.0.1. Short forms are allowed too: 127.1 means 127.0.0.1. Stick to four plain decimal numbers without leading zeros and you'll never meet these surprises.

Network Part and Host Part

Here's the laptop's address with its 32 bits laid out in a row. The /24 after it is the interesting bit:

192.168.1.10/24: one 32-bit number, split in two 192 1 1 0 0 0 0 0 0 168 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 10 0 0 0 0 1 0 1 0 bit 31bit 0 Network part: 24 bits192.168.1 · same for every device on the LAN Host part: 8 bits.10 · this device The /24 says where the line falls: after bit 24, counting from the left.

(Bit-numbering conventions differ: the diagram counts from the right, so the leftmost bit is bit 31, as programmers usually do; networking texts often count from the left instead, starting at 0 or 1. Either way, "/24" means the first 24 bits from the left.)

The first 24 bits identify the network: every device on our LAN starts with the same 24 bits, 192.168.1. The last 8 bits identify the host (the device's interface) within that network. The router, laptop, printer and NAS differ only in those last 8 bits: .1, .10, .20 and .30.

This split is what makes routing possible at all. A router on the Internet doesn't need an entry for every one of the 4.3 billion addresses. It needs an entry for every network, and it can treat everything inside one as a single destination, exactly like the foreign phone exchange that only reads +46. Only the last router, the one actually attached to 192.168.1.0, has to care about the host part. It's the same arrangement the Layers article described from the packet's point of view: the IP destination names the final machine, but each hop only needs to know which direction that machine's network lies in.

Subnet Masks and Prefix Lengths

There are two ways of writing down where the line falls, and you'll see both everywhere.

The subnet mask is a second 32-bit number with a 1 in every network bit and a 0 in every host bit. For our LAN that's 24 ones followed by 8 zeros:

11111111 11111111 11111111 00000000   =   255.255.255.0

The prefix length just counts the ones. 24 ones: /24. Writing 192.168.1.10/24 is called CIDR notation, after the scheme that introduced it (more below). Both say exactly the same thing, and real tools use both. On Linux, ip addr prints the prefix; Windows' ipconfig prints the mask:

$ ip -4 addr show eth0
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
C:\> ipconfig
   IPv4 Address. . . . . . . . . . . : 192.168.1.10
   Subnet Mask . . . . . . . . . . . : 255.255.255.0

(That brd 192.168.1.255 is the broadcast address, which we'll compute in a moment.)

The rule: ones first, then zeros

A mask must be a solid run of ones followed by a solid run of zeros. 255.255.253.0 is not a valid mask, because 253 is 11111101: there's a zero in the middle of the ones. The idea of the mask is "the first n bits are the network", and a mask with holes doesn't describe a first-n-bits anything. (Very early subnetting rules technically allowed non-contiguous masks, but nothing sensible ever used them, and CIDR forbids them.)

That rule means each mask byte can only be one of nine values, the ones you get by filling a byte with ones from the left:

Ones in the byte Bits Value
0 00000000 0
1 10000000 128
2 11000000 192
3 11100000 224
4 11110000 240
5 11111000 248
6 11111100 252
7 11111110 254
8 11111111 255

Memorise this little table and you can convert any mask to a prefix in your head: each 255 is 8, and the one "interesting" byte comes from the table. 255.255.248.0 is 8 + 8 + 5 = /21. Going the other way, /26 is 8 + 8 + 8 + 2, so three 255s and then 192: 255.255.255.192.

Here are the prefixes you'll meet most often:

Prefix Mask Addresses in the block
/8 255.0.0.0 16,777,216
/12 255.240.0.0 1,048,576
/16 255.255.0.0 65,536
/20 255.255.240.0 4,096
/21 255.255.248.0 2,048
/22 255.255.252.0 1,024
/24 255.255.255.0 256
/25 255.255.255.128 128
/26 255.255.255.192 64
/27 255.255.255.224 32
/28 255.255.255.240 16
/29 255.255.255.248 8
/30 255.255.255.252 4
/31 255.255.255.254 2
/32 255.255.255.255 1

Notice the pattern in the last column: each extra bit of prefix halves the block. A /24 has 8 host bits and 2⁸ = 256 addresses; a /25 has 7 and 128.

Wildcard masks

You'll occasionally meet the mask written upside down: zeros for the network bits, ones for the host bits. This is a wildcard mask (sometimes called an inverse mask), and it's what Cisco access lists and OSPF configurations expect. For a /24 it's 0.0.0.255; for a /26, 0.0.0.63. The quick way to get it is to subtract each mask byte from 255. Wildcards turn up again in a moment, because the broadcast address is the network address with every wildcard bit switched on.

Doing the Arithmetic

Everything you might want to know about a subnet falls out of two bitwise operations. Given an address and a mask:

  • Network address = address AND mask. The AND keeps the network bits and forces every host bit to 0.
  • Broadcast address = network OR (NOT mask). The OR forces every host bit to 1.
  • Usable hosts are everything in between: the first is network + 1, the last is broadcast − 1.

Why are the two ends excluded? By long-standing convention the all-zeros host part names the network itself and the all-ones host part means "every host on this network", the directed broadcast. Neither can be assigned to a device, so a block with h host bits holds:

usable hosts=2h−2\text{usable hosts} = 2^h - 2

Worked example 1: 192.168.1.10/24

The easy case first, where the line falls between bytes. I'll colour  network bits  and  host bits :

192.168.1.10/24

address    11000000 10101000 00000001 00001010   192.168.1.10
mask       11111111 11111111 11111111 00000000   255.255.255.0
           ----------------------------------- AND
network    11000000 10101000 00000001 00000000   192.168.1.0
wildcard   00000000 00000000 00000000 11111111   0.0.0.255
           ----------------------------------- network OR wildcard
broadcast  11000000 10101000 00000001 11111111   192.168.1.255
  • Network: 192.168.1.0
  • Broadcast: 192.168.1.255
  • First usable: 192.168.1.1 (our router)
  • Last usable: 192.168.1.254
  • Usable hosts: 2⁸ − 2 = 254

With a byte-aligned mask you barely need the binary: 255 bytes copy through, 0 bytes become 0 (for the network) or 255 (for the broadcast). That's the shortcut the ARP article used. It stops working the moment the line falls inside a byte.

Worked example 2: 192.168.1.130/26

A /26 puts the line two bits into the last byte. The first three bytes behave exactly as before, so all the action is in the fourth. Here it is as a stencil: the mask's ones let the address bits through, its zeros block them.

The mask as a stencil: last byte of 192.168.1.130/26 128 64 32 16 8 4 2 1 address130 1 0 0 0 0 0 1 0 maskAND 192 1 1 0 0 0 0 0 0 network= 128 1 0 0 0 0 0 0 0 mask 1s: bits pass throughmask 0s: bits forced to 0 The first three bytes pass through untouched (mask 255), so the network is 192.168.1.128.

192.168.1.130/26

address    11000000 10101000 00000001 10000010   192.168.1.130
mask       11111111 11111111 11111111 11000000   255.255.255.192
           ----------------------------------- AND
network    11000000 10101000 00000001 10000000   192.168.1.128
wildcard   00000000 00000000 00000000 00111111   0.0.0.63
           ----------------------------------- network OR wildcard
broadcast  11000000 10101000 00000001 10111111   192.168.1.191
  • Network: 192.168.1.128 (130 AND 192 = 128)
  • Broadcast: 192.168.1.191 (128 OR 63 = 191)
  • First usable: 192.168.1.129
  • Last usable: 192.168.1.190
  • Usable hosts: 2⁶ − 2 = 62

There's a decimal shortcut once you trust the binary. A /26 block holds 64 addresses, so /26 networks start at multiples of 64 in the last byte: 0, 64, 128, 192. Which block holds 130? The one starting at 128, and it runs for 64 addresses, to 191.

Worked example 3: 10.20.37.200/21

Now the line falls in the third byte, which trips people up because the "interesting" byte isn't the last one. A /21 mask is 255.255.248.0.

10.20.37.200/21

address    00001010 00010100 00100101 11001000   10.20.37.200
mask       11111111 11111111 11111000 00000000   255.255.248.0
           ----------------------------------- AND
network    00001010 00010100 00100000 00000000   10.20.32.0
broadcast  00001010 00010100 00100111 11111111   10.20.39.255
  • Network: 10.20.32.0 (37 AND 248 = 32; the last byte is all host bits, so it becomes 0)
  • Broadcast: 10.20.39.255 (32 OR 7 = 39; the last byte becomes 255)
  • First usable: 10.20.32.1
  • Last usable: 10.20.39.254
  • Usable hosts: 2¹¹ − 2 = 2,046

Two things here look odd and are perfectly fine. 10.20.32.255 is an ordinary host address in this network, not a broadcast: the broadcast is the all-ones host part, and 10.20.32.255 still has zeros in its host bits (the low three bits of the third byte). Likewise 10.20.33.0 is a perfectly usable host. A trailing .0 or .255 is only special when the mask says it is.

As pseudocode

Every subnet calculator, including the one below, is a few lines of bit twiddling on 32-bit unsigned integers:

function subnet_info(addr, prefix):          # addr as a 32-bit unsigned integer
    mask      = (prefix == 0) ? 0 : (0xFFFFFFFF << (32 - prefix))
    wildcard  = NOT mask                     # all 32 bits flipped
    network   = addr AND mask
    broadcast = network OR wildcard
    if prefix == 32:  first, last = network, network       # a single host
    elif prefix == 31: first, last = network, broadcast    # point-to-point, RFC 3021
    else:             first, last = network + 1, broadcast - 1
    return network, broadcast, first, last

function prefix_from_mask(mask):
    inverted = NOT mask
    if (inverted AND (inverted + 1)) != 0:   # inverted must look like 0...0111...1
        error "mask is not contiguous"
    return 32 - popcount(inverted)

The prefix == 0 special case isn't decoration: in C and JavaScript, shifting a 32-bit value by 32 places doesn't give 0 (it's undefined in C and does nothing in JavaScript), and /0 is a real prefix, as we'll see. The contiguity test works because a valid wildcard is a run of ones at the bottom, and adding 1 to 000…0111 carries all the way up to 000…1000, which shares no bits with it.

The edge cases: /31 and /32

The "minus two" rule makes small blocks wasteful. A /30 has four addresses but only two usable hosts, which is exactly what a link between two routers needs. Half the block is spent on a network and broadcast address that a two-device link has no use for. RFC 3021 (2000) fixed this for point-to-point links: on a /31, both addresses are hosts and there is no broadcast address. 192.168.1.240/31 gives one router .240 and the other .241. Most modern router operating systems support it, and big networks with thousands of links use it to halve their address use.

A /32 is a block of exactly one address. You don't put a /32 on an ordinary LAN interface (there'd be nobody else on the subnet), but it shows up everywhere else: a host route pointing at one specific machine, a router's loopback address used as its stable identity, a firewall rule that matches one address. 192.168.1.30/32 means "the NAS, and nothing else".

Now try it yourself. Every number in the three worked examples should reappear.

Address and mask

/24

Type a.b.c.d/24, a.b.c.d 255.255.255.0 or a.b.c.d/255.255.255.0. Drag the slider, or click any bit, to move the line between network and host.

The 32 bits

Results

Worked by hand

From Classes to CIDR

The mask hasn't always travelled with the address. For IPv4's first twelve years, the address told you its own network size, using its first few bits, like a phone system that reads the area-code length from the first digit. This was classful addressing:

Class First bits First byte Network / host split Networks Hosts each
A 0 0–127 8 / 24 (/8) 128 16,777,214
B 10 128–191 16 / 16 (/16) 16,384 65,534
C 110 192–223 24 / 8 (/24) 2,097,152 254
D 1110 224–239 multicast, not networks – –
E 1111 240–255 reserved – –

(Two of the 128 class A networks, 0 and 127, were never usable as ordinary networks, as we'll see in the special ranges.)

It was elegant: a router could tell a destination's network size from the first byte alone, with no extra information. It was also a disaster, for two reasons.

It wasted addresses. There were only three sizes, and the jumps between them are huge. An organisation with 2,000 computers was far too big for a class C (254 hosts) and got a class B, with room for 65,534. Roughly 97% of it sat unused, and nobody else could have it. Early recipients got class A blocks of nearly 17 million addresses each; MIT (18.0.0.0) and Apple (17.0.0.0) were among them.

It bloated routing tables. The alternative to one class B was handing out several class Cs, but each class C is a separate network and needs its own entry in every Internet router's table. By the early 1990s class B space was running out and routing tables were growing faster than router memory.

Classful: the first bits fixed the network size Class A · 0–127 128 × /8 (16.8M addresses each) B · 128–191 16,384 × /16 C 2.1M × /24 D E 0… 10… 110… multicast · reserved An organisation needing 2,000 addresses, drawn to scale: Class C (/24): 256 addresses. Too small. Class B (/16): 65,536 addresses. About 97% unused. CIDR /21: 2,048 addresses. About 2% unused. teal = addresses actually needed

The first fix came from inside organisations. Subnetting (RFC 950, 1985) let a site split its class B into smaller internal networks using a mask, while the outside world still saw one class B. That's where the word subnet comes from: a sub-network of a classful network.

The real fix was to make the mask part of the address everywhere. Classless Inter-Domain Routing, CIDR (pronounced "cider"), arrived in RFC 1519 in 1993 and was updated as RFC 4632 in 2006. CIDR threw the classes away: a network is any power-of-two block, written with an explicit prefix length, and routers carry that length alongside every route. Our 2,000-host organisation gets a /21 (2,048 addresses) and wastes almost nothing. And, as we'll see under aggregation, CIDR also attacked the routing-table problem.

Classes are long dead in routing, but their ghosts linger. People still say "a class C" when they mean a /24, operating systems still suggest a default mask based on the first byte when you type an address by hand (Windows does), and the class D range is still multicast. The calculator above shows each address's historical class for that reason, not because it means anything to a modern router.

Addresses With Special Jobs

Not every address is a normal, globally unique host address. The IETF keeps a registry of special-purpose blocks. These are the ones you'll actually run into:

Block Range What it's for
0.0.0.0/8 0.0.0.0 – 0.255.255.255 "This network". 0.0.0.0 as a source means "I don't have an address yet": DHCP clients and the ARP probes use it. Never a destination.
10.0.0.0/8 10.0.0.0 – 10.255.255.255 Private (RFC 1918)
100.64.0.0/10 100.64.0.0 – 100.127.255.255 Shared space for carrier-grade NAT (RFC 6598)
127.0.0.0/8 127.0.0.0 – 127.255.255.255 Loopback: never leaves the machine
169.254.0.0/16 169.254.0.0 – 169.254.255.255 Link-local, self-assigned (RFC 3927)
172.16.0.0/12 172.16.0.0 – 172.31.255.255 Private (RFC 1918)
192.0.2.0/24 192.0.2.0 – 192.0.2.255 Documentation, TEST-NET-1 (RFC 5737)
192.168.0.0/16 192.168.0.0 – 192.168.255.255 Private (RFC 1918)
198.18.0.0/15 198.18.0.0 – 198.19.255.255 Network benchmarking (RFC 2544)
198.51.100.0/24 198.51.100.0 – 198.51.100.255 Documentation, TEST-NET-2 (RFC 5737)
203.0.113.0/24 203.0.113.0 – 203.0.113.255 Documentation, TEST-NET-3 (RFC 5737)
224.0.0.0/4 224.0.0.0 – 239.255.255.255 Multicast (old class D)
240.0.0.0/4 240.0.0.0 – 255.255.255.255 Reserved (old class E); its very last address is the limited broadcast
255.255.255.255/32 just that one Limited broadcast

A few deserve a closer look.

Private addresses. RFC 1918 (1996) set aside three blocks that anyone may use inside their own network, without asking anyone, on the understanding that they're never routed on the public Internet: 10.0.0.0/8 (16,777,216 addresses), 172.16.0.0/12 (1,048,576) and 192.168.0.0/16 (65,536). Our whole home LAN lives in 192.168.1.0/24, a slice of the third block, and so do millions of other home networks at the same time. That's fine, because none of those addresses ever appear on the Internet: the router translates them on the way out, which is the subject of the NAT article. Notice the middle block is a /12, so it ends at 172.31.255.255, not 172.255.255.255. 172.32.0.1 is an ordinary public address.

Loopback. Everything in 127.0.0.0/8 loops straight back to the machine that sent it, without touching a network card. 127.0.0.1 is the one everyone uses (localhost); the other 16 million or so are mostly a historical extravagance from the classful days.

Link-local. When a machine is set to get its address from DHCP and no DHCP server answers, it can pick a random address in 169.254.0.0/16 for itself (RFC 3927, 2005), check nobody else has it using ARP probes much like those in the ARP article, and talk to its immediate neighbours. If you ever see your laptop with a 169.254.x.x address, it's telling you DHCP failed. (The first and last /24 inside the block are reserved, so a host picks from 169.254.1.0 to 169.254.254.255.)

Carrier-grade NAT. 100.64.0.0/10 (RFC 6598, 2012) is for ISPs that put their customers behind a second layer of NAT. It's a separate block from RFC 1918 precisely so that it can't collide with whatever private range the customer uses at home. Some overlay VPNs borrow it for the same reason.

Documentation. Three /24s are reserved so that books, RFCs and articles can show realistic addresses without pointing at anybody's real machine. Our web server, 203.0.113.80, has been one of those all along: it lives in TEST-NET-3. A real web server would have a public address from some provider's block. (The same thinking gave us the locally administered MAC addresses in the Ethernet article.)

Multicast and the rest. 224.0.0.0/4 addresses name groups of receivers, not individual hosts; the 224.0.0.22 entry in the ARP article's Windows cache is the IGMPv3 membership-reports multicast group, mapped to an Ethernet multicast MAC. 240.0.0.0/4 has been "reserved for future use" since the 1980s, and plenty of software and hardware still refuses to handle it. Finally, 255.255.255.255 is the limited broadcast: "everyone on this link", with no need to know the network's address. A DHCP client that doesn't yet know what network it's on sends its first request from 0.0.0.0 to 255.255.255.255.

Two kinds of broadcast

255.255.255.255 always means "this link", and routers never forward it. A directed broadcast like 192.168.1.255 means "every host on network 192.168.1.0/24", and in principle could be sent from far away. Attackers abused that in the late 1990s (send one ping to a directed broadcast with a forged source, and every host on the network replies to the victim), so routers now refuse to forward directed broadcasts by default. On the wire, both kinds end up as Ethernet frames to ff:ff:ff:ff:ff:ff.

Subnetting: Carving Up a Block

Suppose our home network grows into a small office. There are now engineers, a guest Wi-Fi network, office staff, a few servers, and a link to a second router. Putting all of them in one flat 192.168.1.0/24 would work, but every ARP broadcast would reach every device, a guest's laptop would be one hop from the servers, and there'd be no way to apply different rules to different groups. We want separate subnets, each its own broadcast domain (usually on its own VLAN), with a router between them.

Splitting a block is just moving the line to the right. Borrow one host bit for the network part and the /24 becomes two /25s:

192.168.1.0/24    11000000 10101000 00000001 xxxxxxxx
  192.168.1.0/25    11000000 10101000 00000001 0xxxxxxx   .0 – .127
  192.168.1.128/25  11000000 10101000 00000001 1xxxxxxx   .128 – .255

Borrow two bits and you get four /26s (the borrowed bits go 00, 01, 10, 11): .0, .64, .128 and .192, each 64 addresses. Three bits gives eight /27s of 32, and so on. Every split halves the block, and the two halves of any block are called siblings: you can always merge two siblings back into their parent.

The alignment rule

That last sentence hides the most important rule in subnetting:

A block must start on a multiple of its own size

A /26 holds 64 addresses, so it can only start at .0, .64, .128 or .192. A /27 (32 addresses) can only start at a multiple of 32. This isn't a convention someone chose: the network address is the address with every host bit set to zero, and "the low h bits are zero" is exactly what "a multiple of 2h" means in binary.

So 192.168.1.64/26 is a fine network, but 192.168.1.96/26 isn't one. 96 isn't a multiple of 64; AND it with the mask and you get 192.168.1.64. Some tools reject 192.168.1.96/26, others quietly treat it as "host .96 inside 192.168.1.64/26". The calculator above will tell you which network an address really belongs to.

VLSM: Different Sizes for Different Needs

Cutting the /24 into equal pieces would be wasteful. The office needs:

Subnet Hosts needed
Engineering 100
Guest Wi-Fi 50
Office 25
Servers 10
Link to the second router 2

Using a different prefix length for each subnet is called variable-length subnet masking (VLSM), and the procedure is simple.

  1. Size each subnet

    For each group, find the smallest block whose usable hosts (2h − 2) cover it. 100 hosts needs 7 host bits (126 usable): a /25. 50 needs 6 bits (62): a /26. 25 needs 5 (30): a /27. 10 needs 4 (14): a /28. The router link needs exactly 2: a /31.

  2. Sort largest first

    Allocating the biggest blocks first keeps every block aligned with no gaps, because each smaller block size divides every larger one.

  3. Hand out blocks from the start of the free space

    Each block starts where the previous one ended.

Carving 192.168.1.0/24, largest first

Engineering  100 hosts  /25  192.168.1.0/25     usable .1   – .126  bcast .127
Guest Wi-Fi   50 hosts  /26  192.168.1.128/26   usable .129 – .190  bcast .191
Office        25 hosts  /27  192.168.1.192/27   usable .193 – .222  bcast .223
Servers       10 hosts  /28  192.168.1.224/28   usable .225 – .238  bcast .239
Router link    2 hosts  /31  192.168.1.240/31   both usable: .240 and .241
Free                         192.168.1.242/31, 192.168.1.244/30, 192.168.1.248/29

128 + 64 + 32 + 16 + 2 = 242 addresses allocated, 14 left over for later. Each subnet's router interface typically takes its first usable address: .1, .129, .193 and .225.

192.168.1.0/24 carved with VLSM (to scale) .0 .128 .192 .224 .240 .255 Engineering /25 100 hosts · .0 – .127 Guest Wi-Fi /26 50 · .128 – .191 Office /27 · 25 Servers /28 · 10 hosts · .224 – .239 Router link /31 · .240 and .241 Free: .242/31 · .244/30 · .248/29 Every block starts on a multiple of its own size.

What goes wrong in the wrong order

To see why the order matters, take a tighter set of requests: a /27, a /28, a /26, another /28 and a /25. That's 32 + 16 + 64 + 16 + 128 = 256 addresses, an exact fit for the /24. Hand them out in the order they arrived, always at the next aligned address after the previous block, never going back to fill a gap:

In arrival order

/27  .0   – .31
/28  .32  – .47
/26  .64  – .127   (can't start at .48: not a multiple of 64)
/28  .128 – .143   (the .48–.63 gap is behind the cursor)
/25  must start at .0 or .128: both taken. FAILS

128 addresses are still free (.48–.63 and .144–.255), but no aligned /25 exists among them.

Largest first

/25  .0   – .127
/26  .128 – .191
/27  .192 – .223
/28  .224 – .239
/28  .240 – .255

Every block lands on an aligned address with no gaps, and the plan fills the /24 exactly.

When every block is at least as big as the ones after it, the next free address is always correctly aligned for the next block, so holes never appear.

Here's the procedure as pseudocode:

function vlsm(parent_network, parent_prefix, needs):    # needs: list of (name, hosts)
    sort needs by hosts, largest first
    next = parent_network
    end  = parent_network + 2^(32 - parent_prefix)
    for (name, hosts) in needs:
        if hosts == 1:   prefix = 32
        elif hosts == 2: prefix = 31                     # point-to-point
        else:            prefix = 32 - ceil(log2(hosts + 2))
        size = 2^(32 - prefix)
        if next + size > end:
            error "does not fit"
        allocate name -> next / prefix                   # aligned, because sizes only shrink
        next = next + size
    return allocations, free space from next to end

Real allocation tools do a bit more (they look for gaps to reuse, and leave room for growth), but this is the core. Try it in the splitter below: split blocks by hand until you have the office's layout, or let the "fit these needs" button do it.

Parent block

Split and merge

Fit these needs (VLSM)

One subnet per line: a name, then the hosts it needs (or a block size such as /27).

Order

Same subnet?

Supernetting and Route Aggregation

Subnetting moves the line right, to make smaller blocks. Moving it left glues blocks together. Suppose a company has four branch networks behind one ISP:

10.1.0.0/24   00001010 00000001 00000000 xxxxxxxx
10.1.1.0/24   00001010 00000001 00000001 xxxxxxxx
10.1.2.0/24   00001010 00000001 00000010 xxxxxxxx
10.1.3.0/24   00001010 00000001 00000011 xxxxxxxx
              first 22 bits identical   →   10.1.0.0/22

All four share their first 22 bits and between them cover every value of the next two. So instead of four routes, the rest of the world needs just one: 10.1.0.0/22. This is aggregation (or summarisation, or supernetting), and it was the second half of CIDR's purpose. Each ISP gets a big block, hands out smaller blocks to its customers, and announces only the big block to everyone else. One route can stand for thousands of customer networks.

10.1.0.0/24 10.1.1.0/24 10.1.2.0/24 10.1.3.0/24 four routes… 10.1.0.0/22 …announced as one 10.1.0.0 – 10.1.3.255

Aggregation only works for blocks that are siblings, all the way up: two /24s can merge into a /23 only if together they form an aligned /23. 10.1.1.0/24 and 10.1.2.0/24 are adjacent but not siblings: the two aligned /23s are 10.1.0.0/23 and 10.1.2.0/23, and each holds only one of them. The smallest single block containing both is 10.1.0.0/22, which also claims 10.1.0.0/24 and 10.1.3.0/24.

Despite aggregation, the global routing table keeps growing as address blocks are split, traded and announced separately; by 2025 it had passed a million IPv4 routes. Without CIDR it would be far bigger.

A teaser: longest-prefix match

Aggregation raises a question. What if one branch, 10.1.2.0/24, moves to a different ISP, so the world sees both 10.1.0.0/22 (via the first ISP) and 10.1.2.0/24 (via the second)? An address like 10.1.2.77 matches both. The rule every IP router follows is longest-prefix match: the most specific route wins.

Route Next hop Matches 10.1.2.77? Matches 10.1.1.5? Matches 203.0.113.80?
10.1.2.0/24 ISP 2 yes (24 bits) no no
10.1.0.0/22 ISP 1 yes (22 bits) yes no
0.0.0.0/0 default yes (0 bits) yes yes

0.0.0.0/0 is the default route: a prefix of length zero, which matches every address, and so loses to anything more specific. It's the routing-table form of the "default gateway" our laptop uses. How routers store and search these tables quickly is a story for Routing Basics.

Local or Remote, Revisited

Now we can redo the ARP article's decision properly. A host compares the network part of the destination with the network part of its own address, using its own mask:

                  laptop's mask: 255.255.255.0 (/24)
laptop      192.168.1.10   11000000 10101000 00000001 00001010  → 192.168.1.0
printer     192.168.1.20   11000000 10101000 00000001 00010100  → 192.168.1.0   same: local
web server  203.0.113.80   11001011 00000000 01110001 01010000  → 203.0.113.0   differs: remote

The printer's network bits match the laptop's, so the laptop ARPs for 192.168.1.20 directly. The server's don't (they differ in the very first byte), so the laptop sends the packet to the default gateway, 192.168.1.1. That's exactly what the ARP article concluded, now with the actual bits.

With a mask that isn't byte-aligned, the octet shortcut stops working. Suppose the office had split its /24 into four equal /26s instead. Now a boundary falls inside the last byte, and two addresses can share three whole bytes and still be on different subnets:

                  mask: 255.255.255.192 (/26)
192.168.1.100   11000000 10101000 00000001 01100100  → 192.168.1.64
192.168.1.130   11000000 10101000 00000001 10000010  → 192.168.1.128   different subnets

When the mask is wrong

The test is only as good as the mask, and each host applies its own. A mistyped mask is one of the most common network misconfigurations, and the two directions fail differently.

Mask too long: laptop set to /28

Someone types 255.255.255.240 instead of 255.255.255.0. The laptop now believes its network is 192.168.1.0/28: only .0 to .15. The router at .1 is still inside, so the Internet works. But the printer at .20 is now "remote" (20 AND 240 = 16, not 0), so the laptop sends print jobs to the router. The router sees a destination on the very link the packet came in on, sends it back out, and many routers also send an ICMP Redirect saying "you could have sent that directly". Printing works, slowly and strangely. The printer's replies, meanwhile, go straight to the laptop, because the printer's mask is still /24.

Mask too short: laptop set to /16

Now the mask is 255.255.0.0, and the laptop thinks every address from 192.168.0.0 to 192.168.255.255 is on its wire. Suppose the office has another subnet, 192.168.2.0/24, behind the router. To reach 192.168.2.40, the laptop doesn't use the router at all: it broadcasts "who has 192.168.2.40?" Nobody on the LAN answers, the ARP entry goes to FAILED, and ping reports "Destination Host Unreachable" from the laptop itself. (Unless the router does proxy ARP, in which case it answers and quietly hides the mistake.)

The worst case is a mask so long that the gateway falls outside it. Give the NAS 192.168.1.30/28 (network .16–.31) with gateway 192.168.1.1, and the gateway is "remote" from the NAS's own point of view. To reach a remote address you go via the gateway, but the gateway itself is remote: there's no way to reach it. Linux, for one, refuses to install such a default route ("Nexthop has invalid gateway") unless told the gateway is on-link. The NAS can still talk to the printer at .20, which falls inside its /28, and to nothing else.

The Subnet Splitter above includes a "same subnet?" checker. Try 192.168.1.10 and 192.168.1.20 with /24 and then /28, and watch the answer flip.

Running Out

Four billion addresses was never going to be enough for a world with more people than that, each carrying a phone, and the classful years spent the supply carelessly. CIDR and private addresses bought decades, but not forever.

  1. 3 February 2011: IANA's free pool is empty

    The Internet Assigned Numbers Authority hands out address space in /8 chunks to five Regional Internet Registries (RIRs). On this date it gave away its last five /8s, one to each RIR. There was nothing left to hand out at the top.

  2. 2011–2020: the regions follow

    APNIC (Asia-Pacific) reached its last /8 in April 2011, RIPE NCC (Europe and the Middle East) in September 2012, LACNIC (Latin America) in June 2014, and ARIN (North America) ran out of free addresses in September 2015. "Reaching the last /8" meant switching to tiny rationed allocations, not running out completely: RIPE NCC finally ran dry in November 2019. AFRINIC (Africa), the fifth registry, lasted longest: it only started allocating from its final /8 in April 2017, and moved to its stricter second rationing phase in January 2020.

  3. Today: a market and two workarounds

    IPv4 addresses are now bought, sold and leased between organisations, and the remaining Internet grows mostly through two techniques: sharing one public address among many private ones with NAT (including the carrier-grade kind that uses 100.64.0.0/10), and moving to a protocol with a vastly larger address space, IPv6.

The subnetting ideas in this article carry straight over to IPv6: prefixes, prefix lengths, longest-prefix match and aggregation all work the same way. The numbers just get much, much bigger.

Where to Go Next

  • IPv6: 128-bit addresses, why every LAN is a /64, and how a host builds its own address.
  • NAT: how a whole house of 192.168.1.x devices shares one public address, and what that breaks.
  • Routing Basics: routing tables, longest-prefix match, and what the router does after the laptop hands it a packet.
  • DHCP: where the laptop's address, mask and gateway come from without anyone typing them.
  • VLANs: putting each of the office's subnets on its own broadcast domain over shared switches.
  • Binary Integers and Number Bases (Number Representation): the binary and hex underneath every dotted quad.

Key Takeaways

  • An IPv4 address is a 32-bit number; dotted-quad notation writes its four bytes in decimal. There are 232, about 4.3 billion, in total.
  • The mask (or prefix length) splits the address into a network part and a host part. A mask is a run of ones followed by zeros: /24 = 255.255.255.0, /26 = 255.255.255.192.
  • Network = address AND mask; broadcast = network OR wildcard; usable hosts run from network + 1 to broadcast − 1, giving 2h − 2 of them. /31 links use both addresses (RFC 3021); /32 is one host.
  • Worked examples: 192.168.1.130/26 is in 192.168.1.128–.191 (62 hosts); 10.20.37.200/21 is in 10.20.32.0–10.20.39.255 (2,046 hosts).
  • Classful addressing (A, B, C) fixed sizes by the first bits and wasted space; CIDR (RFC 1519, 1993; RFC 4632) made the prefix length explicit and enabled aggregation.
  • RFC 1918 private ranges are 10/8, 172.16/12 and 192.168/16. Also special: 127/8 loopback, 169.254/16 link-local, 100.64/10 CGNAT, three documentation /24s (our 203.0.113.80 is one), 224/4 multicast.
  • Subnets must start on a multiple of their size. Allocate VLSM subnets largest first and they pack with no gaps.
  • Routers pick the longest matching prefix; 0.0.0.0/0 is the default route. Hosts decide "local or via the gateway" with their own mask, so a wrong mask breaks things in predictable ways.
  • IANA ran out of IPv4 in 2011 and the regional registries over the following years; NAT and IPv6 are the two ways out.

Our laptop now knows exactly which addresses are its neighbours. But there's a deeper problem lurking behind that 192.168.1.10: millions of other homes are using the very same address right now. Next up: IPv6, the protocol designed so that would never be necessary.