The UDP checksum is calculated to verify the integrity of the UDP header and data. It is optional in IPv4 but mandatory in IPv6. Here is a step-by-step process for calculating the UDP checksum:

  1. Pseudo-header Preparation: Construct a pseudo-header, which is not actually transmitted but used in the checksum calculation. It includes the following fields:
  • Source IP address (4 bytes for IPv4, 16 bytes for IPv6)
  • Destination IP address (4 bytes for IPv4, 16 bytes for IPv6)
  • Zero (1 byte for IPv4, 3 bytes for IPv6)
  • Protocol (1 byte, which is 17 for UDP)
  • UDP length (2 bytes, the length of the UDP header and data)
  1. UDP Header and Data: The actual UDP header and the data are then appended to the pseudo-header for checksum calculation.
  2. Checksum Calculation:
  • Combine the pseudo-header, UDP header, and UDP data.
  • If the length of this combination is odd, pad it with a zero byte at the end to make it even.
  • Calculate the sum of all 16-bit words in this combination, adding any overflow bits back into the lower 16 bits.
  • Take the one’s complement (bitwise NOT) of the result.
  1. Placement: Place the resulting checksum in the UDP header checksum field.

Here’s a more detailed breakdown:

Pseudo-header Construction (IPv4)
+---------------------+---------------------+
|     Source IP       |  Destination IP     |
+---------------------+---------------------+
|  Zero   | Protocol  |      UDP Length     |
+---------------------+---------------------+
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
Pseudo-header Construction (IPv6)
+---------------------------------------------------------------+
|                       Source IP Address                       |
+---------------------------------------------------------------+
|                    Destination IP Address                     |
+---------------------------------------------------------------+
|           UDP Length            |  Zero    | Protocol         |
+---------------------------------------------------------------+
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
UDP Header
+---------------------+---------------------+
|   Source Port       |  Destination Port   |
+---------------------+---------------------+
|     UDP Length      |     UDP Checksum    |
+---------------------+---------------------+
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
Example Calculation (IPv4)

Suppose we have the following:

  • Source IP: 192.168.1.1
  • Destination IP: 192.168.1.2
  • Source Port: 12345
  • Destination Port: 24672
  • Data: “Hello”
  1. Pseudo-header:
Source IP:        C0 A8 01 01
Destination IP:   C0 A8 01 02
Zero:             00
Protocol:         11 (for UDP)
UDP Length:       00 13 (19 bytes, including UDP header and data)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  1. UDP Header and Data:
Source Port:      30 39 (12345)
Destination Port: 60 00 (24672)
Length:           00 13 (19 bytes)
Checksum:         00 00 (initially set to 0 for calculation)
Data:             48 65 6C 6C 6F ("Hello")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  1. Combining and Padding:
Pseudo-header:    C0 A8 01 01 C0 A8 01 02 00 11 00 13
UDP Header+Data:  30 39 60 00 00 13 00 00 48 65 6C 6C 6F
Combined:         C0 A8 01 01 C0 A8 01 02 00 11 00 13 30 39 60 00 00 13 00 00 48 65 6C 6C 6F
  • 1.
  • 2.
  • 3.
  1. Calculating the Sum:
C0A8 + 0101 + C0A8 + 0102 + 0011 + 0013 + 3039 + 6000 + 0013 + 0000 + 4865 + 6C6C + 6F00 (padded with zero)
  • 1.
  1. Sum: Add all 16-bit words together:
= 7E00 (sum)
  • 1.
  1. One’s Complement:
Checksum: 81FF
  • 1.
  1. Final UDP Header:
Source Port:      30 39 (12345)
Destination Port: 60 00 (24672)
Length:           00 13 (19 bytes)
Checksum:         81 FF
  • 1.
  • 2.
  • 3.
  • 4.

This gives the UDP checksum of 81FF for the given data.