Checksum in layer 4

To compute the checksum for a layer 4 header (either TCP or UDP):

  • You first construct the header with an all zeros Checksum.
  • Then you append the pseudo-header to the message.
  • You break the message down into 16-bit segments potentially adding 0’s if needed.
  • Then compute the ones complement addition off all segments. If this is all 0’s it is sent as all 1’s. An all 0 header means that the checksum was not computed.
  • Fill this in as the checksum component. (Now if you perform the ones complement addition of all the 16-bit segments you should end up with all 1’s.)
  • Strip the pseudo-header off the segment.

To verify the checksum on the receivers end they simply need to add all 16-bit segments and check it results in all 1’s. This makes it robust to a single bit flip whilst also being robust to most double bit flips if this happens in the same position in the 16-bit segments it will not be noticed.