UDP maximum packet size

15,452

UDP datagrams are encapsulated inside IP packets. If you are using 20 as the IP packet header size then you mean IPv4, and the minimum IPv4 header size is 20. IPv4 has a theoretical maximum packet size of 65,535 (a 16-bit total length field in the IPv4 header), but the real IPv4 maximum packet size will be the MTU on the link. This size includes the IPv4 header and the IPv4 payload, which will be the UDP datagram, including the UDP header and UDP payload.

Since the UDP datagram is the data of the IPv4 datagram, and the entire length of the IPv4 datagram, including the IPv4 header, is a 16-bit Total Length field of the IPv4 header, the entire IPv4 packet, including the IPv4 header is a maximum of 65,535 octets. This is detailed in the definition of IPv4, RFC 971 Internet protocol, Section 3.1 Internet Header Format:

3.1. Internet Header Format

A summary of the contents of the internet header follows:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |         Header Checksum       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Source Address                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Destination Address                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The definition of the Total Length field is:

Total Length: 16 bits

Total Length is the length of the datagram, measured in octets, including internet header and data. This field allows the length of a datagram to be up to 65,535 octets. Such long datagrams are impractical for most hosts and networks. All hosts must be prepared to accept datagrams of up to 576 octets (whether they arrive whole or in fragments). It is recommended that hosts only send datagrams larger than 576 octets if they have assurance that the destination is prepared to accept the larger datagrams.

The number 576 is selected to allow a reasonable sized data block to be transmitted in addition to the required header information. For example, this size allows a data block of 512 octets plus 64 header octets to fit in a datagram. The maximal internet header is 60 octets, and a typical internet header is 20 octets, allowing a margin for headers of higher level protocols.

That means you must subtract the IPv4 header length from the maximum of 65,535 to arrive at the maximum UDP datagram length, which includes the 8 octet UDP header.

Share:
15,452
Yonatan Kreiner
Author by

Yonatan Kreiner

Updated on June 04, 2022

Comments

  • Yonatan Kreiner
    Yonatan Kreiner almost 2 years

    I checked the maximum UDP packet size and saw it is 65507 bytes of data.
    Which is 65535-8 (udp headers) - 20 (ip headers).
    The UDP length header is 2 bytes long which is 65535 limit.
    I understand that we substract 8 from it because its the size of the UDP header but if it is always 8 why do we need to substract it.
    Same question for the 20 of the ip headers length.

    Thank you

  • Yonatan Kreiner
    Yonatan Kreiner about 7 years
    So we substract from 65535 of the udp length 8 so it could fit in the ip total length header. But why the 20?
  • Ron Maupin
    Ron Maupin about 7 years
    No. The maximum IPv4 packet length of 65,6535 minus the IPv4 header length (minimum 20, maximum 60) gives you the maximum UDP datagram length. Then, you subtract the UDP header size of 8 to get the maximum UDP payload size. This is the theoretical maximum, but the real maximum size will depend on the link MTU, and that will not be anything near the theoretical maximum.
  • Ron Maupin
    Ron Maupin about 7 years
    @YonatanKreiner, in reality, you probably don't want to send anything more than about 500 bytes or so in a UDP datagram because UDP is connectionless and unreliable.
  • Yonatan Kreiner
    Yonatan Kreiner about 7 years
    I get that I need to send small packages in reality. But I still cant see why udp length substract the 20. It substract 8 for the ip payload length to fit in the ip total header am I right? If I am so why the 20 substraction?
  • Ron Maupin
    Ron Maupin about 7 years
    Since the entire IPv4 packet size has a theoretical limit of 65,535, including the IPv4 header (minimum 20 bytes, maximum 60 bytes) and payload (UDP datagram), you must subtract that 20 to 60 bytes of the IPv4 header to get the maximum UDP datagram size. The UDP datagram is the payload of the IPv4 header. I really don't know how else to explain it. I have pointed this out several times.
  • Yonatan Kreiner
    Yonatan Kreiner about 7 years
    Yes the UDP datagram is the ip payload but Why does the ip total length header includes its headers as well? If the ip headers length will be 60 so the max UDP length will be 65477?
  • Ron Maupin
    Ron Maupin about 7 years
    The definition for IPv4, RFC 791, Internet Protocol, defines the Total Length field in the IP header: "Total Length is the length of the datagram, measured in octets, including internet header and data. This field allows the length of a datagram to be up to 65,535 octets."
  • Yonatan Kreiner
    Yonatan Kreiner about 7 years
    I read it and I understand it now. One last question is why they decided that total length will include the headers length if we already have it in headers length? It just make us lose another 20 to 60 bytes that could be payload
  • Ron Maupin
    Ron Maupin about 7 years
    You would need to ask the authors. In particular, you could ask Vint Cerf. Remember that back then, 16-bit CPUs were the norm, and WAN links were fairly unreliable, so the limit was something that more than anyone would need. The same sort of logic was applied to the IPv4 address length, but that proved to be far too small, which is why IPv6 is now necessary.