UDP maximum packet size

2019-06-13 19:46发布

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

1条回答
Melony?
2楼-- · 2019-06-13 20:28

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.

查看更多
登录 后发表回答