An IP (Internet Protocol) datagram is a basic unit of data transmitted over a network using the Internet Protocol. It is responsible for carrying information from a source device to a destination device across interconnected networks.
The Internet Protocol operates at the Network Layer (Layer 3) of the OSI model. It provides logical addressing and routing facilities to deliver packets from one network to another.
An IP datagram consists of two main parts:
- Header: Contains control information such as source address, destination address, packet length, and other details required for packet delivery.
- Data (Payload): Contains the actual information being transmitted, such as TCP segments, UDP datagrams, or ICMP messages.
The IP datagram header helps routers and receiving devices understand how to process and forward the packet correctly.
IP Datagram Header
An IP datagram header is the initial part of an IP datagram that contains important information required for transmitting data across a network.
It provides details about the source device, destination device, packet size, fragmentation, lifetime, and upper-layer protocol.
The header is placed before the actual data payload. Routers examine the header to determine where the packet should be forwarded.
The IPv4 header has a minimum size of 20 bytes and a maximum size of 60 bytes, depending on the presence of optional fields.
IP Datagram Header Format Diagram
The IPv4 datagram header consists of several fields. Each field has a specific size and function that helps in packet transmission and delivery.
The following diagram represents the structure of an IPv4 datagram header.

Figure: IP Datagram Header
Explanation of Each Field in the IP Datagram Header
The IPv4 datagram header contains 13 main fields, including optional fields such as Options and Padding. Each field performs a specific function during packet transmission.
1. Version (4 Bits)
The Version field identifies the version of the Internet Protocol used in the datagram.
- It occupies 4 bits.
- It specifies whether the packet uses IPv4 or another IP version.
- For IPv4, the value is always 4.
For example, the binary value 0100 represents decimal 4, indicating that the datagram uses IPv4.
Purpose: It identifies the IP version so that the receiving device can process the datagram correctly.
2. Internet Header Length (IHL) (4 Bits)
The Internet Header Length (IHL) field specifies the length of the IP datagram header.
- It occupies 4 bits.
- It represents the header length in units of 32-bit words (4 bytes).
- The minimum value is 5, representing 20 bytes.
- The maximum value is 15, representing 60 bytes.
For example, if the IHL value is 5:
Therefore, the header length is 20 bytes.
Purpose: It helps the receiving device determine where the header ends and the actual data begins.
3. Type of Service (ToS) (8 Bits)
The Type of Service field specifies the desired service quality for an IP datagram.
It helps routers handle different types of network traffic according to their requirements.
- It occupies 8 bits.
- It helps indicate packet priority and service requirements.
- It supports traffic handling based on delay, throughput, and reliability.
For example, voice and video applications require low delay, whereas file transfers may require higher throughput.
In modern IPv4 networks, this field is interpreted using two components:
- DSCP (Differentiated Services Code Point): Uses 6 bits to specify the traffic class.
- ECN (Explicit Congestion Notification): Uses 2 bits to provide congestion notification.
Purpose: It helps routers provide suitable service treatment to different types of network traffic.
4. Total Length (16 Bits)
The Total Length field specifies the complete length of the IP datagram, including the header and data.
- It occupies 16 bits.
- Its value is measured in bytes.
- The maximum datagram size is 65,535 bytes.
For example, consider an IP datagram containing:
- Header length = 20 bytes.
- Data length = 500 bytes.
The total length is:
Therefore, the Total Length field contains 520.
Purpose: It specifies the complete size of the IP datagram.
5. Identification (16 Bits)
The Identification field is used to identify an IP datagram and its fragments.
When a large datagram is divided into smaller fragments, all fragments belonging to the same original datagram carry the same Identification value.
- It occupies 16 bits.
- It helps identify fragments belonging to the same original datagram.
- It supports the reassembly of fragmented datagrams at the destination.
For example, if an IP datagram is divided into three fragments, all three fragments contain the same Identification value.
Purpose: It helps the destination device identify and reassemble fragments of the original datagram.
6. Flags (3 Bits)
The Flags field contains information related to fragmentation.
It occupies 3 bits and consists of three bits:
- Reserved Bit: This bit is reserved and must be set to 0.
- DF (Don’t Fragment): When this bit is set to 1, routers must not fragment the datagram.
- MF (More Fragments): When this bit is set to 1, more fragments follow. When it is set to 0, the current fragment is the last fragment or the datagram is not fragmented.
For example, when a datagram is divided into multiple fragments, the MF bit is set to 1 in all fragments except the last fragment.
Purpose: It controls fragmentation and indicates whether additional fragments are expected.
7. Fragment Offset (13 Bits)
The Fragment Offset field indicates the position of a fragment within the original IP datagram.
- It occupies 13 bits.
- It helps the destination device arrange fragments in the correct order.
- It is measured in units of 8 bytes.
For example, when a datagram is divided into smaller fragments, each fragment contains an offset value indicating the position of its data in the original datagram.
The first fragment has an offset value of 0.
Purpose: It helps the destination device correctly reassemble fragmented datagrams.
8. Time to Live (TTL) (8 Bits)
The Time to Live field specifies how long an IP datagram can remain in the network.
- It occupies 8 bits.
- It limits the number of router hops a datagram can make.
- Each router decreases the TTL value by at least 1.
- When the TTL value becomes 0, the datagram is discarded.
For example, if a datagram starts with a TTL value of 64:
- After the first router, TTL becomes 63.
- After the second router, TTL becomes 62.
- This continues until the datagram reaches its destination or its TTL becomes 0.
Purpose: It prevents datagrams from circulating endlessly in the network due to routing loops.
9. Protocol (8 Bits)
The Protocol field identifies the upper-layer protocol used in the data portion of the IP datagram.
- It occupies 8 bits.
- It identifies which protocol should receive the payload.
- It helps the destination device process the received data correctly.
Some common protocol numbers are:
| Protocol | Number |
|---|---|
| ICMP | 1 |
| TCP | 6 |
| UDP | 17 |
| GRE | 47 |
| ESP | 50 |
| AH | 51 |
For example:
- If the Protocol field contains 6, it indicates TCP.
- If it contains 17, it indicates UDP.
- If it contains 1, it indicates ICMP.
Purpose: It identifies the upper-layer protocol responsible for processing the payload.
10. Header Checksum (16 Bits)
The Header Checksum field is used to detect errors in the IP datagram header.
- It occupies 16 bits.
- It checks only the IPv4 header, not the actual data.
- Routers recalculate the checksum when relevant header fields change.
For example, when a router decreases the TTL value, the header changes. Therefore, the router must update the Header Checksum before forwarding the datagram.
Purpose: It helps detect errors in the IP header during transmission.
11. Source IP Address (32 Bits)
The Source IP Address field contains the IPv4 address of the device that originally sends the datagram.
- It occupies 32 bits.
- It identifies the sender.
- It helps the destination device identify where the datagram originated.
Example:
192.168.1.10
If a computer with this IP address sends a datagram, its address is included in the Source IP Address field.
Purpose: It identifies the source device of the IP datagram.
12. Destination IP Address (32 Bits)
The Destination IP Address field contains the IPv4 address of the device that should receive the datagram.
- It occupies 32 bits.
- It identifies the intended receiver.
- Routers use this address to forward the datagram toward its destination.
Example:
192.168.1.20
If a datagram is sent from 192.168.1.10 to 192.168.1.20, the destination address is stored in this field.
Purpose: It identifies the destination device to which the datagram is being transmitted.
13. Options (Variable Length)
The Options field provides additional information and special instructions for processing an IP datagram.
- It is optional.
- Its length can vary from 0 to 40 bytes.
- It provides additional routing and control information.
Some examples of IP options are:
- Record Route: Records the routers through which a datagram travels.
- Timestamp: Records timing information during transmission.
- Loose Source Routing: Allows the sender to specify some routers through which the datagram should pass.
- Strict Source Routing: Allows the sender to specify the exact route that the datagram must follow.
Purpose: It provides additional control and routing information when required.
14. Padding (Variable Length)
The Padding field is used to make the total IP header length a multiple of 32 bits (4 bytes).
- It is used when required by the Options field.
- It contains zero bits.
- It ensures proper alignment of the IP header.
For example, if the Options field does not end at a 32-bit boundary, padding bits are added to complete the required length.
Purpose: It ensures that the IP header length is properly aligned.
Size of an IP Datagram Header
The IPv4 datagram header has a variable size depending on whether optional fields are included.
| Component | Size |
|---|---|
| Minimum header size | 20 bytes |
| Maximum header size | 60 bytes |
| Options field | 0–40 bytes |
| Padding | 0–3 bytes |
| Maximum datagram size | 65,535 bytes |
The total length of an IP datagram is calculated using the following formula:
Example:
Suppose an IP datagram contains a header of 20 bytes and data of 1,000 bytes.
Therefore, the total length of the IP datagram is 1,020 bytes.
Working of an IP Datagram
The IP datagram header helps in transferring data from a source device to a destination device.
The working process involves the following steps:
1. Data Creation: The source device creates data that needs to be transmitted over the network.
2. Header Addition: The Internet Protocol adds a header containing the source address, destination address, packet length, TTL, and other information.
3. Packet Transmission: The IP datagram is passed to the data-link layer for transmission.
4. Routing: Routers examine the destination IP address and determine the next hop.
5. TTL Update: Each router decreases the TTL value by at least one and updates the header checksum.
6. Fragmentation: If required and permitted, a large datagram may be divided into smaller fragments.
7. Destination Delivery: The datagram reaches the destination device. If fragmentation occurred, the destination reassembles the fragments.
8. Data Processing: The destination device examines the Protocol field and passes the payload to the appropriate upper-layer protocol.
Thus, the IP datagram header helps in identifying, routing, and delivering data across interconnected networks.
Importance of IP Datagram Header
The IP datagram header plays an important role in network communication.
Its major functions are:
- Logical Addressing: It identifies the source and destination devices using IP addresses.
- Routing: It helps routers forward datagrams toward the correct destination.
- Fragmentation: It supports dividing large datagrams into smaller fragments when necessary and permitted.
- Error Detection: The Header Checksum helps detect errors in the IPv4 header.
- Loop Prevention: TTL prevents datagrams from circulating endlessly.
- Protocol Identification: It identifies upper-layer protocols such as TCP, UDP, and ICMP.
- Packet Size Identification: The Total Length field specifies the complete size of the datagram.