Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 4 of 6 · Concept · 20 min · revision 3

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

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 (0 to 1023): Assigned to core infrastructure protocols (e.g. 22 for SSH, 53 for DNS, 80 for HTTP, 443 for HTTPS). On Linux and Unix hosts, binding a listening socket to a port below 1024 requires administrative privileges (root) or the kernel capability CAP_NET_BIND_SERVICE. The threshold is the sysctl net.ipv4.ip_unprivileged_port_start (default 1024). Docker (since 20.10) sets it to 0 inside containers that have their own network namespace, so there a non-root process can bind port 80.
  • Registered Ports (1024 to 49151): User and vendor application services (e.g. 3306 for MySQL, 5432 for PostgreSQL, 6379 for Redis, 8080 for 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 49152 to 65535 as the dynamic (ephemeral) range and never assigns those ports to services (RFC 6335), but each operating system chooses its own range: Linux defaults to 32768 to 60999 (the ip_local_port_range setting, readable at /proc/sys/net/ipv4/ip_local_port_range), while Windows uses 49152 to 65535. Ports below 49152 are 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 below 49152, 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 allow 1024 to 65535; a rule for 49152 to 65535 only 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 with EADDRNOTAVAIL (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.

Bash · Inspecting active listening ports and kernel ephemeral port range
# 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 -s

02. 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, the conntrack table 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:

  1. 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.
  2. 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

Practice check · 3 questions

Port tiers, NAT conntrack limits, and firewall state checks

Three questions on ephemeral ports, conntrack and source NAT.

Q1. On a Linux client, `ss -tn` shows an outbound connection from `10.0.1.7:38412` to `198.51.100.20:443`. A teammate says 38412 cannot be a client port, because ephemeral ports start at 49152. Who is right?
Q2. A service opens hundreds of short-lived, unpooled TCP connections per second through a Linux NAT gateway. Under peak load, new connections start timing out. What is the likely cause?
Q3. Instances in the private subnet `10.0.3.0/24` call a partner's API through a NAT gateway whose public address is 203.0.113.10. The partner accepts calls only from addresses on its allowlist. What do you ask the partner to add?
Practice only: not graded, not saved, not evidence.

05. Apply it

Implement these edge and middlebox practices in your deployment pipelines:

  • Use HTTP Connection Pooling: Configure backend HTTP clients (e.g. requests.Session in Python or http.Agent in 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 default net.ipv4.tcp_syn_retries = 6.
  • Run App Containers on Unprivileged Ports: Configure backend application containers to bind to unprivileged ports (e.g. 8080 or 3000) and map them to 80/443 at 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_limit exceeds 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.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check