Sub topic 2
Ports, sockets, and identifying a connection
This chapter assumes you know basic IPv4 addressing and subnet masks, what a router and default gateway do, the difference between TCP and UDP, and how to run commands in a Linux shell. It explains how a host tells one connection from another. NAT depends on exactly this mechanism, so the rest of the tutorial builds on it.
Why an IP address is not enough
An IP address identifies a network interface on a host. A host usually runs many programs at once: a browser with twenty tabs, a mail client, an SSH session, a package updater. All of them share one IP address. When a packet arrives, the operating system must decide which program receives it.
The transport layer solves this with port numbers. A port is a 16-bit unsigned integer, so it ranges from 0 to 65535. TCP and UDP each carry a source port and a destination port in their headers. The IP address gets the packet to the right host. The port gets the data to the right program on that host.
TCP and UDP port spaces are separate. TCP port 53 and UDP port 53 are different endpoints, and a program can bind to one without the other.
Port ranges
IANA divides the port space into three conventional ranges.
| Range | Name | Typical use |
|---|---|---|
| 0–1023 | Well-known (system) ports | Standard services such as 22 (SSH), 80 (HTTP), 443 (HTTPS), 53 (DNS) |
| 1024–49151 | Registered ports | Applications such as 3306 (MySQL) or 8080 (alternate HTTP) |
| 49152–65535 | Dynamic (ephemeral) ports | Temporary source ports chosen by clients |
Linux does not follow the IANA ephemeral range exactly. It uses its own range, which you can read from the kernel.
cat /proc/sys/net/ipv4/ip_local_port_rangeA typical result is 32768 60999. On Linux, binding to a port below 1024 normally requires root privileges or the CAP_NET_BIND_SERVICE capability.
Server ports and client ports
A server listens on a port that clients know in advance. Web servers listen on 80 or 443 because browsers expect that. A client does not need a predictable port. When a client opens a connection, the operating system picks an unused ephemeral port as the source port for that connection.
Suppose your laptop at 192.168.1.20 opens a web page at 198.51.100.7. The first packet of the connection looks like this:
| Field | Value |
|---|---|
| Source IP | 192.168.1.20 |
| Source port | 51514 (ephemeral, chosen by the OS) |
| Destination IP | 198.51.100.7 |
| Destination port | 443 |
| Protocol | TCP |
The server's reply swaps the two ends. Its source is 198.51.100.7:443 and its destination is 192.168.1.20:51514. The client's ephemeral port is what lets the OS hand the reply to the right browser tab.
Sockets
A socket is the programming interface a process uses to send and receive network data. The term is used in two related ways, so it helps to separate them.
- A socket address (or endpoint) is the pair of an IP address and a port, written
192.168.1.20:51514. - A socket in code is an object the kernel gives a process, identified by a file descriptor. A server creates a listening socket bound to a local address and port. Each accepted TCP connection produces a new, separate connected socket.
This distinction explains a common surprise. A web server on port 443 can handle thousands of clients at once, yet only one process is bound to port 443. The listening socket stays on port 443. Each accepted connection is a different connected socket, and all of them share the same local port.
The 5-tuple
How does the kernel keep thousands of connections to the same port apart? It uses five values together, called the 5-tuple:
- Protocol (TCP or UDP)
- Source IP address
- Source port
- Destination IP address
- Destination port
A connection is uniquely identified by its 5-tuple. Two connections can share three of the values and still be different, as long as at least one value differs.
| Connection | Source | Destination | Protocol |
|---|---|---|---|
| A | 192.168.1.20:51514 | 198.51.100.7:443 | TCP |
| B | 192.168.1.20:51515 | 198.51.100.7:443 | TCP |
| C | 192.168.1.21:51514 | 198.51.100.7:443 | TCP |
Connection A and B differ by source port. A and C use the same source port number but come from different hosts. All three are distinct flows to the same server socket address.
A flow is the stream of packets that belong to one 5-tuple. Many devices, including firewalls and NAT gateways, track state per flow. Because replies reverse the tuple, a device can treat the two directions as a single bidirectional conversation.
Seeing connections on a real host
The ss command lists sockets and shows the tuple values.
# Established TCP connections, numeric addresses and ports
ss -tn state established
# Listening TCP sockets and the processes that own them (needs root for other users' processes)
sudo ss -ltnpIn the established output, the Local Address:Port column is the host's own endpoint and Peer Address:Port is the remote endpoint. The protocol comes from the flags you chose. Together these give the full 5-tuple.
The -n flag matters. Without it, ss tries to translate ports into service names such as https, which makes it harder to match values against packet captures or NAT tables.
Ports and the packet header
In IPv4, the IP header carries addresses and a protocol number (6 for TCP, 17 for UDP). The ports are not in the IP header. They sit at the start of the TCP or UDP header, in the first four bytes: two bytes for the source port, then two bytes for the destination port.
This placement has a direct consequence for NAT. A device that rewrites ports must edit a header that belongs to a different layer than the IP header. It must also fix the checksums, because the TCP and UDP checksums cover a pseudo-header that includes the IP addresses and the ports. The next chapters return to this.
The diagram below shows the layering and which header holds each part of the 5-tuple.
Common edge cases
Port reuse across protocols. TCP 5000 and UDP 5000 are independent. A rule that forwards "port 5000" without naming a protocol is ambiguous, and most tools require you to choose.
One listener per address and port. Two processes cannot normally bind the same protocol, address, and port. The second bind() fails with "Address already in use." Options such as SO_REUSEPORT allow it deliberately for load balancing, and binding to different local addresses (for example 127.0.0.1:8080 and 192.168.1.20:8080) also works.
Wildcard binding. A listener bound to 0.0.0.0:80 accepts connections on every IPv4 address of the host. A listener bound to 127.0.0.1:80 accepts only local connections. If a service works locally but not from other machines, check which address it is bound to with ss -ltn.
TIME_WAIT. After a TCP connection closes, the side that closed first keeps the 5-tuple in a TIME_WAIT state for a period, commonly 60 seconds on Linux. This prevents late packets from an old connection from being mistaken for part of a new one using the same tuple.
Ephemeral port exhaustion. A client making many connections to the same destination address and port can use up its ephemeral range, because source port is the only value left that can vary. With about 28,000 ports in the default Linux range, a single host can have roughly that many simultaneous connections to one destination socket address. The same limit applies to a NAT device. When many private hosts share one public address, their connections to one server compete for source ports on that address.
Fragments and ICMP. Only the first IP fragment contains the TCP or UDP header, so later fragments carry no port numbers. ICMP has no ports at all. Devices that track flows use other fields for ICMP, such as the identifier in an echo request.
Why this matters for NAT
NAT changes some of the five values as a packet passes through a gateway. It is safe only if the device can still tell flows apart and map replies back to the right internal host. Two private hosts may both choose source port 51514 for connections to the same server. Once their traffic shares one public address, those two flows would collide, so the gateway must assign a different source port to one of them and remember the change. The port number is the extra room that lets many private hosts share one address.
A forwarded service works the other way. The gateway listens, in effect, on a public address and port, and rewrites the destination to a private socket address. In both cases the 5-tuple is the unit of identity.
Exercise
Hosts 10.0.0.5 and 10.0.0.6 each open a TCP connection from source port 40000 to 203.0.113.9:443. Are these two connections distinct on the server, and if both pass through a NAT gateway with the single public address 198.51.100.1, what must the gateway do?
Show answer
Without NAT they are distinct, because their source IP addresses differ, so the 5-tuples differ. After translation both would appear to come from 198.51.100.1:40000 to 203.0.113.9:443, which is the same 5-tuple. The gateway must give at least one of them a different public source port, for example 40001, and record the mapping so replies are returned to the correct host.
Exercise
A web service works with curl http://127.0.0.1:8080 on the server but times out from another machine on the LAN, and no firewall is involved. Which command shows the likely cause, and what would you look for?
Show answer
Run ss -ltn (or sudo ss -ltnp) on the server and look at the Local Address column for port 8080. If it shows 127.0.0.1:8080, the service is bound only to the loopback address. It must bind to 0.0.0.0 or to the LAN address to accept outside connections.