Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท VPNs and Tunnelling

VPN Fundamentals: Tunnels, Types and Threat Models

Learn what a VPN really provides, how tunnelling and encapsulation work, site-to-site versus remote-access VPNs, split tunnelling and limits, with a toy tunnel in Python.

A virtual private network (VPN) creates a protected path across a network you do not trust, usually the internet. Traffic that goes into one end of the VPN is wrapped, usually encrypted and authenticated, and carried inside ordinary packets to the other end, where it is unwrapped. To the applications at both ends it looks as if the two sites sit on the same private network.

VPNs solve two old problems: connecting offices to each other without leasing private lines, and letting remote workers reach internal services safely from anywhere. They are also heavily marketed to consumers, often with claims that are bigger than what a VPN actually does. This lesson explains the real mechanics and limits first, so the chapters on IPsec, WireGuard, OpenVPN and SSH make sense.

What a VPN provides

A properly built VPN gives you the CIA properties on the path between its two endpoints:

  • Confidentiality: an observer on the path sees only encrypted packets between the endpoints.
  • Integrity: tampering is detected through authentication tags.
  • Authentication: each endpoint proves who it is with keys, certificates or credentials.
  • Replay protection: captured packets resent later are rejected.

What it does not provide:

  • Protection beyond the VPN endpoint. Once traffic leaves the VPN server for the open internet, it is as exposed as any other traffic (unless it uses TLS itself).
  • Anonymity. The VPN operator can see your traffic metadata, and the sites still see the exit address.
  • Safety from a compromised device. If malware runs on the laptop, the tunnel simply carries its traffic into your network.
  • Protection from phishing, weak passwords or vulnerable services.

Tunnelling and encapsulation

A tunnel is made by encapsulation: take a whole original packet (the inner packet, with the private addresses), protect it, and put it as the payload of a new outer packet addressed between the two tunnel endpoints.

flowchart LR
    A["PC 10.0.0.5"] -->|"inner: 10.0.0.5 to 10.1.0.9"| G1["Gateway A 203.0.113.1"]
    G1 -->|"outer: 203.0.113.1 to 198.51.100.2 (inner packet encrypted)"| G2["Gateway B 198.51.100.2"]
    G2 -->|"inner: 10.0.0.5 to 10.1.0.9"| S["Server 10.1.0.9"]

The internet only ever sees the outer addresses. The private addresses travel inside the encrypted payload.

Mode matters. In tunnel mode the entire original packet, including its IP header, is encapsulated. In transport mode only the payload is protected and the original header stays (used between two hosts that talk directly). Tunnelling protocols that do not encrypt, such as GRE, VXLAN and IP-in-IP, move traffic across networks but give no security on their own; they are often wrapped inside IPsec.

Types of VPN

Type Connects Typical use Common protocols
Site-to-site Network to network, between two gateways Branch office to headquarters, data centre to cloud IPsec, WireGuard, GRE over IPsec
Remote access One user's device to a gateway Staff working from home or travelling IPsec/IKEv2, OpenVPN, WireGuard, SSL/TLS VPN
Host-to-host Two machines Securing a database replication link IPsec transport mode, WireGuard
Clientless / browser A user to specific web applications through a portal Contractors needing one internal app TLS reverse proxy
Overlay / mesh Many devices to each other directly Modern "zero trust" networks WireGuard-based meshes

The decision is rarely about which protocol is "best", but about: what clients you must support, how you manage keys and users, how much performance you need, and whether the gateway can be reached through restrictive networks (TLS-based VPNs on port 443 pass through most hotel and corporate firewalls).

Full tunnel versus split tunnel

For a remote-access VPN you decide which traffic goes through the tunnel:

  • Full tunnel: all traffic, including web browsing, goes to the VPN gateway. You can apply corporate filtering and logging to everything, but bandwidth and latency suffer, and the gateway carries personal traffic.
  • Split tunnel: only traffic to corporate networks uses the VPN; the rest goes directly to the internet. It is faster and cheaper, but the device is simultaneously on the internet and the corporate network, so a compromise of the device can bridge the two, and corporate filtering does not see the direct traffic.

The decision is made with routes on the client: the VPN adds routes for the corporate subnets (split) or a default route (full). The longest-prefix match rule decides which route wins.

Python Split versus full tunnel: which route wins?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how the full-tunnel trick works: instead of replacing the default route, the VPN adds two more specific routes (0.0.0.0/1 and 128.0.0.0/1) that together cover the whole internet, so they beat the default route, while the local LAN's /24 is still more specific and stays local. Real clients do exactly this.

A toy tunnel

To see encapsulation concretely, the program below builds and parses a toy "tunnel packet". It is a teaching stand-in: it uses a simple hash-based keystream and an HMAC for authentication, which is fine to show the structure but must never be used to protect real data. Real VPNs use vetted ciphers such as AES-GCM or ChaCha20-Poly1305 inside well-reviewed protocols.

Python Toy tunnel: encapsulate, authenticate, tamper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first print shows what a network observer sees: only the outer addresses and an unreadable payload. A single flipped bit makes the authentication tag fail, so the gateway discards the packet. That is integrity at work. (The real protocols also add replay protection, covered in the next lesson.)

Choosing and operating a VPN

  • Authenticate strongly: certificates or keys, plus multi-factor authentication for remote users. A VPN login that is only a reusable password is a prime target; credential stuffing against VPN portals is routine.
  • Patch the gateway: VPN gateways face the internet and have been among the most exploited devices. Subscribe to vendor advisories and update quickly.
  • Limit what the VPN reaches: give users access to specific networks or applications (least privilege), not the entire internal network. A VPN is not a reason to trust the device.
  • Check device health before allowing a connection (patch level, disk encryption) where you can.
  • Log connections and send logs to your central monitoring.
  • Plan MTU: encapsulation adds overhead, so packets get bigger; set MTU/MSS values so you do not get mysterious stalls on large transfers.
  • Consider the alternative: for single applications, a reverse proxy or zero trust access broker (later lesson) can be safer than a network-level VPN.

Consumer "VPN services" move your trust from your local network or ISP to the VPN company. They can help on untrusted Wi-Fi, but they are not a security cure-all, and they do not make you anonymous.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a VPN NOT protect against?

    A VPN protects the path. A compromised endpoint simply sends its traffic through the tunnel into your network.
  2. 2. In tunnel mode, what is encapsulated?

    Tunnel mode wraps the whole inner packet in a new outer packet between the tunnel endpoints.
  3. 3. Which VPN design lets personal web browsing go directly to the internet while corporate traffic uses the tunnel?

    Split tunnelling adds routes only for corporate networks, so everything else uses the normal path.
  4. 4. Why do full-tunnel VPN clients often add two routes, 0.0.0.0/1 and 128.0.0.0/1, instead of replacing the default route?

    The two /1 routes cover the whole address space and are more specific than 0.0.0.0/0, so they take precedence without deleting the original route.
  5. 5. Which is a sound practice for a remote-access VPN?

    VPN gateways are prime targets. Strong authentication, quick patching, narrow access and logs reduce the risk.

Summary

  • A VPN encapsulates traffic in a protected tunnel across an untrusted network, giving confidentiality, integrity, authentication and replay protection between its endpoints.
  • It does not protect beyond the endpoint, hide you completely or fix a compromised device.
  • Types: site-to-site, remote access, host-to-host, clientless and mesh; full tunnel sends everything through, split tunnel only corporate traffic.
  • Route tables and longest-prefix matching decide what enters the tunnel.
  • Operate VPNs with MFA, fast patching, least-privilege access, logging and sensible MTU settings.

Next: IPsec: IKE, ESP and AH Explained