UDP checksum calculation is simple:
- treat data (payload, non-checksum fields in UDP header, some fields from IP header) as 16 bit integers
- sum them wrapping at 16 bits
- reverse each bit of the result
It's constructed in a way, that verification on receiving end is done by summing up all mentioned data and checksum and checking if result is equal to 0xffff
. So when calculating outer packet checksum you might want to skip summing fields related to inner packet, but add 0xffff
directly instead thus saving resources.
This is applicable only in case inner packet offset is divisible by 16 bit, so checksum calculation for outer packet and for inner packet uses the same integers within inner packet.