networking, updated

IPsec (ESP, AH, IKE)

IPsec encrypts and authenticates IP packets, mostly for site-to-site and remote-access VPNs. IKE negotiates the keys, ESP carries the protected traffic.

OSI layer
3 Network
Carried in
IP (ESP, AH); UDP (IKE, NAT-T)
Ports
500/udp (IKE), 4500/udp (NAT-T)
Identified by
IP protocol 50 (ESP), 51 (AH)
Specs
RFC 4301, RFC 4303, RFC 4302, RFC 7296
Wireshark filter
esp || ah || isakmp
Encrypted
yes

Packet structure

ESP is what you see on almost every IPsec VPN:

IPsec ESP packet10 bytes + variable

Select a field to see what it does.

RFC 4303. IP protocol 50 (or UDP 4500 behind NAT). Everything after the sequence number up to the ICV is encrypted; with AEAD ciphers (AES-GCM) an IV comes first in the payload.

AH only authenticates and is rare today, because it cannot pass NAT:

IPsec AH header12 bytes + variable

Select a field to see what it does.

RFC 4302. IP protocol 51. Authenticates the payload and the unchanging parts of the IP header, but encrypts nothing. Breaks with NAT.

How it works

  1. IKE phase 1 / IKE_SA_INIT (UDP 500): the peers agree on algorithms, do a Diffie-Hellman exchange and authenticate each other (pre-shared key or certificates). Result: a protected IKE SA.
  2. IKE phase 2 / CREATE_CHILD_SA: inside that channel they negotiate the IPsec SAs (one per direction, each with an SPI) and the traffic selectors (which subnets go through the tunnel).
  3. Data: packets are wrapped in ESP. If NAT is detected, IKE moves to UDP 4500 and ESP is wrapped in UDP 4500 too (NAT traversal).
  4. Rekeying: SAs expire after a time or data limit and are replaced; you see new SPIs.
ModeWhat is protectedUsed for
TunnelThe whole original IP packet, inside a new IP headerSite-to-site and remote-access VPNs
TransportOnly the payload; the original IP header staysHost-to-host, L2TP/IPsec, GRE over IPsec

The IPsec VPN guide explains the setup and phases in more depth.

In Wireshark

FilterShows
isakmpIKE negotiation (v1 and v2)
isakmp.exchangetype == 34IKEv2 IKE_SA_INIT
isakmp.exchangetype == 2 || isakmp.exchangetype == 4IKEv1 main mode / aggressive mode
espESP traffic (incl. NAT-T on UDP 4500)
esp.spi == 0xc0ffee01One SA (one direction of one tunnel)
ahAH traffic
udp.port == 4500 && !espNAT-T keepalives (1 byte 0xff) and IKE after NAT detection

Wireshark can decrypt ESP and IKE if you enter the keys (Preferences › Protocols › ESP / ISAKMP), for example exported from strongSwan.

What normal looks like: a short IKE exchange, then ESP with increasing sequence numbers per SPI, rekeys every few hours. Repeated IKE_SA_INIT without completing points to a proposal mismatch or a wrong PSK.

Security notes

References

§ home
~/knubertus/home/
1,1 Top