Lesson 07 beginner

IPv4 Addressing

How a 32-bit IPv4 address is built, the network and host parts, classful addressing (A to E) and why it was replaced by CIDR, public, private and special ranges, the IPv4 header field by field, TTL and fragmentation.

The network layer gives every interface a logical address that works across networks, unlike a MAC address that only matters on one link. For most of the internet’s history that address has been IPv4: 32 bits, about 4.3 billion possible addresses, which turned out to be not nearly enough. This lesson covers how IPv4 addresses are structured and read; lesson 8 does the subnetting math, and lesson 9 covers IPv6.

Reading an IPv4 address

An address is 32 bits, written as four decimal numbers (octets) from 0 to 255 separated by dots:

192      .168      .1        .42
11000000  10101000  00000001  00101010

To convert an octet, add the bit values that are 1: 128, 64, 32, 16, 8, 4, 2, 1. For 42: 32 + 8 + 2 = 42, so 00101010.

Every address has two parts:

  • Network part: identifies the network (all hosts on the same LAN share it).
  • Host part: identifies the interface inside that network.

The prefix length (/24) or subnet mask (255.255.255.0) says where the split is. 192.168.1.42/24 means the first 24 bits (192.168.1) are the network and the last 8 bits (42) are the host.

Classful addressing (history)

Until 1993 the first bits of the address fixed the split:

ClassFirst bitsFirst octetDefault maskNetworksHosts per network
A00 to 127/8 (255.0.0.0)12816,777,214
B10128 to 191/16 (255.255.0.0)16,38465,534
C110192 to 223/24 (255.255.255.0)2,097,152254
D1110224 to 239(multicast)
E1111240 to 255(reserved)

The problem: a company with 2,000 hosts was too big for a class C (254) and got a class B (65,534), wasting over 63,000 addresses. Address space ran out fast and routing tables exploded.

Classless addressing (CIDR)

CIDR (classless inter-domain routing) lets the prefix be any length from /0 to /32. That company now gets a /21 (2,046 hosts). Providers can also aggregate: instead of advertising 8 separate /24s, they advertise one /21 that covers them all. Class names still appear in casual talk (“a class C”) to mean a /24, but routers ignore classes completely.

Public, private and special addresses

RangePurpose
10.0.0.0/8Private (RFC 1918)
172.16.0.0/12 (172.16.0.0 to 172.31.255.255)Private (RFC 1918)
192.168.0.0/16Private (RFC 1918)
100.64.0.0/10Carrier-grade NAT, shared between ISP and customer
127.0.0.0/8Loopback: the host itself (127.0.0.1)
169.254.0.0/16Link-local: self-assigned when DHCP fails
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24Documentation and examples
224.0.0.0/4Multicast
0.0.0.0“This host” or “any address” (0.0.0.0/0 is the default route)
255.255.255.255Limited broadcast on the local network

Private addresses can be reused by every organization but are never routed on the internet. To reach the internet, a router translates them to a public address with NAT (lesson 13). Public addresses are unique worldwide and assigned by regional registries (ARIN, RIPE NCC, LACNIC, APNIC, AFRINIC) to ISPs and organizations.

169.254.x.x means DHCP failed. If a machine shows an address in 169.254.0.0/16, it asked for an address, got no answer and picked one for itself. It can talk to neighbors with the same problem, but nothing else.

Inspect and assign addresses

$ ip -4 addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
    inet 192.168.1.42/24 brd 192.168.1.255 scope global dynamic enp3s0
       valid_lft 84211sec preferred_lft 84211sec
$ sudo ip addr add 192.168.1.50/24 dev enp3s0
$ ip -4 -brief addr show enp3s0
enp3s0           UP             192.168.1.42/24 192.168.1.50/24
$ sudo ip addr del 192.168.1.50/24 dev enp3s0

dynamic with a valid_lft means the address came from DHCP and expires unless renewed. Addresses added with ip addr are temporary; for permanent settings use your distribution’s tool (netplan, NetworkManager’s nmcli, or systemd-networkd).

On Windows:

PS> Get-NetIPAddress -AddressFamily IPv4 | Format-Table InterfaceAlias,IPAddress,PrefixLength
PS> New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 192.168.1.50 -PrefixLength 24

The IPv4 header

Every IPv4 packet starts with at least 20 bytes of header:

FieldBitsWhat it does
Version4Always 4
IHL4Header length in 32-bit words (5 = 20 bytes, no options)
DSCP / ECN6 / 2QoS class (lesson 20) and congestion signaling
Total length16Header + data, up to 65,535 bytes
Identification16Groups fragments of the same packet
Flags3Includes DF (don’t fragment) and MF (more fragments)
Fragment offset13Where this fragment fits, in 8-byte units
TTL8Hop limit; each router subtracts 1 and drops the packet at 0
Protocol8What is inside: 1 ICMP, 6 TCP, 17 UDP, 47 GRE, 89 OSPF
Header checksum16Checks the header only, recomputed at every hop
Source address32Sender
Destination address32Receiver
Options0 to 320Rarely used

TTL in action

TTL stops packets from looping forever. traceroute exploits it: it sends packets with TTL 1, 2, 3… and each router that drops one sends back an ICMP “time exceeded”, revealing itself.

$ traceroute -n 1.1.1.1
traceroute to 1.1.1.1 (1.1.1.1), 30 hops max, 60 byte packets
 1  192.168.1.1  0.612 ms  0.580 ms  0.571 ms
 2  100.64.0.1  4.901 ms  4.877 ms  5.002 ms
 3  198.51.100.9  6.120 ms  6.231 ms  6.077 ms
 4  1.1.1.1  11.734 ms  11.602 ms  11.698 ms

Hop 2 is in 100.64.0.0/10: this ISP uses carrier-grade NAT.

Fragmentation

If a packet is bigger than the next link’s MTU, an IPv4 router may split it into fragments, and only the final destination puts them back together. Fragments are slow, a single lost fragment loses the whole packet, and many firewalls drop them. Modern hosts set DF and use path MTU discovery instead: a router that cannot forward a DF packet replies with ICMP “fragmentation needed”, and the sender lowers its packet size. Blocking all ICMP breaks this, which is a classic cause of “small pages load, big ones hang” problems, especially over VPNs.

Exercise

Which of these addresses can be routed on the public internet?

  1. 10.20.30.40
  2. 8.8.4.4
  3. 172.20.1.1
  4. 192.168.100.1
Show answer

Answer: B. 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private RFC 1918 ranges. 172.20.1.1 falls inside 172.16.0.0/12. Only 8.8.4.4 is public.

Exercise

A laptop shows the address 169.254.37.12 and cannot reach anything. What happened?

  1. It got no answer from a DHCP server and assigned itself a link-local address
  2. It received a carrier-grade NAT address from the ISP
  3. Its DNS server is down
  4. It is using a documentation address
Show answer

Answer: A. 169.254.0.0/16 is self-assigned when DHCP fails. Check the cable or Wi-Fi association, the VLAN and the DHCP server.

Exercise

What does a router do when it receives an IPv4 packet with TTL 1 that it must forward?

  1. Forwards it with TTL 0
  2. Sets TTL back to 64 and forwards it
  3. Drops it and sends an ICMP time exceeded message to the sender
  4. Fragments it
Show answer

Answer: C. Decrementing 1 gives 0, so the packet dies at that router, which reports back with ICMP time exceeded. That is exactly how traceroute discovers each hop.

Key takeaways

  • IPv4 addresses are 32 bits: a network part and a host part split by the prefix length.
  • Classful addressing (A, B, C) is history; CIDR allows any prefix length.
  • RFC 1918 private ranges need NAT to reach the internet; 169.254.x.x means DHCP failed.
  • The IPv4 header carries TTL, protocol number, addresses and fragmentation fields.
  • Don’t block all ICMP: path MTU discovery depends on it.

FAQ

Is 172.32.0.1 a private address?

No. The private block is 172.16.0.0/12, which ends at 172.31.255.255. 172.32.0.1 is a public address.

Why is 127.0.0.1 called localhost?

The whole 127.0.0.0/8 block loops back inside the host and never reaches a network card. The name localhost maps to 127.0.0.1 (and ::1 in IPv6) in /etc/hosts.

Have IPv4 addresses really run out?

The free pools at the regional registries are exhausted. New addresses come from transfers, which are sold at a market price, and the cloud providers now charge for public IPv4 addresses. That pressure is why NAT and IPv6 matter.

Can a single interface have several IP addresses?

Yes. Linux, Windows and every router allow multiple addresses per interface, which is common on servers hosting several services or during migrations.

↑ ↓ navigate Enter open Esc close