This tutorial explains the IPv4 header in detail. Learn what an IPv4 header is, how many fields an IPv4 header contains, what the meaning of each field is, and what the value is stored in each field.
An IP header is bits of information attached to each data packet that is transported in the computer network. This information usually includes addressing and routing details which makes it possible to reassemble the packets and have the original data at the destination.
There are two versions of the IP protocol: IPv4 and IPv6. Both versions use different IP headers for data packets. In this tutorial, we will discuss the IPv4 header in detail.
The following image shows the basic structure of the IPv4 header as defined in RFC 791.
The IPv4 header contains 13 fields. These fields are Version, Internet Header Length, Type of Service, Total Length, Identification, Flags, Fragment offset, Time-to-Live, Protocol, Header Checksum, Source address, Destination address, and Options. The following image shows how these fields are arranged in the IP header.
Let's understand these fields in detail.
This field sets the version of the IP protocol. As mentioned earlier, there are two versions of the IP protocol. If the value of this field is set to 4, then it indicates that the header belongs to the IPv4 protocol. The size of this field is 4 bits.
The size of this field is 4 bits. This field indicates the length of the IPv4 header. Not all IPv4 headers are equal in length. The length of the header depends on how many options are added. An option tells intermediate devices how packets should be forwarded or processed. Most IPv4 options are optional. Depending on the specific requirement, nodes can add options.
The length of the header is calculated in the number of 4-byte blocks. To calculate the length, the number of bytes is divided by 4. For example, if the header contains 20 bytes, the header length in the 4-byte blocks will be 5 (20/4). Similarly, if the value in this field is 10, then the length of the header will be 10 x 4 = 40 bytes.
If an IPv4 option is not an integral multiple of 4 bytes in length, the remaining bytes are filled through padding options. The minimum size of the IPv4 header is 20 bytes. The maximum size of the header including all options is 60 bytes.
This field is used to set the desired service expected by the packet for delivery through routers across the network. RFC 791 defines services for this field. The most common services are the precedence, delay, throughput, reliability, and cost characteristics. The size of this field is 8 bits.
RFC 2474 updates the definition of this field. It renames the original field name as the Differentiated Services (DS) field and defines the bits of this field into two separate groups. In the first group, it defines the first (high-order) 6 bits. In the second group, it defines the last (low-order) 2 bits.
The first 6 bits are used to mark, unmark, and classify packets for forwarding and routing. Nodes can prioritize packets based on the requirements of applications. For example, nodes may prioritize data packets of real-time applications (such as voice over IP and video) over other applications (such as email and file storage). Prioritized data packets take precedence over normal data packets in congested areas of the network. This feature is commonly known as QoS (Quality of Service).
The last 2 bits are used for ECN (Explicit Congestion Notification). The ECN allows an intermediate device to send a notification to the sender device if it is not able to forward the packet due to congestion. ECN is defined in RFC 3168.
This field specifies the total length of the packet. This length includes the length of the header and the length of the payload. By subtracting the header length from the total length, routers can calculate the length of the payload. The size of this field is 16 bits. Since a 16 bits field cannot store a value more than 65535, the maximum length of an IP packet can be 65535 bytes.
If the packet is large, the source node can fragment the packet. If the packet is fragmented all fragments retain the identification value. The destination node uses this value to reassemble the original packet from fragments. The size of this field is 16 bits.
This field is used to enable fragmentation. The size of this field is 3 bits. From these, only two bits are defined. The first bit indicates whether the packet can be fragmented or not. The second bit indicates whether more fragments follow the current fragment.
If the fragment is done, this field is used to indicate the position of the fragment relative to the beginning of the payload. The size of this field is 13 bits.
The size of this field is 8 bits. This field is used to discard the undeliverable packets. The original specification defines this field as a time counter. Intermediate routers determine the length of time required in seconds to forward the packet and decrement this time accordingly. This specification was updated later.
In the modern specification, the sending node sets the maximum number of links on which the packet can travel before being discarded. When the packet crosses a router, the router decrements the TTL value by 1. If the TTL value equals 0 before reaching the destination, the packet is discarded and an ICMP Time Exceeded message is sent to the source of the packet.
The size of this field is 8 bits. This field specifies the upper-layer protocol that will receive the payload of the packet on the destination node. For example, if this field contains a decimal value 17, then the Internet layer of the destination node will transfer the payload to the UDP protocol.
The size of this field is 16 bits. This field provides a checksum on the header only. Since the payload contains its own checksum, the payload is not included in the checksum calculation. Intermediate routers that receive and forward the packet calculate and verify the checksum and discard the packet if checksum verification fails. Since a router decrements the TTL value by 1 before forwarding the packet, the header checksum value is recomputed at each hop between the source and destination nodes.
The size of this field is 32 bits. This field stores the IPv4 address of the sending device.
The size of this field is also 32 bits. This field stores the IPv4 address of the destination device.
This field stores IPv4 options. The size of this field is a multiple of 32 bits. If an option is not 32 bits in the length, it uses padding options in the remaining bits to make the header an integral number of 4-byte blocks.
That's all for this tutorial. In the next tutorial, we will discuss the IPv6 header in detail. If you like this tutorial, please share this tutorial with friends through your favorite social network.