Sub topic 8
IPsec: ESP, IKE and the Two Modes
This chapter assumes you know how encapsulation works (a packet carried as the payload of another packet), the basics of IP routing, and the three security goals of confidentiality, integrity and authentication. It also assumes you know what a key exchange does. Here you will see how IPsec combines these into a working VPN.
What IPsec is
IPsec is a suite of protocols that protects traffic at the IP layer. It is not one protocol. It is a set of parts that each do one job:
- ESP (Encapsulating Security Payload) protects the data packets.
- IKE (Internet Key Exchange, usually version 2) authenticates the peers and agrees on keys.
- The policy database decides which traffic gets protected.
- AH (Authentication Header) provides integrity without encryption. It is rarely used today, because ESP can provide integrity too and AH breaks when NAT rewrites addresses.
Because IPsec works at the IP layer, applications do not need to know about it. A web browser, a database client and a ping all get protected the same way.
Security associations
The central idea is the security association (SA). An SA is a one-way agreement between two peers. It records the protocol (ESP), the algorithms, the keys, a lifetime, and an identifier called the SPI (Security Parameter Index).
Traffic in each direction needs its own SA, so a working tunnel has at least two. The sender writes the SPI into each packet. The receiver uses the SPI, together with the destination address, to find the right keys.
There are two layers of SAs:
| Layer | Created by | Protects | Typical lifetime |
|---|---|---|---|
| IKE SA | IKE_SA_INIT and IKE_AUTH | IKE's own control messages | Hours to a day |
| Child SA | IKE_AUTH, then CREATE_CHILD_SA | The actual data traffic, using ESP | Minutes to hours |
Child SAs are rekeyed often, so a stolen key exposes only a limited amount of traffic.
ESP: protecting the data packets
ESP wraps each packet with a header, a trailer and an authentication value. Its fields are:
- SPI (32 bits): selects the SA.
- Sequence number (32 bits, or 64 bits when extended): lets the receiver detect replayed packets.
- Payload: the protected data. With encryption, this includes any needed initialisation vector.
- Padding, pad length, next header: align the data to the cipher's block size and say what protocol the payload holds. These are encrypted.
- ICV (Integrity Check Value): the authentication tag that proves the packet was not altered.
Modern deployments use an AEAD cipher such as AES-GCM or ChaCha20-Poly1305. An AEAD cipher encrypts and authenticates in one operation. ESP authenticates the SPI and sequence number even though it cannot encrypt them, because the receiver needs to read them first.
ESP sits directly on IP as protocol number 50. It is not TCP or UDP, so it has no port numbers. This matters later for NAT.
Anti-replay
The receiver keeps a sliding window of recently seen sequence numbers. A packet with a number already seen, or far older than the window, is dropped. This stops an attacker from recording a valid packet and sending it again. A side effect is that heavy packet reordering can cause legitimate packets to be dropped.
IKE: setting up the SAs
IKEv2 runs over UDP port 500. It is a request/response protocol. The first two exchanges build everything needed for a working tunnel:
IKESAINIT negotiates the algorithms and performs a Diffie-Hellman exchange. After it, both peers share secret keys, but neither has proved who it is. The Diffie-Hellman exchange gives forward secrecy: the session keys cannot be recovered later from a long-term key.
IKE_AUTH runs inside the encrypted IKE SA. Each side proves its identity with a pre-shared key, a certificate and signature, or EAP (used in remote-access setups with usernames and passwords). The same exchange creates the first Child SA and agrees on traffic selectors.
Traffic selectors describe which packets use a given Child SA, by address range, protocol and port. For a site-to-site link they might be 10.1.0.0/16 <-> 10.2.0.0/16. Each side checks that the other's selectors fall within its own policy.
Later, CREATECHILDSA rekeys a Child SA or adds another one. INFORMATIONAL messages carry deletes, liveness checks and error reports.
The two modes
IPsec can apply ESP in two ways. The difference is what ESP wraps.
Transport mode keeps the original IP header and protects only the payload. The endpoints of the IPsec session are the endpoints of the traffic. It is used for host-to-host protection and is the basis of L2TP/IPsec, where L2TP supplies the tunnel and IPsec protects it.
Tunnel mode encrypts the whole original packet, including its IP header, and adds a new outer IP header. The outer addresses are the two gateways. This is the mode used for site-to-site and most remote-access VPNs, because it can carry private addresses across the internet and hides the real source and destination from observers.
| Row | Transport mode | Tunnel mode |
|---|---|---|
| Original IP header | Visible | Encrypted inside |
| Extra overhead | Lower | Higher (a new 20-byte IPv4 header) |
| Typical use | Host to host | Gateway to gateway, client to gateway |
| Carries private subnets | No | Yes |
Tunnel mode ties back to the earlier chapter on overhead. Each packet grows by the outer header, the ESP header, the IV, padding, trailer and ICV. With AES-GCM over IPv4 this is typically 50 to 60 bytes, so the tunnel MTU is lower than the link MTU and you may need MSS clamping.
A minimal example
Here is a site-to-site tunnel in strongSwan's swanctl.conf format. It uses a pre-shared key for brevity. Certificates are the better choice in production.
connections {
site-to-site {
version = 2
local_addrs = 192.0.2.1
remote_addrs = 198.51.100.1
proposals = aes256gcm16-prfsha384-ecp384
local {
auth = psk
id = 192.0.2.1
}
remote {
auth = psk
id = 198.51.100.1
}
children {
net-net {
local_ts = 10.1.0.0/16
remote_ts = 10.2.0.0/16
esp_proposals = aes256gcm16-ecp384
start_action = start
}
}
}
}
secrets {
ike-1 {
id = 192.0.2.1
secret = "replace-with-a-long-random-string"
}
}The other gateway uses the same file with the local and remote values swapped. Load it with swanctl --load-all and check it with swanctl --list-sas.
Policy-based and route-based tunnels
There are two ways to decide what goes into a Child SA.
- Policy-based: the traffic selectors alone decide. Packets matching the selectors are encrypted. There is no interface, so routing and monitoring tools see nothing. Adding a subnet means renegotiating the SA.
- Route-based: the tunnel is a virtual interface (such as a VTI or XFRM interface), and the selectors are set wide open (
0.0.0.0/0). The routing table decides what enters the tunnel. This works with dynamic routing protocols and is easier to reason about.
Route-based setups reuse the idea from GRE: the tunnel looks like an ordinary interface. The difference is that the interface is protected by ESP.
Common errors
- Mismatched proposals. The log shows
NO_PROPOSAL_CHOSEN. Compare the algorithm lists on both sides. - Mismatched traffic selectors. IKEAUTH succeeds but the Child SA fails with `TSUNACCEPTABLE`. The two sides must agree on the subnets, and each must be a subset of the other's policy.
- Wrong identity. The ID a peer sends must match what the other side expects. Check
idvalues, especially behind NAT where the address differs from the identity. - Firewall blocks. IKE needs UDP 500, and ESP needs IP protocol 50. A firewall that allows only TCP and UDP will drop ESP. With NAT traversal, UDP 4500 is used instead.
- Clock skew with certificates. Certificates that appear expired or not yet valid fail authentication.
- Dead peer, stale SA. One side reboots and forgets the SAs. The other keeps sending ESP that is silently dropped. Dead peer detection (liveness checks in INFORMATIONAL messages) clears the stale state.
Summary of decisions
- Choose tunnel mode to connect networks or to give clients private addresses. Choose transport mode only when the two hosts are the real endpoints.
- Choose IKEv2 over IKEv1 for new work. It is simpler, supports mobility and has built-in NAT traversal.
- Choose AEAD ciphers and a Diffie-Hellman group that gives forward secrecy.
- Choose certificates over pre-shared keys when you have more than a few peers.
Exercise
A site-to-site IPsec tunnel shows an established IKE SA, but pings between the two subnets fail. ip xfrm state shows no Child SA, and the log contains TS_UNACCEPTABLE. What went wrong, and what do you check?
Show answer
IKESAINIT and IKEAUTH succeeded for the IKE SA, but the peers could not agree on traffic selectors for the Child SA. One side proposed subnets that do not fall within the other side's configured policy, for example 10.1.0.0/16 against 10.1.0.0/24. Compare localts and remote_ts on both gateways. They must mirror each other, and each side's proposal must fit inside the other's allowed ranges. Then reload the configuration and confirm a Child SA appears in swanctl --list-sas.
Exercise
Why does ESP in tunnel mode lower the usable MTU, and why can a plain NAT device break ESP unless NAT traversal is used?
Show answer
Tunnel mode adds a new outer IP header plus the ESP header, IV, padding, trailer and ICV to every packet, so the same link MTU leaves less room for the inner packet. NAT devices track connections by TCP or UDP port numbers. ESP is IP protocol 50 and has no ports, so a NAT cannot map multiple hosts to one address. NAT traversal solves this by wrapping ESP in UDP port 4500, which gives the NAT a port to translate.