Does UDP Length Include Header and Data?
The User Datagram Protocol (UDP) Length field specifies the total size of the entire UDP datagram in bytes, which includes both the 8-byte UDP header and the encapsulated data (payload). This article breaks down the structure of the UDP Length field, how its value is calculated, its minimum and maximum limits, and how it interacts with the underlying Internet Protocol (IP) layer.
The Structure of the UDP Header
The UDP header is fixed at 8 bytes (64 bits) in size and consists of four 2-byte (16-bit) fields:
- Source Port (2 bytes): The port number of the sender.
- Destination Port (2 bytes): The port number of the receiver.
- Length (2 bytes): The total length of the UDP header plus the UDP data.
- Checksum (2 bytes): An optional (in IPv4) or mandatory (in IPv6) error-checking field covering the header, data, and a pseudo-IP header.
Calculation and Value Range
Because the Length field counts both the header and the payload, its value is computed as:
\[\text{UDP Length} = 8\text{ bytes (Header)} + \text{Payload Length (Bytes)}\]
- Minimum Value: When a UDP packet carries zero bytes of payload, the Length field is set to 8 (representing just the 8-byte header). A UDP packet cannot have a length value less than 8.
- Maximum Value: As a 16-bit field, the theoretical
maximum value is 65,535 bytes. However, the practical
limit is lower because the UDP datagram is encapsulated inside an IP
packet:
- In standard IPv4, the maximum IP packet size is 65,535 bytes. Subtracting the minimum 20-byte IPv4 header and the 8-byte UDP header leaves a maximum UDP data payload of 65,507 bytes.
- In IPv6, standard packets support payloads up to 65,535 bytes, but IPv6 Jumbograms can support larger payloads using a UDP Length value set to zero to signal the use of the IPv6 Jumbo Payload option.
Comparison with TCP
Unlike TCP, which uses a “Data Offset” field to define only the size of the TCP header, UDP explicitly states the combined length of the header and data. While this creates some redundancy with the “Total Length” field found in the IPv4 header, it ensures that the UDP layer can independently verify the boundary of its own datagram without relying solely on network-layer calculations.