Ports, NAT, firewalls, proxies and TLS along the path
Examine middlebox infrastructure along network paths: ephemeral port exhaustion, stateful vs stateless firewalls, NAT conntrack limits, and TLS edge offloading.
Contentsand lesson path
On this page
- Transport Ports: Well-Known, Registered, and Ephemeral Ranges
- Network Address Translation: SNAT, DNAT, and Conntrack
- Firewalls (Stateful vs. Stateless) and TLS Termination
- Check your understanding
- Apply it
- Review prompt
Lessons of Networking Basics
- Layers as a debugging map, and what L4 and L7 mean Concept · 15 min
- Addresses, CIDR blocks and the default gateway: DHCP, ARP and the next hop Worked example · 25 min
- From a name to a response: DNS, TCP and HTTP in one trace Worked example · 20 min
- Ports, NAT, firewalls, proxies and TLS along the path Concept · 20 min
- Troubleshooting lab: which hop is broken? Lab · 25 min
- Check: subnetting and failure triage Knowledge check · 8 questions
What you will learn
- Describe the TCP/IP layers and assign a given failure (name resolution, routing, closed port, TLS, HTTP error) to the layer it belongs to.
- Explain how DNS resolution, a TCP connection and an HTTP request combine when a client calls a service by name.
01. Transport Ports: Well-Known, Registered, and Ephemeral Ranges
At the Transport Layer (Layer 4), the operating system multiplexes concurrent communications using 16-bit numeric identifiers called ports, spanning from 0 to 65535. A complete socket connection endpoint is defined by a unique five-tuple: (protocol, source_ip, source_port, destination_ip, destination_port).
The port spectrum divides into three formalized tiers:
- Well-Known Ports (
0to1023): Assigned to core infrastructure protocols (e.g.22for SSH,53for DNS,80for HTTP,443for HTTPS). On Linux and Unix hosts, binding a listening socket to a port below 1024 requires administrative privileges (root) or the kernel capabilityCAP_NET_BIND_SERVICE. The threshold is the sysctlnet.ipv4.ip_unprivileged_port_start(default1024). Docker (since 20.10) sets it to0inside containers that have their own network namespace, so there a non-root process can bind port 80. - Registered Ports (
1024to49151): User and vendor application services (e.g.3306for MySQL,5432for PostgreSQL,6379for Redis,8080for web proxies). - Ephemeral (dynamic) ports: Temporary client ports. When your browser or microservice opens an outbound connection to an external API, the kernel picks a free source port from its ephemeral range to receive the return traffic. IANA sets aside
49152to65535as the dynamic (ephemeral) range and never assigns those ports to services (RFC 6335), but each operating system chooses its own range: Linux defaults to32768to60999(theip_local_port_rangesetting, readable at/proc/sys/net/ipv4/ip_local_port_range), while Windows uses49152to65535. Ports below49152are normal on Linux: in repeated runs of 300 local test connections on a default Ubuntu 22.04 host, anywhere from none to all 300 of the source ports were below49152, because the kernel varies where in its range it starts searching. That is why stateless firewall rules for return traffic (such as AWS network ACLs) usually allow1024to65535; a rule for49152to65535only would break Linux clients, and the Linux pool holds only about 28,000 ports per destination. In high-throughput proxy servers, failing to reuse TCP connections can exhaust this ephemeral port space, causing outbound connection requests to fail withEADDRNOTAVAIL(Cannot assign requested address).
Modern high-performance web servers (such as Nginx and Envoy) leverage the Linux SO_REUSEPORT socket option. This allows multiple distinct worker processes to bind independently to the exact same listening port (e.g. 0.0.0.0:80). The Linux kernel then spreads new connections across the listening sockets by hashing each connection's 4-tuple (source and destination address and port), or by a BPF program you attach. It is not round-robin, so with few clients the spread can be uneven. Nginx enables this per listener with listen 80 reuseport;. Each worker then has its own accept queue instead of all of them competing for one, which usually spreads the load better across CPU cores.
# Inspect active TCP listening sockets with process identifiers
ss -tlpn
# Query local kernel ephemeral port range configuration
cat /proc/sys/net/ipv4/ip_local_port_range
# Count active established connections across ephemeral ports
ss -s02. Network Address Translation: SNAT, DNAT, and Conntrack
Because IPv4 address space is finite, private subnets use the private address blocks defined in RFC 1918 (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16). Internet routers do not carry these private addresses, so traffic from them cannot cross the public Internet as it is. Gateways bridge this boundary using Network Address Translation (NAT):
- Source NAT (SNAT / Masquerading): Allows private backend instances to reach external APIs or package repositories. The NAT gateway rewrites the private source IP of outgoing packets to its own public IP, substituting a tracked source port. When the external server responds, the gateway translates the packet back to the internal host.
- Destination NAT (DNAT / Port Forwarding): Allows external clients to reach internal services. The gateway accepts traffic on its public IP and rewrites the destination IP to an internal private host address.
- Connection Tracking (
conntrack): Linux and cloud NAT gateways maintain a state table mapping each active session. If a gateway handles millions of short-lived TCP flows without connection reuse, theconntracktable can fill to capacity (netfilter: nf_conntrack: table full, dropping packet), causing sudden packet drops across all hosts sharing that NAT gateway.
In the Linux kernel netfilter framework, NAT operations are executed within the nat table across two primary hooks: PREROUTING (for DNAT before routing decisions are calculated) and POSTROUTING (for SNAT/MASQUERADE after routing decisions have determined the egress interface). Raising /proc/sys/net/netfilter/nf_conntrack_max (each entry costs some kernel memory) and shortening the conntrack timeouts for idle or closed TCP flows leaves more room in the table during traffic spikes; reusing connections in the clients reduces the number of entries in the first place.
03. Firewalls (Stateful vs. Stateless) and TLS Termination
Network security along the transit path is enforced by firewalls operating under two distinct operational paradigms:
- Stateful Firewalls (e.g. AWS Security Groups, Linux iptables or nftables with
conntrack): Track the state of active TCP connections. An AWS security group does this by itself: when an inbound rule permits traffic on port 443, the return traffic for that session is allowed without a separate outbound rule. Linux netfilter tracks every connection, but it lets return traffic through only if a rule says so: a hand-written iptables ruleset needs an explicit rule such as-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT(in nftables,ct state established,related accept) near the top of the chain. Front ends such as ufw and firewalld add this rule for you, which is why the state tracking can look automatic. - Stateless Packet Filters (e.g. AWS Network Access Control Lists - NACLs): Evaluate each packet in isolation without session memory. If you allow inbound traffic on port 80, you must explicitly permit outbound return traffic on ephemeral ports (
1024-65535), or the connection will hang because return packets are blocked at the perimeter.
TLS Termination vs. End-to-End Encryption: In modern cloud topologies, TLS is frequently terminated at the edge reverse proxy or load balancer. The proxy does the cryptographic work, can validate client certificates, and forwards plain HTTP over the private network to the backends. Where internal traffic must stay encrypted (regulated or zero-trust environments), proxies re-encrypt traffic using internal mutual TLS (mTLS) to secure internal container-to-container transit, verifying cryptographic service identities via X.509 certificates issued automatically by service meshes such as Linkerd or Istio.
04. Check your understanding
05. Apply it
Implement these edge and middlebox practices in your deployment pipelines:
- Use HTTP Connection Pooling: Configure backend HTTP clients (e.g.
requests.Sessionin Python orhttp.Agentin Node) with connection keep-alive to reuse TCP connections, avoiding ephemeral port exhaustion and NAT conntrack table bloat. - Prefer Stateful Security Groups: Use cloud security groups (stateful) over NACLs (stateless) for application security policies to avoid managing fragile ephemeral return port rules.
- Test Firewall Timeouts with Strict Deadlines: When testing port reachability across firewalls, invoke
nc -w 3 -zv <ip> <port>so a script gives up after 3 seconds instead of waiting for the kernel's SYN retries, which take about two minutes with the Linux defaultnet.ipv4.tcp_syn_retries = 6. - Run App Containers on Unprivileged Ports: Configure backend application containers to bind to unprivileged ports (e.g.
8080or3000) and map them to80/443at the ingress controller or load balancer, so the application never needs root or extra capabilities. - Monitor Conntrack Table Utilization: Set up Prometheus node_exporter alerts when
node_nf_conntrack_entries / node_nf_conntrack_entries_limitexceeds 75% on NAT and firewall hosts.
06. Review prompt
Distinguish between well-known and ephemeral port ranges, explaining why ports below 1024 require elevated privileges. Explain the difference between Source NAT and Destination NAT, and describe the operational consequences of exhausting NAT conntrack tables. Detail why stateless firewalls require explicit return traffic rules.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.