How are UDP source ports selected

udp

I just did a packet capture from my machine and filtered out all the UDP connections. I saw certain connections using what appears to be a randomly generated UDP Source port, and certain connection using the same Source port as the UDP Destination Port.

I understand that TCP connections will randomize the source port so the response has a "dedicated" port to respond to. But how does it work with UDP?

I'm looking for the authoritative answer. Links to RFC's (or whatnot) would be greatly appreciated.

What determines the UDP Source port?

  • Is it randomly generated when a response is expected?
  • Does it match the destination port when no response is expected? (instead of using
    0 as a source port)?

Thank you.

Best Answer

It depends on the application. For example, the Linux kernel implemented UDP source port randomization when no source port is specified in kernel 2.6.24.

So, the behavior you are seeing in some connections must be the particular application specifying the source port to be the same as the destination port, while others are leaving it to the kernel.

Related Topic