Part 6 · Real networks: infrastructure, security and operations

Firewalls, load balancers, proxies, VPNs and overlay networks

lesson 14 / 1919 min2 labspublished updated
Course contents
On this page
  1. Firewalls
  2. Load balancers
  3. Proxies
  4. VPNs and tunnels
  5. Enterprise WANs: MPLS and SD-WAN
  6. QoS and traffic shaping

Real networks are assembled from a small set of building blocks: firewalls that decide which packets may pass, load balancers that spread traffic across servers, proxies that sit in the middle of connections, and tunnels that carry one network across another. Here you’ll see how each one works, build a stateful firewall and a port-forwarding load balancer with nftables, and finish with enterprise WANs and QoS.

Firewalls

A firewall is a filter that looks at each packet and decides, according to rules you write, whether it may pass. You already know the information it can decide on: addresses (lesson 4), protocols and ports (lesson 7), and, for some firewalls, whole HTTP requests (lesson 9). The main difference between types is how much each one remembers and understands:

TypeDecides based onNotes
Stateless (packet filter, ACL: access control list)Each packet on its own: IP, port, protocolFast; you must also write rules for return traffic (like AWS NACLs)
StatefulConnection state (via conntrack, the connection table you met with NAT in lesson 5)Replies are allowed automatically; today’s standard (cloud security groups, iptables with ESTABLISHED,RELATED)
NGFW (“next-generation” firewall) / L7Application, user, content, even decrypted TLSSlower and more complex
WAF (web application firewall)HTTP requests: SQL injection, XSS, botsSits in front of web apps, often in the CDN

The design principle is default deny: everything is forbidden except what is explicitly needed. Be strict about inbound traffic, and restrict outbound traffic too (at least on servers).

netfilter in Linux: iptables and nftables

The Linux kernel has hooks at several points along a packet’s path, and your rules run at those hooks. Think of them as callbacks the kernel invokes at fixed stages of a pipeline:

incoming packet
 -> PREROUTING (nat: DNAT)
 -> routing decision:
    to this host: INPUT -> process
    to forward:   FORWARD
                  -> POSTROUTING -> out
local process
 -> OUTPUT -> POSTROUTING (SNAT) -> out
  • Tables: filter (allow or reject), nat (DNAT in PREROUTING, SNAT in POSTROUTING), mangle and raw.
  • nftables is the successor to iptables. On recent distributions, the iptables command itself often runs on top of nftables.
# a minimal stateful host firewall with nftables
sudo nft -f - <<'EOF'
table inet host {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    iif lo accept
    ip protocol icmp accept
    ip6 nexthdr icmpv6 accept
    tcp dport { 22, 443 } accept
  }
}
EOF
sudo nft list ruleset
table inet host {
	chain input {
		type filter hook input priority filter; policy drop;
		ct state established,related accept
		iif "lo" accept
		ip protocol icmp accept
		tcp dport { 22, 443 } accept
	}
}

Rule order matters. First the chain accepts replies on established connections (so return traffic works), then loopback and ICMP, and finally inbound traffic on only two ports: 22 (SSH) and 443 (HTTPS). Everything else falls through to policy drop. That is a complete, safe firewall for a server.

Load balancers

L4 vs L7

L4 (transport)L7 (application)
What it seesIP and portThe whole HTTP or gRPC request
Unit of distributionConnectionEach request
TLSPasses it through or terminates itUsually terminates it
FeaturesFast; any TCP or UDP protocolRouting by host and path, retries, headers, rate limits, canaries
ExamplesAWS NLB, IPVS, MetalLBAWS ALB, NGINX, Envoy, HAProxy (both modes)

Algorithms and details

  • Round robin (and weighted round robin), least connections, least response time, and random of two (pick two backends at random, use the less loaded one).
  • Hashing on the IP or a header, to pin a client to one backend. With consistent hashing, adding or removing a backend moves only a small share of the keys, which matters a lot for caches.
  • Health checks: active (the LB sends probes itself) and passive (based on real errors). An unhealthy backend is taken out of rotation.
  • Sticky sessions, usually via a cookie. They make things simple, but they spread load poorly and make deploys harder. It’s better to move state out of the server.
  • Connection draining: before a backend is removed, let its in-flight requests finish.
  • DSR (Direct Server Return): responses go straight from the backend to the client without passing back through the LB. Useful for heavy outbound traffic.
  • The client’s real IP: behind an L7 LB, the source IP is always the LB’s own; the original IP arrives in X-Forwarded-For. Behind an L4 LB or a chain of proxies, use the PROXY protocol (from HAProxy), which sends the original connection details at the start of the TCP connection.
  • High availability for the LB itself: an active/passive pair sharing a virtual IP (VRRP with keepalived, using gratuitous ARP), or anycast and ECMP at large scale.

Proxies

  • Forward proxy: the client knows about it and is configured to use it (HTTP_PROXY, HTTPS_PROXY, NO_PROXY). For HTTPS the client uses the CONNECT method, and the proxy can’t see the content (unless it does TLS interception with a company CA installed on the devices).
  • Reverse proxy: the client thinks it’s talking to the server itself.
  • Transparent proxy: traffic is steered to the proxy without the client knowing, using DNAT or policy routing.
  • SOCKS5: a general-purpose proxy at the TCP and UDP level (for example, ssh -D).

VPNs and tunnels

A tunnel puts one packet inside another (encapsulation on top of encapsulation) so it can cross a network it couldn’t cross on its own. A VPN is an encrypted tunnel.

TechnologyLayer and transportNotes
IPsecLayer 3; ESP (IP protocol 50), IKEv2 over UDP 500 and 4500 (NAT-T)The standard for site-to-site links between companies and clouds; complex
WireGuardLayer 3; UDP (usually 51820)Modern, little code, fast, Curve25519 and ChaCha20; built into the Linux kernel
OpenVPNLayer 3 or 2; over TLS, on UDP or TCPOlder, flexible
GREUnencrypted tunnel; IP protocol 47Simple; often combined with IPsec
VXLANLayer 2 frames inside UDP 4789Data center overlays, Docker Swarm, Kubernetes (Flannel, Calico in VXLAN mode)
GENEVELike VXLAN with an extensible header; UDP 6081AWS Gateway Load Balancer, OVN, Cilium
# wg0.conf — a minimal WireGuard peer
[Interface]
PrivateKey = <this machine's private key>
Address = 10.8.0.2/24

[Peer]
PublicKey = <server public key>
Endpoint = vpn.example.com:51820
# which destinations go through the tunnel (split tunnel)
AllowedIPs = 10.8.0.0/24, 10.20.0.0/16
# keep NAT mappings alive
PersistentKeepalive = 25
  • Split tunnel: only traffic for specific networks goes through the VPN. Full tunnel: all traffic does (AllowedIPs = 0.0.0.0/0, ::/0).
  • Every tunnel adds header overhead and reduces the MTU: by about 60 to 80 bytes for WireGuard and about 50 bytes for VXLAN. Remember the MTU black hole: inside a tunnel, set the MTU correctly or clamp the MSS.
  • Site-to-site (network to network) vs remote access (user to network). For users, the ZTNA model (identity-based access to each application, not to the whole network) is replacing the traditional VPN.

Enterprise WANs: MPLS and SD-WAN

  • MPLS: instead of looking up the IP address, the ISP’s routers forward based on a short label added to the packet at the edge of the network. ISPs use it to build their own backbones and to sell “MPLS VPN” services: a private network between a company’s branches with guaranteed quality.
  • SD-WAN: instead of (or alongside) MPLS, branches connect over several ordinary Internet links (fiber, 4G, …) through encrypted tunnels, and a central controller picks the path based on live link quality and the type of application. It’s cheaper and more flexible than MPLS.

QoS and traffic shaping

  • DSCP (6 bits in the IP header) marks the traffic class (for example, EF for voice). It only means something inside a network you control; on the public Internet it’s usually ignored or wiped.
  • Bufferbloat: big queues in routers push latency under load up to whole seconds. AQM algorithms such as fq_codel and CAKE (the default in recent Linux and in good routers) fix this.
  • In Linux, tc handles rate limiting, queuing, and simulating a bad network with netem (which you met in the TCP lesson).