1

I would like to ask community to help me finding answers to the following questions:

  1. According to RFC 6056 an operating system should pseudo-randomly select ephemeral ports for each transport layer connection. For TCP, which is stateful protocol, the situation is clear: the ephemeral port is assigned to 5 tuple {protocol, source_ip, source_port, destination_ip, destination_port} until the connection is dropped. However, UDP is a stateless protocol, there is no UDP connection. Therefore, for me it is not clear how an ephemeral port is assigned in case of UDP. Is it different for every request? Is it randomly assigned to 5 tuple and preserved until no packets received during a TIMEOUT?
  2. Are the differences in the implementations of various operating systems, e.g., Windows, Unix, Linux?

I asked the same question on serverfault.com, but maybe it is more relevant here.

1
  • Your second questions is actually off-topic here. Host OSes and configuration are off-topic.
    – Ron Maupin
    Commented Aug 15, 2017 at 15:35

2 Answers 2

2

On clients, each socket is bound either explicitly or implicitly to a local port. This binding lasts as long as the socket does. Whether the same socket is used for multiple requests or a new socket is created for each request is up to the application developer.

On network address translators a timeout based policy is normally used. If no packets are seen for a given mapping during the timeout period then the mapping is discarded.

2
  • Thank you for your answer! This is what exactly I want to know. I just want to clarify one thing. Once local port is assigned implicitly (I assume by the network stack of an operating system), what is the default behavior? I.e., if I develop an application opening one socket and relying on the OS to assign source port, by default is it going to assign one source port until the socket is closed (or timeout?) or new source port for every new request?
    – Yury
    Commented Aug 16, 2017 at 5:31
  • As long as you keep that socket open you will keep the local port on the client. Commented Aug 16, 2017 at 15:02
1

Just like TCP, UDP randomly selects an Ephemeral port for one data stream. So when a datagram is sent by a client from an unbound port number, an ephemeral port number is assigned automatically so the receiving end can reply to the sender. An application can specifically select a port to use for sending data if it's programmed to do so.

UDP contains a checksum in the header field even though it is connectionless. UDP does not care about packets that are dropped & packets that are delivered out of order. But one thing UDP cares about is the integrity of the packets that are received. UDP cannot correct an integrity issue, but it can discard a data gram whose checksum is invalid. Basically the receiving end wont accept a packet that has wrong checksum. There is no mechanism to inform this back to the sender, but it will discard it silently.

The Ephemeral source port varies and depends on the opretating system. Check these sources:

The Ephemeral Port Range

Ephemeral Source Port Selection Strategies

2
  • Thanks for the answer! You're right, UDP contains a checksum but you can always put 0 there, preventing it from being checked. Moreover, from my experience it seems that an OS (in my case a Linux distro) allows to receive UDP packets even with incorrect checksums if you use raw sockets.
    – Yury
    Commented Aug 16, 2017 at 5:35
  • @Yury, the UDP checksum is optional for IPv4, but it is required for IPv6.
    – Ron Maupin
    Commented Jan 20, 2018 at 20:33

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.