Calculation of TCP Checksum (original) (raw)

Last Updated : 12 Jul, 2025

In network communication ensuring data integrity is very important. So to detect errors that may occur during transmission the Transmission Control Protocol which is an important part of the Internet protocol employs a checksum mechanism. This checksum, calculated by the sender and verified by the receiver, acts as a protection against data corruption.

By calculating a checksum value based on the TCP header and data, including a pseudo-header with essential IP information, the sender ensures data integrity. The receiver recalculates the checksum upon receiving the packet. A match confirms data accuracy, while a mismatch triggers retransmission, securing reliable data delivery across the network. In this article, we will learn a good concept of how the TCP/UDP checksum is calculated.

Understanding the TCP Checksum

The **TCP checksum is a method used to detect errors in data transmitted over a network. When data is sent in a TCP segment, the sender calculates a checksum value that represents the data. This value is then included in the segment, and the receiver recalculates the checksum when the data arrives and compares it to the value sent by the sender. If they match then the data is assumed to be error-free, if they don't then it indicates that there was a transmission error, and the data is discarded or retransmitted. The TCP checksum helps ensure that data remains accurate during transmission.

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 is how can checksum be calculated on the IP header as IP comes into the picture in the layer below the Transport Layer. In simple terms, it means that we are in the Transport Layer and the IP data packet is created in Network Layer.

**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. Important concept to note here is that we use a part of the IP header which don't change as the IP packet moves in the network and to overcome all these errors and increase error checking capability we use a **Pseudo IP header. We can 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 the Transport Layer takes help from the network layer for proper error detection.

The Fields of the Pseudo IP header are:

  1. IP of the Source
  2. IP of the Destination
  3. TCP/UDP segment Length
  4. Protocol (stating the type of the protocol used)
  5. 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

Pseudo IP Header

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.

**The TCP checksum includes the following:

**1. Pseudo IP header
**2. TCP header
**3. TCP body

IP Header

**Step-by-Step Guide to Calculate the TCP Checksum

**To calculate the TCP checksum, follow these basic steps:

  1. **Prepare the Data: Combine the TCP header and the data to be transmitted. Make sure to include any necessary padding for alignment.
  2. **Divide the Data: Split the data into 16-bit words (2-byte parts). If the last part is incomplete, pad it with zeros.
  3. **Sum the Words: Add up all the 16-bit words. If the sum exceeds 16 bits, wrap the overflow back around to the lower 16 bits.
  4. **Invert the Sum: After adding all the words, take the one’s complement (invert all the bits) of the sum. This inverted value is the checksum.
  5. **Attach the Checksum: The calculated checksum is then included in the TCP header for transmission.

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.

Common Mistakes in TCP Checksum Calculation and Ways to Avoid Them

**Some common mistakes in calculating the TCP checksum include:

Calculating TCP Checksums in Real-World Scenarios

**Here’s a practical examples of calculating a TCP checksum:

1. **Data Preparation: Let's say we need to calculate the checksum for the following 16-bit words of TCP data:

0x1234, 0x5678, 0x9abc, 0xdef0

2. **Sum the Words: Add them up:

0x1234 + 0x5678 + 0x9abc + 0xdef0 = 0x2468c

3. **Handle Overflow: Since the sum is larger than 16 bits, we take the overflow (the extra bits beyond 16 bits) and add it back to the lower 16 bits:

0x2468c → 0x468 + 0x2 = 0x46a

4. **Invert the Sum: Invert all the bits of the sum (one’s complement of 0x46a):

1's complement of 0x46a = 0xb95

5. **Final Checksum: The checksum is 0xb95, which is added to the TCP header for transmission.

**Comparative Analysis: TCP Checksum vs. UDP Checksum

While both **TCP and **UDP use checksums to detect errors in transmitted data, there are key differences: