In this blog post, I will be explaining the calculation of the UDP checksum field with an example.
According to the RFC 768, UDP checksum field calculation includes the UDP header, Pseudo header, and the UDP Data.
UDP Header consists of sourcePort (16 bits),
destinationPort (16 bits), length (16 bits), and checksum
(16 bits). The sourcePort and destinationPort field values can be obtained directly. The length field is the sum of length
of the UDP header and the UDP data. The checksum field is initialized to zero.
Pseudo Header consists of sourceIP (32 bits),
destinationIP (32 bits), reserved (8 bits set to zero),
protocol (8 bits), and length (16 bits). The sourceIP and
destinationIP can be obtained directly.
The protocol field is transport layer protocol value (which is 0x11 for UDP).
The length field is the sum of length
of the UDP header and the UDP data.
UDP Data is the data part of a UDP segment.
Now that we have the necessary information, for the ethernet frame given below, let's calculate the UDP checksum field:
f8 bc 12 86 23 bf 50 9a 4c 1e bd 18 08 00 45 00
00 28 18 8f 40 00 40 11 8c 57 0a 72 40 b2 0a 72
40 49 13 88 13 88 00 14 00 00 48 65 6c 6c 6f 2c
20 77 6f 72 6c 64
From the above ethernet frame, the following fields can be deduced:
Ethernet Header:
destinationMAC = f8 bc 12 86 23 bfsourceMAC = 50 9a 4c 1e bd 18type = 08 00IP Header:
version and headerLength = 45DSCP and ECN = 00totalLength = 00 28identification = 18 8fflags and fragmentOffset = 40 00TTL = 40protocol = 11headerChecksum = 8c 57sourceIP = 0a 72 40 b2destinationIP = 0a 72 40 49UDP Header:
sourcePort = 13 88destinationPort = 13 88length = 00 14checksum = 00 00UDP Data:
48 65 6c 6c 6f 2c 20 77 6f 72 6c 64
Therefore, the UDP checksum segment:
13 88 13 88 00 14 00 00 0a 72 40 b2 0a 72 40 49
00 11 00 14 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64
Steps to calculate the UDP checksum:
13 88 13 88 00 14 00 00 0a 72 40 b2 0a 72 40 49
00 11 00 14 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64
1388 1388 0014 0000 0a72 40b2 0a72 4049
0011 0014 4865 6c6c 6f2c 2077 6f72 6c64
sum = 2DD72
sum = DD72 + 2 = DD74 (1101110101110100)
checksum = ~sum = 228B (0010001010001011)
Thus, the UDP checksum field value is 0x228B.