Telecommunications and Network Engineering Explanation

What Is a VPN? Types, Protocols and How It Works

What Is a VPN? Types, Protocols and How It Works: featured diagram
Short answer

A VPN (virtual private network) is an encrypted tunnel across a shared network such as the internet. Each private packet is encrypted, authenticated and wrapped inside a new outer packet, so it travels between a client and a gateway, or between two sites, as if both ends shared one private network.

A VPN lets a laptop in a hotel reach a file server in the office, or lets two plants share one private address plan over the public internet. Nobody on the networks in between can read or alter the traffic without being detected.

Three protocol families are in use today: IPsec, TLS and WireGuard. All of them add headers, and those headers change the largest packet you can send.

Watch: How a VPN works in 18 seconds

How a VPN works

A VPN does three jobs at once. It encapsulates each packet inside another one, encrypts the inner packet so it cannot be read, and adds an integrity check so any change on the way is detected and the packet is dropped.

The two ends first agree on keys. IPsec uses IKEv2 (RFC 7296), TLS VPNs use a TLS handshake, and WireGuard uses a handshake based on the Noise protocol framework. After that, every data packet is protected with those session keys.

Untrusted internetencrypted tunnelouter header: public IP to public IPClientVPN appGatewaydecryptsServeroffice LANteal: encryptedorange: plainESP tunnel mode packet with AES-GCM (not to scale)Outer IP20 BSPI + Seq8 BIV8 BOriginal IP packetencryptedPad + 2 BtrailerICV16 Bencryptedintegrity protected by the ICV
A VPN carries each private packet, encrypted and authenticated, inside a new outer packet between public addresses; the gateway removes the wrapping and forwards the original packet.
Download diagram (WebP)

A packet’s journey through the tunnel

  1. An application on the client sends a normal IP packet addressed to a private office address.
  2. The VPN software sees that the destination belongs to the tunnel and encrypts the whole packet with the session key.
  3. It adds a VPN header, an integrity tag and a new outer IP header addressed to the gateway’s public IP.
  4. Routers on the internet forward the outer packet like any other. They see only the two public addresses.
  5. The gateway checks the integrity tag and the sequence number, decrypts the packet and forwards the original packet onto the LAN.

The sequence number gives anti-replay protection. ESP (RFC 4303) puts a counter in every packet so that the receiver drops a copy an attacker records and sends again.

VPN modes: tunnel and transport

IPsec, defined in RFC 4301, has two modes. Tunnel mode wraps the whole original IP packet, header included, and adds a new outer IP header. This is what gateways use, because the private addresses stay hidden inside.

Transport mode keeps the original IP header and protects only the payload behind it. It is used between two hosts that talk to each other directly.

IPsec also defines AH (RFC 4302), which authenticates packets but does not encrypt them. ESP can do both, so nearly all VPNs use ESP.

Types of VPN

  • One user device connects to a gateway at the office or in the cloud.
  • The user authenticates, often with a certificate or multi-factor login, and receives an address from the private range.
  • With full tunnelling all traffic goes through the VPN. With split tunnelling only traffic for company networks does, and the rest goes straight to the internet.
  • Two gateways, usually firewalls or routers, build a permanent tunnel between two networks.
  • Users at either site do nothing; routing sends traffic for the far site into the tunnel.
  • This is the usual way to join a branch office or a remote plant to headquarters over the internet.

Gateways are usually built into the firewall at the network edge, so the decrypted traffic still passes through firewall rules before reaching the LAN.

VPN protocols compared

IPsec works at the network layer. IKEv2 runs over UDP port 500 and authenticates the peers, then sets up security associations (SAs) that hold the keys and algorithms for ESP.

When a NAT device sits in the path, RFC 3948 wraps ESP in UDP on port 4500 so the NAT can track it. NIST SP 800-77 Rev. 1 is the main implementation guide.

A TLS VPN (often called an SSL VPN) uses the same TLS protocol that protects HTTPS. It often runs on TCP port 443, which is open in most networks.

NIST SP 800-113 describes two forms: a browser portal that publishes selected web applications, and a client that tunnels full network traffic.

WireGuard is a layer 3 tunnel over UDP, described by J. A. Donenfeld in 2017 and part of the Linux kernel since version 5.6. It uses one fixed set of algorithms: Curve25519, ChaCha20, Poly1305 and BLAKE2s.

Each peer is identified by its public key. A list of allowed IP addresses ties each key to the networks it may use, which the paper calls cryptokey routing.

FeatureIPsec (IKEv2 + ESP)TLS VPNWireGuard
LayerNetwork layerTransport and applicationNetwork layer over UDP
Key exchangeIKEv2TLS handshakeNoise based handshake
AlgorithmsNegotiated suitesNegotiated suitesOne fixed set
Typical useSite-to-site, mixed vendorsRemote access, browser accessRemote access, cloud links
Firewall traversalUDP 500 and 4500Often TCP 443One UDP port
Where a VPN sits in the OSI model

IPsec and WireGuard protect IP packets, so any application above them is covered without change. A TLS portal works higher up and covers only the applications it publishes. The OSI and TCP/IP model guide shows where each layer fits.

Key formulas for VPN overhead

VPN headers make the packet bigger, but the link MTU (1500 bytes on standard Ethernet) stays the same, so the largest inner packet shrinks by the overhead.

OESP=HIP+8+IV+P+2+ICVO_{ESP} = H_{IP} + 8 + IV + P + 2 + ICV Lmax=MTU−OMSS=Lmax−40η=LL+OL_{max} = MTU - O \qquad MSS = L_{max} - 40 \qquad \eta = \frac{L}{L + O}
QuantityFormulaSymbols and units
ESP tunnel overheadO = H_IP + 8 + IV + P + 2 + ICVH_IP outer IP header (20 B for IPv4), 8 B SPI and sequence number, IV (8 B for AES-GCM), P padding, 2 B pad length and next header, ICV tag (16 B), all in bytes
Largest inner packetL_max = MTU minus OMTU of the outside path, bytes
TCP maximum segment sizeMSS = L_max minus 4020 B inner IPv4 header plus 20 B TCP header
Efficiencyη = L / (L + O)L inner packet length, bytes

ESP padding is chosen so that the encrypted part, from the inner packet to the 2 byte trailer, ends on a 4 byte boundary (RFC 4303). RFC 4106 fixes the AES-GCM IV at 8 bytes and allows an ICV of 8, 12 or 16 bytes.

Worked example: MTU through a site-to-site VPN

Worked example
Given
A site-to-site IPsec tunnel runs over an internet path with a 1500 byte MTU. Outer header IPv4 (20 B), ESP in tunnel mode with AES-GCM: 8 B SPI and sequence number, 8 B IV, 16 B ICV, 2 B trailer. A WireGuard tunnel on the same path adds 60 B over IPv4.
Find
The largest inner packet, the TCP MSS to clamp on the gateway, and the efficiency for each tunnel. Then the outer size of a full 1500 B inner packet sent into the IPsec tunnel.
Assumptions
No NAT in the path, so no UDP wrapper for ESP. Inner traffic is IPv4 and TCP with 20 B headers each.
Solution
IPsec fixed bytes = 20 + 8 + 8 + 16 = 52 B
Room for packet, padding and trailer = 1500 minus 52 = 1448 B, already a multiple of 4, so padding = 0
L_max = 1448 minus 2 = 1446 B
MSS = 1446 minus 40 = 1406 B
η = 1446 / 1500 = 96.4 %
WireGuard: L_max = 1500 minus 60 = 1440 B, MSS = 1440 minus 40 = 1400 B, η = 1440 / 1500 = 96.0 %
Full 1500 B inner packet in IPsec: 1500 + 2 = 1502 B, padded by 2 B to 1504 B
Outer size = 20 + 8 + 8 + 1504 + 16 = 1556 B, which is 56 B over the 1500 B MTU
Answer
IPsec: largest inner packet 1446 B, MSS 1406 B, 96.4 % efficient. WireGuard: 1440 B, MSS 1400 B, 96.0 %. A 1500 B inner packet becomes 1556 B and must be fragmented or dropped.
Limitations
With NAT traversal the extra 8 B UDP header cuts the IPsec limit to 1438 B. IPv6 outer headers are 40 B, which is why WireGuard’s default interface MTU is 1420. Small packets such as voice frames pay the same overhead and are far less efficient.

This is the most common VPN fault. Pages that half load and file transfers that stall are usually large packets that no longer fit. Setting the tunnel MTU or clamping the MSS on the gateway is the standard fix.

What a VPN does not protect

  • The endpoints: malware on the client travels through the tunnel just like normal traffic, so endpoint security still matters.
  • Traffic after the gateway: once decrypted, packets are plain on the LAN or on the far side of a commercial VPN service.
  • Weak authentication: a stolen password on a remote access VPN gives an attacker the same access as the user, so certificates or multi-factor login are the usual protection.

A VPN complements network segmentation such as VLANs and a sound IP addressing plan. The two sites must not use the same private subnet, or routing across the tunnel fails.

VPN interview and viva questions

  1. What is a VPN? A virtual private network is an encrypted, authenticated tunnel across a shared network that carries private packets inside new outer packets between a client and a gateway or between two sites.
  2. What is the difference between IPsec tunnel mode and transport mode? Tunnel mode encrypts the whole original IP packet and adds a new outer IP header, while transport mode keeps the original header and protects only the payload.
  3. Why does ESP carry a sequence number? It gives anti-replay protection, so the receiver drops a recorded copy of a packet that an attacker sends again.
  4. What does IKEv2 do? It authenticates the two peers and sets up the security associations that hold the keys and algorithms ESP uses for data packets.
  5. Why is the TCP MSS clamped on a VPN gateway? VPN headers make every packet bigger, so clamping the MSS keeps full size TCP segments small enough to fit the path MTU after encapsulation.

Frequently asked questions

Does a VPN make me anonymous online?

No. A VPN hides your traffic from the networks between you and the VPN gateway, such as a public Wi-Fi hotspot or your internet provider. The gateway operator can still see the traffic once it is decrypted, and websites can still identify you through logins and cookies. You are trusting the gateway operator instead of the local network.

Is IPsec or WireGuard better?

Both are secure when configured well. IPsec with IKEv2 is an IETF standard supported by almost every firewall and router, which matters for site-to-site links between different vendors. WireGuard has a much smaller design with one fixed set of modern algorithms, which makes it simpler to configure and audit.

Why do some websites stop loading when the VPN is on?

The usual cause is MTU. Encryption headers make each packet bigger, and if a full size packet no longer fits the path it may be dropped, especially when ICMP messages are blocked. Lowering the tunnel MTU or clamping the TCP MSS on the gateway normally fixes it.

References

  1. IETF RFC 4301, Security Architecture for the Internet Protocol, S. Kent and K. Seo, December 2005
  2. IETF RFC 4303, IP Encapsulating Security Payload (ESP), S. Kent, December 2005
  3. IETF RFC 4106, The Use of Galois/Counter Mode (GCM) in IPsec Encapsulating Security Payload (ESP), J. Viega and D. McGrew, June 2005
  4. IETF RFC 7296, Internet Key Exchange Protocol Version 2 (IKEv2), C. Kaufman et al., October 2014
  5. IETF RFC 3948, UDP Encapsulation of IPsec ESP Packets, A. Huttunen et al., January 2005
  6. J. A. Donenfeld, WireGuard: Next Generation Kernel Network Tunnel, Network and Distributed System Security Symposium (NDSS), 2017
  7. NIST SP 800-77 Rev. 1, Guide to IPsec VPNs, June 2020
  8. NIST SP 800-113, Guide to SSL VPNs, July 2008
  9. W. Stallings, Network Security Essentials: Applications and Standards, 6th ed., Pearson

Get one engineering email a week

New articles, worked examples and study notes every week. Free, no spam, unsubscribe any time.

Zenta Engineering

Articles written by the Zenta Engineering editorial staff are checked by our technical team against the references listed on each page, following our editorial policy. Editorial policy.

Related articles