ephemeral port

term

networkingprogramming

Kept in this browser · My desk

A port number that a host's networking stack chooses automatically for its own side of a connection, from a configured range, so that the connection's five-tuple is unique. It belongs to the connection only while it lives, and to the TIME-WAIT state for a while after the side that chose it closes first.

Clients almost never pick their port. Request for Comments (RFC) 6056 calls the ports a stack chooses automatically ephemeral ports and asks for two things: that each choice yield a unique five-tuple, and that it be hard to guess, because blind attacks on a connection depend on knowing the client's port. The ranges differ: the Internet Assigned Numbers Authority (IANA) keeps 49152 to 65535 as dynamic ports, never assigned; RFC 6056 recommends the whole range from 1024 to 65535; Linux defaults to 32768 to 60999, or 28,232 ports, and Windows since Vista to 49152 to 65535, or 16,384. The range is a budget spent per destination: one source address can hold no more simultaneous connections to one server address and port than it has ephemeral ports, and every connection it closed first keeps its port in TIME-WAIT. Proxies, translators and load balancers that connect on behalf of many clients run out first.

What it is. Request for Comments (RFC) 6056 (January 2011, Best Current Practice 156), written by Michael Vittrup Larsen of Tieto and Fernando Gont of the Universidad Tecnológica Nacional in Argentina, describes the usual arrangement: a client names the server's address and port, while its own address and port are normally left unspecified and chosen automatically by the networking stack, and ports chosen that way are known as ephemeral ports, a term the RFC takes from W. Richard Stevens's Unix Network Programming. The rule that matters is in its section 2.2: the selection of ephemeral port numbers must result in a unique five-tuple. On Linux, the ip(7) manual describes the mechanism from the program's side: when connect() or listen() is called on a socket that was never bound, the socket is automatically bound to a random free port; udp(7) adds that a User Datagram Protocol (UDP) socket used without bind() gets a free local port from the range in /proc/sys/net/ipv4/ip_local_port_range; and asking bind() for port 0 has the same effect.

The ranges. The Internet Assigned Numbers Authority (IANA) divides the 16-bit space in RFC 6335 (August 2011): System Ports 0 to 1023, User Ports 1024 to 49151 and Dynamic Ports, also called private or ephemeral, 49152 to 65535, which are never assigned. RFC 6056 recommends going beyond that: ephemeral port selection algorithms should use the whole range from 1024 to 65535, and should use the largest possible range, because a larger range makes the port harder for an off-path attacker to guess; ports that a local service needs should be excluded individually rather than by excluding every registered port. Implementations chose differently. Linux defaults to 32768 to 60999 (28,232 ports), and its documentation asks for the two limits to have different parity, for the range to start at or above ip_unprivileged_port_start (default 1024), and offers ip_local_reserved_ports for numbers that automatic assignment must never take. Microsoft moved Windows Vista and Windows Server 2008, in its own words to comply with IANA recommendations, from 1025 to 5000 (3,976 ports) to 49152 to 65535 (16,384), set separately for the Transmission Control Protocol (TCP) and UDP. RFC 6056's survey of 2011 shows the spread: FreeBSD 8.0 switched to 10000 to 65535 in response to the document, NetBSD 5.0.1 counted down from 65535 without obfuscation, OpenBSD 4.2 used 1024 to 49151 and OpenSolaris 2009.06 used 32768 to 65535.

How the port is chosen. The traditional algorithm of the Berkeley Software Distribution (BSD) kept one global next_ephemeral counter and added one per connection. RFC 6056 explains what was wrong with it: the next ports are easy to predict, an attacker who runs an innocent server the victim connects to learns the counter's current value, and by subtracting consecutive values the attacker can even count how many connections the host opened in between. That matters because blind attacks that inject data into or reset a connection rely on guessing its five-tuple, and the server's address, the server's port and often the client's address are public; the client's port is the part left to guess. The RFC specifies five alternatives in section 3.3: two simple randomizations, a hash-based selection that keeps a separate, randomly initialized counter per destination, a double-hash variant that lowers port reuse further, and random increments. Its survey records Linux 2.6.15 using the hash-based algorithm with the Message Digest 5 (MD5) function.

Why reuse needs care. Uniqueness on the client is not enough. RFC 6056 gives the case of a web server that performs the active close: the client forgets the connection, but the server keeps the five-tuple in TIME-WAIT for 2 x MSL (Maximum Segment Lifetime), and a new connection from the same client and port collides with it, with effects from connection failures to what RFC 1337 called TIME-WAIT assassination. RFC 9293 sets the TIME-WAIT duration at twice the MSL and takes the MSL to be 2 minutes, so a port released by an active close is held for 240 seconds by the specification's numbers, and the standard says plainly that TIME-WAIT limits the rate of connection reuse. Linux's ip(7) states the same constraint from the program's side: a TCP local address that has been bound stays unavailable for some time after closing, unless SO_REUSEADDR is set, which it warns makes TCP less reliable.

The arithmetic of exhaustion. The budget is per destination socket. One client address talking to one server address and port can hold at most 28,232 simultaneous TCP connections with Linux defaults, 16,384 with Windows defaults and 64,512 with RFC 6056's full range; towards a different server address or port the whole budget is available again. If short connections are closed first by the client, the sustainable rate towards one destination is the range divided by the TIME-WAIT duration: 28,232 divided by 240 seconds is about 117.6 new connections a second, 16,384 divided by 240 about 68.3. Hosts rarely reach this alone; proxies, source address translators and load balancers that open server-side connections for many clients from a few addresses reach it routinely, which is why the site's articles on BIG-IP source address translation (SNAT) and on carrier-grade port blocks measure the same ceiling. Translators also have rules of their own for the ports they hand out: RFC 4787 (January 2007) forbids port overloading, recommends keeping a host's source port in the same range (0 to 1023, or 1024 to 65535) after translation, and recommends preserving its parity, because the Real-time Transport Protocol (RTP) uses even ports and its control protocol odd ones.

What goes wrong. Exhaustion shows up as connection failures whose error, Linux's ip(7) notes, differs with the system call that tried to take a port: connect(), bind(), listen() or sendto(). A local service that listens on a number inside the ephemeral range can find its port already taken by an outgoing connection, which is the case RFC 6056 and Linux's ip_local_reserved_ports address. Firewall rules written for an old range break silently when the range changes: Microsoft warned, when Windows moved to 49152 to 65535, that remote procedure call (RPC) traffic between servers could fail where internal firewalls did not allow the new range. And predictable selection is a security weakness in its own right, which RFC 6056 was written to remove.

In practice. On Linux, cat /proc/sys/net/ipv4/ip_local_port_range shows the range and ss -tan state time-wait counts the ports still held after closing; on Windows, netsh int ipv4 show dynamicport tcp shows it and netsh int ipv4 set dynamic tcp start= num= changes it. Neighbours: the port and the socket it belongs to, the five-tuple it completes, TIME-WAIT, which holds it after a close, and the translation devices, Network Address Translation (NAT), port address translation (PAT), SNAT and carrier-grade NAT with its port block allocation, which spend the same budget on behalf of others.

Also known as: ephemeral ports, dynamic ports, dynamic port range, local port range, ip_local_port_range, porta efêmera, portas efêmeras

Sources

← Back to E Back to the glossary