IPv4 Packet Header

Hi Ganesh,

The header length can be a bit confusing. It specified the length of the IP header but we only have 4 bits. With 4 bits, you can create values between 0 and 15, that’s it.

How it works is that each bit represents a 32-bit increment. An IP header has a minimum length, which is 20 bytes.

1 byte = 8 bits so:

20 bytes = 160 bits

160 bits / 32-bit increments = 5

So by setting the header length to 5, we know that the length of the IP header is 20 bytes.

The total length and Identification are simple, these are both 16 bits so we can store values from 0 - 65535 here.

Let me explain the total length, identification, ip flags, and fragment offset with an example.

Let’s say this is our original IP packet:

Sequence Identifier Total Length Don’t Fragment More Fragments Fragment Offset
0 321 5140 0 0 0

This packet is 5140 bytes but that includes a 20 byte IP header. The data portion is 5120 bytes. This packet gets fragmented. Here are our fragments:

Sequence Identifier Total Length Don’t Fragment More Fragments Fragment Offset
0-0 321 1500 0 1 0
0-1 321 1500 0 1 185
0-2 321 1500 0 1 370
0-3 321 700 0 0 555

How did we come up with these values?

The identifier remains the same in all fragments. The first three fragments have the “more fragments” switch enabled.

* The first fragment is 1500 bytes but that includes 20 bytes for the IP header so the “data portion” is 1480 bytes.
* The second fragment is also 1500 bytes and the offset is 185:

  • 185 x 8 = 1480
  • This means the data portion of this fragment starts 1480 bytes into the original IP packet.
    * The third fragment is also 1500 bytes and the offset is 370:
  • 370 x 8 = 2960
  • This means the data portion of this fragment starts 2960 bytes into the original IP packet.
    * The fourth (last) fragments is 700 bytes and the offset is 555:
  • 555 x 8 = 4440
  • This means the data portion of this fragment starts 4440 bytes into the original IP packet.

Once we receive the last fragment, we can tell what the size of the original IP packet is. The offset is 555 which means we start 4440 bytes into the original IP packet.

Now take the data bytes from the last fragment: 700 bytes - 20 bytes for the IP header = 680 bytes.

4440 + 680 = 5120 bytes. That’s the length of the data portion of the original IP packet.

I hope this helps!

Rene

2 Likes