What Is a VPN? Types, Protocols and How It Works
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.
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.
A packet’s journey through the tunnel
- An application on the client sends a normal IP packet addressed to a private office address.
- The VPN software sees that the destination belongs to the tunnel and encrypts the whole packet with the session key.
- It adds a VPN header, an integrity tag and a new outer IP header addressed to the gateway’s public IP.
- Routers on the internet forward the outer packet like any other. They see only the two public addresses.
- 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.
| Feature | IPsec (IKEv2 + ESP) | TLS VPN | WireGuard |
|---|---|---|---|
| Layer | Network layer | Transport and application | Network layer over UDP |
| Key exchange | IKEv2 | TLS handshake | Noise based handshake |
| Algorithms | Negotiated suites | Negotiated suites | One fixed set |
| Typical use | Site-to-site, mixed vendors | Remote access, browser access | Remote access, cloud links |
| Firewall traversal | UDP 500 and 4500 | Often TCP 443 | One 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.
| Quantity | Formula | Symbols and units |
|---|---|---|
| ESP tunnel overhead | O = H_IP + 8 + IV + P + 2 + ICV | H_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 packet | L_max = MTU minus O | MTU of the outside path, bytes |
| TCP maximum segment size | MSS = L_max minus 40 | 20 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
- 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
- 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.
- 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.
- 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.
- 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.
- 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
- IETF RFC 4301, Security Architecture for the Internet Protocol, S. Kent and K. Seo, December 2005
- IETF RFC 4303, IP Encapsulating Security Payload (ESP), S. Kent, December 2005
- IETF RFC 4106, The Use of Galois/Counter Mode (GCM) in IPsec Encapsulating Security Payload (ESP), J. Viega and D. McGrew, June 2005
- IETF RFC 7296, Internet Key Exchange Protocol Version 2 (IKEv2), C. Kaufman et al., October 2014
- IETF RFC 3948, UDP Encapsulation of IPsec ESP Packets, A. Huttunen et al., January 2005
- J. A. Donenfeld, WireGuard: Next Generation Kernel Network Tunnel, Network and Distributed System Security Symposium (NDSS), 2017
- NIST SP 800-77 Rev. 1, Guide to IPsec VPNs, June 2020
- NIST SP 800-113, Guide to SSL VPNs, July 2008
- 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.