Network & delivery

9 min readUpdated

What 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.

ComponentWhat it doesOn the wireSpecified in
IKEv2Authenticates the peers, negotiates algorithms, creates and rekeys SAsUDP 500; UDP 4500 once NAT is detectedRFC 7296
ESPEncrypts and integrity-protects each packetIP protocol 50, or inside UDP 4500 behind NATRFC 4303
AHIntegrity and origin authentication, no encryptionIP protocol 51RFC 4302
NAT traversalWraps ESP in UDP so NAT devices can track itUDP 4500RFC 3948
TCP encapsulationCarries IKE and ESP where UDP is blockedTCP 4500, which implementations must supportRFC 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.

ESPAH
IP protocol number5051
ConfidentialityYes, or NULL encryption for integrity onlyNo
Integrity coversESP header, payload and trailerPayload plus immutable outer IP header fields
Works through NATYes, inside UDP 4500No
Requirement in RFC 4301Must be implementedMay be implemented
Header8 bytes (SPI and sequence number), plus IV, trailer and ICV12 bytes plus ICV
Packet layouts (field sizes from RFC 4303 and RFC 4302; IV and ICV sizes depend on the algorithm)
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 modeTransport mode
What is protectedThe whole original packet, header includedThe payload after the IP header
Addresses an observer seesThe two gateways onlyThe real source and destination
Extra IP headerYes, 20 bytes on IPv4No
Typical useSite-to-site and remote-access VPNsHost 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.

Input chain for an IPsec gateway, checked with nft 1.0.6; capture filter compiled with tcpdump 4.99.3; ip xfrm per iproute2 6.1
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 list

06IPsec 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 pathOverhead at full sizeInner MTUTCP MSS (IPv4)
Plain GRE, no encryption24 bytes1,4761,436
ESP tunnel mode, AES-GCM54 bytes1,4461,406
ESP tunnel mode, AES-GCM, behind NAT (UDP 4500)62 bytes1,4381,398
ESP tunnel mode, AES-CBC with HMAC-SHA-25662 bytes1,4381,398
GRE over IPsec, transport mode, AES-GCM58 bytes1,4421,402
GRE over IPsec, tunnel mode, AES-GCM78 bytes1,4221,382
WireGuard, IPv4 outer header60 bytes1,4401,400
Worked calculation for a 1,500-byte IPv4 path (TCP MSS = inner MTU - 40)
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) = 1420

07IPsec 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)WireGuardOpenVPNGRE
EncryptionNegotiated; AES-GCM mandatory to implementFixed: ChaCha20-Poly1305Negotiated, e.g. AES-GCMNone
AuthenticationIKEv2 with certificates, pre-shared keys or EAPNoise IK handshake with static public keysTLS with certificatesNone
On the wireUDP 500 and 4500, IP protocol 50One UDP port chosen by the operatorUDP 1194 by default, or TCPIP protocol 47
Through NATYes, via UDP 4500YesYesPoorly: no ports for NAT to track
Overhead, 1,500-byte IPv4 path54 to 78 bytes, as above60 bytesDepends on options; mssfix targets 1,492-byte packets by default24 bytes
SpecificationIETF RFCs 4301, 4303 and 7296Protocol published by its author, no RFCProject documentationRFC 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

IPsec is a set of protocols, and an IPsec VPN is one use of it. The same protocols can protect traffic between two hosts without any VPN, for example in transport mode between servers. When people say IPsec VPN they usually mean tunnel-mode ESP between two gateways, or between a device and a gateway, with IKEv2 handling authentication and keys.

IKEv2 is the key-management part of IPsec. It authenticates the two peers, negotiates algorithms and keys, and hands them to ESP, which protects the actual data packets. A VPN client that offers an IKEv2 option is offering IPsec with IKEv2; the encrypted traffic itself travels as ESP, usually inside UDP port 4500 on networks with NAT.

UDP 500 for IKE, UDP 4500 for IKE and ESP when NAT is detected, and IP protocol 50 for ESP without NAT. AH, rarely used, is IP protocol 51. Protocols 50 and 51 are not ports, so firewalls need protocol rules for them. Where UDP is blocked, RFC 9329 allows IKE and ESP to run over TCP port 4500.

Neither is simply more secure. Both use modern authenticated encryption when configured well. WireGuard fixes one set of algorithms and has a small configuration surface, which leaves fewer ways to get it wrong. IPsec negotiates algorithms and supports certificates and EAP, which suits large organisations and mixed vendors, but legacy options such as IKEv1 or 3DES must be switched off deliberately.

The usual causes are a firewall that allows UDP 500 and 4500 but blocks IP protocol 50, traffic selectors that cover different subnets from the ones being routed, or an MTU problem where small packets pass and large ones vanish. List the SAs and policies, capture ESP on the uplink while sending traffic, and test with large pings that are not allowed to fragment.

ESP does, using NAT traversal. IKEv2 detects the NAT during its first exchange, and both sides then switch to UDP port 4500 and wrap each ESP packet in a UDP header that the NAT device can track. AH does not, because it protects the very addresses that NAT has to change. Small keepalive packets hold the NAT mapping open while the tunnel is idle.

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.

  1. 01RFC 4301: Security Architecture for the Internet Protocol · RFC Editor
  2. 02RFC 4303: IP Encapsulating Security Payload (ESP) · RFC Editor
  3. 03RFC 4302: IP Authentication Header · RFC Editor
  4. 04RFC 7296: Internet Key Exchange Protocol Version 2 (IKEv2) · RFC Editor
  5. 05RFC 3948: UDP Encapsulation of IPsec ESP Packets · RFC Editor
  6. 06RFC 9329: TCP Encapsulation of IKE and IPsec Packets · RFC Editor
  7. 07RFC 4106: The Use of Galois/Counter Mode (GCM) in IPsec ESP · RFC Editor
  8. 08RFC 3602: The AES-CBC Cipher Algorithm and Its Use with IPsec · RFC Editor
  9. 09RFC 4868: Using HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 with IPsec · RFC Editor
  10. 10RFC 8221: Cryptographic Algorithm Implementation Requirements for ESP and AH · RFC Editor
  11. 11RFC 9395: Deprecation of the IKEv1 Protocol and Obsoleted Algorithms · RFC Editor
  12. 12RFC 8019: Protecting IKEv2 Implementations from Distributed Denial-of-Service Attacks · RFC Editor
  13. 13RFC 1825: Security Architecture for the Internet Protocol (1995) · RFC Editor
  14. 14RFC 2409: The Internet Key Exchange (IKE) · RFC Editor
  15. 15RFC 2784: Generic Routing Encapsulation (GRE) · RFC Editor
  16. 16Assigned Internet Protocol Numbers · IANA
  17. 17WireGuard protocol and cryptography · WireGuard
  18. 18Linux WireGuard driver: default MTU calculation (device.c) · Linux kernel source
  19. 19OpenVPN 2.6 manual (--port, --proto, --mssfix) · OpenVPN
  20. 20Resolve IPv4 Fragmentation, MTU, MSS, and PMTUD Issues with GRE and IPsec · Cisco

Last updated