Network & delivery
9 min readUpdatedWhat is an IPsec VPN? ESP, AH, IKEv2 and tunnel mode explained
Quick answer
An IPsec VPN encrypts and authenticates traffic at the IP layer, usually between two gateways (site to site) or between a device and a gateway (remote access). IKEv2 authenticates the peers and negotiates keys over UDP port 500, or 4500 behind NAT. The data then travels in ESP, IP protocol 50, which encrypts each packet and checks its integrity. Tunnel mode wraps the whole original packet in a new one; transport mode protects only its payload.
01What is IPsec?
IPsec is a suite of IETF protocols that protects IP packets themselves rather than one application's connection. Its architecture is defined in RFC 4301 (December 2005): a security policy database decides which traffic must be protected, and security associations (SAs) hold the keys and algorithms for each direction. Because it works below TCP and UDP, everything that crosses the tunnel is protected, whatever the application.
An IPsec VPN is its most common use: two offices joined over the internet, a cloud network linked to a data centre, or laptops connecting to a company gateway. IKEv2 authenticates the peers and agrees keys, ESP carries the protected packets and AH, rarely used today, offers integrity without encryption.
| Component | What it does | On the wire | Specified in |
|---|---|---|---|
| IKEv2 | Authenticates the peers, negotiates algorithms, creates and rekeys SAs | UDP 500; UDP 4500 once NAT is detected | RFC 7296 |
| ESP | Encrypts and integrity-protects each packet | IP protocol 50, or inside UDP 4500 behind NAT | RFC 4303 |
| AH | Integrity and origin authentication, no encryption | IP protocol 51 | RFC 4302 |
| NAT traversal | Wraps ESP in UDP so NAT devices can track it | UDP 4500 | RFC 3948 |
| TCP encapsulation | Carries IKE and ESP where UDP is blocked | TCP 4500, which implementations must support | RFC 9329 |
02How does IKEv2 set up an IPsec tunnel?
IKEv2 normally needs four messages to bring a tunnel up. RFC 7296 defines them as two exchanges of a request and a response each, IKE_SA_INIT and IKE_AUTH, which establish the IKE SA and the first Child SA. Later exchanges add SAs, rekey them and report errors:
- IKE_SA_INIT: the peers offer and choose algorithms, exchange Diffie-Hellman values and nonces, and detect whether NAT sits between them; everything after this exchange is encrypted
- IKE_AUTH: each side proves its identity with a certificate, a pre-shared key or EAP, and the first Child SA, which carries the ESP traffic, is created with its traffic selectors
- CREATE_CHILD_SA: adds further Child SAs and rekeys existing ones before their lifetimes expire
- INFORMATIONAL: deletes SAs, reports errors and, when sent empty, checks that the peer is still alive
03ESP vs AH: what is the difference?
The Encapsulating Security Payload (ESP) encrypts the packet and protects its integrity. The Authentication Header (AH) only protects integrity, but extends it to the unchanging fields of the outer IP header, including both addresses. That extra coverage is AH's undoing: a NAT device must rewrite the source address, which breaks AH's integrity check, so AH cannot cross NAT.
The standards reflect this. RFC 4301 requires implementations to support ESP and makes AH optional, and RFC 8221 notes that ESP with NULL encryption, which gives integrity only, is preferred over AH because of NAT traversal. In practice ESP is IPsec, and AH appears mostly in older configurations.
| ESP | AH | |
|---|---|---|
| IP protocol number | 50 | 51 |
| Confidentiality | Yes, or NULL encryption for integrity only | No |
| Integrity covers | ESP header, payload and trailer | Payload plus immutable outer IP header fields |
| Works through NAT | Yes, inside UDP 4500 | No |
| Requirement in RFC 4301 | Must be implemented | May be implemented |
| Header | 8 bytes (SPI and sequence number), plus IV, trailer and ICV | 12 bytes plus ICV |
ESP, tunnel mode
[ new IP ][ SPI 4 | Seq 4 ][ IV ][ original IP | TCP/UDP | data | pad | padlen 1 | next 1 ][ ICV ]
|<------------------ encrypted ------------------------>|
|<---------------------------- integrity-protected ---------------------------->|
ESP, transport mode
[ IP ][ SPI 4 | Seq 4 ][ IV ][ TCP/UDP | data | pad | padlen 1 | next 1 ][ ICV ]
AH, transport mode (the ICV also covers the IP header's immutable fields)
[ IP ][ next 1 | len 1 | reserved 2 | SPI 4 | Seq 4 | ICV ][ TCP/UDP | data ]04IPsec tunnel mode vs transport mode
Tunnel mode encrypts the entire original packet and puts it behind a new IP header addressed to the far gateway, so an observer sees only the two gateway addresses. Site-to-site and remote-access VPNs use it, because the gateways protect traffic for whole networks behind them.
Transport mode keeps the original IP header and protects only what follows it. It saves one IP header and suits host-to-host protection where the endpoints are the communicating hosts. Its best-known use is GRE over IPsec: a GRE tunnel supplies the outer addresses and the ability to carry routing protocols, multicast and IPv6, and transport-mode ESP encrypts the GRE packets between the same two endpoints. For GRE itself, see what GRE tunnelling is.
| Tunnel mode | Transport mode | |
|---|---|---|
| What is protected | The whole original packet, header included | The payload after the IP header |
| Addresses an observer sees | The two gateways only | The real source and destination |
| Extra IP header | Yes, 20 bytes on IPv4 | No |
| Typical use | Site-to-site and remote-access VPNs | Host to host; GRE over IPsec |
05Which ports and protocols does IPsec need?
Three things must pass between the peers: UDP 500 for IKE, UDP 4500 for IKE and ESP once NAT is detected, and IP protocol 50 for ESP when there is no NAT. AH, if used, is IP protocol 51. Protocols 50 and 51 have no ports, so a firewall that opens only UDP lets the tunnel negotiate and then pass no traffic, a classic half-working symptom.
Behind NAT, RFC 3948 wraps ESP in UDP 4500 and marks IKE messages on that port with four zero bytes so the receiver can tell the two apart. Where UDP is blocked entirely, RFC 9329 defines IKE and ESP over TCP port 4500.
The ruleset below admits IKE and ESP only from known peers and accepts traffic that arrived inside an SA after decryption. It is a complete input chain with a default drop, so adapt the SSH rule and the peer addresses first; the wider principles are in firewall configuration.
table inet ipsec_edge {
set ike_peers {
type ipv4_addr
elements = { 198.51.100.7, 203.0.113.20 }
}
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
ct state invalid drop
iifname "lo" accept
tcp dport 22 accept
ip protocol icmp accept
meta l4proto ipv6-icmp accept
# IKEv2 and NAT traversal (UDP 500 and 4500), ESP (IP protocol 50)
ip saddr @ike_peers udp dport { 500, 4500 } accept
ip saddr @ike_peers ip protocol esp accept
# traffic that arrived inside an IPsec SA, after decryption
meta ipsec exists accept
}
}
# watch negotiation and ESP on the uplink
tcpdump -ni eth0 'udp port 500 or udp port 4500 or ip proto 50'
# list the kernel's SAs without printing keys, and its policies (Linux)
ip xfrm state list nokeys
ip xfrm policy list06IPsec overhead and MTU: a worked example
Every layer of encapsulation takes bytes from each packet, and the inner MTU must shrink to match, or full-size packets are dropped or fragmented. The figures assume a 1,500-byte IPv4 path and AES-GCM: an 8-byte ESP header, an 8-byte IV (RFC 4106), a 2-byte trailer, a 16-byte integrity tag, and padding so the encrypted part ends on a 4-byte boundary (RFC 4303). AES-CBC with HMAC-SHA-256 costs more, because its IV is 16 bytes and it pads to 16-byte blocks.
Cisco's guidance for GRE over IPsec recommends an inner MTU of 1,400 bytes because it covers the common mode and algorithm combinations; the table shows how much headroom that leaves. Plain GRE is included for comparison, and GRE vs IPIP shows its arithmetic; the MTU and MSS calculator works out other combinations.
| Encapsulation on a 1,500-byte IPv4 path | Overhead at full size | Inner MTU | TCP MSS (IPv4) |
|---|---|---|---|
| Plain GRE, no encryption | 24 bytes | 1,476 | 1,436 |
| ESP tunnel mode, AES-GCM | 54 bytes | 1,446 | 1,406 |
| ESP tunnel mode, AES-GCM, behind NAT (UDP 4500) | 62 bytes | 1,438 | 1,398 |
| ESP tunnel mode, AES-CBC with HMAC-SHA-256 | 62 bytes | 1,438 | 1,398 |
| GRE over IPsec, transport mode, AES-GCM | 58 bytes | 1,442 | 1,402 |
| GRE over IPsec, tunnel mode, AES-GCM | 78 bytes | 1,422 | 1,382 |
| WireGuard, IPv4 outer header | 60 bytes | 1,440 | 1,400 |
ESP tunnel, AES-GCM: 1500 - 20 IP - 8 ESP - 8 IV - 16 ICV = 1448 for packet + pad + 2
1448 - 2 trailer = 1446 inner MTU -> MSS 1406
same, behind NAT: 1448 - 8 UDP = 1440; - 2 = 1438 inner MTU -> MSS 1398
ESP tunnel, AES-CBC: 1500 - 20 - 8 - 16 IV - 16 ICV = 1440 (90 blocks of 16)
1440 - 2 trailer = 1438 inner MTU -> MSS 1398
GRE over IPsec, transport 1448 - 4 GRE - 2 = 1442 inner MTU -> MSS 1402
GRE over IPsec, tunnel 1448 - 20 IP - 4 GRE - 2 = 1422 inner MTU -> MSS 1382
WireGuard over IPv4: 1500 - 20 IP - 8 UDP - 16 header - 16 tag = 1440 -> MSS 1400
WireGuard default MTU: 1500 - 80 (sized for an IPv6 outer header) = 142007IPsec vs WireGuard vs OpenVPN vs GRE
All four build tunnels, but only three encrypt, and they differ in how they authenticate, what they need from the firewall and what they cost per packet. GRE is included because it is so often paired with IPsec; on its own it provides no confidentiality at all. In short:
- IPsec when the other end is a firewall, router or cloud VPN gateway, because that is the protocol they all speak
- WireGuard when you control both ends and want a small configuration and low overhead
- OpenVPN when only one TCP or UDP port gets through, or existing clients depend on it
- GRE over IPsec when routing protocols, multicast or several address families must cross an encrypted link
| IPsec (IKEv2 + ESP) | WireGuard | OpenVPN | GRE | |
|---|---|---|---|---|
| Encryption | Negotiated; AES-GCM mandatory to implement | Fixed: ChaCha20-Poly1305 | Negotiated, e.g. AES-GCM | None |
| Authentication | IKEv2 with certificates, pre-shared keys or EAP | Noise IK handshake with static public keys | TLS with certificates | None |
| On the wire | UDP 500 and 4500, IP protocol 50 | One UDP port chosen by the operator | UDP 1194 by default, or TCP | IP protocol 47 |
| Through NAT | Yes, via UDP 4500 | Yes | Yes | Poorly: no ports for NAT to track |
| Overhead, 1,500-byte IPv4 path | 54 to 78 bytes, as above | 60 bytes | Depends on options; mssfix targets 1,492-byte packets by default | 24 bytes |
| Specification | IETF RFCs 4301, 4303 and 7296 | Protocol published by its author, no RFC | Project documentation | RFC 2784 |
08IPsec and DDoS: what a VPN can and cannot protect
An IPsec gateway is an exposed service in its own right. IKE responders listen on public UDP ports and must do expensive cryptography for strangers, and RFC 7296 names state and CPU exhaustion through session requests from forged addresses as the attacks to expect. Its answer is a stateless cookie: under load the responder replies to the first message with a COOKIE notification and commits no state until the initiator repeats it from a real address. RFC 8019 adds client puzzles and further hardening for DDoS conditions.
Encryption does not make a link harder to flood. A volumetric UDP flood aimed at the gateway's address fills the uplink before any IPsec processing happens, and the tunnel goes down with the link. Filtering has to happen upstream, at network level, and it cannot look inside ESP: an upstream filter sees outer addresses, protocol 50 or UDP 4500, and packet rates. A web reverse proxy cannot help either, because IKE and ESP are not HTTP; Layer 3/4 vs Layer 7 explains the difference.
- Accept IKE and ESP only from known peers, as in the ruleset above; it sheds load from strangers but cannot stop a flood that fills the link
- Keep cookies and, where supported, puzzles enabled on the IKE responder
- Use upstream network-level DDoS filtering for gateway addresses, the only place a flood larger than the uplink can be dropped
- Give critical sites a second gateway address or uplink, so one flooded address does not cut them off
09IPsec timeline: from RFC 1825 to the end of IKEv1
- August 1995: RFC 1825 publishes the first IPsec security architecture
- November 1998: RFC 2401 revises the architecture and RFC 2409 defines IKE, now known as IKEv1
- January 2005: RFC 3948 standardises UDP encapsulation of ESP for NAT traversal
- December 2005: RFC 4301 and RFC 4303 define today's architecture and ESP; RFC 4306 introduces IKEv2
- October 2014: IKEv2 becomes an Internet Standard as RFC 7296
- October 2017: RFC 8221 makes AES-GCM mandatory to implement for ESP
- November 2022: RFC 9329 specifies IKE and ESP over TCP
- April 2023: RFC 9395 deprecates IKEv1
Questions
Frequently asked questions
Sources
Sources and verification
The nftables ruleset was checked with nft 1.0.6 (nft -c inside an isolated network namespace), the capture filter compiled with tcpdump 4.99.3 (tcpdump -d) and the ip xfrm commands run with iproute2 6.1.0 in an isolated network namespace, on Debian 12, October 2026. The overhead figures are calculated from the RFC field sizes, not measured on a live tunnel.
- 01RFC 4301: Security Architecture for the Internet Protocol · RFC Editor
- 02RFC 4303: IP Encapsulating Security Payload (ESP) · RFC Editor
- 03RFC 4302: IP Authentication Header · RFC Editor
- 04RFC 7296: Internet Key Exchange Protocol Version 2 (IKEv2) · RFC Editor
- 05RFC 3948: UDP Encapsulation of IPsec ESP Packets · RFC Editor
- 06RFC 9329: TCP Encapsulation of IKE and IPsec Packets · RFC Editor
- 07RFC 4106: The Use of Galois/Counter Mode (GCM) in IPsec ESP · RFC Editor
- 08RFC 3602: The AES-CBC Cipher Algorithm and Its Use with IPsec · RFC Editor
- 09RFC 4868: Using HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 with IPsec · RFC Editor
- 10RFC 8221: Cryptographic Algorithm Implementation Requirements for ESP and AH · RFC Editor
- 11RFC 9395: Deprecation of the IKEv1 Protocol and Obsoleted Algorithms · RFC Editor
- 12RFC 8019: Protecting IKEv2 Implementations from Distributed Denial-of-Service Attacks · RFC Editor
- 13RFC 1825: Security Architecture for the Internet Protocol (1995) · RFC Editor
- 14RFC 2409: The Internet Key Exchange (IKE) · RFC Editor
- 15RFC 2784: Generic Routing Encapsulation (GRE) · RFC Editor
- 16Assigned Internet Protocol Numbers · IANA
- 17WireGuard protocol and cryptography · WireGuard
- 18Linux WireGuard driver: default MTU calculation (device.c) · Linux kernel source
- 19OpenVPN 2.6 manual (--port, --proto, --mssfix) · OpenVPN
- 20Resolve IPv4 Fragmentation, MTU, MSS, and PMTUD Issues with GRE and IPsec · Cisco
Last updated