VPNs and secure tunnels

Introduction

When you are away from campus, the university's library database and internal portal will not open: they only accept connections from the campus network. The university's VPN (virtual private network) fixes that. Your laptop builds an encrypted tunnel to a VPN server on campus, and from then on your traffic for campus systems travels through it, as if your laptop were plugged in at the library.

VPNs are also sold to the public with big promises. This lesson explains what a VPN really does, what it hides from whom, and what it cannot do.

How it works

A VPN works by encapsulation: each packet you send is encrypted and placed inside a new packet addressed to the VPN server.

  1. Your app sends a normal packet, for example to 10.20.1.5, a campus address.
  2. The VPN software encrypts that whole packet and puts it inside a new one: from your laptop to the VPN server.
  3. The VPN server decrypts it and sends the original packet on, inside the campus network.
  4. Replies travel back the same way.

Everyone between your laptop and the VPN server (the café Wi-Fi, your mobile operator) sees only encrypted packets going to the VPN server. Here is who can see what:

WhoWithout a VPNWith a VPN
The local Wi-Fi and your internet providerWhich servers you talk to (and the site names in TLS handshakes)Only that you talk to the VPN server, and how much
The VPN providerNothingWhich servers you talk to: you have moved your trust to it
The websiteYour real IP addressThe VPN server's IP address

Two things a VPN does not do: it does not make a website safe (malware and phishing work the same inside a tunnel), and it does not make you anonymous to sites you log in to, since your account and cookies still identify you.

The wrapping costs space. WireGuard, a popular modern VPN protocol, adds 60 bytes to each IPv4 packet, so inner packets must be smaller than the link allows.

// A VPN wraps each packet inside another one. WireGuard over IPv4 adds 60 bytes:
// a new IPv4 header (20), a UDP header (8), WireGuard's own header (16) and an authentication tag (16).
const mtu = 1500; // the largest packet an Ethernet link carries
const overhead = 20 + 8 + 16 + 16;
console.log("overhead per packet: " + overhead + " bytes");
console.log("largest inner packet that fits: " + (mtu - overhead) + " bytes");

for (const inner of [500, 1400, 1480]) {
  const outer = inner + overhead;
  console.log("inner " + inner + " -> outer " + outer + (outer <= mtu ? " fits" : " too big: must be split or rejected"));
}
Open in compiler

Example

Sending everything through the VPN is not always wanted. Split tunnelling sends only the campus networks through the tunnel and everything else directly, which keeps video calls fast and the VPN server unloaded.

// Split tunnelling: only traffic for the university's network goes through the VPN; the rest goes directly.
function toNumber(ip) {
  const parts = ip.split(".").map(Number);
  return ((parts[0] * 256 + parts[1]) * 256 + parts[2]) * 256 + parts[3];
}
function inBlock(ip, block) {
  const [base, bits] = block.split("/");
  const size = 2 ** (32 - Number(bits));
  return Math.floor(toNumber(ip) / size) === Math.floor(toNumber(base) / size);
}

const tunnelled = ["10.0.0.0/8", "172.16.0.0/12"]; // the campus networks
for (const ip of ["10.20.1.5", "172.20.9.9", "142.250.183.14", "172.32.0.1"]) {
  const viaVpn = tunnelled.some((b) => inBlock(ip, b));
  console.log(ip.padEnd(15) + " -> " + (viaVpn ? "through the VPN tunnel" : "directly to the internet"));
}
Open in compiler

Line by line

  1. toNumber turns an IPv4 address into one 32-bit number, so blocks can be compared with arithmetic.
  2. inBlock checks whether an address is inside a CIDR block: divide both by the block size (2 to the power of the host bits) and compare.
  3. tunnelled lists the campus blocks: 10.0.0.0/8 and 172.16.0.0/12 (which covers 172.16.0.0 to 172.31.255.255).
  4. 10.20.1.5 and 172.20.9.9 are inside, so they go through the tunnel; a public address and 172.32.0.1, just past the /12 block, go directly.

Expected result

10.20.1.5       -> through the VPN tunnel
172.20.9.9      -> through the VPN tunnel
142.250.183.14  -> directly to the internet
172.32.0.1      -> directly to the internet

In the real world

  • Companies and universities use VPNs so staff can reach internal systems; newer "zero trust" setups instead check each user and device at every application.
  • Common VPN protocols are IPsec, OpenVPN and WireGuard. WireGuard is small and fast, and its default tunnel size (MTU) of 1420 bytes leaves room for the larger IPv6 overhead too.
  • Site-to-site VPNs join two offices' networks permanently, with no software on each laptop.
  • Some countries restrict or block VPN use, and some VPN apps have been found to log or sell user data; choosing a provider is a decision about whom to trust.

Common mistakes

  • Believing a VPN makes you anonymous. Logged-in accounts, cookies and browser details still identify you.
  • Believing a VPN stops malware or phishing. It protects the path, not the destination.
  • Forgetting the provider sees your traffic's destinations. You have moved trust, not removed it.
  • Exposing a VPN server with weak passwords or old software. VPN servers are a favourite target, because they open the door to the inside.

Best practices

  • Use a VPN on networks you do not trust, such as public Wi-Fi, and for reaching private systems.
  • Keep HTTPS everywhere anyway: a VPN and TLS protect different parts of the path.
  • Protect VPN logins with a second factor, and keep VPN servers patched.
  • Use split tunnelling only when you understand which traffic leaves the tunnel.

Summary

A VPN encrypts your packets and wraps them inside packets to a VPN server, which unwraps and forwards them. Local networks and your provider then see only encrypted traffic to the VPN server, the websites see the VPN server's address, and the VPN provider takes over the view your provider had. It protects the path, not the destination, and it does not make you anonymous. The wrapping adds overhead, such as WireGuard's 60 bytes per IPv4 packet, and split tunnelling sends only chosen networks through it.

Quick check

1. You use a VPN on café Wi-Fi to visit a website. Who sees which website you visit?
2. WireGuard adds 60 bytes to each IPv4 packet, and the link carries at most 1500. What is the largest inner packet that fits?
3. Which threat does a VPN NOT protect you from?
4. With split tunnelling set to 10.0.0.0/8, where does traffic to 11.0.0.1 go?

Key takeaways

  • A VPN hides your traffic from the local network and your provider, and moves that view to the VPN provider.
  • It protects the path, not the destination: phishing and malware still work.
  • Encapsulation adds overhead, such as WireGuard's 60 bytes per IPv4 packet.
  • Use HTTPS as well; VPNs and TLS protect different parts of the path.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.