protocol_ip

checksum

A check sum is basically a value that is computed from data packet to check its integrity. Through integrity, we mean a check on whether the data received is error free or not. This is because while traveling on network a data packet can become corrupt and there has to be a way at the receiving end to know that data is corrupted or not. This is the reason the checksum field is added to the header. At the source side, the checksum is calculated and set in header as a field. At the destination side, the checksum is again calculated and crosschecked with the existing checksum value in header to see if the data packet is OK or not.

This article aims to explain how checksum is calculated, the method is used to calculate checksum. both IP and TCP uses the same method to calculate its checksum, IP checksum is only for IP header, while TCP checksum is for Pseudo IP header + TCP header + TCP payload.

IP header checksum

method:
we divide the IP header is 16 bit words and sum each of them up and then finally do a one’s compliment(补集) of the sum then the value generated out of this operation would be the checksum.

Example
IP header as example. b1e6 is the checksum, let’s see how it’s calculated.
4500 003c 1c46 4000 4006 b1e6 ac10 0a63 ac10 0a0c

4500
003c

sum: 453c

1c46

sum: 6182

4000

sum: a182

4006

sum: e188

ac10

sum: 8d99 larger than 16 bits, overflow, ignore overflow and + 1

0a63

97fc

ac10

sum: 440d larger than 16 bits, overflow, ignore overflow and + 1

0a0c

sum: 4e19

checksum: b1e6 (b1e6 + 4e19 == ffff)