L3 · addressing · reviewed

Subnetting

IPv4 prefixes, masks and longest-prefix match

Divides an address space by moving a boundary through the bits, and decides which route wins by how many of those bits a prefix pins down.

Presenter modeEmbed this figure

Basic Concept and Prefix NotationRFC 4632 · BEST CURRENT PRACTICE · August 2006Considerations of 31-Bit PrefixesRFC 3021 · PROPOSED STANDARD · December 2000Address Allocation for Private InternetsRFC 1918 · BEST CURRENT PRACTICE · February 1996

Why it exists

The original address plan had three sizes: a /8, a /16 and a /24. An organisation needing three hundred addresses took a /16 and wasted sixty-five thousand, and the address space was exhausted by arithmetic rather than by demand.

Classless routing removed the fixed sizes. A prefix carries its own length, so a block can be any power of two and the boundary can sit anywhere in the thirty-two bits. Everything about modern addressing follows from that one change. IntroductionRFC 4632 · BEST CURRENT PRACTICE · August 2006

The same change made routing tables aggregatable. A provider holding a contiguous block advertises one prefix rather than thousands, which is the only reason the global table is the size it is rather than orders of magnitude larger.

The boundary is a position, not a number

A prefix length says how many leading bits are fixed. Everything else about a subnet follows from that one fact.

An IPv4 address is thirty-two bits. A prefix length says how many of them are fixed — the network — and leaves the rest to count addresses inside it. 192.168.4.0/24: Fixed bits 24, Free bits 8. /26 · 62 hosts. /26 · 62 hosts. /26 · 62 hosts. /26 · 62 hosts.

192.168.4.0/24Fixed bits: 24Free bits: 8/26 · 62 hosts/26 · 62 hosts/26 · 62 hosts/26 · 62 hosts
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
192.168.4.0/24zoneFixed bits: 24 · Free bits: 8
/26 · 62 hostshost
/26 · 62 hostshost
/26 · 62 hostshost
/26 · 62 hostshost
192.168.4.0/24/26 · 62 hostslinkup
192.168.4.0/24/26 · 62 hostslinkup
192.168.4.0/24/26 · 62 hostslinkup
192.168.4.0/24/26 · 62 hostslinkup
1 / 4

An IPv4 address is thirty-two bits. A prefix length says how many of them are fixed — the network — and leaves the rest to count addresses inside it.

Dotted decimal hides this. 255.255.255.0 and /24 are the same statement, and only one of them makes it obvious that moving the boundary one bit halves or doubles the block.

What changed

  • 192.168.4.0/24: Fixed bits → 24
  • 192.168.4.0/24: Free bits → 8
  • Emphasis on 192.168.4.0/24

How it works

A prefix length is a count of fixed leading bits. Everything else is derived: the mask is that many ones, the block holds two to the power of the remaining bits, the lowest address is the network and the highest is the broadcast. Basic Concept and Prefix NotationRFC 4632 · BEST CURRENT PRACTICE · August 2006

Forwarding compares an address against every route and selects the one fixing the most bits. Nothing else participates: metric and administrative distance choose between two routes to the same prefix, and have nothing to say about a longer one. Rules for Route AdvertisementRFC 4632 · BEST CURRENT PRACTICE · August 2006

Two lengths are special. A /31 gives a point-to-point link two usable addresses instead of the four a /30 consumed, and a /32 is a single host — a loopback, or a route to one address. Considerations of 31-Bit PrefixesRFC 3021 · PROPOSED STANDARD · December 2000

Try it

Do the arithmetic

Find where a subnet ends, watch longest-prefix match choose between overlapping routes, and see what a set summarises to.

The first 26 bits are the network and never change; the remaining 6 count the addresses inside it. The boundary is obvious in binary and invisible in dotted decimal, which is why subnetting is taught this way.

192.168.4.64/26
Network192.168.4.64
Mask255.255.255.192
Wildcard0.0.0.63
Broadcast192.168.4.127
Host range192.168.4.65192.168.4.126
Addresses64 total, 62 usable
  • 192.168.4.64/28192.168.4.64192.168.4.79
  • 192.168.4.80/28192.168.4.80192.168.4.95
  • 192.168.4.96/28192.168.4.96192.168.4.111
  • 192.168.4.112/28192.168.4.112192.168.4.127

On the wire

Constructed examples, encoded from the field table below them — not captured traffic.

IHL 5, so no options. DF is set because the sender is doing path MTU discovery — which is why a filtered ICMP turns this into a black hole rather than a retry at a smaller size.

Link layer
Ethernet with EtherType 0x0800, or whatever the medium uses to say "IPv4 follows". RFC 791
IPv4 header
Version, length, differentiated services, fragmentation control, TTL, protocol, addresses. RFC 791
Payload
Whatever the protocol field names — 1 for ICMP, 6 for TCP, 17 for UDP, 89 for OSPF, 103 for PIM. RFC 791

Configure it

Address interfaces by length, summarise deliberately, and make the gap fail loudly.

Cisco IOS-XE 17.12 · Catalyst 9500, ISR 4451draft

  1. interface Vlan10
     ip address 192.168.4.1 255.255.255.192

    A /26. IOS still takes a dotted mask here even though everything else in the CLI speaks in lengths.

  2. interface GigabitEthernet0/0/1
     ip address 10.255.0.0 255.255.255.254

    A /31 on a point-to-point link: two addresses, both usable, no broadcast. Half the addressing cost of the /30 this replaced.

    Common mistake: Assuming the lower address is unusable because it looks like a network address. On a /31 there is no network address — that is the entire point.

    RFC 3021 §2

  3. router ospf 1
     area 1 range 10.0.0.0 255.255.252.0

    Advertises one /22 in place of the component routes. Check what else that /22 covers before configuring it.

    RFC 4632 §5.2

  4. ip route 10.0.0.0 255.255.252.0 Null0 254

    A discard route for the summary. Traffic for the part of the block this site does not hold is dropped here with a counter, rather than being forwarded back out and looping.

    Common mistake: Omitting it. Without a discard route a summary with a gap can send traffic back toward its own default and loop until the TTL expires.

Verify

show ip route 10.0.1.1
Which route is selected, and its prefix length.
show ip route summary
Route counts by source.
show ip interface brief
Interface addresses.

Caveats

  • IOS accepts a dotted mask on interfaces and a length nearly everywhere else, which makes copy-paste between contexts error-prone.
  • A /31 is supported on point-to-point interfaces; some platforms reject it on a switched virtual interface.

When it breaks

Symptom first, because that is what you have when it happens.

  1. A host reaches some addresses on its own subnet and sends others to the gateway, which may or may not return them.

    Narrow it down

    1. Compare the host mask against the mask on the router interface.
    2. Identify which destinations work and check whether they fall inside the narrower of the two.
    3. Look for proxy ARP masking the problem.

    Cause

    The masks disagree. Each host decides locally whether a destination is on-link, so a host with a narrower mask sends on-subnet traffic to the gateway and a host with a wider one tries to ARP for addresses that are not there.

    Fix

    Make the mask consistent on every device in the subnet, including the router. It is a per-host decision, so one wrong host produces asymmetric symptoms that look like a network fault.

  2. A network nobody at this site owns becomes unreachable from part of the organisation after a summarisation change.

    Narrow it down

    1. Compare the advertised summary against the prefixes actually held.
    2. Check whether the covered-but-absent networks are advertised more specifically elsewhere.
    3. Trace where traffic for them is landing.

    Cause

    The summary covers addresses this site does not hold. While a more specific route exists elsewhere it is harmless; when that route is withdrawn, traffic follows the summary here and is dropped.

    Fix

    Advertise the prefixes separately, or add a discard route for the summary so the black hole is at least explicit and countable.

  3. A routing protocol is converged and correct, and traffic to one destination goes somewhere else entirely.

    Narrow it down

    1. Look for a more specific route to that destination, from any source.
    2. Check static routes in particular — they are frequently more specific than what a protocol advertises.
    3. Compare prefix lengths rather than administrative distances.

    Cause

    A longer prefix from a different source. Administrative distance never enters into it: a /32 static route beats a /24 from any protocol, because they are not competing for the same prefix.

    Fix

    Remove the stray specific, or make it deliberate. This is the usual reason a working routing protocol appears to be ignored.

  4. A block that should have been large enough runs out partway through an allocation.

    Narrow it down

    1. Check the order the subnets were allocated in.
    2. Look for a small subnet sitting on a boundary a larger one needed.
    3. Add the requirements and compare against the parent block.

    Cause

    Allocating smallest first. A block must start on its own boundary, so a /29 placed early can strand the alignment a /25 required even when the arithmetic says there is room.

    Fix

    Allocate largest first. This is the whole reason variable-length subnetting is taught as a procedure rather than as a sum.

  5. Two hosts on the same wire can reach each other in one direction only, and neither can explain why.

    Narrow it down

    1. Compare the prefix length configured on each host, not just the addresses.
    2. Work out which host believes the other is local and which believes it is remote.
    3. Check whether the one that fails is sending to a gateway that has no route back.

    Cause

    The mask is local configuration and travels in no packet. A host with a longer prefix believes its neighbour is off-subnet and sends to the gateway; the other, with a shorter one, replies directly. One direction works and the other depends entirely on whether the gateway will help.

    Fix

    Make the prefix length identical on every host in the subnet. Nothing on the wire will tell you it is not — the comparison has to be made by looking.

Design notes

Allocate contiguous, aligned blocks per site from the beginning. Summarisation is a property of the address plan, not something configured later — a site holding 10.0.0.0/24 and 10.0.3.0/24 cannot summarise them without also claiming two networks it does not have.

Size subnets for the broadcast domain rather than for the host count. A /16 of hosts on one segment is an ARP problem long before it is an address problem, and the practical ceiling is far below what the addressing permits.

Private address space is not a security boundary and not unique. RFC 1918 blocks appear in every acquisition and every partner connection, and 10.0.0.0/8 in particular collides constantly. Allocating from an unusual part of it costs nothing and saves a renumbering. Address Allocation for Private InternetsRFC 1918 · BEST CURRENT PRACTICE · February 1996

Misconceptions

A 10.x.x.x address is a class A, so it has a /8 mask.
Classes were removed in 1993. The mask comes from configuration and nothing else — 10.1.2.0/24 is an ordinary prefix, and the first octet says nothing about its length. Classful defaults survive only in a few legacy CLI behaviours. IntroductionRFC 4632 · BEST CURRENT PRACTICE · August 2006
Usable hosts is always the block size minus two.
It is for /30 and shorter. A /31 has two usable addresses because a point-to-point link needs no broadcast, and a /32 has one. Both are common on router links and loopbacks, so the exception is not obscure. Considerations of 31-Bit PrefixesRFC 3021 · PROPOSED STANDARD · December 2000
A route with a better metric or administrative distance wins.
Only against another route to the same prefix. Longest match runs first and is absolute: a /32 from the worst possible source beats a /24 from the best one, because the two were never compared.
RFC 1918 addresses cannot be reached from outside.
They are not routed on the public internet, which is not the same as unreachable. Anything with a path in — a VPN, a partner link, an acquisition — reaches them, and because everyone uses the same blocks the collisions are constant. Address Allocation for Private InternetsRFC 1918 · BEST CURRENT PRACTICE · February 1996

More walkthroughs

The most specific route wins, whatever its sourcedesign-choice

Several routes can contain the same address. Only the prefix length decides between them — not the protocol, not the metric, not the order.

A packet for 10.1.2.5 arrives. Three routes in the table contain that address, and all three are valid. Router: Matching routes 3. 0.0.0.0/0. 10.0.0.0/8. 10.1.2.0/24. 10.1.2.5.

RouterMatching routes: 30.0.0.0/010.0.0.0/810.1.2.0/2410.1.2.5
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
RouterrouterMatching routes: 3
0.0.0.0/0cloud
10.0.0.0/8cloud
10.1.2.0/24cloud
10.1.2.5host
Router0.0.0.0/0linkup
Router10.0.0.0/8linkup
Router10.1.2.0/24linkup
10.1.2.0/2410.1.2.5linkup
1 / 4

A packet for 10.1.2.5 arrives. Three routes in the table contain that address, and all three are valid.

What changed

  • Emphasis on Router ↔ 0.0.0.0/0
  • Emphasis on Router ↔ 10.0.0.0/8
  • Emphasis on Router ↔ 10.1.2.0/24
  • Router: Matching routes → 3

A summary that claims more than it holdsfailure

Summarising two prefixes produces the smallest block covering both — which frequently covers addresses neither of them did.

This site holds two /24s: 10.0.0.0/24 and 10.0.3.0/24. Advertising both is two routes; the operator wants one. Edge router: Advertising 2 prefixes. 10.0.0.0/24. 10.0.3.0/24. 10.0.1.0/24 · elsewhere.

Edge routerAdvertising: 2 prefixes10.0.0.0/2410.0.3.0/2410.0.1.0/24 · elsewhere
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
Edge routerrouterAdvertising: 2 prefixes
10.0.0.0/24zone
10.0.3.0/24zone
10.0.1.0/24 · elsewherezone
Edge router10.0.0.0/24linkup
Edge router10.0.3.0/24linkup
1 / 4

This site holds two /24s: 10.0.0.0/24 and 10.0.3.0/24. Advertising both is two routes; the operator wants one.

What changed

  • Edge router: Advertising → 2 prefixes
  • Emphasis on Edge router ↔ 10.0.0.0/24
  • Emphasis on Edge router ↔ 10.0.3.0/24

Terms

Prefix length
The count of leading bits an address fixes. Everything else about a subnet — its size, its network address, its broadcast — is derived from this one number.
Longest prefix match
The forwarding rule: among all routes containing a destination, the one fixing the most bits is used. Metric and administrative distance only compare routes to the same prefix and never override this.
Summary route
One prefix advertised in place of several. Because a prefix is always a contiguous power-of-two block, a summary absorbs any gap between its components and claims reachability for it.

Check yourself

  • A /24 is split into /26s. How many blocks result?
  • How many usable host addresses does a /26 provide?
  • How many usable addresses does a /31 provide?
  • A /8 learned by OSPF and a /24 static route both contain the destination. Static routes have a worse administrative distance here. Which is used?
  • What do 10.0.0.0/24 and 10.0.3.0/24 summarise to?
  • Why does variable-length allocation start with the largest requirement?
  • Where does a packet carry the subnet mask it was sent under?
  • Host A has /24 and host B has /25 on the same wire. A can reach B and B cannot reach A. Why?