IPipv4dotted decimalsubnet maskcidrprivate addresses

IPv4: 32 Bits, Networks and Hosts

192.168.1.42 is 32 bits split by a mask into a network part that routers care about and a host part that only the last router does; the private ranges, loopback, link-local and broadcast addresses are carved out of the same space, and the space ran out — which is why NAT and IPv6 exist.

IPv4
Interview question
Progress

Builds on Network basics in the Networking roadmap.

The problem

A router cannot store a route for every one of four billion addresses. Addresses must be grouped so that one table entry covers a whole organisation. How is a 32-bit number structured so that "where is this" is a prefix match, and which parts of the space were reserved for what?

Four bytes, written in decimal

An IPv4 address is a 32-bit unsigned integer. 192.168.1.42 is the four bytes C0 A8 01 2A, each written in decimal and separated by dots — a notation chosen for humans in 1981 that has confused them ever since, because the dots have no structural meaning. The boundary between "network" and "host" is not at a dot; it is wherever the mask says.

The subnet mask is a second 32-bit number with ones on the left and zeros on the right: 255.255.255.0 is 24 ones then 8 zeros. AND the address with the mask and you get the network address (192.168.1.0); the remaining bits are the host part (.42). CIDR notation writes the mask as a count of ones: 192.168.1.42/24. Routers think entirely in prefixes: a routing table entry is a prefix and a length, and forwarding is "find the entry with the longest prefix that matches this destination". A /24 covers 256 addresses; a /16 covers 65,536; a /8 covers 16.7 million; /32 is one host; /0 is everything (the default route).

192.168.1.42/24 in binary
address  192      .168      .1        .42
         11000000 .10101000 .00000001 .00101010
mask /24 11111111 .11111111 .11111111 .00000000
         └────────── network (24 bits) ─────────┘└ host ┘
network  192.168.1.0        (host bits all 0)
broadcast 192.168.1.255     (host bits all 1)
usable   192.168.1.1 – 192.168.1.254  (254 hosts)

On-link or not: the only decision a host makes

A host uses its own address and mask for exactly one routing decision: is the destination in my network? AND the destination with my mask; if it equals my network address, the destination is on-link — resolve its MAC directly and send. If not, send to the default gateway (the router on my network) and let it worry. 10.0.0.7 from 192.168.1.42/24 is off-link; 192.168.1.200 is on-link. Two hosts on the same cable with masks that disagree can each think the other is off-link and try to route through a gateway that sees no need — a classic misconfiguration.

The two addresses at the ends of every subnet are reserved. Host bits all zero is the network address, naming the subnet itself in routing tables. Host bits all one is the directed broadcast for that subnet (192.168.1.255); 255.255.255.255 is the limited broadcast that never leaves the segment, used by DHCP discovery before a host has any address. Hence the arithmetic every interviewer expects: a /24 has 256 addresses and 254 usable. See Subnetting: Splitting an Address Space.

The carved-out ranges

Not all of the 2^32 space is routable on the internet. RFC 1918 private ranges — 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 — may be used by anyone inside their own network and are never routed publicly; every home network is 192.168.x.x or 10.x.x.x, and so is every corporate LAN and every cloud VPC. Because millions of networks reuse the same addresses, a packet with a private source cannot be replied to across the internet: something at the edge must rewrite it to a public address — that something is NAT, and private addressing is the reason it exists. Cloud VPCs are the same idea: 10.0.0.0/16 per VPC, NAT gateways at the edge, and peering that fails when two VPCs chose the same range.

127.0.0.0/8 is loopback: packets to 127.0.0.1 (or any 127.x) never reach a NIC; the kernel delivers them to itself. localhost resolves here. 169.254.0.0/16 is link-local: a host that asked DHCP and got no answer picks a random address in it, usable only on the local segment — seeing one is the diagnosis "DHCP failed". 100.64.0.0/10 is the shared address space for carrier-grade NAT, so ISPs can number customers without colliding with their home 10.x networks. 224.0.0.0/4 is multicast. 0.0.0.0 means "any address" when binding a socket and "no address yet" as a source. And three /24s — 192.0.2.0, 198.51.100.0, 203.0.113.0 — are reserved for documentation, which is why they appear throughout these lessons and never on the wire.

Special IPv4 ranges
RangeMeaningRouted on the internet?You see it when
10.0.0.0/8private (RFC 1918), 16.7 M addressesnocorporate LANs, cloud VPCs, Kubernetes pods
172.16.0.0/12private (RFC 1918), 1 M addressesnoDocker’s default bridge (172.17.0.0/16)
192.168.0.0/16private (RFC 1918), 65 k addressesnohome routers
100.64.0.0/10shared address space for CGNATnoyour ISP-facing address is not public
127.0.0.0/8loopbackno — never leaves the hostlocalhost, local dev servers
169.254.0.0/16link-local (APIPA)noDHCP failed; cloud metadata at 169.254.169.254
224.0.0.0/4multicastlimitedmDNS 224.0.0.251, OSPF, video
0.0.0.0"any" / unspecifiednobind(0.0.0.0) = listen on all interfaces
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24documentationnothese lessons
everything elsepublic unicastyesservers, ISP-assigned addresses

Running out

Four billion addresses sounded infinite in 1981. Early allocation was by class: class A gave 16 million addresses to one organisation, class B 65,536, class C 256, with nothing in between — so a university with 300 hosts took a class B and wasted 65,000. CIDR (1993) replaced classes with arbitrary prefix lengths and let routes aggregate, which bought twenty years. Private addressing plus NAT bought more, by letting a whole household or company sit behind one public address. IANA handed out its last /8 blocks in February 2011; the regional registries ran out one by one, RIPE (Europe) in November 2019. Today public IPv4 addresses are bought and leased on a market at tens of dollars each, cloud providers charge for every public IPv4 address, and most residential users are behind carrier-grade NAT.

The structural cost is not just scarcity. NAT breaks the end-to-end model: a host behind it cannot be connected to without help, which is why peer-to-peer, VoIP, gaming and WebRTC all carry NAT-traversal machinery, and why the "everyone is a server" internet did not happen on IPv4. The fix is a bigger address space with the end-to-end model restored — see IPv6: Not Just Bigger Addresses — and the reason it took so long is that NAT made IPv4 *work well enough* for the client–server web.

Key points

  • An IPv4 address is 32 bits; the dots are for humans. The mask (or /n) decides where network ends and host begins.
  • Routers forward on prefixes by longest match; a host makes one decision — on-link (same network) or via the default gateway.
  • Host bits all zero = network address; all one = broadcast. A /24 has 256 addresses, 254 usable.
  • Private ranges (10/8, 172.16/12, 192.168/16) are reused by everyone and never routed publicly — which is why NAT exists.
  • Loopback 127/8 never leaves the host; link-local 169.254/16 means DHCP failed; 100.64/10 means you are behind CGNAT.
  • The space ran out (IANA 2011, RIPE 2019). CIDR and NAT delayed it; IPv6 is the answer.

Why does this exist?

Mechanisms are answers to constraints. Open each question before reading the answer.

▸Why split an address into network and host at all?

So that a router can hold one entry for a million hosts. Without prefixes every router would need a route per address; with them, the whole of an organisation is one line, and the internet’s core table stays under a million entries.

▸Why did CIDR replace classes?

Three fixed sizes wasted most of the space and bloated routing tables. Arbitrary prefix lengths let an allocation match a need and let adjacent blocks be advertised as one larger prefix.

▸Why private ranges rather than just more public ones?

There were no more public ones. Private ranges let networks grow without allocations and let a single public address front many hosts — at the price of NAT and the loss of end-to-end reachability.

How it fails

What the failure looks like from inside real software.
Reference · show
  • Wrong mask on one host: it believes a neighbour is off-link, sends to the gateway, the gateway sends an ICMP redirect or the reply path differs; intermittent one-way connectivity.
  • Two networks with the same private range joined by VPN or peering: 10.0.0.5 exists on both sides; routes conflict and half the hosts are unreachable. NAT or renumbering is the only fix.
  • A service bound to 127.0.0.1 "unreachable from the network": it is listening only on loopback; bind 0.0.0.0 (or a specific interface) instead.
  • 169.254.x.x on an interface: DHCP failed — wrong VLAN, DHCP server down, or a rogue DHCP server answered first.
  • Using .0 or .255 of a /24 as a host address: some stacks refuse it, others accept it, and broadcasts land on your host.
  • Documentation ranges in a real config: 203.0.113.x copied from an example is not routed anywhere.

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. What does 10.4.16.0/20 mean? How does a host decide whether a destination is on its own subnet, and what happens differently if it is not?

    Show a strong answer

    10.4.16.0/20 describes 2^12 = 4,096 addresses from 10.4.16.0 to 10.4.31.255; the mask 255.255.240.0 keeps the first 20 bits. In IPv4 the first (network) and last (broadcast) addresses are conventionally unusable, so 4,094 hosts. The prefix length is the whole story — dotted masks are just a notation for it.

    Before sending any packet, a host ANDs the destination with its own mask and compares the result with its own network: 10.4.20.7 & /20 = 10.4.16.0 — same network → on-link: resolve the destination’s MAC with ARP (IPv4) or Neighbor Discovery (IPv6) and send the frame directly. 10.4.40.7 & /20 = 10.4.32.0 — different → off-link: look up the routing table (usually the default route), resolve the gateway’s MAC instead, and send the frame to the gateway with the destination IP unchanged. The packet looks the same; only the Ethernet destination differs.

    Subnets exist because a flat network does not scale: broadcast domains must stay small, routing tables must aggregate (one /20 route instead of 4,096 host routes), and boundaries are where policy (firewalls, ACLs) attaches. Getting the mask wrong on a host is a classic failure: a host that believes it is /16 on a /20 network will try to ARP for addresses that are actually behind the router, and get silence.

    Full interview question, with follow-ups and red flags →
  2. How does NAT let a hundred devices share one public IP? What does the NAT device keep, and what breaks because of it?

    Show a strong answer

    When 192.168.1.42:51000 opens a connection to 203.0.113.5:443, the NAT device rewrites the source to its public 198.51.100.1 and a chosen port, say 40001, and records a translation entry: (192.168.1.42, 51000, 203.0.113.5, 443) ↔ (198.51.100.1, 40001). Return packets to 198.51.100.1:40001 are matched against the table and rewritten back. Ports are what make sharing possible: one public address has ~64k ports, so it can multiplex tens of thousands of simultaneous flows from many private hosts. The IPv4 checksum and TCP/UDP checksums are recomputed on every rewrite.

    Entries are created by outbound traffic and expire by timeout — a few minutes for idle TCP on many devices, tens of seconds for UDP. That produces the classic failures. Inbound connections have no entry, so unsolicited packets are dropped unless a static mapping (port forwarding) exists; peer-to-peer and WebRTC need STUN/TURN and hole-punching to work around it. Idle connections whose entry expires die silently: the next packet matches nothing, and the client sees a reset or a hang — the reason long-lived connections need keep-alives shorter than the NAT timeout.

    Table exhaustion under high connection churn (many short-lived connections, each holding an entry through TIME_WAIT) drops new flows with a timeout to perfectly healthy destinations. And protocols that embed IP addresses in their payload (classic FTP, SIP) break unless the NAT understands them, because the address in the payload is not rewritten.

    Full interview question, with follow-ups and red flags →
  3. How many /24s fit in a /20?

    Show a strong answer