250

I've read a number of articles about UDP packet sizes but have been unable to come to a conclusion on whats correct.

A number of services restrict the largest UDP packet to 512 bytes (like dns)

Given the minimum MTU on the internet is 576 , and the size of the IPv4 header is 20 bytes, and the UDP header 8 bytes. This leaves 548 bytes available for user data

Would I be able to use packets up to the size of 548 without packet fragmentation? Or is there something the creators of DNS knew about, and that why they restricted it to 512 bytes.

Could I even go higher than 548 bytes safely?

2
  • 13
    Its a slighlty different question. I'm asking what is the largest packet I can send over the internet (without any knowledge of the other networks, or probing) which is not going to have fragmentation. Essentially the maximum safe size, that will work on evereything without having to worry about probing the connection.
    – K M
    Commented Jul 8, 2009 at 15:58
  • 2
    You can't eliminate the possibility of fragmentation, but this doesn't make things less safe. If a fragment is dropped, it's the same as if the whole packet was dropped, which happens with UDP anyway. Unsafe would be if a packet exceeded the minimum size that routers were required to support, and was thus not guaranteed to be deliverable (versus guaranteed to be delivered). This is where the 512-byte figure comes in.
    – Beejor
    Commented Feb 29, 2016 at 11:08

13 Answers 13

161

It is true that a typical IPv4 header is 20 bytes, and the UDP header is 8 bytes. However it is possible to include IP options which can increase the size of the IP header to as much as 60 bytes. In addition, sometimes it is necessary for intermediate nodes to encapsulate datagrams inside of another protocol such as IPsec (used for VPNs and the like) in order to route the packet to its destination. So if you do not know the MTU on your particular network path, it is best to leave a reasonable margin for other header information that you may not have anticipated. A 512-byte UDP payload is generally considered to do that, although even that does not leave quite enough space for a maximum size IP header.

3
  • 47
    Just to be clear: having a small size to avoid fragmentation does not make delivery of the packet "Safe", there are still an infinite amount of possibilities making delivery unreliable such as dog ate my network cable. That said; having less fragments makes delivery "safer" because if there were more than one and any one of those never made it - the whole packet (datagram) is dropped by UDP.
    – markmnl
    Commented Jan 5, 2013 at 9:28
  • 6
    For purposes of a question one would presume to use the posters definition of 'safe', not some definition in some standards book that they have never seen.
    – Astara
    Commented Dec 22, 2018 at 2:00
  • 2
    Have real-world routers been known to drop UDP packets instead of fragmenting them?
    – user20574
    Commented Nov 19, 2019 at 11:04
101

The maximum safe UDP payload is 508 bytes. This is a packet size of 576 (the "minimum maximum reassembly buffer size"), minus the maximum 60-byte IP header and the 8-byte UDP header.

Any UDP payload this size or smaller is guaranteed to be deliverable over IP (though not guaranteed to be delivered). Anything larger is allowed to be outright dropped by any router for any reason. Except on an IPv6-only route, where the maximum payload is 1,212 bytes. As others have mentioned, additional protocol headers could be added in some circumstances. A more conservative value of around 300-400 bytes may be preferred instead.

The maximum possible UDP payload is 67 KB, split into 45 IP packets, adding an additional 900 bytes of overhead (IPv4, MTU 1500, minimal 20-byte IP headers).

Any UDP packet may be fragmented. But this isn't too important, because losing a fragment has the same effect as losing an unfragmented packet: the entire packet is dropped. With UDP, this is going to happen either way.

IP packets include a fragment offset field, which indicates the byte offset of the UDP fragment in relation to its UDP packet. This field is 13-bit, allowing 8,192 values, which are in 8-byte units. So the range of such offsets an IP packet can refer to is 0...65,528 bytes. Being an offset, we add 1,480 for the last UDP fragment to get 67,008. Minus the UDP header in the first fragment gives us a nice, round 67 KB.

Sources: RFC 791, RFC 1122, RFC 2460

13
  • 3
    Any UDP packet, by default, is considered "_U_nreliable". The only safe UDP packet size that you could expect to receive would be 1, unfragmented packet. If you want "safe" packets, use a packet protocol on top of TCP.
    – Astara
    Commented Jun 29, 2016 at 1:34
  • 48
    @Astara True, by nature UDP is unreliable. But the question is whether or not a packet of a given size is guaranteed to be deliverable, not guaranteed to be delivered. Packets over a certain size can be (and are) dropped by any router for any reason, whereas smaller ones must be best-effort handled by all routers, according to industry standards. So "safe" in this case means "will my car fit under the bridge" and not "will my car get stuck in traffic".
    – Beejor
    Commented Aug 5, 2016 at 1:28
  • 4
    I recommend to stop repeating what some random guy said and check the facts, because UDP is actually quite reliable. BTW I have safe packets on top of UDP without the unnecessary overhead of TCP. openmymind.net/How-Unreliable-Is-UDP Commented Nov 1, 2018 at 21:11
  • 11
    UDP is not "unreliable" because of the amount of dropped packets, but because packets can be (and are) dropped. You cannot "rely" on any specific packet arrival, order, or confirmation. Data is fragile, and it's like saying car steering that works 99% of the time, and 89% in the right direction, is reliable. Not that UDP isn't great for many things, just that it requires you to basically write your own version of "TCP" atop it. Here's a fascinating real-world case in the game dev world (though a bit outdated): gamasutra.com/view/feature/131781
    – Beejor
    Commented Nov 2, 2018 at 1:13
  • 3
    Why are you saying "65,536 maximum number of fragments"? IP packets include "fragment offset" which is 2 bytes long. This allows 44 fragments of 1480 byte, and a final fragment of 425 byte long.
    – Ali Tou
    Commented Aug 21, 2020 at 13:43
65

The theoretical limit (on Windows) for the maximum size of a UDP packet is 65507 bytes. This is documented here:

The correct maximum UDP message size is 65507, as determined by the following formula: 0xffff - (sizeof(IP Header) + sizeof(UDP Header)) = 65535-(20+8) = 65507

That being said, most protocols limit to a much smaller size - usually either 512 or occasionally 8192. You can often go higher than 548 safely if you are on a reliable network - but if you're broadcasting across the internet at large, the larger you go, the more likely you'll be to run into packet transmission problems and loss.

9
  • 50
    A Microsoft link is not a normative reference. The RFCs are the normative reference; and what you have quoted applies to IPv4 only.
    – user207421
    Commented Jul 22, 2013 at 9:54
  • 2
    Just because MS allows it doesn't mean it's always a good idea, since intermediate routers, etc. might be forced to fragment larger packet sizes (as you mentioned).
    – rogerdpack
    Commented Nov 15, 2013 at 13:55
  • 2
    @EJP They don't explain it clearly on the Microsoft link, but it appears to be a necessary consequence of IPv4: The IPv4 total length field is 16 bits, and that value must include the length of the IP header and the length of the UDP header.
    – jtpereyda
    Commented Oct 13, 2016 at 22:18
  • 2
    @ChristianStewart Max UDP packet size can be as large as 65507. If your router or card doesn't support such size it will have to fragment the packet under the covers in order to comply with the max packet size defined by IPv4 (which is 64kb minus IP and UDP header sizes). en.wikipedia.org/wiki/User_Datagram_Protocol Commented Nov 1, 2018 at 21:08
  • 2
    @"Reed Copsey" -- the original poster did not ask for the maximum allowed size, but the largest size he could use and reasonably expect no fragmentation. I didn't get that he asked for a guarantee of non-fragmentation either, Just a rough value he could use in his algorithm. Either way, you didn't answer the question.
    – Astara
    Commented Dec 22, 2018 at 2:12
50

576 is the minimum maximum reassembly buffer size, i.e. each implementation must be able to reassemble packets of at least that size. See IETF RFC 1122 for details.

7
  • 2
    If, and only if you have a network that doesn't carry IPv6. If it carries IPv6, use the maximum packet size of IPv6-headers then subtract encapsulation headers for doing IPv4 over IPv6. ;-)
    – Astara
    Commented Jun 29, 2016 at 1:37
  • 1
    @Astara In IPv6, fragmentation is done by the sender, so there's no problem with dodgy noncompliant intermediate routers. And if the recipient isn't a memory-constrained embedded size then it can probably reassemble packets up to 64kB at least.
    – user20574
    Commented Nov 19, 2019 at 11:05
  • @user253751 It's not just noncompliant routers that can fragment. There's Path MTU Discovery, but even that isn't enough to completely eliminate fragmentation.
    – dstromberg
    Commented Jun 5, 2020 at 17:11
  • @dstromberg In which situations are IPv6 routers allowed to fragment datagrams?
    – user20574
    Commented Jun 7, 2020 at 17:59
  • @user253751 I don't have much IPv6 yet, but here's one example: imagine an IPv6 network sending jumbograms (>65536 bytes) to another IPv6 network that also supports jumbograms. Further suppose that Path MTU Discovery says that those jumbograms should be supported without fragmentation. But then a router is power cycled, and part of the network path is replaced with equipment that isn't configured for jumbograms.
    – dstromberg
    Commented Jun 9, 2020 at 3:21
14

This article describes maximum transmission unit (MTU) http://en.wikipedia.org/wiki/Maximum_transmission_unit. It states that IP hosts must be able to process 576 bytes for an IP packet. However, it notes the minumum is 68. RFC 791: "Every internet module must be able to forward a datagram of 68 octets without further fragmentation. This is because an internet header may be up to 60 octets, and the minimum fragment is 8 octets."

Thus, safe packet size of 508 = 576 - 60 (IP header) - 8 (udp header) is reasonable.

As mentioned by user607811, fragmentation by other network layers must be reassembled. https://www.rfc-editor.org/rfc/rfc1122#page-56 3.3.2 Reassembly The IP layer MUST implement reassembly of IP datagrams. We designate the largest datagram size that can be reassembled by EMTU_R ("Effective MTU to receive"); this is sometimes called the "reassembly buffer size". EMTU_R MUST be greater than or equal to 576

11

IPv4 minimum reassembly buffer size is 576, IPv6 has it at 1500. Subtract header sizes from here. See UNIX Network Programming by W. Richard Stevens :)

5
  • 1
    Minimum, of course. Thanks for spotting it. Have no idea how nobody have noticed the mistake over the years. Commented May 30, 2016 at 1:40
  • 1
    While IPv6 may have a minimum reassembly buffer of 1500, IPv6 packets are not allowed to be fragmented, and the minimum IPv6 MTU is 1280. An end-device should never need to reassemble a fragmented IPv6 packet.
    – Ron Maupin
    Commented May 30, 2016 at 2:04
  • 1
    @RonMaupin IPv6 packets can be fragmented by the endpoints. Just not by the routers in between.
    – Navin
    Commented Aug 20, 2016 at 13:10
  • 3
    @Navin, no, IPv6 packets won't be fragmented, the data must be fragmented before it is packaged into IPv6 packets, but the packets themselves are not fragmented. There is a difference. Unlike IPv4 packet headers which have fields to deal with fragmentation, IPv6 packet headers do not have anything to deal with fragmentation. The IPv6 packet header is much simpler than the IPv4 packet header.
    – Ron Maupin
    Commented Aug 20, 2016 at 17:04
  • @RonMaupin why end user don't do defragmentation in ipv6? But I read in book source do fragmentation and destination do defragmentation?
    – S. M.
    Commented Mar 10, 2022 at 15:51
9

I've read some good answers here; however, there are some minor mistakes. Some have answered that the Message Length field in the UDP header is a max of 65535 (0xFFFF); this is technically true. Some have answered that the actual maximum is (65535 - IPHL - UDPHL = 65507). The mistake, is that the Message Length field in the UDP Header includes all payload (Layers 5-7), plus the length of the UDP Header (8 Bytes). What this means is that if the message length field is 200 Bytes (0x00C8), the payload is actually 192 Bytes (0x00C0).

What is hard and fast is that the maximum size of an IP datagram is 65535 Bytes. This number is arrived at the sum total of the L3 and L4 headers, plus the Layers 5-7 payload. IP Header + UDP Header + Layers 5-7 = 65535 (Max).

The most correct answer for what is the maximum size of a UDP datagam is 65515 Bytes (0xFFEB), as a UDP datagram includes the UDP header. The most correct answer for what is the maximum size of a UDP payload is 65507 Bytes, as a UDP Payload does not include the UDP header.

1
  • 1
    You didn't answer the question. The questioner wanted to know what was the largest size they could use to avoid the packet fragmenting.
    – Astara
    Commented Dec 22, 2018 at 2:04
8

512 is your best bet. It's used elsewhere and is a nice even number (half of 1024).

6

Given that IPV6 has a size of 1500, I would assert that carriers would not provide separate paths for IPV4 and IPV6 (they are both IP with different types), forcing them to equipment for ipv4 that would be old, redundant, more costly to maintain and less reliable. It wouldn't make any sense. Besides, doing so might easily be considered providing preferential treatment for some traffic -- a no no under rules they probably don't care much about (unless they get caught).

So 1472 should be safe for external use (though that doesn't mean an app like DNS that doesn't know about EDNS will accept it), and if you are talking internal nets, you can more likely know your network layout in which case jumbo packet sizes apply for for non-fragmented packets so for 4096 - 4068 bytes, and for intel's cards with 9014 byte buffers, a package size of ... wait...8086 bytes, would be the max...coincidence? snicker

****UPDATE****

Various answers give maximum values allowed by 1 SW vendor or various answers assuming encapsulation. The user didn't ask for the lowest value possible (like "0" for a safe UDP size), but the largest safe packet size.

Encapsulation values for various layers can be included multiple times. Since once you've encapsulated a stream -- there is nothing prohibiting, say, a VPN layer below that and a complete duplication of encapsulation layers above that.

Since the question was about maximum safe values, I'm assuming that they are talking about the maximum safe value for a UDP packet that can be received. Since no UDP packet is guaranteed, if you receive a UDP packet, the largest safe size would be 1 packet over IPv4 or 1472 bytes.

Note -- if you are using IPv6, the maximum size would be 1452 bytes, as IPv6's header size is 40 bytes vs. IPv4's 20 byte size (and either way, one must still allow 8 bytes for the UDP header).

9
  • 1
    how are you calculating 1472? ethernet has an MTU of 1500, is that what you're referring to?
    – rogerdpack
    Commented Nov 15, 2013 at 14:00
  • 4
    @rogerdpack I think he means that because IPv4 and IPv6 are likely to share a lot of infrastructure, and that IPv6 is getting relatively popular, it should be safe to assume IPv6 limits (thus the 1500). How valid this reasoning is, however, I cannot tell.
    – Thomas
    Commented Dec 18, 2013 at 12:39
  • 2
    1500 must be supported by IPv6 compatible components in the network "chain" -- if one uses IPv4, which can travel over an IPv6-supporting chain (though the reverse isn't true), then since IPv4's header size is 20 bytes, and UDP's header size is 8 bytes, that would leave a 1500-20-8=1472 as the maximum safe size (since IPv6 doesn't allow fragmenting). Note -- if people add enough layers of encapsulation, one could conceivably have no space for DATA. Since you asked for the MAX, one will assume multiple layers of encapsulation overhead are NOT being used.
    – Astara
    Commented Jun 6, 2016 at 19:00
  • "1500 must be supported by IPv6 compatible components in the network chain." No, the minimum IPv6 MTU is 1280. The ethernet MTU is 1500.
    – Ron Maupin
    Commented Jun 7, 2017 at 0:56
  • @RonMaupin - original Q was largest safe UDP packet size, not the MTU. See RFC2460. As well as mentioning an MTU of 1280 octets, it states: Nodes must be able to accept a fragmented packet, that when reassembled is up to 1500 octets. Handling packets larger than 1500 is optional.
    – Astara
    Commented Jun 7, 2017 at 17:53
2

UDP is not "safe", so the question is not great - however -

  • if you are on a Mac the max size you can send by default is 9216 bytes.
  • if you are on Linux (CentOS/RedHat) or Windows 7 the max is 65507 bytes.

If you send 9217 or more (mac) or 65508+ (linux/windows), the socket send function returns with an error.

The above answers discussing fragmentation and MTU and so on are off topic - that all takes place at a lower level, is "invisible" to you, and does not affect "safety" on typical connections to a significant degree.

To answer the actual question meaning though - do not use UDP - use raw sockets so you get better control of everything; since you're writing a game, you need to delve into the flags to get priority into your traffic anyhow, so you may as well get rid of UDP issues at the same time.

1

There isn't one.

I'm going to set aside entirely any 'UDP is best effort' reasoning, and just focus on 'maximum safe UDP packet size' taken as meaning 'small enough to absolutely avoid any fragmentation somewhere along the path'.

For important background:

The only packet size you can rely to be transportable without fragmentation is 24 bytes for IPv4 and 56 bytes IPv6, as the smallest IP headers for a fragment are 20/48 bytes (v4/v6) and a fragment must have at least 4/8 bytes (v4/v6) payload data. Thus a transport system below IP layer that cannot transport at least packets of theses sizes, cannot be used to transport IP traffic [at all]. - Answer to 'how is the MTU 65507 in UDP...?'.

According to the answer above, this is because at that length, the IP fragmentation mechanism cannot operate - it could only generate a single fragment anyway.

... the IP standard requires every IP host to be able to receive IP packets with a total size of 576 bytes.... Note, however, the standard does not say 576 without fragmentation, so even a 576 byte IP packet might find itself fragmented between two hosts [somewhere along the path between source and destination].

So anything bigger than the smallest the internet protocol could possibly do, could in fact find itself fragmented due to being 'invisibly' temporarily encapsulated to just above the limit somewhere along the path, even if you chose the minimum UDP payload, which would be: 576 - 8 (for UDP header) - 20 (IPv4) or 40 (IPv6) = min of 528 (in case you are not sure whether v4 or v6 will be used).

A reason you might be trying to avoid fragmentation, is that it does increase the likelyhood that the packet will go missing in it's entirety. More packets, just due to the higher overhead, would mean greater likelyhood of failure, let alone that each packet (even a fragment) represents another 'chance' to be lost. Of course, if it happens to be handed on to some link that just drops it for being too big...

TCP implementations at OS level do things to try to pick the highest safe MTU within the TCP layer, including dealing with it dynamically changing sometimes, as parts of the path between source and destination can differ packet-by-packet.

For UDP, this whole problem becomes yours also: And it's not so simple as just 'use the largest that will certainly not fragment'.

RFC2460 section 5 has this to say (about minimum MTU for IPv6).

IPv6 requires that every link in the internet have an MTU of 1280 octets or greater. On any link that cannot convey a 1280-octet packet in one piece, link-specific fragmentation and reassembly must be provided at a layer below IPv6.

Therefore fragmentation can also happen at beneath the IP level, where a particular link between two hosts along the path decides to fragment it, then may shove the fragments down parallel pipes... to have them reassembled into one packet at the other end of that one link.

If there is an out-of-ordering of those fragments, that reording mechanism might reassemble the packet incorrectly, without even realizing that it reordered part of it.

Of course, if the hardware was sticking to the IP specification, it should be able to notice that the packets got out of order by inspecting the fragment offset part of the IP header, but most link protocols just reassemble the fragments in the order received, without worrying about out-of-order packet reception.

Now if that 'lower level' interconnect is itself used as a network... such as within the core of a large switch, for instance, then it might be possible that under heavy loads and many parallel paths, such fragments might get out of order before reassembly.

This is super evil, but probably does happen sometimes, leading to the occasional still-delivered and yet badly-mangled packet, which still passes a simple xor-based checksum test... Which happens to be what the IP checksum is.

TCP just sets 'do not fragment' and deals with it, but UDP leaves that bit clear... so if you're using UDP, you should also assume it possible that your stream of bytes might sometimes get reordered even within packets... and so you should verify your data using a checksum method that will catch reordering.

Most links will do 1500 MTU anyway, but link layer have different minimum and maximum datagram sizes:

  • Ethernet: 64 to 1500 (or higher with higher MTU capability)
  • ATM: 53 bytes / datagram only
  • Frame Relay: 46 to 4470 bytes (ref: IP fragmentation is broken

Unfortunately, the UDP header itself is 8 bytes... so this means, that the only size UDP payload that could certainly avoid fragmentation... is zero! (and will only fit in the smallest possible IPv6 packet anyway).

The best / reasonable course is probably to use 512 for your UDP datagrams and do your own checks for data getting out of order within a datagram, as well as dealing packets arriving out of order. (I.E, don't rely at all on IP to drop broken packets for you).

-1

I fear i incur upset reactions but nevertheless, to clarify for me if i'm wrong or those seeing this question and being interested in an answer:

my understanding of https://www.rfc-editor.org/rfc/rfc1122 whose status is "an official specification" and as such is the reference for the terminology used in this question and which is neither superseded by another RFC nor has errata contradicting the following:

theoretically, ie. based on the written spec., UDP like given by https://www.rfc-editor.org/rfc/rfc1122#section-4 has no "packet size". Thus the answer could be "indefinite"

In practice, which is what this questions likely seeked (and which could be updated for current tech in action), this might be different and I don't know.

I apologize if i caused upsetting. https://www.rfc-editor.org/rfc/rfc1122#page-8 The "Internet Protocol Suite" and "Architectural Assumptions" don't make clear to me the "assumption" i was on, based on what I heard, that the layers are separate. Ie. the layer UDP is in does not have to concern itself with the layer IP is in (and the IP layer does have things like Reassembly, EMTU_R, Fragmentation and MMS_R (https://www.rfc-editor.org/rfc/rfc1122#page-56))

2
  • 1
    The UDP header has a datagram length field that is 16 bits, meaning that that the largest theoretical UDP datagram is 65,535, but that can never be reached because UDP is encapsulated inside an IP packet, which has a theoretical overall maximum length of 65,535 (the same) but you must subtract the IP and UDP headers from that size to figure the theoretical maximum data size.
    – Ron Maupin
    Commented Apr 12, 2020 at 0:27
  • I asked this a long time ago but it was looking for a pragmatic answer (what works in real life) rather than what it says in the specs / or in theory. I wanted to get packets form a to b with out fragmentation, it was for a real time games networking problem - I think there are many solutions developed by smarter people now :)
    – K M
    Commented Apr 12, 2020 at 11:00
-1

There are different maximums for udp. In the End, it is your choice which limits you consider safe enough. Note: UDP in itself is not safe, therefore your packets could be lost at any moment.

Note: All of these have a range. How large it can actually be, depends on your IP Options. In general, you can calculate it like that: [upper maximum]-[length of IP-Options, in bytes]. All the ranges listed here exclude the UDP-Header(8 bytes)

Minimum Maximum Tranfer Limit(or something like that, i forgot the actual name and couldn't find it anywhere):512-552 bytes

This is probaly the safest size. The IPv4 protocol reqires all hops to transmit packets of this size, larger packets may be dropped by any host at any time.

Maximum Transfer Unit:1432-1472:

Packets of this size can be sent in a single ethernet frame without being fragmented. The loss of any fragment will result in the whole packet being useless. Therefore, when exceeding this limit, the chances of your packet becoming useless increase, due to fragmentation and potential loss of single fragments.

Absolute Limit: 65467-65507:

No Packet can exceed this limit, because at this limit, the IPv4 protocol reaches its full potential. you cannot send larger packets. This is because the IPv4 protocol reserves only 2 bytes(16 bit) for the total length. setting all these bits to 1, you get 65535. Subtracting the IP(20-60 bytes) and the UDP header(8 bytes), you get this limit. Packets of this size will result in at least 44 fragments. Keep in mind, the loss of any of these fragments will lead to the whole packet being useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.