Computer Networking › Section D · The network layer: data plane › Topic D.3
IPv4 datagrams and addressing
Every packet crossing the internet carries a small IPv4 or IPv6 header, and every router reads it. Knowing what is in the IPv4 header, and how its 32-bit addresses are carved into subnets and prefixes, is the key to reading forwarding tables and planning networks.
The IPv4 header
An IPv4 header is 20 bytes without options. Its fields, in order: version (4); header length, in 32-bit words, normally 5; type of service, now split into a 6-bit differentiated services code and 2 Explicit Congestion Notification bits; total length of the datagram in bytes, at most 65,535; identification, flags and fragment offset, used for fragmentation; time to live (TTL), cut by one at every router, which discards the datagram at zero; protocol, naming the payload's handler, such as 6 for TCP, 17 for UDP and 1 for ICMP; a header checksum; the 32-bit source and destination addresses; and optional extras that are rarely used.
Checksum and TTL
The header checksum covers only the IPv4 header, not the data, and is a 16-bit ones'-complement sum like the one UDP and TCP use. Because every router changes the TTL, every router must also recompute the checksum before forwarding. A datagram whose checksum fails is usually discarded. Why check at both IP and transport? The transport checksum protects the data end to end, while the IP checksum protects the header that routers act on. TTL exists so that a datagram caught in a routing loop dies after a bounded number of hops instead of circling forever.
Addresses belong to interfaces
An IPv4 address is 32 bits, written as four bytes in dotted-decimal form, such as 198.51.100.77. Strictly, an address identifies an interface, the boundary between a device and one link, not the device itself. A laptop with Wi-Fi and Ethernet both active has two addresses; a router joining five links has five. There are \(2^{32}\), about 4.3 billion, possible addresses. IANA handed out its last free blocks to the regional registries in 2011, and most registries had used up their ordinary pools by 2019, which is why address sharing and IPv6 matter.
Subnets and CIDR notation
A subnet is a group of interfaces that can reach each other directly, without a router in between, such as all the devices on one office Ethernet. Its interfaces share the same leftmost bits, the network prefix. Classless Inter-Domain Routing (CIDR), adopted in 1993, writes this as a.b.c.d/x, where x is the prefix length: 198.51.100.64/27 means the first 27 bits name the subnet and the last 5 name hosts. The same boundary written as a subnet mask has 27 ones followed by 5 zeros: 255.255.255.224. Before CIDR, classful addressing allowed only /8, /16 and /24 networks, which wasted huge numbers of addresses.
Worked example: a /27 subnet
A host is configured as 198.51.100.77/27. Find its subnet. 1. Host bits: \(32 - 27 = 5\), so the block has \(2^5 = 32\) addresses. 2. The prefix ends 3 bits into the last byte (\(27 = 24 + 3\)). 77 is 01001101; keeping the top 3 bits, 010, and zeroing the rest gives 01000000, which is 64. 3. The network address is 198.51.100.64, and the block runs to .95. 4. The all-ones host value, 198.51.100.95, is the subnet's broadcast address, and the network address itself is not given to a host, so .65 to .94, 30 addresses, are usable. Check: blocks of 32 start at multiples of 32, and 64 is the largest not above 77.
Special addresses
Some addresses are never assigned to an ordinary interface. In each subnet, the all-zeros host part names the subnet and the all-ones host part is its directed broadcast. The address 255.255.255.255 is the limited broadcast: a datagram sent to it reaches every host on the local subnet and is never forwarded by routers. The block 127.0.0.0/8 is loopback, so 127.0.0.1 always means this machine. Private ranges, which homes and offices reuse freely, and how hosts obtain addresses at all, are the subject of the next topic, DHCP and network address translation.
Route aggregation
CIDR's biggest benefit is aggregation: one prefix can stand for many smaller ones. Picture, scaled down to documentation addresses, an ISP holding 198.51.100.0/24 that gives four customers 198.51.100.0/26, 198.51.100.64/26, 198.51.100.128/26 and 198.51.100.192/26. Instead of announcing four routes to the rest of the internet, it announces just the /24, and the world's routers need one entry; real aggregates are far larger. Addresses are handed out in this nested way: IANA allocates to five regional internet registries, they allocate to ISPs, and ISPs assign to customers. Even so, the global table holds about a million IPv4 prefixes, because aggregation is often broken in practice.
When a customer changes ISP
Suppose the customer with 198.51.100.64/26 moves to a second ISP and keeps its addresses. The new ISP now announces 198.51.100.64/26, while the old one keeps announcing 198.51.100.0/24. A datagram for 198.51.100.70 matches both routes. Routers resolve this with longest-prefix matching, described in the topic Inside a router: the /26 is more specific than the /24, so the datagram goes to the new ISP, while 198.51.100.130 still goes to the old one. More-specific routes punch holes in an aggregate, which is exactly why the global table grows.
Fragmentation
Every link has a maximum transmission unit (MTU), the largest datagram it can carry: 1,500 bytes on standard Ethernet. An IPv4 router facing a smaller MTU can split a datagram into fragments, each a complete datagram with a copy of the header. All fragments share the original's identification value; the more-fragments flag is 1 on every fragment except the last; and the fragment offset gives the position of the fragment's data in units of 8 bytes. Only the destination reassembles. Losing one fragment loses the whole datagram, so hosts set the don't-fragment flag and use path MTU discovery instead.
Worked example: fragmenting a datagram
A 3,000-byte datagram (20-byte header, 2,980 bytes of data) meets a link with a 1,000-byte MTU. 1. Room for data: \(1000 - 20 = 980\) bytes, but offsets count 8-byte units, so each non-final fragment carries a multiple of 8: 976 bytes. 2. \(2980 = 3 \times 976 + 52\), so there are four fragments. 3. Total lengths: 996, 996, 996 and 72 bytes. 4. Offsets: 0, \(976 \div 8 = 122\), 244 and 366; the flag is 1, 1, 1, 0. Check: the data adds up to \(3 \times 976 + 52 = 2980\) bytes, and the last fragment starts at byte \(366 \times 8 = 2928\).
Common mistakes to avoid
First, forgetting that the network and broadcast addresses are not usable: a /27 has 32 addresses but 30 hosts. Second, assuming every subnet is a /24 and its broadcast always ends in .255. Third, reading the fragment offset in bytes instead of 8-byte units. Fourth, thinking TTL counts seconds; routers decrement it once per hop. Fifth, saying a device has one IP address; addresses belong to interfaces. Sixth, assuming an aggregate route wins because it was announced first; the longest matching prefix wins.
Key takeaways
The 20-byte IPv4 header carries version, length, service bits, fragmentation fields, TTL, protocol (6 TCP, 17 UDP, 1 ICMP), a header-only checksum and 32-bit addresses. Addresses identify interfaces. CIDR writes a subnet as a.b.c.d/x; a /x block has \(2^{32-x}\) addresses, minus the network and broadcast addresses for hosts. 255.255.255.255 is the local broadcast and 127.0.0.1 is loopback. Aggregation lets one prefix stand for many; longer prefixes override it. Fragments share an identification value and carry offsets in 8-byte units, and only the destination reassembles them.
Flashcards
Flashcard
20 bytes without options
Test your understanding
Question 1 of 5
What is the broadcast address of the subnet containing 203.0.113.150/28?
Question 2 of 5
How many addresses in a /22 subnet can be assigned to host interfaces?
Question 3 of 5
A fragment arrives with a fragment offset of 122. Where does its data belong in the original datagram's data?
Question 4 of 5
Why must every router recompute the IPv4 header checksum before forwarding a datagram?
Question 5 of 5
ISP A announces 198.51.100.0/24 and ISP B announces 198.51.100.64/26. Where do routers send a datagram for 198.51.100.70?
