Introduction

IPv4 is the network-layer protocol of the Internet. It carries datagrams from a source host to a destination host across many networks, hop by hop, with no connection set-up and no guarantee of delivery, order or freedom from duplication: a best-effort service. Each router looks at the destination address, finds the outgoing link in its forwarding table, decrements the time-to-live, and sends the datagram on.

Every later IP topic builds on two things: the 32-bit address (its notation, the historical classes, the special and private blocks) and the header (each field, its width and its unit). Fragmentation, CIDR and NAT each have their own notes; they use these address and header fields.

Prerequisites

Core concepts

Addresses

An IPv4 address is 32 bits, written as four decimal bytes: 192.168.10.5 is 11000000 10101000 00001010 00000101. An address belongs to an interface, not a host: a router with three interfaces has three addresses.

Every address splits into a network part (the prefix) and a host part. In the classful scheme of the original design the split was fixed by the leading bits; today the split is given explicitly by a prefix length or mask (CIDR Notation and Subnetting), but the classful terms are still in use.

ClassLeading bitsFirst byteNetwork / host bitsNetworksHosts per network
A00 to 1278 / 2427=1282^7 = 128224−2=16,777,2142^{24} - 2 = 16{,}777{,}214
B10128 to 19116 / 16214=16,3842^{14} = 16{,}384216−2=65,5342^{16} - 2 = 65{,}534
C110192 to 22324 / 8221=2,097,1522^{21} = 2{,}097{,}15228−2=2542^8 - 2 = 254
D1110224 to 239multicastn/an/a
E1111240 to 255reservedn/an/a

The 128 class A networks include 0 and 127, which are reserved, leaving 126 usable.

Special addresses

AddressMeaning
Host part all 0sFor conventional broadcast subnets with h≥2h\ge2, the network itself, not assigned to an interface; on an RFC 3021 /31 point-to-point link, it is one endpoint; /32 denotes a single host address
Host part all 1sDirected broadcast only for conventional broadcast subnets with h≥2h\ge2; on an RFC 3021 /31 point-to-point link, it is the other endpoint, not a directed broadcast; /32 has no separate subnet-directed broadcast address
255.255.255.255Limited broadcast: this network, never forwarded by routers
0.0.0.0"This host", used as a source before an address is known (for example in DHCP)
127.0.0.0/8Loopback; 127.0.0.1 reaches the local host
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16Private (RFC 1918): not routed on the public Internet; used behind NAT

For conventional broadcast subnets with h≥2h\ge2 host bits, all-zero and all-one host parts identify the network and broadcast, leaving 2h−22^h-2 assignable addresses. A /31 point-to-point link uses both addresses as endpoints (RFC 3021); /32 denotes one address/host route, not a negative host count.

The header

Text
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-------+-------+---------------+-------------------------------+
|Version|  IHL  |Type of Service|          Total Length         |
+-------+-------+---------------+-----+-------------------------+
|         Identification        |Flags|     Fragment Offset     |
+---------------+---------------+-----+-------------------------+
|  Time to Live |    Protocol   |        Header Checksum        |
+---------------+---------------+-------------------------------+
|                         Source Address                        |
+---------------------------------------------------------------+
|                      Destination Address                      |
+---------------------------------------------------------------+
|                    Options (if IHL > 5)                       |
+---------------------------------------------------------------+
FieldBitsUnit and meaning
Version44 for IPv4
IHL4Header length in 4-byte words: 5 to 15, so 20 to 60 bytes
Type of service8Now DSCP (6 bits, RFC 2474) and ECN (2 bits, RFC 3168)
Total length16Header plus data in bytes: at most 65,535
Identification16Same value in every fragment of one datagram
Flags3Reserved (0), DF (don't fragment), MF (more fragments)
Fragment offset13Position of this fragment's data in 8-byte units
Time to live8Decremented by each router; at 0 the datagram is discarded and an ICMP Time Exceeded message is sent back
Protocol8Payload type: 1 ICMP, 6 TCP, 17 UDP
Header checksum16Internet checksum over the header only
Source, destination32 eachAddresses
Options0 to 40 bytesRare; padded to a multiple of 4 bytes
The IPv4 header drawn as five 32-bit rows with bit positions 0 to 31 across the top: row 1 Version 4 bits, IHL 4 bits, Type of Service 8 bits, Total Length 16 bits; row 2 Identification 16 bits, Flags 3 bits, Fragment Offset 13 bits; row 3 Time to Live 8 bits, Protocol 8 bits, Header Checksum 16 bits; row 4 Source Address 32 bits; row 5 Destination Address 32 bits; then an optional Options row. Unit labels: IHL in 4-byte words, Total Length in bytes, Fragment Offset in 8-byte units. Beneath, the example header bytes 45 00 05 dc 1c 46 40 00 40 06 83 f0 c0 a8 0a 05 ac 10 1e 28 are aligned to their fields.
Figure 1. The IPv4 header: 20 bytes without options, with IHL in 4-byte words, total length in bytes and fragment offset in 8-byte units; the example header 45 00 05 dc ... 83 f0 is aligned beneath.

Mathematical explanation: the units

  • Header length =4×IHL= 4 \times \text{IHL} bytes. IHL 5 means 20 bytes; IHL 15 gives the 60-byte maximum, with 40 bytes of options.
  • Data length =total length−4×IHL= \text{total length} - 4 \times \text{IHL}.
  • Largest datagram 65,535 bytes, so at most 65,535−20=65,51565{,}535 - 20 = 65{,}515 data bytes.
  • Fragment offset ×8\times 8 gives the byte position, so all fragments except the last carry a multiple of 8 data bytes; the largest expressible offset is (213−1)×8=65,528(2^{13} - 1) \times 8 = 65{,}528.
  • Checksum. The 16-bit one's complement of the one's complement sum of the header's 16-bit words, computed with the checksum field set to 0 (Error Detection: Parity and Checksum). Since TTL changes at every router, every router recomputes it. For a changed header word m→m′m\to m\prime, RFC 1624 gives HC′=∼(∼HC+∼m+m′)HC\prime=\mathord{\sim}(\mathord{\sim}HC+\mathord{\sim}m+m\prime), with one's-complement sums. TTL is the high byte of its word: for a TTL-only decrement, add 0x0100 with end-around carry, then normalize a resulting 0xFFFF to 0x0000 (the zero-boundary exception).

Worked examples

Example 1: decoding a header

The 20 header bytes of a datagram, in hexadecimal:

Text
45 00 05 dc 1c 46 40 00 40 06 83 f0 c0 a8 0a 05 ac 10 1e 28
FieldBytesValue
Version, IHL454, 5 words = 20 bytes, no options
Total length05 dc1500 bytes, so 1480 data bytes
Identification1c 467238
Flags, offset40 00DF = 1, MF = 0, offset 0: an unfragmented datagram that must not be fragmented
TTL4064
Protocol06TCP
Checksum83 f0Valid: the one's complement sum of all ten words, checksum included, is 0xFFFF
Sourcec0 a8 0a 05192.168.10.5 (private)
Destinationac 10 1e 28172.16.30.40 (private, inside 172.16.0.0/12)

After one router the TTL is 63 and the new checksum is 84 f0.

Example 2: options

A header has IHL = 7 and total length 576. Header length is 28 bytes, of which 8 are options, and the data is 576−28=548576 - 28 = 548 bytes.

Example 3: classes and host counts

150.20.4.9 has first byte 150, binary 10010110, so class B; network 150.20.0.0, 65,534 hosts. 223.1.1.255 is class C with host part all 1s: the directed broadcast of 223.1.1.0, not a host address. 130.0.0.0 is class B with host part all 0s: the network address.

  • ARP maps an IP address on the local network to a MAC address: one broadcast ARP request ("who has 192.168.10.1?") and one unicast reply from the owner, 2 messages; the result is cached, so later datagrams need none. A host sending to a remote address ARPs for the default router's address, not the remote host's.
  • DHCP gives a host its address: Discover (client broadcast), Offer (server), Request (client broadcast), Acknowledge (server), 4 messages over UDP, server port 67 and client port 68. The client uses source 0.0.0.0 until it has an address.

ICMP messages ride inside IP datagrams (protocol 1) and report errors or answer queries. Echo request (type 8) and echo reply (type 0) are what ping uses. Destination unreachable is type 3, with code 3 for port unreachable and code 4 for fragmentation needed and DF set (IPv4 Fragmentation). Time exceeded is type 11, code 0 when a router decrements the TTL to 0.

A datagram sent with TTL tt is delivered only across at most t−1t - 1 routers: with TTL 3 it crosses 2 routers to the host, but on a path of 3 routers the third router discards it and returns time exceeded. Sending probes with TTL 1, 2, 3, and so on makes router kk answer the probe with TTL kk, which reveals the path one router at a time (the idea behind traceroute); a host 4 routers away is first reached with TTL 5.

Important properties and observations

  • Best effort. IP may lose, reorder or duplicate datagrams; reliability, if needed, is TCP's job.
  • The checksum covers only the header. Transport protocols protect their own data.
  • Hop by hop. Source and destination addresses stay the same along the path (except through NAT); the link-layer addresses change at every hop.
  • IHL and fragment offset are scaled; total length is not. Mixing these units gives wrong lengths and offsets.

Common misconceptions.

  • "IHL counts bytes." It counts 4-byte words.
  • "Total length is the data length." It includes the header.
  • "The IP checksum covers the data." Header only.
  • "A host has one IP address." Each interface has one; routers have several.
  • "Class A has 128 usable networks." 126; networks 0 and 127 are reserved.

GATE examination perspective

  • Header decoding. Split hex into fields, apply the units (IHL ×4\times 4, offset ×8\times 8), and read the flags bit by bit.
  • Class from first byte. 0 to 127 A, 128 to 191 B, 192 to 223 C, 224 to 239 D, 240 to 255 E.
  • Host counts. 2h−22^h - 2 usable addresses for conventional broadcast subnets with h≥2h\ge2 host bits. /31 point-to-point links use both addresses as endpoints (RFC 3021); /32 denotes one address/host route.
  • Special addresses. Network, directed broadcast, limited broadcast, loopback, private blocks.
  • Checksum per hop. It changes because TTL changes.

Summary

Sources

  1. A. S. Tanenbaum, D. J. Wetherall, Computer Networks, 5th ed., Pearson, 2011: Sections 5.6.1 (The IP Version 4 Protocol), 5.6.2 (IP Addresses) and 5.6.4 (Internet Control Protocols).
  2. J. F. Kurose, K. W. Ross, Computer Networking: A Top-Down Approach, 8th ed., Pearson, 2021: Sections 4.3.1 (IPv4 Datagram Format), 4.3.2 (IPv4 Addressing) and 6.4.1 (Link-Layer Addressing and ARP).
  3. B. A. Forouzan, Data Communications and Networking, 5th ed., McGraw-Hill, 2013: Chapters 18 (Introduction to Network Layer) and 19 (Network-Layer Protocols).
  4. J. Postel (Ed.), RFC 791, Internet Protocol, 1981, Section 3.1 (Internet Header Format), (consulted 2026-11-11).
  5. J. Postel, RFC 792, Internet Control Message Protocol, 1981 (Destination Unreachable type 3 with codes 3 and 4, Time Exceeded type 11 code 0, Echo types 8 and 0), (consulted 2026-11-11).
  6. K. Nichols, S. Blake, F. Baker, D. Black, RFC 2474, Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers, 1998, Section 3 (six-bit DSCP), (consulted 2026-11-11).
  7. K. Ramakrishnan, S. Floyd, D. Black, RFC 3168, The Addition of Explicit Congestion Notification (ECN) to IP, 2001, Section 5 (the ECN field: bits 6 and 7 of the former TOS octet), (consulted 2026-11-11).
  8. Y. Rekhter, B. Moskowitz, D. Karrenberg, G. J. de Groot, E. Lear, RFC 1918, Address Allocation for Private Internets, 1996, Section 3, (consulted 2026-11-11).
  9. D. C. Plummer, RFC 826, An Ethernet Address Resolution Protocol, 1982, (consulted 2026-11-11).
  10. R. Droms, RFC 2131, Dynamic Host Configuration Protocol, 1997, Sections 3.1 (allocating a network address) and 4.1 (ports 67 and 68), (consulted 2026-11-11).
  11. A. Retana et al., RFC 3021, Using 31-Bit Prefixes on IPv4 Point-to-Point Links, Sections 2.1 and 2.2.1, (consulted 2026-11-11).

Next steps