Calculation of TCP Checksum
In this article, we will learn a good concept of how the TCP/UDP checksum is calculated.
When we receive data from the application it is broken into smaller data parts as the whole data from the application cannot be sent through the network to the receiver host.
The protocol we use in OSI in the Transport layer is TCP. So, after breaking the data from the application layer into smaller parts. This broken part form the body of the TCP.
The TCP header usually varies from 20 Bytes(with no bits of option fields being used) to 60 Bytes(with all bits of options field being used).
It has fields like Source and Destination Port addresses, urgent pointer, Checksum, etc.
In this article, we are only concerned about the CheckSum field of the TCP.
The CheckSum of the TCP is calculated by taking into account the TCP Header, TCP body and Pseudo IP header.
Now, the main ambiguity that arises that what is how can checksum be calculated on IP header as IP comes into the picture in the layer below the Transport Layer.
In simple terms, it means that we are in Transport Layer and the IP data packet is created in Network Layer.
Then how can we estimate the size of the IP header from the Transport because the guess/estimation would be definitely wrong and thus there would be no point in calculating the checksum on a field which is wrong at the beginning itself?
The error checking capability of TCP/UDP in Transport Layer takes help from the network layer for proper error detection.
But the important concept to note here is that we actually don’t use the IP header rather we use a part of the IP header.
To overcome all these errors and increase error checking capability we use Pseudo IP header.
Pseudo IP header:
The pseudo-header is not an IP header rather it is a part of the IP header. We directly don’t use the IP header because in IP header there are many which would be continuously changing when then packets move along the network . Thus a part of the IP header is taken into account which don’t change as the IP packet moves in the network.
The Fields of the Pseudo IP header are:-
- IP of the Source
- IP of the Destination
- TCP/UDP segment Length
- Protocol (stating the type of the protocol used)
- Fixed of 8-bits
So, the total size of the pseudo header(12 Bytes) = IP of the Source (32 bits) + IP of the Destination (32 bits) +TCP/UDP segment Length(16 bit) + Protocol(8 bits) + Fixed 8 bits
An important concept should be noted that this Pseudo header is created in the Transport layer for calculation and after the calculation is done the Pseudo header is discarded. And the checksum is calculated by using the usual checksum method.
So, this Pseudo Header is not transported across the network, rather the actual IP header which is formed in Network Layer is transported.
So, the TCP checksum includes the:-
1. Pseudo IP header 2. TCP header 3. TCP body
After the calculation of the checksum using the above 3 fields, the checksum result is placed in the checksum field of the TCP header.
As it is already stated that the Pseudo header is discarded and is not transported to the destination host then how does the Destination host check if the data is received correctly or not. Thus, the pseudo-header is once again created in the Transport layer of the Destination host and then again the checksum is calculated in the Transport Layer of Destination Host and finally, the checksum is calculated by the usual method of checksum and is confirmed if the data received is correct or not.
Why IP header error checking two times is needed ?
The IP header is checked two times the first time in the Transport layer and second time in Network Layer. The IP header is checked two times because double checking ensures that any error in the IP header can be detected with proper accuracy.