Sub topic 2
Ports, sockets, and identifying a connection
This chapter assumes you know IPv4 addresses and subnet masks, what a default gateway does, and the basics of TCP and UDP. It covers how a host tells connections apart. NAT depends on this, because a NAT device rewrites exactly these identifying fields.
Ports and sockets
An IP address gets a packet to a host. A port number, a 16-bit value from 0 to 65535, gets the data to the right program on that host. A socket is one endpoint of a conversation: an IP address plus a port, written 192.168.1.20:51514.
Ports fall into three rough groups:
| Range | Name | Typical use |
|---|---|---|
| 0-1023 | Well-known | Servers such as HTTP (80), HTTPS (443), SSH (22) |
| 1024-49151 | Registered | Applications such as databases |
| 49152-65535 | Ephemeral (IANA) | Temporary client ports |
Linux picks ephemeral ports from a configurable range, by default 32768-60999. You can check it with sysctl net.ipv4.ip_local_port_range.
A server listens on a fixed, known port. A client asks the operating system for any free ephemeral port as its source port. This is why many clients can talk to one server port at the same time.
The 5-tuple
A connection is identified by five values, called the 5-tuple:
- Protocol (TCP or UDP)
- Source IP address
- Source port
- Destination IP address
- Destination port
Two connections are different if any one of the five values differs. A web server on port 80 can serve thousands of clients at once, because each client has a different source address or source port.
Both clients use source port 51000, yet the connections do not collide. The source addresses differ, so the 5-tuples differ.
Seeing real connections
Run ss on a Linux host to list sockets:
# -t TCP, -n numeric output, -a all states
ss -tnaEach line shows the local socket and the peer socket. A listening server shows 0.0.0.0:80 as local and 0.0.0.0:* as peer. An established connection shows both sides fully filled in, which is the 5-tuple minus the protocol.
Why this matters for NAT
A NAT device that rewrites a source address can create a clash. Suppose two private hosts, 192.168.1.10 and 192.168.1.11, both use source port 51000 to reach the same server. After both are rewritten to one public address, the 5-tuples would be identical unless the NAT device changes one of the ports. That is why NAT must track and sometimes change ports, not only addresses. Later chapters build on this.
Common errors
- Treating a port as unique across the whole host. Ports are unique only per protocol and per local address, so TCP 80 and UDP 80 are separate.
- Thinking the source port is fixed. Clients normally get a random ephemeral port for each connection.
- Forgetting that UDP has no connection state. The 5-tuple still identifies a UDP flow, but the kernel or NAT device infers the flow from packets it has seen, with timeouts.
Exercise
Two laptops, 192.168.1.10 and 192.168.1.11, both open TCP connections from source port 51000 to 198.51.100.7:443. A NAT device rewrites both source addresses to 203.0.113.5. Why must it also change at least one source port?
Show answer
Without a port change, both connections would have the 5-tuple (TCP, 203.0.113.5, 51000, 198.51.100.7, 443). The server and the NAT device could not tell them apart, so replies could not be mapped to the right laptop. The NAT device must give one connection a different source port.