Calculation of TCP Checksum


The Transmission Control Protocol (TCP) checksum is a method used to detect errors in TCP packets. The checksum is calculated by taking the binary value of all the fields in the TCP header and the data, treating them as a large integer, and then performing a bit-wise ones complement on that integer.

To calculate the TCP checksum, the following steps are performed −

  • The checksum field in the TCP header is set to zero.

  • The binary values of the source and destination IP addresses, the reserved field, the protocol field (set to 6 for TCP), the TCP length, and the TCP data are concatenated.

  • The resulting binary value is treated as a large integer and a bit-wise ones complement is taken.

  • The resulting value is then converted to 16-bit binary and placed in the checksum field of the TCP header.

Here is an example of how the TCP checksum might be calculated for a packet −

Suppose the source IP address is 192.168.0.1, the destination IP address is 192.168.0.2, the reserved field is 0, the protocol field is 6 for TCP, the TCP length is 40 bytes, and the TCP data is "Hello, World!".

  • The checksum field in the TCP header is set to zero.

  • The binary values of the source and destination IP addresses (11000000 10101000 00000000 00000001 and 11000000 10101000 00000000 00000010, respectively), the reserved field (0000000000000000), the protocol field (00000110), the TCP length (00011000), and the TCP data (01001000 01100101 01101100 01101100 01101111 00101100 00101111 01110111 01101111 01110010 01101100 01100100 00100001) are concatenated.

  • The resulting binary value is treated as a large integer and a bit-wise ones complement is taken (in this example, let's say the result is 01010101 01010101).

  • The resulting value is then converted to 16-bit binary and placed in the checksum field of the TCP header (01010101 01010101).

It's important to note that the checksum is calculated over the TCP segment, and the checksum field is computed before the segment is sent, and verified after the segment is received. The receiver computes the checksum again and compares it to the checksum field. If the computed and received checksum values match, it is assumed that the segment was received without error; otherwise, the segment is discarded.

This is a basic overview of how the TCP checksum is calculated. It is an important technique to make sure the data integrity while sending over the network, which is critical in maintaining secure and reliable communication.

TCP Header

The Transmission Control Protocol (TCP) header is a set of fields that are included at the beginning of a TCP segment. The header contains information that is used to control the establishment, maintenance, and termination of a TCP connection. The TCP header is typically 20 bytes in length, but it can be up to 60 bytes if options are included.

The fields in the TCP header include −

  • Source Port − 16-bit field that identifies the port on the source host where the segment originated.

  • Destination Port − 16-bit field that identifies the port on the destination host where the segment is being sent.

  • Sequence Number − 32-bit field that identifies the byte number of the first data byte in the current segment.

  • Acknowledgment Number − 32-bit field that acknowledges receipt of all bytes up to the value in this field.

  • Data Offset − 4-bit field that indicates the number of 32-bit words in the TCP header.

  • Reserved − 6-bit field that is reserved for future use.

  • Flags − 6-bit field that contains various control flags, including the URG, ACK, PSH, RST, SYN, and FIN flags.

  • Window − 16-bit field that indicates the size of the receive window.

  • Checksum − 16-bit field that is used to detect errors in the TCP segment.

  • Urgent Pointer − 16-bit field that, if the URG flag is set, indicates the byte number of the last byte of urgent data in the current segment.

And there are some optional fields like −

  • TCP options − variable-length field that can be used to include additional information in the header.

The combination of the source IP address, source port, destination IP address, and destination port forms a unique identifier for each TCP connection, known as a socket. The sequence and acknowledgement numbers, along with the window and flags fields, are used to control the flow of data during a TCP connection.

TCP uses the fields in the header to provide a reliable, connection-oriented communication service. It guarantees that data sent is received and that it is received in the correct order. This is why TCP is commonly used in applications that require reliable data transfer, such as file transfers and email.

Examples

The TCP checksum is calculated by taking the binary value of all the fields in the TCP header and the data, treating them as a large integer, and then performing a bit-wise ones complement on that integer. Here are a couple of examples of how the TCP checksum might be calculated −

Example 1

Suppose the source IP address is 192.168.1.1, the destination IP address is 192.168.1.2, the reserved field is 0, the protocol field is 6 for TCP, the TCP length is 40 bytes, and the TCP data is "Hello, World!".

  • The binary values of the source IP address (11000000 10101000 00000001 00000001), destination IP address (11000000 10101000 00000001 00000010), the reserved field (0000000000000000), the protocol field (00000110), the TCP length (00011000), and the TCP data (01001000 01100101 01101100 01101100 01101111 00101100 00101111 01110111 01101111 01110010 01101100 01100100 00100001) are concatenated.

  • The resulting binary value is treated as a large integer and a bit-wise ones complement is taken.

  • Let's assume that the result of step 2 is 10101010 10101010 (example number)

  • The resulting value is then converted to 16-bit binary and placed in the checksum field of the TCP header (10101010 10101010).

Example 2

Suppose the source IP address is 172.16.1.1, the destination IP address is 172.16.1.2, the reserved field is 0, the protocol field is 6 for TCP, the TCP length is 100 bytes and the TCP data is "TCP checksum calculation example."

  • The binary values of the source IP address (10101100 00010000 00000001 00000001), destination IP address (10101100 00010000 00000001 00000010), the reserved field (0000000000000000), the protocol field (00000110), the TCP length (01100100) and the TCP data (01010100 01100011 01110000 01100011 01100011 01100101 01110011 01110100 01101001 01101110 01101011 01101001 01101110 01101111 01101110 01101001 01101110 01101011 01101001 01101110 01101011 01101001 01101110 01101011 01101001) are concatenated.

  • The resulting binary value is treated as a large integer and a bit-wise ones complement is taken.

  • Let's assume that the result of step 2 is 11110011 11110011 (example number)

  • The resulting value is then converted to 16-bit binary and placed in the checksum field of the TCP header (11110011 11110011).

It's worth mentioning that the above examples are for illustration purposes and may not reflect the real result, since it depends on the actual data that's in the packet. Also these are examples of how the checksum is calculated and not the actual packets.

Updated on: 08-Feb-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements